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