2 * "$Id: rastertohp.c 12575 2015-03-26 16:36:54Z msweet $"
4 * Hewlett-Packard Page Control Language filter for CUPS.
6 * Copyright 2007-2015 by Apple Inc.
7 * Copyright 1993-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers...
22 #include <cups/cups.h>
24 #include <cups/string-private.h>
25 #include <cups/language-private.h>
26 #include <cups/raster.h>
36 unsigned char *Planes
[4], /* Output buffers */
37 *CompBuffer
, /* Compression buffer */
38 *BitBuffer
; /* Buffer for output bits */
39 unsigned NumPlanes
, /* Number of color planes */
40 ColorBits
, /* Number of bits per color */
41 Feed
; /* Number of lines to skip */
42 int Duplex
, /* Current duplex mode */
43 Page
, /* Current page number */
44 Canceled
; /* Has the current job been canceled? */
52 void StartPage(ppd_file_t
*ppd
, cups_page_header2_t
*header
);
56 void CancelJob(int sig
);
57 void CompressData(unsigned char *line
, unsigned length
, unsigned plane
, unsigned type
);
58 void OutputLine(cups_page_header2_t
*header
);
62 * 'Setup()' - Prepare the printer for printing.
69 * Send a PCL reset sequence.
78 * 'StartPage()' - Start a page of graphics.
82 StartPage(ppd_file_t
*ppd
, /* I - PPD file */
83 cups_page_header2_t
*header
) /* I - Page header */
85 unsigned plane
; /* Looping var */
89 * Show page device dictionary...
92 fprintf(stderr
, "DEBUG: StartPage...\n");
93 fprintf(stderr
, "DEBUG: MediaClass = \"%s\"\n", header
->MediaClass
);
94 fprintf(stderr
, "DEBUG: MediaColor = \"%s\"\n", header
->MediaColor
);
95 fprintf(stderr
, "DEBUG: MediaType = \"%s\"\n", header
->MediaType
);
96 fprintf(stderr
, "DEBUG: OutputType = \"%s\"\n", header
->OutputType
);
98 fprintf(stderr
, "DEBUG: AdvanceDistance = %d\n", header
->AdvanceDistance
);
99 fprintf(stderr
, "DEBUG: AdvanceMedia = %d\n", header
->AdvanceMedia
);
100 fprintf(stderr
, "DEBUG: Collate = %d\n", header
->Collate
);
101 fprintf(stderr
, "DEBUG: CutMedia = %d\n", header
->CutMedia
);
102 fprintf(stderr
, "DEBUG: Duplex = %d\n", header
->Duplex
);
103 fprintf(stderr
, "DEBUG: HWResolution = [ %d %d ]\n", header
->HWResolution
[0],
104 header
->HWResolution
[1]);
105 fprintf(stderr
, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n",
106 header
->ImagingBoundingBox
[0], header
->ImagingBoundingBox
[1],
107 header
->ImagingBoundingBox
[2], header
->ImagingBoundingBox
[3]);
108 fprintf(stderr
, "DEBUG: InsertSheet = %d\n", header
->InsertSheet
);
109 fprintf(stderr
, "DEBUG: Jog = %d\n", header
->Jog
);
110 fprintf(stderr
, "DEBUG: LeadingEdge = %d\n", header
->LeadingEdge
);
111 fprintf(stderr
, "DEBUG: Margins = [ %d %d ]\n", header
->Margins
[0],
113 fprintf(stderr
, "DEBUG: ManualFeed = %d\n", header
->ManualFeed
);
114 fprintf(stderr
, "DEBUG: MediaPosition = %d\n", header
->MediaPosition
);
115 fprintf(stderr
, "DEBUG: MediaWeight = %d\n", header
->MediaWeight
);
116 fprintf(stderr
, "DEBUG: MirrorPrint = %d\n", header
->MirrorPrint
);
117 fprintf(stderr
, "DEBUG: NegativePrint = %d\n", header
->NegativePrint
);
118 fprintf(stderr
, "DEBUG: NumCopies = %d\n", header
->NumCopies
);
119 fprintf(stderr
, "DEBUG: Orientation = %d\n", header
->Orientation
);
120 fprintf(stderr
, "DEBUG: OutputFaceUp = %d\n", header
->OutputFaceUp
);
121 fprintf(stderr
, "DEBUG: PageSize = [ %d %d ]\n", header
->PageSize
[0],
122 header
->PageSize
[1]);
123 fprintf(stderr
, "DEBUG: Separations = %d\n", header
->Separations
);
124 fprintf(stderr
, "DEBUG: TraySwitch = %d\n", header
->TraySwitch
);
125 fprintf(stderr
, "DEBUG: Tumble = %d\n", header
->Tumble
);
126 fprintf(stderr
, "DEBUG: cupsWidth = %d\n", header
->cupsWidth
);
127 fprintf(stderr
, "DEBUG: cupsHeight = %d\n", header
->cupsHeight
);
128 fprintf(stderr
, "DEBUG: cupsMediaType = %d\n", header
->cupsMediaType
);
129 fprintf(stderr
, "DEBUG: cupsBitsPerColor = %d\n", header
->cupsBitsPerColor
);
130 fprintf(stderr
, "DEBUG: cupsBitsPerPixel = %d\n", header
->cupsBitsPerPixel
);
131 fprintf(stderr
, "DEBUG: cupsBytesPerLine = %d\n", header
->cupsBytesPerLine
);
132 fprintf(stderr
, "DEBUG: cupsColorOrder = %d\n", header
->cupsColorOrder
);
133 fprintf(stderr
, "DEBUG: cupsColorSpace = %d\n", header
->cupsColorSpace
);
134 fprintf(stderr
, "DEBUG: cupsCompression = %d\n", header
->cupsCompression
);
137 * Setup printer/job attributes...
140 Duplex
= header
->Duplex
;
141 ColorBits
= header
->cupsBitsPerColor
;
143 if ((!Duplex
|| (Page
& 1)) && header
->MediaPosition
)
144 printf("\033&l%dH", /* Set media position */
145 header
->MediaPosition
);
147 if (Duplex
&& ppd
&& ppd
->model_number
== 2)
150 * Handle duplexing on new DeskJet printers...
153 printf("\033&l-2H"); /* Load media */
156 printf("\033&l2S"); /* Set duplex mode */
159 if (!Duplex
|| (Page
& 1) || (ppd
&& ppd
->model_number
== 2))
162 * Set the media size...
165 printf("\033&l6D\033&k12H"); /* Set 6 LPI, 10 CPI */
166 printf("\033&l0O"); /* Set portrait orientation */
168 switch (header
->PageSize
[1])
170 case 540 : /* Monarch Envelope */
171 printf("\033&l80A"); /* Set page size */
175 printf("\033&l25A"); /* Set page size */
178 case 624 : /* DL Envelope */
179 printf("\033&l90A"); /* Set page size */
182 case 649 : /* C5 Envelope */
183 printf("\033&l91A"); /* Set page size */
186 case 684 : /* COM-10 Envelope */
187 printf("\033&l81A"); /* Set page size */
190 case 709 : /* B5 Envelope */
191 printf("\033&l100A"); /* Set page size */
194 case 756 : /* Executive */
195 printf("\033&l1A"); /* Set page size */
198 case 792 : /* Letter */
199 printf("\033&l2A"); /* Set page size */
203 printf("\033&l26A"); /* Set page size */
206 case 1008 : /* Legal */
207 printf("\033&l3A"); /* Set page size */
211 printf("\033&l27A"); /* Set page size */
214 case 1224 : /* Tabloid */
215 printf("\033&l6A"); /* Set page size */
219 printf("\033&l%dP", /* Set page length */
220 header
->PageSize
[1] / 12);
221 printf("\033&l0E"); /* Set top margin to 0 */
224 if (!Duplex
|| (Page
& 1))
227 * Set other job options...
230 printf("\033&l%dX", header
->NumCopies
); /* Set number copies */
232 if (header
->cupsMediaType
&&
233 (!ppd
|| ppd
->model_number
!= 2 || header
->HWResolution
[0] == 600))
234 printf("\033&l%dM", /* Set media type */
235 header
->cupsMediaType
);
237 if (!ppd
|| ppd
->model_number
!= 2)
239 int mode
= Duplex
? 1 + header
->Tumble
!= 0 : 0;
241 printf("\033&l%dS", mode
); /* Set duplex mode */
242 printf("\033&l0L"); /* Turn off perforation skip */
245 else if (!ppd
|| ppd
->model_number
!= 2)
246 printf("\033&a2G"); /* Set back side */
249 * Set graphics mode...
252 if (ppd
&& ppd
->model_number
== 2)
255 * Figure out the number of color planes...
258 if (header
->cupsColorSpace
== CUPS_CSPACE_KCMY
)
264 * Set the resolution and top-of-form...
267 printf("\033&u%dD", header
->HWResolution
[0]);
269 printf("\033&l0e0L"); /* Reset top and don't skip */
270 printf("\033*p0Y\033*p0X"); /* Set top of form */
273 * Send 26-byte configure image data command with horizontal and
274 * vertical resolutions as well as a color count...
278 putchar(2); /* Format 2 */
279 putchar((int)NumPlanes
); /* Output planes */
281 putchar((int)(header
->HWResolution
[0] >> 8));/* Black resolution */
282 putchar((int)header
->HWResolution
[0]);
283 putchar((int)(header
->HWResolution
[1] >> 8));
284 putchar((int)header
->HWResolution
[1]);
286 putchar(1 << ColorBits
); /* # of black levels */
288 putchar((int)(header
->HWResolution
[0] >> 8));/* Cyan resolution */
289 putchar((int)header
->HWResolution
[0]);
290 putchar((int)(header
->HWResolution
[1] >> 8));
291 putchar((int)header
->HWResolution
[1]);
293 putchar(1 << ColorBits
); /* # of cyan levels */
295 putchar((int)(header
->HWResolution
[0] >> 8));/* Magenta resolution */
296 putchar((int)header
->HWResolution
[0]);
297 putchar((int)(header
->HWResolution
[1] >> 8));
298 putchar((int)header
->HWResolution
[1]);
300 putchar(1 << ColorBits
); /* # of magenta levels */
302 putchar((int)(header
->HWResolution
[0] >> 8));/* Yellow resolution */
303 putchar((int)header
->HWResolution
[0]);
304 putchar((int)(header
->HWResolution
[1] >> 8));
305 putchar((int)header
->HWResolution
[1]);
307 putchar(1 << ColorBits
); /* # of yellow levels */
309 printf("\033&l0H"); /* Set media position */
313 printf("\033*t%uR", header
->HWResolution
[0]);
316 if (header
->cupsColorSpace
== CUPS_CSPACE_KCMY
)
319 printf("\033*r-4U"); /* Set KCMY graphics */
321 else if (header
->cupsColorSpace
== CUPS_CSPACE_CMY
)
324 printf("\033*r-3U"); /* Set CMY graphics */
327 NumPlanes
= 1; /* Black&white graphics */
330 * Set size and position of graphics...
333 printf("\033*r%uS", header
->cupsWidth
); /* Set width */
334 printf("\033*r%uT", header
->cupsHeight
); /* Set height */
336 printf("\033&a0H"); /* Set horizontal position */
339 printf("\033&a%.0fV", /* Set vertical position */
340 10.0 * (ppd
->sizes
[0].length
- ppd
->sizes
[0].top
));
342 printf("\033&a0V"); /* Set top-of-page */
345 printf("\033*r1A"); /* Start graphics */
347 if (header
->cupsCompression
)
348 printf("\033*b%uM", /* Set compression */
349 header
->cupsCompression
);
351 Feed
= 0; /* No blank lines yet */
354 * Allocate memory for a line of graphics...
357 if ((Planes
[0] = malloc(header
->cupsBytesPerLine
+ NumPlanes
)) == NULL
)
359 fputs("ERROR: Unable to allocate memory\n", stderr
);
363 for (plane
= 1; plane
< NumPlanes
; plane
++)
364 Planes
[plane
] = Planes
[0] + plane
* header
->cupsBytesPerLine
/ NumPlanes
;
367 BitBuffer
= malloc(ColorBits
* ((header
->cupsWidth
+ 7) / 8));
371 if (header
->cupsCompression
)
372 CompBuffer
= malloc(header
->cupsBytesPerLine
* 2 + 2);
379 * 'EndPage()' - Finish a page of graphics.
386 * Eject the current page...
391 printf("\033*rC"); /* End color GFX */
393 if (!(Duplex
&& (Page
& 1)))
394 printf("\033&l0H"); /* Eject current page */
398 printf("\033*r0B"); /* End GFX */
400 if (!(Duplex
&& (Page
& 1)))
401 printf("\014"); /* Eject current page */
421 * 'Shutdown()' - Shutdown the printer.
428 * Send a PCL reset sequence.
437 * 'CancelJob()' - Cancel the current job...
441 CancelJob(int sig
) /* I - Signal */
450 * 'CompressData()' - Compress a line of graphics.
454 CompressData(unsigned char *line
, /* I - Data to compress */
455 unsigned length
, /* I - Number of bytes */
456 unsigned plane
, /* I - Color plane */
457 unsigned type
) /* I - Type of compression */
459 unsigned char *line_ptr
, /* Current byte pointer */
460 *line_end
, /* End-of-line byte pointer */
461 *comp_ptr
, /* Pointer into compression buffer */
462 *start
; /* Start of compression sequence */
463 unsigned count
; /* Count of bytes for output */
470 * Do no compression...
474 line_end
= line
+ length
;
479 * Do run-length encoding...
482 line_end
= line
+ length
;
483 for (line_ptr
= line
, comp_ptr
= CompBuffer
;
485 comp_ptr
+= 2, line_ptr
+= count
)
488 (line_ptr
+ count
) < line_end
&&
489 line_ptr
[0] == line_ptr
[count
] &&
493 comp_ptr
[0] = (unsigned char)(count
- 1);
494 comp_ptr
[1] = line_ptr
[0];
497 line_ptr
= CompBuffer
;
503 * Do TIFF pack-bits encoding...
507 line_end
= line
+ length
;
508 comp_ptr
= CompBuffer
;
510 while (line_ptr
< line_end
)
512 if ((line_ptr
+ 1) >= line_end
)
515 * Single byte on the end...
519 *comp_ptr
++ = *line_ptr
++;
521 else if (line_ptr
[0] == line_ptr
[1])
524 * Repeated sequence...
530 while (line_ptr
< (line_end
- 1) &&
531 line_ptr
[0] == line_ptr
[1] &&
538 *comp_ptr
++ = (unsigned char)(257 - count
);
539 *comp_ptr
++ = *line_ptr
++;
544 * Non-repeated sequence...
551 while (line_ptr
< (line_end
- 1) &&
552 line_ptr
[0] != line_ptr
[1] &&
559 *comp_ptr
++ = (unsigned char)(count
- 1);
561 memcpy(comp_ptr
, start
, count
);
566 line_ptr
= CompBuffer
;
572 * Set the length of the data and write a raster plane...
575 printf("\033*b%d%c", (int)(line_end
- line_ptr
), plane
);
576 fwrite(line_ptr
, (size_t)(line_end
- line_ptr
), 1, stdout
);
581 * 'OutputLine()' - Output a line of graphics.
585 OutputLine(cups_page_header2_t
*header
) /* I - Page header */
587 unsigned plane
, /* Current plane */
588 bytes
, /* Bytes to write */
589 count
; /* Bytes to convert */
590 unsigned char bit
, /* Current plane data */
591 bit0
, /* Current low bit data */
592 bit1
, /* Current high bit data */
593 *plane_ptr
, /* Pointer into Planes */
594 *bit_ptr
; /* Pointer into BitBuffer */
598 * Output whitespace as needed...
603 printf("\033*b%dY", Feed
);
608 * Write bitmap data as needed...
611 bytes
= (header
->cupsWidth
+ 7) / 8;
613 for (plane
= 0; plane
< NumPlanes
; plane
++)
620 CompressData(Planes
[plane
], bytes
, plane
< (NumPlanes
- 1) ? 'V' : 'W',
621 header
->cupsCompression
);
626 * Separate low and high bit data into separate buffers.
629 for (count
= header
->cupsBytesPerLine
/ NumPlanes
,
630 plane_ptr
= Planes
[plane
], bit_ptr
= BitBuffer
;
632 count
-= 2, plane_ptr
+= 2, bit_ptr
++)
636 bit0
= (unsigned char)(((bit
& 64) << 1) | ((bit
& 16) << 2) | ((bit
& 4) << 3) | ((bit
& 1) << 4));
637 bit1
= (unsigned char)((bit
& 128) | ((bit
& 32) << 1) | ((bit
& 8) << 2) | ((bit
& 2) << 3));
643 bit0
|= (unsigned char)((bit
& 1) | ((bit
& 4) >> 1) | ((bit
& 16) >> 2) | ((bit
& 64) >> 3));
644 bit1
|= (unsigned char)(((bit
& 2) >> 1) | ((bit
& 8) >> 2) | ((bit
& 32) >> 3) | ((bit
& 128) >> 4));
648 bit_ptr
[bytes
] = bit1
;
652 * Send low and high bits...
655 CompressData(BitBuffer
, bytes
, 'V', header
->cupsCompression
);
656 CompressData(BitBuffer
+ bytes
, bytes
, plane
< (NumPlanes
- 1) ? 'V' : 'W',
657 header
->cupsCompression
);
665 * 'main()' - Main entry and processing of driver.
668 int /* O - Exit status */
669 main(int argc
, /* I - Number of command-line arguments */
670 char *argv
[]) /* I - Command-line arguments */
672 int fd
; /* File descriptor */
673 cups_raster_t
*ras
; /* Raster stream for printing */
674 cups_page_header2_t header
; /* Page header from file */
675 unsigned y
; /* Current line */
676 ppd_file_t
*ppd
; /* PPD file */
677 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
678 struct sigaction action
; /* Actions for POSIX signals */
679 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
683 * Make sure status messages are not buffered...
686 setbuf(stderr
, NULL
);
689 * Check command-line...
692 if (argc
< 6 || argc
> 7)
695 * We don't have the correct number of arguments; write an error message
699 _cupsLangPrintFilter(stderr
, "ERROR",
700 _("%s job-id user title copies options [file]"),
706 * Open the page stream...
711 if ((fd
= open(argv
[6], O_RDONLY
)) == -1)
713 _cupsLangPrintError("ERROR", _("Unable to open raster file"));
721 ras
= cupsRasterOpen(fd
, CUPS_RASTER_READ
);
724 * Register a signal handler to eject the current page if the
730 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
731 sigset(SIGTERM
, CancelJob
);
732 #elif defined(HAVE_SIGACTION)
733 memset(&action
, 0, sizeof(action
));
735 sigemptyset(&action
.sa_mask
);
736 action
.sa_handler
= CancelJob
;
737 sigaction(SIGTERM
, &action
, NULL
);
739 signal(SIGTERM
, CancelJob
);
740 #endif /* HAVE_SIGSET */
743 * Initialize the print device...
746 ppd
= ppdOpenFile(getenv("PPD"));
749 ppd_status_t status
; /* PPD error */
750 int linenum
; /* Line number */
752 _cupsLangPrintFilter(stderr
, "ERROR",
753 _("The PPD file could not be opened."));
755 status
= ppdLastError(&linenum
);
757 fprintf(stderr
, "DEBUG: %s on line %d.\n", ppdErrorString(status
), linenum
);
765 * Process pages as needed...
770 while (cupsRasterReadHeader2(ras
, &header
))
773 * Write a status message with the page number and number of copies.
781 fprintf(stderr
, "PAGE: %d %d\n", Page
, header
.NumCopies
);
782 _cupsLangPrintFilter(stderr
, "INFO", _("Starting page %d."), Page
);
788 StartPage(ppd
, &header
);
791 * Loop for each line on the page...
794 for (y
= 0; y
< header
.cupsHeight
; y
++)
797 * Let the user know how far we have progressed...
805 _cupsLangPrintFilter(stderr
, "INFO",
806 _("Printing page %d, %u%% complete."),
807 Page
, 100 * y
/ header
.cupsHeight
);
808 fprintf(stderr
, "ATTR: job-media-progress=%u\n",
809 100 * y
/ header
.cupsHeight
);
813 * Read a line of graphics...
816 if (cupsRasterReadPixels(ras
, Planes
[0], header
.cupsBytesPerLine
) < 1)
820 * See if the line is blank; if not, write it to the printer...
824 memcmp(Planes
[0], Planes
[0] + 1, header
.cupsBytesPerLine
- 1))
834 _cupsLangPrintFilter(stderr
, "INFO", _("Finished page %d."), Page
);
843 * Shutdown the printer...
852 * Close the raster stream...
855 cupsRasterClose(ras
);
860 * If no pages were printed, send an error message...
865 _cupsLangPrintFilter(stderr
, "ERROR", _("No pages were found."));
874 * End of "$Id: rastertohp.c 12575 2015-03-26 16:36:54Z msweet $".