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