2 * "$Id: rastertoepson.c 7450 2008-04-14 19:39:02Z mike $"
4 * EPSON ESC/P and ESC/P2 filter for the Common UNIX Printing System
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 * 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.h>
36 #include <cups/i18n.h>
37 #include <cups/raster.h>
52 #define EPSON_ICOLOR 4
53 #define EPSON_IPHOTO 5
60 #define pwrite(s,n) fwrite((s), 1, (n), stdout)
67 unsigned char *Planes
[6], /* Output buffers */
68 *CompBuffer
, /* Compression buffer */
69 *LineBuffers
[2]; /* Line bitmap buffers */
70 int Model
, /* Model number */
71 NumPlanes
, /* Number of color planes */
72 Feed
, /* Number of lines to skip */
73 EjectPage
; /* Eject the page when done? */
74 int DotBit
, /* Bit in buffers */
75 DotBytes
, /* # bytes in a dot column */
76 DotColumns
, /* # columns in 1/60 inch */
77 LineCount
, /* # of lines processed */
78 EvenOffset
, /* Offset into 'even' buffers */
79 OddOffset
, /* Offset into 'odd' buffers */
80 Shingling
, /* Shingle output? */
81 Canceled
; /* Has the current job been canceled? */
89 void StartPage(const ppd_file_t
*ppd
, const cups_page_header2_t
*header
);
90 void EndPage(const cups_page_header2_t
*header
);
93 void CancelJob(int sig
);
94 void CompressData(const unsigned char *line
, int length
, int plane
,
95 int type
, int xstep
, int ystep
);
96 void OutputLine(const cups_page_header2_t
*header
);
97 void OutputRows(const cups_page_header2_t
*header
, int row
);
101 * 'Setup()' - Prepare the printer for printing.
107 const char *device_uri
; /* The device for the printer... */
111 * EPSON USB printers need an additional command issued at the
112 * beginning of each job to exit from "packet" mode...
115 if ((device_uri
= getenv("DEVICE_URI")) != NULL
&&
116 strncmp(device_uri
, "usb:", 4) == 0 && Model
>= EPSON_ICOLOR
)
117 pwrite("\000\000\000\033\001@EJL 1284.4\n@EJL \n\033@", 29);
122 * 'StartPage()' - Start a page of graphics.
126 StartPage(const ppd_file_t
*ppd
, /* I - PPD file */
127 const cups_page_header2_t
*header
) /* I - Page header */
129 int n
, t
; /* Numbers */
130 int plane
; /* Looping var */
134 * Send a reset sequence.
137 if (ppd
&& ppd
->nickname
&& strstr(ppd
->nickname
, "OKIDATA") != NULL
)
138 printf("\033{A"); /* Set EPSON emulation mode */
143 * See which type of printer we are using...
146 EjectPage
= header
->Margins
[0] || header
->Margins
[1];
152 printf("\033P\022"); /* Set 10 CPI */
154 if (header
->HWResolution
[0] == 360 || header
->HWResolution
[0] == 240)
156 printf("\033x1"); /* LQ printing */
157 printf("\033U1"); /* Unidirectional */
161 printf("\033x0"); /* Draft printing */
162 printf("\033U0"); /* Bidirectional */
165 printf("\033l%c\033Q%c", 0, /* Side margins */
166 (int)(10.0 * header
->PageSize
[0] / 72.0 + 0.5));
167 printf("\033C%c%c", 0, /* Page length */
168 (int)(header
->PageSize
[1] / 72.0 + 0.5));
169 printf("\033N%c", 0); /* Bottom margin */
170 printf("\033O"); /* No perforation skip */
173 * Setup various buffer limits...
176 DotBytes
= header
->cupsRowCount
/ 8;
177 DotColumns
= header
->HWResolution
[0] / 60;
180 if (Model
== EPSON_9PIN
)
181 printf("\033\063\030"); /* Set line feed */
183 switch (header
->HWResolution
[0])
188 printf("\033\063\030"); /* Set line feed */
195 if (header
->HWResolution
[1] == 180)
196 printf("\033\063\010");/* Set line feed */
198 printf("\033+\010"); /* Set line feed */
205 * Set graphics mode...
208 pwrite("\033(G\001\000\001", 6); /* Graphics mode */
211 * Set the media size...
214 if (Model
< EPSON_ICOLOR
)
216 pwrite("\033(U\001\000", 5); /* Resolution/units */
217 putchar(3600 / header
->HWResolution
[1]);
221 pwrite("\033(U\005\000", 5);
222 putchar(1440 / header
->HWResolution
[1]);
223 putchar(1440 / header
->HWResolution
[1]);
224 putchar(1440 / header
->HWResolution
[0]);
225 putchar(0xa0); /* n/1440ths... */
229 n
= header
->PageSize
[1] * header
->HWResolution
[1] / 72.0;
231 pwrite("\033(C\002\000", 5); /* Page length */
236 t
= (ppd
->sizes
[1].length
- ppd
->sizes
[1].top
) *
237 header
->HWResolution
[1] / 72.0;
241 pwrite("\033(c\004\000", 5); /* Top & bottom margins */
247 if (header
->HWResolution
[1] == 720)
249 pwrite("\033(i\001\000\001", 6); /* Microweave */
250 pwrite("\033(e\002\000\000\001", 7); /* Small dots */
253 pwrite("\033(V\002\000\000\000", 7); /* Set absolute position 0 */
265 if (header
->cupsColorSpace
== CUPS_CSPACE_CMY
)
267 else if (header
->cupsColorSpace
== CUPS_CSPACE_KCMY
)
269 else if (header
->cupsColorSpace
== CUPS_CSPACE_KCMYcm
)
274 Feed
= 0; /* No blank lines yet */
277 * Allocate memory for a line/row of graphics...
280 if ((Planes
[0] = malloc(header
->cupsBytesPerLine
)) == NULL
)
282 fputs("ERROR: Unable to allocate memory!\n", stderr
);
286 for (plane
= 1; plane
< NumPlanes
; plane
++)
287 Planes
[plane
] = Planes
[0] + plane
* header
->cupsBytesPerLine
/ NumPlanes
;
289 if (header
->cupsCompression
|| DotBytes
)
291 if ((CompBuffer
= calloc(2, header
->cupsWidth
)) == NULL
)
293 fputs("ERROR: Unable to allocate memory!\n", stderr
);
302 if ((LineBuffers
[0] = calloc(DotBytes
,
303 header
->cupsWidth
* (Shingling
+ 1))) == NULL
)
305 fputs("ERROR: Unable to allocate memory!\n", stderr
);
309 LineBuffers
[1] = LineBuffers
[0] + DotBytes
* header
->cupsWidth
;
319 * 'EndPage()' - Finish a page of graphics.
323 EndPage(const cups_page_header2_t
*header
) /* I - Page header */
325 if (DotBytes
&& header
)
328 * Flush remaining graphics as needed...
333 if (DotBit
< 128 || EvenOffset
)
334 OutputRows(header
, 0);
336 else if (OddOffset
> EvenOffset
)
338 OutputRows(header
, 1);
339 OutputRows(header
, 0);
343 OutputRows(header
, 0);
344 OutputRows(header
, 1);
349 * Eject the current page...
353 putchar(12); /* Form feed */
366 free(LineBuffers
[0]);
371 * 'Shutdown()' - Shutdown the printer.
378 * Send a reset sequence.
386 * 'CancelJob()' - Cancel the current job...
390 CancelJob(int sig
) /* I - Signal */
399 * 'CompressData()' - Compress a line of graphics.
403 CompressData(const unsigned char *line
, /* I - Data to compress */
404 int length
,/* I - Number of bytes */
405 int plane
, /* I - Color plane */
406 int type
, /* I - Type of compression */
407 int xstep
, /* I - X resolution */
408 int ystep
) /* I - Y resolution */
410 const unsigned char *line_ptr
, /* Current byte pointer */
411 *line_end
, /* End-of-line byte pointer */
412 *start
; /* Start of compression sequence */
413 unsigned char *comp_ptr
, /* Pointer into compression buffer */
414 temp
; /* Current byte */
415 int count
; /* Count of bytes for output */
416 static int ctable
[6] = { 0, 2, 1, 4, 18, 17 };
417 /* KCMYcm color values */
425 line_end
= line
+ length
;
428 * Do depletion for 720 DPI printing...
433 for (comp_ptr
= (unsigned char *)line
; comp_ptr
< line_end
;)
436 * Grab the current byte...
442 * Check adjacent bits...
445 if ((temp
& 0xc0) == 0xc0)
447 if ((temp
& 0x60) == 0x60)
449 if ((temp
& 0x30) == 0x30)
451 if ((temp
& 0x18) == 0x18)
453 if ((temp
& 0x0c) == 0x0c)
455 if ((temp
& 0x06) == 0x06)
457 if ((temp
& 0x03) == 0x03)
463 * Check the last bit in the current byte and the first bit in the
467 if ((temp
& 0x01) && comp_ptr
< line_end
&& *comp_ptr
& 0x80)
476 * Do no compression...
482 * Do TIFF pack-bits encoding...
485 comp_ptr
= CompBuffer
;
487 while (line_ptr
< line_end
)
489 if ((line_ptr
+ 1) >= line_end
)
492 * Single byte on the end...
496 *comp_ptr
++ = *line_ptr
++;
498 else if (line_ptr
[0] == line_ptr
[1])
501 * Repeated sequence...
507 while (line_ptr
< (line_end
- 1) &&
508 line_ptr
[0] == line_ptr
[1] &&
515 *comp_ptr
++ = 257 - count
;
516 *comp_ptr
++ = *line_ptr
++;
521 * Non-repeated sequence...
528 while (line_ptr
< (line_end
- 1) &&
529 line_ptr
[0] != line_ptr
[1] &&
536 *comp_ptr
++ = count
- 1;
538 memcpy(comp_ptr
, start
, count
);
543 line_ptr
= CompBuffer
;
548 putchar(0x0d); /* Move print head to left margin */
550 if (Model
< EPSON_ICOLOR
)
553 * Do graphics the "old" way...
563 printf("\033(r%c%c%c%c", 2, 0, 1, ctable
[plane
] & 15);
564 /* Set extended color */
565 else if (NumPlanes
== 3)
566 printf("\033r%c", ctable
[plane
+ 1]);
569 printf("\033r%c", ctable
[plane
]); /* Set color */
573 * Send a raster plane...
577 printf("\033."); /* Raster graphics */
583 putchar(length
>> 8);
588 * Do graphics the "new" way...
592 putchar(ctable
[plane
]);
595 putchar(length
& 255);
596 putchar(length
>> 8);
601 pwrite(line_ptr
, line_end
- line_ptr
);
607 * 'OutputLine()' - Output a line of graphics.
611 OutputLine(const cups_page_header2_t
*header
) /* I - Page header */
613 if (header
->cupsRowCount
)
616 unsigned char *tempptr
,
621 const unsigned char *pixel
;
626 * Collect bitmap data in the line buffers and write after each buffer.
629 for (x
= header
->cupsWidth
, bit
= 128, pixel
= Planes
[0],
651 * Copy the holding buffer to the output buffer, shingling as necessary...
654 if (Shingling
&& LineCount
!= 0)
657 * Shingle the output...
662 evenptr
= LineBuffers
[1] + OddOffset
;
663 oddptr
= LineBuffers
[0] + EvenOffset
+ DotBytes
;
667 evenptr
= LineBuffers
[0] + EvenOffset
;
668 oddptr
= LineBuffers
[1] + OddOffset
+ DotBytes
;
671 for (width
= header
->cupsWidth
, tempptr
= CompBuffer
;
673 width
-= 2, tempptr
+= 2, oddptr
+= DotBytes
* 2,
674 evenptr
+= DotBytes
* 2)
676 evenptr
[0] = tempptr
[0];
677 oddptr
[0] = tempptr
[1];
683 * Don't shingle the output...
686 for (width
= header
->cupsWidth
, tempptr
= CompBuffer
,
687 evenptr
= LineBuffers
[0] + EvenOffset
;
689 width
--, tempptr
++, evenptr
+= DotBytes
)
690 *evenptr
= tempptr
[0];
693 if (Shingling
&& LineCount
!= 0)
698 if (EvenOffset
== DotBytes
)
701 OutputRows(header
, 0);
704 if (OddOffset
== DotBytes
)
707 OutputRows(header
, 1);
714 if (EvenOffset
== DotBytes
)
717 OutputRows(header
, 0);
724 memset(CompBuffer
, 0, header
->cupsWidth
);
729 int plane
; /* Current plane */
730 int bytes
; /* Bytes per plane */
731 int xstep
, ystep
; /* X & Y resolutions */
735 * Write a single line of bitmap data as needed...
738 xstep
= 3600 / header
->HWResolution
[0];
739 ystep
= 3600 / header
->HWResolution
[1];
740 bytes
= header
->cupsBytesPerLine
/ NumPlanes
;
742 for (plane
= 0; plane
< NumPlanes
; plane
++)
748 if (!Planes
[plane
][0] &&
749 memcmp(Planes
[plane
], Planes
[plane
] + 1, bytes
- 1) == 0)
753 * Output whitespace as needed...
758 pwrite("\033(v\002\000", 5); /* Relative vertical position */
765 CompressData(Planes
[plane
], bytes
, plane
, header
->cupsCompression
, xstep
,
775 * 'OutputRows()' - Output 8, 24, or 48 rows.
779 OutputRows(const cups_page_header2_t
*header
, /* I - Page image header */
780 int row
) /* I - Row number (0 or 1) */
782 unsigned i
, n
; /* Looping vars */
783 int dot_count
, /* Number of bytes to print */
784 dot_min
; /* Minimum number of bytes */
785 unsigned char *dot_ptr
, /* Pointer to print data */
786 *ptr
; /* Current data */
789 dot_min
= DotBytes
* DotColumns
;
791 if (LineBuffers
[row
][0] != 0 ||
792 memcmp(LineBuffers
[row
], LineBuffers
[row
] + 1,
793 header
->cupsWidth
* DotBytes
- 1))
796 * Skip leading space...
800 dot_count
= header
->cupsWidth
* DotBytes
;
801 dot_ptr
= LineBuffers
[row
];
803 while (dot_count
>= dot_min
&& dot_ptr
[0] == 0 &&
804 memcmp(dot_ptr
, dot_ptr
+ 1, dot_min
- 1) == 0)
808 dot_count
-= dot_min
;
812 * Skip trailing space...
815 while (dot_count
>= dot_min
&& dot_ptr
[dot_count
- dot_min
] == 0 &&
816 memcmp(dot_ptr
+ dot_count
- dot_min
,
817 dot_ptr
+ dot_count
- dot_min
+ 1, dot_min
- 1) == 0)
818 dot_count
-= dot_min
;
821 * Position print head for printing...
835 * Start bitmap graphics for this line...
838 printf("\033*"); /* Select bit image */
839 switch (header
->HWResolution
[0])
841 case 60 : /* 60x60/72 DPI gfx */
844 case 120 : /* 120x60/72 DPI gfx */
847 case 180 : /* 180 DPI gfx */
850 case 240 : /* 240x72 DPI gfx */
853 case 360 : /* 360x180/360 DPI gfx */
854 if (header
->HWResolution
[1] == 180)
856 if (Shingling
&& LineCount
!= 0)
857 putchar(40); /* 360x180 fast */
859 putchar(41); /* 360x180 slow */
863 if (Shingling
&& LineCount
!= 0)
864 putchar(72); /* 360x360 fast */
866 putchar(73); /* 360x360 slow */
871 n
= (unsigned)dot_count
/ DotBytes
;
876 * Write the graphics data...
879 if (header
->HWResolution
[0] == 120 ||
880 header
->HWResolution
[0] == 240)
883 * Need to interleave the dots to avoid hosing the print head...
886 for (n
= dot_count
/ 2, ptr
= dot_ptr
; n
> 0; n
--, ptr
+= 2)
893 * Move the head back and print the odd bytes...
906 if (header
->HWResolution
[0] == 120)
907 printf("\033*\001"); /* Select bit image */
909 printf("\033*\003"); /* Select bit image */
911 n
= (unsigned)dot_count
/ DotBytes
;
915 for (n
= dot_count
/ 2, ptr
= dot_ptr
+ 1; n
> 0; n
--, ptr
+= 2)
922 pwrite(dot_ptr
, dot_count
);
931 if (Shingling
&& row
== 1)
933 if (header
->HWResolution
[1] == 360)
942 * Clear the buffer...
945 memset(LineBuffers
[row
], 0, header
->cupsWidth
* DotBytes
);
950 * 'main()' - Main entry and processing of driver.
953 int /* O - Exit status */
954 main(int argc
, /* I - Number of command-line arguments */
955 char *argv
[]) /* I - Command-line arguments */
957 int fd
; /* File descriptor */
958 cups_raster_t
*ras
; /* Raster stream for printing */
959 cups_page_header2_t header
; /* Page header from file */
960 ppd_file_t
*ppd
; /* PPD file */
961 int page
; /* Current page */
962 int y
; /* Current line */
963 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
964 struct sigaction action
; /* Actions for POSIX signals */
965 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
969 * Make sure status messages are not buffered...
972 setbuf(stderr
, NULL
);
975 * Check command-line...
978 if (argc
< 6 || argc
> 7)
981 * We don't have the correct number of arguments; write an error message
985 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
991 * Open the page stream...
996 if ((fd
= open(argv
[6], O_RDONLY
)) == -1)
998 perror("ERROR: Unable to open raster file - ");
1006 ras
= cupsRasterOpen(fd
, CUPS_RASTER_READ
);
1009 * Register a signal handler to eject the current page if the
1015 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
1016 sigset(SIGTERM
, CancelJob
);
1017 #elif defined(HAVE_SIGACTION)
1018 memset(&action
, 0, sizeof(action
));
1020 sigemptyset(&action
.sa_mask
);
1021 action
.sa_handler
= CancelJob
;
1022 sigaction(SIGTERM
, &action
, NULL
);
1024 signal(SIGTERM
, CancelJob
);
1025 #endif /* HAVE_SIGSET */
1028 * Initialize the print device...
1031 ppd
= ppdOpenFile(getenv("PPD"));
1033 Model
= ppd
->model_number
;
1038 * Process pages as needed...
1043 while (cupsRasterReadHeader2(ras
, &header
))
1046 * Write a status message with the page number and number of copies.
1054 fprintf(stderr
, "PAGE: %d %d\n", page
, header
.NumCopies
);
1060 StartPage(ppd
, &header
);
1063 * Loop for each line on the page...
1066 for (y
= 0; y
< header
.cupsHeight
; y
++)
1069 * Let the user know how far we have progressed...
1076 fprintf(stderr
, _("INFO: Printing page %d, %d%% complete...\n"), page
,
1077 100 * y
/ header
.cupsHeight
);
1080 * Read a line of graphics...
1083 if (cupsRasterReadPixels(ras
, Planes
[0], header
.cupsBytesPerLine
) < 1)
1087 * Write it to the printer...
1090 OutputLine(&header
);
1104 * Shutdown the printer...
1112 * Close the raster stream...
1115 cupsRasterClose(ras
);
1120 * If no pages were printed, send an error message...
1124 fputs(_("ERROR: No pages found!\n"), stderr
);
1126 fputs(_("INFO: Ready to print.\n"), stderr
);
1133 * End of "$Id: rastertoepson.c 7450 2008-04-14 19:39:02Z mike $".