]>
Commit | Line | Data |
---|---|---|
5f82b2f5 | 1 | /* |
b2e10895 | 2 | * "$Id$" |
5f82b2f5 | 3 | * |
40c1123a | 4 | * Hewlett-Packard Page Control Language filter for the Common UNIX |
5 | * Printing System (CUPS). | |
5f82b2f5 | 6 | * |
4e8d321f | 7 | * Copyright 2007 by Apple Inc. |
59b77f42 | 8 | * Copyright 1993-2007 by Easy Software Products. |
5f82b2f5 | 9 | * |
40c1123a | 10 | * These coded instructions, statements, and computer programs are the |
4e8d321f | 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/". | |
5f82b2f5 | 15 | * |
dab1a4d8 | 16 | * This file is subject to the Apple OS-Developed Software exception. |
17 | * | |
5f82b2f5 | 18 | * Contents: |
19 | * | |
40c1123a | 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. | |
358cc876 | 24 | * CancelJob() - Cancel the current job... |
40c1123a | 25 | * CompressData() - Compress a line of graphics. |
26 | * OutputLine() - Output a line of graphics. | |
27 | * main() - Main entry and processing of driver. | |
5f82b2f5 | 28 | */ |
29 | ||
30 | /* | |
31 | * Include necessary headers... | |
32 | */ | |
33 | ||
34 | #include <cups/cups.h> | |
5f82b2f5 | 35 | #include <cups/string.h> |
59b77f42 | 36 | #include <cups/i18n.h> |
aff2356d | 37 | #include "raster.h" |
5f82b2f5 | 38 | #include <stdlib.h> |
39 | #include <unistd.h> | |
40 | #include <fcntl.h> | |
358cc876 | 41 | #include <signal.h> |
5f82b2f5 | 42 | |
43 | ||
44 | /* | |
45 | * Globals... | |
46 | */ | |
47 | ||
48 | unsigned char *Planes[4], /* Output buffers */ | |
b5cb0608 | 49 | *CompBuffer, /* Compression buffer */ |
50 | *BitBuffer; /* Buffer for output bits */ | |
5f82b2f5 | 51 | int NumPlanes, /* Number of color planes */ |
b5cb0608 | 52 | ColorBits, /* Number of bits per color */ |
93fee5c4 | 53 | Feed, /* Number of lines to skip */ |
358cc876 | 54 | Duplex, /* Current duplex mode */ |
93fee5c4 | 55 | Page; /* Current page number */ |
5f82b2f5 | 56 | |
57 | ||
58 | /* | |
59 | * Prototypes... | |
60 | */ | |
61 | ||
62 | void Setup(void); | |
b5cb0608 | 63 | void StartPage(ppd_file_t *ppd, cups_page_header_t *header); |
358cc876 | 64 | void EndPage(void); |
5f82b2f5 | 65 | void Shutdown(void); |
66 | ||
358cc876 | 67 | void CancelJob(int sig); |
5f82b2f5 | 68 | void CompressData(unsigned char *line, int length, int plane, int type); |
69 | void OutputLine(cups_page_header_t *header); | |
70 | ||
71 | ||
72 | /* | |
73 | * 'Setup()' - Prepare the printer for printing. | |
74 | */ | |
75 | ||
76 | void | |
77 | Setup(void) | |
78 | { | |
79 | /* | |
80 | * Send a PCL reset sequence. | |
81 | */ | |
82 | ||
83 | putchar(0x1b); | |
84 | putchar('E'); | |
85 | } | |
86 | ||
87 | ||
88 | /* | |
89 | * 'StartPage()' - Start a page of graphics. | |
90 | */ | |
91 | ||
92 | void | |
b5cb0608 | 93 | StartPage(ppd_file_t *ppd, /* I - PPD file */ |
94 | cups_page_header_t *header) /* I - Page header */ | |
5f82b2f5 | 95 | { |
96 | int plane; /* Looping var */ | |
358cc876 | 97 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) |
98 | struct sigaction action; /* Actions for POSIX signals */ | |
99 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
5f82b2f5 | 100 | |
101 | ||
358cc876 | 102 | /* |
103 | * Register a signal handler to eject the current page if the | |
104 | * job is cancelled. | |
105 | */ | |
106 | ||
107 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
108 | sigset(SIGTERM, CancelJob); | |
109 | #elif defined(HAVE_SIGACTION) | |
110 | memset(&action, 0, sizeof(action)); | |
111 | ||
112 | sigemptyset(&action.sa_mask); | |
113 | action.sa_handler = CancelJob; | |
114 | sigaction(SIGTERM, &action, NULL); | |
115 | #else | |
116 | signal(SIGTERM, CancelJob); | |
117 | #endif /* HAVE_SIGSET */ | |
118 | ||
b2e10895 | 119 | /* |
120 | * Show page device dictionary... | |
121 | */ | |
122 | ||
123 | fprintf(stderr, "DEBUG: StartPage...\n"); | |
124 | fprintf(stderr, "DEBUG: MediaClass = \"%s\"\n", header->MediaClass); | |
125 | fprintf(stderr, "DEBUG: MediaColor = \"%s\"\n", header->MediaColor); | |
126 | fprintf(stderr, "DEBUG: MediaType = \"%s\"\n", header->MediaType); | |
127 | fprintf(stderr, "DEBUG: OutputType = \"%s\"\n", header->OutputType); | |
128 | ||
129 | fprintf(stderr, "DEBUG: AdvanceDistance = %d\n", header->AdvanceDistance); | |
130 | fprintf(stderr, "DEBUG: AdvanceMedia = %d\n", header->AdvanceMedia); | |
131 | fprintf(stderr, "DEBUG: Collate = %d\n", header->Collate); | |
132 | fprintf(stderr, "DEBUG: CutMedia = %d\n", header->CutMedia); | |
133 | fprintf(stderr, "DEBUG: Duplex = %d\n", header->Duplex); | |
134 | fprintf(stderr, "DEBUG: HWResolution = [ %d %d ]\n", header->HWResolution[0], | |
135 | header->HWResolution[1]); | |
136 | fprintf(stderr, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n", | |
137 | header->ImagingBoundingBox[0], header->ImagingBoundingBox[1], | |
138 | header->ImagingBoundingBox[2], header->ImagingBoundingBox[3]); | |
139 | fprintf(stderr, "DEBUG: InsertSheet = %d\n", header->InsertSheet); | |
140 | fprintf(stderr, "DEBUG: Jog = %d\n", header->Jog); | |
141 | fprintf(stderr, "DEBUG: LeadingEdge = %d\n", header->LeadingEdge); | |
142 | fprintf(stderr, "DEBUG: Margins = [ %d %d ]\n", header->Margins[0], | |
143 | header->Margins[1]); | |
144 | fprintf(stderr, "DEBUG: ManualFeed = %d\n", header->ManualFeed); | |
145 | fprintf(stderr, "DEBUG: MediaPosition = %d\n", header->MediaPosition); | |
146 | fprintf(stderr, "DEBUG: MediaWeight = %d\n", header->MediaWeight); | |
147 | fprintf(stderr, "DEBUG: MirrorPrint = %d\n", header->MirrorPrint); | |
148 | fprintf(stderr, "DEBUG: NegativePrint = %d\n", header->NegativePrint); | |
149 | fprintf(stderr, "DEBUG: NumCopies = %d\n", header->NumCopies); | |
150 | fprintf(stderr, "DEBUG: Orientation = %d\n", header->Orientation); | |
151 | fprintf(stderr, "DEBUG: OutputFaceUp = %d\n", header->OutputFaceUp); | |
152 | fprintf(stderr, "DEBUG: PageSize = [ %d %d ]\n", header->PageSize[0], | |
153 | header->PageSize[1]); | |
154 | fprintf(stderr, "DEBUG: Separations = %d\n", header->Separations); | |
155 | fprintf(stderr, "DEBUG: TraySwitch = %d\n", header->TraySwitch); | |
156 | fprintf(stderr, "DEBUG: Tumble = %d\n", header->Tumble); | |
157 | fprintf(stderr, "DEBUG: cupsWidth = %d\n", header->cupsWidth); | |
158 | fprintf(stderr, "DEBUG: cupsHeight = %d\n", header->cupsHeight); | |
159 | fprintf(stderr, "DEBUG: cupsMediaType = %d\n", header->cupsMediaType); | |
160 | fprintf(stderr, "DEBUG: cupsBitsPerColor = %d\n", header->cupsBitsPerColor); | |
161 | fprintf(stderr, "DEBUG: cupsBitsPerPixel = %d\n", header->cupsBitsPerPixel); | |
162 | fprintf(stderr, "DEBUG: cupsBytesPerLine = %d\n", header->cupsBytesPerLine); | |
163 | fprintf(stderr, "DEBUG: cupsColorOrder = %d\n", header->cupsColorOrder); | |
164 | fprintf(stderr, "DEBUG: cupsColorSpace = %d\n", header->cupsColorSpace); | |
165 | fprintf(stderr, "DEBUG: cupsCompression = %d\n", header->cupsCompression); | |
166 | ||
358cc876 | 167 | /* |
168 | * Setup printer/job attributes... | |
169 | */ | |
170 | ||
b5cb0608 | 171 | Duplex = header->Duplex; |
172 | ColorBits = header->cupsBitsPerColor; | |
358cc876 | 173 | |
b2e10895 | 174 | if ((!Duplex || (Page & 1)) && header->MediaPosition) |
175 | printf("\033&l%dH", /* Set media position */ | |
176 | header->MediaPosition); | |
177 | ||
178 | if (Duplex && ppd && ppd->model_number == 2) | |
179 | { | |
180 | /* | |
181 | * Handle duplexing on new DeskJet printers... | |
182 | */ | |
183 | ||
184 | printf("\033&l-2H"); /* Load media */ | |
185 | ||
186 | if (Page & 1) | |
187 | printf("\033&l2S"); /* Set duplex mode */ | |
188 | } | |
189 | ||
190 | if (!Duplex || (Page & 1) || (ppd && ppd->model_number == 2)) | |
93fee5c4 | 191 | { |
192 | /* | |
b2e10895 | 193 | * Set the media size... |
93fee5c4 | 194 | */ |
5f82b2f5 | 195 | |
93fee5c4 | 196 | printf("\033&l6D\033&k12H"); /* Set 6 LPI, 10 CPI */ |
b5cb0608 | 197 | printf("\033&l0O"); /* Set portrait orientation */ |
0beadd9e | 198 | |
93fee5c4 | 199 | switch (header->PageSize[1]) |
200 | { | |
201 | case 540 : /* Monarch Envelope */ | |
202 | printf("\033&l80A"); /* Set page size */ | |
203 | break; | |
0beadd9e | 204 | |
93fee5c4 | 205 | case 624 : /* DL Envelope */ |
206 | printf("\033&l90A"); /* Set page size */ | |
207 | break; | |
0beadd9e | 208 | |
93fee5c4 | 209 | case 649 : /* C5 Envelope */ |
210 | printf("\033&l91A"); /* Set page size */ | |
211 | break; | |
0beadd9e | 212 | |
93fee5c4 | 213 | case 684 : /* COM-10 Envelope */ |
214 | printf("\033&l81A"); /* Set page size */ | |
215 | break; | |
0beadd9e | 216 | |
93fee5c4 | 217 | case 709 : /* B5 Envelope */ |
218 | printf("\033&l100A"); /* Set page size */ | |
219 | break; | |
0beadd9e | 220 | |
93fee5c4 | 221 | case 756 : /* Executive */ |
222 | printf("\033&l1A"); /* Set page size */ | |
223 | break; | |
0beadd9e | 224 | |
93fee5c4 | 225 | case 792 : /* Letter */ |
226 | printf("\033&l2A"); /* Set page size */ | |
227 | break; | |
0beadd9e | 228 | |
93fee5c4 | 229 | case 842 : /* A4 */ |
230 | printf("\033&l26A"); /* Set page size */ | |
231 | break; | |
0beadd9e | 232 | |
93fee5c4 | 233 | case 1008 : /* Legal */ |
234 | printf("\033&l3A"); /* Set page size */ | |
235 | break; | |
0beadd9e | 236 | |
93fee5c4 | 237 | case 1191 : /* A3 */ |
238 | printf("\033&l27A"); /* Set page size */ | |
239 | break; | |
0beadd9e | 240 | |
93fee5c4 | 241 | case 1224 : /* Tabloid */ |
242 | printf("\033&l6A"); /* Set page size */ | |
243 | break; | |
244 | } | |
0beadd9e | 245 | |
93fee5c4 | 246 | printf("\033&l%dP", /* Set page length */ |
247 | header->PageSize[1] / 12); | |
248 | printf("\033&l0E"); /* Set top margin to 0 */ | |
b2e10895 | 249 | } |
4b474df6 | 250 | |
b2e10895 | 251 | if (!Duplex || (Page & 1)) |
252 | { | |
253 | /* | |
254 | * Set other job options... | |
255 | */ | |
0beadd9e | 256 | |
b2e10895 | 257 | printf("\033&l%dX", header->NumCopies); /* Set number copies */ |
0beadd9e | 258 | |
b2e10895 | 259 | if (header->cupsMediaType && |
260 | (!ppd || ppd->model_number != 2 || header->HWResolution[0] == 600)) | |
93fee5c4 | 261 | printf("\033&l%dM", /* Set media type */ |
262 | header->cupsMediaType); | |
5f82b2f5 | 263 | |
b2e10895 | 264 | if (!ppd || ppd->model_number != 2) |
265 | { | |
266 | if (header->Duplex) | |
267 | printf("\033&l%dS", /* Set duplex mode */ | |
268 | header->Duplex + header->Tumble); | |
b5cb0608 | 269 | |
b2e10895 | 270 | printf("\033&l0L"); /* Turn off perforation skip */ |
271 | } | |
93fee5c4 | 272 | } |
b2e10895 | 273 | else if (!ppd || ppd->model_number != 2) |
93fee5c4 | 274 | printf("\033&a2G"); /* Set back side */ |
0beadd9e | 275 | |
5f82b2f5 | 276 | /* |
277 | * Set graphics mode... | |
278 | */ | |
279 | ||
b5cb0608 | 280 | if (ppd->model_number == 2) |
5f82b2f5 | 281 | { |
b5cb0608 | 282 | /* |
283 | * Figure out the number of color planes... | |
284 | */ | |
285 | ||
286 | if (header->cupsColorSpace == CUPS_CSPACE_KCMY) | |
287 | NumPlanes = 4; | |
288 | else | |
289 | NumPlanes = 1; | |
290 | ||
b2e10895 | 291 | /* |
292 | * Set the resolution and top-of-form... | |
293 | */ | |
294 | ||
295 | printf("\033&u%dD", header->HWResolution[0]); | |
296 | /* Resolution */ | |
297 | printf("\033&l0e0L"); /* Reset top and don't skip */ | |
298 | printf("\033*p0Y\033*p0X"); /* Set top of form */ | |
299 | ||
b5cb0608 | 300 | /* |
301 | * Send 26-byte configure image data command with horizontal and | |
302 | * vertical resolutions as well as a color count... | |
303 | */ | |
304 | ||
305 | printf("\033*g26W"); | |
306 | putchar(2); /* Format 2 */ | |
307 | putchar(NumPlanes); /* Output planes */ | |
308 | ||
309 | putchar(header->HWResolution[0] >> 8); /* Black resolution */ | |
310 | putchar(header->HWResolution[0]); | |
311 | putchar(header->HWResolution[1] >> 8); | |
312 | putchar(header->HWResolution[1]); | |
313 | putchar(0); | |
314 | putchar(1 << ColorBits); /* # of black levels */ | |
315 | ||
316 | putchar(header->HWResolution[0] >> 8); /* Cyan resolution */ | |
317 | putchar(header->HWResolution[0]); | |
318 | putchar(header->HWResolution[1] >> 8); | |
319 | putchar(header->HWResolution[1]); | |
320 | putchar(0); | |
321 | putchar(1 << ColorBits); /* # of cyan levels */ | |
322 | ||
323 | putchar(header->HWResolution[0] >> 8); /* Magenta resolution */ | |
324 | putchar(header->HWResolution[0]); | |
325 | putchar(header->HWResolution[1] >> 8); | |
326 | putchar(header->HWResolution[1]); | |
327 | putchar(0); | |
328 | putchar(1 << ColorBits); /* # of magenta levels */ | |
329 | ||
330 | putchar(header->HWResolution[0] >> 8); /* Yellow resolution */ | |
331 | putchar(header->HWResolution[0]); | |
332 | putchar(header->HWResolution[1] >> 8); | |
333 | putchar(header->HWResolution[1]); | |
334 | putchar(0); | |
335 | putchar(1 << ColorBits); /* # of yellow levels */ | |
b2e10895 | 336 | |
337 | printf("\033&l0H"); /* Set media position */ | |
5f82b2f5 | 338 | } |
b5cb0608 | 339 | else |
4b474df6 | 340 | { |
b2e10895 | 341 | printf("\033*t%dR", header->HWResolution[0]); |
342 | /* Set resolution */ | |
343 | ||
b5cb0608 | 344 | if (header->cupsColorSpace == CUPS_CSPACE_KCMY) |
345 | { | |
346 | NumPlanes = 4; | |
347 | printf("\033*r-4U"); /* Set KCMY graphics */ | |
348 | } | |
349 | else if (header->cupsColorSpace == CUPS_CSPACE_CMY) | |
350 | { | |
351 | NumPlanes = 3; | |
352 | printf("\033*r-3U"); /* Set CMY graphics */ | |
353 | } | |
354 | else | |
355 | NumPlanes = 1; /* Black&white graphics */ | |
b5cb0608 | 356 | |
b2e10895 | 357 | /* |
358 | * Set size and position of graphics... | |
359 | */ | |
b5cb0608 | 360 | |
b2e10895 | 361 | printf("\033*r%dS", header->cupsWidth); /* Set width */ |
362 | printf("\033*r%dT", header->cupsHeight); /* Set height */ | |
b5cb0608 | 363 | |
b2e10895 | 364 | printf("\033&a0H"); /* Set horizontal position */ |
b5cb0608 | 365 | |
b2e10895 | 366 | if (ppd) |
367 | printf("\033&a%.0fV", /* Set vertical position */ | |
368 | 10.0 * (ppd->sizes[0].length - ppd->sizes[0].top)); | |
369 | else | |
370 | printf("\033&a0V"); /* Set top-of-page */ | |
371 | } | |
5f82b2f5 | 372 | |
ba8a42d9 | 373 | printf("\033*r1A"); /* Start graphics */ |
5f82b2f5 | 374 | |
375 | if (header->cupsCompression) | |
376 | printf("\033*b%dM", /* Set compression */ | |
377 | header->cupsCompression); | |
378 | ||
379 | Feed = 0; /* No blank lines yet */ | |
380 | ||
381 | /* | |
382 | * Allocate memory for a line of graphics... | |
383 | */ | |
384 | ||
385 | Planes[0] = malloc(header->cupsBytesPerLine); | |
386 | for (plane = 1; plane < NumPlanes; plane ++) | |
387 | Planes[plane] = Planes[0] + plane * header->cupsBytesPerLine / NumPlanes; | |
388 | ||
b5cb0608 | 389 | if (ColorBits > 1) |
390 | BitBuffer = malloc(ColorBits * ((header->cupsWidth + 7) / 8)); | |
391 | else | |
392 | BitBuffer = NULL; | |
393 | ||
5f82b2f5 | 394 | if (header->cupsCompression) |
395 | CompBuffer = malloc(header->cupsBytesPerLine * 2); | |
358cc876 | 396 | else |
397 | CompBuffer = NULL; | |
5f82b2f5 | 398 | } |
399 | ||
400 | ||
401 | /* | |
402 | * 'EndPage()' - Finish a page of graphics. | |
403 | */ | |
404 | ||
405 | void | |
358cc876 | 406 | EndPage(void) |
5f82b2f5 | 407 | { |
358cc876 | 408 | #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) |
409 | struct sigaction action; /* Actions for POSIX signals */ | |
410 | #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ | |
411 | ||
412 | ||
5f82b2f5 | 413 | /* |
414 | * Eject the current page... | |
415 | */ | |
416 | ||
417 | if (NumPlanes > 1) | |
418 | { | |
419 | printf("\033*rC"); /* End color GFX */ | |
b5cb0608 | 420 | |
421 | if (!(Duplex && (Page & 1))) | |
422 | printf("\033&l0H"); /* Eject current page */ | |
5f82b2f5 | 423 | } |
424 | else | |
425 | { | |
426 | printf("\033*r0B"); /* End GFX */ | |
93fee5c4 | 427 | |
358cc876 | 428 | if (!(Duplex && (Page & 1))) |
93fee5c4 | 429 | printf("\014"); /* Eject current page */ |
5f82b2f5 | 430 | } |
431 | ||
358cc876 | 432 | fflush(stdout); |
433 | ||
434 | /* | |
435 | * Unregister the signal handler... | |
436 | */ | |
437 | ||
438 | #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ | |
439 | sigset(SIGTERM, SIG_IGN); | |
440 | #elif defined(HAVE_SIGACTION) | |
441 | memset(&action, 0, sizeof(action)); | |
442 | ||
443 | sigemptyset(&action.sa_mask); | |
444 | action.sa_handler = SIG_IGN; | |
445 | sigaction(SIGTERM, &action, NULL); | |
446 | #else | |
447 | signal(SIGTERM, SIG_IGN); | |
448 | #endif /* HAVE_SIGSET */ | |
449 | ||
5f82b2f5 | 450 | /* |
451 | * Free memory... | |
452 | */ | |
453 | ||
454 | free(Planes[0]); | |
455 | ||
b5cb0608 | 456 | if (BitBuffer) |
457 | free(BitBuffer); | |
458 | ||
358cc876 | 459 | if (CompBuffer) |
5f82b2f5 | 460 | free(CompBuffer); |
461 | } | |
462 | ||
463 | ||
464 | /* | |
465 | * 'Shutdown()' - Shutdown the printer. | |
466 | */ | |
467 | ||
468 | void | |
469 | Shutdown(void) | |
470 | { | |
471 | /* | |
472 | * Send a PCL reset sequence. | |
473 | */ | |
474 | ||
475 | putchar(0x1b); | |
476 | putchar('E'); | |
477 | } | |
478 | ||
479 | ||
358cc876 | 480 | /* |
481 | * 'CancelJob()' - Cancel the current job... | |
482 | */ | |
483 | ||
484 | void | |
485 | CancelJob(int sig) /* I - Signal */ | |
486 | { | |
487 | int i; /* Looping var */ | |
488 | ||
489 | ||
490 | (void)sig; | |
491 | ||
492 | /* | |
493 | * Send out lots of NUL bytes to clear out any pending raster data... | |
494 | */ | |
495 | ||
496 | for (i = 0; i < 600; i ++) | |
497 | putchar(0); | |
498 | ||
499 | /* | |
500 | * End the current page and exit... | |
501 | */ | |
502 | ||
503 | EndPage(); | |
504 | Shutdown(); | |
505 | ||
506 | exit(0); | |
507 | } | |
508 | ||
509 | ||
5f82b2f5 | 510 | /* |
511 | * 'CompressData()' - Compress a line of graphics. | |
512 | */ | |
513 | ||
514 | void | |
515 | CompressData(unsigned char *line, /* I - Data to compress */ | |
516 | int length, /* I - Number of bytes */ | |
517 | int plane, /* I - Color plane */ | |
518 | int type) /* I - Type of compression */ | |
519 | { | |
520 | unsigned char *line_ptr, /* Current byte pointer */ | |
521 | *line_end, /* End-of-line byte pointer */ | |
522 | *comp_ptr, /* Pointer into compression buffer */ | |
523 | *start; /* Start of compression sequence */ | |
524 | int count; /* Count of bytes for output */ | |
525 | ||
526 | ||
527 | switch (type) | |
528 | { | |
d21a7597 | 529 | default : |
5f82b2f5 | 530 | /* |
531 | * Do no compression... | |
532 | */ | |
533 | ||
534 | line_ptr = line; | |
535 | line_end = line + length; | |
536 | break; | |
537 | ||
538 | case 1 : | |
539 | /* | |
540 | * Do run-length encoding... | |
541 | */ | |
542 | ||
543 | line_end = line + length; | |
544 | for (line_ptr = line, comp_ptr = CompBuffer; | |
545 | line_ptr < line_end; | |
546 | comp_ptr += 2, line_ptr += count) | |
547 | { | |
548 | for (count = 1; | |
549 | (line_ptr + count) < line_end && | |
550 | line_ptr[0] == line_ptr[count] && | |
551 | count < 256; | |
552 | count ++); | |
553 | ||
554 | comp_ptr[0] = count - 1; | |
555 | comp_ptr[1] = line_ptr[0]; | |
556 | } | |
557 | ||
558 | line_ptr = CompBuffer; | |
559 | line_end = comp_ptr; | |
560 | break; | |
561 | ||
562 | case 2 : | |
563 | /* | |
564 | * Do TIFF pack-bits encoding... | |
565 | */ | |
566 | ||
567 | line_ptr = line; | |
568 | line_end = line + length; | |
569 | comp_ptr = CompBuffer; | |
570 | ||
571 | while (line_ptr < line_end) | |
572 | { | |
573 | if ((line_ptr + 1) >= line_end) | |
574 | { | |
575 | /* | |
576 | * Single byte on the end... | |
577 | */ | |
578 | ||
579 | *comp_ptr++ = 0x00; | |
580 | *comp_ptr++ = *line_ptr++; | |
581 | } | |
582 | else if (line_ptr[0] == line_ptr[1]) | |
583 | { | |
584 | /* | |
585 | * Repeated sequence... | |
586 | */ | |
587 | ||
588 | line_ptr ++; | |
589 | count = 2; | |
590 | ||
591 | while (line_ptr < (line_end - 1) && | |
592 | line_ptr[0] == line_ptr[1] && | |
593 | count < 127) | |
594 | { | |
595 | line_ptr ++; | |
596 | count ++; | |
597 | } | |
598 | ||
599 | *comp_ptr++ = 257 - count; | |
600 | *comp_ptr++ = *line_ptr++; | |
601 | } | |
602 | else | |
603 | { | |
604 | /* | |
605 | * Non-repeated sequence... | |
606 | */ | |
607 | ||
608 | start = line_ptr; | |
609 | line_ptr ++; | |
610 | count = 1; | |
611 | ||
612 | while (line_ptr < (line_end - 1) && | |
613 | line_ptr[0] != line_ptr[1] && | |
614 | count < 127) | |
615 | { | |
616 | line_ptr ++; | |
617 | count ++; | |
618 | } | |
619 | ||
620 | *comp_ptr++ = count - 1; | |
621 | ||
622 | memcpy(comp_ptr, start, count); | |
623 | comp_ptr += count; | |
624 | } | |
625 | } | |
626 | ||
627 | line_ptr = CompBuffer; | |
628 | line_end = comp_ptr; | |
629 | break; | |
630 | } | |
631 | ||
632 | /* | |
633 | * Set the length of the data and write a raster plane... | |
634 | */ | |
635 | ||
e0ba31cd | 636 | printf("\033*b%d%c", (int)(line_end - line_ptr), plane); |
5f82b2f5 | 637 | fwrite(line_ptr, line_end - line_ptr, 1, stdout); |
638 | } | |
639 | ||
640 | ||
641 | /* | |
642 | * 'OutputLine()' - Output a line of graphics. | |
643 | */ | |
644 | ||
645 | void | |
646 | OutputLine(cups_page_header_t *header) /* I - Page header */ | |
647 | { | |
b5cb0608 | 648 | int plane, /* Current plane */ |
649 | bytes, /* Bytes to write */ | |
650 | count; /* Bytes to convert */ | |
651 | unsigned char bit, /* Current plane data */ | |
652 | bit0, /* Current low bit data */ | |
653 | bit1, /* Current high bit data */ | |
654 | *plane_ptr, /* Pointer into Planes */ | |
655 | *bit_ptr; /* Pointer into BitBuffer */ | |
5f82b2f5 | 656 | |
657 | ||
658 | /* | |
659 | * Output whitespace as needed... | |
660 | */ | |
661 | ||
662 | if (Feed > 0) | |
663 | { | |
664 | printf("\033*b%dY", Feed); | |
665 | Feed = 0; | |
666 | } | |
667 | ||
668 | /* | |
669 | * Write bitmap data as needed... | |
670 | */ | |
671 | ||
b5cb0608 | 672 | bytes = (header->cupsWidth + 7) / 8; |
673 | ||
5f82b2f5 | 674 | for (plane = 0; plane < NumPlanes; plane ++) |
b5cb0608 | 675 | if (ColorBits == 1) |
676 | { | |
677 | /* | |
678 | * Send bits as-is... | |
679 | */ | |
680 | ||
681 | CompressData(Planes[plane], bytes, plane < (NumPlanes - 1) ? 'V' : 'W', | |
682 | header->cupsCompression); | |
683 | } | |
684 | else | |
685 | { | |
686 | /* | |
687 | * Separate low and high bit data into separate buffers. | |
688 | */ | |
689 | ||
690 | for (count = header->cupsBytesPerLine / NumPlanes, | |
691 | plane_ptr = Planes[plane], bit_ptr = BitBuffer; | |
692 | count > 0; | |
693 | count -= 2, plane_ptr += 2, bit_ptr ++) | |
694 | { | |
695 | bit = plane_ptr[0]; | |
696 | ||
697 | bit0 = ((bit & 64) << 1) | ((bit & 16) << 2) | ((bit & 4) << 3) | ((bit & 1) << 4); | |
698 | bit1 = (bit & 128) | ((bit & 32) << 1) | ((bit & 8) << 2) | ((bit & 2) << 3); | |
699 | ||
700 | if (count > 1) | |
701 | { | |
702 | bit = plane_ptr[1]; | |
703 | ||
704 | bit0 |= (bit & 1) | ((bit & 4) >> 1) | ((bit & 16) >> 2) | ((bit & 64) >> 3); | |
705 | bit1 |= ((bit & 2) >> 1) | ((bit & 8) >> 2) | ((bit & 32) >> 3) | ((bit & 128) >> 4); | |
706 | } | |
707 | ||
708 | bit_ptr[0] = bit0; | |
709 | bit_ptr[bytes] = bit1; | |
710 | } | |
711 | ||
712 | /* | |
713 | * Send low and high bits... | |
714 | */ | |
715 | ||
716 | CompressData(BitBuffer, bytes, 'V', header->cupsCompression); | |
717 | CompressData(BitBuffer + bytes, bytes, plane < (NumPlanes - 1) ? 'V' : 'W', | |
718 | header->cupsCompression); | |
719 | } | |
358cc876 | 720 | |
721 | fflush(stdout); | |
5f82b2f5 | 722 | } |
723 | ||
724 | ||
725 | /* | |
726 | * 'main()' - Main entry and processing of driver. | |
727 | */ | |
728 | ||
729 | int /* O - Exit status */ | |
730 | main(int argc, /* I - Number of command-line arguments */ | |
731 | char *argv[]) /* I - Command-line arguments */ | |
732 | { | |
b2e10895 | 733 | int fd; /* File descriptor */ |
5f82b2f5 | 734 | cups_raster_t *ras; /* Raster stream for printing */ |
735 | cups_page_header_t header; /* Page header from file */ | |
5f82b2f5 | 736 | int y; /* Current line */ |
b5cb0608 | 737 | ppd_file_t *ppd; /* PPD file */ |
5f82b2f5 | 738 | |
739 | ||
740 | /* | |
cc787dc1 | 741 | * Make sure status messages are not buffered... |
742 | */ | |
743 | ||
744 | setbuf(stderr, NULL); | |
745 | ||
746 | /* | |
747 | * Check command-line... | |
5f82b2f5 | 748 | */ |
749 | ||
750 | if (argc < 6 || argc > 7) | |
751 | { | |
752 | /* | |
753 | * We don't have the correct number of arguments; write an error message | |
40c1123a | 754 | * and return. |
5f82b2f5 | 755 | */ |
756 | ||
59b77f42 | 757 | fprintf(stderr, _("Usage: %s job-id user title copies options [file]\n"), |
758 | argv[0]); | |
5f82b2f5 | 759 | return (1); |
760 | } | |
761 | ||
762 | /* | |
763 | * Open the page stream... | |
764 | */ | |
765 | ||
766 | if (argc == 7) | |
767 | { | |
b2e10895 | 768 | if ((fd = open(argv[6], O_RDONLY)) == -1) |
5f82b2f5 | 769 | { |
770 | perror("ERROR: Unable to open raster file - "); | |
771 | sleep(1); | |
772 | return (1); | |
773 | } | |
774 | } | |
775 | else | |
2e32fcdb | 776 | fd = 0; |
5f82b2f5 | 777 | |
2e32fcdb | 778 | ras = cupsRasterOpen(fd, CUPS_RASTER_READ); |
5f82b2f5 | 779 | |
780 | /* | |
781 | * Initialize the print device... | |
782 | */ | |
783 | ||
b5cb0608 | 784 | ppd = ppdOpenFile(getenv("PPD")); |
785 | ||
5f82b2f5 | 786 | Setup(); |
787 | ||
788 | /* | |
789 | * Process pages as needed... | |
790 | */ | |
791 | ||
93fee5c4 | 792 | Page = 0; |
5f82b2f5 | 793 | |
794 | while (cupsRasterReadHeader(ras, &header)) | |
795 | { | |
796 | /* | |
797 | * Write a status message with the page number and number of copies. | |
798 | */ | |
799 | ||
93fee5c4 | 800 | Page ++; |
5f82b2f5 | 801 | |
93fee5c4 | 802 | fprintf(stderr, "PAGE: %d %d\n", Page, header.NumCopies); |
5f82b2f5 | 803 | |
804 | /* | |
805 | * Start the page... | |
806 | */ | |
807 | ||
b5cb0608 | 808 | StartPage(ppd, &header); |
5f82b2f5 | 809 | |
810 | /* | |
811 | * Loop for each line on the page... | |
812 | */ | |
813 | ||
814 | for (y = 0; y < header.cupsHeight; y ++) | |
815 | { | |
816 | /* | |
817 | * Let the user know how far we have progressed... | |
818 | */ | |
819 | ||
820 | if ((y & 127) == 0) | |
59b77f42 | 821 | fprintf(stderr, _("INFO: Printing page %d, %d%% complete...\n"), Page, |
5f82b2f5 | 822 | 100 * y / header.cupsHeight); |
823 | ||
824 | /* | |
825 | * Read a line of graphics... | |
826 | */ | |
827 | ||
828 | if (cupsRasterReadPixels(ras, Planes[0], header.cupsBytesPerLine) < 1) | |
829 | break; | |
830 | ||
831 | /* | |
832 | * See if the line is blank; if not, write it to the printer... | |
833 | */ | |
834 | ||
835 | if (Planes[0][0] || | |
836 | memcmp(Planes[0], Planes[0] + 1, header.cupsBytesPerLine - 1)) | |
837 | OutputLine(&header); | |
838 | else | |
839 | Feed ++; | |
840 | } | |
841 | ||
842 | /* | |
843 | * Eject the page... | |
844 | */ | |
845 | ||
358cc876 | 846 | EndPage(); |
5f82b2f5 | 847 | } |
848 | ||
849 | /* | |
850 | * Shutdown the printer... | |
851 | */ | |
852 | ||
853 | Shutdown(); | |
854 | ||
b5cb0608 | 855 | if (ppd) |
856 | ppdClose(ppd); | |
857 | ||
5f82b2f5 | 858 | /* |
859 | * Close the raster stream... | |
860 | */ | |
861 | ||
862 | cupsRasterClose(ras); | |
b2e10895 | 863 | if (fd != 0) |
2e32fcdb | 864 | close(fd); |
5f82b2f5 | 865 | |
866 | /* | |
867 | * If no pages were printed, send an error message... | |
868 | */ | |
869 | ||
93fee5c4 | 870 | if (Page == 0) |
59b77f42 | 871 | fputs(_("ERROR: No pages found!\n"), stderr); |
5f82b2f5 | 872 | else |
59b77f42 | 873 | fputs(_("INFO: Ready to print.\n"), stderr); |
5f82b2f5 | 874 | |
93fee5c4 | 875 | return (Page == 0); |
5f82b2f5 | 876 | } |
877 | ||
878 | ||
879 | /* | |
b2e10895 | 880 | * End of "$Id$". |
5f82b2f5 | 881 | */ |