2 * "$Id: rastertohp.c 9384 2010-11-22 07:06:39Z mike $"
4 * Hewlett-Packard Page Control Language filter for CUPS.
6 * Copyright 2007-2010 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 * Setup() - Prepare the printer for printing.
20 * StartPage() - Start a page of graphics.
21 * EndPage() - Finish a page of graphics.
22 * Shutdown() - Shutdown the printer.
23 * CancelJob() - Cancel the current job...
24 * CompressData() - Compress a line of graphics.
25 * OutputLine() - Output a line of graphics.
26 * main() - Main entry and processing of driver.
30 * Include necessary headers...
33 #include <cups/cups.h>
35 #include <cups/string-private.h>
36 #include <cups/language-private.h>
37 #include <cups/raster.h>
47 unsigned char *Planes[4], /* Output buffers */
48 *CompBuffer, /* Compression buffer */
49 *BitBuffer; /* Buffer for output bits */
50 int NumPlanes, /* Number of color planes */
51 ColorBits, /* Number of bits per color */
52 Feed, /* Number of lines to skip */
53 Duplex, /* Current duplex mode */
54 Page, /* Current page number */
55 Canceled; /* Has the current job been canceled? */
63 void StartPage(ppd_file_t *ppd, cups_page_header2_t *header);
67 void CancelJob(int sig);
68 void CompressData(unsigned char *line, int length, int plane, int type);
69 void OutputLine(cups_page_header2_t *header);
73 * 'Setup()' - Prepare the printer for printing.
80 * Send a PCL reset sequence.
89 * 'StartPage()' - Start a page of graphics.
93 StartPage(ppd_file_t *ppd, /* I - PPD file */
94 cups_page_header2_t *header) /* I - Page header */
96 int plane; /* Looping var */
100 * Show page device dictionary...
103 fprintf(stderr, "DEBUG: StartPage...\n");
104 fprintf(stderr, "DEBUG: MediaClass = \"%s\"\n", header->MediaClass);
105 fprintf(stderr, "DEBUG: MediaColor = \"%s\"\n", header->MediaColor);
106 fprintf(stderr, "DEBUG: MediaType = \"%s\"\n", header->MediaType);
107 fprintf(stderr, "DEBUG: OutputType = \"%s\"\n", header->OutputType);
109 fprintf(stderr, "DEBUG: AdvanceDistance = %d\n", header->AdvanceDistance);
110 fprintf(stderr, "DEBUG: AdvanceMedia = %d\n", header->AdvanceMedia);
111 fprintf(stderr, "DEBUG: Collate = %d\n", header->Collate);
112 fprintf(stderr, "DEBUG: CutMedia = %d\n", header->CutMedia);
113 fprintf(stderr, "DEBUG: Duplex = %d\n", header->Duplex);
114 fprintf(stderr, "DEBUG: HWResolution = [ %d %d ]\n", header->HWResolution[0],
115 header->HWResolution[1]);
116 fprintf(stderr, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n",
117 header->ImagingBoundingBox[0], header->ImagingBoundingBox[1],
118 header->ImagingBoundingBox[2], header->ImagingBoundingBox[3]);
119 fprintf(stderr, "DEBUG: InsertSheet = %d\n", header->InsertSheet);
120 fprintf(stderr, "DEBUG: Jog = %d\n", header->Jog);
121 fprintf(stderr, "DEBUG: LeadingEdge = %d\n", header->LeadingEdge);
122 fprintf(stderr, "DEBUG: Margins = [ %d %d ]\n", header->Margins[0],
124 fprintf(stderr, "DEBUG: ManualFeed = %d\n", header->ManualFeed);
125 fprintf(stderr, "DEBUG: MediaPosition = %d\n", header->MediaPosition);
126 fprintf(stderr, "DEBUG: MediaWeight = %d\n", header->MediaWeight);
127 fprintf(stderr, "DEBUG: MirrorPrint = %d\n", header->MirrorPrint);
128 fprintf(stderr, "DEBUG: NegativePrint = %d\n", header->NegativePrint);
129 fprintf(stderr, "DEBUG: NumCopies = %d\n", header->NumCopies);
130 fprintf(stderr, "DEBUG: Orientation = %d\n", header->Orientation);
131 fprintf(stderr, "DEBUG: OutputFaceUp = %d\n", header->OutputFaceUp);
132 fprintf(stderr, "DEBUG: PageSize = [ %d %d ]\n", header->PageSize[0],
133 header->PageSize[1]);
134 fprintf(stderr, "DEBUG: Separations = %d\n", header->Separations);
135 fprintf(stderr, "DEBUG: TraySwitch = %d\n", header->TraySwitch);
136 fprintf(stderr, "DEBUG: Tumble = %d\n", header->Tumble);
137 fprintf(stderr, "DEBUG: cupsWidth = %d\n", header->cupsWidth);
138 fprintf(stderr, "DEBUG: cupsHeight = %d\n", header->cupsHeight);
139 fprintf(stderr, "DEBUG: cupsMediaType = %d\n", header->cupsMediaType);
140 fprintf(stderr, "DEBUG: cupsBitsPerColor = %d\n", header->cupsBitsPerColor);
141 fprintf(stderr, "DEBUG: cupsBitsPerPixel = %d\n", header->cupsBitsPerPixel);
142 fprintf(stderr, "DEBUG: cupsBytesPerLine = %d\n", header->cupsBytesPerLine);
143 fprintf(stderr, "DEBUG: cupsColorOrder = %d\n", header->cupsColorOrder);
144 fprintf(stderr, "DEBUG: cupsColorSpace = %d\n", header->cupsColorSpace);
145 fprintf(stderr, "DEBUG: cupsCompression = %d\n", header->cupsCompression);
148 * Setup printer/job attributes...
151 Duplex = header->Duplex;
152 ColorBits = header->cupsBitsPerColor;
154 if ((!Duplex || (Page & 1)) && header->MediaPosition)
155 printf("\033&l%dH", /* Set media position */
156 header->MediaPosition);
158 if (Duplex && ppd && ppd->model_number == 2)
161 * Handle duplexing on new DeskJet printers...
164 printf("\033&l-2H"); /* Load media */
167 printf("\033&l2S"); /* Set duplex mode */
170 if (!Duplex || (Page & 1) || (ppd && ppd->model_number == 2))
173 * Set the media size...
176 printf("\033&l6D\033&k12H"); /* Set 6 LPI, 10 CPI */
177 printf("\033&l0O"); /* Set portrait orientation */
179 switch (header->PageSize[1])
181 case 540 : /* Monarch Envelope */
182 printf("\033&l80A"); /* Set page size */
186 printf("\033&l25A"); /* Set page size */
189 case 624 : /* DL Envelope */
190 printf("\033&l90A"); /* Set page size */
193 case 649 : /* C5 Envelope */
194 printf("\033&l91A"); /* Set page size */
197 case 684 : /* COM-10 Envelope */
198 printf("\033&l81A"); /* Set page size */
201 case 709 : /* B5 Envelope */
202 printf("\033&l100A"); /* Set page size */
205 case 756 : /* Executive */
206 printf("\033&l1A"); /* Set page size */
209 case 792 : /* Letter */
210 printf("\033&l2A"); /* Set page size */
214 printf("\033&l26A"); /* Set page size */
217 case 1008 : /* Legal */
218 printf("\033&l3A"); /* Set page size */
222 printf("\033&l27A"); /* Set page size */
225 case 1224 : /* Tabloid */
226 printf("\033&l6A"); /* Set page size */
230 printf("\033&l%dP", /* Set page length */
231 header->PageSize[1] / 12);
232 printf("\033&l0E"); /* Set top margin to 0 */
235 if (!Duplex || (Page & 1))
238 * Set other job options...
241 printf("\033&l%dX", header->NumCopies); /* Set number copies */
243 if (header->cupsMediaType &&
244 (!ppd || ppd->model_number != 2 || header->HWResolution[0] == 600))
245 printf("\033&l%dM", /* Set media type */
246 header->cupsMediaType);
248 if (!ppd || ppd->model_number != 2)
250 int mode = Duplex ? 1 + header->Tumble != 0 : 0;
252 printf("\033&l%dS", mode); /* Set duplex mode */
253 printf("\033&l0L"); /* Turn off perforation skip */
256 else if (!ppd || ppd->model_number != 2)
257 printf("\033&a2G"); /* Set back side */
260 * Set graphics mode...
263 if (ppd && ppd->model_number == 2)
266 * Figure out the number of color planes...
269 if (header->cupsColorSpace == CUPS_CSPACE_KCMY)
275 * Set the resolution and top-of-form...
278 printf("\033&u%dD", header->HWResolution[0]);
280 printf("\033&l0e0L"); /* Reset top and don't skip */
281 printf("\033*p0Y\033*p0X"); /* Set top of form */
284 * Send 26-byte configure image data command with horizontal and
285 * vertical resolutions as well as a color count...
289 putchar(2); /* Format 2 */
290 putchar(NumPlanes); /* Output planes */
292 putchar(header->HWResolution[0] >> 8); /* Black resolution */
293 putchar(header->HWResolution[0]);
294 putchar(header->HWResolution[1] >> 8);
295 putchar(header->HWResolution[1]);
297 putchar(1 << ColorBits); /* # of black levels */
299 putchar(header->HWResolution[0] >> 8); /* Cyan resolution */
300 putchar(header->HWResolution[0]);
301 putchar(header->HWResolution[1] >> 8);
302 putchar(header->HWResolution[1]);
304 putchar(1 << ColorBits); /* # of cyan levels */
306 putchar(header->HWResolution[0] >> 8); /* Magenta resolution */
307 putchar(header->HWResolution[0]);
308 putchar(header->HWResolution[1] >> 8);
309 putchar(header->HWResolution[1]);
311 putchar(1 << ColorBits); /* # of magenta levels */
313 putchar(header->HWResolution[0] >> 8); /* Yellow resolution */
314 putchar(header->HWResolution[0]);
315 putchar(header->HWResolution[1] >> 8);
316 putchar(header->HWResolution[1]);
318 putchar(1 << ColorBits); /* # of yellow levels */
320 printf("\033&l0H"); /* Set media position */
324 printf("\033*t%dR", header->HWResolution[0]);
327 if (header->cupsColorSpace == CUPS_CSPACE_KCMY)
330 printf("\033*r-4U"); /* Set KCMY graphics */
332 else if (header->cupsColorSpace == CUPS_CSPACE_CMY)
335 printf("\033*r-3U"); /* Set CMY graphics */
338 NumPlanes = 1; /* Black&white graphics */
341 * Set size and position of graphics...
344 printf("\033*r%dS", header->cupsWidth); /* Set width */
345 printf("\033*r%dT", header->cupsHeight); /* Set height */
347 printf("\033&a0H"); /* Set horizontal position */
350 printf("\033&a%.0fV", /* Set vertical position */
351 10.0 * (ppd->sizes[0].length - ppd->sizes[0].top));
353 printf("\033&a0V"); /* Set top-of-page */
356 printf("\033*r1A"); /* Start graphics */
358 if (header->cupsCompression)
359 printf("\033*b%dM", /* Set compression */
360 header->cupsCompression);
362 Feed = 0; /* No blank lines yet */
365 * Allocate memory for a line of graphics...
368 if ((Planes[0] = malloc(header->cupsBytesPerLine)) == NULL)
370 fputs("ERROR: Unable to allocate memory\n", stderr);
374 for (plane = 1; plane < NumPlanes; plane ++)
375 Planes[plane] = Planes[0] + plane * header->cupsBytesPerLine / NumPlanes;
378 BitBuffer = malloc(ColorBits * ((header->cupsWidth + 7) / 8));
382 if (header->cupsCompression)
383 CompBuffer = malloc(header->cupsBytesPerLine * 2);
390 * 'EndPage()' - Finish a page of graphics.
397 * Eject the current page...
402 printf("\033*rC"); /* End color GFX */
404 if (!(Duplex && (Page & 1)))
405 printf("\033&l0H"); /* Eject current page */
409 printf("\033*r0B"); /* End GFX */
411 if (!(Duplex && (Page & 1)))
412 printf("\014"); /* Eject current page */
432 * 'Shutdown()' - Shutdown the printer.
439 * Send a PCL reset sequence.
448 * 'CancelJob()' - Cancel the current job...
452 CancelJob(int sig) /* I - Signal */
461 * 'CompressData()' - Compress a line of graphics.
465 CompressData(unsigned char *line, /* I - Data to compress */
466 int length, /* I - Number of bytes */
467 int plane, /* I - Color plane */
468 int type) /* I - Type of compression */
470 unsigned char *line_ptr, /* Current byte pointer */
471 *line_end, /* End-of-line byte pointer */
472 *comp_ptr, /* Pointer into compression buffer */
473 *start; /* Start of compression sequence */
474 int count; /* Count of bytes for output */
481 * Do no compression...
485 line_end = line + length;
490 * Do run-length encoding...
493 line_end = line + length;
494 for (line_ptr = line, comp_ptr = CompBuffer;
496 comp_ptr += 2, line_ptr += count)
499 (line_ptr + count) < line_end &&
500 line_ptr[0] == line_ptr[count] &&
504 comp_ptr[0] = count - 1;
505 comp_ptr[1] = line_ptr[0];
508 line_ptr = CompBuffer;
514 * Do TIFF pack-bits encoding...
518 line_end = line + length;
519 comp_ptr = CompBuffer;
521 while (line_ptr < line_end)
523 if ((line_ptr + 1) >= line_end)
526 * Single byte on the end...
530 *comp_ptr++ = *line_ptr++;
532 else if (line_ptr[0] == line_ptr[1])
535 * Repeated sequence...
541 while (line_ptr < (line_end - 1) &&
542 line_ptr[0] == line_ptr[1] &&
549 *comp_ptr++ = 257 - count;
550 *comp_ptr++ = *line_ptr++;
555 * Non-repeated sequence...
562 while (line_ptr < (line_end - 1) &&
563 line_ptr[0] != line_ptr[1] &&
570 *comp_ptr++ = count - 1;
572 memcpy(comp_ptr, start, count);
577 line_ptr = CompBuffer;
583 * Set the length of the data and write a raster plane...
586 printf("\033*b%d%c", (int)(line_end - line_ptr), plane);
587 fwrite(line_ptr, line_end - line_ptr, 1, stdout);
592 * 'OutputLine()' - Output a line of graphics.
596 OutputLine(cups_page_header2_t *header) /* I - Page header */
598 int plane, /* Current plane */
599 bytes, /* Bytes to write */
600 count; /* Bytes to convert */
601 unsigned char bit, /* Current plane data */
602 bit0, /* Current low bit data */
603 bit1, /* Current high bit data */
604 *plane_ptr, /* Pointer into Planes */
605 *bit_ptr; /* Pointer into BitBuffer */
609 * Output whitespace as needed...
614 printf("\033*b%dY", Feed);
619 * Write bitmap data as needed...
622 bytes = (header->cupsWidth + 7) / 8;
624 for (plane = 0; plane < NumPlanes; plane ++)
631 CompressData(Planes[plane], bytes, plane < (NumPlanes - 1) ? 'V' : 'W',
632 header->cupsCompression);
637 * Separate low and high bit data into separate buffers.
640 for (count = header->cupsBytesPerLine / NumPlanes,
641 plane_ptr = Planes[plane], bit_ptr = BitBuffer;
643 count -= 2, plane_ptr += 2, bit_ptr ++)
647 bit0 = ((bit & 64) << 1) | ((bit & 16) << 2) | ((bit & 4) << 3) | ((bit & 1) << 4);
648 bit1 = (bit & 128) | ((bit & 32) << 1) | ((bit & 8) << 2) | ((bit & 2) << 3);
654 bit0 |= (bit & 1) | ((bit & 4) >> 1) | ((bit & 16) >> 2) | ((bit & 64) >> 3);
655 bit1 |= ((bit & 2) >> 1) | ((bit & 8) >> 2) | ((bit & 32) >> 3) | ((bit & 128) >> 4);
659 bit_ptr[bytes] = bit1;
663 * Send low and high bits...
666 CompressData(BitBuffer, bytes, 'V', header->cupsCompression);
667 CompressData(BitBuffer + bytes, bytes, plane < (NumPlanes - 1) ? 'V' : 'W',
668 header->cupsCompression);
676 * 'main()' - Main entry and processing of driver.
679 int /* O - Exit status */
680 main(int argc, /* I - Number of command-line arguments */
681 char *argv[]) /* I - Command-line arguments */
683 int fd; /* File descriptor */
684 cups_raster_t *ras; /* Raster stream for printing */
685 cups_page_header2_t header; /* Page header from file */
686 int y; /* Current line */
687 ppd_file_t *ppd; /* PPD file */
688 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
689 struct sigaction action; /* Actions for POSIX signals */
690 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
694 * Make sure status messages are not buffered...
697 setbuf(stderr, NULL);
700 * Check command-line...
703 if (argc < 6 || argc > 7)
706 * We don't have the correct number of arguments; write an error message
710 _cupsLangPrintFilter(stderr, "ERROR",
711 _("%s job-id user title copies options [file]"),
717 * Open the page stream...
722 if ((fd = open(argv[6], O_RDONLY)) == -1)
724 _cupsLangPrintError("ERROR", _("Unable to open raster file"));
732 ras = cupsRasterOpen(fd, CUPS_RASTER_READ);
735 * Register a signal handler to eject the current page if the
741 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
742 sigset(SIGTERM, CancelJob);
743 #elif defined(HAVE_SIGACTION)
744 memset(&action, 0, sizeof(action));
746 sigemptyset(&action.sa_mask);
747 action.sa_handler = CancelJob;
748 sigaction(SIGTERM, &action, NULL);
750 signal(SIGTERM, CancelJob);
751 #endif /* HAVE_SIGSET */
754 * Initialize the print device...
757 ppd = ppdOpenFile(getenv("PPD"));
760 ppd_status_t status; /* PPD error */
761 int linenum; /* Line number */
763 _cupsLangPrintFilter(stderr, "ERROR",
764 _("The PPD file could not be opened."));
766 status = ppdLastError(&linenum);
768 fprintf(stderr, "DEBUG: %s on line %d.\n", ppdErrorString(status), linenum);
776 * Process pages as needed...
781 while (cupsRasterReadHeader2(ras, &header))
784 * Write a status message with the page number and number of copies.
792 fprintf(stderr, "PAGE: %d %d\n", Page, header.NumCopies);
793 _cupsLangPrintFilter(stderr, "INFO", _("Starting page %d."), Page);
799 StartPage(ppd, &header);
802 * Loop for each line on the page...
805 for (y = 0; y < header.cupsHeight; y ++)
808 * Let the user know how far we have progressed...
816 _cupsLangPrintFilter(stderr, "INFO",
817 _("Printing page %d, %d%% complete."),
818 Page, 100 * y / header.cupsHeight);
819 fprintf(stderr, "ATTR: job-media-progress=%d\n",
820 100 * y / header.cupsHeight);
824 * Read a line of graphics...
827 if (cupsRasterReadPixels(ras, Planes[0], header.cupsBytesPerLine) < 1)
831 * See if the line is blank; if not, write it to the printer...
835 memcmp(Planes[0], Planes[0] + 1, header.cupsBytesPerLine - 1))
845 _cupsLangPrintFilter(stderr, "INFO", _("Finished page %d."), Page);
854 * Shutdown the printer...
863 * Close the raster stream...
866 cupsRasterClose(ras);
871 * If no pages were printed, send an error message...
876 _cupsLangPrintFilter(stderr, "ERROR", _("No pages were found."));
881 _cupsLangPrintFilter(stderr, "INFO", _("Ready to print."));
888 * End of "$Id: rastertohp.c 9384 2010-11-22 07:06:39Z mike $".