2 * "$Id: raster.c 6649 2007-07-11 21:46:42Z mike $"
4 * Raster file routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * This file is part of the CUPS Imaging library.
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/".
17 * This file is subject to the Apple OS-Developed Software exception.
21 * cupsRasterClose() - Close a raster stream.
22 * cupsRasterOpen() - Open a raster stream.
23 * cupsRasterReadHeader() - Read a raster page header and store it in a
24 * V1 page header structure.
25 * cupsRasterReadHeader2() - Read a raster page header and store it in a
26 * V2 page header structure.
27 * cupsRasterReadPixels() - Read raster pixels.
28 * cupsRasterWriteHeader() - Write a raster page header from a V1 page
30 * cupsRasterWriteHeader2() - Write a raster page header from a V2 page
32 * cupsRasterWritePixels() - Write raster pixels.
33 * cups_raster_read() - Read through the raster buffer.
34 * cups_raster_read_header() - Read a raster page header.
35 * cups_raster_update() - Update the raster header and row count for the
37 * cups_raster_write() - Write a row of raster data...
38 * cups_read() - Read bytes from a file.
39 * cups_swap() - Swap bytes in raster data...
40 * cups_write() - Write bytes to a file.
44 * Include necessary headers...
47 #include "image-private.h"
48 #include <cups/debug.h>
51 #include <cups/string.h>
53 #if defined(WIN32) || defined(__EMX__)
57 #endif /* WIN32 || __EMX__ */
61 * Private structures...
64 struct _cups_raster_s
/**** Raster stream data ****/
66 unsigned sync
; /* Sync word from start of stream */
67 int fd
; /* File descriptor */
68 cups_mode_t mode
; /* Read/write mode */
69 cups_page_header2_t header
; /* Raster header for current page */
70 int count
, /* Current row run-length count */
71 remaining
, /* Remaining rows in page image */
72 bpp
; /* Bytes per pixel/color */
73 unsigned char *pixels
, /* Pixels for current row */
74 *pend
, /* End of pixel buffer */
75 *pcurrent
; /* Current byte in pixel buffer */
76 int compressed
, /* Non-zero if data is compressed */
77 swapped
; /* Non-zero if data is byte-swapped */
78 unsigned char *buffer
, /* Read/write buffer */
79 *bufptr
, /* Current (read) position in buffer */
80 *bufend
; /* End of current (read) buffer */
81 int bufsize
; /* Buffer size */
89 static unsigned cups_raster_read_header(cups_raster_t
*r
);
90 static int cups_raster_read(cups_raster_t
*r
, unsigned char *buf
,
92 static void cups_raster_update(cups_raster_t
*r
);
93 static int cups_raster_write(cups_raster_t
*r
, const unsigned char *pixels
);
94 static int cups_read(int fd
, unsigned char *buf
, int bytes
);
95 static void cups_swap(unsigned char *buf
, int bytes
);
96 static int cups_write(int fd
, const unsigned char *buf
, int bytes
);
100 * 'cupsRasterClose()' - Close a raster stream.
104 cupsRasterClose(cups_raster_t
*r
) /* I - Stream to close */
120 * 'cupsRasterOpen()' - Open a raster stream.
123 cups_raster_t
* /* O - New stream */
124 cupsRasterOpen(int fd
, /* I - File descriptor */
125 cups_mode_t mode
) /* I - Mode */
127 cups_raster_t
*r
; /* New stream */
130 _cupsRasterClearError();
132 if ((r
= calloc(sizeof(cups_raster_t
), 1)) == NULL
)
134 _cupsRasterAddError("Unable to allocate memory for raster stream: %s\n",
140 r
->mode
= mode
== CUPS_RASTER_WRITE_COMPRESSED
? CUPS_RASTER_WRITE
: mode
;
142 if (mode
== CUPS_RASTER_READ
)
145 * Open for read - get sync word...
148 if (!cups_read(r
->fd
, (unsigned char *)&(r
->sync
), sizeof(r
->sync
)))
150 _cupsRasterAddError("Unable to read header from raster stream: %s\n",
156 if (r
->sync
!= CUPS_RASTER_SYNC
&&
157 r
->sync
!= CUPS_RASTER_REVSYNC
&&
158 r
->sync
!= CUPS_RASTER_SYNCv1
&&
159 r
->sync
!= CUPS_RASTER_REVSYNCv1
&&
160 r
->sync
!= CUPS_RASTER_SYNCv2
&&
161 r
->sync
!= CUPS_RASTER_REVSYNCv2
)
163 _cupsRasterAddError("Unknown raster format %08x!\n", r
->sync
);
168 if (r
->sync
== CUPS_RASTER_SYNCv2
||
169 r
->sync
== CUPS_RASTER_REVSYNCv2
)
172 if (r
->sync
== CUPS_RASTER_REVSYNC
||
173 r
->sync
== CUPS_RASTER_REVSYNCv1
||
174 r
->sync
== CUPS_RASTER_REVSYNCv2
)
177 DEBUG_printf(("r->swapped=%d, r->sync=%08x\n", r
->swapped
, r
->sync
));
182 * Open for write - put sync word...
185 if (mode
== CUPS_RASTER_WRITE_COMPRESSED
)
188 r
->sync
= CUPS_RASTER_SYNCv2
;
191 r
->sync
= CUPS_RASTER_SYNC
;
193 if (cups_write(r
->fd
, (unsigned char *)&(r
->sync
), sizeof(r
->sync
))
196 _cupsRasterAddError("Unable to write raster stream header: %s\n",
208 * 'cupsRasterReadHeader()' - Read a raster page header and store it in a
209 * V1 page header structure.
212 unsigned /* O - 1 on success, 0 on fail */
213 cupsRasterReadHeader(
214 cups_raster_t
*r
, /* I - Raster stream */
215 cups_page_header_t
*h
) /* I - Pointer to header data */
218 * Get the raster header...
221 if (!cups_raster_read_header(r
))
225 * Copy the header to the user-supplied buffer...
228 memcpy(h
, &(r
->header
), sizeof(cups_page_header_t
));
235 * 'cupsRasterReadHeader2()' - Read a raster page header and store it in a
236 * V2 page header structure.
241 unsigned /* O - 1 on success, 0 on fail */
242 cupsRasterReadHeader2(
243 cups_raster_t
*r
, /* I - Raster stream */
244 cups_page_header2_t
*h
) /* I - Pointer to header data */
247 * Get the raster header...
250 if (!cups_raster_read_header(r
))
254 * Copy the header to the user-supplied buffer...
257 memcpy(h
, &(r
->header
), sizeof(cups_page_header2_t
));
264 * 'cupsRasterReadPixels()' - Read raster pixels.
267 unsigned /* O - Number of bytes read */
268 cupsRasterReadPixels(cups_raster_t
*r
, /* I - Raster stream */
269 unsigned char *p
, /* I - Pointer to pixel buffer */
270 unsigned len
) /* I - Number of bytes to read */
272 int bytes
; /* Bytes read */
273 unsigned cupsBytesPerLine
; /* cupsBytesPerLine value */
274 unsigned remaining
; /* Bytes remaining */
275 unsigned char *ptr
, /* Pointer to read buffer */
276 byte
, /* Byte from file */
277 *temp
; /* Pointer into buffer */
278 int count
; /* Repetition count */
281 if (r
== NULL
|| r
->mode
!= CUPS_RASTER_READ
|| r
->remaining
== 0)
287 * Read without compression...
290 r
->remaining
-= len
/ r
->header
.cupsBytesPerLine
;
292 if (!cups_read(r
->fd
, p
, len
))
296 * Swap bytes as needed...
299 if ((r
->header
.cupsBitsPerColor
== 16 ||
300 r
->header
.cupsBitsPerPixel
== 12 ||
301 r
->header
.cupsBitsPerPixel
== 16) &&
313 * Read compressed data...
317 cupsBytesPerLine
= r
->header
.cupsBytesPerLine
;
319 while (remaining
> 0 && r
->remaining
> 0)
324 * Need to read a new row...
327 if (remaining
== cupsBytesPerLine
)
333 * Read using a modified TIFF "packbits" compression...
336 if (!cups_raster_read(r
, &byte
, 1))
345 bytes
= cupsBytesPerLine
;
350 * Get a new repeat count...
353 if (!cups_raster_read(r
, &byte
, 1))
359 * Copy N literal pixels...
362 count
= (257 - byte
) * r
->bpp
;
367 if (!cups_raster_read(r
, temp
, count
))
376 * Repeat the next N bytes...
379 count
= (byte
+ 1) * r
->bpp
;
388 if (!cups_raster_read(r
, temp
, r
->bpp
))
396 memcpy(temp
, temp
- r
->bpp
, r
->bpp
);
404 * Swap bytes as needed...
407 if ((r
->header
.cupsBitsPerColor
== 16 ||
408 r
->header
.cupsBitsPerPixel
== 12 ||
409 r
->header
.cupsBitsPerPixel
== 16) &&
411 cups_swap(ptr
, bytes
);
417 if (remaining
>= cupsBytesPerLine
)
419 bytes
= cupsBytesPerLine
;
420 r
->pcurrent
= r
->pixels
;
427 r
->pcurrent
= r
->pixels
+ bytes
;
431 * Copy data as needed...
435 memcpy(p
, ptr
, bytes
);
440 * Copy fragment from buffer...
443 if ((bytes
= r
->pend
- r
->pcurrent
) > remaining
)
446 memcpy(p
, r
->pcurrent
, bytes
);
447 r
->pcurrent
+= bytes
;
449 if (r
->pcurrent
>= r
->pend
)
451 r
->pcurrent
= r
->pixels
;
466 * 'cupsRasterWriteHeader()' - Write a raster page header from a V1 page
470 unsigned /* O - 1 on success, 0 on failure */
471 cupsRasterWriteHeader(
472 cups_raster_t
*r
, /* I - Raster stream */
473 cups_page_header_t
*h
) /* I - Raster page header */
475 if (r
== NULL
|| r
->mode
!= CUPS_RASTER_WRITE
)
479 * Make a copy of the header, and compute the number of raster
480 * lines in the page image...
483 memset(&(r
->header
), 0, sizeof(r
->header
));
484 memcpy(&(r
->header
), h
, sizeof(cups_page_header_t
));
486 cups_raster_update(r
);
489 * Write the raster header...
492 return (cups_write(r
->fd
, (unsigned char *)&(r
->header
), sizeof(r
->header
))
498 * 'cupsRasterWriteHeader2()' - Write a raster page header from a V2 page
504 unsigned /* O - 1 on success, 0 on failure */
505 cupsRasterWriteHeader2(
506 cups_raster_t
*r
, /* I - Raster stream */
507 cups_page_header2_t
*h
) /* I - Raster page header */
509 if (r
== NULL
|| r
->mode
!= CUPS_RASTER_WRITE
)
513 * Make a copy of the header, and compute the number of raster
514 * lines in the page image...
517 memcpy(&(r
->header
), h
, sizeof(cups_page_header2_t
));
519 cups_raster_update(r
);
522 * Write the raster header...
525 return (cups_write(r
->fd
, (unsigned char *)&(r
->header
), sizeof(r
->header
))
531 * 'cupsRasterWritePixels()' - Write raster pixels.
534 unsigned /* O - Number of bytes written */
535 cupsRasterWritePixels(cups_raster_t
*r
, /* I - Raster stream */
536 unsigned char *p
, /* I - Bytes to write */
537 unsigned len
)/* I - Number of bytes to write */
539 int bytes
; /* Bytes read */
540 unsigned remaining
; /* Bytes remaining */
543 DEBUG_printf(("cupsRasterWritePixels(r=%p, p=%p, len=%u), remaining=%u\n",
544 r
, p
, len
, r
->remaining
));
546 if (r
== NULL
|| r
->mode
!= CUPS_RASTER_WRITE
|| r
->remaining
== 0)
552 * Without compression, just write the raster data raw...
555 r
->remaining
-= len
/ r
->header
.cupsBytesPerLine
;
557 return (cups_write(r
->fd
, p
, len
));
561 * Otherwise, compress each line...
564 for (remaining
= len
; remaining
> 0; remaining
-= bytes
, p
+= bytes
)
567 * Figure out the number of remaining bytes on the current line...
570 if ((bytes
= remaining
) > (r
->pend
- r
->pcurrent
))
571 bytes
= r
->pend
- r
->pcurrent
;
576 * Check to see if this line is the same as the previous line...
579 if (memcmp(p
, r
->pcurrent
, bytes
))
581 if (!cups_raster_write(r
, r
->pixels
))
589 * Mark more bytes as the same...
592 r
->pcurrent
+= bytes
;
594 if (r
->pcurrent
>= r
->pend
)
597 * Increase the repeat count...
601 r
->pcurrent
= r
->pixels
;
604 * Flush out this line if it is the last one...
609 if (r
->remaining
== 0)
610 return (cups_raster_write(r
, r
->pixels
));
611 else if (r
->count
== 256)
613 if (cups_raster_write(r
, r
->pixels
) == 0)
627 * Copy the raster data to the buffer...
630 memcpy(r
->pcurrent
, p
, bytes
);
632 r
->pcurrent
+= bytes
;
634 if (r
->pcurrent
>= r
->pend
)
637 * Increase the repeat count...
641 r
->pcurrent
= r
->pixels
;
644 * Flush out this line if it is the last one...
649 if (r
->remaining
== 0)
650 return (cups_raster_write(r
, r
->pixels
));
660 * 'cups_raster_read_header()' - Read a raster page header.
663 static unsigned /* O - 1 on success, 0 on fail */
664 cups_raster_read_header(
665 cups_raster_t
*r
) /* I - Raster stream */
667 int len
; /* Length for read/swap */
670 if (r
== NULL
|| r
->mode
!= CUPS_RASTER_READ
)
674 * Get the length of the raster header...
677 if (r
->sync
== CUPS_RASTER_SYNCv1
|| r
->sync
== CUPS_RASTER_REVSYNCv1
)
678 len
= sizeof(cups_page_header_t
);
680 len
= sizeof(cups_page_header2_t
);
686 memset(&(r
->header
), 0, sizeof(r
->header
));
688 if (cups_raster_read(r
, (unsigned char *)&(r
->header
), len
) < len
)
692 * Swap bytes as needed...
697 unsigned *s
, /* Current word */
698 temp
; /* Temporary copy */
701 DEBUG_puts("Swapping header bytes...");
703 for (len
= 81, s
= &(r
->header
.AdvanceDistance
);
707 DEBUG_printf(("%08x =>", *s
));
710 *s
= ((temp
& 0xff) << 24) |
711 ((temp
& 0xff00) << 8) |
712 ((temp
& 0xff0000) >> 8) |
713 ((temp
& 0xff000000) >> 24);
715 DEBUG_printf((" %08x\n", *s
));
720 * Update the header and row count...
723 cups_raster_update(r
);
730 * 'cups_raster_read()' - Read through the raster buffer.
733 static int /* O - Number of bytes read */
734 cups_raster_read(cups_raster_t
*r
, /* I - Raster stream */
735 unsigned char *buf
, /* I - Buffer */
736 int bytes
) /* I - Number of bytes to read */
738 int count
, /* Number of bytes read */
739 remaining
, /* Remaining bytes in buffer */
740 total
; /* Total bytes read */
743 DEBUG_printf(("cups_raster_read(r=%p, buf=%p, bytes=%d)\n", r
, buf
, bytes
));
746 return (cups_read(r
->fd
, buf
, bytes
));
749 * Allocate a read buffer as needed...
752 count
= 2 * r
->header
.cupsBytesPerLine
;
754 if (count
> r
->bufsize
)
756 int offset
= r
->bufptr
- r
->buffer
; /* Offset to current start of buffer */
757 int end
= r
->bufend
- r
->buffer
; /* Offset to current end of buffer */
758 unsigned char *rptr
; /* Pointer in read buffer */
761 rptr
= realloc(r
->buffer
, count
);
763 rptr
= malloc(count
);
769 r
->bufptr
= rptr
+ offset
;
770 r
->bufend
= rptr
+ end
;
775 * Loop until we have read everything...
778 for (total
= 0, remaining
= r
->bufend
- r
->bufptr
;
780 total
+= count
, buf
+= count
)
782 count
= bytes
- total
;
784 DEBUG_printf(("count=%d, remaining=%d, buf=%p, bufptr=%p, bufend=%p...\n",
785 count
, remaining
, buf
, r
->bufptr
, r
->bufend
));
792 * Read into the raster buffer and then copy...
795 remaining
= cups_read(r
->fd
, r
->buffer
, r
->bufsize
);
799 r
->bufptr
= r
->buffer
;
800 r
->bufend
= r
->buffer
+ remaining
;
805 * Read directly into "buf"...
808 count
= cups_read(r
->fd
, buf
, count
);
818 * Copy bytes from raster buffer to "buf"...
821 if (count
> remaining
)
830 *buf
= *(r
->bufptr
)++;
833 else if (count
< 128)
836 * Copy up to 127 bytes without using memcpy(); this is
837 * faster because it avoids an extra function call and is
838 * often further optimized by the compiler...
841 unsigned char *bufptr
; /* Temporary buffer pointer */
846 for (bufptr
= r
->bufptr
; count
> 0; count
--, total
++)
854 * Use memcpy() for a large read...
857 memcpy(buf
, r
->bufptr
, count
);
868 * 'cups_raster_update()' - Update the raster header and row count for the
873 cups_raster_update(cups_raster_t
*r
) /* I - Raster stream */
875 if (r
->sync
== CUPS_RASTER_SYNCv1
|| r
->sync
== CUPS_RASTER_REVSYNCv1
||
876 r
->header
.cupsNumColors
== 0)
879 * Set the "cupsNumColors" field according to the colorspace...
882 switch (r
->header
.cupsColorSpace
)
886 case CUPS_CSPACE_WHITE
:
887 case CUPS_CSPACE_GOLD
:
888 case CUPS_CSPACE_SILVER
:
889 r
->header
.cupsNumColors
= 1;
892 case CUPS_CSPACE_RGB
:
893 case CUPS_CSPACE_CMY
:
894 case CUPS_CSPACE_YMC
:
895 case CUPS_CSPACE_CIEXYZ
:
896 case CUPS_CSPACE_CIELab
:
897 case CUPS_CSPACE_ICC1
:
898 case CUPS_CSPACE_ICC2
:
899 case CUPS_CSPACE_ICC3
:
900 case CUPS_CSPACE_ICC4
:
901 case CUPS_CSPACE_ICC5
:
902 case CUPS_CSPACE_ICC6
:
903 case CUPS_CSPACE_ICC7
:
904 case CUPS_CSPACE_ICC8
:
905 case CUPS_CSPACE_ICC9
:
906 case CUPS_CSPACE_ICCA
:
907 case CUPS_CSPACE_ICCB
:
908 case CUPS_CSPACE_ICCC
:
909 case CUPS_CSPACE_ICCD
:
910 case CUPS_CSPACE_ICCE
:
911 case CUPS_CSPACE_ICCF
:
912 r
->header
.cupsNumColors
= 3;
915 case CUPS_CSPACE_RGBA
:
916 case CUPS_CSPACE_RGBW
:
917 case CUPS_CSPACE_CMYK
:
918 case CUPS_CSPACE_YMCK
:
919 case CUPS_CSPACE_KCMY
:
920 case CUPS_CSPACE_GMCK
:
921 case CUPS_CSPACE_GMCS
:
922 r
->header
.cupsNumColors
= 4;
925 case CUPS_CSPACE_KCMYcm
:
926 if (r
->header
.cupsBitsPerPixel
< 8)
927 r
->header
.cupsNumColors
= 6;
929 r
->header
.cupsNumColors
= 4;
935 * Set the number of bytes per pixel/color...
938 if (r
->header
.cupsColorOrder
== CUPS_ORDER_CHUNKED
)
939 r
->bpp
= (r
->header
.cupsBitsPerPixel
+ 7) / 8;
941 r
->bpp
= (r
->header
.cupsBitsPerColor
+ 7) / 8;
944 * Set the number of remaining rows...
947 if (r
->header
.cupsColorOrder
== CUPS_ORDER_PLANAR
)
948 r
->remaining
= r
->header
.cupsHeight
* r
->header
.cupsNumColors
;
950 r
->remaining
= r
->header
.cupsHeight
;
953 * Allocate the compression buffer...
958 if (r
->pixels
!= NULL
)
961 r
->pixels
= calloc(r
->header
.cupsBytesPerLine
, 1);
962 r
->pcurrent
= r
->pixels
;
963 r
->pend
= r
->pixels
+ r
->header
.cupsBytesPerLine
;
970 * 'cups_raster_write()' - Write a row of compressed raster data...
973 static int /* O - Number of bytes written */
975 cups_raster_t
*r
, /* I - Raster stream */
976 const unsigned char *pixels
) /* I - Pixel data to write */
978 const unsigned char *start
, /* Start of sequence */
979 *ptr
, /* Current pointer in sequence */
980 *pend
, /* End of raster buffer */
981 *plast
; /* Pointer to last pixel */
982 unsigned char *wptr
; /* Pointer into write buffer */
983 int bpp
, /* Bytes per pixel */
985 maxrun
; /* Maximum run of 128 * bpp */
988 DEBUG_printf(("cups_raster_write(r=%p, pixels=%p)\n", r
, pixels
));
991 * Allocate a write buffer as needed...
994 count
= r
->header
.cupsBytesPerLine
* 2;
995 if (count
> r
->bufsize
)
998 wptr
= realloc(r
->buffer
, count
);
1000 wptr
= malloc(count
);
1010 * Write the row repeat count...
1014 pend
= pixels
+ r
->header
.cupsBytesPerLine
;
1017 *wptr
++ = r
->count
- 1;
1021 * Write using a modified TIFF "packbits" compression...
1024 for (ptr
= pixels
; ptr
< pend
;)
1032 * Encode a single pixel at the end...
1036 for (count
= bpp
; count
> 0; count
--)
1039 else if (!memcmp(start
, ptr
, bpp
))
1042 * Encode a sequence of repeating pixels...
1045 for (count
= 2; count
< 128 && ptr
< plast
; count
++, ptr
+= bpp
)
1046 if (memcmp(ptr
, ptr
+ bpp
, bpp
))
1049 *wptr
++ = count
- 1;
1050 for (count
= bpp
; count
> 0; count
--)
1056 * Encode a sequence of non-repeating pixels...
1059 for (count
= 1; count
< 127 && ptr
< plast
; count
++, ptr
+= bpp
)
1060 if (!memcmp(ptr
, ptr
+ bpp
, bpp
))
1063 if (ptr
>= plast
&& count
< 128)
1069 *wptr
++ = 257 - count
;
1072 memcpy(wptr
, start
, count
);
1077 return (cups_write(r
->fd
, r
->buffer
, wptr
- r
->buffer
));
1082 * 'cups_read()' - Read bytes from a file.
1085 static int /* O - Bytes read or -1 */
1086 cups_read(int fd
, /* I - File descriptor */
1087 unsigned char *buf
, /* I - Buffer for read */
1088 int bytes
) /* I - Number of bytes to read */
1090 int count
, /* Number of bytes read */
1091 total
; /* Total bytes read */
1094 for (total
= 0; total
< bytes
; total
+= count
, buf
+= count
)
1096 count
= read(fd
, buf
, bytes
- total
);
1114 * 'cups_swap()' - Swap bytes in raster data...
1118 cups_swap(unsigned char *buf
, /* I - Buffer to swap */
1119 int bytes
) /* I - Number of bytes to swap */
1121 unsigned char even
, odd
; /* Temporary variables */
1140 * 'cups_write()' - Write bytes to a file.
1143 static int /* O - Bytes written or -1 */
1144 cups_write(int fd
, /* I - File descriptor */
1145 const unsigned char *buf
, /* I - Bytes to write */
1146 int bytes
) /* I - Number of bytes to write */
1148 int count
, /* Number of bytes written */
1149 total
; /* Total bytes written */
1152 for (total
= 0; total
< bytes
; total
+= count
, buf
+= count
)
1154 count
= write(fd
, buf
, bytes
- total
);
1170 * End of "$Id: raster.c 6649 2007-07-11 21:46:42Z mike $".