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