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-2009 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>
53 #define EPSON_ICOLOR 4
54 #define EPSON_IPHOTO 5
61 #define pwrite(s,n) fwrite((s), 1, (n), stdout)
68 unsigned char *Planes
[6], /* Output buffers */
69 *CompBuffer
, /* Compression buffer */
70 *LineBuffers
[2]; /* Line bitmap buffers */
71 int Model
, /* Model number */
72 NumPlanes
, /* Number of color planes */
73 Feed
, /* Number of lines to skip */
74 EjectPage
; /* Eject the page when done? */
75 int DotBit
, /* Bit in buffers */
76 DotBytes
, /* # bytes in a dot column */
77 DotColumns
, /* # columns in 1/60 inch */
78 LineCount
, /* # of lines processed */
79 EvenOffset
, /* Offset into 'even' buffers */
80 OddOffset
, /* Offset into 'odd' buffers */
81 Shingling
, /* Shingle output? */
82 Canceled
; /* Has the current job been canceled? */
90 void StartPage(const ppd_file_t
*ppd
, const cups_page_header2_t
*header
);
91 void EndPage(const cups_page_header2_t
*header
);
94 void CancelJob(int sig
);
95 void CompressData(const unsigned char *line
, int length
, int plane
,
96 int type
, int xstep
, int ystep
);
97 void OutputLine(const cups_page_header2_t
*header
);
98 void OutputRows(const cups_page_header2_t
*header
, int row
);
102 * 'Setup()' - Prepare the printer for printing.
108 const char *device_uri
; /* The device for the printer... */
112 * EPSON USB printers need an additional command issued at the
113 * beginning of each job to exit from "packet" mode...
116 if ((device_uri
= getenv("DEVICE_URI")) != NULL
&&
117 strncmp(device_uri
, "usb:", 4) == 0 && Model
>= EPSON_ICOLOR
)
118 pwrite("\000\000\000\033\001@EJL 1284.4\n@EJL \n\033@", 29);
123 * 'StartPage()' - Start a page of graphics.
128 const ppd_file_t
*ppd
, /* I - PPD file */
129 const cups_page_header2_t
*header
) /* I - Page header */
131 int n
, t
; /* Numbers */
132 int plane
; /* Looping var */
136 * Send a reset sequence.
139 if (ppd
&& ppd
->nickname
&& strstr(ppd
->nickname
, "OKIDATA") != NULL
)
140 printf("\033{A"); /* Set EPSON emulation mode */
145 * See which type of printer we are using...
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("\033\062\033C%c", /* Page length in 1/6th inches */
168 (int)(header
->PageSize
[1] / 12.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.
324 const cups_page_header2_t
*header
) /* I - Page header */
326 if (DotBytes
&& header
)
329 * Flush remaining graphics as needed...
334 if (DotBit
< 128 || EvenOffset
)
335 OutputRows(header
, 0);
337 else if (OddOffset
> EvenOffset
)
339 OutputRows(header
, 1);
340 OutputRows(header
, 0);
344 OutputRows(header
, 0);
345 OutputRows(header
, 1);
350 * 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.
612 const cups_page_header2_t
*header
) /* I - Page header */
614 if (header
->cupsRowCount
)
617 unsigned char *tempptr
,
622 const unsigned char *pixel
;
627 * Collect bitmap data in the line buffers and write after each buffer.
630 for (x
= header
->cupsWidth
, bit
= 128, pixel
= Planes
[0],
652 * Copy the holding buffer to the output buffer, shingling as necessary...
655 if (Shingling
&& LineCount
!= 0)
658 * Shingle the output...
663 evenptr
= LineBuffers
[1] + OddOffset
;
664 oddptr
= LineBuffers
[0] + EvenOffset
+ DotBytes
;
668 evenptr
= LineBuffers
[0] + EvenOffset
;
669 oddptr
= LineBuffers
[1] + OddOffset
+ DotBytes
;
672 for (width
= header
->cupsWidth
, tempptr
= CompBuffer
;
674 width
-= 2, tempptr
+= 2, oddptr
+= DotBytes
* 2,
675 evenptr
+= DotBytes
* 2)
677 evenptr
[0] = tempptr
[0];
678 oddptr
[0] = tempptr
[1];
684 * Don't shingle the output...
687 for (width
= header
->cupsWidth
, tempptr
= CompBuffer
,
688 evenptr
= LineBuffers
[0] + EvenOffset
;
690 width
--, tempptr
++, evenptr
+= DotBytes
)
691 *evenptr
= tempptr
[0];
694 if (Shingling
&& LineCount
!= 0)
699 if (EvenOffset
== DotBytes
)
702 OutputRows(header
, 0);
705 if (OddOffset
== DotBytes
)
708 OutputRows(header
, 1);
715 if (EvenOffset
== DotBytes
)
718 OutputRows(header
, 0);
725 memset(CompBuffer
, 0, header
->cupsWidth
);
730 int plane
; /* Current plane */
731 int bytes
; /* Bytes per plane */
732 int xstep
, ystep
; /* X & Y resolutions */
736 * Write a single line of bitmap data as needed...
739 xstep
= 3600 / header
->HWResolution
[0];
740 ystep
= 3600 / header
->HWResolution
[1];
741 bytes
= header
->cupsBytesPerLine
/ NumPlanes
;
743 for (plane
= 0; plane
< NumPlanes
; plane
++)
749 if (!Planes
[plane
][0] &&
750 memcmp(Planes
[plane
], Planes
[plane
] + 1, bytes
- 1) == 0)
754 * Output whitespace as needed...
759 pwrite("\033(v\002\000", 5); /* Relative vertical position */
766 CompressData(Planes
[plane
], bytes
, plane
, header
->cupsCompression
, xstep
,
776 * 'OutputRows()' - Output 8, 24, or 48 rows.
781 const cups_page_header2_t
*header
, /* I - Page image header */
782 int row
) /* I - Row number (0 or 1) */
784 unsigned i
, n
; /* Looping vars */
785 int dot_count
, /* Number of bytes to print */
786 dot_min
; /* Minimum number of bytes */
787 unsigned char *dot_ptr
, /* Pointer to print data */
788 *ptr
; /* Current data */
791 dot_min
= DotBytes
* DotColumns
;
793 if (LineBuffers
[row
][0] != 0 ||
794 memcmp(LineBuffers
[row
], LineBuffers
[row
] + 1,
795 header
->cupsWidth
* DotBytes
- 1))
798 * Skip leading space...
802 dot_count
= header
->cupsWidth
* DotBytes
;
803 dot_ptr
= LineBuffers
[row
];
805 while (dot_count
>= dot_min
&& dot_ptr
[0] == 0 &&
806 memcmp(dot_ptr
, dot_ptr
+ 1, dot_min
- 1) == 0)
810 dot_count
-= dot_min
;
814 * Skip trailing space...
817 while (dot_count
>= dot_min
&& dot_ptr
[dot_count
- dot_min
] == 0 &&
818 memcmp(dot_ptr
+ dot_count
- dot_min
,
819 dot_ptr
+ dot_count
- dot_min
+ 1, dot_min
- 1) == 0)
820 dot_count
-= dot_min
;
823 * Position print head for printing...
837 * Start bitmap graphics for this line...
840 printf("\033*"); /* Select bit image */
841 switch (header
->HWResolution
[0])
843 case 60 : /* 60x60/72 DPI gfx */
846 case 120 : /* 120x60/72 DPI gfx */
849 case 180 : /* 180 DPI gfx */
852 case 240 : /* 240x72 DPI gfx */
855 case 360 : /* 360x180/360 DPI gfx */
856 if (header
->HWResolution
[1] == 180)
858 if (Shingling
&& LineCount
!= 0)
859 putchar(40); /* 360x180 fast */
861 putchar(41); /* 360x180 slow */
865 if (Shingling
&& LineCount
!= 0)
866 putchar(72); /* 360x360 fast */
868 putchar(73); /* 360x360 slow */
873 n
= (unsigned)dot_count
/ DotBytes
;
878 * Write the graphics data...
881 if (header
->HWResolution
[0] == 120 ||
882 header
->HWResolution
[0] == 240)
885 * Need to interleave the dots to avoid hosing the print head...
888 for (n
= dot_count
/ 2, ptr
= dot_ptr
; n
> 0; n
--, ptr
+= 2)
895 * Move the head back and print the odd bytes...
908 if (header
->HWResolution
[0] == 120)
909 printf("\033*\001"); /* Select bit image */
911 printf("\033*\003"); /* Select bit image */
913 n
= (unsigned)dot_count
/ DotBytes
;
917 for (n
= dot_count
/ 2, ptr
= dot_ptr
+ 1; n
> 0; n
--, ptr
+= 2)
924 pwrite(dot_ptr
, dot_count
);
933 if (Shingling
&& row
== 1)
935 if (header
->HWResolution
[1] == 360)
944 * Clear the buffer...
947 memset(LineBuffers
[row
], 0, header
->cupsWidth
* DotBytes
);
952 * 'main()' - Main entry and processing of driver.
955 int /* O - Exit status */
956 main(int argc
, /* I - Number of command-line arguments */
957 char *argv
[]) /* I - Command-line arguments */
959 int fd
; /* File descriptor */
960 cups_raster_t
*ras
; /* Raster stream for printing */
961 cups_page_header2_t header
; /* Page header from file */
962 ppd_file_t
*ppd
; /* PPD file */
963 int page
; /* Current page */
964 int y
; /* Current line */
965 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
966 struct sigaction action
; /* Actions for POSIX signals */
967 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
971 * Make sure status messages are not buffered...
974 setbuf(stderr
, NULL
);
977 * Check command-line...
980 if (argc
< 6 || argc
> 7)
983 * We don't have the correct number of arguments; write an error message
987 _cupsLangPrintf(stderr
,
988 _("Usage: %s job-id user title copies options [file]\n"),
994 * Open the page stream...
999 if ((fd
= open(argv
[6], O_RDONLY
)) == -1)
1001 _cupsLangPrintf(stderr
, _("ERROR: Unable to open raster file - %s\n"),
1010 ras
= cupsRasterOpen(fd
, CUPS_RASTER_READ
);
1013 * Register a signal handler to eject the current page if the
1019 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
1020 sigset(SIGTERM
, CancelJob
);
1021 #elif defined(HAVE_SIGACTION)
1022 memset(&action
, 0, sizeof(action
));
1024 sigemptyset(&action
.sa_mask
);
1025 action
.sa_handler
= CancelJob
;
1026 sigaction(SIGTERM
, &action
, NULL
);
1028 signal(SIGTERM
, CancelJob
);
1029 #endif /* HAVE_SIGSET */
1032 * Initialize the print device...
1035 ppd
= ppdOpenFile(getenv("PPD"));
1037 Model
= ppd
->model_number
;
1042 * Process pages as needed...
1047 while (cupsRasterReadHeader2(ras
, &header
))
1050 * Write a status message with the page number and number of copies.
1058 fprintf(stderr
, "PAGE: %d %d\n", page
, header
.NumCopies
);
1064 StartPage(ppd
, &header
);
1067 * Loop for each line on the page...
1070 for (y
= 0; y
< header
.cupsHeight
; y
++)
1073 * Let the user know how far we have progressed...
1080 _cupsLangPrintf(stderr
, _("INFO: Printing page %d, %d%% complete...\n"),
1081 page
, 100 * y
/ header
.cupsHeight
);
1084 * Read a line of graphics...
1087 if (cupsRasterReadPixels(ras
, Planes
[0], header
.cupsBytesPerLine
) < 1)
1091 * Write it to the printer...
1094 OutputLine(&header
);
1108 * Shutdown the printer...
1116 * Close the raster stream...
1119 cupsRasterClose(ras
);
1124 * If no pages were printed, send an error message...
1129 _cupsLangPuts(stderr
, _("ERROR: No pages found!\n"));
1134 _cupsLangPuts(stderr
, _("INFO: Ready to print.\n"));
1141 * End of "$Id: rastertoepson.c 7450 2008-04-14 19:39:02Z mike $".