]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/raster.c
Merge changes from CUPS 1.6svn-r10112.
[thirdparty/cups.git] / filter / raster.c
1 /*
2 * "$Id: raster.c 7720 2008-07-11 22:46:21Z mike $"
3 *
4 * Raster file routines for CUPS.
5 *
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * This file is part of the CUPS Imaging library.
10 *
11 * These coded instructions, statements, and computer programs are the
12 * property of Apple Inc. and are protected by Federal copyright
13 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
14 * which should have been included with this file. If this file is
15 * file is missing or damaged, see the license at "http://www.cups.org/".
16 *
17 * This file is subject to the Apple OS-Developed Software exception.
18 *
19 * Contents:
20 *
21 * cupsRasterClose() - Close a raster stream.
22 * cupsRasterOpen() - Open a raster stream using a file descriptor.
23 * cupsRasterOpenIO() - Open a raster stream using a callback function.
24 * cupsRasterReadHeader() - Read a raster page header and store it in a
25 * version 1 page header structure.
26 * cupsRasterReadHeader2() - Read a raster page header and store it in a
27 * version 2 page header structure.
28 * cupsRasterReadPixels() - Read raster pixels.
29 * cupsRasterWriteHeader() - Write a raster page header from a version 1
30 * page header structure.
31 * cupsRasterWriteHeader2() - Write a raster page header from a version 2
32 * page header structure.
33 * cupsRasterWritePixels() - Write raster pixels.
34 * cups_raster_read_header() - Read a raster page header.
35 * cups_raster_read() - Read through the raster buffer.
36 * cups_raster_update() - Update the raster header and row count for the
37 * current page.
38 * cups_raster_write() - Write a row of compressed raster data...
39 * cups_read_fd() - Read bytes from a file.
40 * cups_swap() - Swap bytes in raster data...
41 * cups_write_fd() - Write bytes to a file.
42 */
43
44 /*
45 * Include necessary headers...
46 */
47
48 #include <cups/raster-private.h>
49
50
51 /*
52 * Private structures...
53 */
54
55 struct _cups_raster_s /**** Raster stream data ****/
56 {
57 unsigned sync; /* Sync word from start of stream */
58 void *ctx; /* File descriptor */
59 cups_raster_iocb_t iocb; /* IO callback */
60 cups_mode_t mode; /* Read/write mode */
61 cups_page_header2_t header; /* Raster header for current page */
62 int count, /* Current row run-length count */
63 remaining, /* Remaining rows in page image */
64 bpp; /* Bytes per pixel/color */
65 unsigned char *pixels, /* Pixels for current row */
66 *pend, /* End of pixel buffer */
67 *pcurrent; /* Current byte in pixel buffer */
68 int compressed, /* Non-zero if data is compressed */
69 swapped; /* Non-zero if data is byte-swapped */
70 unsigned char *buffer, /* Read/write buffer */
71 *bufptr, /* Current (read) position in buffer */
72 *bufend; /* End of current (read) buffer */
73 size_t bufsize; /* Buffer size */
74 };
75
76
77 /*
78 * Local functions...
79 */
80
81 static int cups_raster_io(cups_raster_t *r, unsigned char *buf, int bytes);
82 static unsigned cups_raster_read_header(cups_raster_t *r);
83 static int cups_raster_read(cups_raster_t *r, unsigned char *buf,
84 int bytes);
85 static void cups_raster_update(cups_raster_t *r);
86 static int cups_raster_write(cups_raster_t *r,
87 const unsigned char *pixels);
88 static ssize_t cups_read_fd(void *ctx, unsigned char *buf, size_t bytes);
89 static void cups_swap(unsigned char *buf, int bytes);
90 static ssize_t cups_write_fd(void *ctx, unsigned char *buf, size_t bytes);
91
92
93 /*
94 * 'cupsRasterClose()' - Close a raster stream.
95 *
96 * The file descriptor associated with the raster stream must be closed
97 * separately as needed.
98 */
99
100 void
101 cupsRasterClose(cups_raster_t *r) /* I - Stream to close */
102 {
103 if (r != NULL)
104 {
105 if (r->buffer)
106 free(r->buffer);
107
108 if (r->pixels)
109 free(r->pixels);
110
111 free(r);
112 }
113 }
114
115
116 /*
117 * 'cupsRasterOpen()' - Open a raster stream using a file descriptor.
118 *
119 * This function associates a raster stream with the given file descriptor.
120 * For most printer driver filters, "fd" will be 0 (stdin). For most raster
121 * image processor (RIP) filters that generate raster data, "fd" will be 1
122 * (stdout).
123 *
124 * When writing raster data, the @code CUPS_RASTER_WRITE@,
125 * @code CUPS_RASTER_WRITE_COMPRESS@, or @code CUPS_RASTER_WRITE_PWG@ mode can
126 * be used - compressed and PWG output is generally 25-50% smaller but adds a
127 * 100-300% execution time overhead.
128 */
129
130 cups_raster_t * /* O - New stream */
131 cupsRasterOpen(int fd, /* I - File descriptor */
132 cups_mode_t mode) /* I - Mode - @code CUPS_RASTER_READ@,
133 @code CUPS_RASTER_WRITE@,
134 @code CUPS_RASTER_WRITE_COMPRESSED@,
135 or @code CUPS_RASTER_WRITE_PWG@ */
136 {
137 if (mode == CUPS_RASTER_READ)
138 return (cupsRasterOpenIO(cups_read_fd, (void *)((intptr_t)fd), mode));
139 else
140 return (cupsRasterOpenIO(cups_write_fd, (void *)((intptr_t)fd), mode));
141 }
142
143
144 /*
145 * 'cupsRasterOpenIO()' - Open a raster stream using a callback function.
146 *
147 * This function associates a raster stream with the given callback function and
148 * context pointer.
149 *
150 * When writing raster data, the @code CUPS_RASTER_WRITE@,
151 * @code CUPS_RASTER_WRITE_COMPRESS@, or @code CUPS_RASTER_WRITE_PWG@ mode can
152 * be used - compressed and PWG output is generally 25-50% smaller but adds a
153 * 100-300% execution time overhead.
154 */
155
156 cups_raster_t * /* O - New stream */
157 cupsRasterOpenIO(
158 cups_raster_iocb_t iocb, /* I - Read/write callback */
159 void *ctx, /* I - Context pointer for callback */
160 cups_mode_t mode) /* I - Mode - @code CUPS_RASTER_READ@,
161 @code CUPS_RASTER_WRITE@,
162 @code CUPS_RASTER_WRITE_COMPRESSED@,
163 or @code CUPS_RASTER_WRITE_PWG@ */
164 {
165 cups_raster_t *r; /* New stream */
166
167
168 _cupsRasterClearError();
169
170 if ((r = calloc(sizeof(cups_raster_t), 1)) == NULL)
171 {
172 _cupsRasterAddError("Unable to allocate memory for raster stream: %s\n",
173 strerror(errno));
174 return (NULL);
175 }
176
177 r->ctx = ctx;
178 r->iocb = iocb;
179 r->mode = mode;
180
181 if (mode == CUPS_RASTER_READ)
182 {
183 /*
184 * Open for read - get sync word...
185 */
186
187 if (cups_raster_io(r, (unsigned char *)&(r->sync), sizeof(r->sync)) !=
188 sizeof(r->sync))
189 {
190 _cupsRasterAddError("Unable to read header from raster stream: %s\n",
191 strerror(errno));
192 free(r);
193 return (NULL);
194 }
195
196 if (r->sync != CUPS_RASTER_SYNC &&
197 r->sync != CUPS_RASTER_REVSYNC &&
198 r->sync != CUPS_RASTER_SYNCv1 &&
199 r->sync != CUPS_RASTER_REVSYNCv1 &&
200 r->sync != CUPS_RASTER_SYNCv2 &&
201 r->sync != CUPS_RASTER_REVSYNCv2)
202 {
203 _cupsRasterAddError("Unknown raster format %08x!\n", r->sync);
204 free(r);
205 return (NULL);
206 }
207
208 if (r->sync == CUPS_RASTER_SYNCv2 ||
209 r->sync == CUPS_RASTER_REVSYNCv2)
210 r->compressed = 1;
211
212 if (r->sync == CUPS_RASTER_REVSYNC ||
213 r->sync == CUPS_RASTER_REVSYNCv1 ||
214 r->sync == CUPS_RASTER_REVSYNCv2)
215 r->swapped = 1;
216
217 DEBUG_printf(("r->swapped=%d, r->sync=%08x\n", r->swapped, r->sync));
218 }
219 else
220 {
221 /*
222 * Open for write - put sync word...
223 */
224
225 switch (mode)
226 {
227 default :
228 case CUPS_RASTER_WRITE :
229 r->sync = CUPS_RASTER_SYNC;
230 break;
231
232 case CUPS_RASTER_WRITE_COMPRESSED :
233 r->compressed = 1;
234 r->sync = CUPS_RASTER_SYNCv2;
235 break;
236
237 case CUPS_RASTER_WRITE_PWG :
238 r->compressed = 1;
239 r->sync = htonl(CUPS_RASTER_SYNC_PWG);
240 r->swapped = r->sync != CUPS_RASTER_SYNC_PWG;
241 break;
242 }
243
244 if (cups_raster_io(r, (unsigned char *)&(r->sync), sizeof(r->sync))
245 < sizeof(r->sync))
246 {
247 _cupsRasterAddError("Unable to write raster stream header: %s\n",
248 strerror(errno));
249 free(r);
250 return (NULL);
251 }
252 }
253
254 return (r);
255 }
256
257
258 /*
259 * 'cupsRasterReadHeader()' - Read a raster page header and store it in a
260 * version 1 page header structure.
261 *
262 * This function is deprecated. Use @link cupsRasterReadHeader2@ instead.
263 *
264 * Version 1 page headers were used in CUPS 1.0 and 1.1 and contain a subset
265 * of the version 2 page header data. This function handles reading version 2
266 * page headers and copying only the version 1 data into the provided buffer.
267 *
268 * @deprecated@
269 */
270
271 unsigned /* O - 1 on success, 0 on failure/end-of-file */
272 cupsRasterReadHeader(
273 cups_raster_t *r, /* I - Raster stream */
274 cups_page_header_t *h) /* I - Pointer to header data */
275 {
276 /*
277 * Get the raster header...
278 */
279
280 if (!cups_raster_read_header(r))
281 return (0);
282
283 /*
284 * Copy the header to the user-supplied buffer...
285 */
286
287 memcpy(h, &(r->header), sizeof(cups_page_header_t));
288
289 return (1);
290 }
291
292
293 /*
294 * 'cupsRasterReadHeader2()' - Read a raster page header and store it in a
295 * version 2 page header structure.
296 *
297 * @since CUPS 1.2/Mac OS X 10.5@
298 */
299
300 unsigned /* O - 1 on success, 0 on failure/end-of-file */
301 cupsRasterReadHeader2(
302 cups_raster_t *r, /* I - Raster stream */
303 cups_page_header2_t *h) /* I - Pointer to header data */
304 {
305 /*
306 * Get the raster header...
307 */
308
309 if (!cups_raster_read_header(r))
310 return (0);
311
312 /*
313 * Copy the header to the user-supplied buffer...
314 */
315
316 memcpy(h, &(r->header), sizeof(cups_page_header2_t));
317
318 return (1);
319 }
320
321
322 /*
323 * 'cupsRasterReadPixels()' - Read raster pixels.
324 *
325 * For best performance, filters should read one or more whole lines.
326 * The "cupsBytesPerLine" value from the page header can be used to allocate
327 * the line buffer and as the number of bytes to read.
328 */
329
330 unsigned /* O - Number of bytes read */
331 cupsRasterReadPixels(cups_raster_t *r, /* I - Raster stream */
332 unsigned char *p, /* I - Pointer to pixel buffer */
333 unsigned len) /* I - Number of bytes to read */
334 {
335 int bytes; /* Bytes read */
336 unsigned cupsBytesPerLine; /* cupsBytesPerLine value */
337 unsigned remaining; /* Bytes remaining */
338 unsigned char *ptr, /* Pointer to read buffer */
339 byte, /* Byte from file */
340 *temp; /* Pointer into buffer */
341 int count; /* Repetition count */
342
343
344 if (r == NULL || r->mode != CUPS_RASTER_READ || r->remaining == 0 ||
345 r->header.cupsBytesPerLine == 0)
346 return (0);
347
348 if (!r->compressed)
349 {
350 /*
351 * Read without compression...
352 */
353
354 r->remaining -= len / r->header.cupsBytesPerLine;
355
356 if (cups_raster_io(r, p, len) < (ssize_t)len)
357 return (0);
358
359 /*
360 * Swap bytes as needed...
361 */
362
363 if (r->swapped &&
364 (r->header.cupsBitsPerColor == 16 ||
365 r->header.cupsBitsPerPixel == 12 ||
366 r->header.cupsBitsPerPixel == 16))
367 cups_swap(p, len);
368
369 /*
370 * Return...
371 */
372
373 return (len);
374 }
375
376 /*
377 * Read compressed data...
378 */
379
380 remaining = len;
381 cupsBytesPerLine = r->header.cupsBytesPerLine;
382
383 while (remaining > 0 && r->remaining > 0)
384 {
385 if (r->count == 0)
386 {
387 /*
388 * Need to read a new row...
389 */
390
391 if (remaining == cupsBytesPerLine)
392 ptr = p;
393 else
394 ptr = r->pixels;
395
396 /*
397 * Read using a modified PackBits compression...
398 */
399
400 if (!cups_raster_read(r, &byte, 1))
401 return (0);
402
403 r->count = byte + 1;
404
405 if (r->count > 1)
406 ptr = r->pixels;
407
408 temp = ptr;
409 bytes = cupsBytesPerLine;
410
411 while (bytes > 0)
412 {
413 /*
414 * Get a new repeat count...
415 */
416
417 if (!cups_raster_read(r, &byte, 1))
418 return (0);
419
420 if (byte & 128)
421 {
422 /*
423 * Copy N literal pixels...
424 */
425
426 count = (257 - byte) * r->bpp;
427
428 if (count > bytes)
429 count = bytes;
430
431 if (!cups_raster_read(r, temp, count))
432 return (0);
433
434 temp += count;
435 bytes -= count;
436 }
437 else
438 {
439 /*
440 * Repeat the next N bytes...
441 */
442
443 count = (byte + 1) * r->bpp;
444 if (count > bytes)
445 count = bytes;
446
447 if (count < r->bpp)
448 break;
449
450 bytes -= count;
451
452 if (!cups_raster_read(r, temp, r->bpp))
453 return (0);
454
455 temp += r->bpp;
456 count -= r->bpp;
457
458 while (count > 0)
459 {
460 memcpy(temp, temp - r->bpp, r->bpp);
461 temp += r->bpp;
462 count -= r->bpp;
463 }
464 }
465 }
466
467 /*
468 * Swap bytes as needed...
469 */
470
471 if ((r->header.cupsBitsPerColor == 16 ||
472 r->header.cupsBitsPerPixel == 12 ||
473 r->header.cupsBitsPerPixel == 16) &&
474 r->swapped)
475 cups_swap(ptr, bytes);
476
477 /*
478 * Update pointers...
479 */
480
481 if (remaining >= cupsBytesPerLine)
482 {
483 bytes = cupsBytesPerLine;
484 r->pcurrent = r->pixels;
485 r->count --;
486 r->remaining --;
487 }
488 else
489 {
490 bytes = remaining;
491 r->pcurrent = r->pixels + bytes;
492 }
493
494 /*
495 * Copy data as needed...
496 */
497
498 if (ptr != p)
499 memcpy(p, ptr, bytes);
500 }
501 else
502 {
503 /*
504 * Copy fragment from buffer...
505 */
506
507 if ((unsigned)(bytes = (int)(r->pend - r->pcurrent)) > remaining)
508 bytes = remaining;
509
510 memcpy(p, r->pcurrent, bytes);
511 r->pcurrent += bytes;
512
513 if (r->pcurrent >= r->pend)
514 {
515 r->pcurrent = r->pixels;
516 r->count --;
517 r->remaining --;
518 }
519 }
520
521 remaining -= bytes;
522 p += bytes;
523 }
524
525 return (len);
526 }
527
528
529 /*
530 * 'cupsRasterWriteHeader()' - Write a raster page header from a version 1 page
531 * header structure.
532 *
533 * This function is deprecated. Use @link cupsRasterWriteHeader2@ instead.
534 *
535 * @deprecated@
536 */
537
538 unsigned /* O - 1 on success, 0 on failure */
539 cupsRasterWriteHeader(
540 cups_raster_t *r, /* I - Raster stream */
541 cups_page_header_t *h) /* I - Raster page header */
542 {
543 if (r == NULL || r->mode == CUPS_RASTER_READ)
544 return (0);
545
546 /*
547 * Make a copy of the header, and compute the number of raster
548 * lines in the page image...
549 */
550
551 memset(&(r->header), 0, sizeof(r->header));
552 memcpy(&(r->header), h, sizeof(cups_page_header_t));
553
554 cups_raster_update(r);
555
556 /*
557 * Write the raster header...
558 */
559
560 if (r->mode == CUPS_RASTER_WRITE_PWG)
561 {
562 /*
563 * PWG raster data is always network byte order with much of the page header
564 * zeroed.
565 */
566
567 cups_page_header2_t fh; /* File page header */
568
569 memset(&fh, 0, sizeof(fh));
570
571 strlcpy(fh.MediaClass, "PwgRaster", sizeof(fh.MediaClass));
572 /* PwgRaster */
573 strlcpy(fh.MediaColor, r->header.MediaColor, sizeof(fh.MediaColor));
574 strlcpy(fh.MediaType, r->header.MediaType, sizeof(fh.MediaType));
575 strlcpy(fh.OutputType, r->header.OutputType, sizeof(fh.OutputType));
576 /* PrintContentType */
577
578 fh.CutMedia = htonl(r->header.CutMedia);
579 fh.Duplex = htonl(r->header.Duplex);
580 fh.HWResolution[0] = htonl(r->header.HWResolution[0]);
581 fh.HWResolution[1] = htonl(r->header.HWResolution[1]);
582 fh.ImagingBoundingBox[0] = htonl(r->header.ImagingBoundingBox[0]);
583 fh.ImagingBoundingBox[1] = htonl(r->header.ImagingBoundingBox[1]);
584 fh.ImagingBoundingBox[2] = htonl(r->header.ImagingBoundingBox[2]);
585 fh.ImagingBoundingBox[3] = htonl(r->header.ImagingBoundingBox[3]);
586 fh.InsertSheet = htonl(r->header.InsertSheet);
587 fh.Jog = htonl(r->header.Jog);
588 fh.LeadingEdge = htonl(r->header.LeadingEdge);
589 fh.ManualFeed = htonl(r->header.ManualFeed);
590 fh.MediaPosition = htonl(r->header.MediaPosition);
591 fh.MediaWeight = htonl(r->header.MediaWeight);
592 fh.NumCopies = htonl(r->header.NumCopies);
593 fh.Orientation = htonl(r->header.Orientation);
594 fh.PageSize[0] = htonl(r->header.PageSize[0]);
595 fh.PageSize[1] = htonl(r->header.PageSize[1]);
596 fh.Tumble = htonl(r->header.Tumble);
597 fh.cupsWidth = htonl(r->header.cupsWidth);
598 fh.cupsHeight = htonl(r->header.cupsHeight);
599 fh.cupsBitsPerColor = htonl(r->header.cupsBitsPerColor);
600 fh.cupsBitsPerPixel = htonl(r->header.cupsBitsPerPixel);
601 fh.cupsBytesPerLine = htonl(r->header.cupsBytesPerLine);
602 fh.cupsColorOrder = htonl(r->header.cupsColorOrder);
603 fh.cupsColorSpace = htonl(r->header.cupsColorSpace);
604 fh.cupsNumColors = htonl(r->header.cupsNumColors);
605 fh.cupsInteger[0] = htonl(r->header.cupsInteger[0]);
606 /* TotalPageCount */
607 fh.cupsInteger[1] = htonl(r->header.cupsInteger[1]);
608 /* CrossFeedTransform */
609 fh.cupsInteger[2] = htonl(r->header.cupsInteger[2]);
610 /* FeedTransform */
611 fh.cupsInteger[3] = htonl(r->header.cupsInteger[3]);
612 /* ImageBoxLeft */
613 fh.cupsInteger[4] = htonl(r->header.cupsInteger[4]);
614 /* ImageBoxTop */
615 fh.cupsInteger[5] = htonl(r->header.cupsInteger[5]);
616 /* ImageBoxRight */
617 fh.cupsInteger[6] = htonl(r->header.cupsInteger[6]);
618 /* ImageBoxBottom */
619 fh.cupsInteger[7] = htonl(r->header.cupsInteger[7]);
620 /* BlackPrimary */
621 fh.cupsInteger[8] = htonl(r->header.cupsInteger[8]);
622 /* PrintQuality */
623 fh.cupsInteger[14] = htonl(r->header.cupsInteger[14]);
624 /* VendorIdentifier */
625 fh.cupsInteger[15] = htonl(r->header.cupsInteger[15]);
626 /* VendorLength */
627
628 memcpy(fh.cupsReal, r->header.cupsReal,
629 sizeof(fh.cupsReal) + sizeof(fh.cupsString));
630 /* VendorData */
631
632 strlcpy(fh.cupsRenderingIntent, r->header.cupsRenderingIntent,
633 sizeof(fh.cupsRenderingIntent));
634 strlcpy(fh.cupsPageSizeName, r->header.cupsPageSizeName,
635 sizeof(fh.cupsPageSizeName));
636
637 return (cups_raster_io(r, (unsigned char *)&fh, sizeof(fh)) == sizeof(fh));
638 }
639 else
640 return (cups_raster_io(r, (unsigned char *)&(r->header), sizeof(r->header))
641 == sizeof(r->header));
642 }
643
644
645 /*
646 * 'cupsRasterWriteHeader2()' - Write a raster page header from a version 2
647 * page header structure.
648 *
649 * The page header can be initialized using @link cupsRasterInterpretPPD@.
650 *
651 * @since CUPS 1.2/Mac OS X 10.5@
652 */
653
654 unsigned /* O - 1 on success, 0 on failure */
655 cupsRasterWriteHeader2(
656 cups_raster_t *r, /* I - Raster stream */
657 cups_page_header2_t *h) /* I - Raster page header */
658 {
659 if (r == NULL || r->mode == CUPS_RASTER_READ)
660 return (0);
661
662 /*
663 * Make a copy of the header, and compute the number of raster
664 * lines in the page image...
665 */
666
667 memcpy(&(r->header), h, sizeof(cups_page_header2_t));
668
669 cups_raster_update(r);
670
671 /*
672 * Write the raster header...
673 */
674
675 if (r->mode == CUPS_RASTER_WRITE_PWG)
676 {
677 /*
678 * PWG raster data is always network byte order with most of the page header
679 * zeroed.
680 */
681
682 cups_page_header2_t fh; /* File page header */
683
684 memset(&fh, 0, sizeof(fh));
685 strlcpy(fh.MediaClass, "PwgRaster", sizeof(fh.MediaClass));
686 strlcpy(fh.MediaColor, r->header.MediaColor, sizeof(fh.MediaColor));
687 strlcpy(fh.MediaType, r->header.MediaType, sizeof(fh.MediaType));
688 strlcpy(fh.OutputType, r->header.OutputType, sizeof(fh.OutputType));
689 strlcpy(fh.cupsRenderingIntent, r->header.cupsRenderingIntent,
690 sizeof(fh.cupsRenderingIntent));
691 strlcpy(fh.cupsPageSizeName, r->header.cupsPageSizeName,
692 sizeof(fh.cupsPageSizeName));
693
694 fh.CutMedia = htonl(r->header.CutMedia);
695 fh.Duplex = htonl(r->header.Duplex);
696 fh.HWResolution[0] = htonl(r->header.HWResolution[0]);
697 fh.HWResolution[1] = htonl(r->header.HWResolution[1]);
698 fh.ImagingBoundingBox[0] = htonl(r->header.ImagingBoundingBox[0]);
699 fh.ImagingBoundingBox[1] = htonl(r->header.ImagingBoundingBox[1]);
700 fh.ImagingBoundingBox[2] = htonl(r->header.ImagingBoundingBox[2]);
701 fh.ImagingBoundingBox[3] = htonl(r->header.ImagingBoundingBox[3]);
702 fh.InsertSheet = htonl(r->header.InsertSheet);
703 fh.Jog = htonl(r->header.Jog);
704 fh.LeadingEdge = htonl(r->header.LeadingEdge);
705 fh.ManualFeed = htonl(r->header.ManualFeed);
706 fh.MediaPosition = htonl(r->header.MediaPosition);
707 fh.MediaWeight = htonl(r->header.MediaWeight);
708 fh.NumCopies = htonl(r->header.NumCopies);
709 fh.Orientation = htonl(r->header.Orientation);
710 fh.PageSize[0] = htonl(r->header.PageSize[0]);
711 fh.PageSize[1] = htonl(r->header.PageSize[1]);
712 fh.Tumble = htonl(r->header.Tumble);
713 fh.cupsWidth = htonl(r->header.cupsWidth);
714 fh.cupsHeight = htonl(r->header.cupsHeight);
715 fh.cupsBitsPerColor = htonl(r->header.cupsBitsPerColor);
716 fh.cupsBitsPerPixel = htonl(r->header.cupsBitsPerPixel);
717 fh.cupsBytesPerLine = htonl(r->header.cupsBytesPerLine);
718 fh.cupsColorOrder = htonl(r->header.cupsColorOrder);
719 fh.cupsColorSpace = htonl(r->header.cupsColorSpace);
720 fh.cupsNumColors = htonl(r->header.cupsNumColors);
721 fh.cupsInteger[0] = htonl(r->header.cupsInteger[0]);
722 fh.cupsInteger[1] = htonl(r->header.cupsInteger[1]);
723 fh.cupsInteger[2] = htonl(r->header.cupsInteger[2]);
724 fh.cupsInteger[3] = htonl((unsigned)(r->header.cupsImagingBBox[0] *
725 r->header.HWResolution[0]));
726 fh.cupsInteger[4] = htonl((unsigned)(r->header.cupsImagingBBox[1] *
727 r->header.HWResolution[1]));
728 fh.cupsInteger[5] = htonl((unsigned)(r->header.cupsImagingBBox[2] *
729 r->header.HWResolution[0]));
730 fh.cupsInteger[6] = htonl((unsigned)(r->header.cupsImagingBBox[3] *
731 r->header.HWResolution[1]));
732 fh.cupsInteger[7] = htonl(0xffffff);
733
734 return (cups_raster_io(r, (unsigned char *)&fh, sizeof(fh)) == sizeof(fh));
735 }
736 else
737 return (cups_raster_io(r, (unsigned char *)&(r->header), sizeof(r->header))
738 == sizeof(r->header));
739 }
740
741
742 /*
743 * 'cupsRasterWritePixels()' - Write raster pixels.
744 *
745 * For best performance, filters should write one or more whole lines.
746 * The "cupsBytesPerLine" value from the page header can be used to allocate
747 * the line buffer and as the number of bytes to write.
748 */
749
750 unsigned /* O - Number of bytes written */
751 cupsRasterWritePixels(cups_raster_t *r, /* I - Raster stream */
752 unsigned char *p, /* I - Bytes to write */
753 unsigned len)/* I - Number of bytes to write */
754 {
755 int bytes; /* Bytes read */
756 unsigned remaining; /* Bytes remaining */
757
758
759 DEBUG_printf(("cupsRasterWritePixels(r=%p, p=%p, len=%u), remaining=%u\n",
760 r, p, len, r->remaining));
761
762 if (r == NULL || r->mode == CUPS_RASTER_READ || r->remaining == 0)
763 return (0);
764
765 if (!r->compressed)
766 {
767 /*
768 * Without compression, just write the raster data raw unless the data needs
769 * to be swapped...
770 */
771
772 r->remaining -= len / r->header.cupsBytesPerLine;
773
774 if (r->swapped &&
775 (r->header.cupsBitsPerColor == 16 ||
776 r->header.cupsBitsPerPixel == 12 ||
777 r->header.cupsBitsPerPixel == 16))
778 {
779 unsigned char *bufptr; /* Pointer into write buffer */
780 unsigned count; /* Remaining count */
781
782 /*
783 * Allocate a write buffer as needed...
784 */
785
786 if ((size_t)len > r->bufsize)
787 {
788 if (r->buffer)
789 bufptr = realloc(r->buffer, len);
790 else
791 bufptr = malloc(len);
792
793 if (!bufptr)
794 return (0);
795
796 r->buffer = bufptr;
797 r->bufsize = len;
798 }
799
800 /*
801 * Byte swap the pixels...
802 */
803
804 for (bufptr = r->buffer, count = len; count > 1; count -= 2, bufptr += 2)
805 {
806 bufptr[1] = *p++;
807 bufptr[0] = *p++;
808 }
809
810 if (count) /* This should never happen... */
811 *bufptr = *p;
812
813 /*
814 * Write the byte-swapped buffer...
815 */
816
817 return (cups_raster_io(r, r->buffer, len));
818 }
819 else
820 return (cups_raster_io(r, p, len));
821 }
822
823 /*
824 * Otherwise, compress each line...
825 */
826
827 for (remaining = len; remaining > 0; remaining -= bytes, p += bytes)
828 {
829 /*
830 * Figure out the number of remaining bytes on the current line...
831 */
832
833 if ((bytes = remaining) > (int)(r->pend - r->pcurrent))
834 bytes = (int)(r->pend - r->pcurrent);
835
836 if (r->count > 0)
837 {
838 /*
839 * Check to see if this line is the same as the previous line...
840 */
841
842 if (memcmp(p, r->pcurrent, bytes))
843 {
844 if (!cups_raster_write(r, r->pixels))
845 return (0);
846
847 r->count = 0;
848 }
849 else
850 {
851 /*
852 * Mark more bytes as the same...
853 */
854
855 r->pcurrent += bytes;
856
857 if (r->pcurrent >= r->pend)
858 {
859 /*
860 * Increase the repeat count...
861 */
862
863 r->count ++;
864 r->pcurrent = r->pixels;
865
866 /*
867 * Flush out this line if it is the last one...
868 */
869
870 r->remaining --;
871
872 if (r->remaining == 0)
873 return (cups_raster_write(r, r->pixels));
874 else if (r->count == 256)
875 {
876 if (cups_raster_write(r, r->pixels) == 0)
877 return (0);
878
879 r->count = 0;
880 }
881 }
882
883 continue;
884 }
885 }
886
887 if (r->count == 0)
888 {
889 /*
890 * Copy the raster data to the buffer...
891 */
892
893 memcpy(r->pcurrent, p, bytes);
894
895 r->pcurrent += bytes;
896
897 if (r->pcurrent >= r->pend)
898 {
899 /*
900 * Increase the repeat count...
901 */
902
903 r->count ++;
904 r->pcurrent = r->pixels;
905
906 /*
907 * Flush out this line if it is the last one...
908 */
909
910 r->remaining --;
911
912 if (r->remaining == 0)
913 return (cups_raster_write(r, r->pixels));
914 }
915 }
916 }
917
918 return (len);
919 }
920
921
922 /*
923 * 'cups_raster_read_header()' - Read a raster page header.
924 */
925
926 static unsigned /* O - 1 on success, 0 on fail */
927 cups_raster_read_header(
928 cups_raster_t *r) /* I - Raster stream */
929 {
930 int len; /* Length for read/swap */
931
932
933 if (r == NULL || r->mode != CUPS_RASTER_READ)
934 return (0);
935
936 /*
937 * Get the length of the raster header...
938 */
939
940 if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1)
941 len = sizeof(cups_page_header_t);
942 else
943 len = sizeof(cups_page_header2_t);
944
945 /*
946 * Read the header...
947 */
948
949 memset(&(r->header), 0, sizeof(r->header));
950
951 if (cups_raster_read(r, (unsigned char *)&(r->header), len) < len)
952 return (0);
953
954 /*
955 * Swap bytes as needed...
956 */
957
958 if (r->swapped)
959 {
960 unsigned *s, /* Current word */
961 temp; /* Temporary copy */
962
963
964 DEBUG_puts("Swapping header bytes...");
965
966 for (len = 81, s = &(r->header.AdvanceDistance);
967 len > 0;
968 len --, s ++)
969 {
970 DEBUG_printf(("%08x =>", *s));
971
972 temp = *s;
973 *s = ((temp & 0xff) << 24) |
974 ((temp & 0xff00) << 8) |
975 ((temp & 0xff0000) >> 8) |
976 ((temp & 0xff000000) >> 24);
977
978 DEBUG_printf((" %08x\n", *s));
979 }
980 }
981
982 /*
983 * Update the header and row count...
984 */
985
986 cups_raster_update(r);
987
988 return (r->header.cupsBytesPerLine != 0 && r->header.cupsHeight != 0);
989 }
990
991
992 /*
993 * 'cups_raster_io()' - Read/write bytes from a context, handling interruptions.
994 */
995
996 static int /* O - Bytes read or -1 */
997 cups_raster_io(cups_raster_t *r, /* I - Raster stream */
998 unsigned char *buf, /* I - Buffer for read/write */
999 int bytes) /* I - Number of bytes to read/write */
1000 {
1001 ssize_t count; /* Number of bytes read/written */
1002 size_t total; /* Total bytes read/written */
1003
1004
1005 DEBUG_printf(("4cups_raster_io(r=%p, buf=%p, bytes=%d)", r, buf, bytes));
1006
1007 for (total = 0; total < (size_t)bytes; total += count, buf += count)
1008 {
1009 count = (*r->iocb)(r->ctx, buf, bytes - total);
1010
1011 DEBUG_printf(("5cups_raster_io: count=%d, total=%d", (int)count,
1012 (int)total));
1013 if (count == 0)
1014 return (0);
1015 else if (count < 0)
1016 return (-1);
1017 }
1018
1019 return ((int)total);
1020 }
1021
1022
1023 /*
1024 * 'cups_raster_read()' - Read through the raster buffer.
1025 */
1026
1027 static int /* O - Number of bytes read */
1028 cups_raster_read(cups_raster_t *r, /* I - Raster stream */
1029 unsigned char *buf, /* I - Buffer */
1030 int bytes) /* I - Number of bytes to read */
1031 {
1032 int count, /* Number of bytes read */
1033 remaining, /* Remaining bytes in buffer */
1034 total; /* Total bytes read */
1035
1036
1037 DEBUG_printf(("cups_raster_read(r=%p, buf=%p, bytes=%d)\n", r, buf, bytes));
1038
1039 if (!r->compressed)
1040 return (cups_raster_io(r, buf, bytes));
1041
1042 /*
1043 * Allocate a read buffer as needed...
1044 */
1045
1046 count = 2 * r->header.cupsBytesPerLine;
1047
1048 if ((size_t)count > r->bufsize)
1049 {
1050 int offset = (int)(r->bufptr - r->buffer);
1051 /* Offset to current start of buffer */
1052 int end = (int)(r->bufend - r->buffer);
1053 /* Offset to current end of buffer */
1054 unsigned char *rptr; /* Pointer in read buffer */
1055
1056 if (r->buffer)
1057 rptr = realloc(r->buffer, count);
1058 else
1059 rptr = malloc(count);
1060
1061 if (!rptr)
1062 return (0);
1063
1064 r->buffer = rptr;
1065 r->bufptr = rptr + offset;
1066 r->bufend = rptr + end;
1067 r->bufsize = count;
1068 }
1069
1070 /*
1071 * Loop until we have read everything...
1072 */
1073
1074 for (total = 0, remaining = (int)(r->bufend - r->bufptr);
1075 total < bytes;
1076 total += count, buf += count)
1077 {
1078 count = bytes - total;
1079
1080 DEBUG_printf(("count=%d, remaining=%d, buf=%p, bufptr=%p, bufend=%p...\n",
1081 count, remaining, buf, r->bufptr, r->bufend));
1082
1083 if (remaining == 0)
1084 {
1085 if (count < 16)
1086 {
1087 /*
1088 * Read into the raster buffer and then copy...
1089 */
1090
1091 remaining = (*r->iocb)(r->ctx, r->buffer, r->bufsize);
1092 if (remaining <= 0)
1093 return (0);
1094
1095 r->bufptr = r->buffer;
1096 r->bufend = r->buffer + remaining;
1097 }
1098 else
1099 {
1100 /*
1101 * Read directly into "buf"...
1102 */
1103
1104 count = (*r->iocb)(r->ctx, buf, count);
1105
1106 if (count <= 0)
1107 return (0);
1108
1109 continue;
1110 }
1111 }
1112
1113 /*
1114 * Copy bytes from raster buffer to "buf"...
1115 */
1116
1117 if (count > remaining)
1118 count = remaining;
1119
1120 if (count == 1)
1121 {
1122 /*
1123 * Copy 1 byte...
1124 */
1125
1126 *buf = *(r->bufptr)++;
1127 remaining --;
1128 }
1129 else if (count < 128)
1130 {
1131 /*
1132 * Copy up to 127 bytes without using memcpy(); this is
1133 * faster because it avoids an extra function call and is
1134 * often further optimized by the compiler...
1135 */
1136
1137 unsigned char *bufptr; /* Temporary buffer pointer */
1138
1139 remaining -= count;
1140
1141 for (bufptr = r->bufptr; count > 0; count --, total ++)
1142 *buf++ = *bufptr++;
1143
1144 r->bufptr = bufptr;
1145 }
1146 else
1147 {
1148 /*
1149 * Use memcpy() for a large read...
1150 */
1151
1152 memcpy(buf, r->bufptr, count);
1153 r->bufptr += count;
1154 remaining -= count;
1155 }
1156 }
1157
1158 return (total);
1159 }
1160
1161
1162 /*
1163 * 'cups_raster_update()' - Update the raster header and row count for the
1164 * current page.
1165 */
1166
1167 static void
1168 cups_raster_update(cups_raster_t *r) /* I - Raster stream */
1169 {
1170 if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1 ||
1171 r->header.cupsNumColors == 0)
1172 {
1173 /*
1174 * Set the "cupsNumColors" field according to the colorspace...
1175 */
1176
1177 switch (r->header.cupsColorSpace)
1178 {
1179 case CUPS_CSPACE_W :
1180 case CUPS_CSPACE_K :
1181 case CUPS_CSPACE_WHITE :
1182 case CUPS_CSPACE_GOLD :
1183 case CUPS_CSPACE_SILVER :
1184 case CUPS_CSPACE_SW :
1185 r->header.cupsNumColors = 1;
1186 break;
1187
1188 case CUPS_CSPACE_RGB :
1189 case CUPS_CSPACE_CMY :
1190 case CUPS_CSPACE_YMC :
1191 case CUPS_CSPACE_CIEXYZ :
1192 case CUPS_CSPACE_CIELab :
1193 case CUPS_CSPACE_SRGB :
1194 case CUPS_CSPACE_ADOBERGB :
1195 case CUPS_CSPACE_ICC1 :
1196 case CUPS_CSPACE_ICC2 :
1197 case CUPS_CSPACE_ICC3 :
1198 case CUPS_CSPACE_ICC4 :
1199 case CUPS_CSPACE_ICC5 :
1200 case CUPS_CSPACE_ICC6 :
1201 case CUPS_CSPACE_ICC7 :
1202 case CUPS_CSPACE_ICC8 :
1203 case CUPS_CSPACE_ICC9 :
1204 case CUPS_CSPACE_ICCA :
1205 case CUPS_CSPACE_ICCB :
1206 case CUPS_CSPACE_ICCC :
1207 case CUPS_CSPACE_ICCD :
1208 case CUPS_CSPACE_ICCE :
1209 case CUPS_CSPACE_ICCF :
1210 r->header.cupsNumColors = 3;
1211 break;
1212
1213 case CUPS_CSPACE_RGBA :
1214 case CUPS_CSPACE_RGBW :
1215 case CUPS_CSPACE_CMYK :
1216 case CUPS_CSPACE_YMCK :
1217 case CUPS_CSPACE_KCMY :
1218 case CUPS_CSPACE_GMCK :
1219 case CUPS_CSPACE_GMCS :
1220 r->header.cupsNumColors = 4;
1221 break;
1222
1223 case CUPS_CSPACE_KCMYcm :
1224 if (r->header.cupsBitsPerPixel < 8)
1225 r->header.cupsNumColors = 6;
1226 else
1227 r->header.cupsNumColors = 4;
1228 break;
1229
1230 case CUPS_CSPACE_DEVICE1 :
1231 case CUPS_CSPACE_DEVICE2 :
1232 case CUPS_CSPACE_DEVICE3 :
1233 case CUPS_CSPACE_DEVICE4 :
1234 case CUPS_CSPACE_DEVICE5 :
1235 case CUPS_CSPACE_DEVICE6 :
1236 case CUPS_CSPACE_DEVICE7 :
1237 case CUPS_CSPACE_DEVICE8 :
1238 case CUPS_CSPACE_DEVICE9 :
1239 case CUPS_CSPACE_DEVICEA :
1240 case CUPS_CSPACE_DEVICEB :
1241 case CUPS_CSPACE_DEVICEC :
1242 case CUPS_CSPACE_DEVICED :
1243 case CUPS_CSPACE_DEVICEE :
1244 case CUPS_CSPACE_DEVICEF :
1245 r->header.cupsNumColors = r->header.cupsColorSpace -
1246 CUPS_CSPACE_DEVICE1 + 1;
1247 break;
1248 }
1249 }
1250
1251 /*
1252 * Set the number of bytes per pixel/color...
1253 */
1254
1255 if (r->header.cupsColorOrder == CUPS_ORDER_CHUNKED)
1256 r->bpp = (r->header.cupsBitsPerPixel + 7) / 8;
1257 else
1258 r->bpp = (r->header.cupsBitsPerColor + 7) / 8;
1259
1260 /*
1261 * Set the number of remaining rows...
1262 */
1263
1264 if (r->header.cupsColorOrder == CUPS_ORDER_PLANAR)
1265 r->remaining = r->header.cupsHeight * r->header.cupsNumColors;
1266 else
1267 r->remaining = r->header.cupsHeight;
1268
1269 /*
1270 * Allocate the compression buffer...
1271 */
1272
1273 if (r->compressed)
1274 {
1275 if (r->pixels != NULL)
1276 free(r->pixels);
1277
1278 r->pixels = calloc(r->header.cupsBytesPerLine, 1);
1279 r->pcurrent = r->pixels;
1280 r->pend = r->pixels + r->header.cupsBytesPerLine;
1281 r->count = 0;
1282 }
1283 }
1284
1285
1286 /*
1287 * 'cups_raster_write()' - Write a row of compressed raster data...
1288 */
1289
1290 static int /* O - Number of bytes written */
1291 cups_raster_write(
1292 cups_raster_t *r, /* I - Raster stream */
1293 const unsigned char *pixels) /* I - Pixel data to write */
1294 {
1295 const unsigned char *start, /* Start of sequence */
1296 *ptr, /* Current pointer in sequence */
1297 *pend, /* End of raster buffer */
1298 *plast; /* Pointer to last pixel */
1299 unsigned char *wptr; /* Pointer into write buffer */
1300 int bpp, /* Bytes per pixel */
1301 count; /* Count */
1302
1303
1304 DEBUG_printf(("cups_raster_write(r=%p, pixels=%p)\n", r, pixels));
1305
1306 /*
1307 * Allocate a write buffer as needed...
1308 */
1309
1310 count = r->header.cupsBytesPerLine * 2;
1311 if ((size_t)count > r->bufsize)
1312 {
1313 if (r->buffer)
1314 wptr = realloc(r->buffer, count);
1315 else
1316 wptr = malloc(count);
1317
1318 if (!wptr)
1319 return (-1);
1320
1321 r->buffer = wptr;
1322 r->bufsize = count;
1323 }
1324
1325 /*
1326 * Write the row repeat count...
1327 */
1328
1329 bpp = r->bpp;
1330 pend = pixels + r->header.cupsBytesPerLine;
1331 plast = pend - bpp;
1332 wptr = r->buffer;
1333 *wptr++ = r->count - 1;
1334
1335 /*
1336 * Write using a modified PackBits compression...
1337 */
1338
1339 for (ptr = pixels; ptr < pend;)
1340 {
1341 start = ptr;
1342 ptr += bpp;
1343
1344 if (ptr == pend)
1345 {
1346 /*
1347 * Encode a single pixel at the end...
1348 */
1349
1350 *wptr++ = 0;
1351 for (count = bpp; count > 0; count --)
1352 *wptr++ = *start++;
1353 }
1354 else if (!memcmp(start, ptr, bpp))
1355 {
1356 /*
1357 * Encode a sequence of repeating pixels...
1358 */
1359
1360 for (count = 2; count < 128 && ptr < plast; count ++, ptr += bpp)
1361 if (memcmp(ptr, ptr + bpp, bpp))
1362 break;
1363
1364 *wptr++ = count - 1;
1365 for (count = bpp; count > 0; count --)
1366 *wptr++ = *ptr++;
1367 }
1368 else
1369 {
1370 /*
1371 * Encode a sequence of non-repeating pixels...
1372 */
1373
1374 for (count = 1; count < 128 && ptr < plast; count ++, ptr += bpp)
1375 if (!memcmp(ptr, ptr + bpp, bpp))
1376 break;
1377
1378 if (ptr >= plast && count < 128)
1379 {
1380 count ++;
1381 ptr += bpp;
1382 }
1383
1384 *wptr++ = 257 - count;
1385
1386 count *= bpp;
1387 memcpy(wptr, start, count);
1388 wptr += count;
1389 }
1390 }
1391
1392 return (cups_raster_io(r, r->buffer, (int)(wptr - r->buffer)));
1393 }
1394
1395
1396 /*
1397 * 'cups_read_fd()' - Read bytes from a file.
1398 */
1399
1400 static ssize_t /* O - Bytes read or -1 */
1401 cups_read_fd(void *ctx, /* I - File descriptor as pointer */
1402 unsigned char *buf, /* I - Buffer for read */
1403 size_t bytes) /* I - Maximum number of bytes to read */
1404 {
1405 int fd = (int)((intptr_t)ctx);
1406 /* File descriptor */
1407 ssize_t count; /* Number of bytes read */
1408
1409
1410 #ifdef WIN32 /* Sigh */
1411 while ((count = read(fd, buf, (unsigned)bytes)) < 0)
1412 #else
1413 while ((count = read(fd, buf, bytes)) < 0)
1414 #endif /* WIN32 */
1415 if (errno != EINTR && errno != EAGAIN)
1416 return (-1);
1417
1418 return (count);
1419 }
1420
1421
1422 /*
1423 * 'cups_swap()' - Swap bytes in raster data...
1424 */
1425
1426 static void
1427 cups_swap(unsigned char *buf, /* I - Buffer to swap */
1428 int bytes) /* I - Number of bytes to swap */
1429 {
1430 unsigned char even, odd; /* Temporary variables */
1431
1432
1433 bytes /= 2;
1434
1435 while (bytes > 0)
1436 {
1437 even = buf[0];
1438 odd = buf[1];
1439 buf[0] = odd;
1440 buf[1] = even;
1441
1442 buf += 2;
1443 bytes --;
1444 }
1445 }
1446
1447
1448 /*
1449 * 'cups_write_fd()' - Write bytes to a file.
1450 */
1451
1452 static ssize_t /* O - Bytes written or -1 */
1453 cups_write_fd(void *ctx, /* I - File descriptor pointer */
1454 unsigned char *buf, /* I - Bytes to write */
1455 size_t bytes) /* I - Number of bytes to write */
1456 {
1457 int fd = (int)((intptr_t)ctx);
1458 /* File descriptor */
1459 ssize_t count; /* Number of bytes written */
1460
1461
1462 #ifdef WIN32 /* Sigh */
1463 while ((count = write(fd, buf, (unsigned)bytes)) < 0)
1464 #else
1465 while ((count = write(fd, buf, bytes)) < 0)
1466 #endif /* WIN32 */
1467 if (errno != EINTR && errno != EAGAIN)
1468 return (-1);
1469
1470 return (count);
1471 }
1472
1473
1474 /*
1475 * End of "$Id: raster.c 7720 2008-07-11 22:46:21Z mike $".
1476 */