Debian Codegraph @

test/pcl-unprint.c

raw symbols SCIP
1 /*
2 * pclunprint.c - convert an HP PCL file into an image file for viewing.
3 *
4 * Copyright 2000 Dave Hill ([email protected])
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Revision History:
20 *
21 * See ChangeLog
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 #include <gutenprint/util.h>
28 #include<stdio.h>
29 #include<stdlib.h>
30 #include<ctype.h>
31 #include<string.h>
32
33 /*
34 * Size of buffer used to read file
35 */
36 #define READ_SIZE 16384
37
38 /*
39 * Largest data attached to a command. 1024 means that we can have up to 8192
40 * pixels in a row
41 */
42 #define MAX_DATA 16384
43
44 FILE *read_fd,*write_fd;
45 char read_buffer[READ_SIZE];
46 char data_buffer[MAX_DATA];
47 char initial_command[3];
48 int initial_command_index;
49 char final_command;
50 int numeric_arg;
51 int max_data_len = 0;
52
53 int read_pointer;
54 int read_size;
55 int eof;
56 int combined_command = 0;
57 int skip_output = 0;
58
59 /*
60 * Data about the image
61 */
62
63 typedef struct {
64 int colour_type; /* Mono, 3/4 colour */
65 int pixel_type; /* Mono, CMY, RGB */
66 int black_depth; /* 2 level, 4 level */
67 int cyan_depth; /* 2 level, 4 level */
68 int magenta_depth; /* 2 level, 4 level */
69 int yellow_depth; /* 2 level, 4 level */
70 int lcyan_depth; /* 2 level, 4 level */
71 int lmagenta_depth; /* 2 level, 4 level */
72 int image_width;
73 int image_height;
74 int compression_type; /* Uncompressed or TIFF */
75 } image_t;
76
77 /*
78 * collected data read from file
79 */
80
81 typedef struct {
82 char **black_bufs; /* Storage for black rows */
83 int black_data_rows_per_row; /* Number of black rows */
84 char **cyan_bufs;
85 int cyan_data_rows_per_row;
86 char **magenta_bufs;
87 int magenta_data_rows_per_row;
88 char **yellow_bufs;
89 int yellow_data_rows_per_row;
90 char **lcyan_bufs;
91 int lcyan_data_rows_per_row;
92 char **lmagenta_bufs;
93 int lmagenta_data_rows_per_row;
94 int buffer_length;
95 int active_length; /* Length of output data */
96 int output_depth;
97 int input_depth;
98 int pixels_depth;
99 } output_t;
100
101 #define PCL_MONO 1
102 #define PCL_CMY 3
103 #define PCL_CMYK 4
104 #define PCL_CMYKcm 6
105 #define PCL_RGB 65
106
107 #define PCL_COMPRESSION_NONE 0
108 #define PCL_COMPRESSION_RUNLENGTH 1
109 #define PCL_COMPRESSION_TIFF 2
110 #define PCL_COMPRESSION_DELTA 3
111 #define PCL_COMPRESSION_ADAPTIVE 5
112 #define PCL_COMPRESSION_CRDR 9 /* Compressed row delta replacement */
113
114 /* PCL COMMANDS */
115
116 typedef enum {
117 PCL_RESET = 1,
118 PCL_MEDIA_SIZE,
119 PCL_PERF_SKIP,
120 PCL_TOP_MARGIN,
121 PCL_MEDIA_TYPE,
122 PCL_MEDIA_SOURCE,
123 PCL_SHINGLING,
124 PCL_RASTERGRAPHICS_QUALITY,
125 PCL_DEPLETION,
126 PCL_CONFIGURE,
127 PCL_RESOLUTION,
128 PCL_COLOURTYPE,
129 PCL_COMPRESSIONTYPE,
130 PCL_LEFTRASTER_POS,
131 PCL_TOPRASTER_POS,
132 PCL_RASTER_WIDTH,
133 PCL_RASTER_HEIGHT,
134 PCL_START_RASTER,
135 PCL_END_RASTER,
136 PCL_END_COLOUR_RASTER,
137 PCL_DATA,
138 PCL_DATA_LAST,
139 PCL_PRINT_QUALITY,
140 PCL_ENTER_PJL,
141 PCL_GRAY_BALANCE,
142 PCL_DRIVER_CONFIG,
143 PCL_PAGE_ORIENTATION,
144 PCL_VERTICAL_CURSOR_POSITIONING_BY_DOTS,
145 PCL_HORIZONTAL_CURSOR_POSITIONING_BY_DOTS,
146 PCL_UNIT_OF_MEASURE,
147 PCL_RELATIVE_VERTICAL_PIXEL_MOVEMENT,
148 PCL_PALETTE_CONFIGURATION,
149 PCL_LPI,
150 PCL_CPI,
151 PCL_PAGE_LENGTH,
152 PCL_NUM_COPIES,
153 PCL_DUPLEX,
154 PCL_MEDIA_SIDE,
155 PCL_RTL_CONFIGURE,
156 PCL_ENTER_PCL,
157 PCL_ENTER_HPGL2,
158 PCL_NEGATIVE_MOTION,
159 PCL_MEDIA_DEST,
160 PCL_JOB_SEPARATION,
161 PCL_LEFT_OFFSET_REGISTRATION,
162 PCL_TOP_OFFSET_REGISTRATION,
163 PCL_PRINT_DIRECTION,
164 PCL_LEFT_MARGIN,
165 PCL_RIGHT_MARGIN,
166 PCL_RESET_MARGINS,PCL_TEXT_LENGTH,
167 /* Added by rlk 2017-12-25 */
168 PCL_RASTER_RENDER, /* Rendering algorithm */
169 PCL_COLOR_COMPONENT_1, /* Color component 1 */
170 PCL_COLOR_COMPONENT_2, /* Color component 2 */
171 PCL_COLOR_COMPONENT_3, /* Color component 3 */
172 PCL_ASSIGN_COLOR_INDEX,
173 PCL_SET_FOREGROUND_COLOR,
174 PCL_SET_VMI,
175 PCL_SEED_ROW_SOURCE,
176 } command_t;
177
178 typedef struct {
179 const char initial_command[3]; /* First part of command */
180 const char final_command; /* Last part of command */
181 int has_data; /* Data follows */
182 int clean_state; /* Leaves printer state clean */
183 command_t command; /* Command name */
184 const char *description; /* Text for printing */
185 } commands_t;
186
187 const commands_t pcl_commands[] =
188 {
189 /* Two-character sequences ESC <x> */
190 { "E", '\0', 0, 1, PCL_RESET, "PCL RESET" },
191 { "9", '\0', 0, 0, PCL_RESET_MARGINS, "Reset Margins" },
192 { "%", 'A', 0, 0, PCL_ENTER_PCL, "PCL mode" },
193 { "%", 'B', 0, 0, PCL_ENTER_HPGL2, "HPGL/2 mode" },
194 { "%", 'X', 0, 0, PCL_ENTER_PJL, "UEL/Enter PJL mode" },
195 /* Parameterised sequences */
196 /* Raster positioning */
197 { "&a", 'G', 0, 0, PCL_MEDIA_SIDE, "Set Media Side" },
198 { "&a", 'H', 0, 0, PCL_LEFTRASTER_POS, "Left Raster Position" },
199 { "&a", 'L', 0, 0, PCL_LEFT_MARGIN, "Left Margin by Column" },
200 { "&a", 'M', 0, 0, PCL_RIGHT_MARGIN, "Right Margin by Column" },
201 { "&a", 'N', 0, 0, PCL_NEGATIVE_MOTION, "Negative Motion" },
202 { "&a", 'P', 0, 0, PCL_PRINT_DIRECTION, "Print Direction" },
203 { "&a", 'V', 0, 0, PCL_TOPRASTER_POS, "Top Raster Position" },
204 /* Characters */
205 { "&k", 'H', 0, 0, PCL_CPI, "Characters per Inch" },
206 /* Media */
207 { "&l", 'A', 0, 0, PCL_MEDIA_SIZE , "Media Size" },
208 { "&l", 'C', 0, 0, PCL_SET_VMI, "Set Vertical Line Spacing" },
209 { "&l", 'D', 0, 0, PCL_LPI , "Lines per Inch" },
210 { "&l", 'E', 0, 0, PCL_TOP_MARGIN , "Top Margin" },
211 { "&l", 'F', 0, 0, PCL_TEXT_LENGTH , "Text Length" },
212 { "&l", 'G', 0, 0, PCL_MEDIA_DEST, "Media Destination" },
213 { "&l", 'H', 0, 0, PCL_MEDIA_SOURCE, "Media Source" },
214 { "&l", 'L', 0, 0, PCL_PERF_SKIP , "Perf. Skip" },
215 { "&l", 'M', 0, 0, PCL_MEDIA_TYPE , "Media Type" },
216 { "&l", 'O', 0, 0, PCL_PAGE_ORIENTATION, "Page Orientation" },
217 { "&l", 'P', 0, 0, PCL_PAGE_LENGTH, "Page Length in Lines" },
218 { "&l", 'S', 0, 0, PCL_DUPLEX, "Duplex mode" },
219 { "&l", 'T', 0, 0, PCL_JOB_SEPARATION, "Job Separation" },
220 { "&l", 'U', 0, 0, PCL_LEFT_OFFSET_REGISTRATION, "Left Offset Registration" },
221 { "&l", 'X', 0, 0, PCL_NUM_COPIES, "Number of copies" },
222 { "&l", 'Z', 0, 0, PCL_TOP_OFFSET_REGISTRATION, "Top Offset Registration" },
223 /* Units */
224 { "&u", 'D', 0, 0, PCL_UNIT_OF_MEASURE, "Unit of Measure" }, /* from bpd05446 */
225 /* Raster data */
226 { "*b", 'B', 0, 0, PCL_GRAY_BALANCE, "Gray Balance" }, /* from PCL Developer's Guide 6.0 */
227 { "*b", 'M', 0, 0, PCL_COMPRESSIONTYPE, "Compression Type" },
228 { "*b", 'S', 0, 0, PCL_SEED_ROW_SOURCE, "Seed row # source" },
229 { "*b", 'V', 1, 0, PCL_DATA, "Data, intermediate" },
230 { "*b", 'W', 1, 0, PCL_DATA_LAST, "Data, last" },
231 { "*b", 'Y', 0, 0, PCL_RELATIVE_VERTICAL_PIXEL_MOVEMENT, "Relative Vertical Pixel Movement" },
232 /* Palette */
233 { "*d", 'W', 1, 0, PCL_PALETTE_CONFIGURATION, "Palette Configuration" },
234 /* Plane configuration */
235 { "*g", 'W', 1, 0, PCL_CONFIGURE, "Configure Raster Data" },
236 /* Raster Graphics */
237 { "*o", 'D', 0, 0, PCL_DEPLETION, "Depletion" },
238 { "*o", 'M', 0, 0, PCL_PRINT_QUALITY, "Print Quality" },
239 { "*o", 'Q', 0, 0, PCL_SHINGLING, "Raster Graphics Shingling" },
240 { "*o", 'W', 1, 0, PCL_DRIVER_CONFIG, "Driver Configuration Command" },
241 /* Cursor Positioning */
242 { "*p", 'X', 0, 0, PCL_HORIZONTAL_CURSOR_POSITIONING_BY_DOTS, "Horizontal Cursor Positioning by Dots" },
243 { "*p", 'Y', 0, 0, PCL_VERTICAL_CURSOR_POSITIONING_BY_DOTS, "Vertical Cursor Positioning by Dots" },
244 /* Raster graphics */
245 { "*r", 'A', 0, 0, PCL_START_RASTER, "Start Raster Graphics" },
246 { "*r", 'B', 0, 0, PCL_END_RASTER, "End Raster Graphics"},
247 { "*r", 'C', 0, 0, PCL_END_COLOUR_RASTER, "End Colour Raster Graphics" },
248 { "*r", 'Q', 0, 0, PCL_RASTERGRAPHICS_QUALITY, "Raster Graphics Quality" },
249 { "*r", 'S', 0, 0, PCL_RASTER_WIDTH, "Raster Width" },
250 { "*r", 'T', 0, 0, PCL_RASTER_HEIGHT, "Raster Height" },
251 { "*r", 'U', 0, 0, PCL_COLOURTYPE, "Colour Type" },
252 /* Resolution */
253 { "*t", 'R', 0, 0, PCL_RESOLUTION, "Resolution" },
254 { "*t", 'J', 0, 0, PCL_RASTER_RENDER, "Render Algorithm" },
255 /* RTL/PCL5 */
256 { "*v", 'W', 1, 0, PCL_RTL_CONFIGURE, "RTL Configure Image Data" },
257 { "*v", 'A', 0, 0, PCL_COLOR_COMPONENT_1, "Color Component 1" },
258 { "*v", 'B', 0, 0, PCL_COLOR_COMPONENT_2, "Color Component 2" },
259 { "*v", 'C', 0, 0, PCL_COLOR_COMPONENT_3, "Color Component 3" },
260 { "*v", 'I', 0, 0, PCL_ASSIGN_COLOR_INDEX, "Assign Color Index" },
261 { "*v", 'S', 0, 0, PCL_SET_FOREGROUND_COLOR, "Set Foreground Color" },
262 };
263
264 int pcl_find_command (void);
265 void fill_buffer (void);
266 void pcl_read_command (void);
267 void write_grey (output_t *output, image_t *image);
268 void write_pixel (output_t *output, image_t *image);
269 void write_colour (output_t *output, image_t *image);
270 int decode_tiff (char *in_buffer, int data_length, char *decode_buf,
271 int maxlen);
272 int decode_delta (char *in_buffer, size_t data_length, char *decode_buf,
273 size_t maxlen);
274 void pcl_reset (image_t *i);
275 int depth_to_rows (int depth);
276
277
278 /*
279 * pcl_find_command(). Search the commands table for the command.
280 */
281
282 int pcl_find_command(void)
283 {
284
285 int num_commands = sizeof(pcl_commands) / sizeof(commands_t);
286 int i;
287
288 for (i=0; i < num_commands; i++) {
289 if ((strcmp(initial_command, pcl_commands[i].initial_command) == 0) &&
290 (final_command == pcl_commands[i].final_command))
291 return(i);
292 }
293
294 return (-1);
295 }
296
297 /*
298 * fill_buffer() - Read a new chunk from the input file
299 */
300
301 void fill_buffer(void)
302 {
303
304 if ((read_pointer == -1) || (read_pointer >= read_size)) {
305 read_size = (int) fread(&read_buffer, sizeof(char), READ_SIZE, read_fd);
306
307 #ifdef DEBUG
308 fprintf(stderr, "Read %d characters\n", read_size);
309 #endif
310
311 if (read_size == 0) {
312 #ifdef DEBUG
313 fprintf(stderr, "No More to read!\n");
314 #endif
315 eof = 1;
316 return;
317 }
318 read_pointer = 0;
319 }
320 }
321
322 /*
323 * pcl_read_command() - Read the data stream and parse the next PCL
324 * command.
325 */
326
327 void pcl_read_command(void)
328 {
329
330 char c;
331 int minus;
332 int skipped_chars;
333
334 /*
335 Precis from the PCL Developer's Guide 6.0:-
336
337 There are two formats for PCL commands; "Two Character" and
338 "Parameterised".
339
340 A "Two Character" command is: ESC <x>, where <x> has a decimal
341 value between 48 and 126 (inclusive).
342
343 A "Parameterised" command is: ESC <x> <y> [n] <z>
344 where x, y and z are characters, and [n] is an optional number.
345 The character <x> has a decimal value between 33 and 47 (incl).
346 The character <y> has a decimal value between 96 and 126 (incl).
347
348 Some commands are followed by data, in this case, [n] is the
349 number of bytes to read, otherwise <n> is a numeric argument.
350 The number <n> can consist of +, - and 0-9 (and .).
351
352 The character <z> is either in the range 64-94 for a termination
353 or 96-126 for a combined command. (The ref guide gives these
354 as "96-126" and "64-96" which cannot be right as 96 appears twice!)
355
356 It is possible to combine parameterised commands if the
357 command prefix is the same, e.g. ESC & l 26 a 0 L is the same as
358 ESC & l 26 A ESC & l 0 L. The key to this is that the terminator for
359 the first command is in the range 96-126 (lower case).
360
361 There is a problem with the "escape command" (ESC %) as it does not
362 conform to this specification, so we have to check for it specifically!
363 */
364
365 #define PCL_DIGIT(c) (((unsigned int) c >= 48) && ((unsigned int) c <= 57))
366 #define PCL_TWOCHAR(c) (((unsigned int) c >= 48) && ((unsigned int) c <= 126))
367 #define PCL_PARAM(c) (((unsigned int) c >= 33) && ((unsigned int) c <= 47))
368 #define PCL_PARAM2(c) (((unsigned int) c >= 96) && ((unsigned int) c <= 126))
369 #define PCL_COMBINED_TERM(c) (((unsigned int) c >= 96) && ((unsigned int) c <= 126))
370 #define PCL_TERM(c) (((unsigned int) c >= 64) && ((unsigned int) c <= 94))
371 #define PCL_CONVERT_TERM(c) (c - (char) 32)
372
373 numeric_arg=0;
374 minus = 0;
375 final_command = '\0';
376
377 fill_buffer();
378 if (eof == 1)
379 return;
380
381 c = read_buffer[read_pointer++];
382 #ifdef DEBUG
383 fprintf(stderr, "Got %c\n", c);
384 #endif
385
386 /*
387 * If we are not in a "combined command", we are looking for ESC
388 */
389
390 if (combined_command == 0) {
391 int non_ff_skipped_chars = 0;
392 if(c != (char) 0x1b) {
393
394 /*
395 * all we can do is to chew through the file looking for another ESC.
396 */
397
398 skipped_chars = 0;
399 fprintf(stderr, " ");
400 while (c != (char) 0x1b) {
401 if (c == (char) 0x0c)
402 fprintf(stderr, " FF");
403 else
404 non_ff_skipped_chars++;
405 skipped_chars++;
406 fill_buffer();
407 if (eof == 1) {
408 fprintf(stderr, "ERROR: EOF looking for ESC!\n");
409 return;
410 }
411 c = read_buffer[read_pointer++];
412 }
413 if (non_ff_skipped_chars != 0)
414 fprintf(stderr, "ERROR: No ESC found (out of sync?) searching... ");
415 fprintf(stderr, " %d character%s skipped.\n", skipped_chars,
416 skipped_chars == 1 ? "" : "s");
417 }
418
419 /*
420 * We got an ESC, process normally
421 */
422
423 initial_command_index=0;
424 initial_command[initial_command_index] = '\0';
425 fill_buffer();
426 if (eof == 1) {
427 fprintf(stderr, "ERROR: EOF after ESC!\n");
428 return;
429 }
430
431 /* Get first command letter */
432
433 c = read_buffer[read_pointer++];
434 initial_command[initial_command_index++] = c;
435
436 #ifdef DEBUG
437 fprintf(stderr, "Got %c\n", c);
438 #endif
439
440 /* Check to see if this character forms a "two character" command,
441 or is a special command. */
442
443 if (PCL_TWOCHAR(c)) {
444 #ifdef DEBUG
445 fprintf(stderr, "Two character command\n");
446 #endif
447 initial_command[initial_command_index] = '\0';
448 return;
449 } /* two character check */
450
451 /* Now check for a "parameterised" sequence. */
452
453 else if (PCL_PARAM(c)) {
454 #ifdef DEBUG
455 fprintf(stderr, "Parameterised command\n");
456 #endif
457
458 /* Get the next character in the command */
459
460 fill_buffer();
461 if (eof == 1) {
462
463 #ifdef DEBUG
464 fprintf(stderr, "EOF in middle of command!\n");
465 #endif
466 eof = 0; /* Ignore it */
467 initial_command[initial_command_index] = '\0';
468 return;
469 }
470 c = read_buffer[read_pointer++];
471 #ifdef DEBUG
472 fprintf(stderr, "Got %c\n", c);
473 #endif
474
475 /* Check that it is legal and store it */
476
477 if (PCL_PARAM2(c)) {
478 initial_command[initial_command_index++] = c;
479 initial_command[initial_command_index] = '\0';
480
481 /* Get the next character in the command then fall into the numeric part */
482
483 fill_buffer();
484 if (eof == 1) {
485
486 #ifdef DEBUG
487 fprintf(stderr, "EOF in middle of command!\n");
488 #endif
489 eof = 0; /* Ignore it */
490 return;
491 }
492 c = read_buffer[read_pointer++];
493 #ifdef DEBUG
494 fprintf(stderr, "Got %c\n", c);
495 #endif
496
497 }
498 else {
499 /* The second character is not legal. If the first character is '%' then allow it
500 * through */
501
502 if (initial_command[0] == '%') {
503 #ifdef DEBUG
504 fprintf(stderr, "ESC%% command\n");
505 #endif
506 initial_command[initial_command_index] = '\0';
507 }
508 else {
509 fprintf(stderr, "ERROR: Illegal second character %c in parameterised command.\n",
510 c);
511 initial_command[initial_command_index] = '\0';
512 return;
513 }
514 }
515 } /* Parameterised check */
516
517 /* If we get here, the command is illegal */
518
519 else {
520 fprintf(stderr, "ERROR: Illegal first character %c in command.\n",
521 c);
522 initial_command[initial_command_index] = '\0';
523 return;
524 }
525 } /* End of (combined_command) */
526
527 /*
528 We get here if either this is a combined sequence, or we have processed
529 the beginning of a parameterised sequence. There is an optional number
530 next, which may be preceded by "+" or "-". FIXME We should also handle
531 decimal points.
532 */
533
534 if ((c == '-') || (c == '+') || (PCL_DIGIT(c))) {
535 if (c == '-')
536 minus = 1;
537 else if (c == '+')
538 minus = 0;
539 else
540 numeric_arg = (int) (c - '0');
541
542 /* Continue until non-numeric seen */
543
544 while (1) {
545 fill_buffer();
546 if (eof == 1) {
547 fprintf(stderr, "ERROR: EOF in middle of command!\n");
548 return;
549 }
550 c = read_buffer[read_pointer++];
551
552 #ifdef DEBUG
553 fprintf(stderr, "Got %c\n", c);
554 #endif
555
556 if (! PCL_DIGIT(c)) {
557 break; /* End of loop */
558 }
559 numeric_arg = (10 * numeric_arg) + (int) (c - '0');
560 }
561 }
562
563 /*
564 We fell out of the loop when we read a non-numeric character.
565 Treat this as the terminating character and check for a combined
566 command. We should check that the letter is a valid terminator,
567 but it doesn't matter as we'll just not recognize the command!
568 */
569
570 combined_command = (PCL_COMBINED_TERM(c) != 0);
571 if (combined_command == 1) {
572 #ifdef DEBUG
573 fprintf(stderr, "Combined command\n");
574 #endif
575 final_command = PCL_CONVERT_TERM(c);
576 }
577 else
578 final_command = c;
579
580 if (minus == 1)
581 numeric_arg = -numeric_arg;
582
583 return;
584 }
585
586 /*
587 * write_grey() - write out one line of mono PNM image
588 */
589
590 /* FIXME - multiple levels */
591
592 void write_grey(output_t *output, /* I: data */
593 image_t *image) /* I: Image data */
594 {
595 int wholebytes = image->image_width / 8;
596 int crumbs = image->image_width - (wholebytes * 8);
597 char *buf = output->black_bufs[0];
598
599 int i, j;
600 char tb[8];
601
602 #ifdef DEBUG
603 fprintf(stderr, "Data Length: %d, wholebytes: %d, crumbs: %d\n",
604 output->active_length, wholebytes, crumbs);
605 #endif
606
607 for (i=0; i < wholebytes; i++) {
608 for (j=0; j < 8; j++) {
609 tb[j] = (((buf[i] >> (7-j)) & 1));
610 tb[j] = output->output_depth - tb[j];
611 }
612 (void) fwrite(&tb[0], sizeof(char), 8, write_fd);
613 }
614 for (j=0; j < crumbs; j++) {
615 tb[j] = (((buf[wholebytes] >> (7-j)) & 1));
616 tb[j] = output->output_depth - tb[j];
617 }
618 (void) fwrite(&tb[0], sizeof(char), (size_t) crumbs, write_fd);
619 }
620
621
622 /*
623 * write_pixel() - write out one line of pixel PNM image
624 */
625
626 /* FIXME - other than direct by pixel */
627 void write_pixel(output_t *output, /* I: data */
628 image_t *image) /* I: Image data */
629 {
630 unsigned char *buf = (unsigned char *) (output->black_bufs[0]);
631 int i;
632 if (image->colour_type == PCL_CMY) {
633 for (i = 0; i < image->image_width * 3; i++) {
634 buf[i] = -buf[i];
635 }
636 }
637 (void) fwrite(buf, sizeof(char), image->image_width * 3, write_fd);
638 /* Needed for delta compression */
639 if (image->colour_type == PCL_CMY) {
640 for (i = 0; i < image->image_width * 3; i++) {
641 buf[i] = -buf[i];
642 }
643 }
644 }
645
646 /*
647 * write_colour() - Write out one row of RGB PNM data.
648 */
649
650 /* FIXME - multiple levels and CMYK/CMYKcm */
651
652 void write_colour(output_t *output, /* I: Data buffers */
653 image_t *image) /* I: Image data */
654 {
655 int wholebytes = image->image_width / 8;
656 int crumbs = image->image_width - (wholebytes * 8);
657
658 int i, j, jj;
659 char tb[8*3];
660
661 char *cyan_buf;
662 char *magenta_buf;
663 char *yellow_buf;
664 char *black_buf;
665
666 return;
667 cyan_buf = output->cyan_bufs[0];
668 magenta_buf = output->magenta_bufs[0];
669 yellow_buf = output->yellow_bufs[0];
670 if (image->colour_type != PCL_CMY)
671 black_buf = output->black_bufs[0];
672 else
673 black_buf = NULL;
674
675 fprintf(stderr, "Data Length: %d, wholebytes: %d, crumbs: %d, planes: %d\n",
676 output->active_length, wholebytes, crumbs, image->colour_type);
677
678 fprintf(stderr, "Cyan: ");
679 for (i=0; i < output->active_length; i++) {
680 fprintf(stderr, "%02x ", (unsigned char) cyan_buf[i]);
681 }
682 fprintf(stderr, "\n");
683 fprintf(stderr, "Magenta: ");
684 for (i=0; i < output->active_length; i++) {
685 fprintf(stderr, "%02x ", (unsigned char) magenta_buf[i]);
686 }
687 fprintf(stderr, "\n");
688 fprintf(stderr, "Yellow: ");
689 for (i=0; i < output->active_length; i++) {
690 fprintf(stderr, "%02x ", (unsigned char) yellow_buf[i]);
691 }
692 fprintf(stderr, "\n");
693 if (image->colour_type == PCL_CMYK) {
694 fprintf(stderr, "Black: ");
695 for (i=0; i < output->active_length; i++) {
696 fprintf(stderr, "%02x ", (unsigned char) black_buf[i]);
697 }
698 fprintf(stderr, "\n");
699 }
700
701 if (output->pixels_depth == 1) {
702 if (image->colour_type == PCL_CMY) {
703 for (i=0; i < wholebytes; i++) {
704 for (j=0,jj=0; j < 8; j++,jj+=3) {
705 tb[jj] = (((cyan_buf[i] >> (7-j)) & 1));
706 tb[jj] = output->output_depth - tb[jj];
707 tb[jj+1] = (((magenta_buf[i] >> (7-j)) & 1));
708 tb[jj+1] = output->output_depth - tb[jj+1];
709 tb[jj+2] = (((yellow_buf[i] >> (7-j)) & 1));
710 tb[jj+2] = output->output_depth - tb[jj+2];
711 }
712 (void) fwrite(&tb[0], sizeof(char), (size_t) (8*3), write_fd);
713 }
714 for (j=0,jj=0; j < crumbs; j++,jj+=3) {
715 tb[jj] = (((cyan_buf[wholebytes] >> (7-j)) & 1));
716 tb[jj] = output->output_depth - tb[jj];
717 tb[jj+1] = (((magenta_buf[wholebytes] >> (7-j)) & 1));
718 tb[jj+1] = output->output_depth - tb[jj+1];
719 tb[jj+2] = (((yellow_buf[wholebytes] >> (7-j)) & 1));
720 tb[jj+2] = output->output_depth - tb[jj+2];
721 }
722 (void) fwrite(&tb[0], sizeof(char), (size_t) crumbs*3, write_fd);
723 }
724 else {
725 for (i=0; i < wholebytes; i++) {
726 for (j=0,jj=0; j < 8; j++,jj+=3) {
727 #if !defined OUTPUT_CMYK_ONLY_K && !defined OUTPUT_CMYK_ONLY_CMY
728 tb[jj] = ((((cyan_buf[i]|black_buf[i]) >> (7-j)) & 1));
729 tb[jj+1] = ((((magenta_buf[i]|black_buf[i]) >> (7-j)) & 1));
730 tb[jj+2] = ((((yellow_buf[i]|black_buf[i]) >> (7-j)) & 1));
731 #endif
732 #ifdef OUTPUT_CMYK_ONLY_K
733 tb[jj] = (((black_buf[i] >> (7-j)) & 1));
734 tb[jj+1] = (((black_buf[i] >> (7-j)) & 1));
735 tb[jj+2] = (((black_buf[i] >> (7-j)) & 1));
736 #endif
737 #ifdef OUTPUT_CMYK_ONLY_CMY
738 tb[jj] = (((cyan_buf[i] >> (7-j)) & 1));
739 tb[jj+1] = (((magenta_buf[i] >> (7-j)) & 1));
740 tb[jj+2] = (((yellow_buf[i] >> (7-j)) & 1));
741 #endif
742 tb[jj] = output->output_depth - tb[jj];
743 tb[jj+1] = output->output_depth - tb[jj+1];
744 tb[jj+2] = output->output_depth - tb[jj+2];
745 }
746 (void) fwrite(&tb[0], sizeof(char), (size_t) (8*3), write_fd);
747 }
748 for (j=0,jj=0; j < crumbs; j++,jj+=3) {
749 #if !defined OUTPUT_CMYK_ONLY_K && !defined OUTPUT_CMYK_ONLY_CMY
750 tb[jj] = ((((cyan_buf[wholebytes]|black_buf[wholebytes]) >> (7-j)) & 1));
751 tb[jj+1] = ((((magenta_buf[wholebytes]|black_buf[wholebytes]) >> (7-j)) & 1));
752 tb[jj+2] = ((((yellow_buf[wholebytes]|black_buf[wholebytes]) >> (7-j)) & 1));
753 #endif
754 #ifdef OUTPUT_CMYK_ONLY_K
755 tb[jj] = (((black_buf[wholebytes] >> (7-j)) & 1));
756 tb[jj+1] = (((black_buf[wholebytes] >> (7-j)) & 1));
757 tb[jj+2] = (((black_buf[wholebytes] >> (7-j)) & 1));
758 #endif
759 #ifdef OUTPUT_CMYK_ONLY_CMY
760 tb[jj] = (((cyan_buf[wholebytes] >> (7-j)) & 1));
761 tb[jj+1] = (((magenta_buf[wholebytes] >> (7-j)) & 1));
762 tb[jj+2] = (((yellow_buf[wholebytes] >> (7-j)) & 1));
763 #endif
764 tb[jj] = output->output_depth - tb[jj];
765 tb[jj+1] = output->output_depth - tb[jj+1];
766 tb[jj+2] = output->output_depth - tb[jj+2];
767 }
768 (void) fwrite(&tb[0], sizeof(char), (size_t) crumbs*3, write_fd);
769 }
770 }
771 }
772 /*
773 * decode_tiff() - Uncompress a TIFF encoded buffer
774 */
775
776 int decode_tiff(char *in_buffer, /* I: Data buffer */
777 int data_length, /* I: Length of data */
778 char *decode_buf, /* O: decoded data */
779 int maxlen) /* I: Max length of decode_buf */
780 {
781 /* The TIFF coding consists of either:-
782 *
783 * (0 <= count <= 127) (count+1 bytes of data) for non repeating data
784 * or
785 * (-127 <= count <= -1) (data) for 1-count bytes of repeating data
786 */
787
788 int count;
789 int pos = 0;
790 int dpos = 0;
791 #ifdef DEBUG
792 int i;
793 #endif
794
795 while(pos < data_length ) {
796
797 count = in_buffer[pos];
798
799 if ((count >= 0) && (count <= 127)) {
800 #ifdef DEBUG
801 fprintf(stderr, "%d bytes of nonrepeated data\n", count+1);
802 fprintf(stderr, "DATA: ");
803 for (i=0; i< (count+1); i++) {
804 fprintf(stderr, "%02x ", (unsigned char) in_buffer[pos + 1 + i]);
805 }
806 fprintf(stderr, "\n");
807 #endif
808 if ((dpos + count + 1) > maxlen) {
809 fprintf(stderr, "ERROR: Too much expanded data (%d > %d)!\n", dpos + count + 1, maxlen);
810 exit(EXIT_FAILURE);
811 }
812 memcpy(&decode_buf[dpos], &in_buffer[pos+1], (size_t) (count + 1));
813 dpos += count + 1;
814 pos += count + 2;
815 }
816 else if ((count >= -127) && (count < 0)) {
817 #ifdef DEBUG
818 fprintf(stderr, "%02x repeated %d times\n", (unsigned char) in_buffer[pos + 1], 1 - count);
819 #endif
820 if ((dpos + 1 - count) > maxlen) {
821 fprintf(stderr, "ERROR: Too much expanded data (%d > %d)!\n", dpos + 1 - count, maxlen);
822 exit(EXIT_FAILURE);
823 }
824 memset(&decode_buf[dpos], in_buffer[pos + 1], (size_t) (1 - count));
825 dpos += 1 - count;
826 pos += 2;
827 }
828 else {
829 fprintf(stderr, "ERROR: Illegal TIFF count: %d, skipped\n", count);
830 pos += 2;
831 }
832 }
833
834 #ifdef DEBUG
835 fprintf(stderr, "TIFFOUT: ");
836 for (i=0; i< dpos; i++) {
837 fprintf(stderr, "%02x ", (unsigned char) decode_buf[i]);
838 }
839 fprintf(stderr, "\n");
840 #endif
841 return(dpos);
842 }
843
844 /*
845 * decode_delta() - Uncompress a delta encoded buffer
846 */
847
848 int decode_delta(char *in_buffer, /* I: Data buffer */
849 size_t data_length, /* I: Length of data */
850 char *decode_buf, /* I/O: decoded data */
851 size_t maxlen) /* I: Max length of decode_buf */
852 {
853
854 unsigned command_byte = 0;
855 unsigned delta_bytes = 0;
856 unsigned offset_from_last = 0;
857 unsigned next_offset = 0;
858
859 size_t pos = 0;
860 int dpos = 0;
861 #ifdef DEBUG
862 int i = 0;
863 int j = 0;
864 #endif
865
866 #ifdef DEBUG
867 fprintf(stderr, ">>>Decode delta %p %d %p %d\n", in_buffer, data_length,
868 decode_buf, maxlen);
869 #endif
870 while(pos < data_length ) {
871
872 command_byte = in_buffer[pos];
873 delta_bytes = (((unsigned char) command_byte) >> 5) + 1;
874 offset_from_last = (command_byte & ((1 << 5) - 1));
875 if (offset_from_last == 31) {
876 do {
877 next_offset = (unsigned char) in_buffer[++pos];
878 offset_from_last += next_offset;
879 } while (next_offset == 0xff);
880 }
881 #ifdef DEBUG
882 fprintf(stderr, "%d: command %02x (delta %d, offset %d)\n", i,
883 (unsigned char) command_byte, delta_bytes, offset_from_last);
884 fprintf(stderr, " pos %d dpos %d data_length %d maxlen %d\n",
885 pos, dpos, data_length, maxlen);
886 #endif
887 pos++;
888 if (data_length - pos < delta_bytes) {
889 fprintf(stderr, "ERROR: data overrun in delta (delta %d, remaining %zd)\n",
890 delta_bytes, data_length - pos);
891 }
892 dpos += offset_from_last;
893 (void) memcpy(decode_buf + dpos, in_buffer + pos, delta_bytes);
894 #ifdef DEBUG
895 fprintf(stderr, " ");
896 for (j = 0; j < delta_bytes; j++) {
897 fprintf(stderr, "%02x ", (unsigned char) in_buffer[pos + j]);
898 }
899 fprintf(stderr, "\n");
900 #endif
901 pos += delta_bytes;
902 dpos += delta_bytes;
903 #ifdef DEBUG
904 fprintf(stderr, " pos %d dpos %d data_length %d maxlen %d\n",
905 pos, dpos, data_length, maxlen);
906 i++;
907 #endif
908 }
909 /*
910 fprintf(stderr, " Decode delta %d %d %d\n", pos, data_length, dpos);
911 */
912
913 #ifdef DEBUG
914 fprintf(stderr, "DELTAOUT: ");
915 for (i=0; i< dpos; i++) {
916 fprintf(stderr, "%02x ", (unsigned char) decode_buf[i]);
917 }
918 fprintf(stderr, "\n");
919 #endif
920
921 (void)maxlen;
922
923 return(dpos);
924 }
925
926 /*
927 * pcl_reset() - Rest image parameters to default
928 */
929
930 void pcl_reset(image_t *i)
931 {
932 i->colour_type = PCL_MONO;
933 i->pixel_type = PCL_MONO;
934 i->black_depth = 2; /* Assume mono */
935 i->cyan_depth = 0;
936 i->magenta_depth = 0;
937 i->yellow_depth = 0;
938 i->lcyan_depth = 0;
939 i->lmagenta_depth = 0;
940 i->image_width = -1;
941 i->image_height = -1;
942 i->compression_type = 0; /* should this be NONE? */
943 }
944
945 /*
946 * depth_to_rows() - convert the depth of the colour into the number
947 * of data rows needed to represent it. Assumes that depth is a power
948 * of 2, FIXME if not!
949 */
950
951 int depth_to_rows(int depth)
952 {
953 int rows;
954
955 if (depth == 0)
956 return(0);
957
958 for (rows = 1; rows < 8; rows++) {
959 if ((depth >> rows) == 1)
960 return(rows);
961 }
962 fprintf(stderr, "ERROR: depth %d too big to handle in depth_to_rows()!\n",
963 depth);
964 return(0); /* ?? */
965 }
966
967 static void
968 print_command(int index, int arg)
969 {
970 char buf[16];
971 const commands_t *cmd = &(pcl_commands[index]);
972 (void) snprintf(buf, 15, "%s%d%c", cmd->initial_command, arg, cmd->final_command);
973 fprintf(stderr, "%-8s (%s): ", buf, pcl_commands[index].description);
974 }
975
976 /*
977 * Main
978 */
979
980 int main(int argc, char *argv[])
981 {
982
983 int command_index;
984 command_t command;
985 int i, j; /* Loop/general variables */
986 int image_row_counter = -1; /* Count of current row */
987 int total_image_rows = 0; /* All rows seen */
988 int current_data_row = -1; /* Count of data rows received for this output row */
989 int expected_data_rows_per_row = -1;
990 /* Expected no of data rows per output row */
991 image_t image_data; /* Data concerning image */
992 long filepos = -1;
993 int wrote_header = 0;
994 int state_is_clean = 1;
995
996 /*
997 * Holders for the decoded lines
998 */
999
1000 output_t output_data;
1001
1002 /*
1003 * The above pointers (when allocated) are then copied into this
1004 * variable in the correct order so that the received data can
1005 * be stored.
1006 */
1007
1008 char **received_rows;
1009
1010 output_data.black_bufs = NULL; /* Storage for black rows */
1011 output_data.black_data_rows_per_row = 0; /* Number of black rows */
1012 output_data.cyan_bufs = NULL;
1013 output_data.cyan_data_rows_per_row = 0;
1014 output_data.magenta_bufs = NULL;
1015 output_data.magenta_data_rows_per_row = 0;
1016 output_data.yellow_bufs = NULL;
1017 output_data.yellow_data_rows_per_row = 0;
1018 output_data.lcyan_bufs = NULL;
1019 output_data.lcyan_data_rows_per_row = 0;
1020 output_data.lmagenta_bufs = NULL;
1021 output_data.lmagenta_data_rows_per_row = 0;
1022 output_data.buffer_length = 0;
1023 output_data.active_length = 0;
1024 output_data.output_depth = 0;
1025 output_data.input_depth = 1;
1026 output_data.pixels_depth = 1;
1027
1028 received_rows = NULL;
1029
1030 if(argc == 1){
1031 read_fd = stdin;
1032 write_fd = stdout;
1033 }
1034 else if(argc == 2){
1035 read_fd = fopen(argv[1],"r");
1036 write_fd = stdout;
1037 }
1038 else {
1039 if(*argv[1] == '-'){
1040 read_fd = stdin;
1041 write_fd = fopen(argv[2],"w");
1042 }
1043 else {
1044 read_fd = fopen(argv[1],"r");
1045 write_fd = fopen(argv[2],"w");
1046 }
1047 }
1048
1049 if (read_fd == (FILE *)NULL) {
1050 fprintf(stderr, "ERROR: Error Opening input file.\n");
1051 exit (EXIT_FAILURE);
1052 }
1053
1054 if (write_fd == (FILE *)NULL) {
1055 fprintf(stderr, "ERROR: Error Opening output file.\n");
1056 exit (EXIT_FAILURE);
1057 }
1058
1059 read_pointer=-1;
1060 eof=0;
1061 initial_command_index=0;
1062 initial_command[initial_command_index] = '\0';
1063 numeric_arg=0;
1064 final_command = '\0';
1065
1066
1067 pcl_reset(&image_data);
1068
1069 while (1) {
1070 pcl_read_command();
1071 if (eof == 1) {
1072 if (! state_is_clean)
1073 fprintf(stderr, "EOF while reading command.\n");
1074 (void) fclose(read_fd);
1075 (void) fclose(write_fd);
1076 exit(EXIT_SUCCESS);
1077 }
1078
1079 #ifdef DEBUG
1080 fprintf(stderr, "initial_command: %s, numeric_arg: %d, final_command: %c\n",
1081 initial_command, numeric_arg, final_command);
1082 #endif
1083
1084 command_index = pcl_find_command();
1085 if (command_index == -1) {
1086 fprintf(stderr, "ERROR: Unknown (and unhandled) command: %s%d%c\n", initial_command,
1087 numeric_arg, final_command);
1088 /* We may have to skip some data here */
1089 }
1090 else {
1091 command = pcl_commands[command_index].command;
1092 state_is_clean = pcl_commands[command_index].clean_state;
1093 if (pcl_commands[command_index].has_data == 1) {
1094
1095 /* Read the data into data_buffer */
1096
1097 #ifdef DEBUG
1098 fprintf(stderr, "Data: ");
1099 #endif
1100
1101 if (numeric_arg > MAX_DATA) {
1102 fprintf(stderr, "ERROR: Too much data (%d), increase MAX_DATA (%d)!\n", numeric_arg, MAX_DATA);
1103 exit(EXIT_FAILURE);
1104 }
1105
1106 for (i=0; i < numeric_arg; i++) {
1107 fill_buffer();
1108 if (eof == 1) {
1109 fprintf(stderr, "ERROR: Unexpected EOF whilst reading data\n");
1110 exit(EXIT_FAILURE);
1111 }
1112 data_buffer[i] = read_buffer[read_pointer++];
1113
1114 #ifdef DEBUG
1115 fprintf(stderr, "%02x ", (unsigned char) data_buffer[i]);
1116 #endif
1117
1118 }
1119
1120 #ifdef DEBUG
1121 fprintf(stderr, "\n");
1122 #endif
1123
1124 }
1125 switch(command) {
1126 case PCL_RESET :
1127 print_command(command_index, numeric_arg);
1128 fprintf(stderr, "\n");
1129 pcl_reset(&image_data);
1130 break;
1131
1132 case PCL_RESET_MARGINS :
1133 print_command(command_index, numeric_arg);
1134 fprintf(stderr, "\n");
1135 break;
1136
1137 case PCL_START_RASTER :
1138 print_command(command_index, numeric_arg);
1139 fprintf(stderr, "\n");
1140
1141 /* Make sure we have all the stuff needed to work out what we are going
1142 to write out. */
1143
1144 i = 0; /* use as error indicator */
1145
1146 if (image_data.image_width == -1) {
1147 fprintf(stderr, "ERROR: Image width not set!\n");
1148 i++;
1149 }
1150 if (image_data.image_height == -1) {
1151 fprintf(stderr, "WARNING: Image height not set!\n");
1152 }
1153
1154 if ((image_data.black_depth != 0) &&
1155 (image_data.black_depth != 2)) {
1156 fprintf(stderr, "WARNING: Only 2 level black dithers handled.\n");
1157 }
1158 if ((image_data.cyan_depth != 0) &&
1159 (image_data.cyan_depth != 2)) {
1160 fprintf(stderr, "WARNING: Only 2 level cyan dithers handled.\n");
1161 }
1162 if ((image_data.magenta_depth != 0) &&
1163 (image_data.magenta_depth != 2)) {
1164 fprintf(stderr, "WARNING: Only 2 level magenta dithers handled.\n");
1165 }
1166 if ((image_data.yellow_depth != 0) &&
1167 (image_data.yellow_depth != 2)) {
1168 fprintf(stderr, "WARNING: only 2 level yellow dithers handled.\n");
1169 }
1170 if (image_data.lcyan_depth != 0) {
1171 fprintf(stderr, "WARNING: Light cyan dithers not yet handled.\n");
1172 }
1173 if (image_data.lmagenta_depth != 0) {
1174 fprintf(stderr, "WARNING: Light magenta dithers not yet handled.\n");
1175 }
1176
1177 if (image_data.compression_type != PCL_COMPRESSION_NONE &&
1178 image_data.compression_type != PCL_COMPRESSION_TIFF &&
1179 image_data.compression_type != PCL_COMPRESSION_DELTA) {
1180 fprintf(stderr,
1181 "Sorry, only 'no compression' or 'tiff compression' handled.\n");
1182 i++;
1183 }
1184
1185 if (i != 0) {
1186 fprintf(stderr, "PNM output suppressed, will continue diagnostic output.\n");
1187 skip_output = 1;
1188 }
1189
1190 if (skip_output == 0) {
1191 if (! wrote_header) {
1192 if (image_data.colour_type == PCL_MONO &&
1193 image_data.pixel_type == PCL_MONO)
1194 (void) fputs("P5\n", write_fd); /* Raw, Grey */
1195 else
1196 (void) fputs("P6\n", write_fd); /* Raw, RGB */
1197
1198 (void) fputs("# Written by pclunprint.\n", write_fd);
1199
1200 /*
1201 * Remember the file position where we wrote the image width and height
1202 * (you don't want to know why!)
1203 */
1204
1205 filepos = ftell(write_fd);
1206
1207 fprintf(write_fd, "%10d %10d\n", image_data.image_width,
1208 image_data.image_height);
1209 }
1210
1211 /*
1212 * Write the depth of the image
1213 */
1214
1215 if (image_data.black_depth != 0)
1216 output_data.output_depth = image_data.black_depth - 1;
1217 else
1218 output_data.output_depth = image_data.cyan_depth - 1;
1219 if (! wrote_header) {
1220 if (output_data.pixels_depth > 1)
1221 fprintf(write_fd, "%d\n", 255);
1222 else
1223 fprintf(write_fd, "%d\n", output_data.output_depth);
1224 }
1225 wrote_header = 1;
1226
1227
1228 image_row_counter = 0;
1229 current_data_row = 0;
1230
1231 output_data.black_data_rows_per_row = depth_to_rows(image_data.black_depth);
1232 output_data.cyan_data_rows_per_row = depth_to_rows(image_data.cyan_depth);
1233 output_data.magenta_data_rows_per_row = depth_to_rows(image_data.magenta_depth);
1234 output_data.yellow_data_rows_per_row = depth_to_rows(image_data.yellow_depth);
1235 output_data.lcyan_data_rows_per_row = depth_to_rows(image_data.lcyan_depth);
1236 output_data.lmagenta_data_rows_per_row = depth_to_rows(image_data.lmagenta_depth);
1237
1238 /*
1239 * Allocate some storage for the expected planes
1240 */
1241
1242 output_data.buffer_length = (image_data.image_width + 7) / 8;
1243
1244 if (output_data.black_data_rows_per_row != 0) {
1245 output_data.black_bufs = stp_zalloc(output_data.black_data_rows_per_row * sizeof (char *));
1246 for (i=0; i < output_data.black_data_rows_per_row; i++) {
1247 output_data.black_bufs[i] = stp_zalloc(output_data.buffer_length * sizeof (char) * output_data.input_depth * output_data.pixels_depth);
1248 }
1249 }
1250 if (output_data.cyan_data_rows_per_row != 0) {
1251 output_data.cyan_bufs = stp_zalloc(output_data.cyan_data_rows_per_row * sizeof (char *));
1252 for (i=0; i < output_data.cyan_data_rows_per_row; i++) {
1253 output_data.cyan_bufs[i] = stp_zalloc(output_data.buffer_length * sizeof (char) * output_data.input_depth * output_data.pixels_depth);
1254 }
1255 }
1256 if (output_data.magenta_data_rows_per_row != 0) {
1257 output_data.magenta_bufs = stp_zalloc(output_data.magenta_data_rows_per_row * sizeof (char *));
1258 for (i=0; i < output_data.magenta_data_rows_per_row; i++) {
1259 output_data.magenta_bufs[i] = stp_zalloc(output_data.buffer_length * sizeof (char) * output_data.input_depth * output_data.pixels_depth);
1260 }
1261 }
1262 if (output_data.yellow_data_rows_per_row != 0) {
1263 output_data.yellow_bufs = stp_zalloc(output_data.yellow_data_rows_per_row * sizeof (char *));
1264 for (i=0; i < output_data.yellow_data_rows_per_row; i++) {
1265 output_data.yellow_bufs[i] = stp_zalloc(output_data.buffer_length * sizeof (char) * output_data.input_depth * output_data.pixels_depth);
1266 }
1267 }
1268 if (output_data.lcyan_data_rows_per_row != 0) {
1269 output_data.lcyan_bufs = stp_zalloc(output_data.lcyan_data_rows_per_row * sizeof (char *));
1270 for (i=0; i < output_data.lcyan_data_rows_per_row; i++) {
1271 output_data.lcyan_bufs[i] = stp_zalloc(output_data.buffer_length * sizeof (char) * output_data.input_depth * output_data.pixels_depth);
1272 }
1273 }
1274 if (output_data.lmagenta_data_rows_per_row != 0) {
1275 output_data.lmagenta_bufs = stp_zalloc(output_data.lmagenta_data_rows_per_row * sizeof (char *));
1276 for (i=0; i < output_data.lmagenta_data_rows_per_row; i++) {
1277 output_data.lmagenta_bufs[i] = stp_zalloc(output_data.buffer_length * sizeof (char) * output_data.input_depth * output_data.pixels_depth);
1278 }
1279 }
1280
1281 /*
1282 * Now store the pointers in the right order to make life easier in the
1283 * decoding phase
1284 */
1285
1286 expected_data_rows_per_row = output_data.black_data_rows_per_row +
1287 output_data.cyan_data_rows_per_row + output_data.magenta_data_rows_per_row +
1288 output_data.yellow_data_rows_per_row + output_data.lcyan_data_rows_per_row +
1289 output_data.lmagenta_data_rows_per_row;
1290
1291 received_rows = stp_malloc(expected_data_rows_per_row * sizeof(char *));
1292 j = 0;
1293 for (i = 0; i < output_data.black_data_rows_per_row; i++)
1294 received_rows[j++] = output_data.black_bufs[i];
1295 for (i = 0; i < output_data.cyan_data_rows_per_row; i++)
1296 received_rows[j++] = output_data.cyan_bufs[i];
1297 for (i = 0; i < output_data.magenta_data_rows_per_row; i++)
1298 received_rows[j++] = output_data.magenta_bufs[i];
1299 for (i = 0; i < output_data.yellow_data_rows_per_row; i++)
1300 received_rows[j++] = output_data.yellow_bufs[i];
1301 for (i = 0; i < output_data.lcyan_data_rows_per_row; i++)
1302 received_rows[j++] = output_data.lcyan_bufs[i];
1303 for (i = 0; i < output_data.lmagenta_data_rows_per_row; i++)
1304 received_rows[j++] = output_data.lmagenta_bufs[i];
1305 }
1306 break;
1307
1308 case PCL_END_RASTER :
1309 case PCL_END_COLOUR_RASTER :
1310 print_command(command_index, numeric_arg);
1311
1312 if (skip_output == 0) {
1313
1314 /*
1315 * Check that we got the correct number of rows of data. If the expected number is
1316 * -1, we have to go back and fill in the PNM parameters (which is why we remembered
1317 * where they were in the file!)
1318 */
1319
1320 if (image_data.image_height == -1) {
1321 image_data.image_height = image_row_counter;
1322 total_image_rows += image_row_counter;
1323 if (fseek(write_fd, filepos, SEEK_SET) != -1) {
1324 fprintf(write_fd, "%10d %10d\n", image_data.image_width,
1325 total_image_rows);
1326 fseek(write_fd, 0L, SEEK_END);
1327 }
1328 }
1329
1330 if (image_row_counter != image_data.image_height)
1331 fprintf(stderr, "ERROR: Row count mismatch. Expected %d rows, got %d rows.\n",
1332 image_data.image_height, image_row_counter);
1333 else
1334 fprintf(stderr, "\t%d rows processed, max %d.\n",
1335 image_row_counter, max_data_len);
1336
1337 image_data.image_height = -1;
1338 image_row_counter = -1;
1339
1340 if (output_data.black_data_rows_per_row != 0) {
1341 for (i=0; i < output_data.black_data_rows_per_row; i++) {
1342 stp_free(output_data.black_bufs[i]);
1343 }
1344 stp_free(output_data.black_bufs);
1345 output_data.black_bufs = NULL;
1346 }
1347 output_data.black_data_rows_per_row = 0;
1348 if (output_data.cyan_data_rows_per_row != 0) {
1349 for (i=0; i < output_data.cyan_data_rows_per_row; i++) {
1350 stp_free(output_data.cyan_bufs[i]);
1351 }
1352 stp_free(output_data.cyan_bufs);
1353 output_data.cyan_bufs = NULL;
1354 }
1355 output_data.cyan_data_rows_per_row = 0;
1356 if (output_data.magenta_data_rows_per_row != 0) {
1357 for (i=0; i < output_data.magenta_data_rows_per_row; i++) {
1358 stp_free(output_data.magenta_bufs[i]);
1359 }
1360 stp_free(output_data.magenta_bufs);
1361 output_data.magenta_bufs = NULL;
1362 }
1363 output_data.magenta_data_rows_per_row = 0;
1364 if (output_data.yellow_data_rows_per_row != 0) {
1365 for (i=0; i < output_data.yellow_data_rows_per_row; i++) {
1366 stp_free(output_data.yellow_bufs[i]);
1367 }
1368 stp_free(output_data.yellow_bufs);
1369 output_data.yellow_bufs = NULL;
1370 }
1371 output_data.yellow_data_rows_per_row = 0;
1372 if (output_data.lcyan_data_rows_per_row != 0) {
1373 for (i=0; i < output_data.lcyan_data_rows_per_row; i++) {
1374 stp_free(output_data.lcyan_bufs[i]);
1375 }
1376 stp_free(output_data.lcyan_bufs);
1377 output_data.lcyan_bufs = NULL;
1378 }
1379 output_data.lcyan_data_rows_per_row = 0;
1380 if (output_data.lmagenta_data_rows_per_row != 0) {
1381 for (i=0; i < output_data.lmagenta_data_rows_per_row; i++) {
1382 stp_free(output_data.lmagenta_bufs[i]);
1383 }
1384 stp_free(output_data.lmagenta_bufs);
1385 output_data.lmagenta_bufs = NULL;
1386 }
1387 output_data.lmagenta_data_rows_per_row = 0;
1388 stp_free(received_rows);
1389 received_rows = NULL;
1390 }
1391 break;
1392
1393 case PCL_SET_VMI :
1394 print_command(command_index, numeric_arg);
1395 fprintf(stderr, "\n");
1396 break;
1397
1398 case PCL_MEDIA_SIZE :
1399 print_command(command_index, numeric_arg);
1400 switch (numeric_arg) {
1401 case 2 :
1402 fprintf(stderr, "Letter\n");
1403 break;
1404 case 3 :
1405 fprintf(stderr, "Legal\n");
1406 break;
1407 case 6 :
1408 fprintf(stderr, "Tabloid\n");
1409 break;
1410 case 26 :
1411 fprintf(stderr, "A4\n");
1412 break;
1413 case 27 :
1414 fprintf(stderr, "A3\n");
1415 break;
1416 case 101 :
1417 fprintf(stderr, "Custom\n");
1418 break;
1419 default :
1420 fprintf(stderr, "Unknown (%d)\n", numeric_arg);
1421 break;
1422 }
1423 break;
1424
1425 case PCL_MEDIA_TYPE :
1426 print_command(command_index, numeric_arg);
1427 switch (numeric_arg) {
1428 case 0 :
1429 fprintf(stderr, "Plain\n");
1430 break;
1431 case 1 :
1432 fprintf(stderr, "Bond\n");
1433 break;
1434 case 2 :
1435 fprintf(stderr, "Premium\n");
1436 break;
1437 case 3 :
1438 fprintf(stderr, "Glossy/Photo\n");
1439 break;
1440 case 4 :
1441 fprintf(stderr, "Transparency\n");
1442 break;
1443 case 5 :
1444 fprintf(stderr, "Quick-dry Photo\n");
1445 break;
1446 case 6 :
1447 fprintf(stderr, "Quick-dry Transparency\n");
1448 break;
1449 default :
1450 fprintf(stderr, "Unknown (%d)\n", numeric_arg);
1451 break;
1452 }
1453 break;
1454
1455 case PCL_MEDIA_SOURCE :
1456 print_command(command_index, numeric_arg);
1457 switch (numeric_arg) {
1458 case -2 :
1459 fprintf(stderr, "FEED CURRENT\n");
1460 break;
1461 case 0 :
1462 fprintf(stderr, "EJECT\n");
1463 break;
1464 case 1 :
1465 fprintf(stderr, "LJ Tray 2 or Portable CSF or DJ Tray\n");
1466 break;
1467 case 2 :
1468 fprintf(stderr, "Manual\n");
1469 break;
1470 case 3 :
1471 fprintf(stderr, "Envelope\n");
1472 break;
1473 case 4 :
1474 fprintf(stderr, "LJ Tray 3 or Desktop CSF or DJ Tray 2\n");
1475 break;
1476 case 5 :
1477 fprintf(stderr, "LJ Tray 4 or DJ optional\n");
1478 break;
1479 case 7 :
1480 fprintf(stderr, "DJ Autosource\n");
1481 break;
1482 case 8 :
1483 fprintf(stderr, "LJ Tray 1\n");
1484 break;
1485 default :
1486 fprintf(stderr, "Unknown (%d)\n", numeric_arg);
1487 break;
1488 }
1489 break;
1490
1491 case PCL_SHINGLING :
1492 print_command(command_index, numeric_arg);
1493 switch (numeric_arg) {
1494 case 0 :
1495 fprintf(stderr, "None\n");
1496 break;
1497 case 1 :
1498 fprintf(stderr, "2 passes\n");
1499 break;
1500 case 2 :
1501 fprintf(stderr, "4 passes\n");
1502 break;
1503 default :
1504 fprintf(stderr, "Unknown (%d)\n", numeric_arg);
1505 break;
1506 }
1507 break;
1508
1509 case PCL_RASTERGRAPHICS_QUALITY :
1510 print_command(command_index, numeric_arg);
1511 switch (numeric_arg) {
1512 case 0 :
1513 fprintf(stderr, "(set by printer controls)\n");
1514 break;
1515 case 1 :
1516 fprintf(stderr, "Draft\n");
1517 break;
1518 case 2 :
1519 fprintf(stderr, "High\n");
1520 break;
1521 default :
1522 fprintf(stderr, "Unknown (%d)\n", numeric_arg);
1523 break;
1524 }
1525 break;
1526
1527 case PCL_DEPLETION :
1528 print_command(command_index, numeric_arg);
1529 switch (numeric_arg) {
1530 case 1 :
1531 fprintf(stderr, "None\n");
1532 break;
1533 case 2 :
1534 fprintf(stderr, "25%%\n");
1535 break;
1536 case 3 :
1537 fprintf(stderr, "50%%\n");
1538 break;
1539 case 5 :
1540 fprintf(stderr, "50%% with gamma correction\n");
1541 break;
1542 default :
1543 fprintf(stderr, "Unknown (%d)\n", numeric_arg);
1544 break;
1545 }
1546 break;
1547
1548 case PCL_PRINT_QUALITY :
1549 print_command(command_index, numeric_arg);
1550 switch (numeric_arg) {
1551 case -1 :
1552 fprintf(stderr, "Draft\n");
1553 break;
1554 case 0 :
1555 fprintf(stderr, "Normal\n");
1556 break;
1557 case 1 :
1558 fprintf(stderr, "Presentation\n");
1559 break;
1560 default :
1561 fprintf(stderr, "Unknown (%d)\n", numeric_arg);
1562 break;
1563 }
1564 break;
1565
1566 case PCL_RELATIVE_VERTICAL_PIXEL_MOVEMENT :
1567 print_command(command_index, numeric_arg);
1568
1569 if (skip_output == 0) {
1570
1571 /* Check that we are in raster mode */
1572
1573 if (expected_data_rows_per_row == -1)
1574 fprintf(stderr, "ERROR: raster data without start raster!\n");
1575
1576 /*
1577 What we need to do now is to write out "N" rows of all-white data to
1578 simulate the vertical slew
1579 */
1580
1581 for (i=0; i<expected_data_rows_per_row; i++)
1582 {
1583 memset(received_rows[i], 0, (size_t) output_data.buffer_length * sizeof(char) * output_data.output_depth);
1584 }
1585 for (i=0; i<numeric_arg; i++)
1586 {
1587 if (image_data.colour_type == PCL_MONO) {
1588 if (image_data.pixel_type == PCL_MONO) {
1589 write_grey(&output_data, &image_data);
1590 } else {
1591 write_pixel(&output_data, &image_data);
1592 }
1593 } else {
1594 write_colour(&output_data, &image_data);
1595 }
1596 image_row_counter++;
1597 }
1598 }
1599 fprintf(stderr, "\n");
1600 break;
1601
1602 case PCL_DUPLEX :
1603 print_command(command_index, numeric_arg);
1604 switch (numeric_arg) {
1605 case 0 :
1606 fprintf(stderr, "None\n");
1607 break;
1608 case 1 :
1609 fprintf(stderr, "Duplex No Tumble (Long Edge)\n");
1610 break;
1611 case 2 :
1612 fprintf(stderr, "Duplex Tumble (Short Edge)\n");
1613 break;
1614 default :
1615 fprintf(stderr, "Unknown (%d)\n", numeric_arg);
1616 break;
1617 }
1618 break;
1619
1620 case PCL_DRIVER_CONFIG :
1621 print_command(command_index, numeric_arg);
1622 fprintf(stderr, "(ignored)");
1623 fprintf(stderr, " Data: ");
1624 for (i=0; i < numeric_arg; i++) {
1625 fprintf(stderr, "%02x ", (unsigned char) data_buffer[i]);
1626 }
1627 fprintf(stderr, "\n");
1628 fprintf(stderr, " Device: ");
1629 switch (data_buffer[0]) {
1630 case 6:
1631 fprintf(stderr, "Color Laser Printer\n");
1632 break;
1633 default:
1634 fprintf(stderr, "Unknown\n");
1635 break;
1636 }
1637 fprintf(stderr, " Function: ");
1638 switch (data_buffer[1]) {
1639 case 0:
1640 fprintf(stderr, "Lightness %d\n", data_buffer[2]);
1641 break;
1642 case 1:
1643 fprintf(stderr, "Saturation %d\n", data_buffer[2]);
1644 break;
1645 case 3:
1646 fprintf(stderr, "Scaling Algorithm: ");
1647 switch (data_buffer[2]) {
1648 case 0:
1649 fprintf(stderr, "Pixel Replication\n");
1650 break;
1651 case 1:
1652 fprintf(stderr, "Bilinear Interpolation\n");
1653 break;
1654 case 2:
1655 fprintf(stderr, "Modified Bilinear Interpolation\n");
1656 break;
1657 default:
1658 fprintf(stderr, "Unknown\n");
1659 }
1660 break;
1661 case 4:
1662 fprintf(stderr, "Color Treatment: ");
1663 switch (data_buffer[2]) {
1664 case 0:
1665 fprintf(stderr, "No Adjustment\n");
1666 break;
1667 case 1:
1668 fprintf(stderr, "Process Blue\n");
1669 break;
1670 case 2:
1671 fprintf(stderr, "Vivid Graphics\n");
1672 break;
1673 case 3:
1674 fprintf(stderr, "Transparency\n");
1675 break;
1676 case 4:
1677 fprintf(stderr, "Out of Gamut\n");
1678 break;
1679 case 5:
1680 fprintf(stderr, "CIE Lab Match\n");
1681 break;
1682 case 6:
1683 fprintf(stderr, "Screen Match\n");
1684 break;
1685 default:
1686 fprintf(stderr, "Unknown\n");
1687 }
1688 break;
1689 case 5:
1690 fprintf(stderr, "Download Color Map: ");
1691 switch (data_buffer[2]) {
1692 case 1:
1693 fprintf(stderr, "CMY Color Space\n");
1694 break;
1695 case 3:
1696 fprintf(stderr, "CIE Lab Color Space\n");
1697 break;
1698 default:
1699 fprintf(stderr, "Unknown\n");
1700 }
1701 break;
1702 default:
1703 fprintf(stderr, "Unknown\n");
1704 break;
1705 }
1706 break;
1707 case PCL_PERF_SKIP :
1708 case PCL_TOP_MARGIN :
1709 case PCL_RESOLUTION :
1710 case PCL_LEFTRASTER_POS :
1711 case PCL_TOPRASTER_POS :
1712 case PCL_VERTICAL_CURSOR_POSITIONING_BY_DOTS :
1713 case PCL_HORIZONTAL_CURSOR_POSITIONING_BY_DOTS :
1714 case PCL_PALETTE_CONFIGURATION :
1715 case PCL_UNIT_OF_MEASURE :
1716 case PCL_GRAY_BALANCE :
1717 case PCL_LPI :
1718 case PCL_CPI :
1719 case PCL_PAGE_LENGTH :
1720 case PCL_NUM_COPIES :
1721 case PCL_ENTER_PCL :
1722 case PCL_NEGATIVE_MOTION :
1723 case PCL_JOB_SEPARATION :
1724 case PCL_LEFT_OFFSET_REGISTRATION :
1725 case PCL_TOP_OFFSET_REGISTRATION :
1726 case PCL_PRINT_DIRECTION :
1727 case PCL_LEFT_MARGIN :
1728 case PCL_RIGHT_MARGIN :
1729 case PCL_TEXT_LENGTH :
1730 print_command(command_index, numeric_arg);
1731 fprintf(stderr, "(ignored)");
1732 if (pcl_commands[command_index].has_data == 1) {
1733 fprintf(stderr, " Data: ");
1734 for (i=0; i < numeric_arg; i++) {
1735 fprintf(stderr, "%02x ", (unsigned char) data_buffer[i]);
1736 }
1737 }
1738 fprintf(stderr, "\n");
1739 break;
1740
1741 case PCL_COLOURTYPE :
1742 print_command(command_index, numeric_arg);
1743 image_data.colour_type = -numeric_arg;
1744 switch (image_data.colour_type) {
1745 case PCL_MONO :
1746 fprintf(stderr, "MONO\n");
1747 break;
1748 case PCL_CMY :
1749 fprintf(stderr, "CMY (one cart)\n");
1750 image_data.black_depth = 0; /* Black levels */
1751 image_data.cyan_depth = 2; /* Cyan levels */
1752 image_data.magenta_depth = 2; /* Magenta levels */
1753 image_data.yellow_depth = 2; /* Yellow levels */
1754 break;
1755 case PCL_CMYK :
1756 fprintf(stderr, "CMYK (two cart)\n");
1757 image_data.black_depth = 2; /* Black levels */
1758 image_data.cyan_depth = 2; /* Cyan levels */
1759 image_data.magenta_depth = 2; /* Magenta levels */
1760 image_data.yellow_depth = 2; /* Yellow levels */
1761 break;
1762 default :
1763 fprintf(stderr, "Unknown (%d)\n", -numeric_arg);
1764 break;
1765 }
1766 break;
1767
1768 case PCL_COMPRESSIONTYPE :
1769 print_command(command_index, numeric_arg);
1770 image_data.compression_type = numeric_arg;
1771 switch (image_data.compression_type) {
1772 case PCL_COMPRESSION_NONE :
1773 fprintf(stderr, "NONE\n");
1774 break;
1775 case PCL_COMPRESSION_RUNLENGTH :
1776 fprintf(stderr, "Runlength\n");
1777 break;
1778 case PCL_COMPRESSION_TIFF :
1779 fprintf(stderr, "TIFF\n");
1780 break;
1781 case PCL_COMPRESSION_DELTA :
1782 fprintf(stderr, "DELTA\n");
1783 break;
1784 case PCL_COMPRESSION_ADAPTIVE :
1785 fprintf(stderr, "ADAPTIVE (mono only)\n");
1786 break;
1787 case PCL_COMPRESSION_CRDR :
1788 fprintf(stderr, "Compressed Row Delta Replacement\n");
1789 break;
1790 default :
1791 fprintf(stderr, "Unknown (%d)\n", image_data.compression_type);
1792 break;
1793 }
1794 break;
1795
1796 case PCL_RASTER_WIDTH :
1797 print_command(command_index, numeric_arg);
1798 fprintf(stderr, "\n");
1799 image_data.image_width = numeric_arg;
1800 break;
1801
1802 case PCL_RASTER_HEIGHT :
1803 print_command(command_index, numeric_arg);
1804 fprintf(stderr, "\n");
1805 image_data.image_height = numeric_arg;
1806 break;
1807
1808 case PCL_SEED_ROW_SOURCE :
1809 print_command(command_index, numeric_arg);
1810 fprintf(stderr, "\n");
1811 break;
1812
1813 case PCL_RTL_CONFIGURE :
1814 print_command(command_index, numeric_arg);
1815 for (i = 0; i < numeric_arg; i++)
1816 fprintf(stderr, " %02x", (unsigned char) data_buffer[i]);
1817 fprintf(stderr, "\n");
1818 fprintf(stderr, " Color Space: ");
1819 switch (data_buffer[0]) {
1820 case 0:
1821 fprintf(stderr, "DeviceRGB\n");
1822 image_data.pixel_type = PCL_RGB;
1823 break;
1824 case 1:
1825 fprintf(stderr, "DeviceCMY\n");
1826 image_data.pixel_type = PCL_CMY;
1827 break;
1828 case 2:
1829 fprintf(stderr, "sRGB\n");
1830 image_data.pixel_type = PCL_RGB;
1831 break;
1832 default:
1833 fprintf(stderr, "Unknown\n");
1834 break;
1835 }
1836 fprintf(stderr, " Encoding: ");
1837 switch (data_buffer[1]) {
1838 case 0:
1839 case 1:
1840 fprintf(stderr, "Indexed by %s, %d bits/index",
1841 data_buffer[1] == 0 ? "Plane" : "Pixel",
1842 data_buffer[2]);
1843 output_data.input_depth = data_buffer[2];
1844 break;
1845 case 2:
1846 case 3:
1847 fprintf(stderr, "Direct by %s, depth %d\n",
1848 data_buffer[1] == 2 ? "Plane" : "Pixel",
1849 data_buffer[3]);
1850 output_data.pixels_depth = 3;
1851 output_data.input_depth = data_buffer[3];
1852 break;
1853 default:
1854 fprintf(stderr, "Unknown\n");
1855 break;
1856 }
1857 break;
1858
1859 case PCL_CONFIGURE :
1860 print_command(command_index, numeric_arg);
1861 fprintf(stderr, "\tFormat: %d\n", data_buffer[0]);
1862 if (data_buffer[0] == 2) {
1863
1864 /*
1865 * the data that follows depends on the colour type (buffer[1]). The size
1866 * of the data should be 2 + (6 * number of planes).
1867 */
1868
1869 fprintf(stderr, "\tOutput Planes: ");
1870 image_data.colour_type = data_buffer[1]; /* # output planes */
1871 switch (image_data.colour_type) {
1872 case PCL_MONO :
1873 fprintf(stderr, "MONO\n");
1874
1875 /* Size should be 8 */
1876
1877 if (numeric_arg != 8)
1878 fprintf(stderr, "ERROR: Expected 8 bytes of data, got %d\n", numeric_arg);
1879
1880 fprintf(stderr, "\tBlack: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[2]<<8)+(unsigned char)data_buffer[3],
1881 ((unsigned char) data_buffer[4]<<8)+(unsigned char) data_buffer[5], data_buffer[7]);
1882 image_data.black_depth = data_buffer[7]; /* Black levels */
1883 break;
1884 case PCL_CMY :
1885 fprintf(stderr, "CMY (one cart)\n");
1886
1887 /* Size should be 20 */
1888
1889 if (numeric_arg != 20)
1890 fprintf(stderr, "ERROR: Expected 20 bytes of data, got %d\n", numeric_arg);
1891
1892 fprintf(stderr, "\tCyan: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[2]<<8)+(unsigned char) data_buffer[3],
1893 ((unsigned char) data_buffer[4]<<8)+(unsigned char) data_buffer[5], data_buffer[7]);
1894 fprintf(stderr, "\tMagenta: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[8]<<8)+(unsigned char) data_buffer[9],
1895 ((unsigned char) data_buffer[10]<<8)+(unsigned char) data_buffer[11], data_buffer[13]);
1896 fprintf(stderr, "\tYellow: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[14]<<8)+(unsigned char) data_buffer[15],
1897 ((unsigned char) data_buffer[16]<<8)+(unsigned char) data_buffer[17], data_buffer[19]);
1898 image_data.black_depth = 0;
1899 image_data.cyan_depth = data_buffer[7]; /* Cyan levels */
1900 image_data.magenta_depth = data_buffer[13]; /* Magenta levels */
1901 image_data.yellow_depth = data_buffer[19]; /* Yellow levels */
1902 break;
1903 case PCL_CMYK :
1904 fprintf(stderr, "CMYK (two cart)\n");
1905
1906 /* Size should be 26 */
1907
1908 if (numeric_arg != 26)
1909 fprintf(stderr, "ERROR: Expected 26 bytes of data, got %d\n", numeric_arg);
1910
1911 fprintf(stderr, "\tBlack: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[2]<<8)+(unsigned char) data_buffer[3],
1912 ((unsigned char) data_buffer[4]<<8)+(unsigned char) data_buffer[5], data_buffer[7]);
1913 fprintf(stderr, "\tCyan: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[8]<<8)+(unsigned char) data_buffer[9],
1914 ((unsigned char) data_buffer[10]<<8)+(unsigned char) data_buffer[11], data_buffer[13]);
1915 fprintf(stderr, "\tMagenta: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[14]<<8)+(unsigned char) data_buffer[15],
1916 ((unsigned char) data_buffer[16]<<8)+(unsigned char) data_buffer[17], data_buffer[19]);
1917 fprintf(stderr, "\tYellow: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[20]<<8)+(unsigned char) data_buffer[21],
1918 ((unsigned char) data_buffer[22]<<8)+(unsigned char) data_buffer[23], data_buffer[25]);
1919 image_data.black_depth = data_buffer[7]; /* Black levels */
1920 image_data.cyan_depth = data_buffer[13]; /* Cyan levels */
1921 image_data.magenta_depth = data_buffer[19]; /* Magenta levels */
1922 image_data.yellow_depth = data_buffer[25]; /* Yellow levels */
1923 break;
1924 case PCL_CMYKcm :
1925 fprintf(stderr, "CMYKcm (two cart photo)\n");
1926
1927 /* Size should be 38 */
1928
1929 if (numeric_arg != 38)
1930 fprintf(stderr, "ERROR: Expected 38 bytes of data, got %d\n", numeric_arg);
1931
1932 fprintf(stderr, "\tBlack: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[2]<<8)+(unsigned char) data_buffer[3],
1933 ((unsigned char) data_buffer[4]<<8)+(unsigned char) data_buffer[5], data_buffer[7]);
1934 fprintf(stderr, "\tCyan: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[8]<<8)+(unsigned char) data_buffer[9],
1935 ((unsigned char) data_buffer[10]<<8)+(unsigned char) data_buffer[11], data_buffer[13]);
1936 fprintf(stderr, "\tMagenta: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[14]<<8)+(unsigned char) data_buffer[15],
1937 ((unsigned char) data_buffer[16]<<8)+(unsigned char) data_buffer[17], data_buffer[19]);
1938 fprintf(stderr, "\tYellow: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[20]<<8)+(unsigned char) data_buffer[21],
1939 ((unsigned char) data_buffer[22]<<8)+(unsigned char) data_buffer[23], data_buffer[25]);
1940 fprintf(stderr, "\tLight Cyan: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[26]<<8)+(unsigned char) data_buffer[27],
1941 ((unsigned char) data_buffer[28]<<8)+(unsigned char) data_buffer[29], data_buffer[31]);
1942 fprintf(stderr, "\tLight Magenta: X dpi: %d, Y dpi: %d, Levels: %d\n", ((unsigned char) data_buffer[32]<<8)+(unsigned char) data_buffer[33],
1943 ((unsigned char) data_buffer[34]<<8)+(unsigned char) data_buffer[35], data_buffer[37]);
1944 image_data.black_depth = data_buffer[7]; /* Black levels */
1945 image_data.cyan_depth = data_buffer[13]; /* Cyan levels */
1946 image_data.magenta_depth = data_buffer[19]; /* Magenta levels */
1947 image_data.yellow_depth = data_buffer[25]; /* Yellow levels */
1948 image_data.lcyan_depth = data_buffer[31]; /* Cyan levels */
1949 image_data.lmagenta_depth = data_buffer[37]; /* Magenta levels */
1950 break;
1951 default :
1952 fprintf(stderr, "Unknown (%d)\n", data_buffer[1]);
1953 break;
1954 }
1955 }
1956 else {
1957 fprintf(stderr, "Unknown format %d\n", data_buffer[0]);
1958 fprintf(stderr, "Data: ");
1959 for (i=0; i < numeric_arg; i++) {
1960 fprintf(stderr, "%02x ", (unsigned char) data_buffer[i]);
1961 }
1962 fprintf(stderr, "\n");
1963 }
1964 break;
1965
1966 case PCL_DATA :
1967 case PCL_DATA_LAST :
1968 if (skip_output == 1) {
1969 print_command(command_index, numeric_arg);
1970 fprintf(stderr, "\n");
1971 }
1972 else {
1973
1974 /*
1975 * Make sure that we have enough data to process this command!
1976 */
1977
1978 if (expected_data_rows_per_row == -1)
1979 fprintf(stderr, "ERROR: raster data without start raster!\n");
1980
1981 if (numeric_arg > max_data_len)
1982 max_data_len = numeric_arg;
1983 /*
1984 * The last flag indicates that this is the end of the planes for a row
1985 * so we check it against the number of planes we have seen and are
1986 * expecting.
1987 */
1988
1989 if (command == PCL_DATA_LAST) {
1990 if (current_data_row != (expected_data_rows_per_row - 1))
1991 fprintf(stderr, "ERROR: 'Last Plane' set on plane %d of %d!\n",
1992 current_data_row, expected_data_rows_per_row);
1993 }
1994 else {
1995 if (current_data_row == (expected_data_rows_per_row - 1))
1996 fprintf(stderr, "ERROR: Expected 'last plane', but not set!\n");
1997 }
1998
1999 /*
2000 * Accumulate the data rows for each output row,then write the image.
2001 */
2002 /*
2003 fprintf(stderr, ">>>>>Current %d %p %p %p\n",
2004 current_data_row, received_rows,
2005 data_buffer, received_rows[current_data_row]);
2006 */
2007
2008 if (image_data.compression_type == PCL_COMPRESSION_NONE) {
2009 memcpy(received_rows[current_data_row], &data_buffer, (size_t) numeric_arg);
2010 output_data.active_length = numeric_arg * output_data.input_depth * output_data.pixels_depth;
2011 }
2012 else if (image_data.compression_type == PCL_COMPRESSION_TIFF) {
2013 output_data.active_length = decode_tiff(data_buffer, numeric_arg, received_rows[current_data_row], output_data.buffer_length * output_data.input_depth * output_data.pixels_depth);
2014
2015 }
2016 else if (image_data.compression_type == PCL_COMPRESSION_DELTA) {
2017 output_data.active_length = decode_delta(data_buffer, numeric_arg, received_rows[current_data_row], output_data.buffer_length * output_data.input_depth * output_data.pixels_depth);
2018 }
2019 /*
2020 fprintf(stderr, "<<<<<Current %d %p %p %p\n",
2021 current_data_row, received_rows,
2022 data_buffer, received_rows[current_data_row]);
2023 */
2024 if (command == PCL_DATA_LAST) {
2025 if (image_data.colour_type == PCL_MONO) {
2026 if (image_data.pixel_type == PCL_MONO) {
2027 write_grey(&output_data, &image_data);
2028 } else {
2029 write_pixel(&output_data, &image_data);
2030 }
2031 } else {
2032 write_colour(&output_data, &image_data);
2033 }
2034 current_data_row = 0;
2035 image_row_counter++;
2036 }
2037 else
2038 current_data_row++;
2039 }
2040 break;
2041
2042 case PCL_ENTER_HPGL2 :
2043 case PCL_ENTER_PJL : {
2044 int c;
2045 print_command(command_index, numeric_arg);
2046
2047 /*
2048 * This is a special command. Read up to the next ESC and output it.
2049 */
2050
2051 c = 0;
2052 while (1) {
2053 fill_buffer();
2054 if (eof == 1) {
2055 fprintf(stderr, "\n");
2056 break;
2057 }
2058 c = read_buffer[read_pointer++];
2059 if (c == '\033')
2060 break;
2061 fprintf(stderr, "%c", c);
2062 }
2063
2064 /*
2065 * Now we are sitting at the "ESC" that is the start of the next command.
2066 */
2067 read_pointer--;
2068 fprintf(stderr, "\n");
2069 fprintf(stderr, "End of %s\n", pcl_commands[command_index].description);
2070 }
2071 break;
2072
2073 case PCL_PAGE_ORIENTATION :
2074 print_command(command_index, numeric_arg);
2075 switch (numeric_arg) {
2076 case 0 :
2077 fprintf(stderr, "Portrait");
2078 break;
2079 case 1 :
2080 fprintf(stderr, "Landscape");
2081 break;
2082 case 2 :
2083 fprintf(stderr, "Reverse Portrait");
2084 break;
2085 case 3 :
2086 fprintf(stderr, "Reverse Landscape");
2087 break;
2088 default :
2089 fprintf(stderr, "Unknown (%d)", numeric_arg);
2090 break;
2091 }
2092 fprintf(stderr, " (ignored)\n");
2093 break;
2094
2095 case PCL_MEDIA_SIDE :
2096 print_command(command_index, numeric_arg);
2097 switch (numeric_arg) {
2098 case 0 :
2099 fprintf(stderr, "Next side");
2100 break;
2101 case 1 :
2102 fprintf(stderr, "Front side");
2103 break;
2104 case 2 :
2105 fprintf(stderr, "Back side");
2106 break;
2107 default :
2108 fprintf(stderr, "Unknown (%d)", numeric_arg);
2109 break;
2110 }
2111 fprintf(stderr, " (ignored)\n");
2112 break;
2113
2114 case PCL_MEDIA_DEST :
2115 print_command(command_index, numeric_arg);
2116 switch (numeric_arg) {
2117 case 1 :
2118 fprintf(stderr, "Upper Output bin");
2119 break;
2120 case 2 :
2121 fprintf(stderr, "Lower (Rear) Output bin");
2122 break;
2123 default :
2124 fprintf(stderr, "Unknown (%d)", numeric_arg);
2125 break;
2126 }
2127 fprintf(stderr, " (ignored)\n");
2128 break;
2129
2130 case PCL_RASTER_RENDER :
2131 print_command(command_index, numeric_arg);
2132 switch (numeric_arg) {
2133 case 0 :
2134 fprintf(stderr, "Continuous tone detail (high lpi)");
2135 break;
2136 case 3 :
2137 fprintf(stderr, "Device-best dither");
2138 break;
2139 case 15 :
2140 fprintf(stderr, "Continuous tone smooth (high lpi)");
2141 break;
2142 case 18 :
2143 fprintf(stderr, "Continuous tone basic (low lpi)");
2144 break;
2145 default :
2146 fprintf(stderr, "Unknown (%d)", numeric_arg);
2147 break;
2148 }
2149 fprintf(stderr, " (ignored)\n");
2150 break;
2151 case PCL_COLOR_COMPONENT_1 :
2152 case PCL_COLOR_COMPONENT_2 :
2153 case PCL_COLOR_COMPONENT_3 :
2154 case PCL_ASSIGN_COLOR_INDEX :
2155 print_command(command_index, numeric_arg);
2156 fprintf(stderr, " (ignored)\n");
2157 break;
2158 case PCL_SET_FOREGROUND_COLOR :
2159 print_command(command_index, numeric_arg);
2160 fprintf(stderr, " (ignored)\n");
2161 break;
2162
2163 default :
2164 fprintf(stderr, "ERROR: No handler for %s!\n", pcl_commands[command_index].description);
2165 break;
2166 }
2167 }
2168 }
2169 }