diff options
Diffstat (limited to 'gs/base/genht.c')
-rw-r--r-- | gs/base/genht.c | 298 |
1 files changed, 149 insertions, 149 deletions
diff --git a/gs/base/genht.c b/gs/base/genht.c index 159877c9a..acd01b133 100644 --- a/gs/base/genht.c +++ b/gs/base/genht.c @@ -1,6 +1,6 @@ /* Copyright (C) 2001-2006 Artifex Software, Inc. All Rights Reserved. - + This software is provided AS-IS with no warranty, either express or implied. @@ -35,17 +35,17 @@ * in the file (there may be more than one) must follow this format, * where ... indicates arbitrary text: (optionally:) - /halftone_name ... - /HalftoneType 5 + /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 + /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 @@ -70,9 +70,9 @@ read_file(FILE *in, char *cname) 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; + fprintf(stderr, "Can't allocate %d bytes to read %s.\n", + len + 1, cname); + return 0; } rewind(in); nread = fread(cont, 1, len, in); @@ -88,23 +88,23 @@ parse_line(char **pstr, char **pline) top: while (*str && strchr(" \t\r\n", *str)) /* trim leading space */ - ++str; + ++str; if (*str == 0) { - *pline = 0; - return false; + *pline = 0; + return false; } *pline = str; while (*str && !strchr("\r\n", *str)) /* find EOL */ - ++str; + ++str; while (str > *pline && strchr(" \t", str[-1])) /* trim trailing space */ - --str; + --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 **pprefix, char **pcont) { char *str; char *line; @@ -116,110 +116,110 @@ parse_halftone(gx_device_halftone_resource_t *phtr, byte **pThresholds, /* Parse the file. */ for (str = *pcont; parse_line(&str, &line);) { - char *end; - char *prefix; - char terminator; + 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 (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; - } + 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 */ + return 1; /* end of definitions */ if (HalftoneType < 0) - fprintf(stderr, "HalftoneType not found.\n"); + fprintf(stderr, "HalftoneType not found.\n"); if (Width < 0) - fprintf(stderr, "Width not found.\n"); + fprintf(stderr, "Width not found.\n"); if (Height < 0) - fprintf(stderr, "Height not found.\n"); + fprintf(stderr, "Height not found.\n"); if (Thresholds == 0) - fprintf(stderr, "Thresholds not found.\n"); + fprintf(stderr, "Thresholds not found.\n"); if (rname == 0 || Thresholds == 0) - return -1; + return -1; phtr->rname = rname; phtr->HalftoneType = HalftoneType; phtr->Width = Width; @@ -232,7 +232,7 @@ parse_halftone(gx_device_halftone_resource_t *phtr, byte **pThresholds, /* Write a halftone as a C procedure. */ static int write_halftone(FILE *out, gx_device_halftone_resource_t *phtr, - const char *prefix, int index) + const char *prefix, int index) { int num_bits = phtr->Width * phtr->Height; int i; @@ -240,32 +240,32 @@ write_halftone(FILE *out, gx_device_halftone_resource_t *phtr, /* Write the initial comment. */ fputs("\n/* ", out); if (prefix) - fprintf(out, "%s.", 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]); + 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]); + 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); + index, phtr->rname, phtr->HalftoneType, phtr->Width, phtr->Height, + phtr->num_levels, index, index, + ht_order_procs_short.bit_data_elt_size); return 0; } @@ -288,60 +288,60 @@ main(int argc, char *argv[]) int index, i; if (argc != 3) { - fprintf(stderr, "Usage: genht ht_res.ps ht_data.c\n"); - exit(1); + 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); - } + 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); + exit(1); fclose(in); out = fopen(oname, "w"); if (out == 0) { - fprintf(stderr, "Can't open %s for output.\n", oname); - exit(1); + 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); + 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); + (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; + 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); + prefix); for (i = 0; i < index; ++i) - fprintf(out, "&res_%d, ", i); + fprintf(out, "&res_%d, ", i); fputs("0\n };\n return res;\n}\n", out); fclose(out); if (code < 0) - exit(1); + exit(1); return 0; } |