]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/raster.c
Make sure cupsPageSize is initialized.
[thirdparty/cups.git] / filter / raster.c
1 /*
2 * "$Id$"
3 *
4 * Raster file routines for CUPS.
5 *
6 * Copyright 2007-2015 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
20 /*
21 * Include necessary headers...
22 */
23
24 #include <cups/raster-private.h>
25 #ifdef HAVE_STDINT_H
26 # include <stdint.h>
27 #endif /* HAVE_STDINT_H */
28
29
30 /*
31 * Private structures...
32 */
33
34 struct _cups_raster_s /**** Raster stream data ****/
35 {
36 unsigned sync; /* Sync word from start of stream */
37 void *ctx; /* File descriptor */
38 cups_raster_iocb_t iocb; /* IO callback */
39 cups_mode_t mode; /* Read/write mode */
40 cups_page_header2_t header; /* Raster header for current page */
41 unsigned count, /* Current row run-length count */
42 remaining, /* Remaining rows in page image */
43 bpp; /* Bytes per pixel/color */
44 unsigned char *pixels, /* Pixels for current row */
45 *pend, /* End of pixel buffer */
46 *pcurrent; /* Current byte in pixel buffer */
47 int compressed, /* Non-zero if data is compressed */
48 swapped; /* Non-zero if data is byte-swapped */
49 unsigned char *buffer, /* Read/write buffer */
50 *bufptr, /* Current (read) position in buffer */
51 *bufend; /* End of current (read) buffer */
52 size_t bufsize; /* Buffer size */
53 #ifdef DEBUG
54 size_t iocount; /* Number of bytes read/written */
55 #endif /* DEBUG */
56 };
57
58
59 /*
60 * Local functions...
61 */
62
63 static ssize_t cups_raster_io(cups_raster_t *r, unsigned char *buf, size_t bytes);
64 static unsigned cups_raster_read_header(cups_raster_t *r);
65 static ssize_t cups_raster_read(cups_raster_t *r, unsigned char *buf,
66 size_t bytes);
67 static int cups_raster_update(cups_raster_t *r);
68 static ssize_t cups_raster_write(cups_raster_t *r,
69 const unsigned char *pixels);
70 static ssize_t cups_read_fd(void *ctx, unsigned char *buf, size_t bytes);
71 static void cups_swap(unsigned char *buf, size_t bytes);
72 static ssize_t cups_write_fd(void *ctx, unsigned char *buf, size_t bytes);
73
74
75 /*
76 * 'cupsRasterClose()' - Close a raster stream.
77 *
78 * The file descriptor associated with the raster stream must be closed
79 * separately as needed.
80 */
81
82 void
83 cupsRasterClose(cups_raster_t *r) /* I - Stream to close */
84 {
85 if (r != NULL)
86 {
87 if (r->buffer)
88 free(r->buffer);
89
90 if (r->pixels)
91 free(r->pixels);
92
93 free(r);
94 }
95 }
96
97
98 /*
99 * 'cupsRasterInitPWGHeader()' - Initialize a page header for PWG Raster output.
100 *
101 * The "media" argument specifies the media to use.
102 *
103 * The "type" argument specifies a "pwg-raster-document-type-supported" value
104 * that controls the color space and bit depth of the raster data.
105 *
106 * The "xres" and "yres" arguments specify the raster resolution in dots per
107 * inch.
108 *
109 * The "sheet_back" argument specifies a "pwg-raster-document-sheet-back" value
110 * to apply for the back side of a page. Pass @code NULL@ for the front side.
111 *
112 * @since CUPS 2.2@
113 */
114
115 int /* O - 1 on success, 0 on failure */
116 cupsRasterInitPWGHeader(
117 cups_page_header2_t *h, /* I - Page header */
118 pwg_media_t *media, /* I - PWG media information */
119 const char *type, /* I - PWG raster type string */
120 int xdpi, /* I - Cross-feed direction (horizontal) resolution */
121 int ydpi, /* I - Feed direction (vertical) resolution */
122 const char *sides, /* I - IPP "sides" option value */
123 const char *sheet_back) /* I - Transform for back side or @code NULL@ for none */
124 {
125 if (!h || !media || !type || xdpi <= 0 || ydpi <= 0)
126 {
127 _cupsRasterAddError("%s", strerror(EINVAL));
128 return (0);
129 }
130
131 /*
132 * Initialize the page header...
133 */
134
135 memset(h, 0, sizeof(cups_page_header2_t));
136
137 strlcpy(h->cupsPageSizeName, media->pwg, sizeof(h->cupsPageSizeName));
138
139 h->PageSize[0] = (unsigned)(72 * media->width / 2540);
140 h->PageSize[1] = (unsigned)(72 * media->length / 2540);
141
142 /* This never gets written but is needed for some applications */
143 h->cupsPageSize[0] = 72.0f * media->width / 2540.0f;
144 h->cupsPageSize[1] = 72.0f * media->length / 2540.0f;
145
146 h->ImagingBoundingBox[2] = h->PageSize[0];
147 h->ImagingBoundingBox[3] = h->PageSize[1];
148
149 h->HWResolution[0] = (unsigned)xdpi;
150 h->HWResolution[1] = (unsigned)ydpi;
151
152 h->cupsWidth = (unsigned)(media->width * xdpi / 2540);
153 h->cupsHeight = (unsigned)(media->length * ydpi / 2540);
154
155 if (h->cupsWidth > 0x00ffffff || h->cupsHeight > 0x00ffffff)
156 {
157 _cupsRasterAddError("Raster dimensions too large.");
158 return (0);
159 }
160
161 h->cupsInteger[CUPS_RASTER_PWG_ImageBoxRight] = h->cupsWidth;
162 h->cupsInteger[CUPS_RASTER_PWG_ImageBoxBottom] = h->cupsHeight;
163
164 /*
165 * Colorspace and bytes per line...
166 */
167
168 if (!strcmp(type, "adobe-rgb_8"))
169 {
170 h->cupsBitsPerColor = 8;
171 h->cupsBitsPerPixel = 24;
172 h->cupsColorSpace = CUPS_CSPACE_ADOBERGB;
173 }
174 else if (!strcmp(type, "adobe-rgb_16"))
175 {
176 h->cupsBitsPerColor = 16;
177 h->cupsBitsPerPixel = 48;
178 h->cupsColorSpace = CUPS_CSPACE_ADOBERGB;
179 }
180 else if (!strcmp(type, "black_1"))
181 {
182 h->cupsBitsPerColor = 1;
183 h->cupsBitsPerPixel = 1;
184 h->cupsColorSpace = CUPS_CSPACE_K;
185 }
186 else if (!strcmp(type, "black_8"))
187 {
188 h->cupsBitsPerColor = 8;
189 h->cupsBitsPerPixel = 8;
190 h->cupsColorSpace = CUPS_CSPACE_K;
191 }
192 else if (!strcmp(type, "black_16"))
193 {
194 h->cupsBitsPerColor = 16;
195 h->cupsBitsPerPixel = 16;
196 h->cupsColorSpace = CUPS_CSPACE_K;
197 }
198 else if (!strcmp(type, "cmyk_8"))
199 {
200 h->cupsBitsPerColor = 8;
201 h->cupsBitsPerPixel = 32;
202 h->cupsColorSpace = CUPS_CSPACE_CMYK;
203 }
204 else if (!strcmp(type, "cmyk_16"))
205 {
206 h->cupsBitsPerColor = 16;
207 h->cupsBitsPerPixel = 64;
208 h->cupsColorSpace = CUPS_CSPACE_CMYK;
209 }
210 else if (!strncmp(type, "device", 6) && type[6] >= '1' && type[6] <= '9')
211 {
212 int ncolors, bits; /* Number of colors and bits */
213
214
215 if (sscanf(type, "device%d_%d", &ncolors, &bits) != 2 || ncolors > 15 || (bits != 8 && bits != 16))
216 {
217 _cupsRasterAddError("Unsupported raster type \'%s\'.", type);
218 return (0);
219 }
220
221 h->cupsBitsPerColor = (unsigned)bits;
222 h->cupsBitsPerPixel = (unsigned)(ncolors * bits);
223 h->cupsColorSpace = (cups_cspace_t)(CUPS_CSPACE_DEVICE1 + ncolors - 1);
224 }
225 else if (!strcmp(type, "rgb_8"))
226 {
227 h->cupsBitsPerColor = 8;
228 h->cupsBitsPerPixel = 24;
229 h->cupsColorSpace = CUPS_CSPACE_RGB;
230 }
231 else if (!strcmp(type, "rgb_16"))
232 {
233 h->cupsBitsPerColor = 16;
234 h->cupsBitsPerPixel = 48;
235 h->cupsColorSpace = CUPS_CSPACE_RGB;
236 }
237 else if (!strcmp(type, "sgray_1"))
238 {
239 h->cupsBitsPerColor = 1;
240 h->cupsBitsPerPixel = 1;
241 h->cupsColorSpace = CUPS_CSPACE_SW;
242 }
243 else if (!strcmp(type, "sgray_8"))
244 {
245 h->cupsBitsPerColor = 8;
246 h->cupsBitsPerPixel = 8;
247 h->cupsColorSpace = CUPS_CSPACE_SW;
248 }
249 else if (!strcmp(type, "sgray_16"))
250 {
251 h->cupsBitsPerColor = 16;
252 h->cupsBitsPerPixel = 16;
253 h->cupsColorSpace = CUPS_CSPACE_SW;
254 }
255 else if (!strcmp(type, "srgb_8"))
256 {
257 h->cupsBitsPerColor = 8;
258 h->cupsBitsPerPixel = 24;
259 h->cupsColorSpace = CUPS_CSPACE_SRGB;
260 }
261 else if (!strcmp(type, "srgb_16"))
262 {
263 h->cupsBitsPerColor = 16;
264 h->cupsBitsPerPixel = 48;
265 h->cupsColorSpace = CUPS_CSPACE_SRGB;
266 }
267 else
268 {
269 _cupsRasterAddError("Unsupported raster type \'%s\'.", type);
270 return (0);
271 }
272
273 h->cupsColorOrder = CUPS_ORDER_CHUNKED;
274 h->cupsNumColors = h->cupsBitsPerPixel / h->cupsBitsPerColor;
275 h->cupsBytesPerLine = (h->cupsWidth * h->cupsBitsPerPixel + 7) / 8;
276
277 /*
278 * Duplex support...
279 */
280
281 h->cupsInteger[CUPS_RASTER_PWG_CrossFeedTransform] = 1;
282 h->cupsInteger[CUPS_RASTER_PWG_FeedTransform] = 1;
283
284 if (sides)
285 {
286 if (!strcmp(sides, "two-sided-long-edge"))
287 {
288 h->Duplex = 1;
289 }
290 else if (!strcmp(sides, "two-sided-short-edge"))
291 {
292 h->Duplex = 1;
293 h->Tumble = 1;
294 }
295 else if (strcmp(sides, "one-sided"))
296 {
297 _cupsRasterAddError("Unsupported sides value \'%s\'.", sides);
298 return (0);
299 }
300
301 if (sheet_back)
302 {
303 if (!strcmp(sheet_back, "flipped"))
304 {
305 if (h->Tumble)
306 h->cupsInteger[CUPS_RASTER_PWG_CrossFeedTransform] = 0xffffffffU;
307 else
308 h->cupsInteger[CUPS_RASTER_PWG_FeedTransform] = 0xffffffffU;
309 }
310 else if (!strcmp(sheet_back, "manual-tumble"))
311 {
312 if (h->Tumble)
313 {
314 h->cupsInteger[CUPS_RASTER_PWG_CrossFeedTransform] = 0xffffffffU;
315 h->cupsInteger[CUPS_RASTER_PWG_FeedTransform] = 0xffffffffU;
316 }
317 }
318 else if (!strcmp(sheet_back, "rotated"))
319 {
320 if (!h->Tumble)
321 {
322 h->cupsInteger[CUPS_RASTER_PWG_CrossFeedTransform] = 0xffffffffU;
323 h->cupsInteger[CUPS_RASTER_PWG_FeedTransform] = 0xffffffffU;
324 }
325 }
326 else if (strcmp(sheet_back, "normal"))
327 {
328 _cupsRasterAddError("Unsupported sheet_back value \'%s\'.", sheet_back);
329 return (0);
330 }
331 }
332 }
333
334 return (1);
335 }
336
337
338 /*
339 * 'cupsRasterOpen()' - Open a raster stream using a file descriptor.
340 *
341 * This function associates a raster stream with the given file descriptor.
342 * For most printer driver filters, "fd" will be 0 (stdin). For most raster
343 * image processor (RIP) filters that generate raster data, "fd" will be 1
344 * (stdout).
345 *
346 * When writing raster data, the @code CUPS_RASTER_WRITE@,
347 * @code CUPS_RASTER_WRITE_COMPRESS@, or @code CUPS_RASTER_WRITE_PWG@ mode can
348 * be used - compressed and PWG output is generally 25-50% smaller but adds a
349 * 100-300% execution time overhead.
350 */
351
352 cups_raster_t * /* O - New stream */
353 cupsRasterOpen(int fd, /* I - File descriptor */
354 cups_mode_t mode) /* I - Mode - @code CUPS_RASTER_READ@,
355 @code CUPS_RASTER_WRITE@,
356 @code CUPS_RASTER_WRITE_COMPRESSED@,
357 or @code CUPS_RASTER_WRITE_PWG@ */
358 {
359 if (mode == CUPS_RASTER_READ)
360 return (cupsRasterOpenIO(cups_read_fd, (void *)((intptr_t)fd), mode));
361 else
362 return (cupsRasterOpenIO(cups_write_fd, (void *)((intptr_t)fd), mode));
363 }
364
365
366 /*
367 * 'cupsRasterOpenIO()' - Open a raster stream using a callback function.
368 *
369 * This function associates a raster stream with the given callback function and
370 * context pointer.
371 *
372 * When writing raster data, the @code CUPS_RASTER_WRITE@,
373 * @code CUPS_RASTER_WRITE_COMPRESS@, or @code CUPS_RASTER_WRITE_PWG@ mode can
374 * be used - compressed and PWG output is generally 25-50% smaller but adds a
375 * 100-300% execution time overhead.
376 */
377
378 cups_raster_t * /* O - New stream */
379 cupsRasterOpenIO(
380 cups_raster_iocb_t iocb, /* I - Read/write callback */
381 void *ctx, /* I - Context pointer for callback */
382 cups_mode_t mode) /* I - Mode - @code CUPS_RASTER_READ@,
383 @code CUPS_RASTER_WRITE@,
384 @code CUPS_RASTER_WRITE_COMPRESSED@,
385 or @code CUPS_RASTER_WRITE_PWG@ */
386 {
387 cups_raster_t *r; /* New stream */
388
389
390 _cupsRasterClearError();
391
392 if ((r = calloc(sizeof(cups_raster_t), 1)) == NULL)
393 {
394 _cupsRasterAddError("Unable to allocate memory for raster stream: %s\n",
395 strerror(errno));
396 return (NULL);
397 }
398
399 r->ctx = ctx;
400 r->iocb = iocb;
401 r->mode = mode;
402
403 if (mode == CUPS_RASTER_READ)
404 {
405 /*
406 * Open for read - get sync word...
407 */
408
409 if (cups_raster_io(r, (unsigned char *)&(r->sync), sizeof(r->sync)) !=
410 sizeof(r->sync))
411 {
412 _cupsRasterAddError("Unable to read header from raster stream: %s\n",
413 strerror(errno));
414 free(r);
415 return (NULL);
416 }
417
418 if (r->sync != CUPS_RASTER_SYNC &&
419 r->sync != CUPS_RASTER_REVSYNC &&
420 r->sync != CUPS_RASTER_SYNCv1 &&
421 r->sync != CUPS_RASTER_REVSYNCv1 &&
422 r->sync != CUPS_RASTER_SYNCv2 &&
423 r->sync != CUPS_RASTER_REVSYNCv2)
424 {
425 _cupsRasterAddError("Unknown raster format %08x!\n", r->sync);
426 free(r);
427 return (NULL);
428 }
429
430 if (r->sync == CUPS_RASTER_SYNCv2 ||
431 r->sync == CUPS_RASTER_REVSYNCv2)
432 r->compressed = 1;
433
434 if (r->sync == CUPS_RASTER_REVSYNC ||
435 r->sync == CUPS_RASTER_REVSYNCv1 ||
436 r->sync == CUPS_RASTER_REVSYNCv2)
437 r->swapped = 1;
438
439 DEBUG_printf(("1cupsRasterOpenIO: r->swapped=%d, r->sync=%08x\n", r->swapped, r->sync));
440 }
441 else
442 {
443 /*
444 * Open for write - put sync word...
445 */
446
447 switch (mode)
448 {
449 default :
450 case CUPS_RASTER_WRITE :
451 r->sync = CUPS_RASTER_SYNC;
452 break;
453
454 case CUPS_RASTER_WRITE_COMPRESSED :
455 r->compressed = 1;
456 r->sync = CUPS_RASTER_SYNCv2;
457 break;
458
459 case CUPS_RASTER_WRITE_PWG :
460 r->compressed = 1;
461 r->sync = htonl(CUPS_RASTER_SYNC_PWG);
462 r->swapped = r->sync != CUPS_RASTER_SYNC_PWG;
463 break;
464 }
465
466 if (cups_raster_io(r, (unsigned char *)&(r->sync), sizeof(r->sync)) < (ssize_t)sizeof(r->sync))
467 {
468 _cupsRasterAddError("Unable to write raster stream header: %s\n",
469 strerror(errno));
470 free(r);
471 return (NULL);
472 }
473 }
474
475 return (r);
476 }
477
478
479 /*
480 * 'cupsRasterReadHeader()' - Read a raster page header and store it in a
481 * version 1 page header structure.
482 *
483 * This function is deprecated. Use @link cupsRasterReadHeader2@ instead.
484 *
485 * Version 1 page headers were used in CUPS 1.0 and 1.1 and contain a subset
486 * of the version 2 page header data. This function handles reading version 2
487 * page headers and copying only the version 1 data into the provided buffer.
488 *
489 * @deprecated@
490 */
491
492 unsigned /* O - 1 on success, 0 on failure/end-of-file */
493 cupsRasterReadHeader(
494 cups_raster_t *r, /* I - Raster stream */
495 cups_page_header_t *h) /* I - Pointer to header data */
496 {
497 /*
498 * Get the raster header...
499 */
500
501 if (!cups_raster_read_header(r))
502 {
503 memset(h, 0, sizeof(cups_page_header_t));
504 return (0);
505 }
506
507 /*
508 * Copy the header to the user-supplied buffer...
509 */
510
511 memcpy(h, &(r->header), sizeof(cups_page_header_t));
512
513 return (1);
514 }
515
516
517 /*
518 * 'cupsRasterReadHeader2()' - Read a raster page header and store it in a
519 * version 2 page header structure.
520 *
521 * @since CUPS 1.2/OS X 10.5@
522 */
523
524 unsigned /* O - 1 on success, 0 on failure/end-of-file */
525 cupsRasterReadHeader2(
526 cups_raster_t *r, /* I - Raster stream */
527 cups_page_header2_t *h) /* I - Pointer to header data */
528 {
529 /*
530 * Get the raster header...
531 */
532
533 DEBUG_printf(("cupsRasterReadHeader2(r=%p, h=%p)", r, h));
534
535 if (!cups_raster_read_header(r))
536 {
537 memset(h, 0, sizeof(cups_page_header2_t));
538 return (0);
539 }
540
541 /*
542 * Copy the header to the user-supplied buffer...
543 */
544
545 memcpy(h, &(r->header), sizeof(cups_page_header2_t));
546
547 return (1);
548 }
549
550
551 /*
552 * 'cupsRasterReadPixels()' - Read raster pixels.
553 *
554 * For best performance, filters should read one or more whole lines.
555 * The "cupsBytesPerLine" value from the page header can be used to allocate
556 * the line buffer and as the number of bytes to read.
557 */
558
559 unsigned /* O - Number of bytes read */
560 cupsRasterReadPixels(cups_raster_t *r, /* I - Raster stream */
561 unsigned char *p, /* I - Pointer to pixel buffer */
562 unsigned len) /* I - Number of bytes to read */
563 {
564 ssize_t bytes; /* Bytes read */
565 unsigned cupsBytesPerLine; /* cupsBytesPerLine value */
566 unsigned remaining; /* Bytes remaining */
567 unsigned char *ptr, /* Pointer to read buffer */
568 byte, /* Byte from file */
569 *temp; /* Pointer into buffer */
570 unsigned count; /* Repetition count */
571
572
573 DEBUG_printf(("cupsRasterReadPixels(r=%p, p=%p, len=%u)", r, p, len));
574
575 if (r == NULL || r->mode != CUPS_RASTER_READ || r->remaining == 0 ||
576 r->header.cupsBytesPerLine == 0)
577 {
578 DEBUG_puts("1cupsRasterReadPixels: Returning 0.");
579 return (0);
580 }
581
582 DEBUG_printf(("1cupsRasterReadPixels: compressed=%d, remaining=%u", r->compressed, r->remaining));
583
584 if (!r->compressed)
585 {
586 /*
587 * Read without compression...
588 */
589
590 r->remaining -= len / r->header.cupsBytesPerLine;
591
592 if (cups_raster_io(r, p, len) < (ssize_t)len)
593 {
594 DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0.");
595 return (0);
596 }
597
598 /*
599 * Swap bytes as needed...
600 */
601
602 if (r->swapped &&
603 (r->header.cupsBitsPerColor == 16 ||
604 r->header.cupsBitsPerPixel == 12 ||
605 r->header.cupsBitsPerPixel == 16))
606 cups_swap(p, len);
607
608 /*
609 * Return...
610 */
611
612 DEBUG_printf(("1cupsRasterReadPixels: Returning %u", len));
613
614 return (len);
615 }
616
617 /*
618 * Read compressed data...
619 */
620
621 remaining = len;
622 cupsBytesPerLine = r->header.cupsBytesPerLine;
623
624 while (remaining > 0 && r->remaining > 0)
625 {
626 if (r->count == 0)
627 {
628 /*
629 * Need to read a new row...
630 */
631
632 if (remaining == cupsBytesPerLine)
633 ptr = p;
634 else
635 ptr = r->pixels;
636
637 /*
638 * Read using a modified PackBits compression...
639 */
640
641 if (!cups_raster_read(r, &byte, 1))
642 {
643 DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0.");
644 return (0);
645 }
646
647 r->count = (unsigned)byte + 1;
648
649 if (r->count > 1)
650 ptr = r->pixels;
651
652 temp = ptr;
653 bytes = (ssize_t)cupsBytesPerLine;
654
655 while (bytes > 0)
656 {
657 /*
658 * Get a new repeat count...
659 */
660
661 if (!cups_raster_read(r, &byte, 1))
662 {
663 DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0.");
664 return (0);
665 }
666
667 if (byte & 128)
668 {
669 /*
670 * Copy N literal pixels...
671 */
672
673 count = (unsigned)(257 - byte) * r->bpp;
674
675 if (count > (unsigned)bytes)
676 count = (unsigned)bytes;
677
678 if (!cups_raster_read(r, temp, count))
679 {
680 DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0.");
681 return (0);
682 }
683
684 temp += count;
685 bytes -= count;
686 }
687 else
688 {
689 /*
690 * Repeat the next N bytes...
691 */
692
693 count = ((unsigned)byte + 1) * r->bpp;
694 if (count > (unsigned)bytes)
695 count = (unsigned)bytes;
696
697 if (count < r->bpp)
698 break;
699
700 bytes -= count;
701
702 if (!cups_raster_read(r, temp, r->bpp))
703 {
704 DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0.");
705 return (0);
706 }
707
708 temp += r->bpp;
709 count -= r->bpp;
710
711 while (count > 0)
712 {
713 memcpy(temp, temp - r->bpp, r->bpp);
714 temp += r->bpp;
715 count -= r->bpp;
716 }
717 }
718 }
719
720 /*
721 * Swap bytes as needed...
722 */
723
724 if ((r->header.cupsBitsPerColor == 16 ||
725 r->header.cupsBitsPerPixel == 12 ||
726 r->header.cupsBitsPerPixel == 16) &&
727 r->swapped)
728 cups_swap(ptr, (size_t)bytes);
729
730 /*
731 * Update pointers...
732 */
733
734 if (remaining >= cupsBytesPerLine)
735 {
736 bytes = (ssize_t)cupsBytesPerLine;
737 r->pcurrent = r->pixels;
738 r->count --;
739 r->remaining --;
740 }
741 else
742 {
743 bytes = (ssize_t)remaining;
744 r->pcurrent = r->pixels + bytes;
745 }
746
747 /*
748 * Copy data as needed...
749 */
750
751 if (ptr != p)
752 memcpy(p, ptr, (size_t)bytes);
753 }
754 else
755 {
756 /*
757 * Copy fragment from buffer...
758 */
759
760 if ((unsigned)(bytes = (int)(r->pend - r->pcurrent)) > remaining)
761 bytes = (ssize_t)remaining;
762
763 memcpy(p, r->pcurrent, (size_t)bytes);
764 r->pcurrent += bytes;
765
766 if (r->pcurrent >= r->pend)
767 {
768 r->pcurrent = r->pixels;
769 r->count --;
770 r->remaining --;
771 }
772 }
773
774 remaining -= (unsigned)bytes;
775 p += bytes;
776 }
777
778 DEBUG_printf(("1cupsRasterReadPixels: Returning %u", len));
779
780 return (len);
781 }
782
783
784 /*
785 * 'cupsRasterWriteHeader()' - Write a raster page header from a version 1 page
786 * header structure.
787 *
788 * This function is deprecated. Use @link cupsRasterWriteHeader2@ instead.
789 *
790 * @deprecated@
791 */
792
793 unsigned /* O - 1 on success, 0 on failure */
794 cupsRasterWriteHeader(
795 cups_raster_t *r, /* I - Raster stream */
796 cups_page_header_t *h) /* I - Raster page header */
797 {
798 if (r == NULL || r->mode == CUPS_RASTER_READ)
799 return (0);
800
801 /*
802 * Make a copy of the header, and compute the number of raster
803 * lines in the page image...
804 */
805
806 memset(&(r->header), 0, sizeof(r->header));
807 memcpy(&(r->header), h, sizeof(cups_page_header_t));
808
809 if (!cups_raster_update(r))
810 return (0);
811
812 /*
813 * Write the raster header...
814 */
815
816 if (r->mode == CUPS_RASTER_WRITE_PWG)
817 {
818 /*
819 * PWG raster data is always network byte order with much of the page header
820 * zeroed.
821 */
822
823 cups_page_header2_t fh; /* File page header */
824
825 memset(&fh, 0, sizeof(fh));
826
827 strlcpy(fh.MediaClass, "PwgRaster", sizeof(fh.MediaClass));
828 /* PwgRaster */
829 strlcpy(fh.MediaColor, r->header.MediaColor, sizeof(fh.MediaColor));
830 strlcpy(fh.MediaType, r->header.MediaType, sizeof(fh.MediaType));
831 strlcpy(fh.OutputType, r->header.OutputType, sizeof(fh.OutputType));
832 /* PrintContentType */
833
834 fh.CutMedia = htonl(r->header.CutMedia);
835 fh.Duplex = htonl(r->header.Duplex);
836 fh.HWResolution[0] = htonl(r->header.HWResolution[0]);
837 fh.HWResolution[1] = htonl(r->header.HWResolution[1]);
838 fh.ImagingBoundingBox[0] = htonl(r->header.ImagingBoundingBox[0]);
839 fh.ImagingBoundingBox[1] = htonl(r->header.ImagingBoundingBox[1]);
840 fh.ImagingBoundingBox[2] = htonl(r->header.ImagingBoundingBox[2]);
841 fh.ImagingBoundingBox[3] = htonl(r->header.ImagingBoundingBox[3]);
842 fh.InsertSheet = htonl(r->header.InsertSheet);
843 fh.Jog = htonl(r->header.Jog);
844 fh.LeadingEdge = htonl(r->header.LeadingEdge);
845 fh.ManualFeed = htonl(r->header.ManualFeed);
846 fh.MediaPosition = htonl(r->header.MediaPosition);
847 fh.MediaWeight = htonl(r->header.MediaWeight);
848 fh.NumCopies = htonl(r->header.NumCopies);
849 fh.Orientation = htonl(r->header.Orientation);
850 fh.PageSize[0] = htonl(r->header.PageSize[0]);
851 fh.PageSize[1] = htonl(r->header.PageSize[1]);
852 fh.Tumble = htonl(r->header.Tumble);
853 fh.cupsWidth = htonl(r->header.cupsWidth);
854 fh.cupsHeight = htonl(r->header.cupsHeight);
855 fh.cupsBitsPerColor = htonl(r->header.cupsBitsPerColor);
856 fh.cupsBitsPerPixel = htonl(r->header.cupsBitsPerPixel);
857 fh.cupsBytesPerLine = htonl(r->header.cupsBytesPerLine);
858 fh.cupsColorOrder = htonl(r->header.cupsColorOrder);
859 fh.cupsColorSpace = htonl(r->header.cupsColorSpace);
860 fh.cupsNumColors = htonl(r->header.cupsNumColors);
861 fh.cupsInteger[0] = htonl(r->header.cupsInteger[0]);
862 /* TotalPageCount */
863 fh.cupsInteger[1] = htonl(r->header.cupsInteger[1]);
864 /* CrossFeedTransform */
865 fh.cupsInteger[2] = htonl(r->header.cupsInteger[2]);
866 /* FeedTransform */
867 fh.cupsInteger[3] = htonl(r->header.cupsInteger[3]);
868 /* ImageBoxLeft */
869 fh.cupsInteger[4] = htonl(r->header.cupsInteger[4]);
870 /* ImageBoxTop */
871 fh.cupsInteger[5] = htonl(r->header.cupsInteger[5]);
872 /* ImageBoxRight */
873 fh.cupsInteger[6] = htonl(r->header.cupsInteger[6]);
874 /* ImageBoxBottom */
875 fh.cupsInteger[7] = htonl(r->header.cupsInteger[7]);
876 /* BlackPrimary */
877 fh.cupsInteger[8] = htonl(r->header.cupsInteger[8]);
878 /* PrintQuality */
879 fh.cupsInteger[14] = htonl(r->header.cupsInteger[14]);
880 /* VendorIdentifier */
881 fh.cupsInteger[15] = htonl(r->header.cupsInteger[15]);
882 /* VendorLength */
883
884 void *dst = fh.cupsReal; /* Bypass bogus compiler warning */
885 void *src = r->header.cupsReal;
886 memcpy(dst, src, sizeof(fh.cupsReal) + sizeof(fh.cupsString));
887 /* VendorData */
888
889 strlcpy(fh.cupsRenderingIntent, r->header.cupsRenderingIntent,
890 sizeof(fh.cupsRenderingIntent));
891 strlcpy(fh.cupsPageSizeName, r->header.cupsPageSizeName,
892 sizeof(fh.cupsPageSizeName));
893
894 return (cups_raster_io(r, (unsigned char *)&fh, sizeof(fh)) == sizeof(fh));
895 }
896 else
897 return (cups_raster_io(r, (unsigned char *)&(r->header), sizeof(r->header))
898 == sizeof(r->header));
899 }
900
901
902 /*
903 * 'cupsRasterWriteHeader2()' - Write a raster page header from a version 2
904 * page header structure.
905 *
906 * The page header can be initialized using @link cupsRasterInterpretPPD@.
907 *
908 * @since CUPS 1.2/OS X 10.5@
909 */
910
911 unsigned /* O - 1 on success, 0 on failure */
912 cupsRasterWriteHeader2(
913 cups_raster_t *r, /* I - Raster stream */
914 cups_page_header2_t *h) /* I - Raster page header */
915 {
916 if (r == NULL || r->mode == CUPS_RASTER_READ)
917 return (0);
918
919 /*
920 * Make a copy of the header, and compute the number of raster
921 * lines in the page image...
922 */
923
924 memcpy(&(r->header), h, sizeof(cups_page_header2_t));
925
926 if (!cups_raster_update(r))
927 return (0);
928
929 /*
930 * Write the raster header...
931 */
932
933 if (r->mode == CUPS_RASTER_WRITE_PWG)
934 {
935 /*
936 * PWG raster data is always network byte order with most of the page header
937 * zeroed.
938 */
939
940 cups_page_header2_t fh; /* File page header */
941
942 memset(&fh, 0, sizeof(fh));
943 strlcpy(fh.MediaClass, "PwgRaster", sizeof(fh.MediaClass));
944 strlcpy(fh.MediaColor, r->header.MediaColor, sizeof(fh.MediaColor));
945 strlcpy(fh.MediaType, r->header.MediaType, sizeof(fh.MediaType));
946 strlcpy(fh.OutputType, r->header.OutputType, sizeof(fh.OutputType));
947 strlcpy(fh.cupsRenderingIntent, r->header.cupsRenderingIntent,
948 sizeof(fh.cupsRenderingIntent));
949 strlcpy(fh.cupsPageSizeName, r->header.cupsPageSizeName,
950 sizeof(fh.cupsPageSizeName));
951
952 fh.CutMedia = htonl(r->header.CutMedia);
953 fh.Duplex = htonl(r->header.Duplex);
954 fh.HWResolution[0] = htonl(r->header.HWResolution[0]);
955 fh.HWResolution[1] = htonl(r->header.HWResolution[1]);
956 fh.ImagingBoundingBox[0] = htonl(r->header.ImagingBoundingBox[0]);
957 fh.ImagingBoundingBox[1] = htonl(r->header.ImagingBoundingBox[1]);
958 fh.ImagingBoundingBox[2] = htonl(r->header.ImagingBoundingBox[2]);
959 fh.ImagingBoundingBox[3] = htonl(r->header.ImagingBoundingBox[3]);
960 fh.InsertSheet = htonl(r->header.InsertSheet);
961 fh.Jog = htonl(r->header.Jog);
962 fh.LeadingEdge = htonl(r->header.LeadingEdge);
963 fh.ManualFeed = htonl(r->header.ManualFeed);
964 fh.MediaPosition = htonl(r->header.MediaPosition);
965 fh.MediaWeight = htonl(r->header.MediaWeight);
966 fh.NumCopies = htonl(r->header.NumCopies);
967 fh.Orientation = htonl(r->header.Orientation);
968 fh.PageSize[0] = htonl(r->header.PageSize[0]);
969 fh.PageSize[1] = htonl(r->header.PageSize[1]);
970 fh.Tumble = htonl(r->header.Tumble);
971 fh.cupsWidth = htonl(r->header.cupsWidth);
972 fh.cupsHeight = htonl(r->header.cupsHeight);
973 fh.cupsBitsPerColor = htonl(r->header.cupsBitsPerColor);
974 fh.cupsBitsPerPixel = htonl(r->header.cupsBitsPerPixel);
975 fh.cupsBytesPerLine = htonl(r->header.cupsBytesPerLine);
976 fh.cupsColorOrder = htonl(r->header.cupsColorOrder);
977 fh.cupsColorSpace = htonl(r->header.cupsColorSpace);
978 fh.cupsNumColors = htonl(r->header.cupsNumColors);
979 fh.cupsInteger[0] = htonl(r->header.cupsInteger[0]);
980 fh.cupsInteger[1] = htonl(r->header.cupsInteger[1]);
981 fh.cupsInteger[2] = htonl(r->header.cupsInteger[2]);
982 fh.cupsInteger[3] = htonl((unsigned)(r->header.cupsImagingBBox[0] * r->header.HWResolution[0] / 72.0));
983 fh.cupsInteger[4] = htonl((unsigned)(r->header.cupsImagingBBox[1] * r->header.HWResolution[1] / 72.0));
984 fh.cupsInteger[5] = htonl((unsigned)(r->header.cupsImagingBBox[2] * r->header.HWResolution[0] / 72.0));
985 fh.cupsInteger[6] = htonl((unsigned)(r->header.cupsImagingBBox[3] * r->header.HWResolution[1] / 72.0));
986 fh.cupsInteger[7] = htonl(0xffffff);
987
988 return (cups_raster_io(r, (unsigned char *)&fh, sizeof(fh)) == sizeof(fh));
989 }
990 else
991 return (cups_raster_io(r, (unsigned char *)&(r->header), sizeof(r->header))
992 == sizeof(r->header));
993 }
994
995
996 /*
997 * 'cupsRasterWritePixels()' - Write raster pixels.
998 *
999 * For best performance, filters should write one or more whole lines.
1000 * The "cupsBytesPerLine" value from the page header can be used to allocate
1001 * the line buffer and as the number of bytes to write.
1002 */
1003
1004 unsigned /* O - Number of bytes written */
1005 cupsRasterWritePixels(cups_raster_t *r, /* I - Raster stream */
1006 unsigned char *p, /* I - Bytes to write */
1007 unsigned len)/* I - Number of bytes to write */
1008 {
1009 ssize_t bytes; /* Bytes read */
1010 unsigned remaining; /* Bytes remaining */
1011
1012
1013 DEBUG_printf(("cupsRasterWritePixels(r=%p, p=%p, len=%u), remaining=%u\n",
1014 r, p, len, r->remaining));
1015
1016 if (r == NULL || r->mode == CUPS_RASTER_READ || r->remaining == 0)
1017 return (0);
1018
1019 if (!r->compressed)
1020 {
1021 /*
1022 * Without compression, just write the raster data raw unless the data needs
1023 * to be swapped...
1024 */
1025
1026 r->remaining -= len / r->header.cupsBytesPerLine;
1027
1028 if (r->swapped &&
1029 (r->header.cupsBitsPerColor == 16 ||
1030 r->header.cupsBitsPerPixel == 12 ||
1031 r->header.cupsBitsPerPixel == 16))
1032 {
1033 unsigned char *bufptr; /* Pointer into write buffer */
1034 unsigned count; /* Remaining count */
1035
1036 /*
1037 * Allocate a write buffer as needed...
1038 */
1039
1040 if ((size_t)len > r->bufsize)
1041 {
1042 if (r->buffer)
1043 bufptr = realloc(r->buffer, len);
1044 else
1045 bufptr = malloc(len);
1046
1047 if (!bufptr)
1048 return (0);
1049
1050 r->buffer = bufptr;
1051 r->bufsize = len;
1052 }
1053
1054 /*
1055 * Byte swap the pixels...
1056 */
1057
1058 for (bufptr = r->buffer, count = len; count > 1; count -= 2, bufptr += 2)
1059 {
1060 bufptr[1] = *p++;
1061 bufptr[0] = *p++;
1062 }
1063
1064 if (count) /* This should never happen... */
1065 *bufptr = *p;
1066
1067 /*
1068 * Write the byte-swapped buffer...
1069 */
1070
1071 bytes = cups_raster_io(r, r->buffer, len);
1072 }
1073 else
1074 bytes = cups_raster_io(r, p, len);
1075
1076 if (bytes < len)
1077 return (0);
1078 else
1079 return (len);
1080 }
1081
1082 /*
1083 * Otherwise, compress each line...
1084 */
1085
1086 for (remaining = len; remaining > 0; remaining -= (unsigned)bytes, p += bytes)
1087 {
1088 /*
1089 * Figure out the number of remaining bytes on the current line...
1090 */
1091
1092 if ((bytes = (ssize_t)remaining) > (ssize_t)(r->pend - r->pcurrent))
1093 bytes = (ssize_t)(r->pend - r->pcurrent);
1094
1095 if (r->count > 0)
1096 {
1097 /*
1098 * Check to see if this line is the same as the previous line...
1099 */
1100
1101 if (memcmp(p, r->pcurrent, (size_t)bytes))
1102 {
1103 if (cups_raster_write(r, r->pixels) <= 0)
1104 return (0);
1105
1106 r->count = 0;
1107 }
1108 else
1109 {
1110 /*
1111 * Mark more bytes as the same...
1112 */
1113
1114 r->pcurrent += bytes;
1115
1116 if (r->pcurrent >= r->pend)
1117 {
1118 /*
1119 * Increase the repeat count...
1120 */
1121
1122 r->count ++;
1123 r->pcurrent = r->pixels;
1124
1125 /*
1126 * Flush out this line if it is the last one...
1127 */
1128
1129 r->remaining --;
1130
1131 if (r->remaining == 0)
1132 {
1133 if (cups_raster_write(r, r->pixels) <= 0)
1134 return (0);
1135 else
1136 return (len);
1137 }
1138 else if (r->count == 256)
1139 {
1140 if (cups_raster_write(r, r->pixels) <= 0)
1141 return (0);
1142
1143 r->count = 0;
1144 }
1145 }
1146
1147 continue;
1148 }
1149 }
1150
1151 if (r->count == 0)
1152 {
1153 /*
1154 * Copy the raster data to the buffer...
1155 */
1156
1157 memcpy(r->pcurrent, p, (size_t)bytes);
1158
1159 r->pcurrent += bytes;
1160
1161 if (r->pcurrent >= r->pend)
1162 {
1163 /*
1164 * Increase the repeat count...
1165 */
1166
1167 r->count ++;
1168 r->pcurrent = r->pixels;
1169
1170 /*
1171 * Flush out this line if it is the last one...
1172 */
1173
1174 r->remaining --;
1175
1176 if (r->remaining == 0)
1177 {
1178 if (cups_raster_write(r, r->pixels) <= 0)
1179 return (0);
1180 }
1181 }
1182 }
1183 }
1184
1185 return (len);
1186 }
1187
1188
1189 /*
1190 * 'cups_raster_read_header()' - Read a raster page header.
1191 */
1192
1193 static unsigned /* O - 1 on success, 0 on fail */
1194 cups_raster_read_header(
1195 cups_raster_t *r) /* I - Raster stream */
1196 {
1197 size_t len; /* Length for read/swap */
1198
1199
1200 DEBUG_printf(("3cups_raster_read_header(r=%p), r->mode=%d", r, r ? r->mode : 0));
1201
1202 if (r == NULL || r->mode != CUPS_RASTER_READ)
1203 return (0);
1204
1205 DEBUG_printf(("4cups_raster_read_header: r->iocount=" CUPS_LLFMT, CUPS_LLCAST r->iocount));
1206
1207 /*
1208 * Get the length of the raster header...
1209 */
1210
1211 if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1)
1212 len = sizeof(cups_page_header_t);
1213 else
1214 len = sizeof(cups_page_header2_t);
1215
1216 DEBUG_printf(("4cups_raster_read_header: len=%d", (int)len));
1217
1218 /*
1219 * Read the header...
1220 */
1221
1222 memset(&(r->header), 0, sizeof(r->header));
1223
1224 if (cups_raster_read(r, (unsigned char *)&(r->header), len) < (ssize_t)len)
1225 {
1226 DEBUG_printf(("4cups_raster_read_header: EOF, r->iocount=" CUPS_LLFMT, CUPS_LLCAST r->iocount));
1227 return (0);
1228 }
1229
1230 /*
1231 * Swap bytes as needed...
1232 */
1233
1234 if (r->swapped)
1235 {
1236 unsigned *s, /* Current word */
1237 temp; /* Temporary copy */
1238
1239
1240 DEBUG_puts("4cups_raster_read_header: Swapping header bytes.");
1241
1242 for (len = 81, s = &(r->header.AdvanceDistance);
1243 len > 0;
1244 len --, s ++)
1245 {
1246 temp = *s;
1247 *s = ((temp & 0xff) << 24) |
1248 ((temp & 0xff00) << 8) |
1249 ((temp & 0xff0000) >> 8) |
1250 ((temp & 0xff000000) >> 24);
1251
1252 DEBUG_printf(("4cups_raster_read_header: %08x => %08x", temp, *s));
1253 }
1254 }
1255
1256 /*
1257 * Update the header and row count...
1258 */
1259
1260 if (!cups_raster_update(r))
1261 return (0);
1262
1263 DEBUG_printf(("4cups_raster_read_header: cupsBitsPerPixel=%u, cupsBitsPerColor=%u, cupsBytesPerLine=%u, cupsWidth=%u, cupsHeight=%u, r->bpp=%d", r->header.cupsBitsPerPixel, r->header.cupsBitsPerColor, r->header.cupsBytesPerLine, r->header.cupsWidth, r->header.cupsHeight, r->bpp));
1264
1265 return (r->header.cupsBitsPerPixel > 0 && r->header.cupsBitsPerPixel <= 240 && r->header.cupsBitsPerColor > 0 && r->header.cupsBitsPerColor <= 16 && r->header.cupsBytesPerLine > 0 && r->header.cupsBytesPerLine <= 0x7fffffff && r->header.cupsHeight != 0 && (r->header.cupsBytesPerLine % r->bpp) == 0);
1266 }
1267
1268
1269 /*
1270 * 'cups_raster_io()' - Read/write bytes from a context, handling interruptions.
1271 */
1272
1273 static ssize_t /* O - Bytes read/write or -1 */
1274 cups_raster_io(cups_raster_t *r, /* I - Raster stream */
1275 unsigned char *buf, /* I - Buffer for read/write */
1276 size_t bytes) /* I - Number of bytes to read/write */
1277 {
1278 ssize_t count, /* Number of bytes read/written */
1279 total; /* Total bytes read/written */
1280
1281
1282 DEBUG_printf(("5cups_raster_io(r=%p, buf=%p, bytes=" CUPS_LLFMT ")", r, buf, CUPS_LLCAST bytes));
1283
1284 for (total = 0; total < (ssize_t)bytes; total += count, buf += count)
1285 {
1286 count = (*r->iocb)(r->ctx, buf, bytes - (size_t)total);
1287
1288 DEBUG_printf(("6cups_raster_io: count=%d, total=%d", (int)count, (int)total));
1289 if (count == 0)
1290 {
1291 DEBUG_puts("6cups_raster_io: Returning 0.");
1292 return (0);
1293 }
1294 else if (count < 0)
1295 {
1296 DEBUG_puts("6cups_raster_io: Returning -1 on error.");
1297 return (-1);
1298 }
1299
1300 #ifdef DEBUG
1301 r->iocount += (size_t)count;
1302 #endif /* DEBUG */
1303 }
1304
1305 DEBUG_printf(("6cups_raster_io: Returning " CUPS_LLFMT ".", CUPS_LLCAST total));
1306
1307 return (total);
1308 }
1309
1310
1311 /*
1312 * 'cups_raster_read()' - Read through the raster buffer.
1313 */
1314
1315 static ssize_t /* O - Number of bytes read */
1316 cups_raster_read(cups_raster_t *r, /* I - Raster stream */
1317 unsigned char *buf, /* I - Buffer */
1318 size_t bytes) /* I - Number of bytes to read */
1319 {
1320 ssize_t count, /* Number of bytes read */
1321 remaining, /* Remaining bytes in buffer */
1322 total; /* Total bytes read */
1323
1324
1325 DEBUG_printf(("5cups_raster_read(r=%p, buf=%p, bytes=" CUPS_LLFMT ")\n", r, buf, CUPS_LLCAST bytes));
1326
1327 if (!r->compressed)
1328 return (cups_raster_io(r, buf, bytes));
1329
1330 /*
1331 * Allocate a read buffer as needed...
1332 */
1333
1334 count = (ssize_t)(2 * r->header.cupsBytesPerLine);
1335 if (count < 65536)
1336 count = 65536;
1337
1338 if ((size_t)count > r->bufsize)
1339 {
1340 ssize_t offset = r->bufptr - r->buffer;
1341 /* Offset to current start of buffer */
1342 ssize_t end = r->bufend - r->buffer;/* Offset to current end of buffer */
1343 unsigned char *rptr; /* Pointer in read buffer */
1344
1345 if (r->buffer)
1346 rptr = realloc(r->buffer, (size_t)count);
1347 else
1348 rptr = malloc((size_t)count);
1349
1350 if (!rptr)
1351 return (0);
1352
1353 r->buffer = rptr;
1354 r->bufptr = rptr + offset;
1355 r->bufend = rptr + end;
1356 r->bufsize = (size_t)count;
1357 }
1358
1359 /*
1360 * Loop until we have read everything...
1361 */
1362
1363 for (total = 0, remaining = (int)(r->bufend - r->bufptr);
1364 total < (ssize_t)bytes;
1365 total += count, buf += count)
1366 {
1367 count = (ssize_t)bytes - total;
1368
1369 DEBUG_printf(("6cups_raster_read: count=" CUPS_LLFMT ", remaining=" CUPS_LLFMT ", buf=%p, bufptr=%p, bufend=%p", CUPS_LLCAST count, CUPS_LLCAST remaining, buf, r->bufptr, r->bufend));
1370
1371 if (remaining == 0)
1372 {
1373 if (count < 16)
1374 {
1375 /*
1376 * Read into the raster buffer and then copy...
1377 */
1378
1379 remaining = (*r->iocb)(r->ctx, r->buffer, r->bufsize);
1380 if (remaining <= 0)
1381 return (0);
1382
1383 r->bufptr = r->buffer;
1384 r->bufend = r->buffer + remaining;
1385
1386 #ifdef DEBUG
1387 r->iocount += (size_t)remaining;
1388 #endif /* DEBUG */
1389 }
1390 else
1391 {
1392 /*
1393 * Read directly into "buf"...
1394 */
1395
1396 count = (*r->iocb)(r->ctx, buf, (size_t)count);
1397
1398 if (count <= 0)
1399 return (0);
1400
1401 #ifdef DEBUG
1402 r->iocount += (size_t)count;
1403 #endif /* DEBUG */
1404
1405 continue;
1406 }
1407 }
1408
1409 /*
1410 * Copy bytes from raster buffer to "buf"...
1411 */
1412
1413 if (count > remaining)
1414 count = remaining;
1415
1416 if (count == 1)
1417 {
1418 /*
1419 * Copy 1 byte...
1420 */
1421
1422 *buf = *(r->bufptr)++;
1423 remaining --;
1424 }
1425 else if (count < 128)
1426 {
1427 /*
1428 * Copy up to 127 bytes without using memcpy(); this is
1429 * faster because it avoids an extra function call and is
1430 * often further optimized by the compiler...
1431 */
1432
1433 unsigned char *bufptr; /* Temporary buffer pointer */
1434
1435 remaining -= count;
1436
1437 for (bufptr = r->bufptr; count > 0; count --, total ++)
1438 *buf++ = *bufptr++;
1439
1440 r->bufptr = bufptr;
1441 }
1442 else
1443 {
1444 /*
1445 * Use memcpy() for a large read...
1446 */
1447
1448 memcpy(buf, r->bufptr, (size_t)count);
1449 r->bufptr += count;
1450 remaining -= count;
1451 }
1452 }
1453
1454 DEBUG_printf(("6cups_raster_read: Returning %ld", (long)total));
1455
1456 return (total);
1457 }
1458
1459
1460 /*
1461 * 'cups_raster_update()' - Update the raster header and row count for the
1462 * current page.
1463 */
1464
1465 static int /* O - 1 on success, 0 on failure */
1466 cups_raster_update(cups_raster_t *r) /* I - Raster stream */
1467 {
1468 if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1 ||
1469 r->header.cupsNumColors == 0)
1470 {
1471 /*
1472 * Set the "cupsNumColors" field according to the colorspace...
1473 */
1474
1475 switch (r->header.cupsColorSpace)
1476 {
1477 case CUPS_CSPACE_W :
1478 case CUPS_CSPACE_K :
1479 case CUPS_CSPACE_WHITE :
1480 case CUPS_CSPACE_GOLD :
1481 case CUPS_CSPACE_SILVER :
1482 case CUPS_CSPACE_SW :
1483 r->header.cupsNumColors = 1;
1484 break;
1485
1486 case CUPS_CSPACE_RGB :
1487 case CUPS_CSPACE_CMY :
1488 case CUPS_CSPACE_YMC :
1489 case CUPS_CSPACE_CIEXYZ :
1490 case CUPS_CSPACE_CIELab :
1491 case CUPS_CSPACE_SRGB :
1492 case CUPS_CSPACE_ADOBERGB :
1493 case CUPS_CSPACE_ICC1 :
1494 case CUPS_CSPACE_ICC2 :
1495 case CUPS_CSPACE_ICC3 :
1496 case CUPS_CSPACE_ICC4 :
1497 case CUPS_CSPACE_ICC5 :
1498 case CUPS_CSPACE_ICC6 :
1499 case CUPS_CSPACE_ICC7 :
1500 case CUPS_CSPACE_ICC8 :
1501 case CUPS_CSPACE_ICC9 :
1502 case CUPS_CSPACE_ICCA :
1503 case CUPS_CSPACE_ICCB :
1504 case CUPS_CSPACE_ICCC :
1505 case CUPS_CSPACE_ICCD :
1506 case CUPS_CSPACE_ICCE :
1507 case CUPS_CSPACE_ICCF :
1508 r->header.cupsNumColors = 3;
1509 break;
1510
1511 case CUPS_CSPACE_RGBA :
1512 case CUPS_CSPACE_RGBW :
1513 case CUPS_CSPACE_CMYK :
1514 case CUPS_CSPACE_YMCK :
1515 case CUPS_CSPACE_KCMY :
1516 case CUPS_CSPACE_GMCK :
1517 case CUPS_CSPACE_GMCS :
1518 r->header.cupsNumColors = 4;
1519 break;
1520
1521 case CUPS_CSPACE_KCMYcm :
1522 if (r->header.cupsBitsPerPixel < 8)
1523 r->header.cupsNumColors = 6;
1524 else
1525 r->header.cupsNumColors = 4;
1526 break;
1527
1528 case CUPS_CSPACE_DEVICE1 :
1529 case CUPS_CSPACE_DEVICE2 :
1530 case CUPS_CSPACE_DEVICE3 :
1531 case CUPS_CSPACE_DEVICE4 :
1532 case CUPS_CSPACE_DEVICE5 :
1533 case CUPS_CSPACE_DEVICE6 :
1534 case CUPS_CSPACE_DEVICE7 :
1535 case CUPS_CSPACE_DEVICE8 :
1536 case CUPS_CSPACE_DEVICE9 :
1537 case CUPS_CSPACE_DEVICEA :
1538 case CUPS_CSPACE_DEVICEB :
1539 case CUPS_CSPACE_DEVICEC :
1540 case CUPS_CSPACE_DEVICED :
1541 case CUPS_CSPACE_DEVICEE :
1542 case CUPS_CSPACE_DEVICEF :
1543 r->header.cupsNumColors = r->header.cupsColorSpace -
1544 CUPS_CSPACE_DEVICE1 + 1;
1545 break;
1546
1547 default :
1548 /* Unknown color space */
1549 return (0);
1550 }
1551 }
1552
1553 /*
1554 * Set the number of bytes per pixel/color...
1555 */
1556
1557 if (r->header.cupsColorOrder == CUPS_ORDER_CHUNKED)
1558 r->bpp = (r->header.cupsBitsPerPixel + 7) / 8;
1559 else
1560 r->bpp = (r->header.cupsBitsPerColor + 7) / 8;
1561
1562 if (r->bpp == 0)
1563 r->bpp = 1;
1564
1565 /*
1566 * Set the number of remaining rows...
1567 */
1568
1569 if (r->header.cupsColorOrder == CUPS_ORDER_PLANAR)
1570 r->remaining = r->header.cupsHeight * r->header.cupsNumColors;
1571 else
1572 r->remaining = r->header.cupsHeight;
1573
1574 /*
1575 * Allocate the compression buffer...
1576 */
1577
1578 if (r->compressed)
1579 {
1580 if (r->pixels != NULL)
1581 free(r->pixels);
1582
1583 if ((r->pixels = calloc(r->header.cupsBytesPerLine, 1)) == NULL)
1584 {
1585 r->pcurrent = NULL;
1586 r->pend = NULL;
1587 r->count = 0;
1588
1589 return (0);
1590 }
1591
1592 r->pcurrent = r->pixels;
1593 r->pend = r->pixels + r->header.cupsBytesPerLine;
1594 r->count = 0;
1595 }
1596
1597 return (1);
1598 }
1599
1600
1601 /*
1602 * 'cups_raster_write()' - Write a row of compressed raster data...
1603 */
1604
1605 static ssize_t /* O - Number of bytes written */
1606 cups_raster_write(
1607 cups_raster_t *r, /* I - Raster stream */
1608 const unsigned char *pixels) /* I - Pixel data to write */
1609 {
1610 const unsigned char *start, /* Start of sequence */
1611 *ptr, /* Current pointer in sequence */
1612 *pend, /* End of raster buffer */
1613 *plast; /* Pointer to last pixel */
1614 unsigned char *wptr; /* Pointer into write buffer */
1615 unsigned bpp, /* Bytes per pixel */
1616 count; /* Count */
1617
1618
1619 DEBUG_printf(("3cups_raster_write(r=%p, pixels=%p)\n", r, pixels));
1620
1621 /*
1622 * Allocate a write buffer as needed...
1623 */
1624
1625 count = r->header.cupsBytesPerLine * 2;
1626 if (count < 65536)
1627 count = 65536;
1628
1629 if ((size_t)count > r->bufsize)
1630 {
1631 if (r->buffer)
1632 wptr = realloc(r->buffer, count);
1633 else
1634 wptr = malloc(count);
1635
1636 if (!wptr)
1637 {
1638 DEBUG_printf(("4cups_raster_write: Unable to allocate " CUPS_LLFMT " bytes for raster buffer: %s", CUPS_LLCAST count, strerror(errno)));
1639 return (-1);
1640 }
1641
1642 r->buffer = wptr;
1643 r->bufsize = count;
1644 }
1645
1646 /*
1647 * Write the row repeat count...
1648 */
1649
1650 bpp = r->bpp;
1651 pend = pixels + r->header.cupsBytesPerLine;
1652 plast = pend - bpp;
1653 wptr = r->buffer;
1654 *wptr++ = (unsigned char)(r->count - 1);
1655
1656 /*
1657 * Write using a modified PackBits compression...
1658 */
1659
1660 for (ptr = pixels; ptr < pend;)
1661 {
1662 start = ptr;
1663 ptr += bpp;
1664
1665 if (ptr == pend)
1666 {
1667 /*
1668 * Encode a single pixel at the end...
1669 */
1670
1671 *wptr++ = 0;
1672 for (count = bpp; count > 0; count --)
1673 *wptr++ = *start++;
1674 }
1675 else if (!memcmp(start, ptr, bpp))
1676 {
1677 /*
1678 * Encode a sequence of repeating pixels...
1679 */
1680
1681 for (count = 2; count < 128 && ptr < plast; count ++, ptr += bpp)
1682 if (memcmp(ptr, ptr + bpp, bpp))
1683 break;
1684
1685 *wptr++ = (unsigned char)(count - 1);
1686 for (count = bpp; count > 0; count --)
1687 *wptr++ = *ptr++;
1688 }
1689 else
1690 {
1691 /*
1692 * Encode a sequence of non-repeating pixels...
1693 */
1694
1695 for (count = 1; count < 128 && ptr < plast; count ++, ptr += bpp)
1696 if (!memcmp(ptr, ptr + bpp, bpp))
1697 break;
1698
1699 if (ptr >= plast && count < 128)
1700 {
1701 count ++;
1702 ptr += bpp;
1703 }
1704
1705 *wptr++ = (unsigned char)(257 - count);
1706
1707 count *= bpp;
1708 memcpy(wptr, start, count);
1709 wptr += count;
1710 }
1711 }
1712
1713 DEBUG_printf(("4cups_raster_write: Writing " CUPS_LLFMT " bytes.", CUPS_LLCAST (wptr - r->buffer)));
1714
1715 return (cups_raster_io(r, r->buffer, (size_t)(wptr - r->buffer)));
1716 }
1717
1718
1719 /*
1720 * 'cups_read_fd()' - Read bytes from a file.
1721 */
1722
1723 static ssize_t /* O - Bytes read or -1 */
1724 cups_read_fd(void *ctx, /* I - File descriptor as pointer */
1725 unsigned char *buf, /* I - Buffer for read */
1726 size_t bytes) /* I - Maximum number of bytes to read */
1727 {
1728 int fd = (int)((intptr_t)ctx);
1729 /* File descriptor */
1730 ssize_t count; /* Number of bytes read */
1731
1732
1733 #ifdef WIN32 /* Sigh */
1734 while ((count = read(fd, buf, (unsigned)bytes)) < 0)
1735 #else
1736 while ((count = read(fd, buf, bytes)) < 0)
1737 #endif /* WIN32 */
1738 if (errno != EINTR && errno != EAGAIN)
1739 {
1740 DEBUG_printf(("4cups_read_fd: %s", strerror(errno)));
1741 return (-1);
1742 }
1743
1744 DEBUG_printf(("4cups_read_fd: Returning %d bytes.", (int)count));
1745
1746 return (count);
1747 }
1748
1749
1750 /*
1751 * 'cups_swap()' - Swap bytes in raster data...
1752 */
1753
1754 static void
1755 cups_swap(unsigned char *buf, /* I - Buffer to swap */
1756 size_t bytes) /* I - Number of bytes to swap */
1757 {
1758 unsigned char even, odd; /* Temporary variables */
1759
1760
1761 bytes /= 2;
1762
1763 while (bytes > 0)
1764 {
1765 even = buf[0];
1766 odd = buf[1];
1767 buf[0] = odd;
1768 buf[1] = even;
1769
1770 buf += 2;
1771 bytes --;
1772 }
1773 }
1774
1775
1776 /*
1777 * 'cups_write_fd()' - Write bytes to a file.
1778 */
1779
1780 static ssize_t /* O - Bytes written or -1 */
1781 cups_write_fd(void *ctx, /* I - File descriptor pointer */
1782 unsigned char *buf, /* I - Bytes to write */
1783 size_t bytes) /* I - Number of bytes to write */
1784 {
1785 int fd = (int)((intptr_t)ctx);
1786 /* File descriptor */
1787 ssize_t count; /* Number of bytes written */
1788
1789
1790 #ifdef WIN32 /* Sigh */
1791 while ((count = write(fd, buf, (unsigned)bytes)) < 0)
1792 #else
1793 while ((count = write(fd, buf, bytes)) < 0)
1794 #endif /* WIN32 */
1795 if (errno != EINTR && errno != EAGAIN)
1796 {
1797 DEBUG_printf(("4cups_write_fd: %s", strerror(errno)));
1798 return (-1);
1799 }
1800
1801 return (count);
1802 }
1803
1804
1805 /*
1806 * End of "$Id$".
1807 */