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? */
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 defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
326 struct sigaction action
; /* Actions for POSIX signals */
327 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
330 if (DotBytes
&& header
)
333 * Flush remaining graphics as needed...
338 if (DotBit
< 128 || EvenOffset
)
339 OutputRows(header
, 0);
341 else if (OddOffset
> EvenOffset
)
343 OutputRows(header
, 1);
344 OutputRows(header
, 0);
348 OutputRows(header
, 0);
349 OutputRows(header
, 1);
354 * Eject the current page...
358 putchar(12); /* Form feed */
371 free(LineBuffers
[0]);
376 * 'Shutdown()' - Shutdown the printer.
383 * Send a reset sequence.
391 * 'CancelJob()' - Cancel the current job...
395 CancelJob(int sig
) /* I - Signal */
404 * 'CompressData()' - Compress a line of graphics.
408 CompressData(const unsigned char *line
, /* I - Data to compress */
409 int length
,/* I - Number of bytes */
410 int plane
, /* I - Color plane */
411 int type
, /* I - Type of compression */
412 int xstep
, /* I - X resolution */
413 int ystep
) /* I - Y resolution */
415 const unsigned char *line_ptr
, /* Current byte pointer */
416 *line_end
, /* End-of-line byte pointer */
417 *start
; /* Start of compression sequence */
418 unsigned char *comp_ptr
, /* Pointer into compression buffer */
419 temp
; /* Current byte */
420 int count
; /* Count of bytes for output */
421 static int ctable
[6] = { 0, 2, 1, 4, 18, 17 };
422 /* KCMYcm color values */
430 line_end
= line
+ length
;
433 * Do depletion for 720 DPI printing...
438 for (comp_ptr
= (unsigned char *)line
; comp_ptr
< line_end
;)
441 * Grab the current byte...
447 * Check adjacent bits...
450 if ((temp
& 0xc0) == 0xc0)
452 if ((temp
& 0x60) == 0x60)
454 if ((temp
& 0x30) == 0x30)
456 if ((temp
& 0x18) == 0x18)
458 if ((temp
& 0x0c) == 0x0c)
460 if ((temp
& 0x06) == 0x06)
462 if ((temp
& 0x03) == 0x03)
468 * Check the last bit in the current byte and the first bit in the
472 if ((temp
& 0x01) && comp_ptr
< line_end
&& *comp_ptr
& 0x80)
481 * Do no compression...
487 * Do TIFF pack-bits encoding...
490 comp_ptr
= CompBuffer
;
492 while (line_ptr
< line_end
)
494 if ((line_ptr
+ 1) >= line_end
)
497 * Single byte on the end...
501 *comp_ptr
++ = *line_ptr
++;
503 else if (line_ptr
[0] == line_ptr
[1])
506 * Repeated sequence...
512 while (line_ptr
< (line_end
- 1) &&
513 line_ptr
[0] == line_ptr
[1] &&
520 *comp_ptr
++ = 257 - count
;
521 *comp_ptr
++ = *line_ptr
++;
526 * Non-repeated sequence...
533 while (line_ptr
< (line_end
- 1) &&
534 line_ptr
[0] != line_ptr
[1] &&
541 *comp_ptr
++ = count
- 1;
543 memcpy(comp_ptr
, start
, count
);
548 line_ptr
= CompBuffer
;
553 putchar(0x0d); /* Move print head to left margin */
555 if (Model
< EPSON_ICOLOR
)
558 * Do graphics the "old" way...
568 printf("\033(r%c%c%c%c", 2, 0, 1, ctable
[plane
] & 15);
569 /* Set extended color */
570 else if (NumPlanes
== 3)
571 printf("\033r%c", ctable
[plane
+ 1]);
574 printf("\033r%c", ctable
[plane
]); /* Set color */
578 * Send a raster plane...
582 printf("\033."); /* Raster graphics */
588 putchar(length
>> 8);
593 * Do graphics the "new" way...
597 putchar(ctable
[plane
]);
600 putchar(length
& 255);
601 putchar(length
>> 8);
606 pwrite(line_ptr
, line_end
- line_ptr
);
612 * 'OutputLine()' - Output a line of graphics.
616 OutputLine(const cups_page_header2_t
*header
) /* I - Page header */
618 if (header
->cupsRowCount
)
621 unsigned char *tempptr
,
626 const unsigned char *pixel
;
631 * Collect bitmap data in the line buffers and write after each buffer.
634 for (x
= header
->cupsWidth
, bit
= 128, pixel
= Planes
[0],
656 * Copy the holding buffer to the output buffer, shingling as necessary...
659 if (Shingling
&& LineCount
!= 0)
662 * Shingle the output...
667 evenptr
= LineBuffers
[1] + OddOffset
;
668 oddptr
= LineBuffers
[0] + EvenOffset
+ DotBytes
;
672 evenptr
= LineBuffers
[0] + EvenOffset
;
673 oddptr
= LineBuffers
[1] + OddOffset
+ DotBytes
;
676 for (width
= header
->cupsWidth
, tempptr
= CompBuffer
;
678 width
-= 2, tempptr
+= 2, oddptr
+= DotBytes
* 2,
679 evenptr
+= DotBytes
* 2)
681 evenptr
[0] = tempptr
[0];
682 oddptr
[0] = tempptr
[1];
688 * Don't shingle the output...
691 for (width
= header
->cupsWidth
, tempptr
= CompBuffer
,
692 evenptr
= LineBuffers
[0] + EvenOffset
;
694 width
--, tempptr
++, evenptr
+= DotBytes
)
695 *evenptr
= tempptr
[0];
698 if (Shingling
&& LineCount
!= 0)
703 if (EvenOffset
== DotBytes
)
706 OutputRows(header
, 0);
709 if (OddOffset
== DotBytes
)
712 OutputRows(header
, 1);
719 if (EvenOffset
== DotBytes
)
722 OutputRows(header
, 0);
729 memset(CompBuffer
, 0, header
->cupsWidth
);
734 int plane
; /* Current plane */
735 int bytes
; /* Bytes per plane */
736 int xstep
, ystep
; /* X & Y resolutions */
740 * Write a single line of bitmap data as needed...
743 xstep
= 3600 / header
->HWResolution
[0];
744 ystep
= 3600 / header
->HWResolution
[1];
745 bytes
= header
->cupsBytesPerLine
/ NumPlanes
;
747 for (plane
= 0; plane
< NumPlanes
; plane
++)
753 if (!Planes
[plane
][0] &&
754 memcmp(Planes
[plane
], Planes
[plane
] + 1, bytes
- 1) == 0)
758 * Output whitespace as needed...
763 pwrite("\033(v\002\000", 5); /* Relative vertical position */
770 CompressData(Planes
[plane
], bytes
, plane
, header
->cupsCompression
, xstep
,
780 * 'OutputRows()' - Output 8, 24, or 48 rows.
784 OutputRows(const cups_page_header2_t
*header
, /* I - Page image header */
785 int row
) /* I - Row number (0 or 1) */
787 unsigned i
, n
; /* Looping vars */
788 int dot_count
, /* Number of bytes to print */
789 dot_min
; /* Minimum number of bytes */
790 unsigned char *dot_ptr
, /* Pointer to print data */
791 *ptr
; /* Current data */
794 dot_min
= DotBytes
* DotColumns
;
796 if (LineBuffers
[row
][0] != 0 ||
797 memcmp(LineBuffers
[row
], LineBuffers
[row
] + 1,
798 header
->cupsWidth
* DotBytes
- 1))
801 * Skip leading space...
805 dot_count
= header
->cupsWidth
* DotBytes
;
806 dot_ptr
= LineBuffers
[row
];
808 while (dot_count
>= dot_min
&& dot_ptr
[0] == 0 &&
809 memcmp(dot_ptr
, dot_ptr
+ 1, dot_min
- 1) == 0)
813 dot_count
-= dot_min
;
817 * Skip trailing space...
820 while (dot_count
>= dot_min
&& dot_ptr
[dot_count
- dot_min
] == 0 &&
821 memcmp(dot_ptr
+ dot_count
- dot_min
,
822 dot_ptr
+ dot_count
- dot_min
+ 1, dot_min
- 1) == 0)
823 dot_count
-= dot_min
;
826 * Position print head for printing...
840 * Start bitmap graphics for this line...
843 printf("\033*"); /* Select bit image */
844 switch (header
->HWResolution
[0])
846 case 60 : /* 60x60/72 DPI gfx */
849 case 120 : /* 120x60/72 DPI gfx */
852 case 180 : /* 180 DPI gfx */
855 case 240 : /* 240x72 DPI gfx */
858 case 360 : /* 360x180/360 DPI gfx */
859 if (header
->HWResolution
[1] == 180)
861 if (Shingling
&& LineCount
!= 0)
862 putchar(40); /* 360x180 fast */
864 putchar(41); /* 360x180 slow */
868 if (Shingling
&& LineCount
!= 0)
869 putchar(72); /* 360x360 fast */
871 putchar(73); /* 360x360 slow */
876 n
= (unsigned)dot_count
/ DotBytes
;
881 * Write the graphics data...
884 if (header
->HWResolution
[0] == 120 ||
885 header
->HWResolution
[0] == 240)
888 * Need to interleave the dots to avoid hosing the print head...
891 for (n
= dot_count
/ 2, ptr
= dot_ptr
; n
> 0; n
--, ptr
+= 2)
898 * Move the head back and print the odd bytes...
911 if (header
->HWResolution
[0] == 120)
912 printf("\033*\001"); /* Select bit image */
914 printf("\033*\003"); /* Select bit image */
916 n
= (unsigned)dot_count
/ DotBytes
;
920 for (n
= dot_count
/ 2, ptr
= dot_ptr
+ 1; n
> 0; n
--, ptr
+= 2)
927 pwrite(dot_ptr
, dot_count
);
936 if (Shingling
&& row
== 1)
938 if (header
->HWResolution
[1] == 360)
947 * Clear the buffer...
950 memset(LineBuffers
[row
], 0, header
->cupsWidth
* DotBytes
);
955 * 'main()' - Main entry and processing of driver.
958 int /* O - Exit status */
959 main(int argc
, /* I - Number of command-line arguments */
960 char *argv
[]) /* I - Command-line arguments */
962 int fd
; /* File descriptor */
963 cups_raster_t
*ras
; /* Raster stream for printing */
964 cups_page_header2_t header
; /* Page header from file */
965 ppd_file_t
*ppd
; /* PPD file */
966 int page
; /* Current page */
967 int y
; /* Current line */
968 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
969 struct sigaction action
; /* Actions for POSIX signals */
970 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
974 * Make sure status messages are not buffered...
977 setbuf(stderr
, NULL
);
980 * Check command-line...
983 if (argc
< 6 || argc
> 7)
986 * We don't have the correct number of arguments; write an error message
990 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
996 * Open the page stream...
1001 if ((fd
= open(argv
[6], O_RDONLY
)) == -1)
1003 perror("ERROR: Unable to open raster file - ");
1011 ras
= cupsRasterOpen(fd
, CUPS_RASTER_READ
);
1014 * Register a signal handler to eject the current page if the
1020 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
1021 sigset(SIGTERM
, CancelJob
);
1022 #elif defined(HAVE_SIGACTION)
1023 memset(&action
, 0, sizeof(action
));
1025 sigemptyset(&action
.sa_mask
);
1026 action
.sa_handler
= CancelJob
;
1027 sigaction(SIGTERM
, &action
, NULL
);
1029 signal(SIGTERM
, CancelJob
);
1030 #endif /* HAVE_SIGSET */
1033 * Initialize the print device...
1036 ppd
= ppdOpenFile(getenv("PPD"));
1038 Model
= ppd
->model_number
;
1043 * Process pages as needed...
1048 while (cupsRasterReadHeader2(ras
, &header
))
1051 * Write a status message with the page number and number of copies.
1059 fprintf(stderr
, "PAGE: %d %d\n", page
, header
.NumCopies
);
1065 StartPage(ppd
, &header
);
1068 * Loop for each line on the page...
1071 for (y
= 0; y
< header
.cupsHeight
; y
++)
1074 * Let the user know how far we have progressed...
1081 fprintf(stderr
, _("INFO: Printing page %d, %d%% complete...\n"), page
,
1082 100 * y
/ header
.cupsHeight
);
1085 * Read a line of graphics...
1088 if (cupsRasterReadPixels(ras
, Planes
[0], header
.cupsBytesPerLine
) < 1)
1092 * Write it to the printer...
1095 OutputLine(&header
);
1109 * Shutdown the printer...
1117 * Close the raster stream...
1120 cupsRasterClose(ras
);
1125 * If no pages were printed, send an error message...
1129 fputs(_("ERROR: No pages found!\n"), stderr
);
1131 fputs(_("INFO: Ready to print.\n"), stderr
);
1138 * End of "$Id: rastertoepson.c 6649 2007-07-11 21:46:42Z mike $".