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