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