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