2 * "$Id: rastertohp.c 6649 2007-07-11 21:46:42Z 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 */
186 case 624 : /* DL Envelope */
187 printf("\033&l90A"); /* Set page size */
190 case 649 : /* C5 Envelope */
191 printf("\033&l91A"); /* Set page size */
194 case 684 : /* COM-10 Envelope */
195 printf("\033&l81A"); /* Set page size */
198 case 709 : /* B5 Envelope */
199 printf("\033&l100A"); /* Set page size */
202 case 756 : /* Executive */
203 printf("\033&l1A"); /* Set page size */
206 case 792 : /* Letter */
207 printf("\033&l2A"); /* Set page size */
211 printf("\033&l26A"); /* Set page size */
214 case 1008 : /* Legal */
215 printf("\033&l3A"); /* Set page size */
219 printf("\033&l27A"); /* Set page size */
222 case 1224 : /* Tabloid */
223 printf("\033&l6A"); /* Set page size */
227 printf("\033&l%dP", /* Set page length */
228 header
->PageSize
[1] / 12);
229 printf("\033&l0E"); /* Set top margin to 0 */
232 if (!Duplex
|| (Page
& 1))
235 * Set other job options...
238 printf("\033&l%dX", header
->NumCopies
); /* Set number copies */
240 if (header
->cupsMediaType
&&
241 (!ppd
|| ppd
->model_number
!= 2 || header
->HWResolution
[0] == 600))
242 printf("\033&l%dM", /* Set media type */
243 header
->cupsMediaType
);
245 if (!ppd
|| ppd
->model_number
!= 2)
248 printf("\033&l%dS", /* Set duplex mode */
249 header
->Duplex
+ header
->Tumble
);
251 printf("\033&l0L"); /* Turn off perforation skip */
254 else if (!ppd
|| ppd
->model_number
!= 2)
255 printf("\033&a2G"); /* Set back side */
258 * Set graphics mode...
261 if (ppd
&& ppd
->model_number
== 2)
264 * Figure out the number of color planes...
267 if (header
->cupsColorSpace
== CUPS_CSPACE_KCMY
)
273 * Set the resolution and top-of-form...
276 printf("\033&u%dD", header
->HWResolution
[0]);
278 printf("\033&l0e0L"); /* Reset top and don't skip */
279 printf("\033*p0Y\033*p0X"); /* Set top of form */
282 * Send 26-byte configure image data command with horizontal and
283 * vertical resolutions as well as a color count...
287 putchar(2); /* Format 2 */
288 putchar(NumPlanes
); /* Output planes */
290 putchar(header
->HWResolution
[0] >> 8); /* Black resolution */
291 putchar(header
->HWResolution
[0]);
292 putchar(header
->HWResolution
[1] >> 8);
293 putchar(header
->HWResolution
[1]);
295 putchar(1 << ColorBits
); /* # of black levels */
297 putchar(header
->HWResolution
[0] >> 8); /* Cyan resolution */
298 putchar(header
->HWResolution
[0]);
299 putchar(header
->HWResolution
[1] >> 8);
300 putchar(header
->HWResolution
[1]);
302 putchar(1 << ColorBits
); /* # of cyan levels */
304 putchar(header
->HWResolution
[0] >> 8); /* Magenta resolution */
305 putchar(header
->HWResolution
[0]);
306 putchar(header
->HWResolution
[1] >> 8);
307 putchar(header
->HWResolution
[1]);
309 putchar(1 << ColorBits
); /* # of magenta levels */
311 putchar(header
->HWResolution
[0] >> 8); /* Yellow resolution */
312 putchar(header
->HWResolution
[0]);
313 putchar(header
->HWResolution
[1] >> 8);
314 putchar(header
->HWResolution
[1]);
316 putchar(1 << ColorBits
); /* # of yellow levels */
318 printf("\033&l0H"); /* Set media position */
322 printf("\033*t%dR", header
->HWResolution
[0]);
325 if (header
->cupsColorSpace
== CUPS_CSPACE_KCMY
)
328 printf("\033*r-4U"); /* Set KCMY graphics */
330 else if (header
->cupsColorSpace
== CUPS_CSPACE_CMY
)
333 printf("\033*r-3U"); /* Set CMY graphics */
336 NumPlanes
= 1; /* Black&white graphics */
339 * Set size and position of graphics...
342 printf("\033*r%dS", header
->cupsWidth
); /* Set width */
343 printf("\033*r%dT", header
->cupsHeight
); /* Set height */
345 printf("\033&a0H"); /* Set horizontal position */
348 printf("\033&a%.0fV", /* Set vertical position */
349 10.0 * (ppd
->sizes
[0].length
- ppd
->sizes
[0].top
));
351 printf("\033&a0V"); /* Set top-of-page */
354 printf("\033*r1A"); /* Start graphics */
356 if (header
->cupsCompression
)
357 printf("\033*b%dM", /* Set compression */
358 header
->cupsCompression
);
360 Feed
= 0; /* No blank lines yet */
363 * Allocate memory for a line of graphics...
366 if ((Planes
[0] = malloc(header
->cupsBytesPerLine
)) == NULL
)
368 fputs("ERROR: Unable to allocate memory!\n", stderr
);
372 for (plane
= 1; plane
< NumPlanes
; plane
++)
373 Planes
[plane
] = Planes
[0] + plane
* header
->cupsBytesPerLine
/ NumPlanes
;
376 BitBuffer
= malloc(ColorBits
* ((header
->cupsWidth
+ 7) / 8));
380 if (header
->cupsCompression
)
381 CompBuffer
= malloc(header
->cupsBytesPerLine
* 2);
388 * 'EndPage()' - Finish a page of graphics.
395 * Eject the current page...
400 printf("\033*rC"); /* End color GFX */
402 if (!(Duplex
&& (Page
& 1)))
403 printf("\033&l0H"); /* Eject current page */
407 printf("\033*r0B"); /* End GFX */
409 if (!(Duplex
&& (Page
& 1)))
410 printf("\014"); /* Eject current page */
430 * 'Shutdown()' - Shutdown the printer.
437 * Send a PCL reset sequence.
446 * 'CancelJob()' - Cancel the current job...
450 CancelJob(int sig
) /* I - Signal */
459 * 'CompressData()' - Compress a line of graphics.
463 CompressData(unsigned char *line
, /* I - Data to compress */
464 int length
, /* I - Number of bytes */
465 int plane
, /* I - Color plane */
466 int type
) /* I - Type of compression */
468 unsigned char *line_ptr
, /* Current byte pointer */
469 *line_end
, /* End-of-line byte pointer */
470 *comp_ptr
, /* Pointer into compression buffer */
471 *start
; /* Start of compression sequence */
472 int count
; /* Count of bytes for output */
479 * Do no compression...
483 line_end
= line
+ length
;
488 * Do run-length encoding...
491 line_end
= line
+ length
;
492 for (line_ptr
= line
, comp_ptr
= CompBuffer
;
494 comp_ptr
+= 2, line_ptr
+= count
)
497 (line_ptr
+ count
) < line_end
&&
498 line_ptr
[0] == line_ptr
[count
] &&
502 comp_ptr
[0] = count
- 1;
503 comp_ptr
[1] = line_ptr
[0];
506 line_ptr
= CompBuffer
;
512 * Do TIFF pack-bits encoding...
516 line_end
= line
+ length
;
517 comp_ptr
= CompBuffer
;
519 while (line_ptr
< line_end
)
521 if ((line_ptr
+ 1) >= line_end
)
524 * Single byte on the end...
528 *comp_ptr
++ = *line_ptr
++;
530 else if (line_ptr
[0] == line_ptr
[1])
533 * Repeated sequence...
539 while (line_ptr
< (line_end
- 1) &&
540 line_ptr
[0] == line_ptr
[1] &&
547 *comp_ptr
++ = 257 - count
;
548 *comp_ptr
++ = *line_ptr
++;
553 * Non-repeated sequence...
560 while (line_ptr
< (line_end
- 1) &&
561 line_ptr
[0] != line_ptr
[1] &&
568 *comp_ptr
++ = count
- 1;
570 memcpy(comp_ptr
, start
, count
);
575 line_ptr
= CompBuffer
;
581 * Set the length of the data and write a raster plane...
584 printf("\033*b%d%c", (int)(line_end
- line_ptr
), plane
);
585 fwrite(line_ptr
, line_end
- line_ptr
, 1, stdout
);
590 * 'OutputLine()' - Output a line of graphics.
594 OutputLine(cups_page_header2_t
*header
) /* I - Page header */
596 int plane
, /* Current plane */
597 bytes
, /* Bytes to write */
598 count
; /* Bytes to convert */
599 unsigned char bit
, /* Current plane data */
600 bit0
, /* Current low bit data */
601 bit1
, /* Current high bit data */
602 *plane_ptr
, /* Pointer into Planes */
603 *bit_ptr
; /* Pointer into BitBuffer */
607 * Output whitespace as needed...
612 printf("\033*b%dY", Feed
);
617 * Write bitmap data as needed...
620 bytes
= (header
->cupsWidth
+ 7) / 8;
622 for (plane
= 0; plane
< NumPlanes
; plane
++)
629 CompressData(Planes
[plane
], bytes
, plane
< (NumPlanes
- 1) ? 'V' : 'W',
630 header
->cupsCompression
);
635 * Separate low and high bit data into separate buffers.
638 for (count
= header
->cupsBytesPerLine
/ NumPlanes
,
639 plane_ptr
= Planes
[plane
], bit_ptr
= BitBuffer
;
641 count
-= 2, plane_ptr
+= 2, bit_ptr
++)
645 bit0
= ((bit
& 64) << 1) | ((bit
& 16) << 2) | ((bit
& 4) << 3) | ((bit
& 1) << 4);
646 bit1
= (bit
& 128) | ((bit
& 32) << 1) | ((bit
& 8) << 2) | ((bit
& 2) << 3);
652 bit0
|= (bit
& 1) | ((bit
& 4) >> 1) | ((bit
& 16) >> 2) | ((bit
& 64) >> 3);
653 bit1
|= ((bit
& 2) >> 1) | ((bit
& 8) >> 2) | ((bit
& 32) >> 3) | ((bit
& 128) >> 4);
657 bit_ptr
[bytes
] = bit1
;
661 * Send low and high bits...
664 CompressData(BitBuffer
, bytes
, 'V', header
->cupsCompression
);
665 CompressData(BitBuffer
+ bytes
, bytes
, plane
< (NumPlanes
- 1) ? 'V' : 'W',
666 header
->cupsCompression
);
674 * 'main()' - Main entry and processing of driver.
677 int /* O - Exit status */
678 main(int argc
, /* I - Number of command-line arguments */
679 char *argv
[]) /* I - Command-line arguments */
681 int fd
; /* File descriptor */
682 cups_raster_t
*ras
; /* Raster stream for printing */
683 cups_page_header2_t header
; /* Page header from file */
684 int y
; /* Current line */
685 ppd_file_t
*ppd
; /* PPD file */
686 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
687 struct sigaction action
; /* Actions for POSIX signals */
688 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
692 * Make sure status messages are not buffered...
695 setbuf(stderr
, NULL
);
698 * Check command-line...
701 if (argc
< 6 || argc
> 7)
704 * We don't have the correct number of arguments; write an error message
708 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
714 * Open the page stream...
719 if ((fd
= open(argv
[6], O_RDONLY
)) == -1)
721 perror("ERROR: Unable to open raster file - ");
729 ras
= cupsRasterOpen(fd
, CUPS_RASTER_READ
);
732 * Register a signal handler to eject the current page if the
738 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
739 sigset(SIGTERM
, CancelJob
);
740 #elif defined(HAVE_SIGACTION)
741 memset(&action
, 0, sizeof(action
));
743 sigemptyset(&action
.sa_mask
);
744 action
.sa_handler
= CancelJob
;
745 sigaction(SIGTERM
, &action
, NULL
);
747 signal(SIGTERM
, CancelJob
);
748 #endif /* HAVE_SIGSET */
751 * Initialize the print device...
754 ppd
= ppdOpenFile(getenv("PPD"));
759 * Process pages as needed...
764 while (cupsRasterReadHeader2(ras
, &header
))
767 * Write a status message with the page number and number of copies.
775 fprintf(stderr
, "PAGE: %d %d\n", Page
, header
.NumCopies
);
781 StartPage(ppd
, &header
);
784 * Loop for each line on the page...
787 for (y
= 0; y
< header
.cupsHeight
; y
++)
790 * Let the user know how far we have progressed...
797 fprintf(stderr
, _("INFO: Printing page %d, %d%% complete...\n"), Page
,
798 100 * y
/ header
.cupsHeight
);
801 * Read a line of graphics...
804 if (cupsRasterReadPixels(ras
, Planes
[0], header
.cupsBytesPerLine
) < 1)
808 * See if the line is blank; if not, write it to the printer...
812 memcmp(Planes
[0], Planes
[0] + 1, header
.cupsBytesPerLine
- 1))
829 * Shutdown the printer...
838 * Close the raster stream...
841 cupsRasterClose(ras
);
846 * If no pages were printed, send an error message...
850 fputs(_("ERROR: No pages found!\n"), stderr
);
852 fputs(_("INFO: Ready to print.\n"), stderr
);
859 * End of "$Id: rastertohp.c 6649 2007-07-11 21:46:42Z mike $".