2 * "$Id: rastertoepson.c 6649 2007-07-11 21:46:42Z mike $"
4 * EPSON ESC/P and ESC/P2 filter for the Common UNIX Printing System
7 * Copyright 2007 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>
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? */
88 void StartPage(const ppd_file_t
*ppd
, const cups_page_header_t
*header
);
89 void EndPage(const cups_page_header_t
*header
);
92 void CancelJob(int sig
);
93 void CompressData(const unsigned char *line
, int length
, int plane
,
94 int type
, int xstep
, int ystep
);
95 void OutputLine(const cups_page_header_t
*header
);
96 void OutputRows(const cups_page_header_t
*header
, int row
);
100 * 'Setup()' - Prepare the printer for printing.
106 const char *device_uri
; /* The device for the printer... */
110 * EPSON USB printers need an additional command issued at the
111 * beginning of each job to exit from "packet" mode...
114 if ((device_uri
= getenv("DEVICE_URI")) != NULL
&&
115 strncmp(device_uri
, "usb:", 4) == 0 && Model
>= EPSON_ICOLOR
)
116 pwrite("\000\000\000\033\001@EJL 1284.4\n@EJL \n\033@", 29);
121 * 'StartPage()' - Start a page of graphics.
125 StartPage(const ppd_file_t
*ppd
, /* I - PPD file */
126 const cups_page_header_t
*header
) /* I - Page header */
128 int n
, t
; /* Numbers */
129 int plane
; /* Looping var */
130 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
131 struct sigaction action
; /* Actions for POSIX signals */
132 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
136 * Register a signal handler to eject the current page if the
140 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
141 sigset(SIGTERM
, CancelJob
);
142 #elif defined(HAVE_SIGACTION)
143 memset(&action
, 0, sizeof(action
));
145 sigemptyset(&action
.sa_mask
);
146 action
.sa_handler
= CancelJob
;
147 sigaction(SIGTERM
, &action
, NULL
);
149 signal(SIGTERM
, CancelJob
);
150 #endif /* HAVE_SIGSET */
153 * Send a reset sequence.
156 if (ppd
->nickname
&& strstr(ppd
->nickname
, "OKIDATA") != NULL
)
157 printf("\033{A"); /* Set EPSON emulation mode */
162 * See which type of printer we are using...
165 EjectPage
= header
->Margins
[0] || header
->Margins
[1];
167 switch (ppd
->model_number
)
171 printf("\033P\022"); /* Set 10 CPI */
173 if (header
->HWResolution
[0] == 360 || header
->HWResolution
[0] == 240)
175 printf("\033x1"); /* LQ printing */
176 printf("\033U1"); /* Unidirectional */
180 printf("\033x0"); /* Draft printing */
181 printf("\033U0"); /* Bidirectional */
184 printf("\033l%c\033Q%c", 0, /* Side margins */
185 (int)(10.0 * header
->PageSize
[0] / 72.0 + 0.5));
186 printf("\033C%c%c", 0, /* Page length */
187 (int)(header
->PageSize
[1] / 72.0 + 0.5));
188 printf("\033N%c", 0); /* Bottom margin */
189 printf("\033O"); /* No perforation skip */
192 * Setup various buffer limits...
195 DotBytes
= header
->cupsRowCount
/ 8;
196 DotColumns
= header
->HWResolution
[0] / 60;
199 if (ppd
->model_number
== EPSON_9PIN
)
200 printf("\033\063\030"); /* Set line feed */
202 switch (header
->HWResolution
[0])
207 printf("\033\063\030"); /* Set line feed */
214 if (header
->HWResolution
[1] == 180)
215 printf("\033\063\010");/* Set line feed */
217 printf("\033+\010"); /* Set line feed */
224 * Set graphics mode...
227 pwrite("\033(G\001\000\001", 6); /* Graphics mode */
230 * Set the media size...
233 if (Model
< EPSON_ICOLOR
)
235 pwrite("\033(U\001\000", 5); /* Resolution/units */
236 putchar(3600 / header
->HWResolution
[1]);
240 pwrite("\033(U\005\000", 5);
241 putchar(1440 / header
->HWResolution
[1]);
242 putchar(1440 / header
->HWResolution
[1]);
243 putchar(1440 / header
->HWResolution
[0]);
244 putchar(0xa0); /* n/1440ths... */
248 n
= header
->PageSize
[1] * header
->HWResolution
[1] / 72.0;
250 pwrite("\033(C\002\000", 5); /* Page length */
254 t
= (ppd
->sizes
[1].length
- ppd
->sizes
[1].top
) *
255 header
->HWResolution
[1] / 72.0;
257 pwrite("\033(c\004\000", 5); /* Top & bottom margins */
263 if (header
->HWResolution
[1] == 720)
265 pwrite("\033(i\001\000\001", 6); /* Microweave */
266 pwrite("\033(e\002\000\000\001", 7); /* Small dots */
269 pwrite("\033(V\002\000\000\000", 7); /* Set absolute position 0 */
281 if (header
->cupsColorSpace
== CUPS_CSPACE_CMY
)
283 else if (header
->cupsColorSpace
== CUPS_CSPACE_KCMY
)
285 else if (header
->cupsColorSpace
== CUPS_CSPACE_KCMYcm
)
290 Feed
= 0; /* No blank lines yet */
293 * Allocate memory for a line/row of graphics...
296 Planes
[0] = malloc(header
->cupsBytesPerLine
);
297 for (plane
= 1; plane
< NumPlanes
; plane
++)
298 Planes
[plane
] = Planes
[0] + plane
* header
->cupsBytesPerLine
/ NumPlanes
;
300 if (header
->cupsCompression
|| DotBytes
)
301 CompBuffer
= calloc(2, header
->cupsWidth
);
307 LineBuffers
[0] = calloc(DotBytes
, header
->cupsWidth
* (Shingling
+ 1));
308 LineBuffers
[1] = LineBuffers
[0] + DotBytes
* header
->cupsWidth
;
318 * 'EndPage()' - Finish a page of graphics.
322 EndPage(const cups_page_header_t
*header
) /* I - Page header */
324 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
325 struct sigaction action
; /* Actions for POSIX signals */
326 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
329 if (DotBytes
&& header
)
332 * Flush remaining graphics as needed...
337 if (DotBit
< 128 || EvenOffset
)
338 OutputRows(header
, 0);
340 else if (OddOffset
> EvenOffset
)
342 OutputRows(header
, 1);
343 OutputRows(header
, 0);
347 OutputRows(header
, 0);
348 OutputRows(header
, 1);
353 * Eject the current page...
357 putchar(12); /* Form feed */
361 * Unregister the signal handler...
364 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
365 sigset(SIGTERM
, SIG_IGN
);
366 #elif defined(HAVE_SIGACTION)
367 memset(&action
, 0, sizeof(action
));
369 sigemptyset(&action
.sa_mask
);
370 action
.sa_handler
= SIG_IGN
;
371 sigaction(SIGTERM
, &action
, NULL
);
373 signal(SIGTERM
, SIG_IGN
);
374 #endif /* HAVE_SIGSET */
386 free(LineBuffers
[0]);
391 * 'Shutdown()' - Shutdown the printer.
398 * Send a reset sequence.
406 * 'CancelJob()' - Cancel the current job...
410 CancelJob(int sig
) /* I - Signal */
412 int i
; /* Looping var */
418 * Send out lots of NUL bytes to clear out any pending raster data...
422 i
= DotBytes
* 360 * 8;
430 * End the current page and exit...
441 * 'CompressData()' - Compress a line of graphics.
445 CompressData(const unsigned char *line
, /* I - Data to compress */
446 int length
,/* I - Number of bytes */
447 int plane
, /* I - Color plane */
448 int type
, /* I - Type of compression */
449 int xstep
, /* I - X resolution */
450 int ystep
) /* I - Y resolution */
452 const unsigned char *line_ptr
, /* Current byte pointer */
453 *line_end
, /* End-of-line byte pointer */
454 *start
; /* Start of compression sequence */
455 unsigned char *comp_ptr
, /* Pointer into compression buffer */
456 temp
; /* Current byte */
457 int count
; /* Count of bytes for output */
458 static int ctable
[6] = { 0, 2, 1, 4, 18, 17 };
459 /* KCMYcm color values */
467 line_end
= line
+ length
;
470 * Do depletion for 720 DPI printing...
475 for (comp_ptr
= (unsigned char *)line
; comp_ptr
< line_end
;)
478 * Grab the current byte...
484 * Check adjacent bits...
487 if ((temp
& 0xc0) == 0xc0)
489 if ((temp
& 0x60) == 0x60)
491 if ((temp
& 0x30) == 0x30)
493 if ((temp
& 0x18) == 0x18)
495 if ((temp
& 0x0c) == 0x0c)
497 if ((temp
& 0x06) == 0x06)
499 if ((temp
& 0x03) == 0x03)
505 * Check the last bit in the current byte and the first bit in the
509 if ((temp
& 0x01) && comp_ptr
< line_end
&& *comp_ptr
& 0x80)
518 * Do no compression...
524 * Do TIFF pack-bits encoding...
527 comp_ptr
= CompBuffer
;
529 while (line_ptr
< line_end
)
531 if ((line_ptr
+ 1) >= line_end
)
534 * Single byte on the end...
538 *comp_ptr
++ = *line_ptr
++;
540 else if (line_ptr
[0] == line_ptr
[1])
543 * Repeated sequence...
549 while (line_ptr
< (line_end
- 1) &&
550 line_ptr
[0] == line_ptr
[1] &&
557 *comp_ptr
++ = 257 - count
;
558 *comp_ptr
++ = *line_ptr
++;
563 * Non-repeated sequence...
570 while (line_ptr
< (line_end
- 1) &&
571 line_ptr
[0] != line_ptr
[1] &&
578 *comp_ptr
++ = count
- 1;
580 memcpy(comp_ptr
, start
, count
);
585 line_ptr
= CompBuffer
;
590 putchar(0x0d); /* Move print head to left margin */
592 if (Model
< EPSON_ICOLOR
)
595 * Do graphics the "old" way...
605 printf("\033(r%c%c%c%c", 2, 0, 1, ctable
[plane
] & 15);
606 /* Set extended color */
607 else if (NumPlanes
== 3)
608 printf("\033r%c", ctable
[plane
+ 1]);
611 printf("\033r%c", ctable
[plane
]); /* Set color */
615 * Send a raster plane...
619 printf("\033."); /* Raster graphics */
625 putchar(length
>> 8);
630 * Do graphics the "new" way...
634 putchar(ctable
[plane
]);
637 putchar(length
& 255);
638 putchar(length
>> 8);
643 pwrite(line_ptr
, line_end
- line_ptr
);
649 * 'OutputLine()' - Output a line of graphics.
653 OutputLine(const cups_page_header_t
*header
) /* I - Page header */
655 if (header
->cupsRowCount
)
658 unsigned char *tempptr
,
663 const unsigned char *pixel
;
668 * Collect bitmap data in the line buffers and write after each buffer.
671 for (x
= header
->cupsWidth
, bit
= 128, pixel
= Planes
[0],
693 * Copy the holding buffer to the output buffer, shingling as necessary...
696 if (Shingling
&& LineCount
!= 0)
699 * Shingle the output...
704 evenptr
= LineBuffers
[1] + OddOffset
;
705 oddptr
= LineBuffers
[0] + EvenOffset
+ DotBytes
;
709 evenptr
= LineBuffers
[0] + EvenOffset
;
710 oddptr
= LineBuffers
[1] + OddOffset
+ DotBytes
;
713 for (width
= header
->cupsWidth
, tempptr
= CompBuffer
;
715 width
-= 2, tempptr
+= 2, oddptr
+= DotBytes
* 2,
716 evenptr
+= DotBytes
* 2)
718 evenptr
[0] = tempptr
[0];
719 oddptr
[0] = tempptr
[1];
725 * Don't shingle the output...
728 for (width
= header
->cupsWidth
, tempptr
= CompBuffer
,
729 evenptr
= LineBuffers
[0] + EvenOffset
;
731 width
--, tempptr
++, evenptr
+= DotBytes
)
732 *evenptr
= tempptr
[0];
735 if (Shingling
&& LineCount
!= 0)
740 if (EvenOffset
== DotBytes
)
743 OutputRows(header
, 0);
746 if (OddOffset
== DotBytes
)
749 OutputRows(header
, 1);
756 if (EvenOffset
== DotBytes
)
759 OutputRows(header
, 0);
766 memset(CompBuffer
, 0, header
->cupsWidth
);
771 int plane
; /* Current plane */
772 int bytes
; /* Bytes per plane */
773 int xstep
, ystep
; /* X & Y resolutions */
777 * Write a single line of bitmap data as needed...
780 xstep
= 3600 / header
->HWResolution
[0];
781 ystep
= 3600 / header
->HWResolution
[1];
782 bytes
= header
->cupsBytesPerLine
/ NumPlanes
;
784 for (plane
= 0; plane
< NumPlanes
; plane
++)
790 if (!Planes
[plane
][0] &&
791 memcmp(Planes
[plane
], Planes
[plane
] + 1, bytes
- 1) == 0)
795 * Output whitespace as needed...
800 pwrite("\033(v\002\000", 5); /* Relative vertical position */
807 CompressData(Planes
[plane
], bytes
, plane
, header
->cupsCompression
, xstep
,
817 * 'OutputRows()' - Output 8, 24, or 48 rows.
821 OutputRows(const cups_page_header_t
*header
, /* I - Page image header */
822 int row
) /* I - Row number (0 or 1) */
824 unsigned i
, n
; /* Looping vars */
825 int dot_count
, /* Number of bytes to print */
826 dot_min
; /* Minimum number of bytes */
827 unsigned char *dot_ptr
, /* Pointer to print data */
828 *ptr
; /* Current data */
831 dot_min
= DotBytes
* DotColumns
;
833 if (LineBuffers
[row
][0] != 0 ||
834 memcmp(LineBuffers
[row
], LineBuffers
[row
] + 1,
835 header
->cupsWidth
* DotBytes
- 1))
838 * Skip leading space...
842 dot_count
= header
->cupsWidth
* DotBytes
;
843 dot_ptr
= LineBuffers
[row
];
845 while (dot_count
>= dot_min
&& dot_ptr
[0] == 0 &&
846 memcmp(dot_ptr
, dot_ptr
+ 1, dot_min
- 1) == 0)
850 dot_count
-= dot_min
;
854 * Skip trailing space...
857 while (dot_count
>= dot_min
&& dot_ptr
[dot_count
- dot_min
] == 0 &&
858 memcmp(dot_ptr
+ dot_count
- dot_min
,
859 dot_ptr
+ dot_count
- dot_min
+ 1, dot_min
- 1) == 0)
860 dot_count
-= dot_min
;
863 * Position print head for printing...
877 * Start bitmap graphics for this line...
880 printf("\033*"); /* Select bit image */
881 switch (header
->HWResolution
[0])
883 case 60 : /* 60x60/72 DPI gfx */
886 case 120 : /* 120x60/72 DPI gfx */
889 case 180 : /* 180 DPI gfx */
892 case 240 : /* 240x72 DPI gfx */
895 case 360 : /* 360x180/360 DPI gfx */
896 if (header
->HWResolution
[1] == 180)
898 if (Shingling
&& LineCount
!= 0)
899 putchar(40); /* 360x180 fast */
901 putchar(41); /* 360x180 slow */
905 if (Shingling
&& LineCount
!= 0)
906 putchar(72); /* 360x360 fast */
908 putchar(73); /* 360x360 slow */
913 n
= (unsigned)dot_count
/ DotBytes
;
918 * Write the graphics data...
921 if (header
->HWResolution
[0] == 120 ||
922 header
->HWResolution
[0] == 240)
925 * Need to interleave the dots to avoid hosing the print head...
928 for (n
= dot_count
/ 2, ptr
= dot_ptr
; n
> 0; n
--, ptr
+= 2)
935 * Move the head back and print the odd bytes...
948 if (header
->HWResolution
[0] == 120)
949 printf("\033*\001"); /* Select bit image */
951 printf("\033*\003"); /* Select bit image */
953 n
= (unsigned)dot_count
/ DotBytes
;
957 for (n
= dot_count
/ 2, ptr
= dot_ptr
+ 1; n
> 0; n
--, ptr
+= 2)
964 pwrite(dot_ptr
, dot_count
);
973 if (Shingling
&& row
== 1)
975 if (header
->HWResolution
[1] == 360)
984 * Clear the buffer...
987 memset(LineBuffers
[row
], 0, header
->cupsWidth
* DotBytes
);
992 * 'main()' - Main entry and processing of driver.
995 int /* O - Exit status */
996 main(int argc
, /* I - Number of command-line arguments */
997 char *argv
[]) /* I - Command-line arguments */
999 int fd
; /* File descriptor */
1000 cups_raster_t
*ras
; /* Raster stream for printing */
1001 cups_page_header_t header
; /* Page header from file */
1002 ppd_file_t
*ppd
; /* PPD file */
1003 int page
; /* Current page */
1004 int y
; /* Current line */
1008 * Make sure status messages are not buffered...
1011 setbuf(stderr
, NULL
);
1014 * Check command-line...
1017 if (argc
< 6 || argc
> 7)
1020 * We don't have the correct number of arguments; write an error message
1024 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
1030 * Open the page stream...
1035 if ((fd
= open(argv
[6], O_RDONLY
)) == -1)
1037 perror("ERROR: Unable to open raster file - ");
1045 ras
= cupsRasterOpen(fd
, CUPS_RASTER_READ
);
1048 * Initialize the print device...
1051 ppd
= ppdOpenFile(getenv("PPD"));
1053 Model
= ppd
->model_number
;
1058 * Process pages as needed...
1063 while (cupsRasterReadHeader(ras
, &header
))
1066 * Write a status message with the page number and number of copies.
1071 fprintf(stderr
, "PAGE: %d %d\n", page
, header
.NumCopies
);
1077 StartPage(ppd
, &header
);
1080 * Loop for each line on the page...
1083 for (y
= 0; y
< header
.cupsHeight
; y
++)
1086 * Let the user know how far we have progressed...
1090 fprintf(stderr
, _("INFO: Printing page %d, %d%% complete...\n"), page
,
1091 100 * y
/ header
.cupsHeight
);
1094 * Read a line of graphics...
1097 if (cupsRasterReadPixels(ras
, Planes
[0], header
.cupsBytesPerLine
) < 1)
1101 * Write it to the printer...
1104 OutputLine(&header
);
1115 * Shutdown the printer...
1123 * Close the raster stream...
1126 cupsRasterClose(ras
);
1131 * If no pages were printed, send an error message...
1135 fputs(_("ERROR: No pages found!\n"), stderr
);
1137 fputs(_("INFO: Ready to print.\n"), stderr
);
1144 * End of "$Id: rastertoepson.c 6649 2007-07-11 21:46:42Z mike $".