1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/* Copyright (C) 2001-2012 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134, San Rafael,
CA 94903, U.S.A., +1(415)492-9861, for further information.
*/
/* Generate C code for compiling halftones into ROM. */
#include "malloc_.h"
#include "stdio_.h"
#include "string_.h"
#include "gscdefs.h"
#include "gsmemory.h"
#include "gxbitmap.h"
#include "gxhttile.h"
#include "gxtmap.h"
#include "gxdht.h"
#include "gxdhtres.h"
#include "strimpl.h"
#include "sstring.h"
/*
* This program translates PostScript halftone resources into C data
* structures that can then be compiled into executables and put in shared,
* read-only memory. The syntax of the resource file is a subset of
* PostScript, tightly constrained so that it can be parsed easily. Blank
* lines and PostScript comments are ignored, but otherwise each halftone
* in the file (there may be more than one) must follow this format,
* where ... indicates arbitrary text:
(optionally:)
/halftone_name ...
/HalftoneType 5
(zero or more times:)
/halftone_name ...
/HalftoneType 3
/Width xxx
/Height xxx
/Thresholds ...
--hex data terminated by a >--
(zero or more times:)
/halftone_name 1 index
(finally)
... defineresource
* Lines that don't follow the above syntax may appear anywhere in the file
* except in the middle of the hex data. Width and Height must precede
* Thresholds, but otherwise the 4 parameters may appear in any order.
* White space at the beginning or end of a line is ignored, and any
* amount of white space may separate the parameter name from its value.
*
* We envision that this format will eventually be extended to cover
* HalftoneType 16 halftones with a single rectangle, allowing 16-bit
* threshold values.
*/
/* Read a source file into memory. */
static char *
read_file(FILE *in, char *cname)
{
int len, nread;
char *cont;
fseek(in, 0L, 2 /*SEEK_END*/);
len = ftell(in);
cont = malloc(len + 1);
if (cont == 0) {
fprintf(stderr, "Can't allocate %d bytes to read %s.\n",
len + 1, cname);
return 0;
}
rewind(in);
nread = fread(cont, 1, len, in);
cont[nread] = 0;
return cont;
}
/* Parse a Halftone resource file into memory. */
static bool
parse_line(char **pstr, char **pline)
{
char *str = *pstr;
top:
while (*str && strchr(" \t\r\n", *str)) /* trim leading space */
++str;
if (*str == 0) {
*pline = 0;
return false;
}
*pline = str;
while (*str && !strchr("\r\n", *str)) /* find EOL */
++str;
while (str > *pline && strchr(" \t", str[-1])) /* trim trailing space */
--str;
*str = 0;
*pstr = str + 1;
return true;
}
static int
parse_halftone(gx_device_halftone_resource_t *phtr, byte **pThresholds,
char **pprefix, char **pcont)
{
char *str;
char *line;
char *rname = 0;
int HalftoneType = -1;
int Width = -1, Height = -1;
byte *Thresholds = 0;
stream_AXD_state ss;
/* Parse the file. */
for (str = *pcont; parse_line(&str, &line);) {
char *end;
char *prefix;
char terminator;
if (line[0] == '%')
continue;
if (strlen(line) >= 14 &&
!strcmp(line + strlen(line) - 14, "defineresource")
)
break;
if (line[0] != '/')
continue;
if (strlen(line) >= 8 &&
!strcmp(line + strlen(line) - 8, " 1 index")
)
continue;
end = ++line;
while (*end && !strchr(" \t<", *end)) /* find end of name */
++end;
terminator = *end;
*end = 0;
if (rname == 0) { /* first name is halftone name */
rname = malloc(strlen(line) + 1);
strcpy(rname, line);
continue;
}
if (terminator == 0) /* name alone */
continue;
++end;
if (!strcmp(line, "HalftoneType")) {
if (sscanf(end, "%d", &HalftoneType) != 1) {
fprintf(stderr, "Invalid HalftoneType syntax: %s\n", line - 1);
return -1;
}
switch (HalftoneType) {
case 3:
break;
case 5:
if (*pprefix)
free(*pprefix);
*pprefix = rname;
rname = 0;
break;
default:
fprintf(stderr, "Invalid HalftoneType: %s\n", end);
return -1;
}
continue;
} else if (!strcmp(line, "Width")) {
if (sscanf(end, "%d", &Width) != 1 ||
Width <= 0 || Width > 0x4000
) {
fprintf(stderr, "Invalid Width: %s\n", end);
return -1;
}
} else if (!strcmp(line, "Height")) {
if (sscanf(end, "%d", &Height) != 1 ||
Height <= 0 || Height > 0x4000
) {
fprintf(stderr, "Invalid Height: %s\n", end);
return -1;
}
} else if (!strcmp(line, "Thresholds")) {
uint ignore;
uint num_levels = 256;
uint num_bits = Width * Height;
char *eol = end + strlen(end); /* skip rest of line */
stream_cursor_read r;
stream_cursor_write w;
if (Width < 0 || Height < 0) {
fprintf(stderr, "Width and Height must precede Thresholds.\n");
return -1;
}
phtr->num_levels = num_levels;
phtr->levels =
malloc(num_levels * sizeof(*phtr->levels));
phtr->bit_data =
malloc(num_bits * sizeof(ushort));
Thresholds = malloc(num_bits);
s_AXD_init_inline(&ss);
r.ptr = (const byte *)eol;
r.limit = (const byte *)eol + strlen(eol + 1);
w.ptr = Thresholds - 1;
w.limit = w.ptr + num_bits;
s_AXD_template.process((stream_state *)&ss, &r, &w, true);
str = (char *)r.ptr + 1;
break;
}
}
/* Check for successful parsing. */
if (rname == 0)
return 1; /* end of definitions */
if (HalftoneType < 0)
fprintf(stderr, "HalftoneType not found.\n");
if (Width < 0)
fprintf(stderr, "Width not found.\n");
if (Height < 0)
fprintf(stderr, "Height not found.\n");
if (Thresholds == 0)
fprintf(stderr, "Thresholds not found.\n");
if (rname == 0 || Thresholds == 0)
return -1;
phtr->rname = rname;
phtr->HalftoneType = HalftoneType;
phtr->Width = Width;
phtr->Height = Height;
*pThresholds = Thresholds;
*pcont = str;
return 0;
}
/* Write a halftone as a C procedure. */
static int
write_halftone(FILE *out, gx_device_halftone_resource_t *phtr,
const char *prefix, int index)
{
int num_bits = phtr->Width * phtr->Height;
int i;
/* Write the initial comment. */
fputs("\n/* ", out);
if (prefix)
fprintf(out, "%s.", prefix);
fprintf(out, "%s */\n", phtr->rname);
/* Write the levels array. */
fprintf(out, "static const unsigned int levels_%d[] = {", index);
for (i = 0; i < phtr->num_levels; ++i) {
if (i % 10 == 0)
fputs("\n", out);
fprintf(out, "%5u,", phtr->levels[i]);
}
fputs("\n0};\n", out);
/* Write the bit_data array. */
fprintf(out, "static const unsigned short bit_data_%d[] = {", index);
for (i = 0; i < num_bits; ++i) {
if (i % 10 == 0)
fputs("\n", out);
fprintf(out, "%5u,", ((const ushort *)phtr->bit_data)[i]);
}
fputs("\n0};\n", out);
/* Write the top-level structure. */
fprintf(out, "static const gx_device_halftone_resource_t res_%d = {\n \"%s\", %d, %d, %d, %d, levels_%d, bit_data_%d, %u\n};\n",
index, phtr->rname, phtr->HalftoneType, phtr->Width, phtr->Height,
phtr->num_levels, index, index,
ht_order_procs_short.bit_data_elt_size);
return 0;
}
/* Main program */
int
main(int argc, char *argv[])
{
char *iname;
FILE *in;
char *oname;
FILE *out;
int code;
char *cont;
char *line;
gx_device_halftone_resource_t res;
char *prefix = 0;
byte *Thresholds;
gx_ht_order order;
int index, i;
if (argc != 3) {
fprintf(stderr, "Usage: genht ht_res.ps ht_data.c\n");
exit(1);
}
iname = argv[1];
oname = argv[2];
in = fopen(iname, "rb");
if (in == 0) {
in = fopen(iname, "r");
if (in == 0) {
fprintf(stderr, "Can't read %s.\n", iname);
exit(1);
}
}
cont = read_file(in, iname);
if (cont == 0)
exit(1);
fclose(in);
out = fopen(oname, "w");
if (out == 0) {
fprintf(stderr, "Can't open %s for output.\n", oname);
exit(1);
}
fprintf(out, "/*\n * This file %s was generated from %s by genht.\n * Do not edit this file.\n *\n", oname, iname);
/* Copy initial comments from the input file. */
while (parse_line(&cont, &line) && line[0] == '%')
if (line[1] != '!')
fprintf(out, " * %s\n", line + 1);
cont[-1] = '\n';
cont = line;
fputs(" */\n#include \"gxdhtres.h\"\n", out);
for (index = 0;
(code = parse_halftone(&res, &Thresholds, &prefix, &cont)) == 0;
++index) {
order.width = res.Width;
order.num_levels = res.num_levels;
order.levels = (uint *)res.levels;
order.num_bits = res.Width * res.Height;
order.bit_data = (void *)res.bit_data;
ht_order_procs_short.construct_order(&order, Thresholds);
write_halftone(out, &res, prefix, index);
}
if (prefix == 0)
prefix = res.rname;
fputs("/* Check the prototype. */\n", out);
fprintf(out, "DEVICE_HALFTONE_RESOURCE_PROC(gs_dht_%s);\n", prefix);
fputs("\nconst gx_device_halftone_resource_t *const *\n", out);
fprintf(out, "gs_dht_%s(void)\n{\n static const gx_device_halftone_resource_t *const res[] = {\n\t",
prefix);
for (i = 0; i < index; ++i)
fprintf(out, "&res_%d, ", i);
fputs("0\n };\n return res;\n}\n", out);
fclose(out);
if (code < 0)
exit(1);
return 0;
}
/* Stubs */
ENUM_PTRS_BEGIN_PROC(gs_no_struct_enum_ptrs)
{
return 0;
ENUM_PTRS_END_PROC
}
RELOC_PTRS_BEGIN(gs_no_struct_reloc_ptrs)
{
}
RELOC_PTRS_END
public_st_stream_state();
void
gx_ht_complete_threshold_order(gx_ht_order *porder)
{
}
/*
* In order to avoid a linking step, we #include the required files here
* rather than compiling them separately.
*/
#include "gxhtbit.c"
#include "scantab.c"
#include "sstring.c"
const gx_dht_proc gx_device_halftone_list[] = { 0 };
|