]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
bc44d920 | 2 | * "$Id: image.c 6649 2007-07-11 21:46:42Z mike $" |
ef416fc2 | 3 | * |
4 | * Base image support for the Common UNIX Printing System (CUPS). | |
5 | * | |
91c84a35 | 6 | * Copyright 2007-2008 by Apple Inc. |
ef416fc2 | 7 | * Copyright 1993-2005 by Easy Software Products. |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
19 | * cupsImageClose() - Close an image file. | |
20 | * cupsImageGetCol() - Get a column of pixels from an image. | |
21 | * cupsImageGetColorSpace() - Get the image colorspace. | |
22 | * cupsImageGetDepth() - Get the number of bytes per pixel. | |
23 | * cupsImageGetHeight() - Get the height of an image. | |
24 | * cupsImageGetRow() - Get a row of pixels from an image. | |
25 | * cupsImageGetWidth() - Get the width of an image. | |
26 | * cupsImageGetXPPI() - Get the horizontal resolution of an image. | |
27 | * cupsImageGetYPPI() - Get the vertical resolution of an image. | |
28 | * cupsImageOpen() - Open an image file and read it into memory. | |
29 | * _cupsImagePutCol() - Put a column of pixels to an image. | |
30 | * _cupsImagePutRow() - Put a row of pixels to an image. | |
31 | * cupsImageSetMaxTiles() - Set the maximum number of tiles to cache. | |
32 | * flush_tile() - Flush the least-recently-used tile in the cache. | |
33 | * get_tile() - Get a cached tile. | |
34 | */ | |
35 | ||
36 | /* | |
37 | * Include necessary headers... | |
38 | */ | |
39 | ||
40 | #include "image-private.h" | |
41 | ||
42 | ||
43 | /* | |
44 | * Local functions... | |
45 | */ | |
46 | ||
47 | static void flush_tile(cups_image_t *img); | |
48 | static cups_ib_t *get_tile(cups_image_t *img, int x, int y); | |
49 | ||
50 | ||
51 | /* | |
52 | * 'cupsImageClose()' - Close an image file. | |
53 | */ | |
54 | ||
55 | void | |
56 | cupsImageClose(cups_image_t *img) /* I - Image to close */ | |
57 | { | |
58 | cups_ic_t *current, /* Current cached tile */ | |
59 | *next; /* Next cached tile */ | |
60 | ||
61 | ||
62 | /* | |
63 | * Wipe the tile cache file (if any)... | |
64 | */ | |
65 | ||
3dfe78b3 | 66 | if (img->cachefile >= 0) |
ef416fc2 | 67 | { |
68 | DEBUG_printf(("Closing/removing swap file \"%s\"...\n", img->cachename)); | |
69 | ||
3dfe78b3 | 70 | close(img->cachefile); |
ef416fc2 | 71 | unlink(img->cachename); |
72 | } | |
73 | ||
74 | /* | |
75 | * Free the image cache... | |
76 | */ | |
77 | ||
78 | DEBUG_puts("Freeing memory..."); | |
79 | ||
80 | for (current = img->first, next = NULL; current != NULL; current = next) | |
81 | { | |
82 | DEBUG_printf(("Freeing cache (%p, next = %p)...\n", current, next)); | |
83 | ||
84 | next = current->next; | |
85 | free(current); | |
86 | } | |
87 | ||
88 | /* | |
89 | * Free the rest of memory... | |
90 | */ | |
91 | ||
92 | if (img->tiles != NULL) | |
93 | { | |
94 | DEBUG_printf(("Freeing tiles (%p)...\n", img->tiles[0])); | |
95 | ||
96 | free(img->tiles[0]); | |
97 | ||
98 | DEBUG_printf(("Freeing tile pointers (%p)...\n", img->tiles)); | |
99 | ||
100 | free(img->tiles); | |
101 | } | |
102 | ||
103 | free(img); | |
104 | } | |
105 | ||
106 | ||
107 | /* | |
108 | * 'cupsImageGetCol()' - Get a column of pixels from an image. | |
109 | */ | |
110 | ||
111 | int /* O - -1 on error, 0 on success */ | |
112 | cupsImageGetCol(cups_image_t *img, /* I - Image */ | |
113 | int x, /* I - Column */ | |
114 | int y, /* I - Start row */ | |
115 | int height, /* I - Column height */ | |
116 | cups_ib_t *pixels) /* O - Pixel data */ | |
117 | { | |
118 | int bpp, /* Bytes per pixel */ | |
119 | twidth, /* Tile width */ | |
120 | count; /* Number of pixels to get */ | |
121 | const cups_ib_t *ib; /* Pointer into tile */ | |
122 | ||
123 | ||
124 | if (img == NULL || x < 0 || x >= img->xsize || y >= img->ysize) | |
125 | return (-1); | |
126 | ||
127 | if (y < 0) | |
128 | { | |
129 | height += y; | |
130 | y = 0; | |
131 | } | |
132 | ||
133 | if ((y + height) > img->ysize) | |
134 | height = img->ysize - y; | |
135 | ||
136 | if (height < 1) | |
137 | return (-1); | |
138 | ||
139 | bpp = cupsImageGetDepth(img); | |
140 | twidth = bpp * (CUPS_TILE_SIZE - 1); | |
141 | ||
142 | while (height > 0) | |
143 | { | |
144 | ib = get_tile(img, x, y); | |
145 | ||
146 | if (ib == NULL) | |
147 | return (-1); | |
148 | ||
149 | count = CUPS_TILE_SIZE - (y & (CUPS_TILE_SIZE - 1)); | |
150 | if (count > height) | |
151 | count = height; | |
152 | ||
153 | y += count; | |
154 | height -= count; | |
155 | ||
156 | for (; count > 0; count --, ib += twidth) | |
157 | switch (bpp) | |
158 | { | |
159 | case 4 : | |
160 | *pixels++ = *ib++; | |
161 | case 3 : | |
162 | *pixels++ = *ib++; | |
163 | *pixels++ = *ib++; | |
164 | case 1 : | |
165 | *pixels++ = *ib++; | |
166 | break; | |
167 | } | |
168 | } | |
169 | ||
170 | return (0); | |
171 | } | |
172 | ||
173 | ||
174 | /* | |
175 | * 'cupsImageGetColorSpace()' - Get the image colorspace. | |
176 | */ | |
177 | ||
178 | cups_icspace_t /* O - Colorspace */ | |
179 | cupsImageGetColorSpace( | |
180 | cups_image_t *img) /* I - Image */ | |
181 | { | |
182 | return (img->colorspace); | |
183 | } | |
184 | ||
185 | ||
186 | /* | |
187 | * 'cupsImageGetDepth()' - Get the number of bytes per pixel. | |
188 | */ | |
189 | ||
190 | int /* O - Bytes per pixel */ | |
191 | cupsImageGetDepth(cups_image_t *img) /* I - Image */ | |
192 | { | |
193 | return (abs(img->colorspace)); | |
194 | } | |
195 | ||
196 | ||
197 | /* | |
198 | * 'cupsImageGetHeight()' - Get the height of an image. | |
199 | */ | |
200 | ||
201 | unsigned /* O - Height in pixels */ | |
202 | cupsImageGetHeight(cups_image_t *img) /* I - Image */ | |
203 | { | |
204 | return (img->ysize); | |
205 | } | |
206 | ||
207 | ||
208 | /* | |
209 | * 'cupsImageGetRow()' - Get a row of pixels from an image. | |
210 | */ | |
211 | ||
212 | int /* O - -1 on error, 0 on success */ | |
213 | cupsImageGetRow(cups_image_t *img, /* I - Image */ | |
214 | int x, /* I - Start column */ | |
215 | int y, /* I - Row */ | |
216 | int width, /* I - Width of row */ | |
217 | cups_ib_t *pixels) /* O - Pixel data */ | |
218 | { | |
219 | int bpp, /* Bytes per pixel */ | |
220 | count; /* Number of pixels to get */ | |
221 | const cups_ib_t *ib; /* Pointer to pixels */ | |
222 | ||
223 | ||
224 | if (img == NULL || y < 0 || y >= img->ysize || x >= img->xsize) | |
225 | return (-1); | |
226 | ||
227 | if (x < 0) | |
228 | { | |
229 | width += x; | |
230 | x = 0; | |
231 | } | |
232 | ||
233 | if ((x + width) > img->xsize) | |
234 | width = img->xsize - x; | |
235 | ||
236 | if (width < 1) | |
237 | return (-1); | |
238 | ||
239 | bpp = img->colorspace < 0 ? -img->colorspace : img->colorspace; | |
240 | ||
241 | while (width > 0) | |
242 | { | |
243 | ib = get_tile(img, x, y); | |
244 | ||
245 | if (ib == NULL) | |
246 | return (-1); | |
247 | ||
248 | count = CUPS_TILE_SIZE - (x & (CUPS_TILE_SIZE - 1)); | |
249 | if (count > width) | |
250 | count = width; | |
251 | memcpy(pixels, ib, count * bpp); | |
252 | pixels += count * bpp; | |
253 | x += count; | |
254 | width -= count; | |
255 | } | |
256 | ||
257 | return (0); | |
258 | } | |
259 | ||
260 | ||
261 | /* | |
262 | * 'cupsImageGetWidth()' - Get the width of an image. | |
263 | */ | |
264 | ||
265 | unsigned /* O - Width in pixels */ | |
266 | cupsImageGetWidth(cups_image_t *img) /* I - Image */ | |
267 | { | |
268 | return (img->xsize); | |
269 | } | |
270 | ||
271 | ||
272 | /* | |
273 | * 'cupsImageGetXPPI()' - Get the horizontal resolution of an image. | |
274 | */ | |
275 | ||
276 | unsigned /* O - Horizontal PPI */ | |
277 | cupsImageGetXPPI(cups_image_t *img) /* I - Image */ | |
278 | { | |
279 | return (img->xppi); | |
280 | } | |
281 | ||
282 | ||
283 | /* | |
284 | * 'cupsImageGetYPPI()' - Get the vertical resolution of an image. | |
285 | */ | |
286 | ||
287 | unsigned /* O - Vertical PPI */ | |
288 | cupsImageGetYPPI(cups_image_t *img) /* I - Image */ | |
289 | { | |
290 | return (img->yppi); | |
291 | } | |
292 | ||
293 | ||
294 | /* | |
295 | * 'cupsImageOpen()' - Open an image file and read it into memory. | |
296 | */ | |
297 | ||
298 | cups_image_t * /* O - New image */ | |
299 | cupsImageOpen( | |
300 | const char *filename, /* I - Filename of image */ | |
301 | cups_icspace_t primary, /* I - Primary colorspace needed */ | |
302 | cups_icspace_t secondary, /* I - Secondary colorspace if primary no good */ | |
303 | int saturation, /* I - Color saturation level */ | |
304 | int hue, /* I - Color hue adjustment */ | |
305 | const cups_ib_t *lut) /* I - RGB gamma/brightness LUT */ | |
306 | { | |
307 | FILE *fp; /* File pointer */ | |
308 | unsigned char header[16], /* First 16 bytes of file */ | |
309 | header2[16]; /* Bytes 2048-2064 (PhotoCD) */ | |
310 | cups_image_t *img; /* New image buffer */ | |
311 | int status; /* Status of load... */ | |
312 | ||
313 | ||
314 | DEBUG_printf(("cupsImageOpen(\"%s\", %d, %d, %d, %d, %p)\n", | |
315 | filename ? filename : "(null)", primary, secondary, | |
316 | saturation, hue, lut)); | |
317 | ||
318 | /* | |
319 | * Figure out the file type... | |
320 | */ | |
321 | ||
322 | if ((fp = fopen(filename, "r")) == NULL) | |
91c84a35 | 323 | return (NULL); |
ef416fc2 | 324 | |
325 | if (fread(header, 1, sizeof(header), fp) == 0) | |
326 | { | |
ef416fc2 | 327 | fclose(fp); |
328 | return (NULL); | |
329 | } | |
330 | ||
331 | fseek(fp, 2048, SEEK_SET); | |
332 | memset(header2, 0, sizeof(header2)); | |
333 | fread(header2, 1, sizeof(header2), fp); | |
334 | fseek(fp, 0, SEEK_SET); | |
335 | ||
336 | /* | |
337 | * Allocate memory... | |
338 | */ | |
339 | ||
340 | img = calloc(sizeof(cups_image_t), 1); | |
341 | ||
342 | if (img == NULL) | |
343 | { | |
91c84a35 | 344 | fclose(fp); |
ef416fc2 | 345 | return (NULL); |
346 | } | |
347 | ||
348 | /* | |
349 | * Load the image as appropriate... | |
350 | */ | |
351 | ||
352 | img->max_ics = CUPS_TILE_MINIMUM; | |
353 | img->xppi = 128; | |
354 | img->yppi = 128; | |
355 | ||
356 | if (!memcmp(header, "GIF87a", 6) || !memcmp(header, "GIF89a", 6)) | |
357 | status = _cupsImageReadGIF(img, fp, primary, secondary, saturation, hue, | |
358 | lut); | |
359 | else if (!memcmp(header, "BM", 2)) | |
360 | status = _cupsImageReadBMP(img, fp, primary, secondary, saturation, hue, | |
361 | lut); | |
362 | else if (header[0] == 0x01 && header[1] == 0xda) | |
363 | status = _cupsImageReadSGI(img, fp, primary, secondary, saturation, hue, | |
364 | lut); | |
365 | else if (header[0] == 0x59 && header[1] == 0xa6 && | |
366 | header[2] == 0x6a && header[3] == 0x95) | |
367 | status = _cupsImageReadSunRaster(img, fp, primary, secondary, saturation, | |
368 | hue, lut); | |
369 | else if (header[0] == 'P' && header[1] >= '1' && header[1] <= '6') | |
370 | status = _cupsImageReadPNM(img, fp, primary, secondary, saturation, hue, | |
371 | lut); | |
372 | else if (!memcmp(header2, "PCD_IPI", 7)) | |
373 | status = _cupsImageReadPhotoCD(img, fp, primary, secondary, saturation, | |
374 | hue, lut); | |
375 | else if (!memcmp(header + 8, "\000\010", 2) || | |
376 | !memcmp(header + 8, "\000\030", 2)) | |
377 | status = _cupsImageReadPIX(img, fp, primary, secondary, saturation, hue, | |
378 | lut); | |
379 | #if defined(HAVE_LIBPNG) && defined(HAVE_LIBZ) | |
380 | else if (!memcmp(header, "\211PNG", 4)) | |
381 | status = _cupsImageReadPNG(img, fp, primary, secondary, saturation, hue, | |
382 | lut); | |
383 | #endif /* HAVE_LIBPNG && HAVE_LIBZ */ | |
384 | #ifdef HAVE_LIBJPEG | |
385 | else if (!memcmp(header, "\377\330\377", 3) && /* Start-of-Image */ | |
386 | header[3] >= 0xe0 && header[3] <= 0xef) /* APPn */ | |
387 | status = _cupsImageReadJPEG(img, fp, primary, secondary, saturation, hue, | |
388 | lut); | |
389 | #endif /* HAVE_LIBJPEG */ | |
390 | #ifdef HAVE_LIBTIFF | |
391 | else if (!memcmp(header, "MM\000\052", 4) || | |
392 | !memcmp(header, "II\052\000", 4)) | |
393 | status = _cupsImageReadTIFF(img, fp, primary, secondary, saturation, hue, | |
394 | lut); | |
395 | #endif /* HAVE_LIBTIFF */ | |
396 | else | |
397 | { | |
91c84a35 | 398 | fclose(fp); |
ef416fc2 | 399 | status = -1; |
400 | } | |
401 | ||
402 | if (status) | |
403 | { | |
404 | free(img); | |
405 | return (NULL); | |
406 | } | |
407 | else | |
408 | return (img); | |
409 | } | |
410 | ||
411 | ||
412 | /* | |
413 | * '_cupsImagePutCol()' - Put a column of pixels to an image. | |
414 | */ | |
415 | ||
416 | int /* O - -1 on error, 0 on success */ | |
417 | _cupsImagePutCol( | |
418 | cups_image_t *img, /* I - Image */ | |
419 | int x, /* I - Column */ | |
420 | int y, /* I - Start row */ | |
421 | int height, /* I - Column height */ | |
422 | const cups_ib_t *pixels) /* I - Pixels to put */ | |
423 | { | |
424 | int bpp, /* Bytes per pixel */ | |
425 | twidth, /* Width of tile */ | |
426 | count; /* Number of pixels to put */ | |
427 | int tilex, /* Column within tile */ | |
428 | tiley; /* Row within tile */ | |
429 | cups_ib_t *ib; /* Pointer to pixels in tile */ | |
430 | ||
431 | ||
432 | if (img == NULL || x < 0 || x >= img->xsize || y >= img->ysize) | |
433 | return (-1); | |
434 | ||
435 | if (y < 0) | |
436 | { | |
437 | height += y; | |
438 | y = 0; | |
439 | } | |
440 | ||
441 | if ((y + height) > img->ysize) | |
442 | height = img->ysize - y; | |
443 | ||
444 | if (height < 1) | |
445 | return (-1); | |
446 | ||
447 | bpp = cupsImageGetDepth(img); | |
448 | twidth = bpp * (CUPS_TILE_SIZE - 1); | |
449 | tilex = x / CUPS_TILE_SIZE; | |
450 | tiley = y / CUPS_TILE_SIZE; | |
451 | ||
452 | while (height > 0) | |
453 | { | |
454 | ib = get_tile(img, x, y); | |
455 | ||
456 | if (ib == NULL) | |
457 | return (-1); | |
458 | ||
459 | img->tiles[tiley][tilex].dirty = 1; | |
460 | tiley ++; | |
461 | ||
462 | count = CUPS_TILE_SIZE - (y & (CUPS_TILE_SIZE - 1)); | |
463 | if (count > height) | |
464 | count = height; | |
465 | ||
466 | y += count; | |
467 | height -= count; | |
468 | ||
469 | for (; count > 0; count --, ib += twidth) | |
470 | switch (bpp) | |
471 | { | |
472 | case 4 : | |
473 | *ib++ = *pixels++; | |
474 | case 3 : | |
475 | *ib++ = *pixels++; | |
476 | *ib++ = *pixels++; | |
477 | case 1 : | |
478 | *ib++ = *pixels++; | |
479 | break; | |
480 | } | |
481 | } | |
482 | ||
483 | return (0); | |
484 | } | |
485 | ||
486 | ||
487 | /* | |
488 | * '_cupsImagePutRow()' - Put a row of pixels to an image. | |
489 | */ | |
490 | ||
491 | int /* O - -1 on error, 0 on success */ | |
492 | _cupsImagePutRow( | |
493 | cups_image_t *img, /* I - Image */ | |
494 | int x, /* I - Start column */ | |
495 | int y, /* I - Row */ | |
496 | int width, /* I - Row width */ | |
497 | const cups_ib_t *pixels) /* I - Pixel data */ | |
498 | { | |
499 | int bpp, /* Bytes per pixel */ | |
500 | count; /* Number of pixels to put */ | |
501 | int tilex, /* Column within tile */ | |
502 | tiley; /* Row within tile */ | |
503 | cups_ib_t *ib; /* Pointer to pixels in tile */ | |
504 | ||
505 | ||
506 | if (img == NULL || y < 0 || y >= img->ysize || x >= img->xsize) | |
507 | return (-1); | |
508 | ||
509 | if (x < 0) | |
510 | { | |
511 | width += x; | |
512 | x = 0; | |
513 | } | |
514 | ||
515 | if ((x + width) > img->xsize) | |
516 | width = img->xsize - x; | |
517 | ||
518 | if (width < 1) | |
519 | return (-1); | |
520 | ||
521 | bpp = img->colorspace < 0 ? -img->colorspace : img->colorspace; | |
522 | tilex = x / CUPS_TILE_SIZE; | |
523 | tiley = y / CUPS_TILE_SIZE; | |
524 | ||
525 | while (width > 0) | |
526 | { | |
527 | ib = get_tile(img, x, y); | |
528 | ||
529 | if (ib == NULL) | |
530 | return (-1); | |
531 | ||
532 | img->tiles[tiley][tilex].dirty = 1; | |
533 | ||
534 | count = CUPS_TILE_SIZE - (x & (CUPS_TILE_SIZE - 1)); | |
535 | if (count > width) | |
536 | count = width; | |
537 | memcpy(ib, pixels, count * bpp); | |
538 | pixels += count * bpp; | |
539 | x += count; | |
540 | width -= count; | |
541 | tilex ++; | |
542 | } | |
543 | ||
544 | return (0); | |
545 | } | |
546 | ||
547 | ||
548 | /* | |
549 | * 'cupsImageSetMaxTiles()' - Set the maximum number of tiles to cache. | |
550 | * | |
551 | * If the "max_tiles" argument is 0 then the maximum number of tiles is | |
552 | * computed from the image size or the RIP_CACHE environment variable. | |
553 | */ | |
554 | ||
555 | void | |
556 | cupsImageSetMaxTiles( | |
557 | cups_image_t *img, /* I - Image to set */ | |
558 | int max_tiles) /* I - Number of tiles to cache */ | |
559 | { | |
560 | int cache_size, /* Size of tile cache in bytes */ | |
561 | min_tiles, /* Minimum number of tiles to cache */ | |
562 | max_size; /* Maximum cache size in bytes */ | |
563 | char *cache_env, /* Cache size environment variable */ | |
564 | cache_units[255]; /* Cache size units */ | |
565 | ||
566 | ||
567 | min_tiles = max(CUPS_TILE_MINIMUM, | |
568 | 1 + max((img->xsize + CUPS_TILE_SIZE - 1) / CUPS_TILE_SIZE, | |
569 | (img->ysize + CUPS_TILE_SIZE - 1) / CUPS_TILE_SIZE)); | |
570 | ||
571 | if (max_tiles == 0) | |
572 | max_tiles = ((img->xsize + CUPS_TILE_SIZE - 1) / CUPS_TILE_SIZE) * | |
573 | ((img->ysize + CUPS_TILE_SIZE - 1) / CUPS_TILE_SIZE); | |
574 | ||
575 | cache_size = max_tiles * CUPS_TILE_SIZE * CUPS_TILE_SIZE * | |
576 | cupsImageGetDepth(img); | |
577 | ||
578 | if ((cache_env = getenv("RIP_MAX_CACHE")) != NULL) | |
579 | { | |
580 | switch (sscanf(cache_env, "%d%254s", &max_size, cache_units)) | |
581 | { | |
582 | case 0 : | |
583 | max_size = 32 * 1024 * 1024; | |
584 | break; | |
585 | case 1 : | |
586 | max_size *= 4 * CUPS_TILE_SIZE * CUPS_TILE_SIZE; | |
587 | break; | |
588 | case 2 : | |
589 | if (tolower(cache_units[0] & 255) == 'g') | |
590 | max_size *= 1024 * 1024 * 1024; | |
591 | else if (tolower(cache_units[0] & 255) == 'm') | |
592 | max_size *= 1024 * 1024; | |
593 | else if (tolower(cache_units[0] & 255) == 'k') | |
594 | max_size *= 1024; | |
595 | else if (tolower(cache_units[0] & 255) == 't') | |
596 | max_size *= 4 * CUPS_TILE_SIZE * CUPS_TILE_SIZE; | |
597 | break; | |
598 | } | |
599 | } | |
600 | else | |
601 | max_size = 32 * 1024 * 1024; | |
602 | ||
603 | if (cache_size > max_size) | |
604 | max_tiles = max_size / CUPS_TILE_SIZE / CUPS_TILE_SIZE / | |
605 | cupsImageGetDepth(img); | |
606 | ||
607 | if (max_tiles < min_tiles) | |
608 | max_tiles = min_tiles; | |
609 | ||
610 | img->max_ics = max_tiles; | |
611 | ||
612 | DEBUG_printf(("max_ics=%d...\n", img->max_ics)); | |
613 | } | |
614 | ||
615 | ||
616 | /* | |
617 | * 'flush_tile()' - Flush the least-recently-used tile in the cache. | |
618 | */ | |
619 | ||
620 | static void | |
621 | flush_tile(cups_image_t *img) /* I - Image */ | |
622 | { | |
ef416fc2 | 623 | int bpp; /* Bytes per pixel */ |
624 | cups_itile_t *tile; /* Pointer to tile */ | |
625 | ||
626 | ||
627 | bpp = cupsImageGetDepth(img); | |
628 | tile = img->first->tile; | |
629 | ||
630 | if (!tile->dirty) | |
631 | { | |
632 | tile->ic = NULL; | |
633 | return; | |
634 | } | |
635 | ||
3dfe78b3 | 636 | if (img->cachefile < 0) |
ef416fc2 | 637 | { |
3dfe78b3 MS |
638 | if ((img->cachefile = cupsTempFd(img->cachename, |
639 | sizeof(img->cachename))) < 0) | |
ef416fc2 | 640 | { |
3dfe78b3 | 641 | tile->ic = NULL; |
ef416fc2 | 642 | tile->dirty = 0; |
643 | return; | |
644 | } | |
645 | ||
646 | DEBUG_printf(("Created swap file \"%s\"...\n", img->cachename)); | |
3dfe78b3 | 647 | } |
ef416fc2 | 648 | |
3dfe78b3 MS |
649 | if (tile->pos >= 0) |
650 | { | |
651 | if (lseek(img->cachefile, tile->pos, SEEK_SET) != tile->pos) | |
ef416fc2 | 652 | { |
ef416fc2 | 653 | tile->ic = NULL; |
654 | tile->dirty = 0; | |
655 | return; | |
656 | } | |
657 | } | |
ef416fc2 | 658 | else |
659 | { | |
3dfe78b3 | 660 | if ((tile->pos = lseek(img->cachefile, 0, SEEK_END)) < 0) |
ef416fc2 | 661 | { |
3dfe78b3 | 662 | tile->ic = NULL; |
ef416fc2 | 663 | tile->dirty = 0; |
664 | return; | |
665 | } | |
ef416fc2 | 666 | } |
667 | ||
3dfe78b3 | 668 | write(img->cachefile, tile->ic->pixels, bpp * CUPS_TILE_SIZE * CUPS_TILE_SIZE); |
ef416fc2 | 669 | |
ef416fc2 | 670 | tile->ic = NULL; |
671 | tile->dirty = 0; | |
672 | } | |
673 | ||
674 | ||
675 | /* | |
676 | * 'get_tile()' - Get a cached tile. | |
677 | */ | |
678 | ||
679 | static cups_ib_t * /* O - Pointer to tile or NULL */ | |
680 | get_tile(cups_image_t *img, /* I - Image */ | |
681 | int x, /* I - Column in image */ | |
682 | int y) /* I - Row in image */ | |
683 | { | |
684 | int bpp, /* Bytes per pixel */ | |
685 | tilex, /* Column within tile */ | |
686 | tiley, /* Row within tile */ | |
687 | xtiles, /* Number of tiles horizontally */ | |
688 | ytiles; /* Number of tiles vertically */ | |
689 | cups_ic_t *ic; /* Cache pointer */ | |
690 | cups_itile_t *tile; /* Tile pointer */ | |
691 | ||
692 | ||
693 | if (img->tiles == NULL) | |
694 | { | |
695 | xtiles = (img->xsize + CUPS_TILE_SIZE - 1) / CUPS_TILE_SIZE; | |
696 | ytiles = (img->ysize + CUPS_TILE_SIZE - 1) / CUPS_TILE_SIZE; | |
697 | ||
698 | DEBUG_printf(("Creating tile array (%dx%d)\n", xtiles, ytiles)); | |
699 | ||
91c84a35 MS |
700 | if ((img->tiles = calloc(sizeof(cups_itile_t *), ytiles)) == NULL) |
701 | return (NULL); | |
702 | ||
3dfe78b3 | 703 | if ((tile = calloc(xtiles * sizeof(cups_itile_t), ytiles)) == NULL) |
91c84a35 | 704 | return (NULL); |
ef416fc2 | 705 | |
706 | for (tiley = 0; tiley < ytiles; tiley ++) | |
707 | { | |
708 | img->tiles[tiley] = tile; | |
709 | for (tilex = xtiles; tilex > 0; tilex --, tile ++) | |
710 | tile->pos = -1; | |
711 | } | |
712 | } | |
713 | ||
714 | bpp = cupsImageGetDepth(img); | |
715 | tilex = x / CUPS_TILE_SIZE; | |
716 | tiley = y / CUPS_TILE_SIZE; | |
717 | tile = img->tiles[tiley] + tilex; | |
718 | x &= (CUPS_TILE_SIZE - 1); | |
719 | y &= (CUPS_TILE_SIZE - 1); | |
720 | ||
721 | if ((ic = tile->ic) == NULL) | |
722 | { | |
723 | if (img->num_ics < img->max_ics) | |
724 | { | |
91c84a35 MS |
725 | if ((ic = calloc(sizeof(cups_ic_t) + |
726 | bpp * CUPS_TILE_SIZE * CUPS_TILE_SIZE, 1)) == NULL) | |
727 | { | |
728 | if (img->num_ics == 0) | |
729 | return (NULL); | |
730 | ||
731 | flush_tile(img); | |
732 | ic = img->first; | |
733 | } | |
734 | else | |
735 | { | |
736 | ic->pixels = ((cups_ib_t *)ic) + sizeof(cups_ic_t); | |
ef416fc2 | 737 | |
91c84a35 | 738 | img->num_ics ++; |
ef416fc2 | 739 | |
91c84a35 MS |
740 | DEBUG_printf(("Allocated cache tile %d (%p)...\n", img->num_ics, ic)); |
741 | } | |
ef416fc2 | 742 | } |
743 | else | |
744 | { | |
745 | DEBUG_printf(("Flushing old cache tile (%p)...\n", img->first)); | |
746 | ||
747 | flush_tile(img); | |
748 | ic = img->first; | |
749 | } | |
750 | ||
751 | ic->tile = tile; | |
752 | tile->ic = ic; | |
753 | ||
754 | if (tile->pos >= 0) | |
755 | { | |
3dfe78b3 MS |
756 | DEBUG_printf(("Loading cache tile from file position " CUPS_LLFMT "...\n", |
757 | CUPS_LLCAST tile->pos)); | |
ef416fc2 | 758 | |
3dfe78b3 MS |
759 | lseek(img->cachefile, tile->pos, SEEK_SET); |
760 | read(img->cachefile, ic->pixels, bpp * CUPS_TILE_SIZE * CUPS_TILE_SIZE); | |
ef416fc2 | 761 | } |
762 | else | |
763 | { | |
764 | DEBUG_puts("Clearing cache tile..."); | |
765 | ||
766 | memset(ic->pixels, 0, bpp * CUPS_TILE_SIZE * CUPS_TILE_SIZE); | |
767 | } | |
768 | } | |
769 | ||
770 | if (ic == img->first) | |
771 | { | |
772 | if (ic->next != NULL) | |
773 | ic->next->prev = NULL; | |
774 | ||
775 | img->first = ic->next; | |
776 | ic->next = NULL; | |
777 | ic->prev = NULL; | |
778 | } | |
779 | else if (img->first == NULL) | |
780 | img->first = ic; | |
781 | ||
782 | if (ic != img->last) | |
783 | { | |
784 | /* | |
785 | * Remove the cache entry from the list... | |
786 | */ | |
787 | ||
788 | if (ic->prev != NULL) | |
789 | ic->prev->next = ic->next; | |
790 | if (ic->next != NULL) | |
791 | ic->next->prev = ic->prev; | |
792 | ||
793 | /* | |
794 | * And add it to the end... | |
795 | */ | |
796 | ||
797 | if (img->last != NULL) | |
798 | img->last->next = ic; | |
799 | ||
800 | ic->prev = img->last; | |
801 | img->last = ic; | |
802 | } | |
803 | ||
804 | ic->next = NULL; | |
805 | ||
806 | return (ic->pixels + bpp * (y * CUPS_TILE_SIZE + x)); | |
807 | } | |
808 | ||
809 | ||
810 | /* | |
bc44d920 | 811 | * End of "$Id: image.c 6649 2007-07-11 21:46:42Z mike $". |
ef416fc2 | 812 | */ |