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-2008 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>
48 unsigned char *Planes
[4], /* Output buffers */
49 *CompBuffer
, /* Compression buffer */
50 *BitBuffer
; /* Buffer for output bits */
51 int NumPlanes
, /* Number of color planes */
52 ColorBits
, /* Number of bits per color */
53 Feed
, /* Number of lines to skip */
54 Duplex
, /* Current duplex mode */
55 Page
, /* Current page number */
56 Canceled
; /* Has the current job been canceled? */
64 void StartPage(ppd_file_t
*ppd
, cups_page_header2_t
*header
);
68 void CancelJob(int sig
);
69 void CompressData(unsigned char *line
, int length
, int plane
, int type
);
70 void OutputLine(cups_page_header2_t
*header
);
74 * 'Setup()' - Prepare the printer for printing.
81 * Send a PCL reset sequence.
90 * 'StartPage()' - Start a page of graphics.
94 StartPage(ppd_file_t
*ppd
, /* I - PPD file */
95 cups_page_header2_t
*header
) /* I - Page header */
97 int plane
; /* Looping var */
101 * Show page device dictionary...
104 fprintf(stderr
, "DEBUG: StartPage...\n");
105 fprintf(stderr
, "DEBUG: MediaClass = \"%s\"\n", header
->MediaClass
);
106 fprintf(stderr
, "DEBUG: MediaColor = \"%s\"\n", header
->MediaColor
);
107 fprintf(stderr
, "DEBUG: MediaType = \"%s\"\n", header
->MediaType
);
108 fprintf(stderr
, "DEBUG: OutputType = \"%s\"\n", header
->OutputType
);
110 fprintf(stderr
, "DEBUG: AdvanceDistance = %d\n", header
->AdvanceDistance
);
111 fprintf(stderr
, "DEBUG: AdvanceMedia = %d\n", header
->AdvanceMedia
);
112 fprintf(stderr
, "DEBUG: Collate = %d\n", header
->Collate
);
113 fprintf(stderr
, "DEBUG: CutMedia = %d\n", header
->CutMedia
);
114 fprintf(stderr
, "DEBUG: Duplex = %d\n", header
->Duplex
);
115 fprintf(stderr
, "DEBUG: HWResolution = [ %d %d ]\n", header
->HWResolution
[0],
116 header
->HWResolution
[1]);
117 fprintf(stderr
, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n",
118 header
->ImagingBoundingBox
[0], header
->ImagingBoundingBox
[1],
119 header
->ImagingBoundingBox
[2], header
->ImagingBoundingBox
[3]);
120 fprintf(stderr
, "DEBUG: InsertSheet = %d\n", header
->InsertSheet
);
121 fprintf(stderr
, "DEBUG: Jog = %d\n", header
->Jog
);
122 fprintf(stderr
, "DEBUG: LeadingEdge = %d\n", header
->LeadingEdge
);
123 fprintf(stderr
, "DEBUG: Margins = [ %d %d ]\n", header
->Margins
[0],
125 fprintf(stderr
, "DEBUG: ManualFeed = %d\n", header
->ManualFeed
);
126 fprintf(stderr
, "DEBUG: MediaPosition = %d\n", header
->MediaPosition
);
127 fprintf(stderr
, "DEBUG: MediaWeight = %d\n", header
->MediaWeight
);
128 fprintf(stderr
, "DEBUG: MirrorPrint = %d\n", header
->MirrorPrint
);
129 fprintf(stderr
, "DEBUG: NegativePrint = %d\n", header
->NegativePrint
);
130 fprintf(stderr
, "DEBUG: NumCopies = %d\n", header
->NumCopies
);
131 fprintf(stderr
, "DEBUG: Orientation = %d\n", header
->Orientation
);
132 fprintf(stderr
, "DEBUG: OutputFaceUp = %d\n", header
->OutputFaceUp
);
133 fprintf(stderr
, "DEBUG: PageSize = [ %d %d ]\n", header
->PageSize
[0],
134 header
->PageSize
[1]);
135 fprintf(stderr
, "DEBUG: Separations = %d\n", header
->Separations
);
136 fprintf(stderr
, "DEBUG: TraySwitch = %d\n", header
->TraySwitch
);
137 fprintf(stderr
, "DEBUG: Tumble = %d\n", header
->Tumble
);
138 fprintf(stderr
, "DEBUG: cupsWidth = %d\n", header
->cupsWidth
);
139 fprintf(stderr
, "DEBUG: cupsHeight = %d\n", header
->cupsHeight
);
140 fprintf(stderr
, "DEBUG: cupsMediaType = %d\n", header
->cupsMediaType
);
141 fprintf(stderr
, "DEBUG: cupsBitsPerColor = %d\n", header
->cupsBitsPerColor
);
142 fprintf(stderr
, "DEBUG: cupsBitsPerPixel = %d\n", header
->cupsBitsPerPixel
);
143 fprintf(stderr
, "DEBUG: cupsBytesPerLine = %d\n", header
->cupsBytesPerLine
);
144 fprintf(stderr
, "DEBUG: cupsColorOrder = %d\n", header
->cupsColorOrder
);
145 fprintf(stderr
, "DEBUG: cupsColorSpace = %d\n", header
->cupsColorSpace
);
146 fprintf(stderr
, "DEBUG: cupsCompression = %d\n", header
->cupsCompression
);
149 * Setup printer/job attributes...
152 Duplex
= header
->Duplex
;
153 ColorBits
= header
->cupsBitsPerColor
;
155 if ((!Duplex
|| (Page
& 1)) && header
->MediaPosition
)
156 printf("\033&l%dH", /* Set media position */
157 header
->MediaPosition
);
159 if (Duplex
&& ppd
&& ppd
->model_number
== 2)
162 * Handle duplexing on new DeskJet printers...
165 printf("\033&l-2H"); /* Load media */
168 printf("\033&l2S"); /* Set duplex mode */
171 if (!Duplex
|| (Page
& 1) || (ppd
&& ppd
->model_number
== 2))
174 * Set the media size...
177 printf("\033&l6D\033&k12H"); /* Set 6 LPI, 10 CPI */
178 printf("\033&l0O"); /* Set portrait orientation */
180 switch (header
->PageSize
[1])
182 case 540 : /* Monarch Envelope */
183 printf("\033&l80A"); /* Set page size */
187 printf("\033&l25A"); /* Set page size */
190 case 624 : /* DL Envelope */
191 printf("\033&l90A"); /* Set page size */
194 case 649 : /* C5 Envelope */
195 printf("\033&l91A"); /* Set page size */
198 case 684 : /* COM-10 Envelope */
199 printf("\033&l81A"); /* Set page size */
202 case 709 : /* B5 Envelope */
203 printf("\033&l100A"); /* Set page size */
206 case 756 : /* Executive */
207 printf("\033&l1A"); /* Set page size */
210 case 792 : /* Letter */
211 printf("\033&l2A"); /* Set page size */
215 printf("\033&l26A"); /* Set page size */
218 case 1008 : /* Legal */
219 printf("\033&l3A"); /* Set page size */
223 printf("\033&l27A"); /* Set page size */
226 case 1224 : /* Tabloid */
227 printf("\033&l6A"); /* Set page size */
231 printf("\033&l%dP", /* Set page length */
232 header
->PageSize
[1] / 12);
233 printf("\033&l0E"); /* Set top margin to 0 */
236 if (!Duplex
|| (Page
& 1))
239 * Set other job options...
242 printf("\033&l%dX", header
->NumCopies
); /* Set number copies */
244 if (header
->cupsMediaType
&&
245 (!ppd
|| ppd
->model_number
!= 2 || header
->HWResolution
[0] == 600))
246 printf("\033&l%dM", /* Set media type */
247 header
->cupsMediaType
);
249 if (!ppd
|| ppd
->model_number
!= 2)
252 printf("\033&l%dS", /* Set duplex mode */
253 header
->Duplex
+ header
->Tumble
);
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 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
718 * Open the page stream...
723 if ((fd
= open(argv
[6], O_RDONLY
)) == -1)
725 perror("ERROR: Unable to open raster file - ");
733 ras
= cupsRasterOpen(fd
, CUPS_RASTER_READ
);
736 * Register a signal handler to eject the current page if the
742 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
743 sigset(SIGTERM
, CancelJob
);
744 #elif defined(HAVE_SIGACTION)
745 memset(&action
, 0, sizeof(action
));
747 sigemptyset(&action
.sa_mask
);
748 action
.sa_handler
= CancelJob
;
749 sigaction(SIGTERM
, &action
, NULL
);
751 signal(SIGTERM
, CancelJob
);
752 #endif /* HAVE_SIGSET */
755 * Initialize the print device...
758 ppd
= ppdOpenFile(getenv("PPD"));
763 * Process pages as needed...
768 while (cupsRasterReadHeader2(ras
, &header
))
771 * Write a status message with the page number and number of copies.
779 fprintf(stderr
, "PAGE: %d %d\n", Page
, header
.NumCopies
);
785 StartPage(ppd
, &header
);
788 * Loop for each line on the page...
791 for (y
= 0; y
< header
.cupsHeight
; y
++)
794 * Let the user know how far we have progressed...
801 fprintf(stderr
, _("INFO: Printing page %d, %d%% complete...\n"), Page
,
802 100 * y
/ header
.cupsHeight
);
805 * Read a line of graphics...
808 if (cupsRasterReadPixels(ras
, Planes
[0], header
.cupsBytesPerLine
) < 1)
812 * See if the line is blank; if not, write it to the printer...
816 memcmp(Planes
[0], Planes
[0] + 1, header
.cupsBytesPerLine
- 1))
833 * Shutdown the printer...
842 * Close the raster stream...
845 cupsRasterClose(ras
);
850 * If no pages were printed, send an error message...
854 fputs(_("ERROR: No pages found!\n"), stderr
);
856 fputs(_("INFO: Ready to print.\n"), stderr
);
863 * End of "$Id: rastertohp.c 7834 2008-08-04 21:02:09Z mike $".