]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/raster.c
ec3033899ce9acd4228d04eef57a11d311321f99
[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 bytes = cups_raster_io(r, r->buffer, len);
830 }
831 else
832 bytes = cups_raster_io(r, p, len);
833
834 if (bytes < len)
835 return (0);
836 else
837 return (len);
838 }
839
840 /*
841 * Otherwise, compress each line...
842 */
843
844 for (remaining = len; remaining > 0; remaining -= (unsigned)bytes, p += bytes)
845 {
846 /*
847 * Figure out the number of remaining bytes on the current line...
848 */
849
850 if ((bytes = (ssize_t)remaining) > (ssize_t)(r->pend - r->pcurrent))
851 bytes = (ssize_t)(r->pend - r->pcurrent);
852
853 if (r->count > 0)
854 {
855 /*
856 * Check to see if this line is the same as the previous line...
857 */
858
859 if (memcmp(p, r->pcurrent, (size_t)bytes))
860 {
861 if (cups_raster_write(r, r->pixels) <= 0)
862 return (0);
863
864 r->count = 0;
865 }
866 else
867 {
868 /*
869 * Mark more bytes as the same...
870 */
871
872 r->pcurrent += bytes;
873
874 if (r->pcurrent >= r->pend)
875 {
876 /*
877 * Increase the repeat count...
878 */
879
880 r->count ++;
881 r->pcurrent = r->pixels;
882
883 /*
884 * Flush out this line if it is the last one...
885 */
886
887 r->remaining --;
888
889 if (r->remaining == 0)
890 {
891 if (cups_raster_write(r, r->pixels) <= 0)
892 return (0);
893 else
894 return (len);
895 }
896 else if (r->count == 256)
897 {
898 if (cups_raster_write(r, r->pixels) <= 0)
899 return (0);
900
901 r->count = 0;
902 }
903 }
904
905 continue;
906 }
907 }
908
909 if (r->count == 0)
910 {
911 /*
912 * Copy the raster data to the buffer...
913 */
914
915 memcpy(r->pcurrent, p, (size_t)bytes);
916
917 r->pcurrent += bytes;
918
919 if (r->pcurrent >= r->pend)
920 {
921 /*
922 * Increase the repeat count...
923 */
924
925 r->count ++;
926 r->pcurrent = r->pixels;
927
928 /*
929 * Flush out this line if it is the last one...
930 */
931
932 r->remaining --;
933
934 if (r->remaining == 0)
935 {
936 if (cups_raster_write(r, r->pixels) <= 0)
937 return (0);
938 }
939 }
940 }
941 }
942
943 return (len);
944 }
945
946
947 /*
948 * 'cups_raster_read_header()' - Read a raster page header.
949 */
950
951 static unsigned /* O - 1 on success, 0 on fail */
952 cups_raster_read_header(
953 cups_raster_t *r) /* I - Raster stream */
954 {
955 size_t len; /* Length for read/swap */
956
957
958 DEBUG_printf(("3cups_raster_read_header(r=%p), r->mode=%d", r, r ? r->mode : 0));
959
960 if (r == NULL || r->mode != CUPS_RASTER_READ)
961 return (0);
962
963 DEBUG_printf(("4cups_raster_read_header: r->iocount=" CUPS_LLFMT, CUPS_LLCAST r->iocount));
964
965 /*
966 * Get the length of the raster header...
967 */
968
969 if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1)
970 len = sizeof(cups_page_header_t);
971 else
972 len = sizeof(cups_page_header2_t);
973
974 DEBUG_printf(("4cups_raster_read_header: len=%d", (int)len));
975
976 /*
977 * Read the header...
978 */
979
980 memset(&(r->header), 0, sizeof(r->header));
981
982 if (cups_raster_read(r, (unsigned char *)&(r->header), len) < (ssize_t)len)
983 {
984 DEBUG_printf(("4cups_raster_read_header: EOF, r->iocount=" CUPS_LLFMT, CUPS_LLCAST r->iocount));
985 return (0);
986 }
987
988 /*
989 * Swap bytes as needed...
990 */
991
992 if (r->swapped)
993 {
994 unsigned *s, /* Current word */
995 temp; /* Temporary copy */
996
997
998 DEBUG_puts("4cups_raster_read_header: Swapping header bytes.");
999
1000 for (len = 81, s = &(r->header.AdvanceDistance);
1001 len > 0;
1002 len --, s ++)
1003 {
1004 temp = *s;
1005 *s = ((temp & 0xff) << 24) |
1006 ((temp & 0xff00) << 8) |
1007 ((temp & 0xff0000) >> 8) |
1008 ((temp & 0xff000000) >> 24);
1009
1010 DEBUG_printf(("4cups_raster_read_header: %08x => %08x", temp, *s));
1011 }
1012 }
1013
1014 /*
1015 * Update the header and row count...
1016 */
1017
1018 cups_raster_update(r);
1019
1020 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));
1021
1022 return (r->header.cupsBitsPerPixel != 0 && r->header.cupsBitsPerColor != 0 && r->header.cupsBytesPerLine != 0 && r->header.cupsHeight != 0 && (r->header.cupsBytesPerLine % r->bpp) == 0);
1023 }
1024
1025
1026 /*
1027 * 'cups_raster_io()' - Read/write bytes from a context, handling interruptions.
1028 */
1029
1030 static ssize_t /* O - Bytes read/write or -1 */
1031 cups_raster_io(cups_raster_t *r, /* I - Raster stream */
1032 unsigned char *buf, /* I - Buffer for read/write */
1033 size_t bytes) /* I - Number of bytes to read/write */
1034 {
1035 ssize_t count, /* Number of bytes read/written */
1036 total; /* Total bytes read/written */
1037
1038
1039 DEBUG_printf(("5cups_raster_io(r=%p, buf=%p, bytes=" CUPS_LLFMT ")", r, buf, CUPS_LLCAST bytes));
1040
1041 for (total = 0; total < (ssize_t)bytes; total += count, buf += count)
1042 {
1043 count = (*r->iocb)(r->ctx, buf, bytes - (size_t)total);
1044
1045 DEBUG_printf(("6cups_raster_io: count=%d, total=%d", (int)count, (int)total));
1046 if (count == 0)
1047 {
1048 DEBUG_puts("6cups_raster_io: Returning 0.");
1049 return (0);
1050 }
1051 else if (count < 0)
1052 {
1053 DEBUG_puts("6cups_raster_io: Returning -1 on error.");
1054 return (-1);
1055 }
1056
1057 #ifdef DEBUG
1058 r->iocount += (size_t)count;
1059 #endif /* DEBUG */
1060 }
1061
1062 DEBUG_printf(("6cups_raster_io: Returning " CUPS_LLFMT ".", CUPS_LLCAST total));
1063
1064 return (total);
1065 }
1066
1067
1068 /*
1069 * 'cups_raster_read()' - Read through the raster buffer.
1070 */
1071
1072 static ssize_t /* O - Number of bytes read */
1073 cups_raster_read(cups_raster_t *r, /* I - Raster stream */
1074 unsigned char *buf, /* I - Buffer */
1075 size_t bytes) /* I - Number of bytes to read */
1076 {
1077 ssize_t count, /* Number of bytes read */
1078 remaining, /* Remaining bytes in buffer */
1079 total; /* Total bytes read */
1080
1081
1082 DEBUG_printf(("5cups_raster_read(r=%p, buf=%p, bytes=" CUPS_LLFMT ")\n", r, buf, CUPS_LLCAST bytes));
1083
1084 if (!r->compressed)
1085 return (cups_raster_io(r, buf, bytes));
1086
1087 /*
1088 * Allocate a read buffer as needed...
1089 */
1090
1091 count = (ssize_t)(2 * r->header.cupsBytesPerLine);
1092 if (count < 65536)
1093 count = 65536;
1094
1095 if ((size_t)count > r->bufsize)
1096 {
1097 ssize_t offset = r->bufptr - r->buffer;
1098 /* Offset to current start of buffer */
1099 ssize_t end = r->bufend - r->buffer;/* Offset to current end of buffer */
1100 unsigned char *rptr; /* Pointer in read buffer */
1101
1102 if (r->buffer)
1103 rptr = realloc(r->buffer, (size_t)count);
1104 else
1105 rptr = malloc((size_t)count);
1106
1107 if (!rptr)
1108 return (0);
1109
1110 r->buffer = rptr;
1111 r->bufptr = rptr + offset;
1112 r->bufend = rptr + end;
1113 r->bufsize = (size_t)count;
1114 }
1115
1116 /*
1117 * Loop until we have read everything...
1118 */
1119
1120 for (total = 0, remaining = (int)(r->bufend - r->bufptr);
1121 total < (ssize_t)bytes;
1122 total += count, buf += count)
1123 {
1124 count = (ssize_t)bytes - total;
1125
1126 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));
1127
1128 if (remaining == 0)
1129 {
1130 if (count < 16)
1131 {
1132 /*
1133 * Read into the raster buffer and then copy...
1134 */
1135
1136 remaining = (*r->iocb)(r->ctx, r->buffer, r->bufsize);
1137 if (remaining <= 0)
1138 return (0);
1139
1140 r->bufptr = r->buffer;
1141 r->bufend = r->buffer + remaining;
1142
1143 #ifdef DEBUG
1144 r->iocount += (size_t)remaining;
1145 #endif /* DEBUG */
1146 }
1147 else
1148 {
1149 /*
1150 * Read directly into "buf"...
1151 */
1152
1153 count = (*r->iocb)(r->ctx, buf, (size_t)count);
1154
1155 if (count <= 0)
1156 return (0);
1157
1158 #ifdef DEBUG
1159 r->iocount += (size_t)count;
1160 #endif /* DEBUG */
1161
1162 continue;
1163 }
1164 }
1165
1166 /*
1167 * Copy bytes from raster buffer to "buf"...
1168 */
1169
1170 if (count > remaining)
1171 count = remaining;
1172
1173 if (count == 1)
1174 {
1175 /*
1176 * Copy 1 byte...
1177 */
1178
1179 *buf = *(r->bufptr)++;
1180 remaining --;
1181 }
1182 else if (count < 128)
1183 {
1184 /*
1185 * Copy up to 127 bytes without using memcpy(); this is
1186 * faster because it avoids an extra function call and is
1187 * often further optimized by the compiler...
1188 */
1189
1190 unsigned char *bufptr; /* Temporary buffer pointer */
1191
1192 remaining -= count;
1193
1194 for (bufptr = r->bufptr; count > 0; count --, total ++)
1195 *buf++ = *bufptr++;
1196
1197 r->bufptr = bufptr;
1198 }
1199 else
1200 {
1201 /*
1202 * Use memcpy() for a large read...
1203 */
1204
1205 memcpy(buf, r->bufptr, (size_t)count);
1206 r->bufptr += count;
1207 remaining -= count;
1208 }
1209 }
1210
1211 DEBUG_printf(("6cups_raster_read: Returning %ld", (long)total));
1212
1213 return (total);
1214 }
1215
1216
1217 /*
1218 * 'cups_raster_update()' - Update the raster header and row count for the
1219 * current page.
1220 */
1221
1222 static void
1223 cups_raster_update(cups_raster_t *r) /* I - Raster stream */
1224 {
1225 if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1 ||
1226 r->header.cupsNumColors == 0)
1227 {
1228 /*
1229 * Set the "cupsNumColors" field according to the colorspace...
1230 */
1231
1232 switch (r->header.cupsColorSpace)
1233 {
1234 case CUPS_CSPACE_W :
1235 case CUPS_CSPACE_K :
1236 case CUPS_CSPACE_WHITE :
1237 case CUPS_CSPACE_GOLD :
1238 case CUPS_CSPACE_SILVER :
1239 case CUPS_CSPACE_SW :
1240 r->header.cupsNumColors = 1;
1241 break;
1242
1243 case CUPS_CSPACE_RGB :
1244 case CUPS_CSPACE_CMY :
1245 case CUPS_CSPACE_YMC :
1246 case CUPS_CSPACE_CIEXYZ :
1247 case CUPS_CSPACE_CIELab :
1248 case CUPS_CSPACE_SRGB :
1249 case CUPS_CSPACE_ADOBERGB :
1250 case CUPS_CSPACE_ICC1 :
1251 case CUPS_CSPACE_ICC2 :
1252 case CUPS_CSPACE_ICC3 :
1253 case CUPS_CSPACE_ICC4 :
1254 case CUPS_CSPACE_ICC5 :
1255 case CUPS_CSPACE_ICC6 :
1256 case CUPS_CSPACE_ICC7 :
1257 case CUPS_CSPACE_ICC8 :
1258 case CUPS_CSPACE_ICC9 :
1259 case CUPS_CSPACE_ICCA :
1260 case CUPS_CSPACE_ICCB :
1261 case CUPS_CSPACE_ICCC :
1262 case CUPS_CSPACE_ICCD :
1263 case CUPS_CSPACE_ICCE :
1264 case CUPS_CSPACE_ICCF :
1265 r->header.cupsNumColors = 3;
1266 break;
1267
1268 case CUPS_CSPACE_RGBA :
1269 case CUPS_CSPACE_RGBW :
1270 case CUPS_CSPACE_CMYK :
1271 case CUPS_CSPACE_YMCK :
1272 case CUPS_CSPACE_KCMY :
1273 case CUPS_CSPACE_GMCK :
1274 case CUPS_CSPACE_GMCS :
1275 r->header.cupsNumColors = 4;
1276 break;
1277
1278 case CUPS_CSPACE_KCMYcm :
1279 if (r->header.cupsBitsPerPixel < 8)
1280 r->header.cupsNumColors = 6;
1281 else
1282 r->header.cupsNumColors = 4;
1283 break;
1284
1285 case CUPS_CSPACE_DEVICE1 :
1286 case CUPS_CSPACE_DEVICE2 :
1287 case CUPS_CSPACE_DEVICE3 :
1288 case CUPS_CSPACE_DEVICE4 :
1289 case CUPS_CSPACE_DEVICE5 :
1290 case CUPS_CSPACE_DEVICE6 :
1291 case CUPS_CSPACE_DEVICE7 :
1292 case CUPS_CSPACE_DEVICE8 :
1293 case CUPS_CSPACE_DEVICE9 :
1294 case CUPS_CSPACE_DEVICEA :
1295 case CUPS_CSPACE_DEVICEB :
1296 case CUPS_CSPACE_DEVICEC :
1297 case CUPS_CSPACE_DEVICED :
1298 case CUPS_CSPACE_DEVICEE :
1299 case CUPS_CSPACE_DEVICEF :
1300 r->header.cupsNumColors = r->header.cupsColorSpace -
1301 CUPS_CSPACE_DEVICE1 + 1;
1302 break;
1303 }
1304 }
1305
1306 /*
1307 * Set the number of bytes per pixel/color...
1308 */
1309
1310 if (r->header.cupsColorOrder == CUPS_ORDER_CHUNKED)
1311 r->bpp = (r->header.cupsBitsPerPixel + 7) / 8;
1312 else
1313 r->bpp = (r->header.cupsBitsPerColor + 7) / 8;
1314
1315 if (r->bpp == 0)
1316 r->bpp = 1;
1317
1318 /*
1319 * Set the number of remaining rows...
1320 */
1321
1322 if (r->header.cupsColorOrder == CUPS_ORDER_PLANAR)
1323 r->remaining = r->header.cupsHeight * r->header.cupsNumColors;
1324 else
1325 r->remaining = r->header.cupsHeight;
1326
1327 /*
1328 * Allocate the compression buffer...
1329 */
1330
1331 if (r->compressed)
1332 {
1333 if (r->pixels != NULL)
1334 free(r->pixels);
1335
1336 r->pixels = calloc(r->header.cupsBytesPerLine, 1);
1337 r->pcurrent = r->pixels;
1338 r->pend = r->pixels + r->header.cupsBytesPerLine;
1339 r->count = 0;
1340 }
1341 }
1342
1343
1344 /*
1345 * 'cups_raster_write()' - Write a row of compressed raster data...
1346 */
1347
1348 static ssize_t /* O - Number of bytes written */
1349 cups_raster_write(
1350 cups_raster_t *r, /* I - Raster stream */
1351 const unsigned char *pixels) /* I - Pixel data to write */
1352 {
1353 const unsigned char *start, /* Start of sequence */
1354 *ptr, /* Current pointer in sequence */
1355 *pend, /* End of raster buffer */
1356 *plast; /* Pointer to last pixel */
1357 unsigned char *wptr; /* Pointer into write buffer */
1358 unsigned bpp, /* Bytes per pixel */
1359 count; /* Count */
1360
1361
1362 DEBUG_printf(("3cups_raster_write(r=%p, pixels=%p)\n", r, pixels));
1363
1364 /*
1365 * Allocate a write buffer as needed...
1366 */
1367
1368 count = r->header.cupsBytesPerLine * 2;
1369 if (count < 65536)
1370 count = 65536;
1371
1372 if ((size_t)count > r->bufsize)
1373 {
1374 if (r->buffer)
1375 wptr = realloc(r->buffer, count);
1376 else
1377 wptr = malloc(count);
1378
1379 if (!wptr)
1380 {
1381 DEBUG_printf(("4cups_raster_write: Unable to allocate " CUPS_LLFMT " bytes for raster buffer: %s", CUPS_LLCAST count, strerror(errno)));
1382 return (-1);
1383 }
1384
1385 r->buffer = wptr;
1386 r->bufsize = count;
1387 }
1388
1389 /*
1390 * Write the row repeat count...
1391 */
1392
1393 bpp = r->bpp;
1394 pend = pixels + r->header.cupsBytesPerLine;
1395 plast = pend - bpp;
1396 wptr = r->buffer;
1397 *wptr++ = (unsigned char)(r->count - 1);
1398
1399 /*
1400 * Write using a modified PackBits compression...
1401 */
1402
1403 for (ptr = pixels; ptr < pend;)
1404 {
1405 start = ptr;
1406 ptr += bpp;
1407
1408 if (ptr == pend)
1409 {
1410 /*
1411 * Encode a single pixel at the end...
1412 */
1413
1414 *wptr++ = 0;
1415 for (count = bpp; count > 0; count --)
1416 *wptr++ = *start++;
1417 }
1418 else if (!memcmp(start, ptr, bpp))
1419 {
1420 /*
1421 * Encode a sequence of repeating pixels...
1422 */
1423
1424 for (count = 2; count < 128 && ptr < plast; count ++, ptr += bpp)
1425 if (memcmp(ptr, ptr + bpp, bpp))
1426 break;
1427
1428 *wptr++ = (unsigned char)(count - 1);
1429 for (count = bpp; count > 0; count --)
1430 *wptr++ = *ptr++;
1431 }
1432 else
1433 {
1434 /*
1435 * Encode a sequence of non-repeating pixels...
1436 */
1437
1438 for (count = 1; count < 128 && ptr < plast; count ++, ptr += bpp)
1439 if (!memcmp(ptr, ptr + bpp, bpp))
1440 break;
1441
1442 if (ptr >= plast && count < 128)
1443 {
1444 count ++;
1445 ptr += bpp;
1446 }
1447
1448 *wptr++ = (unsigned char)(257 - count);
1449
1450 count *= bpp;
1451 memcpy(wptr, start, count);
1452 wptr += count;
1453 }
1454 }
1455
1456 DEBUG_printf(("4cups_raster_write: Writing " CUPS_LLFMT " bytes.", CUPS_LLCAST (wptr - r->buffer)));
1457
1458 return (cups_raster_io(r, r->buffer, (size_t)(wptr - r->buffer)));
1459 }
1460
1461
1462 /*
1463 * 'cups_read_fd()' - Read bytes from a file.
1464 */
1465
1466 static ssize_t /* O - Bytes read or -1 */
1467 cups_read_fd(void *ctx, /* I - File descriptor as pointer */
1468 unsigned char *buf, /* I - Buffer for read */
1469 size_t bytes) /* I - Maximum number of bytes to read */
1470 {
1471 int fd = (int)((intptr_t)ctx);
1472 /* File descriptor */
1473 ssize_t count; /* Number of bytes read */
1474
1475
1476 #ifdef WIN32 /* Sigh */
1477 while ((count = read(fd, buf, (unsigned)bytes)) < 0)
1478 #else
1479 while ((count = read(fd, buf, bytes)) < 0)
1480 #endif /* WIN32 */
1481 if (errno != EINTR && errno != EAGAIN)
1482 {
1483 DEBUG_printf(("4cups_read_fd: %s", strerror(errno)));
1484 return (-1);
1485 }
1486
1487 DEBUG_printf(("4cups_read_fd: Returning %d bytes.", (int)count));
1488
1489 return (count);
1490 }
1491
1492
1493 /*
1494 * 'cups_swap()' - Swap bytes in raster data...
1495 */
1496
1497 static void
1498 cups_swap(unsigned char *buf, /* I - Buffer to swap */
1499 size_t bytes) /* I - Number of bytes to swap */
1500 {
1501 unsigned char even, odd; /* Temporary variables */
1502
1503
1504 bytes /= 2;
1505
1506 while (bytes > 0)
1507 {
1508 even = buf[0];
1509 odd = buf[1];
1510 buf[0] = odd;
1511 buf[1] = even;
1512
1513 buf += 2;
1514 bytes --;
1515 }
1516 }
1517
1518
1519 /*
1520 * 'cups_write_fd()' - Write bytes to a file.
1521 */
1522
1523 static ssize_t /* O - Bytes written or -1 */
1524 cups_write_fd(void *ctx, /* I - File descriptor pointer */
1525 unsigned char *buf, /* I - Bytes to write */
1526 size_t bytes) /* I - Number of bytes to write */
1527 {
1528 int fd = (int)((intptr_t)ctx);
1529 /* File descriptor */
1530 ssize_t count; /* Number of bytes written */
1531
1532
1533 #ifdef WIN32 /* Sigh */
1534 while ((count = write(fd, buf, (unsigned)bytes)) < 0)
1535 #else
1536 while ((count = write(fd, buf, bytes)) < 0)
1537 #endif /* WIN32 */
1538 if (errno != EINTR && errno != EAGAIN)
1539 {
1540 DEBUG_printf(("4cups_write_fd: %s", strerror(errno)));
1541 return (-1);
1542 }
1543
1544 return (count);
1545 }
1546
1547
1548 /*
1549 * End of "$Id$".
1550 */