]>
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 | ||
a2326b5b | 507 | if ((unsigned)(bytes = (int)(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 | /* | |
10ddcf65 | 563 | * PWG raster data is always network byte order with much of the page header |
c8fef167 MS |
564 | * zeroed. |
565 | */ | |
566 | ||
567 | cups_page_header2_t fh; /* File page header */ | |
568 | ||
569 | memset(&fh, 0, sizeof(fh)); | |
10ddcf65 MS |
570 | |
571 | strlcpy(fh.MediaClass, "PwgRaster", sizeof(fh.MediaClass)); | |
572 | /* PwgRaster */ | |
573 | strlcpy(fh.MediaColor, r->header.MediaColor, sizeof(fh.MediaColor)); | |
574 | strlcpy(fh.MediaType, r->header.MediaType, sizeof(fh.MediaType)); | |
575 | strlcpy(fh.OutputType, r->header.OutputType, sizeof(fh.OutputType)); | |
576 | /* PrintContentType */ | |
577 | ||
578 | fh.CutMedia = htonl(r->header.CutMedia); | |
579 | fh.Duplex = htonl(r->header.Duplex); | |
580 | fh.HWResolution[0] = htonl(r->header.HWResolution[0]); | |
581 | fh.HWResolution[1] = htonl(r->header.HWResolution[1]); | |
582 | fh.ImagingBoundingBox[0] = htonl(r->header.ImagingBoundingBox[0]); | |
583 | fh.ImagingBoundingBox[1] = htonl(r->header.ImagingBoundingBox[1]); | |
584 | fh.ImagingBoundingBox[2] = htonl(r->header.ImagingBoundingBox[2]); | |
585 | fh.ImagingBoundingBox[3] = htonl(r->header.ImagingBoundingBox[3]); | |
586 | fh.InsertSheet = htonl(r->header.InsertSheet); | |
587 | fh.Jog = htonl(r->header.Jog); | |
588 | fh.LeadingEdge = htonl(r->header.LeadingEdge); | |
589 | fh.ManualFeed = htonl(r->header.ManualFeed); | |
590 | fh.MediaPosition = htonl(r->header.MediaPosition); | |
591 | fh.MediaWeight = htonl(r->header.MediaWeight); | |
592 | fh.NumCopies = htonl(r->header.NumCopies); | |
593 | fh.Orientation = htonl(r->header.Orientation); | |
594 | fh.PageSize[0] = htonl(r->header.PageSize[0]); | |
595 | fh.PageSize[1] = htonl(r->header.PageSize[1]); | |
596 | fh.Tumble = htonl(r->header.Tumble); | |
597 | fh.cupsWidth = htonl(r->header.cupsWidth); | |
598 | fh.cupsHeight = htonl(r->header.cupsHeight); | |
599 | fh.cupsBitsPerColor = htonl(r->header.cupsBitsPerColor); | |
600 | fh.cupsBitsPerPixel = htonl(r->header.cupsBitsPerPixel); | |
601 | fh.cupsBytesPerLine = htonl(r->header.cupsBytesPerLine); | |
602 | fh.cupsColorOrder = htonl(r->header.cupsColorOrder); | |
603 | fh.cupsColorSpace = htonl(r->header.cupsColorSpace); | |
604 | fh.cupsNumColors = htonl(r->header.cupsNumColors); | |
605 | fh.cupsInteger[0] = htonl(r->header.cupsInteger[0]); | |
606 | /* TotalPageCount */ | |
607 | fh.cupsInteger[1] = htonl(r->header.cupsInteger[1]); | |
608 | /* CrossFeedTransform */ | |
609 | fh.cupsInteger[2] = htonl(r->header.cupsInteger[2]); | |
610 | /* FeedTransform */ | |
611 | fh.cupsInteger[3] = htonl(r->header.cupsInteger[3]); | |
612 | /* ImageBoxLeft */ | |
613 | fh.cupsInteger[4] = htonl(r->header.cupsInteger[4]); | |
614 | /* ImageBoxTop */ | |
615 | fh.cupsInteger[5] = htonl(r->header.cupsInteger[5]); | |
616 | /* ImageBoxRight */ | |
617 | fh.cupsInteger[6] = htonl(r->header.cupsInteger[6]); | |
618 | /* ImageBoxBottom */ | |
619 | fh.cupsInteger[7] = htonl(r->header.cupsInteger[7]); | |
620 | /* BlackPrimary */ | |
621 | fh.cupsInteger[8] = htonl(r->header.cupsInteger[8]); | |
622 | /* PrintQuality */ | |
623 | fh.cupsInteger[14] = htonl(r->header.cupsInteger[14]); | |
624 | /* VendorIdentifier */ | |
625 | fh.cupsInteger[15] = htonl(r->header.cupsInteger[15]); | |
626 | /* VendorLength */ | |
627 | ||
628 | memcpy(fh.cupsReal, r->header.cupsReal, | |
629 | sizeof(fh.cupsReal) + sizeof(fh.cupsString)); | |
630 | /* VendorData */ | |
631 | ||
632 | strlcpy(fh.cupsRenderingIntent, r->header.cupsRenderingIntent, | |
633 | sizeof(fh.cupsRenderingIntent)); | |
634 | strlcpy(fh.cupsPageSizeName, r->header.cupsPageSizeName, | |
635 | sizeof(fh.cupsPageSizeName)); | |
c8fef167 | 636 | |
f14324a7 | 637 | return (cups_raster_io(r, (unsigned char *)&fh, sizeof(fh)) == sizeof(fh)); |
c8fef167 MS |
638 | } |
639 | else | |
f14324a7 | 640 | return (cups_raster_io(r, (unsigned char *)&(r->header), sizeof(r->header)) |
c8fef167 | 641 | == sizeof(r->header)); |
ef416fc2 | 642 | } |
643 | ||
644 | ||
645 | /* | |
db0bd74a MS |
646 | * 'cupsRasterWriteHeader2()' - Write a raster page header from a version 2 |
647 | * page header structure. | |
648 | * | |
649 | * The page header can be initialized using @link cupsRasterInterpretPPD@. | |
ef416fc2 | 650 | * |
db0bd74a | 651 | * @since CUPS 1.2/Mac OS X 10.5@ |
ef416fc2 | 652 | */ |
f14324a7 | 653 | |
ef416fc2 | 654 | unsigned /* O - 1 on success, 0 on failure */ |
655 | cupsRasterWriteHeader2( | |
656 | cups_raster_t *r, /* I - Raster stream */ | |
657 | cups_page_header2_t *h) /* I - Raster page header */ | |
658 | { | |
c8fef167 | 659 | if (r == NULL || r->mode == CUPS_RASTER_READ) |
ef416fc2 | 660 | return (0); |
661 | ||
662 | /* | |
663 | * Make a copy of the header, and compute the number of raster | |
664 | * lines in the page image... | |
665 | */ | |
666 | ||
667 | memcpy(&(r->header), h, sizeof(cups_page_header2_t)); | |
668 | ||
669 | cups_raster_update(r); | |
670 | ||
671 | /* | |
672 | * Write the raster header... | |
673 | */ | |
674 | ||
c8fef167 MS |
675 | if (r->mode == CUPS_RASTER_WRITE_PWG) |
676 | { | |
677 | /* | |
678 | * PWG raster data is always network byte order with most of the page header | |
679 | * zeroed. | |
680 | */ | |
681 | ||
682 | cups_page_header2_t fh; /* File page header */ | |
683 | ||
684 | memset(&fh, 0, sizeof(fh)); | |
771bd8cb MS |
685 | strlcpy(fh.MediaClass, "PwgRaster", sizeof(fh.MediaClass)); |
686 | strlcpy(fh.MediaColor, r->header.MediaColor, sizeof(fh.MediaColor)); | |
687 | strlcpy(fh.MediaType, r->header.MediaType, sizeof(fh.MediaType)); | |
688 | strlcpy(fh.OutputType, r->header.OutputType, sizeof(fh.OutputType)); | |
689 | strlcpy(fh.cupsRenderingIntent, r->header.cupsRenderingIntent, | |
690 | sizeof(fh.cupsRenderingIntent)); | |
691 | strlcpy(fh.cupsPageSizeName, r->header.cupsPageSizeName, | |
692 | sizeof(fh.cupsPageSizeName)); | |
693 | ||
694 | fh.CutMedia = htonl(r->header.CutMedia); | |
695 | fh.Duplex = htonl(r->header.Duplex); | |
696 | fh.HWResolution[0] = htonl(r->header.HWResolution[0]); | |
697 | fh.HWResolution[1] = htonl(r->header.HWResolution[1]); | |
698 | fh.ImagingBoundingBox[0] = htonl(r->header.ImagingBoundingBox[0]); | |
699 | fh.ImagingBoundingBox[1] = htonl(r->header.ImagingBoundingBox[1]); | |
700 | fh.ImagingBoundingBox[2] = htonl(r->header.ImagingBoundingBox[2]); | |
701 | fh.ImagingBoundingBox[3] = htonl(r->header.ImagingBoundingBox[3]); | |
702 | fh.InsertSheet = htonl(r->header.InsertSheet); | |
703 | fh.Jog = htonl(r->header.Jog); | |
704 | fh.LeadingEdge = htonl(r->header.LeadingEdge); | |
705 | fh.ManualFeed = htonl(r->header.ManualFeed); | |
706 | fh.MediaPosition = htonl(r->header.MediaPosition); | |
707 | fh.MediaWeight = htonl(r->header.MediaWeight); | |
708 | fh.NumCopies = htonl(r->header.NumCopies); | |
709 | fh.Orientation = htonl(r->header.Orientation); | |
710 | fh.PageSize[0] = htonl(r->header.PageSize[0]); | |
711 | fh.PageSize[1] = htonl(r->header.PageSize[1]); | |
712 | fh.Tumble = htonl(r->header.Tumble); | |
713 | fh.cupsWidth = htonl(r->header.cupsWidth); | |
714 | fh.cupsHeight = htonl(r->header.cupsHeight); | |
715 | fh.cupsBitsPerColor = htonl(r->header.cupsBitsPerColor); | |
716 | fh.cupsBitsPerPixel = htonl(r->header.cupsBitsPerPixel); | |
717 | fh.cupsBytesPerLine = htonl(r->header.cupsBytesPerLine); | |
718 | fh.cupsColorOrder = htonl(r->header.cupsColorOrder); | |
719 | fh.cupsColorSpace = htonl(r->header.cupsColorSpace); | |
720 | fh.cupsNumColors = htonl(r->header.cupsNumColors); | |
721 | fh.cupsInteger[0] = htonl(r->header.cupsInteger[0]); | |
722 | fh.cupsInteger[1] = htonl(r->header.cupsInteger[1]); | |
723 | fh.cupsInteger[2] = htonl(r->header.cupsInteger[2]); | |
83e08001 MS |
724 | fh.cupsInteger[3] = htonl((unsigned)(r->header.cupsImagingBBox[0] * |
725 | r->header.HWResolution[0])); | |
726 | fh.cupsInteger[4] = htonl((unsigned)(r->header.cupsImagingBBox[1] * | |
727 | r->header.HWResolution[1])); | |
728 | fh.cupsInteger[5] = htonl((unsigned)(r->header.cupsImagingBBox[2] * | |
729 | r->header.HWResolution[0])); | |
730 | fh.cupsInteger[6] = htonl((unsigned)(r->header.cupsImagingBBox[3] * | |
731 | r->header.HWResolution[1])); | |
771bd8cb | 732 | fh.cupsInteger[7] = htonl(0xffffff); |
c8fef167 | 733 | |
f14324a7 | 734 | return (cups_raster_io(r, (unsigned char *)&fh, sizeof(fh)) == sizeof(fh)); |
c8fef167 MS |
735 | } |
736 | else | |
f14324a7 | 737 | return (cups_raster_io(r, (unsigned char *)&(r->header), sizeof(r->header)) |
c8fef167 | 738 | == sizeof(r->header)); |
ef416fc2 | 739 | } |
740 | ||
741 | ||
742 | /* | |
743 | * 'cupsRasterWritePixels()' - Write raster pixels. | |
79e1d494 MS |
744 | * |
745 | * For best performance, filters should write one or more whole lines. | |
746 | * The "cupsBytesPerLine" value from the page header can be used to allocate | |
747 | * the line buffer and as the number of bytes to write. | |
ef416fc2 | 748 | */ |
749 | ||
750 | unsigned /* O - Number of bytes written */ | |
751 | cupsRasterWritePixels(cups_raster_t *r, /* I - Raster stream */ | |
752 | unsigned char *p, /* I - Bytes to write */ | |
753 | unsigned len)/* I - Number of bytes to write */ | |
754 | { | |
f7deaa1a | 755 | int bytes; /* Bytes read */ |
756 | unsigned remaining; /* Bytes remaining */ | |
757 | ||
758 | ||
09a101d6 | 759 | DEBUG_printf(("cupsRasterWritePixels(r=%p, p=%p, len=%u), remaining=%u\n", |
760 | r, p, len, r->remaining)); | |
f301802f | 761 | |
c8fef167 | 762 | if (r == NULL || r->mode == CUPS_RASTER_READ || r->remaining == 0) |
ef416fc2 | 763 | return (0); |
764 | ||
f7deaa1a | 765 | if (!r->compressed) |
766 | { | |
767 | /* | |
c8fef167 MS |
768 | * Without compression, just write the raster data raw unless the data needs |
769 | * to be swapped... | |
f7deaa1a | 770 | */ |
771 | ||
772 | r->remaining -= len / r->header.cupsBytesPerLine; | |
773 | ||
c8fef167 MS |
774 | if (r->swapped && |
775 | (r->header.cupsBitsPerColor == 16 || | |
776 | r->header.cupsBitsPerPixel == 12 || | |
777 | r->header.cupsBitsPerPixel == 16)) | |
778 | { | |
779 | unsigned char *bufptr; /* Pointer into write buffer */ | |
780 | unsigned count; /* Remaining count */ | |
781 | ||
782 | /* | |
783 | * Allocate a write buffer as needed... | |
784 | */ | |
785 | ||
786 | if ((size_t)len > r->bufsize) | |
787 | { | |
788 | if (r->buffer) | |
789 | bufptr = realloc(r->buffer, len); | |
790 | else | |
791 | bufptr = malloc(len); | |
792 | ||
793 | if (!bufptr) | |
794 | return (0); | |
795 | ||
796 | r->buffer = bufptr; | |
797 | r->bufsize = len; | |
798 | } | |
799 | ||
800 | /* | |
801 | * Byte swap the pixels... | |
802 | */ | |
803 | ||
804 | for (bufptr = r->buffer, count = len; count > 1; count -= 2, bufptr += 2) | |
805 | { | |
806 | bufptr[1] = *p++; | |
807 | bufptr[0] = *p++; | |
808 | } | |
809 | ||
810 | if (count) /* This should never happen... */ | |
811 | *bufptr = *p; | |
812 | ||
813 | /* | |
814 | * Write the byte-swapped buffer... | |
815 | */ | |
816 | ||
f14324a7 | 817 | return (cups_raster_io(r, r->buffer, len)); |
c8fef167 MS |
818 | } |
819 | else | |
f14324a7 | 820 | return (cups_raster_io(r, p, len)); |
f7deaa1a | 821 | } |
822 | ||
ed486911 | 823 | /* |
f7deaa1a | 824 | * Otherwise, compress each line... |
ed486911 | 825 | */ |
ef416fc2 | 826 | |
f7deaa1a | 827 | for (remaining = len; remaining > 0; remaining -= bytes, p += bytes) |
828 | { | |
829 | /* | |
830 | * Figure out the number of remaining bytes on the current line... | |
831 | */ | |
832 | ||
a2326b5b MS |
833 | if ((bytes = remaining) > (int)(r->pend - r->pcurrent)) |
834 | bytes = (int)(r->pend - r->pcurrent); | |
f7deaa1a | 835 | |
836 | if (r->count > 0) | |
837 | { | |
838 | /* | |
839 | * Check to see if this line is the same as the previous line... | |
840 | */ | |
841 | ||
842 | if (memcmp(p, r->pcurrent, bytes)) | |
843 | { | |
844 | if (!cups_raster_write(r, r->pixels)) | |
845 | return (0); | |
846 | ||
847 | r->count = 0; | |
848 | } | |
849 | else | |
850 | { | |
851 | /* | |
852 | * Mark more bytes as the same... | |
853 | */ | |
854 | ||
855 | r->pcurrent += bytes; | |
856 | ||
857 | if (r->pcurrent >= r->pend) | |
858 | { | |
859 | /* | |
860 | * Increase the repeat count... | |
861 | */ | |
862 | ||
863 | r->count ++; | |
864 | r->pcurrent = r->pixels; | |
865 | ||
866 | /* | |
867 | * Flush out this line if it is the last one... | |
868 | */ | |
869 | ||
870 | r->remaining --; | |
871 | ||
872 | if (r->remaining == 0) | |
873 | return (cups_raster_write(r, r->pixels)); | |
874 | else if (r->count == 256) | |
875 | { | |
876 | if (cups_raster_write(r, r->pixels) == 0) | |
877 | return (0); | |
f301802f | 878 | |
f7deaa1a | 879 | r->count = 0; |
880 | } | |
881 | } | |
882 | ||
883 | continue; | |
884 | } | |
885 | } | |
886 | ||
887 | if (r->count == 0) | |
888 | { | |
889 | /* | |
890 | * Copy the raster data to the buffer... | |
891 | */ | |
892 | ||
893 | memcpy(r->pcurrent, p, bytes); | |
894 | ||
895 | r->pcurrent += bytes; | |
896 | ||
897 | if (r->pcurrent >= r->pend) | |
898 | { | |
899 | /* | |
900 | * Increase the repeat count... | |
901 | */ | |
902 | ||
903 | r->count ++; | |
904 | r->pcurrent = r->pixels; | |
905 | ||
906 | /* | |
907 | * Flush out this line if it is the last one... | |
908 | */ | |
909 | ||
910 | r->remaining --; | |
911 | ||
912 | if (r->remaining == 0) | |
913 | return (cups_raster_write(r, r->pixels)); | |
914 | } | |
915 | } | |
916 | } | |
917 | ||
918 | return (len); | |
ef416fc2 | 919 | } |
920 | ||
921 | ||
922 | /* | |
923 | * 'cups_raster_read_header()' - Read a raster page header. | |
924 | */ | |
925 | ||
926 | static unsigned /* O - 1 on success, 0 on fail */ | |
927 | cups_raster_read_header( | |
928 | cups_raster_t *r) /* I - Raster stream */ | |
929 | { | |
09a101d6 | 930 | int len; /* Length for read/swap */ |
ef416fc2 | 931 | |
932 | ||
933 | if (r == NULL || r->mode != CUPS_RASTER_READ) | |
934 | return (0); | |
935 | ||
936 | /* | |
937 | * Get the length of the raster header... | |
938 | */ | |
939 | ||
940 | if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1) | |
941 | len = sizeof(cups_page_header_t); | |
942 | else | |
943 | len = sizeof(cups_page_header2_t); | |
944 | ||
945 | /* | |
946 | * Read the header... | |
947 | */ | |
948 | ||
949 | memset(&(r->header), 0, sizeof(r->header)); | |
950 | ||
ed486911 | 951 | if (cups_raster_read(r, (unsigned char *)&(r->header), len) < len) |
ef416fc2 | 952 | return (0); |
953 | ||
954 | /* | |
955 | * Swap bytes as needed... | |
956 | */ | |
957 | ||
ed486911 | 958 | if (r->swapped) |
09a101d6 | 959 | { |
960 | unsigned *s, /* Current word */ | |
961 | temp; /* Temporary copy */ | |
962 | ||
963 | ||
964 | DEBUG_puts("Swapping header bytes..."); | |
965 | ||
966 | for (len = 81, s = &(r->header.AdvanceDistance); | |
ef416fc2 | 967 | len > 0; |
968 | len --, s ++) | |
09a101d6 | 969 | { |
970 | DEBUG_printf(("%08x =>", *s)); | |
971 | ||
972 | temp = *s; | |
973 | *s = ((temp & 0xff) << 24) | | |
974 | ((temp & 0xff00) << 8) | | |
975 | ((temp & 0xff0000) >> 8) | | |
976 | ((temp & 0xff000000) >> 24); | |
977 | ||
978 | DEBUG_printf((" %08x\n", *s)); | |
979 | } | |
980 | } | |
ef416fc2 | 981 | |
982 | /* | |
983 | * Update the header and row count... | |
984 | */ | |
985 | ||
986 | cups_raster_update(r); | |
987 | ||
a4845881 | 988 | return (r->header.cupsBytesPerLine != 0 && r->header.cupsHeight != 0); |
ef416fc2 | 989 | } |
990 | ||
991 | ||
f14324a7 MS |
992 | /* |
993 | * 'cups_raster_io()' - Read/write bytes from a context, handling interruptions. | |
994 | */ | |
995 | ||
996 | static int /* O - Bytes read or -1 */ | |
83e08001 | 997 | cups_raster_io(cups_raster_t *r, /* I - Raster stream */ |
f14324a7 MS |
998 | unsigned char *buf, /* I - Buffer for read/write */ |
999 | int bytes) /* I - Number of bytes to read/write */ | |
1000 | { | |
1001 | ssize_t count; /* Number of bytes read/written */ | |
1002 | size_t total; /* Total bytes read/written */ | |
1003 | ||
1004 | ||
1005 | DEBUG_printf(("4cups_raster_io(r=%p, buf=%p, bytes=%d)", r, buf, bytes)); | |
1006 | ||
83e08001 | 1007 | for (total = 0; total < (size_t)bytes; total += count, buf += count) |
f14324a7 MS |
1008 | { |
1009 | count = (*r->iocb)(r->ctx, buf, bytes - total); | |
1010 | ||
1011 | DEBUG_printf(("5cups_raster_io: count=%d, total=%d", (int)count, | |
1012 | (int)total)); | |
1013 | if (count == 0) | |
1014 | return (0); | |
1015 | else if (count < 0) | |
1016 | return (-1); | |
1017 | } | |
1018 | ||
1019 | return ((int)total); | |
1020 | } | |
1021 | ||
1022 | ||
ed486911 | 1023 | /* |
1024 | * 'cups_raster_read()' - Read through the raster buffer. | |
1025 | */ | |
1026 | ||
1027 | static int /* O - Number of bytes read */ | |
1028 | cups_raster_read(cups_raster_t *r, /* I - Raster stream */ | |
1029 | unsigned char *buf, /* I - Buffer */ | |
1030 | int bytes) /* I - Number of bytes to read */ | |
1031 | { | |
1032 | int count, /* Number of bytes read */ | |
1033 | remaining, /* Remaining bytes in buffer */ | |
1034 | total; /* Total bytes read */ | |
1035 | ||
1036 | ||
1037 | DEBUG_printf(("cups_raster_read(r=%p, buf=%p, bytes=%d)\n", r, buf, bytes)); | |
1038 | ||
1039 | if (!r->compressed) | |
f14324a7 | 1040 | return (cups_raster_io(r, buf, bytes)); |
ed486911 | 1041 | |
1042 | /* | |
1043 | * Allocate a read buffer as needed... | |
1044 | */ | |
1045 | ||
1046 | count = 2 * r->header.cupsBytesPerLine; | |
1047 | ||
c8fef167 | 1048 | if ((size_t)count > r->bufsize) |
ed486911 | 1049 | { |
a2326b5b MS |
1050 | int offset = (int)(r->bufptr - r->buffer); |
1051 | /* Offset to current start of buffer */ | |
1052 | int end = (int)(r->bufend - r->buffer); | |
1053 | /* Offset to current end of buffer */ | |
ed486911 | 1054 | unsigned char *rptr; /* Pointer in read buffer */ |
1055 | ||
1056 | if (r->buffer) | |
1057 | rptr = realloc(r->buffer, count); | |
1058 | else | |
1059 | rptr = malloc(count); | |
1060 | ||
1061 | if (!rptr) | |
1062 | return (0); | |
1063 | ||
1064 | r->buffer = rptr; | |
1065 | r->bufptr = rptr + offset; | |
1066 | r->bufend = rptr + end; | |
1067 | r->bufsize = count; | |
1068 | } | |
1069 | ||
1070 | /* | |
1071 | * Loop until we have read everything... | |
1072 | */ | |
1073 | ||
a2326b5b | 1074 | for (total = 0, remaining = (int)(r->bufend - r->bufptr); |
ed486911 | 1075 | total < bytes; |
1076 | total += count, buf += count) | |
1077 | { | |
1078 | count = bytes - total; | |
1079 | ||
1080 | DEBUG_printf(("count=%d, remaining=%d, buf=%p, bufptr=%p, bufend=%p...\n", | |
1081 | count, remaining, buf, r->bufptr, r->bufend)); | |
1082 | ||
1083 | if (remaining == 0) | |
1084 | { | |
1085 | if (count < 16) | |
1086 | { | |
1087 | /* | |
1088 | * Read into the raster buffer and then copy... | |
1089 | */ | |
1090 | ||
c8fef167 | 1091 | remaining = (*r->iocb)(r->ctx, r->buffer, r->bufsize); |
ed486911 | 1092 | if (remaining <= 0) |
1093 | return (0); | |
1094 | ||
1095 | r->bufptr = r->buffer; | |
1096 | r->bufend = r->buffer + remaining; | |
1097 | } | |
1098 | else | |
1099 | { | |
1100 | /* | |
1101 | * Read directly into "buf"... | |
1102 | */ | |
1103 | ||
c8fef167 | 1104 | count = (*r->iocb)(r->ctx, buf, count); |
ed486911 | 1105 | |
1106 | if (count <= 0) | |
1107 | return (0); | |
1108 | ||
1109 | continue; | |
1110 | } | |
1111 | } | |
1112 | ||
1113 | /* | |
1114 | * Copy bytes from raster buffer to "buf"... | |
1115 | */ | |
1116 | ||
1117 | if (count > remaining) | |
1118 | count = remaining; | |
1119 | ||
1120 | if (count == 1) | |
1121 | { | |
1122 | /* | |
1123 | * Copy 1 byte... | |
1124 | */ | |
1125 | ||
1126 | *buf = *(r->bufptr)++; | |
1127 | remaining --; | |
1128 | } | |
1129 | else if (count < 128) | |
1130 | { | |
1131 | /* | |
1132 | * Copy up to 127 bytes without using memcpy(); this is | |
1133 | * faster because it avoids an extra function call and is | |
1134 | * often further optimized by the compiler... | |
1135 | */ | |
1136 | ||
1137 | unsigned char *bufptr; /* Temporary buffer pointer */ | |
1138 | ||
ed486911 | 1139 | remaining -= count; |
1140 | ||
1141 | for (bufptr = r->bufptr; count > 0; count --, total ++) | |
1142 | *buf++ = *bufptr++; | |
1143 | ||
1144 | r->bufptr = bufptr; | |
1145 | } | |
1146 | else | |
1147 | { | |
1148 | /* | |
1149 | * Use memcpy() for a large read... | |
1150 | */ | |
1151 | ||
1152 | memcpy(buf, r->bufptr, count); | |
1153 | r->bufptr += count; | |
1154 | remaining -= count; | |
1155 | } | |
1156 | } | |
1157 | ||
1158 | return (total); | |
1159 | } | |
1160 | ||
1161 | ||
ef416fc2 | 1162 | /* |
1163 | * 'cups_raster_update()' - Update the raster header and row count for the | |
1164 | * current page. | |
1165 | */ | |
1166 | ||
1167 | static void | |
1168 | cups_raster_update(cups_raster_t *r) /* I - Raster stream */ | |
1169 | { | |
f301802f | 1170 | if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1 || |
1171 | r->header.cupsNumColors == 0) | |
ef416fc2 | 1172 | { |
1173 | /* | |
1174 | * Set the "cupsNumColors" field according to the colorspace... | |
1175 | */ | |
1176 | ||
1177 | switch (r->header.cupsColorSpace) | |
1178 | { | |
1179 | case CUPS_CSPACE_W : | |
1180 | case CUPS_CSPACE_K : | |
1181 | case CUPS_CSPACE_WHITE : | |
1182 | case CUPS_CSPACE_GOLD : | |
1183 | case CUPS_CSPACE_SILVER : | |
c7017ecc | 1184 | case CUPS_CSPACE_SW : |
ef416fc2 | 1185 | r->header.cupsNumColors = 1; |
1186 | break; | |
1187 | ||
ef416fc2 | 1188 | case CUPS_CSPACE_RGB : |
1189 | case CUPS_CSPACE_CMY : | |
1190 | case CUPS_CSPACE_YMC : | |
1191 | case CUPS_CSPACE_CIEXYZ : | |
1192 | case CUPS_CSPACE_CIELab : | |
c7017ecc MS |
1193 | case CUPS_CSPACE_SRGB : |
1194 | case CUPS_CSPACE_ADOBERGB : | |
f301802f | 1195 | case CUPS_CSPACE_ICC1 : |
1196 | case CUPS_CSPACE_ICC2 : | |
ef416fc2 | 1197 | case CUPS_CSPACE_ICC3 : |
f301802f | 1198 | case CUPS_CSPACE_ICC4 : |
1199 | case CUPS_CSPACE_ICC5 : | |
1200 | case CUPS_CSPACE_ICC6 : | |
1201 | case CUPS_CSPACE_ICC7 : | |
1202 | case CUPS_CSPACE_ICC8 : | |
1203 | case CUPS_CSPACE_ICC9 : | |
1204 | case CUPS_CSPACE_ICCA : | |
1205 | case CUPS_CSPACE_ICCB : | |
1206 | case CUPS_CSPACE_ICCC : | |
1207 | case CUPS_CSPACE_ICCD : | |
1208 | case CUPS_CSPACE_ICCE : | |
1209 | case CUPS_CSPACE_ICCF : | |
ef416fc2 | 1210 | r->header.cupsNumColors = 3; |
1211 | break; | |
1212 | ||
1213 | case CUPS_CSPACE_RGBA : | |
1214 | case CUPS_CSPACE_RGBW : | |
1215 | case CUPS_CSPACE_CMYK : | |
1216 | case CUPS_CSPACE_YMCK : | |
1217 | case CUPS_CSPACE_KCMY : | |
1218 | case CUPS_CSPACE_GMCK : | |
1219 | case CUPS_CSPACE_GMCS : | |
ef416fc2 | 1220 | r->header.cupsNumColors = 4; |
1221 | break; | |
1222 | ||
1223 | case CUPS_CSPACE_KCMYcm : | |
1224 | if (r->header.cupsBitsPerPixel < 8) | |
1225 | r->header.cupsNumColors = 6; | |
1226 | else | |
1227 | r->header.cupsNumColors = 4; | |
1228 | break; | |
c7017ecc MS |
1229 | |
1230 | case CUPS_CSPACE_DEVICE1 : | |
1231 | case CUPS_CSPACE_DEVICE2 : | |
1232 | case CUPS_CSPACE_DEVICE3 : | |
1233 | case CUPS_CSPACE_DEVICE4 : | |
1234 | case CUPS_CSPACE_DEVICE5 : | |
1235 | case CUPS_CSPACE_DEVICE6 : | |
1236 | case CUPS_CSPACE_DEVICE7 : | |
1237 | case CUPS_CSPACE_DEVICE8 : | |
1238 | case CUPS_CSPACE_DEVICE9 : | |
1239 | case CUPS_CSPACE_DEVICEA : | |
1240 | case CUPS_CSPACE_DEVICEB : | |
1241 | case CUPS_CSPACE_DEVICEC : | |
1242 | case CUPS_CSPACE_DEVICED : | |
1243 | case CUPS_CSPACE_DEVICEE : | |
1244 | case CUPS_CSPACE_DEVICEF : | |
1245 | r->header.cupsNumColors = r->header.cupsColorSpace - | |
1246 | CUPS_CSPACE_DEVICE1 + 1; | |
1247 | break; | |
ef416fc2 | 1248 | } |
1249 | } | |
1250 | ||
1251 | /* | |
1252 | * Set the number of bytes per pixel/color... | |
1253 | */ | |
1254 | ||
1255 | if (r->header.cupsColorOrder == CUPS_ORDER_CHUNKED) | |
1256 | r->bpp = (r->header.cupsBitsPerPixel + 7) / 8; | |
1257 | else | |
1258 | r->bpp = (r->header.cupsBitsPerColor + 7) / 8; | |
1259 | ||
1260 | /* | |
1261 | * Set the number of remaining rows... | |
1262 | */ | |
1263 | ||
1264 | if (r->header.cupsColorOrder == CUPS_ORDER_PLANAR) | |
1265 | r->remaining = r->header.cupsHeight * r->header.cupsNumColors; | |
1266 | else | |
1267 | r->remaining = r->header.cupsHeight; | |
1268 | ||
1269 | /* | |
ed486911 | 1270 | * Allocate the compression buffer... |
ef416fc2 | 1271 | */ |
1272 | ||
ed486911 | 1273 | if (r->compressed) |
1274 | { | |
1275 | if (r->pixels != NULL) | |
1276 | free(r->pixels); | |
ef416fc2 | 1277 | |
ed486911 | 1278 | r->pixels = calloc(r->header.cupsBytesPerLine, 1); |
1279 | r->pcurrent = r->pixels; | |
1280 | r->pend = r->pixels + r->header.cupsBytesPerLine; | |
1281 | r->count = 0; | |
1282 | } | |
ef416fc2 | 1283 | } |
1284 | ||
1285 | ||
f7deaa1a | 1286 | /* |
1287 | * 'cups_raster_write()' - Write a row of compressed raster data... | |
1288 | */ | |
1289 | ||
1290 | static int /* O - Number of bytes written */ | |
1291 | cups_raster_write( | |
1292 | cups_raster_t *r, /* I - Raster stream */ | |
1293 | const unsigned char *pixels) /* I - Pixel data to write */ | |
1294 | { | |
1295 | const unsigned char *start, /* Start of sequence */ | |
1296 | *ptr, /* Current pointer in sequence */ | |
1297 | *pend, /* End of raster buffer */ | |
1298 | *plast; /* Pointer to last pixel */ | |
1299 | unsigned char *wptr; /* Pointer into write buffer */ | |
1300 | int bpp, /* Bytes per pixel */ | |
1f0275e3 | 1301 | count; /* Count */ |
f7deaa1a | 1302 | |
1303 | ||
09a101d6 | 1304 | DEBUG_printf(("cups_raster_write(r=%p, pixels=%p)\n", r, pixels)); |
f7deaa1a | 1305 | |
1306 | /* | |
1307 | * Allocate a write buffer as needed... | |
1308 | */ | |
1309 | ||
1310 | count = r->header.cupsBytesPerLine * 2; | |
c8fef167 | 1311 | if ((size_t)count > r->bufsize) |
f7deaa1a | 1312 | { |
1313 | if (r->buffer) | |
1314 | wptr = realloc(r->buffer, count); | |
1315 | else | |
1316 | wptr = malloc(count); | |
1317 | ||
1318 | if (!wptr) | |
1319 | return (-1); | |
1320 | ||
1321 | r->buffer = wptr; | |
1322 | r->bufsize = count; | |
1323 | } | |
1324 | ||
1325 | /* | |
1326 | * Write the row repeat count... | |
1327 | */ | |
1328 | ||
1329 | bpp = r->bpp; | |
1330 | pend = pixels + r->header.cupsBytesPerLine; | |
1331 | plast = pend - bpp; | |
1332 | wptr = r->buffer; | |
1333 | *wptr++ = r->count - 1; | |
f7deaa1a | 1334 | |
1335 | /* | |
07ed0e9a | 1336 | * Write using a modified PackBits compression... |
f7deaa1a | 1337 | */ |
1338 | ||
1339 | for (ptr = pixels; ptr < pend;) | |
1340 | { | |
1341 | start = ptr; | |
1342 | ptr += bpp; | |
1343 | ||
1344 | if (ptr == pend) | |
1345 | { | |
1346 | /* | |
1347 | * Encode a single pixel at the end... | |
1348 | */ | |
1349 | ||
1350 | *wptr++ = 0; | |
1351 | for (count = bpp; count > 0; count --) | |
1352 | *wptr++ = *start++; | |
1353 | } | |
1354 | else if (!memcmp(start, ptr, bpp)) | |
1355 | { | |
1356 | /* | |
1357 | * Encode a sequence of repeating pixels... | |
1358 | */ | |
1359 | ||
1360 | for (count = 2; count < 128 && ptr < plast; count ++, ptr += bpp) | |
1361 | if (memcmp(ptr, ptr + bpp, bpp)) | |
1362 | break; | |
1363 | ||
1364 | *wptr++ = count - 1; | |
1365 | for (count = bpp; count > 0; count --) | |
1366 | *wptr++ = *ptr++; | |
1367 | } | |
1368 | else | |
1369 | { | |
1370 | /* | |
1371 | * Encode a sequence of non-repeating pixels... | |
1372 | */ | |
1373 | ||
07ed0e9a | 1374 | for (count = 1; count < 128 && ptr < plast; count ++, ptr += bpp) |
f7deaa1a | 1375 | if (!memcmp(ptr, ptr + bpp, bpp)) |
1376 | break; | |
1377 | ||
1378 | if (ptr >= plast && count < 128) | |
1379 | { | |
1380 | count ++; | |
1381 | ptr += bpp; | |
1382 | } | |
f14324a7 | 1383 | |
f7deaa1a | 1384 | *wptr++ = 257 - count; |
1385 | ||
1386 | count *= bpp; | |
1387 | memcpy(wptr, start, count); | |
1388 | wptr += count; | |
1389 | } | |
1390 | } | |
1391 | ||
a2326b5b | 1392 | return (cups_raster_io(r, r->buffer, (int)(wptr - r->buffer))); |
f7deaa1a | 1393 | } |
1394 | ||
1395 | ||
ef416fc2 | 1396 | /* |
c8fef167 | 1397 | * 'cups_read_fd()' - Read bytes from a file. |
ef416fc2 | 1398 | */ |
1399 | ||
c8fef167 MS |
1400 | static ssize_t /* O - Bytes read or -1 */ |
1401 | cups_read_fd(void *ctx, /* I - File descriptor as pointer */ | |
1402 | unsigned char *buf, /* I - Buffer for read */ | |
f14324a7 | 1403 | size_t bytes) /* I - Maximum number of bytes to read */ |
ef416fc2 | 1404 | { |
c8fef167 MS |
1405 | int fd = (int)((intptr_t)ctx); |
1406 | /* File descriptor */ | |
1407 | ssize_t count; /* Number of bytes read */ | |
ef416fc2 | 1408 | |
ef416fc2 | 1409 | |
a2326b5b MS |
1410 | #ifdef WIN32 /* Sigh */ |
1411 | while ((count = read(fd, buf, (unsigned)bytes)) < 0) | |
1412 | #else | |
f14324a7 | 1413 | while ((count = read(fd, buf, bytes)) < 0) |
a2326b5b | 1414 | #endif /* WIN32 */ |
f14324a7 MS |
1415 | if (errno != EINTR && errno != EAGAIN) |
1416 | return (-1); | |
ef416fc2 | 1417 | |
f14324a7 | 1418 | return (count); |
ef416fc2 | 1419 | } |
1420 | ||
1421 | ||
ed486911 | 1422 | /* |
1423 | * 'cups_swap()' - Swap bytes in raster data... | |
1424 | */ | |
1425 | ||
1426 | static void | |
1427 | cups_swap(unsigned char *buf, /* I - Buffer to swap */ | |
1428 | int bytes) /* I - Number of bytes to swap */ | |
1429 | { | |
1430 | unsigned char even, odd; /* Temporary variables */ | |
1431 | ||
1432 | ||
1433 | bytes /= 2; | |
1434 | ||
1435 | while (bytes > 0) | |
1436 | { | |
1437 | even = buf[0]; | |
1438 | odd = buf[1]; | |
1439 | buf[0] = odd; | |
1440 | buf[1] = even; | |
1441 | ||
1442 | buf += 2; | |
1443 | bytes --; | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | ||
ef416fc2 | 1448 | /* |
c8fef167 | 1449 | * 'cups_write_fd()' - Write bytes to a file. |
ef416fc2 | 1450 | */ |
1451 | ||
c8fef167 MS |
1452 | static ssize_t /* O - Bytes written or -1 */ |
1453 | cups_write_fd(void *ctx, /* I - File descriptor pointer */ | |
1454 | unsigned char *buf, /* I - Bytes to write */ | |
1455 | size_t bytes) /* I - Number of bytes to write */ | |
ef416fc2 | 1456 | { |
c8fef167 MS |
1457 | int fd = (int)((intptr_t)ctx); |
1458 | /* File descriptor */ | |
1459 | ssize_t count; /* Number of bytes written */ | |
ef416fc2 | 1460 | |
ef416fc2 | 1461 | |
a2326b5b MS |
1462 | #ifdef WIN32 /* Sigh */ |
1463 | while ((count = write(fd, buf, (unsigned)bytes)) < 0) | |
1464 | #else | |
f14324a7 | 1465 | while ((count = write(fd, buf, bytes)) < 0) |
a2326b5b | 1466 | #endif /* WIN32 */ |
f14324a7 MS |
1467 | if (errno != EINTR && errno != EAGAIN) |
1468 | return (-1); | |
ef416fc2 | 1469 | |
f14324a7 | 1470 | return (count); |
ef416fc2 | 1471 | } |
1472 | ||
1473 | ||
1474 | /* | |
b19ccc9e | 1475 | * End of "$Id: raster.c 7720 2008-07-11 22:46:21Z mike $". |
ef416fc2 | 1476 | */ |