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