2 * "$Id: rastertohp.c 7834 2008-08-04 21:02:09Z mike $"
4 * Hewlett-Packard Page Control Language filter for the Common UNIX
5 * Printing System (CUPS).
7 * Copyright 2007-2009 by Apple Inc.
8 * Copyright 1993-2007 by Easy Software Products.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * Setup() - Prepare the printer for printing.
21 * StartPage() - Start a page of graphics.
22 * EndPage() - Finish a page of graphics.
23 * Shutdown() - Shutdown the printer.
24 * CancelJob() - Cancel the current job...
25 * CompressData() - Compress a line of graphics.
26 * OutputLine() - Output a line of graphics.
27 * main() - Main entry and processing of driver.
31 * Include necessary headers...
34 #include <cups/cups.h>
35 #include <cups/string.h>
36 #include <cups/i18n.h>
37 #include <cups/raster.h>
49 unsigned char *Planes
[4], /* Output buffers */
50 *CompBuffer
, /* Compression buffer */
51 *BitBuffer
; /* Buffer for output bits */
52 int NumPlanes
, /* Number of color planes */
53 ColorBits
, /* Number of bits per color */
54 Feed
, /* Number of lines to skip */
55 Duplex
, /* Current duplex mode */
56 Page
, /* Current page number */
57 Canceled
; /* Has the current job been canceled? */
65 void StartPage(ppd_file_t
*ppd
, cups_page_header2_t
*header
);
69 void CancelJob(int sig
);
70 void CompressData(unsigned char *line
, int length
, int plane
, int type
);
71 void OutputLine(cups_page_header2_t
*header
);
75 * 'Setup()' - Prepare the printer for printing.
82 * Send a PCL reset sequence.
91 * 'StartPage()' - Start a page of graphics.
95 StartPage(ppd_file_t
*ppd
, /* I - PPD file */
96 cups_page_header2_t
*header
) /* I - Page header */
98 int plane
; /* Looping var */
102 * Show page device dictionary...
105 fprintf(stderr
, "DEBUG: StartPage...\n");
106 fprintf(stderr
, "DEBUG: MediaClass = \"%s\"\n", header
->MediaClass
);
107 fprintf(stderr
, "DEBUG: MediaColor = \"%s\"\n", header
->MediaColor
);
108 fprintf(stderr
, "DEBUG: MediaType = \"%s\"\n", header
->MediaType
);
109 fprintf(stderr
, "DEBUG: OutputType = \"%s\"\n", header
->OutputType
);
111 fprintf(stderr
, "DEBUG: AdvanceDistance = %d\n", header
->AdvanceDistance
);
112 fprintf(stderr
, "DEBUG: AdvanceMedia = %d\n", header
->AdvanceMedia
);
113 fprintf(stderr
, "DEBUG: Collate = %d\n", header
->Collate
);
114 fprintf(stderr
, "DEBUG: CutMedia = %d\n", header
->CutMedia
);
115 fprintf(stderr
, "DEBUG: Duplex = %d\n", header
->Duplex
);
116 fprintf(stderr
, "DEBUG: HWResolution = [ %d %d ]\n", header
->HWResolution
[0],
117 header
->HWResolution
[1]);
118 fprintf(stderr
, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n",
119 header
->ImagingBoundingBox
[0], header
->ImagingBoundingBox
[1],
120 header
->ImagingBoundingBox
[2], header
->ImagingBoundingBox
[3]);
121 fprintf(stderr
, "DEBUG: InsertSheet = %d\n", header
->InsertSheet
);
122 fprintf(stderr
, "DEBUG: Jog = %d\n", header
->Jog
);
123 fprintf(stderr
, "DEBUG: LeadingEdge = %d\n", header
->LeadingEdge
);
124 fprintf(stderr
, "DEBUG: Margins = [ %d %d ]\n", header
->Margins
[0],
126 fprintf(stderr
, "DEBUG: ManualFeed = %d\n", header
->ManualFeed
);
127 fprintf(stderr
, "DEBUG: MediaPosition = %d\n", header
->MediaPosition
);
128 fprintf(stderr
, "DEBUG: MediaWeight = %d\n", header
->MediaWeight
);
129 fprintf(stderr
, "DEBUG: MirrorPrint = %d\n", header
->MirrorPrint
);
130 fprintf(stderr
, "DEBUG: NegativePrint = %d\n", header
->NegativePrint
);
131 fprintf(stderr
, "DEBUG: NumCopies = %d\n", header
->NumCopies
);
132 fprintf(stderr
, "DEBUG: Orientation = %d\n", header
->Orientation
);
133 fprintf(stderr
, "DEBUG: OutputFaceUp = %d\n", header
->OutputFaceUp
);
134 fprintf(stderr
, "DEBUG: PageSize = [ %d %d ]\n", header
->PageSize
[0],
135 header
->PageSize
[1]);
136 fprintf(stderr
, "DEBUG: Separations = %d\n", header
->Separations
);
137 fprintf(stderr
, "DEBUG: TraySwitch = %d\n", header
->TraySwitch
);
138 fprintf(stderr
, "DEBUG: Tumble = %d\n", header
->Tumble
);
139 fprintf(stderr
, "DEBUG: cupsWidth = %d\n", header
->cupsWidth
);
140 fprintf(stderr
, "DEBUG: cupsHeight = %d\n", header
->cupsHeight
);
141 fprintf(stderr
, "DEBUG: cupsMediaType = %d\n", header
->cupsMediaType
);
142 fprintf(stderr
, "DEBUG: cupsBitsPerColor = %d\n", header
->cupsBitsPerColor
);
143 fprintf(stderr
, "DEBUG: cupsBitsPerPixel = %d\n", header
->cupsBitsPerPixel
);
144 fprintf(stderr
, "DEBUG: cupsBytesPerLine = %d\n", header
->cupsBytesPerLine
);
145 fprintf(stderr
, "DEBUG: cupsColorOrder = %d\n", header
->cupsColorOrder
);
146 fprintf(stderr
, "DEBUG: cupsColorSpace = %d\n", header
->cupsColorSpace
);
147 fprintf(stderr
, "DEBUG: cupsCompression = %d\n", header
->cupsCompression
);
150 * Setup printer/job attributes...
153 Duplex
= header
->Duplex
;
154 ColorBits
= header
->cupsBitsPerColor
;
156 if ((!Duplex
|| (Page
& 1)) && header
->MediaPosition
)
157 printf("\033&l%dH", /* Set media position */
158 header
->MediaPosition
);
160 if (Duplex
&& ppd
&& ppd
->model_number
== 2)
163 * Handle duplexing on new DeskJet printers...
166 printf("\033&l-2H"); /* Load media */
169 printf("\033&l2S"); /* Set duplex mode */
172 if (!Duplex
|| (Page
& 1) || (ppd
&& ppd
->model_number
== 2))
175 * Set the media size...
178 printf("\033&l6D\033&k12H"); /* Set 6 LPI, 10 CPI */
179 printf("\033&l0O"); /* Set portrait orientation */
181 switch (header
->PageSize
[1])
183 case 540 : /* Monarch Envelope */
184 printf("\033&l80A"); /* Set page size */
188 printf("\033&l25A"); /* Set page size */
191 case 624 : /* DL Envelope */
192 printf("\033&l90A"); /* Set page size */
195 case 649 : /* C5 Envelope */
196 printf("\033&l91A"); /* Set page size */
199 case 684 : /* COM-10 Envelope */
200 printf("\033&l81A"); /* Set page size */
203 case 709 : /* B5 Envelope */
204 printf("\033&l100A"); /* Set page size */
207 case 756 : /* Executive */
208 printf("\033&l1A"); /* Set page size */
211 case 792 : /* Letter */
212 printf("\033&l2A"); /* Set page size */
216 printf("\033&l26A"); /* Set page size */
219 case 1008 : /* Legal */
220 printf("\033&l3A"); /* Set page size */
224 printf("\033&l27A"); /* Set page size */
227 case 1224 : /* Tabloid */
228 printf("\033&l6A"); /* Set page size */
232 printf("\033&l%dP", /* Set page length */
233 header
->PageSize
[1] / 12);
234 printf("\033&l0E"); /* Set top margin to 0 */
237 if (!Duplex
|| (Page
& 1))
240 * Set other job options...
243 printf("\033&l%dX", header
->NumCopies
); /* Set number copies */
245 if (header
->cupsMediaType
&&
246 (!ppd
|| ppd
->model_number
!= 2 || header
->HWResolution
[0] == 600))
247 printf("\033&l%dM", /* Set media type */
248 header
->cupsMediaType
);
250 if (!ppd
|| ppd
->model_number
!= 2)
252 int mode
= Duplex
? 1 + header
->Tumble
!= 0 : 0;
254 printf("\033&l%dS", mode
); /* Set duplex mode */
255 printf("\033&l0L"); /* Turn off perforation skip */
258 else if (!ppd
|| ppd
->model_number
!= 2)
259 printf("\033&a2G"); /* Set back side */
262 * Set graphics mode...
265 if (ppd
&& ppd
->model_number
== 2)
268 * Figure out the number of color planes...
271 if (header
->cupsColorSpace
== CUPS_CSPACE_KCMY
)
277 * Set the resolution and top-of-form...
280 printf("\033&u%dD", header
->HWResolution
[0]);
282 printf("\033&l0e0L"); /* Reset top and don't skip */
283 printf("\033*p0Y\033*p0X"); /* Set top of form */
286 * Send 26-byte configure image data command with horizontal and
287 * vertical resolutions as well as a color count...
291 putchar(2); /* Format 2 */
292 putchar(NumPlanes
); /* Output planes */
294 putchar(header
->HWResolution
[0] >> 8); /* Black resolution */
295 putchar(header
->HWResolution
[0]);
296 putchar(header
->HWResolution
[1] >> 8);
297 putchar(header
->HWResolution
[1]);
299 putchar(1 << ColorBits
); /* # of black levels */
301 putchar(header
->HWResolution
[0] >> 8); /* Cyan resolution */
302 putchar(header
->HWResolution
[0]);
303 putchar(header
->HWResolution
[1] >> 8);
304 putchar(header
->HWResolution
[1]);
306 putchar(1 << ColorBits
); /* # of cyan levels */
308 putchar(header
->HWResolution
[0] >> 8); /* Magenta resolution */
309 putchar(header
->HWResolution
[0]);
310 putchar(header
->HWResolution
[1] >> 8);
311 putchar(header
->HWResolution
[1]);
313 putchar(1 << ColorBits
); /* # of magenta levels */
315 putchar(header
->HWResolution
[0] >> 8); /* Yellow resolution */
316 putchar(header
->HWResolution
[0]);
317 putchar(header
->HWResolution
[1] >> 8);
318 putchar(header
->HWResolution
[1]);
320 putchar(1 << ColorBits
); /* # of yellow levels */
322 printf("\033&l0H"); /* Set media position */
326 printf("\033*t%dR", header
->HWResolution
[0]);
329 if (header
->cupsColorSpace
== CUPS_CSPACE_KCMY
)
332 printf("\033*r-4U"); /* Set KCMY graphics */
334 else if (header
->cupsColorSpace
== CUPS_CSPACE_CMY
)
337 printf("\033*r-3U"); /* Set CMY graphics */
340 NumPlanes
= 1; /* Black&white graphics */
343 * Set size and position of graphics...
346 printf("\033*r%dS", header
->cupsWidth
); /* Set width */
347 printf("\033*r%dT", header
->cupsHeight
); /* Set height */
349 printf("\033&a0H"); /* Set horizontal position */
352 printf("\033&a%.0fV", /* Set vertical position */
353 10.0 * (ppd
->sizes
[0].length
- ppd
->sizes
[0].top
));
355 printf("\033&a0V"); /* Set top-of-page */
358 printf("\033*r1A"); /* Start graphics */
360 if (header
->cupsCompression
)
361 printf("\033*b%dM", /* Set compression */
362 header
->cupsCompression
);
364 Feed
= 0; /* No blank lines yet */
367 * Allocate memory for a line of graphics...
370 if ((Planes
[0] = malloc(header
->cupsBytesPerLine
)) == NULL
)
372 fputs("ERROR: Unable to allocate memory!\n", stderr
);
376 for (plane
= 1; plane
< NumPlanes
; plane
++)
377 Planes
[plane
] = Planes
[0] + plane
* header
->cupsBytesPerLine
/ NumPlanes
;
380 BitBuffer
= malloc(ColorBits
* ((header
->cupsWidth
+ 7) / 8));
384 if (header
->cupsCompression
)
385 CompBuffer
= malloc(header
->cupsBytesPerLine
* 2);
392 * 'EndPage()' - Finish a page of graphics.
399 * Eject the current page...
404 printf("\033*rC"); /* End color GFX */
406 if (!(Duplex
&& (Page
& 1)))
407 printf("\033&l0H"); /* Eject current page */
411 printf("\033*r0B"); /* End GFX */
413 if (!(Duplex
&& (Page
& 1)))
414 printf("\014"); /* Eject current page */
434 * 'Shutdown()' - Shutdown the printer.
441 * Send a PCL reset sequence.
450 * 'CancelJob()' - Cancel the current job...
454 CancelJob(int sig
) /* I - Signal */
463 * 'CompressData()' - Compress a line of graphics.
467 CompressData(unsigned char *line
, /* I - Data to compress */
468 int length
, /* I - Number of bytes */
469 int plane
, /* I - Color plane */
470 int type
) /* I - Type of compression */
472 unsigned char *line_ptr
, /* Current byte pointer */
473 *line_end
, /* End-of-line byte pointer */
474 *comp_ptr
, /* Pointer into compression buffer */
475 *start
; /* Start of compression sequence */
476 int count
; /* Count of bytes for output */
483 * Do no compression...
487 line_end
= line
+ length
;
492 * Do run-length encoding...
495 line_end
= line
+ length
;
496 for (line_ptr
= line
, comp_ptr
= CompBuffer
;
498 comp_ptr
+= 2, line_ptr
+= count
)
501 (line_ptr
+ count
) < line_end
&&
502 line_ptr
[0] == line_ptr
[count
] &&
506 comp_ptr
[0] = count
- 1;
507 comp_ptr
[1] = line_ptr
[0];
510 line_ptr
= CompBuffer
;
516 * Do TIFF pack-bits encoding...
520 line_end
= line
+ length
;
521 comp_ptr
= CompBuffer
;
523 while (line_ptr
< line_end
)
525 if ((line_ptr
+ 1) >= line_end
)
528 * Single byte on the end...
532 *comp_ptr
++ = *line_ptr
++;
534 else if (line_ptr
[0] == line_ptr
[1])
537 * Repeated sequence...
543 while (line_ptr
< (line_end
- 1) &&
544 line_ptr
[0] == line_ptr
[1] &&
551 *comp_ptr
++ = 257 - count
;
552 *comp_ptr
++ = *line_ptr
++;
557 * Non-repeated sequence...
564 while (line_ptr
< (line_end
- 1) &&
565 line_ptr
[0] != line_ptr
[1] &&
572 *comp_ptr
++ = count
- 1;
574 memcpy(comp_ptr
, start
, count
);
579 line_ptr
= CompBuffer
;
585 * Set the length of the data and write a raster plane...
588 printf("\033*b%d%c", (int)(line_end
- line_ptr
), plane
);
589 fwrite(line_ptr
, line_end
- line_ptr
, 1, stdout
);
594 * 'OutputLine()' - Output a line of graphics.
598 OutputLine(cups_page_header2_t
*header
) /* I - Page header */
600 int plane
, /* Current plane */
601 bytes
, /* Bytes to write */
602 count
; /* Bytes to convert */
603 unsigned char bit
, /* Current plane data */
604 bit0
, /* Current low bit data */
605 bit1
, /* Current high bit data */
606 *plane_ptr
, /* Pointer into Planes */
607 *bit_ptr
; /* Pointer into BitBuffer */
611 * Output whitespace as needed...
616 printf("\033*b%dY", Feed
);
621 * Write bitmap data as needed...
624 bytes
= (header
->cupsWidth
+ 7) / 8;
626 for (plane
= 0; plane
< NumPlanes
; plane
++)
633 CompressData(Planes
[plane
], bytes
, plane
< (NumPlanes
- 1) ? 'V' : 'W',
634 header
->cupsCompression
);
639 * Separate low and high bit data into separate buffers.
642 for (count
= header
->cupsBytesPerLine
/ NumPlanes
,
643 plane_ptr
= Planes
[plane
], bit_ptr
= BitBuffer
;
645 count
-= 2, plane_ptr
+= 2, bit_ptr
++)
649 bit0
= ((bit
& 64) << 1) | ((bit
& 16) << 2) | ((bit
& 4) << 3) | ((bit
& 1) << 4);
650 bit1
= (bit
& 128) | ((bit
& 32) << 1) | ((bit
& 8) << 2) | ((bit
& 2) << 3);
656 bit0
|= (bit
& 1) | ((bit
& 4) >> 1) | ((bit
& 16) >> 2) | ((bit
& 64) >> 3);
657 bit1
|= ((bit
& 2) >> 1) | ((bit
& 8) >> 2) | ((bit
& 32) >> 3) | ((bit
& 128) >> 4);
661 bit_ptr
[bytes
] = bit1
;
665 * Send low and high bits...
668 CompressData(BitBuffer
, bytes
, 'V', header
->cupsCompression
);
669 CompressData(BitBuffer
+ bytes
, bytes
, plane
< (NumPlanes
- 1) ? 'V' : 'W',
670 header
->cupsCompression
);
678 * 'main()' - Main entry and processing of driver.
681 int /* O - Exit status */
682 main(int argc
, /* I - Number of command-line arguments */
683 char *argv
[]) /* I - Command-line arguments */
685 int fd
; /* File descriptor */
686 cups_raster_t
*ras
; /* Raster stream for printing */
687 cups_page_header2_t header
; /* Page header from file */
688 int y
; /* Current line */
689 ppd_file_t
*ppd
; /* PPD file */
690 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
691 struct sigaction action
; /* Actions for POSIX signals */
692 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
696 * Make sure status messages are not buffered...
699 setbuf(stderr
, NULL
);
702 * Check command-line...
705 if (argc
< 6 || argc
> 7)
708 * We don't have the correct number of arguments; write an error message
712 _cupsLangPrintf(stderr
,
713 _("Usage: %s job-id user title copies options [file]\n"),
719 * Open the page stream...
724 if ((fd
= open(argv
[6], O_RDONLY
)) == -1)
726 _cupsLangPrintf(stderr
, _("ERROR: Unable to open raster file - %s\n"),
735 ras
= cupsRasterOpen(fd
, CUPS_RASTER_READ
);
738 * Register a signal handler to eject the current page if the
744 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
745 sigset(SIGTERM
, CancelJob
);
746 #elif defined(HAVE_SIGACTION)
747 memset(&action
, 0, sizeof(action
));
749 sigemptyset(&action
.sa_mask
);
750 action
.sa_handler
= CancelJob
;
751 sigaction(SIGTERM
, &action
, NULL
);
753 signal(SIGTERM
, CancelJob
);
754 #endif /* HAVE_SIGSET */
757 * Initialize the print device...
760 ppd
= ppdOpenFile(getenv("PPD"));
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
);
787 StartPage(ppd
, &header
);
790 * Loop for each line on the page...
793 for (y
= 0; y
< header
.cupsHeight
; y
++)
796 * Let the user know how far we have progressed...
803 _cupsLangPrintf(stderr
, _("INFO: Printing page %d, %d%% complete...\n"),
804 Page
, 100 * y
/ header
.cupsHeight
);
807 * Read a line of graphics...
810 if (cupsRasterReadPixels(ras
, Planes
[0], header
.cupsBytesPerLine
) < 1)
814 * See if the line is blank; if not, write it to the printer...
818 memcmp(Planes
[0], Planes
[0] + 1, header
.cupsBytesPerLine
- 1))
835 * Shutdown the printer...
844 * Close the raster stream...
847 cupsRasterClose(ras
);
852 * If no pages were printed, send an error message...
857 _cupsLangPuts(stderr
, _("ERROR: No pages found!\n"));
862 _cupsLangPuts(stderr
, _("INFO: Ready to print.\n"));
869 * End of "$Id: rastertohp.c 7834 2008-08-04 21:02:09Z mike $".