Debian Codegraph @

contrib/pcl3/src/pcl3opts.c

raw
1 /******************************************************************************
2 File: $Id: pcl3opts.c,v 1.17 2001/05/31 15:19:16 Martin Rel $
3 Contents: Program to convert information in PCL-3+ files into options to be
4 used for the ghostscript device "pcl3" in order to produce a
5 file using a similar configuration
6 Author: Martin Lottermoser, Greifswaldstrasse 28, 38124 Braunschweig,
7 Germany; e-mail: [email protected].
8
9 *******************************************************************************
10 * *
11 * Copyright (C) 1999, 2000, 2001 by Martin Lottermoser *
12 * All rights reserved *
13 * *
14 ******************************************************************************/
15
16 /*****************************************************************************/
17
18 #ifndef _XOPEN_SOURCE
19 #define _XOPEN_SOURCE 500
20 #endif
21
22 /* Standard headers */
23 #include <assert.h>
24 #include <errno.h>
25 #include <locale.h>
26 #include <nl_types.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 /* prevent gp.h redefining fopen */
34 #define fopen fopen
35
36 /* Application headers */
37 #include "pclsize.h"
38 #include "pclgen.h"
39 #include "pclscan.h"
40
41 /*****************************************************************************/
42
43 #define check(p) \
44 if ((p) == NULL) { \
45 emessage(9, "Memory allocation failure: %s.\n", strerror(errno)); \
46 return -1; \
47 }
48
49 /*****************************************************************************/
50
51 /* Global variables */
52
53 static const char *progname; /* The program's name in messages */
54 static nl_catd catd = (nl_catd)(-1); /* NLS message catalogue descriptor */
55
56 /*****************************************************************************/
57
58 static void message(gp_file *f, int msg_id, const char *fmt, va_list ap)
59 {
60 vfprintf(f, catd == (nl_catd)(-1)? fmt: catgets(catd, 2, msg_id, fmt), ap);
61
62 return;
63 }
64
65 /*****************************************************************************/
66
67 static void emessage(int msg_id, const char *fmt, ...)
68 {
69 va_list ap;
70
71 fprintf(stderr, "? %s: ", progname);
72 va_start(ap, fmt);
73 message(stderr, msg_id, fmt, ap);
74 va_end(ap);
75
76 return;
77 }
78
79 /*****************************************************************************/
80
81 static void imessage(gp_file *f, int msg_id, const char *fmt, ...)
82 {
83 va_list ap;
84
85 va_start(ap, fmt);
86 message(f, msg_id, fmt, ap);
87 va_end(ap);
88
89 return;
90 }
91
92 /*****************************************************************************/
93
94 static void check_line_length(gp_file *out, int *line_length, int expected)
95 {
96 if (*line_length + expected <= 78) return;
97
98 fputs(" \\\n", out);
99 fputs(" ", out);
100 *line_length = 4;
101
102 return;
103 }
104
105 /*****************************************************************************/
106
107 /* Type for state and information collected during parsing a file */
108 typedef struct {
109 gp_file *out;
110
111 /* Capability */
112 pcl_bool
113 seen_ERG_B, /* End Raster Graphics */
114 seen_ERG_C,
115 seen_RGR, /* Raster Graphics Resolution */
116 seen_CRD, /* Configure Raster Data */
117 seen_old_quality,
118 seen_new_quality,
119 seen_unknown_compression;
120
121 /* State */
122 int
123 first_colorant_planes,
124 next_plane,
125 number_of_outputs;
126 pcl_FileData
127 fdata,
128 fdata_old;
129 pcl_bool
130 CRD_active,
131 seen_first_colorant,
132 seen_raster_data;
133 } CollectedInfo;
134
135 /*****************************************************************************/
136
137 static void print_result(CollectedInfo *ip) /* NLS: 10, 70 */
138 {
139 pcl_bool
140 different_resolutions = FALSE,
141 different_non_black_levels = FALSE;
142 pcl_Compression
143 default_compression;
144 int j;
145 int ll; /* line length */
146 unsigned int min_hres, min_vres, black_levels, non_black_levels, start;
147 pcl3_sizetable sizetable = { 0 };
148
149 if (ip->number_of_outputs > 0)
150 imessage(ip->out, 10,
151 "\nThe previous raster data section is followed by one with a different\n"
152 "configuration.\n");
153 ip->number_of_outputs++;
154
155 /* Find the smallest resolutions and look for different resolutions */
156 min_hres = ip->fdata.colorant_array[0].hres;
157 min_vres = ip->fdata.colorant_array[0].vres;
158 for (j = 1; j < ip->fdata.number_of_colorants; j++) {
159 int
160 h = ip->fdata.colorant_array[j].hres,
161 v = ip->fdata.colorant_array[j].vres;
162 if (h != min_hres || v != min_vres) different_resolutions = TRUE;
163 if (h < min_hres) min_hres = h;
164 if (v < min_vres) min_vres = v;
165 }
166
167 black_levels = ip->fdata.colorant_array[0].levels;
168
169 /* Check for deviations in the non-black levels */
170 start = (ip->fdata.palette == pcl_CMY || ip->fdata.palette == pcl_RGB? 0: 1);
171 non_black_levels = ip->fdata.colorant_array[start].levels;
172 for (j = 1; j < 3; j++)
173 if (ip->fdata.colorant_array[start + j].levels != non_black_levels)
174 different_non_black_levels = TRUE;
175
176 if (different_resolutions || different_non_black_levels) {
177 imessage(ip->out, 11,
178 "I've found a raster data section which uses a combination of "
179 "resolutions\n"
180 "and intensity levels which pcl3 cannot reproduce:\n");
181 for (j = 0; j < ip->fdata.number_of_colorants; j++)
182 imessage(ip->out, 12, " colorant %d: %4d x %4d ppi %2d levels\n",
183 j + 1,
184 ip->fdata.colorant_array[j].hres, ip->fdata.colorant_array[j].vres,
185 ip->fdata.colorant_array[j].levels);
186 if (ip->fdata.palette == pcl_CMYK &&
187 non_black_levels > 2 && black_levels < 4)
188 black_levels = 4;
189 }
190
191 imessage(ip->out, 13,
192 "My best guess for the command line needed to generate a similar file "
193 "is:\n");
194
195 /* Device selection */
196 ll = -1;
197 ll += fprintf(ip->out, "\n gs -sDEVICE=pcl3 -sSubdevice=");
198 default_compression = pcl_cm_tiff;
199 if (ip->seen_CRD || ip->seen_new_quality)
200 ll += fprintf(ip->out, "unspec");
201 else if (ip->seen_ERG_B && !ip->seen_ERG_C && ip->fdata.shingling == -1 &&
202 (ip->fdata.palette == pcl_no_palette || ip->fdata.palette == pcl_black)) {
203 /* This command line can still fail because of, e.g., resolution. */
204 ll += fprintf(ip->out, "hpdj500");
205 default_compression = pcl_cm_delta;
206 }
207 else
208 ll += fprintf(ip->out, "unspecold");
209
210 /* Special global language features */
211 if (ip->fdata.NULs_to_send > 0) {
212 check_line_length(ip->out, &ll, sizeof("-dSendNULs=9600"));
213 /* Note the missing SP which is instead accounted for by NUL. */
214 ll += fprintf(ip->out, " -dSendNULs=%d", ip->fdata.NULs_to_send);
215 }
216 if (ip->fdata.PJL_job != NULL) {
217 check_line_length(ip->out, &ll,
218 sizeof("-sPJLJob=") + strlen(ip->fdata.PJL_job));
219 ll += fprintf(ip->out, " -sPJLJob=%s", ip->fdata.PJL_job);
220 }
221 if (ip->fdata.PJL_language != NULL) {
222 check_line_length(ip->out, &ll,
223 sizeof("-sPJLLanguage=") + strlen(ip->fdata.PJL_language));
224 ll += fprintf(ip->out, " -sPJLLanguage=%s", ip->fdata.PJL_language);
225 }
226 if (ip->seen_CRD && !ip->seen_RGR) {
227 check_line_length(ip->out, &ll, sizeof("-dOnlyCRD"));
228 ll += fprintf(ip->out, " -dOnlyCRD");
229 }
230
231 /* Media handling except media source */
232 if (ip->fdata.manual_feed) {
233 check_line_length(ip->out, &ll, sizeof("-dManualFeed"));
234 ll += fprintf(ip->out, " -dManualFeed");
235 }
236 if (ip->fdata.duplex > 0) {
237 const char s[] = " -dDuplex";
238
239 if (ip->fdata.duplex == 1) {
240 const char s[] = " -sDuplexCapability=sameLeadingEdge";
241 check_line_length(ip->out, &ll, sizeof(s) - 1);
242 ll += fprintf(ip->out, s);
243 }
244 else if (ip->fdata.duplex == 2) {
245 const char s[] = " -sDuplexCapability=oppositeLeadingEdge";
246 check_line_length(ip->out, &ll, sizeof(s) - 1);
247 ll += fprintf(ip->out, s);
248 }
249 check_line_length(ip->out, &ll, sizeof(s) - 1);
250 ll += fprintf(ip->out, s);
251 }
252 if (ip->fdata.dry_time >= 0) {
253 check_line_length(ip->out, &ll, sizeof("-dDryTime=") + 4);
254 ll += fprintf(ip->out, " -dDryTime=%d", ip->fdata.dry_time);
255 }
256
257 /* Resolution */
258 if (min_hres != 0) {
259 check_line_length(ip->out, &ll, 12);
260 ll += fprintf(ip->out, " -r%u", min_hres);
261 if (min_vres != min_hres) ll += fprintf(ip->out, "x%u", min_vres);
262 }
263
264 /* Colour model */
265 if (ip->fdata.palette != pcl_no_palette) {
266 check_line_length(ip->out, &ll, sizeof("-sColourModel=CMY+K"));
267 ll += fprintf(ip->out, " -sColourModel=");
268 switch (ip->fdata.palette) {
269 case pcl_black:
270 ll += fprintf(ip->out, "Gray"); break;
271 case pcl_RGB:
272 ll += fprintf(ip->out, "RGB"); break;
273 case pcl_CMY:
274 ll += fprintf(ip->out, "CMY"); break;
275 case pcl_CMYK:
276 if (ip->seen_new_quality || ip->seen_CRD) /* Guess */
277 ll += fprintf(ip->out, "CMYK");
278 else
279 ll += fprintf(ip->out, "CMY+K");
280 break;
281 default:
282 assert(0); /* bug guard */
283 }
284 }
285
286 /* Intensity levels */
287 if (ip->fdata.palette == pcl_CMY) {
288 if (non_black_levels > 2) {
289 check_line_length(ip->out, &ll, sizeof("-dCMYLevels=") + 2);
290 ll += fprintf(ip->out, " -dCMYLevels=%d", non_black_levels);
291 }
292 }
293 else {
294 if (black_levels > 2) {
295 check_line_length(ip->out, &ll, sizeof("-dBlackLevels=") + 2);
296 ll += fprintf(ip->out, " -dBlackLevels=%d", black_levels);
297 }
298 if (ip->fdata.palette == pcl_CMYK && non_black_levels > 2) {
299 check_line_length(ip->out, &ll, sizeof("-dCMYLevels=") + 2);
300 ll += fprintf(ip->out, " -dCMYLevels=%d", non_black_levels);
301 }
302 }
303
304 /* Print quality, possibly also medium */
305 if (ip->seen_new_quality) {
306 check_line_length(ip->out, &ll, sizeof("-sPrintQuality=presentation"));
307 ll += fprintf(ip->out, " -sPrintQuality=");
308 switch (ip->fdata.print_quality) {
309 case -1:
310 ll += fprintf(ip->out, "draft"); break;
311 case 0:
312 ll += fprintf(ip->out, "normal"); break;
313 case 1:
314 ll += fprintf(ip->out, "presentation"); break;
315 default:
316 ll += fprintf(ip->out, "%d", ip->fdata.print_quality);
317 }
318
319 check_line_length(ip->out, &ll,
320 sizeof("-sMedium='quick dry transparency'"));
321 ll += fprintf(ip->out, " -sMedium=");
322 switch (ip->fdata.media_type) {
323 case 0:
324 ll += fprintf(ip->out, "plain"); break;
325 case 1:
326 ll += fprintf(ip->out, "bond"); break;
327 case 2:
328 ll += fprintf(ip->out, "Premium"); break;
329 case 3:
330 ll += fprintf(ip->out, "glossy"); break;
331 case 4:
332 ll += fprintf(ip->out, "transparency"); break;
333 case 5:
334 ll += fprintf(ip->out, "'quick dry glossy'"); break;
335 case 6:
336 ll += fprintf(ip->out, "'quick dry transparency'"); break;
337 default:
338 ll += fprintf(ip->out, "%d", ip->fdata.media_type);
339 }
340 }
341 else if (ip->seen_old_quality) {
342 if (ip->fdata.depletion != 0) {
343 check_line_length(ip->out, &ll, sizeof("-dDepletion=") + 2);
344 ll += fprintf(ip->out, " -dDepletion=%d", ip->fdata.depletion);
345 }
346 if (ip->fdata.shingling != -1) {
347 check_line_length(ip->out, &ll, sizeof("-dShingling=") + 2);
348 ll += fprintf(ip->out, " -dShingling=%d", ip->fdata.shingling);
349 }
350 if (ip->fdata.raster_graphics_quality != -1) {
351 check_line_length(ip->out, &ll,
352 sizeof("-dRasterGraphicsQuality=") + 2);
353 ll += fprintf(ip->out, " -dRasterGraphicsQuality=%d",
354 ip->fdata.raster_graphics_quality);
355 }
356 }
357
358 /* Compression. We display this option only if the value is different from
359 what pcl3 would otherwise use. */
360 if (ip->fdata.compression != default_compression) {
361 check_line_length(ip->out, &ll, sizeof("-dCompressionMethod=") + 1);
362 ll += fprintf(ip->out, " -dCompressionMethod=%d", ip->fdata.compression);
363 }
364
365 fputs("\n\n", ip->out);
366 if (ip->fdata.palette == pcl_CMYK && !ip->seen_first_colorant) {
367 imessage(ip->out, 70,
368 "Although the colour model in this file is declared as CMYK, "
369 "the file does not\n"
370 "use the black colorant. If you find that the printer does not accept "
371 "a file\n"
372 "generated with the command line above, "
373 "try the CMY colour model instead.\n\n");
374 }
375
376 if (ip->fdata.size != 0) {
377 ms_MediaCode media_code = pcl3_media_code(&sizetable, ip->fdata.size);
378 const ms_SizeDescription *size = ms_find_size_from_code(media_code);
379 if (size == NULL)
380 imessage(ip->out, 14,
381 "This file uses a PCL page size code (%d) which is unknown to pcl3.\n",
382 (int)ip->fdata.size);
383 else {
384 imessage(ip->out, 15, "The page size is set to %s", size->name);
385 /* I'm using the size specification as an indication of the PCL Page Size
386 code used, hence I'm adding the card flag if present. Note that
387 adding "Big" is not useful. */
388 if (media_code & PCL_CARD_FLAG) fputs(PCL_CARD_STRING, ip->out);
389 if (size->dimen[0] > 0) {
390 const char *unit = catgets(catd, 2, 16, "mm");
391
392 fputs(" (", ip->out);
393 if (strcmp(unit, "bp") == 0)
394 fprintf(ip->out, "%.0f x %.0f bp", size->dimen[0],
395 size->dimen[1]);
396 else if (strcmp(unit, "in") == 0)
397 fprintf(ip->out, "%.1f x %.1f in", size->dimen[0]/BP_PER_IN,
398 size->dimen[1]/BP_PER_IN);
399 else
400 fprintf(ip->out, "%.0f x %.0f mm", size->dimen[0]/BP_PER_MM,
401 size->dimen[1]/BP_PER_MM);
402 fputc(')', ip->out);
403 }
404 fputs(".\n", ip->out);
405 }
406 }
407 if (ip->fdata.media_source != 0 && ip->fdata.media_source != 1)
408 imessage(ip->out, 19, "The media source (input tray) selected is %d.\n",
409 ip->fdata.media_source);
410 if (ip->fdata.media_destination != 0)
411 imessage(ip->out, 17,
412 "The media destination (output tray) selected is %d.\n",
413 ip->fdata.media_destination);
414 if (ip->fdata.media_source != 0 && ip->fdata.media_source != 1 ||
415 ip->fdata.media_destination != 0)
416 imessage(ip->out, 18,
417 "To be able to select a media position (input or output) you will have "
418 "to\n"
419 "configure the `InputAttributes' or `OutputAttributes' dictionaries\n"
420 "appropriately. See the manual page gs-pcl3(1).\n");
421
422 return;
423 }
424
425 /*****************************************************************************/
426
427 static int action_PageSize(gp_file *in, const pcl_Command *cmd, void *i)
428 {
429 CollectedInfo *ip = i;
430
431 ip->fdata.size = cmd->i;
432
433 return 0;
434 }
435
436 /*****************************************************************************/
437
438 static int action_dry_time(gp_file *in, const pcl_Command *cmd, void *i)
439 {
440 CollectedInfo *ip = i;
441
442 ip->fdata.dry_time = cmd->i;
443
444 return 0;
445 }
446
447 /*****************************************************************************/
448
449 static int action_destination(gp_file *in, const pcl_Command *cmd, void *i)
450 {
451 CollectedInfo *ip = i;
452
453 ip->fdata.media_destination = cmd->i;
454
455 return 0;
456 }
457
458 /*****************************************************************************/
459
460 static int action_source(gp_file *in, const pcl_Command *cmd, void *i)
461 {
462 CollectedInfo *ip = i;
463
464 if (cmd->i != 0 && cmd->i != -2) {
465 if (cmd->i == 2) ip->fdata.manual_feed = TRUE;
466 else ip->fdata.media_source = cmd->i;
467 }
468
469 return 0;
470 }
471
472 /*****************************************************************************/
473
474 static int action_media_type(gp_file *in, const pcl_Command *cmd, void *i)
475 {
476 CollectedInfo *ip = i;
477
478 ip->seen_new_quality = TRUE;
479 ip->fdata.media_type = cmd->i;
480
481 return 0;
482 }
483
484 /*****************************************************************************/
485
486 static int action_duplex(gp_file *in, const pcl_Command *cmd, void *i)
487 { /* NLS: 60 */
488 CollectedInfo *ip = i;
489
490 ip->fdata.duplex = cmd->i;
491 if (ip->fdata.duplex < 0 || 2 < ip->fdata.duplex)
492 imessage(ip->out, 60, "I've found an unknown value for the command to "
493 "select\nsimplex/duplex printing: %d.\n", ip->fdata.duplex);
494
495 return 0;
496 }
497
498 /*****************************************************************************/
499
500 /* PJL white space. Note: no expressions with side effects permitted. */
501 #define PJL_ws(c) ((c) == ' ' || (c) == '\t')
502
503 /******************************************************************************
504
505 Function to check "@PJL" prefix
506
507 Return codes:
508 0: starts correctly, prefix has been read,
509 1: starts wrong, read characters have been pushed back,
510 -1: starts wrong, read characters could not be pushed back and an error
511 message has been issued.
512 */
513
514 static int check_prefix(gp_file *in)
515 {
516 static const pcl_Octet prefix[] = "@PJL";
517 /* Note that the "@PJL" prefix is required to be uppercase in PJL. */
518 int c, j;
519
520 /* Check for prefix followed by white space or EOL */
521 j = 0; /* number of matching characters read */
522 while (j < 4 && (c = fgetc(in)) == prefix[j]) j++;
523 if (j == 4) {
524 c = fgetc(in);
525 if (PJL_ws(c) || c == '\r' || c == '\n') j++;
526 /* I'm assuming that we're dealing with legal PJL, i.e., the CR is followed
527 by LF. It is not the responsibility of this program to discover PJL
528 syntax errors. */
529 }
530 if (j < 5) {
531 /* Keep in mind that one byte of push-back is guaranteed to work. */
532 if (c != EOF) ungetc(c, in);
533 while (j > 0) {
534 j--;
535 if (ungetc(prefix[j], in) == EOF) {
536 emessage(80,
537 "Error trying to push back characters while parsing PJL:\n %s.\n",
538 strerror(errno));
539 /* This should only happen for unprofessional PJL, i.e., without a
540 terminating ENTER LANGUAGE. */
541 return -1;
542 }
543 }
544
545 return 1;
546 }
547
548 /* Ensure we've not read past the end of the line */
549 if (c == '\n') ungetc(c, in);
550
551 return 0;
552 }
553
554 /*****************************************************************************/
555
556 #define PJL_letter(c) ('a' <= (c) && (c) <= 'z' || 'A' <= (c) && (c) <= 'Z')
557
558 /*****************************************************************************/
559
560 #define TILE_SIZE 200
561
562 static int action_UEL(gp_file *in, const pcl_Command *cmd, void *i)
563 {
564 CollectedInfo *ip = i;
565 int c;
566 pcl_Octet *buffer, *s;
567 size_t allocated, used;
568
569 if (cmd->i != 12345) return 1;
570
571 /* As we have entered PJL, the previous personality is irrelevant. */
572 if (ip->fdata.PJL_language != NULL) {
573 free(ip->fdata.PJL_language); ip->fdata.PJL_language = NULL;
574 }
575
576 buffer = (pcl_Octet *)malloc(TILE_SIZE);
577 check(buffer);
578 allocated = TILE_SIZE;
579
580 /* Read one PJL command at a time (necessary in order to catch ENTER) */
581 do {
582 c = check_prefix(in);
583 if (c != 0) c = EOF;
584 else {
585 /* Read up to EOL or EOF */
586 used = 0;
587 while ((c = fgetc(in)) != EOF && c != '\n') {
588 if (used >= allocated - 1) {
589 allocated += TILE_SIZE;
590 buffer = (pcl_Octet *)realloc(buffer, allocated);
591 check(buffer);
592 }
593 buffer[used] = c;
594 used++;
595 }
596 /* Remove an optional trailing CR */
597 if (used > 0 && buffer[used-1] == 'r') used--;
598 buffer[used] = '\0';
599
600 /* Skip optional whitespace */
601 s = buffer;
602 while (PJL_ws(*s)) s++;
603
604 if (PJL_letter(*s)) {
605 pcl_Octet *t;
606 int l;
607
608 /* Isolate command and convert to upper case. Note that we're running
609 in an internationalized environment, hence we can't use toupper(). */
610 t = s;
611 do {
612 if ('a' <= *t && *t <= 'z') *t -= 'a' - 'A';
613 t++;
614 } while (*t != '\0' && !PJL_ws(*t));
615
616 /* Identify command */
617 l = t - s;
618 #if 0
619 fprintf(stderr, "Command (length %d): `", l);
620 fwrite(s, 1, l, stderr);
621 fprintf(stderr, "'.\n");
622 #endif
623 if (l == sizeof("ENTER") - 1 && strncmp(s, "ENTER", l) == 0 &&
624 PJL_ws(*t)) {
625 /* Check for "LANGUAGE" */
626 s = t + 1;
627 while (PJL_ws(*s)) s++;
628 t = s;
629 while (*t != '\0' && !PJL_ws(*t) && *t != '=') {
630 if ('a' <= *t && *t <= 'z') *t -= 'a' - 'A';
631 t++;
632 }
633 l = t - s;
634 if (l == sizeof("LANGUAGE") - 1 && strncmp(s, "LANGUAGE", t - s) == 0
635 && (PJL_ws(*t) || *t == '=')) {
636 s = strchr(t, '='); /* again assuming legal PJL */
637 if (s != NULL) {
638 s++;
639 while (PJL_ws(*s)) s++;
640 if (PJL_letter(*s)){
641 t = s + 1;
642 while (*t != '\0' && !PJL_ws(*t)) t++;
643 ip->fdata.PJL_language = (char *)malloc(t - s + 1);
644 check(ip->fdata.PJL_language);
645 strncpy(ip->fdata.PJL_language, s, t - s);
646 ip->fdata.PJL_language[t - s] = '\0';
647 }
648 }
649 c = EOF; /* exit PJL and this loop */
650 }
651 }
652 else if (l == 3 && strncmp(s, "JOB", 3) == 0) {
653 /* Don't bother to identify the job name; let the user create an
654 unnamed job. */
655 static char jobname[] = "";
656 ip->fdata.PJL_job = jobname;
657 }
658 }
659 }
660 }
661 while (c != EOF);
662
663 free(buffer);
664
665 return 0;
666 }
667
668 /*****************************************************************************/
669
670 /* NLS: 20 */
671 static int action_compression(gp_file *in, const pcl_Command *cmd, void *i)
672 {
673 CollectedInfo *ip = i;
674
675 if (cmd->i != pcl_cm_none && cmd->i != pcl_cm_rl && cmd->i != pcl_cm_tiff &&
676 cmd->i != pcl_cm_delta && cmd->i != pcl_cm_crdr) {
677 if (!ip->seen_unknown_compression)
678 imessage(ip->out, 20,
679 "This file uses at least one compression method for raster data (%d)\n"
680 "which pcl3 does not know.\n", cmd->i);
681 ip->seen_unknown_compression = TRUE;
682 }
683 else {
684 if (cmd->i > ip->fdata.compression) ip->fdata.compression = cmd->i;
685 }
686
687 return 0;
688 }
689
690 /*****************************************************************************/
691
692 /* The following function is only called for "*bV" and "*bW". */
693
694 static int action_raster_data(gp_file *in, const pcl_Command *cmd, void *i)
695 {
696 CollectedInfo *ip = i;
697 int j;
698
699 ip->seen_raster_data = TRUE;
700 if (!ip->seen_first_colorant && cmd->i > 0 &&
701 ip->next_plane < ip->first_colorant_planes)
702 ip->seen_first_colorant = TRUE;
703
704 for (j = cmd->i; j > 0; j--) fgetc(in);
705 if (cmd->chars[2] == 'W') ip->next_plane = 0; else ip->next_plane++;
706
707 return 0;
708 }
709
710 /*****************************************************************************/
711
712 #define two_octets(index) (buffer[index]*256 + buffer[(index) + 1])
713
714 /*****************************************************************************/
715
716 static void print_CRD(gp_file *out, const pcl_Octet *buffer)
717 {
718 int j;
719
720 for (j = 0; j < buffer[1]; j++)
721 imessage(out, 12, " colorant %d: %4d x %4d ppi %2d levels\n",
722 j + 1, two_octets(2 + 6*j), two_octets(4 + 6*j),
723 two_octets(6 + 6*j));
724
725 return;
726 }
727
728 /*****************************************************************************/
729
730 static int action_CRD(gp_file *in, const pcl_Command *cmd, void *i) /* NLS: 30 */
731 {
732 CollectedInfo *ip = i;
733 pcl_Octet *buffer;
734 int errors = 0;
735
736 ip->seen_CRD = TRUE;
737
738 if (cmd->i <= 0) {
739 ip->CRD_active = FALSE;
740 ip->fdata.palette = pcl_no_palette;
741 ip->fdata.number_of_colorants = 1;
742 ip->fdata.colorant_array[0].hres = ip->fdata.colorant_array[0].vres = 75;
743 ip->fdata.colorant_array[0].levels = 2;
744 ip->first_colorant_planes = 1;
745 return 0;
746 }
747 ip->CRD_active = TRUE;
748
749 buffer = (pcl_Octet *)malloc(cmd->i);
750 check(buffer);
751 fread(buffer, sizeof(pcl_Octet), cmd->i, in);
752
753 if (buffer[0] == 2) {
754 /* Just occasionally, a little paranoia is good for the soul, let alone for
755 for the reliability of the program! */
756 if (2 + buffer[1]*6 != cmd->i) {
757 emessage(30, "Illegal field length in Configure Raster Data command.\n");
758 free(buffer);
759 return -1;
760 }
761
762 if (buffer[1] == 1 || buffer[1] == 3 || buffer[1] == 4) {
763 int j;
764
765 ip->fdata.number_of_colorants = buffer[1];
766 switch (ip->fdata.number_of_colorants) {
767 case 1:
768 ip->fdata.palette = pcl_black; break;
769 case 3:
770 ip->fdata.palette = pcl_CMY; break;
771 case 4:
772 ip->fdata.palette = pcl_CMYK; break;
773 }
774
775 for (j = 0; j < ip->fdata.number_of_colorants; j++) {
776 ip->fdata.colorant_array[j].hres = two_octets(2 + 6*j);
777 ip->fdata.colorant_array[j].vres = two_octets(4 + 6*j);
778 ip->fdata.colorant_array[j].levels = two_octets(6 + 6*j);
779 }
780
781 {
782 int power = 1;
783 ip->first_colorant_planes = 0;
784
785 while (power < ip->fdata.colorant_array[0].levels) {
786 power *= 2;
787 ip->first_colorant_planes++;
788 }
789 }
790 }
791 else {
792 emessage(31,
793 "This file contains a Configure Raster Data command with an\n"
794 " illegal number of colorants (%d) and the following data:\n",
795 buffer[1]);
796 print_CRD(stderr, buffer);
797 errors++;
798 }
799 }
800 else {
801 emessage(32,
802 "This file contains a Configure Raster Data command with an\n"
803 " unknown format (%d).\n",
804 (int)buffer[0]);
805 if (cmd->i > 2 && cmd->i == 2 + buffer[1]*6) {
806 imessage(stderr, 33, " It *might* mean the following:\n");
807 print_CRD(stderr, buffer);
808 imessage(stderr, 34, " This format is not supported by pcl3.\n");
809 }
810 errors++;
811 }
812 free(buffer);
813
814 return errors > 0? -1: 0;
815 }
816
817 /*****************************************************************************/
818
819 static int action_old_quality(gp_file *in, const pcl_Command *cmd, void *i)
820 {
821 CollectedInfo *ip = i;
822
823 ip->seen_old_quality = TRUE;
824 if (cmd->chars[1] == 'o') {
825 if (cmd->chars[2] == 'D') ip->fdata.depletion = cmd->i;
826 else if (cmd->chars[2] == 'Q') ip->fdata.shingling = cmd->i;
827 }
828 else if (cmd->chars[1] == 'r' && cmd->chars[2] == 'Q')
829 ip->fdata.raster_graphics_quality = cmd->i;
830
831 return 0;
832 }
833
834 /*****************************************************************************/
835
836 static int action_quality(gp_file *in, const pcl_Command *cmd, void *i)
837 {
838 CollectedInfo *ip = i;
839
840 ip->seen_new_quality = TRUE;
841 ip->fdata.print_quality = cmd->i;
842
843 return 0;
844 }
845
846 /*****************************************************************************/
847
848 /* NLS: 40 */
849 static int action_colour(gp_file *in, const pcl_Command *cmd, void *i)
850 {
851 CollectedInfo *ip = i;
852
853 if (!ip->CRD_active) {
854 switch (cmd->i) {
855 case -4:
856 ip->fdata.palette = pcl_CMYK; break;
857 case -3:
858 ip->fdata.palette = pcl_CMY; break;
859 case -1:
860 /*FALLTHROUGH*/
861 case 0:
862 /*FALLTHROUGH*/
863 case 1:
864 ip->fdata.palette = pcl_black; break;
865 case 3:
866 ip->fdata.palette = pcl_RGB; break;
867 default:
868 emessage(41, "This file uses a palette (%d) not supported by pcl3.\n",
869 cmd->i);
870 return -1;
871 }
872 ip->fdata.number_of_colorants = abs(cmd->i);
873
874 {
875 int j;
876
877 for (j = 0; j < ip->fdata.number_of_colorants; j++) {
878 ip->fdata.colorant_array[j].hres = ip->fdata.colorant_array[j].vres =
879 ip->fdata.colorant_array[0].hres;
880 ip->fdata.colorant_array[j].levels = 2;
881 }
882 }
883
884 ip->first_colorant_planes = 1;
885 }
886
887 return 0;
888 }
889
890 /*****************************************************************************/
891
892 static int action_resolution(gp_file *in, const pcl_Command *cmd, void *i)
893 {
894 CollectedInfo *ip = i;
895
896 ip->seen_RGR = TRUE;
897
898 if (!ip->CRD_active) {
899 int j;
900
901 for (j = 0; j < ip->fdata.number_of_colorants; j++) {
902 ip->fdata.colorant_array[j].hres =
903 ip->fdata.colorant_array[j].vres = cmd->i;
904 ip->fdata.colorant_array[j].levels = 2;
905 }
906 }
907
908 return 0;
909 }
910
911 /*****************************************************************************/
912
913 static int action_end_raster(gp_file *in, const pcl_Command *cmd, void *i)
914 {
915 CollectedInfo *ip = i;
916
917 if (cmd->chars[2] == 'B') ip->seen_ERG_B = TRUE;
918 else if (cmd->chars[2] == 'C') ip->seen_ERG_C = TRUE;
919
920 if (ip->seen_raster_data &&
921 memcmp(&ip->fdata, &ip->fdata_old, sizeof(pcl_FileData)) != 0) {
922 print_result(ip);
923 memcpy(&ip->fdata_old, &ip->fdata, sizeof(pcl_FileData));
924 }
925 ip->seen_raster_data = FALSE;
926
927 return 0;
928 }
929
930 /*****************************************************************************/
931
932 typedef struct {
933 char cmd[4];
934 pcl_CommandInterpreter action;
935 } ActionEntry;
936
937 static ActionEntry action_table[] = {
938 {"%-X", &action_UEL}, /* Universal Exit Language */
939 {"&bT", &action_dry_time}, /* Set Dry Time */
940 {"&lA", &action_PageSize}, /* Page Size */
941 {"&lG", &action_destination}, /* Set Output Bin */
942 {"&lH", &action_source}, /* Media Source */
943 {"&lM", &action_media_type}, /* Media Type */
944 {"&lS", &action_duplex}, /* Simplex/Duplex Print */
945 {"*bM", &action_compression}, /* Raster Graphics Compression Method */
946 {"*bV", &action_raster_data}, /* Transfer Raster Graphics Data by Plane */
947 {"*bW", &action_raster_data}, /* Transfer Raster Graphics Data by Row */
948 {"*gW", &action_CRD}, /* Configure Raster Data */
949 {"*oD", &action_old_quality}, /* Set Raster Graphics Depletion */
950 {"*oM", &action_quality}, /* Print Quality */
951 {"*oQ", &action_old_quality}, /* Set Raster Graphics Shingling */
952 {"*rB", &action_end_raster}, /* End Raster Graphics (old) */
953 {"*rC", &action_end_raster}, /* End Raster Graphics (new) */
954 {"*rQ", &action_old_quality}, /* Raster Graphics Quality */
955 {"*rU", &action_colour}, /* Set Number of Planes Per Row */
956 {"*tR", &action_resolution} /* Set Raster Graphics Resolution */
957 };
958
959 /*****************************************************************************/
960
961 static int cmp_entries(const void *a, const void *b)
962 {
963 return
964 strncmp(((const ActionEntry *)a)->cmd, ((const ActionEntry *)b)->cmd, 3);
965 }
966
967 /*****************************************************************************/
968
969 static int analyzer(gp_file *in, const pcl_Command *cmd, void *i)
970 {
971 ActionEntry key;
972 const ActionEntry *entry;
973 CollectedInfo *ip = i;
974
975 /* Control codes */
976 if (cmd->kind == 1) {
977 if (cmd->chars[0] == '\0' &&
978 /* Guard against NULs somewhere else except initially */
979 !ip->seen_RGR && !ip->seen_CRD)
980 ip->fdata.NULs_to_send = cmd->i;
981 return 0;
982 }
983
984 /* Two-character escape sequences */
985 if (cmd->kind == 2) {
986 switch (cmd->chars[0]) {
987 case 'E': /* Printer Reset */
988 {
989 /* Preserve some global data (mostly PJL state) across PCL Reset
990 invocations */
991 unsigned int NULs_to_send = ip->fdata.NULs_to_send;
992 char *PJL_language = ip->fdata.PJL_language;
993 char *PJL_job = ip->fdata.PJL_job;
994
995 ip->CRD_active = FALSE;
996 ip->seen_raster_data = FALSE;
997 memset(&ip->fdata, 0, sizeof(pcl_FileData));
998 ip->fdata.colorant_array[0].hres =
999 ip->fdata.colorant_array[0].vres = 75;
1000 ip->fdata.colorant_array[0].levels = 2;
1001 ip->fdata.number_of_colorants = 1;
1002 ip->fdata.duplex = -1;
1003 ip->fdata.dry_time = -1;
1004 ip->fdata.raster_graphics_quality = -1;
1005 ip->fdata.shingling = -1;
1006
1007 ip->fdata.NULs_to_send = NULs_to_send;
1008 ip->fdata.PJL_language = PJL_language;
1009 ip->fdata.PJL_job = PJL_job;
1010 }
1011 break;
1012 default:
1013 return 1;
1014 }
1015 return 0;
1016 }
1017
1018 /* Parameterized escape sequences */
1019 strncpy(key.cmd, cmd->chars, 3);
1020 entry = (const ActionEntry *)bsearch(&key, action_table,
1021 sizeof(action_table)/sizeof(ActionEntry), sizeof(ActionEntry), cmp_entries);
1022 if (entry != NULL) return (*entry->action)(in, cmd, ip);
1023
1024 return 1;
1025 }
1026
1027 /******************************************************************************
1028
1029 Function: work
1030
1031 On errors, the return value is positive, on success it is zero.
1032
1033 ******************************************************************************/
1034
1035 static int work(gp_file *in, gp_file *out) /* NLS: 50 */
1036 {
1037 CollectedInfo info;
1038 int rc;
1039
1040 memset(&info, 0, sizeof(CollectedInfo));
1041 info.out = out;
1042 info.fdata.duplex = -1;
1043 info.fdata.dry_time = -1;
1044 info.fdata.raster_graphics_quality = -1;
1045 info.fdata.shingling = -1;
1046
1047 rc = pcl_scan(in, &analyzer, &info, NULL, NULL);
1048 if (rc != 0) return 1;
1049
1050 if (info.number_of_outputs == 0) {
1051 emessage(50,
1052 "This does not seem to be a PCL file containing raster data.\n");
1053 return 1;
1054 }
1055
1056 return 0;
1057 }
1058
1059 /******************************************************************************
1060
1061 Function: main
1062
1063 Usage: pcl3opts <options> <input files>
1064 <option> ::= '-o' <output file>
1065 <input files> ::= <empty> | <input file> <input files>
1066
1067 ******************************************************************************/
1068
1069 int main(int argc, char **argv)
1070 {
1071 char
1072 *outfile_name = NULL;
1073 gp_file
1074 *outfile,
1075 *infile;
1076 int
1077 c,
1078 errors = 0,
1079 number_of_infiles;
1080
1081 #ifndef NDEBUG
1082 {
1083 int j;
1084
1085 for (j = 1; j < sizeof(action_table)/sizeof(ActionEntry); j++)
1086 assert(cmp_entries(action_table + (j - 1), action_table + j) < 0);
1087 }
1088 #endif /* !NDEBUG */
1089
1090 /* Set the locale depending on the environment */
1091 setlocale(LC_ALL, "");
1092 catd = catopen("pcl3opts", 0);
1093
1094 puts("*** pcl3opts\n"); /* Note that puts() appends another '\n'. */
1095
1096 /* Set the program's name. We assume: argc <= 0 || parts(argv) >= 1. */
1097 if (argc <= 0 || argv[0] == NULL || argv[0][0] == '\0')
1098 progname = "pcl3opts";
1099 else {
1100 if ((progname = strrchr(argv[0], '/')) == NULL || progname[1] == '\0')
1101 progname = argv[0];
1102 else
1103 progname++;
1104 }
1105
1106 /* Parse the options */
1107 while ((c = getopt(argc, argv, "o:")) != EOF) {
1108 switch(c) {
1109 case 'o':
1110 outfile_name = optarg;
1111 break;
1112 default:
1113 errors++;
1114 break;
1115 }
1116 }
1117
1118 /* Leave on errors in the command line */
1119 if (errors > 0) {
1120 emessage(1, "Usage: %s [<options>] [<input files>].\n"
1121 " <option> ::= -o <output file>\n",
1122 progname);
1123 exit(EXIT_FAILURE);
1124 }
1125
1126 /* Open the output file */
1127 if (outfile_name == NULL) outfile = stdout;
1128 else {
1129 outfile = fopen(outfile_name, "w");
1130 if (outfile == NULL) {
1131 emessage(2, "The file `%s' could not be opened for writing:\n %s.\n",
1132 outfile_name, strerror(errno));
1133 exit(EXIT_FAILURE);
1134 }
1135 }
1136
1137 /* Loop over the input files */
1138 number_of_infiles = argc - optind;
1139 if (number_of_infiles <= 0) errors += work(stdin, outfile);
1140 else do {
1141 char *infile_name;
1142
1143 /* Open */
1144 infile_name = argv[optind];
1145 infile = fopen(infile_name, "rb");
1146 if (infile == NULL) {
1147 emessage(3, "Could not open the input file `%s':\n %s.\n",
1148 infile_name, strerror(errno));
1149 exit(EXIT_FAILURE);
1150 }
1151
1152 if (number_of_infiles > 1) fprintf(outfile, "*** %s:\n", infile_name);
1153 errors += work(infile, outfile);
1154
1155 /* Close */
1156 if (fclose(infile) != 0) {
1157 emessage(4, "Error closing `%s':\n %s.\n",
1158 infile_name, strerror(errno));
1159 errors++;
1160 }
1161 optind++;
1162 if (errors == 0 && optind < argc) fputs("\n", outfile);
1163 } while (errors == 0 && optind < argc);
1164
1165 /* Close the output file */
1166 if (fclose(outfile) != 0) {
1167 emessage(4, "Error closing `%s':\n %s.\n",
1168 outfile_name == NULL? "[standard output]": outfile_name, strerror(errno));
1169 errors++;
1170 }
1171
1172 catclose(catd);
1173
1174 exit(errors == 0? EXIT_SUCCESS: EXIT_FAILURE);
1175 }