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-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? */
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
&& 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];
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 (Model
== 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 */
255 t
= (ppd
->sizes
[1].length
- ppd
->sizes
[1].top
) *
256 header
->HWResolution
[1] / 72.0;
260 pwrite("\033(c\004\000", 5); /* Top & bottom margins */
266 if (header
->HWResolution
[1] == 720)
268 pwrite("\033(i\001\000\001", 6); /* Microweave */
269 pwrite("\033(e\002\000\000\001", 7); /* Small dots */
272 pwrite("\033(V\002\000\000\000", 7); /* Set absolute position 0 */
284 if (header
->cupsColorSpace
== CUPS_CSPACE_CMY
)
286 else if (header
->cupsColorSpace
== CUPS_CSPACE_KCMY
)
288 else if (header
->cupsColorSpace
== CUPS_CSPACE_KCMYcm
)
293 Feed
= 0; /* No blank lines yet */
296 * Allocate memory for a line/row of graphics...
299 if ((Planes
[0] = malloc(header
->cupsBytesPerLine
)) == NULL
)
301 fputs("ERROR: Unable to allocate memory!\n", stderr
);
305 for (plane
= 1; plane
< NumPlanes
; plane
++)
306 Planes
[plane
] = Planes
[0] + plane
* header
->cupsBytesPerLine
/ NumPlanes
;
308 if (header
->cupsCompression
|| DotBytes
)
310 if ((CompBuffer
= calloc(2, header
->cupsWidth
)) == NULL
)
312 fputs("ERROR: Unable to allocate memory!\n", stderr
);
321 if ((LineBuffers
[0] = calloc(DotBytes
,
322 header
->cupsWidth
* (Shingling
+ 1))) == NULL
)
324 fputs("ERROR: Unable to allocate memory!\n", stderr
);
328 LineBuffers
[1] = LineBuffers
[0] + DotBytes
* header
->cupsWidth
;
338 * 'EndPage()' - Finish a page of graphics.
342 EndPage(const cups_page_header_t
*header
) /* I - Page header */
344 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
345 struct sigaction action
; /* Actions for POSIX signals */
346 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
349 if (DotBytes
&& header
)
352 * Flush remaining graphics as needed...
357 if (DotBit
< 128 || EvenOffset
)
358 OutputRows(header
, 0);
360 else if (OddOffset
> EvenOffset
)
362 OutputRows(header
, 1);
363 OutputRows(header
, 0);
367 OutputRows(header
, 0);
368 OutputRows(header
, 1);
373 * Eject the current page...
377 putchar(12); /* Form feed */
381 * Unregister the signal handler...
384 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
385 sigset(SIGTERM
, SIG_IGN
);
386 #elif defined(HAVE_SIGACTION)
387 memset(&action
, 0, sizeof(action
));
389 sigemptyset(&action
.sa_mask
);
390 action
.sa_handler
= SIG_IGN
;
391 sigaction(SIGTERM
, &action
, NULL
);
393 signal(SIGTERM
, SIG_IGN
);
394 #endif /* HAVE_SIGSET */
406 free(LineBuffers
[0]);
411 * 'Shutdown()' - Shutdown the printer.
418 * Send a reset sequence.
426 * 'CancelJob()' - Cancel the current job...
430 CancelJob(int sig
) /* I - Signal */
432 int i
; /* Looping var */
438 * Send out lots of NUL bytes to clear out any pending raster data...
442 i
= DotBytes
* 360 * 8;
450 * End the current page and exit...
461 * 'CompressData()' - Compress a line of graphics.
465 CompressData(const 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 */
469 int xstep
, /* I - X resolution */
470 int ystep
) /* I - Y resolution */
472 const unsigned char *line_ptr
, /* Current byte pointer */
473 *line_end
, /* End-of-line byte pointer */
474 *start
; /* Start of compression sequence */
475 unsigned char *comp_ptr
, /* Pointer into compression buffer */
476 temp
; /* Current byte */
477 int count
; /* Count of bytes for output */
478 static int ctable
[6] = { 0, 2, 1, 4, 18, 17 };
479 /* KCMYcm color values */
487 line_end
= line
+ length
;
490 * Do depletion for 720 DPI printing...
495 for (comp_ptr
= (unsigned char *)line
; comp_ptr
< line_end
;)
498 * Grab the current byte...
504 * Check adjacent bits...
507 if ((temp
& 0xc0) == 0xc0)
509 if ((temp
& 0x60) == 0x60)
511 if ((temp
& 0x30) == 0x30)
513 if ((temp
& 0x18) == 0x18)
515 if ((temp
& 0x0c) == 0x0c)
517 if ((temp
& 0x06) == 0x06)
519 if ((temp
& 0x03) == 0x03)
525 * Check the last bit in the current byte and the first bit in the
529 if ((temp
& 0x01) && comp_ptr
< line_end
&& *comp_ptr
& 0x80)
538 * Do no compression...
544 * Do TIFF pack-bits encoding...
547 comp_ptr
= CompBuffer
;
549 while (line_ptr
< line_end
)
551 if ((line_ptr
+ 1) >= line_end
)
554 * Single byte on the end...
558 *comp_ptr
++ = *line_ptr
++;
560 else if (line_ptr
[0] == line_ptr
[1])
563 * Repeated sequence...
569 while (line_ptr
< (line_end
- 1) &&
570 line_ptr
[0] == line_ptr
[1] &&
577 *comp_ptr
++ = 257 - count
;
578 *comp_ptr
++ = *line_ptr
++;
583 * Non-repeated sequence...
590 while (line_ptr
< (line_end
- 1) &&
591 line_ptr
[0] != line_ptr
[1] &&
598 *comp_ptr
++ = count
- 1;
600 memcpy(comp_ptr
, start
, count
);
605 line_ptr
= CompBuffer
;
610 putchar(0x0d); /* Move print head to left margin */
612 if (Model
< EPSON_ICOLOR
)
615 * Do graphics the "old" way...
625 printf("\033(r%c%c%c%c", 2, 0, 1, ctable
[plane
] & 15);
626 /* Set extended color */
627 else if (NumPlanes
== 3)
628 printf("\033r%c", ctable
[plane
+ 1]);
631 printf("\033r%c", ctable
[plane
]); /* Set color */
635 * Send a raster plane...
639 printf("\033."); /* Raster graphics */
645 putchar(length
>> 8);
650 * Do graphics the "new" way...
654 putchar(ctable
[plane
]);
657 putchar(length
& 255);
658 putchar(length
>> 8);
663 pwrite(line_ptr
, line_end
- line_ptr
);
669 * 'OutputLine()' - Output a line of graphics.
673 OutputLine(const cups_page_header_t
*header
) /* I - Page header */
675 if (header
->cupsRowCount
)
678 unsigned char *tempptr
,
683 const unsigned char *pixel
;
688 * Collect bitmap data in the line buffers and write after each buffer.
691 for (x
= header
->cupsWidth
, bit
= 128, pixel
= Planes
[0],
713 * Copy the holding buffer to the output buffer, shingling as necessary...
716 if (Shingling
&& LineCount
!= 0)
719 * Shingle the output...
724 evenptr
= LineBuffers
[1] + OddOffset
;
725 oddptr
= LineBuffers
[0] + EvenOffset
+ DotBytes
;
729 evenptr
= LineBuffers
[0] + EvenOffset
;
730 oddptr
= LineBuffers
[1] + OddOffset
+ DotBytes
;
733 for (width
= header
->cupsWidth
, tempptr
= CompBuffer
;
735 width
-= 2, tempptr
+= 2, oddptr
+= DotBytes
* 2,
736 evenptr
+= DotBytes
* 2)
738 evenptr
[0] = tempptr
[0];
739 oddptr
[0] = tempptr
[1];
745 * Don't shingle the output...
748 for (width
= header
->cupsWidth
, tempptr
= CompBuffer
,
749 evenptr
= LineBuffers
[0] + EvenOffset
;
751 width
--, tempptr
++, evenptr
+= DotBytes
)
752 *evenptr
= tempptr
[0];
755 if (Shingling
&& LineCount
!= 0)
760 if (EvenOffset
== DotBytes
)
763 OutputRows(header
, 0);
766 if (OddOffset
== DotBytes
)
769 OutputRows(header
, 1);
776 if (EvenOffset
== DotBytes
)
779 OutputRows(header
, 0);
786 memset(CompBuffer
, 0, header
->cupsWidth
);
791 int plane
; /* Current plane */
792 int bytes
; /* Bytes per plane */
793 int xstep
, ystep
; /* X & Y resolutions */
797 * Write a single line of bitmap data as needed...
800 xstep
= 3600 / header
->HWResolution
[0];
801 ystep
= 3600 / header
->HWResolution
[1];
802 bytes
= header
->cupsBytesPerLine
/ NumPlanes
;
804 for (plane
= 0; plane
< NumPlanes
; plane
++)
810 if (!Planes
[plane
][0] &&
811 memcmp(Planes
[plane
], Planes
[plane
] + 1, bytes
- 1) == 0)
815 * Output whitespace as needed...
820 pwrite("\033(v\002\000", 5); /* Relative vertical position */
827 CompressData(Planes
[plane
], bytes
, plane
, header
->cupsCompression
, xstep
,
837 * 'OutputRows()' - Output 8, 24, or 48 rows.
841 OutputRows(const cups_page_header_t
*header
, /* I - Page image header */
842 int row
) /* I - Row number (0 or 1) */
844 unsigned i
, n
; /* Looping vars */
845 int dot_count
, /* Number of bytes to print */
846 dot_min
; /* Minimum number of bytes */
847 unsigned char *dot_ptr
, /* Pointer to print data */
848 *ptr
; /* Current data */
851 dot_min
= DotBytes
* DotColumns
;
853 if (LineBuffers
[row
][0] != 0 ||
854 memcmp(LineBuffers
[row
], LineBuffers
[row
] + 1,
855 header
->cupsWidth
* DotBytes
- 1))
858 * Skip leading space...
862 dot_count
= header
->cupsWidth
* DotBytes
;
863 dot_ptr
= LineBuffers
[row
];
865 while (dot_count
>= dot_min
&& dot_ptr
[0] == 0 &&
866 memcmp(dot_ptr
, dot_ptr
+ 1, dot_min
- 1) == 0)
870 dot_count
-= dot_min
;
874 * Skip trailing space...
877 while (dot_count
>= dot_min
&& dot_ptr
[dot_count
- dot_min
] == 0 &&
878 memcmp(dot_ptr
+ dot_count
- dot_min
,
879 dot_ptr
+ dot_count
- dot_min
+ 1, dot_min
- 1) == 0)
880 dot_count
-= dot_min
;
883 * Position print head for printing...
897 * Start bitmap graphics for this line...
900 printf("\033*"); /* Select bit image */
901 switch (header
->HWResolution
[0])
903 case 60 : /* 60x60/72 DPI gfx */
906 case 120 : /* 120x60/72 DPI gfx */
909 case 180 : /* 180 DPI gfx */
912 case 240 : /* 240x72 DPI gfx */
915 case 360 : /* 360x180/360 DPI gfx */
916 if (header
->HWResolution
[1] == 180)
918 if (Shingling
&& LineCount
!= 0)
919 putchar(40); /* 360x180 fast */
921 putchar(41); /* 360x180 slow */
925 if (Shingling
&& LineCount
!= 0)
926 putchar(72); /* 360x360 fast */
928 putchar(73); /* 360x360 slow */
933 n
= (unsigned)dot_count
/ DotBytes
;
938 * Write the graphics data...
941 if (header
->HWResolution
[0] == 120 ||
942 header
->HWResolution
[0] == 240)
945 * Need to interleave the dots to avoid hosing the print head...
948 for (n
= dot_count
/ 2, ptr
= dot_ptr
; n
> 0; n
--, ptr
+= 2)
955 * Move the head back and print the odd bytes...
968 if (header
->HWResolution
[0] == 120)
969 printf("\033*\001"); /* Select bit image */
971 printf("\033*\003"); /* Select bit image */
973 n
= (unsigned)dot_count
/ DotBytes
;
977 for (n
= dot_count
/ 2, ptr
= dot_ptr
+ 1; n
> 0; n
--, ptr
+= 2)
984 pwrite(dot_ptr
, dot_count
);
993 if (Shingling
&& row
== 1)
995 if (header
->HWResolution
[1] == 360)
1004 * Clear the buffer...
1007 memset(LineBuffers
[row
], 0, header
->cupsWidth
* DotBytes
);
1012 * 'main()' - Main entry and processing of driver.
1015 int /* O - Exit status */
1016 main(int argc
, /* I - Number of command-line arguments */
1017 char *argv
[]) /* I - Command-line arguments */
1019 int fd
; /* File descriptor */
1020 cups_raster_t
*ras
; /* Raster stream for printing */
1021 cups_page_header_t header
; /* Page header from file */
1022 ppd_file_t
*ppd
; /* PPD file */
1023 int page
; /* Current page */
1024 int y
; /* Current line */
1028 * Make sure status messages are not buffered...
1031 setbuf(stderr
, NULL
);
1034 * Check command-line...
1037 if (argc
< 6 || argc
> 7)
1040 * We don't have the correct number of arguments; write an error message
1044 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
1050 * Open the page stream...
1055 if ((fd
= open(argv
[6], O_RDONLY
)) == -1)
1057 perror("ERROR: Unable to open raster file - ");
1065 ras
= cupsRasterOpen(fd
, CUPS_RASTER_READ
);
1068 * Initialize the print device...
1071 ppd
= ppdOpenFile(getenv("PPD"));
1073 Model
= ppd
->model_number
;
1078 * Process pages as needed...
1083 while (cupsRasterReadHeader(ras
, &header
))
1086 * Write a status message with the page number and number of copies.
1091 fprintf(stderr
, "PAGE: %d %d\n", page
, header
.NumCopies
);
1097 StartPage(ppd
, &header
);
1100 * Loop for each line on the page...
1103 for (y
= 0; y
< header
.cupsHeight
; y
++)
1106 * Let the user know how far we have progressed...
1110 fprintf(stderr
, _("INFO: Printing page %d, %d%% complete...\n"), page
,
1111 100 * y
/ header
.cupsHeight
);
1114 * Read a line of graphics...
1117 if (cupsRasterReadPixels(ras
, Planes
[0], header
.cupsBytesPerLine
) < 1)
1121 * Write it to the printer...
1124 OutputLine(&header
);
1135 * Shutdown the printer...
1143 * Close the raster stream...
1146 cupsRasterClose(ras
);
1151 * If no pages were printed, send an error message...
1155 fputs(_("ERROR: No pages found!\n"), stderr
);
1157 fputs(_("INFO: Ready to print.\n"), stderr
);
1164 * End of "$Id: rastertoepson.c 6649 2007-07-11 21:46:42Z mike $".