Debian Codegraph @

converter/other/pnmtopclxl.c

raw
1 /*
2 * -------------------------------------------------------------
3 *
4 * (C) 2002 Jochen Karrer, Linuxdata GbR
5 *
6 * convert a pnm to PCL-XL image
7 *
8 * -------------------------------------------------------------
9 */
10
11 /* Engineering note: One PCL-XL printer prints an error message like
12 this when it doesn't like the PCL it sees:
13
14 PCL XL error
15 Subsystem: IMAGE
16 Error: IllegalAttributeValue
17 Operator: ReadImage
18 Position: 8
19
20 "Position" is the sequence number of the PCL operator it was trying
21 to execute.
22 */
23
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <ctype.h>
31
32 #include "pm_c_util.h"
33 #include "pam.h"
34 #include "shhopt.h"
35 #include "mallocvar.h"
36 #include "nstring.h"
37 #include "runlength.h"
38
39 #include "pclxl.h"
40
41 #define PAPERWIDTH(format) (xlPaperFormats[format].width)
42 #define PAPERHEIGHT(format) (xlPaperFormats[format].height)
43
44
45
46 typedef struct InputSource {
47 const char * name;
48 struct InputSource * next;
49 } InputSource;
50
51
52
53 struct cmdlineInfo {
54 /* All the information the user supplied in the command line,
55 in a form easy for the program to use.
56 */
57 InputSource * sourceP;
58 unsigned int dpi;
59 enum MediaSize format;
60 unsigned int feederSpec;
61 int feeder;
62 unsigned int outtraySpec;
63 int outtray;
64 unsigned int duplexSpec;
65 enum DuplexPageMode duplex;
66 unsigned int copiesSpec;
67 int copies;
68 unsigned int center;
69 float xoffs;
70 float yoffs;
71 unsigned int colorok;
72 unsigned int verbose;
73 const char * jobsetup; /* -jobsetup option value. NULL if none */
74 unsigned int rendergray;
75 unsigned int embedded;
76 };
77
78
79
80 static void
81 parseCommandLine(int argc, char ** argv,
82 struct cmdlineInfo * const cmdlineP) {
83 /*----------------------------------------------------------------------------
84 parse program command line described in Unix standard form by argc
85 and argv. Return the information in the options as *cmdlineP.
86
87 If command line is internally inconsistent (invalid options, etc.),
88 issue error message to stderr and abort program.
89
90 Note that the strings we return are stored in the storage that
91 was passed to us as the argv array. We also trash *argv.
92 -----------------------------------------------------------------------------*/
93 optEntry *option_def = malloc( 100*sizeof( optEntry ) );
94 /* Instructions to pm_optParseOptions3 on how to parse our options.
95 */
96 optStruct3 opt;
97
98 unsigned int option_def_index;
99
100 char *formatOpt;
101 char *duplexOpt;
102 unsigned int dpiSpec, xoffsSpec, yoffsSpec, formatSpec, jobsetupSpec;
103
104 option_def_index = 0; /* incremented by OPTENT3 */
105 OPTENT3(0, "dpi", OPT_UINT, &cmdlineP->dpi,
106 &dpiSpec, 0);
107 OPTENT3(0, "xoffs", OPT_FLOAT, &cmdlineP->xoffs,
108 &xoffsSpec, 0);
109 OPTENT3(0, "yoffs", OPT_FLOAT, &cmdlineP->yoffs,
110 &yoffsSpec, 0);
111 OPTENT3(0, "format", OPT_STRING, &formatOpt,
112 &formatSpec, 0);
113 OPTENT3(0, "duplex", OPT_STRING, &duplexOpt,
114 &cmdlineP->duplexSpec, 0);
115 OPTENT3(0, "copies", OPT_UINT, &cmdlineP->copies,
116 &cmdlineP->copiesSpec, 0);
117 OPTENT3(0, "colorok", OPT_FLAG, NULL,
118 &cmdlineP->colorok, 0);
119 OPTENT3(0, "center", OPT_FLAG, NULL,
120 &cmdlineP->center, 0 );
121 OPTENT3(0, "feeder", OPT_UINT, &cmdlineP->feeder,
122 &cmdlineP->feederSpec, 0);
123 OPTENT3(0, "outtray", OPT_UINT, &cmdlineP->outtray,
124 &cmdlineP->outtraySpec, 0);
125 OPTENT3(0, "verbose", OPT_FLAG, NULL,
126 &cmdlineP->verbose, 0);
127 OPTENT3(0, "jobsetup", OPT_STRING, &cmdlineP->jobsetup,
128 &jobsetupSpec, 0);
129 OPTENT3(0, "rendergray", OPT_FLAG, NULL,
130 &cmdlineP->rendergray, 0 );
131 OPTENT3(0, "embedded", OPT_FLAG, NULL,
132 &cmdlineP->embedded, 0 );
133
134 opt.opt_table = option_def;
135 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
136 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
137
138 pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0);
139 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
140
141 if (!dpiSpec)
142 cmdlineP->dpi = 300;
143 if (!xoffsSpec)
144 cmdlineP->xoffs = 0.0;
145 if (!yoffsSpec)
146 cmdlineP->yoffs = 0.0;
147
148 if (cmdlineP->duplexSpec) {
149 if (strncmp(duplexOpt, "vertical", strlen(duplexOpt)) == 0)
150 cmdlineP->duplex = eDuplexVerticalBinding;
151 else if (strncmp(duplexOpt, "horizontal", strlen(duplexOpt)) == 0)
152 cmdlineP->duplex = eDuplexHorizontalBinding;
153 else
154 pm_error("Invalid value '%s' for -duplex option", duplexOpt);
155 }
156
157 if (formatSpec) {
158 bool found;
159 int i;
160 for (i = 0, found=FALSE; xlPaperFormats[i].name && !found; ++i) {
161 if (streq(xlPaperFormats[i].name, formatOpt)) {
162 found = TRUE;
163 cmdlineP->format = xlPaperFormats[i].xl_nr;
164 }
165 }
166 if (!found) {
167 int i;
168 pm_message("Valid -format values:");
169 for (i = 0; xlPaperFormats[i].name; ++i) {
170 if (xlPaperFormats[i].width > 0)
171 pm_message(" %s", xlPaperFormats[i].name);
172 }
173 pm_error("Invalid -format option '%s' specified.", formatOpt);
174 }
175 } else
176 cmdlineP->format = eLetterPaper;
177
178 if (!jobsetupSpec)
179 cmdlineP->jobsetup = NULL;
180
181 if (cmdlineP->embedded) {
182 if (xoffsSpec || yoffsSpec || formatSpec || cmdlineP->duplexSpec
183 || cmdlineP->copiesSpec || dpiSpec || cmdlineP->center
184 || cmdlineP->feederSpec || cmdlineP->outtraySpec
185 || jobsetupSpec || cmdlineP->rendergray)
186 pm_error("With -embedded, you may not specify "
187 "-xoffs, -yoffs, -format, -duplex, copies, -dpi, "
188 "-center, -feeder, -outtray, -jobsetup, or -rendergray");
189
190 if (argc-1 > 1)
191 pm_message("With -embedded, you may not specify more than one "
192 "input image. You specified %u", argc-1);
193 }
194
195 if (argc-1 < 1) {
196 MALLOCVAR(cmdlineP->sourceP);
197 cmdlineP->sourceP->name = "-";
198 cmdlineP->sourceP->next = NULL;
199 } else {
200 unsigned int i;
201 InputSource ** nextLinkP;
202
203 nextLinkP = &cmdlineP->sourceP;
204 for (i = 1; i < argc; ++i) {
205 InputSource * sourceP;
206 MALLOCVAR(sourceP);
207 sourceP->name = argv[i];
208 *nextLinkP = sourceP;
209 nextLinkP = &sourceP->next;
210 *nextLinkP = NULL;
211 }
212 }
213 }
214
215
216
217 static void
218 freeSource(InputSource * const firstSourceP) {
219
220 InputSource * sourceP;
221
222 sourceP = firstSourceP;
223 while(sourceP) {
224 InputSource * const nextP = sourceP->next;
225 free(sourceP);
226 sourceP = nextP;
227 }
228 }
229
230
231
232 static void
233 freeCmdline(struct cmdlineInfo const cmdline) {
234
235 freeSource(cmdline.sourceP);
236 }
237
238
239
240 static int
241 XY_Write(int const fd,
242 const void * const buf,
243 int const cnt) {
244
245 int len;
246 bool error;
247
248 for (len =0, error = false; len < cnt && !error;) {
249 ssize_t const rc = write(fd, (char*)buf + len , cnt - len);
250 if (rc <= 0)
251 error = true;
252 else
253 len += rc;
254 }
255 return error ? -1 : len;
256 }
257
258
259
260 #define XY_Puts(fd, str) XY_Write(fd, str, strlen(str))
261
262
263
264 typedef struct pclGenerator {
265 enum ColorDepth colorDepth;
266 enum Colorspace colorSpace;
267 int width,height;
268 unsigned int linelen; /* bytes per line */
269 unsigned int paddedLinelen; /* bytes per line + padding */
270 unsigned char * data;
271 unsigned int cursor;
272 void (*getnextrow)(struct pclGenerator *, struct pam *);
273 } pclGenerator;
274
275
276
277 static void
278 pnmToPcllinePackbits(pclGenerator * const pclGeneratorP,
279 struct pam * const pamP) {
280
281 tuple * tuplerow;
282 unsigned char accum;
283 unsigned char bitmask;
284 unsigned int col, padCt;
285 unsigned int const padsize =
286 pclGeneratorP->paddedLinelen - pclGeneratorP->linelen;
287
288 tuplerow = pnm_allocpamrow(pamP);
289
290 pnm_readpamrow(pamP, tuplerow);
291
292 bitmask = 0x80; accum = 0x00;
293 for (col = 0; col < pamP->width; ++col) {
294 if (tuplerow[col][0] == PAM_PBM_WHITE)
295 accum |= bitmask;
296 bitmask >>= 1;
297 if (bitmask == 0) {
298 pclGeneratorP->data[pclGeneratorP->cursor++] = accum;
299 bitmask = 0x80; accum = 0x0;
300 }
301 }
302 if (bitmask != 0x80)
303 pclGeneratorP->data[pclGeneratorP->cursor++] = accum;
304
305 for (padCt = 0; padCt < padsize; ++padCt)
306 pclGeneratorP->data[pclGeneratorP->cursor++] = 0;
307
308 pnm_freepamrow(tuplerow);
309 }
310
311
312
313 static unsigned int
314 pclPaddedLinelen(unsigned int const linelen) {
315
316 if (UINT_MAX - 3 < linelen)
317 pm_error("Image too big to process");
318
319 return (((linelen + 3) / 4) * 4);
320 }
321
322
323
324 static unsigned int
325 pclDatabuffSize(unsigned int const paddedLinelen) {
326
327 if (UINT_MAX / 20 < paddedLinelen)
328 pm_error("Image too big to process");
329
330 return (paddedLinelen * 20);
331 }
332
333
334
335 static void
336 createPclGeneratorPackbits(struct pam * const pamP,
337 pclGenerator ** const pclGeneratorPP) {
338
339 /* Samples are black or white and packed 8 to a byte */
340
341 pclGenerator * pclGeneratorP;
342
343 MALLOCVAR_NOFAIL(pclGeneratorP);
344
345 pclGeneratorP->colorDepth = e1Bit;
346 pclGeneratorP->colorSpace = eGray;
347 pclGeneratorP->linelen = (pamP->width+7)/8;
348 pclGeneratorP->paddedLinelen = pclPaddedLinelen(pclGeneratorP->linelen);
349 pclGeneratorP->height = pamP->height;
350 pclGeneratorP->width = (pamP->width);
351
352 pclGeneratorP->data =
353 malloc(pclDatabuffSize(pclGeneratorP->paddedLinelen));
354
355 if (pclGeneratorP->data == NULL)
356 pm_error("Unable to allocate row buffer.");
357
358 pclGeneratorP->getnextrow = pnmToPcllinePackbits;
359
360 *pclGeneratorPP = pclGeneratorP;
361 }
362
363
364
365 static void
366 pnmToPcllineWholebytes(pclGenerator * const pclGeneratorP,
367 struct pam * const pamP) {
368
369 tuple * tuplerow;
370 unsigned int col, padCt;
371 unsigned int const padsize =
372 pclGeneratorP->paddedLinelen - pclGeneratorP->linelen;
373
374 tuplerow = pnm_allocpamrow(pamP);
375
376 pnm_readpamrow(pamP, tuplerow);
377
378 for (col = 0; col < pamP->width; ++col) {
379 unsigned int plane;
380 for (plane = 0; plane < pamP->depth; ++plane) {
381 pclGeneratorP->data[pclGeneratorP->cursor++] =
382 pnm_scalesample(tuplerow[col][plane], pamP->maxval, 255);
383 }
384 }
385
386 for(padCt=0; padCt < padsize; ++padCt)
387 pclGeneratorP->data[pclGeneratorP->cursor++] = 0;
388
389 pnm_freepamrow(tuplerow);
390 }
391
392
393
394 static void
395 createPclGeneratorWholebytes(struct pam * const pamP,
396 pclGenerator ** const pclGenPP) {
397 /* One sample per byte */
398
399 pclGenerator * pclGenP;
400
401 MALLOCVAR_NOFAIL(pclGenP);
402
403 if (pamP->depth < 3)
404 pclGenP->colorSpace = eGray;
405 else
406 pclGenP->colorSpace = eRGB;
407
408 pclGenP->colorDepth = e8Bit;
409 pclGenP->height = pamP->height;
410 pclGenP->width = pamP->width;
411
412 if (UINT_MAX / pamP->width < pamP->depth)
413 pm_error("Image too big to process");
414 else {
415 pclGenP->linelen = pamP->width * pamP->depth;
416 pclGenP->paddedLinelen = pclPaddedLinelen(pclGenP->linelen);
417 pclGenP->data = malloc(pclDatabuffSize(pclGenP->paddedLinelen));
418 if (pclGenP->data == NULL)
419 pm_error("Unable to allocate row buffer.");
420 }
421
422 pclGenP->getnextrow = pnmToPcllineWholebytes;
423
424 *pclGenPP = pclGenP;
425 }
426
427
428
429 static void
430 destroyPclGenerator(pclGenerator * const pclGenP) {
431
432 free(pclGenP->data);
433
434 free(pclGenP);
435 }
436
437
438
439 static void
440 createPclGenerator(struct pam * const pamP,
441 pclGenerator ** const pclGeneratorPP,
442 bool const colorok) {
443
444 if (pamP->depth > 1 && !colorok)
445 pm_message("WARNING: generating a color print stream because the "
446 "input image is PPM. "
447 "To generate a black and white print stream, run the input "
448 "through Ppmtopgm. To suppress this warning, use the "
449 "-colorok option.");
450
451 if (pamP->depth == 1 && pamP->maxval == 1)
452 createPclGeneratorPackbits(pamP, pclGeneratorPP);
453 else
454 createPclGeneratorWholebytes(pamP, pclGeneratorPP);
455 }
456
457
458
459 struct tPrinter {
460 const char *name;
461 float topmargin;
462 float bottommargin;
463 float leftmargin;
464 float rightmargin;
465 } xlPrinters[] = {
466 { "lj2200",0,0,0,0 }
467 };
468
469
470
471 static int
472 out_ubyte(int const fd,
473 unsigned char const data) {
474
475 return XY_Write(fd, &data, 1);
476 }
477
478
479
480 static int
481 XL_Operator(int const fd,
482 enum Operator const data) {
483
484 return out_ubyte(fd, data);
485 }
486
487
488
489 static int
490 out_uint16(int const fd,
491 unsigned short const data ) {
492
493 unsigned char c[2];
494
495 c[0] = data & 0xff;
496 c[1] = data >>8;
497
498 return XY_Write(fd, c , ARRAY_SIZE(c));
499 }
500
501
502
503 static int
504 out_uint32(int fd,unsigned int data ) {
505 unsigned char c[4];
506 c[0] = data&0xff; c[1]=(data>>8)&0xff; c[2]=(data>>16)&0xff; c[3]=data>>24;
507 return XY_Write(fd,c,4);
508 }
509
510
511
512 static int
513 out_sint16(int const fd,
514 signed short const sdata ) {
515
516 unsigned short const data= (unsigned short)sdata;
517
518 unsigned char c[2];
519
520 c[0] = data & 0xff;
521 c[1] = data >> 8;
522
523 return XY_Write(fd, c, ARRAY_SIZE(c));
524 }
525
526
527
528 static int
529 xl_ubyte(int const fd,
530 unsigned char const data) {
531
532 unsigned char const tag = 0xc0;
533
534 XY_Write(fd, &tag, 1);
535
536 return out_ubyte(fd, data);
537 }
538
539
540
541 static int
542 xl_uint16(int const fd,
543 unsigned short const data) {
544
545 unsigned char const tag = 0xc1;
546
547 XY_Write(fd, &tag, 1);
548
549 return out_uint16(fd, data);
550 }
551
552
553
554 static int
555 xl_ubyte_array(int const fd,
556 const unsigned char * const data,
557 int const len) {
558
559 unsigned int i;
560 unsigned char head[4];
561
562 head[0] = 0xc8;
563 head[1] = 0xc1;
564 head[2] = len & 0xff;
565 head[3] = (len >> 8) & 0xff;
566
567 XY_Write(fd, head, ARRAY_SIZE(head));
568
569 for (i = 0; i < len; ++i)
570 out_ubyte(fd, data[i]);
571
572 return 0;
573 }
574
575
576
577 static int
578 xl_uint16_xy(int const fd,
579 unsigned short const xdata,
580 unsigned short const ydata ) {
581
582 unsigned char const tag = 0xd1;
583
584 XY_Write(fd, &tag, 1);
585 out_uint16(fd, xdata);
586
587 return out_uint16(fd, ydata);
588 }
589
590
591
592 static int
593 xl_sint16_xy(int const fd,
594 signed short const xdata,
595 signed short const ydata ) {
596
597 unsigned char const tag = 0xd3;
598
599 XY_Write(fd, &tag, 1);
600
601 out_sint16(fd, xdata);
602
603 return out_sint16(fd, ydata);
604 }
605
606
607
608 static int
609 xl_attr_ubyte(int const fd,
610 enum Attribute const data) {
611
612 unsigned char const tag = 0xf8;
613
614 XY_Write(fd, &tag, 1);
615
616 return out_ubyte(fd, data);
617 }
618
619
620
621 static int
622 xl_dataLength(int const fd,
623 unsigned int const dataLength ) {
624
625 unsigned char const tag = 0xfa;
626
627 XY_Write(fd, &tag, 1);
628
629 return out_uint32(fd, dataLength);
630 }
631
632
633
634 static void
635 openDataSource(int const outFd,
636 enum DataOrg const dataOrg,
637 enum DataSource const dataSource) {
638
639 xl_ubyte(outFd, dataOrg); xl_attr_ubyte(outFd, aDataOrg);
640 xl_ubyte(outFd, dataSource); xl_attr_ubyte(outFd, aSourceType);
641 XL_Operator(outFd, oOpenDataSource);
642 }
643
644
645
646 static void
647 closeDataSource(int const outFd) {
648
649 XL_Operator(outFd, oCloseDataSource);
650 }
651
652
653
654 static void
655 convertAndWriteRleBlock(int const outFd,
656 pclGenerator * const pclGeneratorP,
657 struct pam * const pamP,
658 int const firstLine,
659 int const lineCt,
660 unsigned char * const outbuf) {
661
662 size_t rlelen;
663 unsigned int line;
664
665 pclGeneratorP->cursor = 0;
666 for (line = firstLine; line < firstLine + lineCt; ++line) {
667 pclGeneratorP->getnextrow(pclGeneratorP, pamP);
668 }
669
670 pm_rlenc_compressbyte(pclGeneratorP->data, outbuf, PM_RLE_PACKBITS,
671 pclGeneratorP->paddedLinelen * lineCt, &rlelen);
672
673 xl_dataLength(outFd, rlelen);
674 XY_Write(outFd, outbuf, rlelen);
675 }
676
677
678
679 /*
680 * ------------------------------------------------------------
681 * XL_WriteImage
682 * Write a PCL-XL image to the datastream
683 * ------------------------------------------------------------
684 */
685 static void
686 convertAndWriteImage(int const outFd,
687 pclGenerator * const pclGenP,
688 struct pam * const pamP) {
689
690 size_t const inSize = (pclGenP-> linelen + 3) * 20;
691
692 int blockStartLine;
693 unsigned char * outbuf;
694
695 xl_ubyte(outFd, eDirectPixel); xl_attr_ubyte(outFd, aColorMapping);
696 xl_ubyte(outFd, pclGenP->colorDepth); xl_attr_ubyte(outFd, aColorDepth);
697 xl_uint16(outFd, pclGenP->width); xl_attr_ubyte(outFd, aSourceWidth);
698 xl_uint16(outFd, pclGenP->height); xl_attr_ubyte(outFd, aSourceHeight);
699 xl_uint16_xy(outFd, pclGenP->width*1, pclGenP->height*1);
700 xl_attr_ubyte(outFd, aDestinationSize);
701 XL_Operator(outFd, oBeginImage);
702
703 pm_rlenc_allocoutbuf(&outbuf, inSize, PM_RLE_PACKBITS);
704
705 for (blockStartLine = 0; blockStartLine < pclGenP->height; ) {
706 unsigned int const blockHeight =
707 MIN(20, pclGenP->height-blockStartLine);
708
709 xl_uint16(outFd, blockStartLine); xl_attr_ubyte(outFd, aStartLine);
710 xl_uint16(outFd, blockHeight); xl_attr_ubyte(outFd, aBlockHeight);
711 xl_ubyte(outFd, eRLECompression); xl_attr_ubyte(outFd, aCompressMode);
712 /* In modern PCL-XL, we could use a PadBytesMultiple attribute
713 here to avoid having to pad the data to a multiple of 4
714 bytes. But PCL-XL 1.1 didn't have PadBytesMultiple.
715 xl_ubyte(outFd, 1); xl_attr_ubyte(outFd, aPadBytesMultiple);
716 */
717 XL_Operator(outFd, oReadImage);
718 convertAndWriteRleBlock(outFd, pclGenP, pamP,
719 blockStartLine, blockHeight, outbuf);
720 blockStartLine += blockHeight;
721 }
722 pm_rlenc_freebuf(outbuf);
723 XL_Operator(outFd, oEndImage);
724 }
725
726
727
728 static void
729 printEmbeddedImage(int const outFd,
730 const InputSource * const sourceP,
731 bool const colorok) {
732
733 FILE * ifP;
734 struct pam pam;
735 pclGenerator * pclGeneratorP;
736
737 openDataSource(outFd, eBinaryLowByteFirst, eDefaultDataSource);
738
739 ifP = pm_openr(sourceP->name);
740
741 pnm_readpaminit(ifP, &pam, PAM_STRUCT_SIZE(tuple_type));
742
743 createPclGenerator(&pam, &pclGeneratorP, colorok);
744
745 convertAndWriteImage(outFd, pclGeneratorP, &pam);
746
747 destroyPclGenerator(pclGeneratorP);
748
749 pm_close(ifP);
750
751 closeDataSource(outFd);
752 }
753
754
755
756 static void
757 copyFile(const char * const sourceFileName,
758 int const destFd) {
759
760 FILE * sourceFileP;
761
762 sourceFileP = pm_openr(sourceFileName);
763
764 while (!feof(sourceFileP)) {
765 char buffer[1024];
766 size_t bytesRead;
767 size_t totalBytesWritten;
768
769 bytesRead = fread(buffer, 1, sizeof(buffer), sourceFileP);
770
771 if (ferror(sourceFileP))
772 pm_error("Read from file failed. errno=%d (%s)",
773 errno, strerror(errno));
774
775 totalBytesWritten = 0;
776
777 while (totalBytesWritten < bytesRead) {
778 ssize_t rc;
779
780 rc = write(destFd, buffer, bytesRead);
781
782 if (rc < 0)
783 pm_error("Write to file failed. errno=%d (%s)",
784 errno, strerror(errno));
785 else
786 totalBytesWritten += rc;
787 }
788 }
789 pm_close(sourceFileP);
790 }
791
792
793
794 static void
795 jobHead(int const outFd,
796 bool const renderGray,
797 const char * const userJobSetupFileName) {
798 /*----------------------------------------------------------------------------
799 Start a PJL job.
800
801 Switch printer to PCL-XL mode. This is called "entering a printer
802 language" in PCL terms. In particular, we enter the PCL-XL language,
803 as opposed to e.g. Postscript.
804 -----------------------------------------------------------------------------*/
805 /* Reset */
806 XY_Puts(outFd,"\033%-12345X");
807
808 if (userJobSetupFileName)
809 copyFile(userJobSetupFileName, outFd);
810
811 if (renderGray)
812 XY_Puts(outFd, "@PJL SET RENDERMODE=GRAYSCALE\n");
813
814 XY_Puts(outFd, "@PJL ENTER LANGUAGE=PCLXL\n");
815 XY_Puts(outFd, ") HP-PCL XL;1;1;Generated by Netpbm Pnmtopclxl\n");
816 }
817
818
819
820 static void
821 jobEnd(int const outFd) {
822 /*----------------------------------------------------------------------------
823 End a PJL job.
824
825 Reset printer to quiescent mode. Exit the printer language.
826 -----------------------------------------------------------------------------*/
827 XY_Puts(outFd,"\033%-12345X");
828 }
829
830
831
832 static void
833 beginPage(int const outFd,
834 bool const doDuplex,
835 enum DuplexPageMode const duplex,
836 bool const doMediaSource,
837 int const mediaSource,
838 bool const doMediaDestination,
839 int const mediaDestination,
840 enum MediaSize const format) {
841 /*----------------------------------------------------------------------------
842 Emit a BeginPage printer command.
843 -----------------------------------------------------------------------------*/
844 if (doDuplex) {
845 xl_ubyte(outFd, duplex); xl_attr_ubyte(outFd, aDuplexPageMode);
846 }
847
848 if (doMediaSource) {
849 /* if not included same as last time in same session is selected */
850 xl_ubyte(outFd, mediaSource); xl_attr_ubyte(outFd, aMediaSource);
851 }
852
853 if (doMediaDestination) {
854 xl_ubyte(outFd, mediaDestination);
855 xl_attr_ubyte(outFd, aMediaDestination);
856 }
857
858 xl_ubyte(outFd, ePortraitOrientation); xl_attr_ubyte(outFd, aOrientation);
859 xl_ubyte(outFd, format); xl_attr_ubyte(outFd, aMediaSize);
860
861 XL_Operator(outFd, oBeginPage);
862 }
863
864
865
866 static void
867 setColorSpace(int const outFd,
868 enum Colorspace const colorSpace,
869 const unsigned char * const palette,
870 unsigned int const paletteSize,
871 enum ColorDepth const paletteDepth) {
872 /*----------------------------------------------------------------------------
873 Emit printer control to set the color space.
874
875 'palette' == NULL means no palette (raster contains colors, not indexes
876 into a palette).
877
878 'paletteSize' is the number of bytes in the palette (undefined if
879 'palette' is NULL)
880
881 'paletteDepth' is the color depth of the entries in the palette.
882 e8Bit means the palette contains 8 bit values.
883
884 The palette is a "direct color" palette: A separate table for each
885 color component (i.e. one table for grayscale; three tables for
886 RGB). Each table is indexed by a value from the raster and yields
887 a byte of color component value.
888
889 The palette has to be the right size to fit the number of color
890 components and raster color depth (bits per component in the raster).
891
892 E.g. with raster color depth of 1 bit (e1Bit) and RGB color (eRGB),
893 'paletteSize' would have to be 6 -- a table for each of R, G, and
894 B, of two elements each.
895
896 It is not clear from the documentation what the situation is when
897 paletteDepth is not e8Bit. Is each palette entry still a byte and only
898 some of the byte gets used? Or are there multiple entries per byte?
899 -----------------------------------------------------------------------------*/
900 xl_ubyte(outFd, colorSpace); xl_attr_ubyte(outFd, aColorSpace);
901 if (palette) {
902 xl_ubyte(outFd, paletteDepth);
903 xl_attr_ubyte(outFd, aPaletteDepth);
904 xl_ubyte_array(outFd, palette, paletteSize);
905 xl_attr_ubyte(outFd, aPaletteData);
906 }
907 XL_Operator(outFd, oSetColorSpace);
908 }
909
910
911
912 static void
913 positionCursor(int const outFd,
914 bool const center,
915 float const xoffs,
916 float const yoffs,
917 int const imageWidth,
918 int const imageHeight,
919 unsigned int const dpi,
920 enum MediaSize const format) {
921 /*----------------------------------------------------------------------------
922 Emit printer control to position the cursor to start the page.
923 -----------------------------------------------------------------------------*/
924 float xpos, ypos;
925
926 if (center) {
927 float const width = 1.0 * imageWidth/dpi;
928 float const height = 1.0 * imageHeight/dpi;
929 xpos = (PAPERWIDTH(format) - width)/2;
930 ypos = (PAPERHEIGHT(format) - height)/2;
931 } else {
932 xpos = xoffs;
933 ypos = yoffs;
934 }
935 /* cursor positioning */
936 xl_sint16_xy(outFd, xpos * dpi, ypos * dpi); xl_attr_ubyte(outFd, aPoint);
937 XL_Operator(outFd, oSetCursor);
938 }
939
940
941
942 static void
943 endPage(int const outFd,
944 bool const doCopies,
945 unsigned int const copies) {
946 /*----------------------------------------------------------------------------
947 Emit an EndPage printer command.
948 -----------------------------------------------------------------------------*/
949 if (doCopies) {
950 /* wrong in example in PCL-XL manual. Type is uint16 ! */
951 xl_uint16(outFd, copies); xl_attr_ubyte(outFd, aPageCopies);
952 }
953 XL_Operator(outFd, oEndPage);
954 }
955
956
957
958 static void
959 convertAndPrintPage(int const outFd,
960 pclGenerator * const pclGeneratorP,
961 struct pam * const pamP,
962 enum MediaSize const format,
963 unsigned int const dpi,
964 bool const center,
965 float const xoffs,
966 float const yoffs,
967 bool const doDuplex,
968 enum DuplexPageMode const duplex,
969 bool const doCopies,
970 unsigned int const copies,
971 bool const doMediaSource,
972 int const mediaSource,
973 bool const doMediaDestination,
974 int const mediaDestination) {
975
976 beginPage(outFd, doDuplex, duplex, doMediaSource, mediaSource,
977 doMediaDestination, mediaDestination, format);
978
979 /* Before Netpbm 10.27 (March 2005), we always set up a two-byte 8
980 bit deep palette: {0, 255}. I don't know why, because this
981 works only for e1Bit color depth an eGray color space, and in
982 that case does the same thing as having no palette at all. But
983 in other cases, it doesn't work. E.g. with eRGB, e8Bit, we got
984 an IllegalArraySize error from the printer on the SetColorSpace
985 command.
986
987 So we don't use a palette at all now.
988 */
989 setColorSpace(outFd, pclGeneratorP->colorSpace, NULL, 0, 0);
990
991 positionCursor(outFd, center, xoffs, yoffs,
992 pclGeneratorP->width, pclGeneratorP->height, dpi, format);
993
994 convertAndWriteImage(outFd, pclGeneratorP, pamP);
995
996 endPage(outFd, doCopies, copies);
997 }
998
999
1000
1001 static void
1002 beginSession(int const outFd,
1003 unsigned int const xdpi,
1004 unsigned int const ydpi,
1005 enum Measure const measure,
1006 bool const noReporting,
1007 enum ErrorReport const errorReport) {
1008
1009 xl_uint16_xy(outFd, xdpi, ydpi); xl_attr_ubyte(outFd, aUnitsPerMeasure);
1010 xl_ubyte(outFd, measure); xl_attr_ubyte(outFd, aMeasure);
1011 /* xl_ubyte(outFd,eNoReporting); xl_attr_ubyte(outFd,aErrorReport); */
1012 xl_ubyte(outFd,errorReport); xl_attr_ubyte(outFd,aErrorReport);
1013 XL_Operator(outFd,oBeginSession);
1014 }
1015
1016
1017
1018 static void
1019 endSession(int outFd) {
1020 XL_Operator(outFd,oEndSession);
1021 }
1022
1023
1024
1025 static void
1026 printPages(int const outFd,
1027 InputSource * const firstSourceP,
1028 enum MediaSize const format,
1029 unsigned int const dpi,
1030 bool const center,
1031 float const xoffs,
1032 float const yoffs,
1033 bool const doDuplex,
1034 enum DuplexPageMode const duplex,
1035 bool const doCopies,
1036 unsigned int const copies,
1037 bool const doMediaSource,
1038 int const mediaSource,
1039 bool const doMediaDestination,
1040 int const mediaDestination,
1041 bool const colorok) {
1042 /*----------------------------------------------------------------------------
1043 Loop over all input files, and each file, all images.
1044 -----------------------------------------------------------------------------*/
1045 InputSource * sourceP;
1046 unsigned int sourceNum;
1047
1048 sourceP = firstSourceP;
1049
1050 openDataSource(outFd, eBinaryLowByteFirst, eDefaultDataSource);
1051
1052 sourceNum = 0; /* initial value */
1053
1054 while (sourceP) {
1055 FILE * ifP;
1056 struct pam pam;
1057 int eof;
1058 unsigned int pageNum;
1059
1060 ifP = pm_openr(sourceP->name);
1061
1062 ++sourceNum;
1063
1064 pageNum = 0; /* initial value */
1065
1066 eof = FALSE;
1067 while(!eof) {
1068 pnm_nextimage(ifP, &eof);
1069 if (!eof) {
1070 pclGenerator * pclGeneratorP;
1071
1072 ++pageNum;
1073 pm_message("Processing File %u, Page %u", sourceNum, pageNum);
1074
1075 pnm_readpaminit(ifP, &pam, PAM_STRUCT_SIZE(tuple_type));
1076
1077 createPclGenerator(&pam, &pclGeneratorP, colorok);
1078
1079 convertAndPrintPage(
1080 outFd, pclGeneratorP, &pam,
1081 format, dpi, center, xoffs, yoffs, doDuplex, duplex,
1082 doCopies, copies, doMediaSource, mediaSource,
1083 doMediaDestination, mediaDestination);
1084
1085 destroyPclGenerator(pclGeneratorP);
1086 }
1087 }
1088 pm_close(ifP);
1089 sourceP = sourceP->next;
1090 }
1091 closeDataSource(outFd);
1092 }
1093
1094
1095
1096 int
1097 main(int argc, char *argv[]) {
1098
1099 int const outFd = STDOUT_FILENO;
1100
1101 struct cmdlineInfo cmdline;
1102
1103 /* In case you're wondering why we do direct file descriptor I/O
1104 instead of stream (FILE *), it's because Jochen originally
1105 wrote this code for an embedded system with diet-libc. Without
1106 the stream library, the statically linked binary was only about
1107 5K big.
1108 */
1109 pnm_init(&argc, argv);
1110
1111 parseCommandLine(argc, argv, &cmdline);
1112
1113 if (cmdline.embedded)
1114 printEmbeddedImage(outFd, cmdline.sourceP, cmdline.colorok);
1115 else {
1116 jobHead(outFd, cmdline.rendergray, cmdline.jobsetup);
1117
1118 beginSession(outFd, cmdline.dpi, cmdline.dpi, eInch,
1119 FALSE, eBackChAndErrPage);
1120
1121 printPages(outFd, cmdline.sourceP,
1122 cmdline.format, cmdline.dpi, cmdline.center,
1123 cmdline.xoffs, cmdline.yoffs,
1124 cmdline.duplexSpec, cmdline.duplex,
1125 cmdline.copiesSpec, cmdline.copies,
1126 cmdline.feederSpec, cmdline.feeder,
1127 cmdline.outtraySpec, cmdline.outtray,
1128 cmdline.colorok
1129 );
1130 endSession(outFd);
1131
1132 jobEnd(outFd);
1133 }
1134 freeCmdline(cmdline);
1135
1136 return 0;
1137 }
1138
1139
1140