Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/contrib/pds/tif_imageiter.c

raw
1 /*
2 * Copyright (c) 1991-1996 Sam Leffler
3 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25 /*
26 * TIFF Library
27 *
28 * Written by Conrad J. Poelman, PL/WSAT, Kirtland AFB, NM on 26 Mar 96.
29 *
30 * This file contains code to allow a calling program to "iterate" over each
31 * pixels in an image as it is read from the file. The iterator takes care of
32 * reading strips versus (possibly clipped) tiles, decoding the information
33 * according to the decoding method, and so on, so that calling program can
34 * ignore those details. The calling program does, however, need to be
35 * conscious of the type of the pixel data that it is receiving.
36 *
37 * For reasons of efficiency, the callback function actually gets called for
38 * "blocks" of pixels rather than for individual pixels. The format of the
39 * callback arguments is given below.
40 *
41 * This code was taken from TIFFReadRGBAImage() in tif_getimage.c of the
42 * original TIFF distribution, and simplified and generalized to provide this
43 * general iteration capability. Those routines could certainly be
44 * re-implemented in terms of a TIFFImageIter if desired.
45 *
46 */
47 #include "tif_imageiter.h"
48 #include "tiffiop.h"
49 #include <assert.h>
50 #include <stdio.h>
51
52 static int gtTileContig(TIFFImageIter *, void *udata, uint32_t, uint32_t);
53 static int gtTileSeparate(TIFFImageIter *, void *udata, uint32_t, uint32_t);
54 static int gtStripContig(TIFFImageIter *, void *udata, uint32_t, uint32_t);
55 static int gtStripSeparate(TIFFImageIter *, void *udata, uint32_t, uint32_t);
56
57 static const char photoTag[] = "PhotometricInterpretation";
58
59 static int isCCITTCompression(TIFF *tif)
60 {
61 uint16_t compress;
62 TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
63 return (compress == COMPRESSION_CCITTFAX3 ||
64 compress == COMPRESSION_CCITTFAX4 ||
65 compress == COMPRESSION_CCITTRLE ||
66 compress == COMPRESSION_CCITTRLEW);
67 }
68
69 int TIFFImageIterBegin(TIFFImageIter *img, TIFF *tif, int stop, char emsg[1024])
70 {
71 uint16_t *sampleinfo;
72 uint16_t extrasamples;
73 uint16_t planarconfig;
74 int colorchannels;
75
76 img->tif = tif;
77 img->stoponerr = stop;
78 TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
79 img->alpha = 0;
80 TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
81 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &extrasamples,
82 &sampleinfo);
83 if (extrasamples == 1)
84 switch (sampleinfo[0])
85 {
86 case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */
87 case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */
88 img->alpha = sampleinfo[0];
89 break;
90 }
91 colorchannels = img->samplesperpixel - extrasamples;
92 TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
93 if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
94 {
95 switch (colorchannels)
96 {
97 case 1:
98 if (isCCITTCompression(tif))
99 img->photometric = PHOTOMETRIC_MINISWHITE;
100 else
101 img->photometric = PHOTOMETRIC_MINISBLACK;
102 break;
103 case 3:
104 img->photometric = PHOTOMETRIC_RGB;
105 break;
106 default:
107 sprintf(emsg, "Missing needed %s tag", photoTag);
108 return (0);
109 }
110 }
111 switch (img->photometric)
112 {
113 case PHOTOMETRIC_PALETTE:
114 if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &img->redcmap,
115 &img->greencmap, &img->bluecmap))
116 {
117 TIFFErrorExtR(tif, TIFFFileName(tif),
118 "Missing required \"Colormap\" tag");
119 return (0);
120 }
121 /* fall through... */
122 case PHOTOMETRIC_MINISWHITE:
123 case PHOTOMETRIC_MINISBLACK:
124 /* This should work now so skip the check - BSR
125 if (planarconfig == PLANARCONFIG_CONTIG &&
126 img->samplesperpixel != 1) { sprintf(emsg, "Sorry, can not handle
127 contiguous data with %s=%d, and %s=%d", photoTag,
128 img->photometric, "Samples/pixel", img->samplesperpixel); return
129 (0);
130 }
131 */
132 break;
133 case PHOTOMETRIC_YCBCR:
134 if (planarconfig != PLANARCONFIG_CONTIG)
135 {
136 sprintf(emsg, "Sorry, can not handle YCbCr images with %s=%d",
137 "Planarconfiguration", planarconfig);
138 return (0);
139 }
140 /* It would probably be nice to have a reality check here. */
141 {
142 uint16_t compress;
143 TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
144 if (compress == COMPRESSION_JPEG &&
145 planarconfig == PLANARCONFIG_CONTIG)
146 {
147 /* can rely on libjpeg to convert to RGB */
148 /* XXX should restore current state on exit */
149 TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
150 img->photometric = PHOTOMETRIC_RGB;
151 }
152 }
153 break;
154 case PHOTOMETRIC_RGB:
155 if (colorchannels < 3)
156 {
157 sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
158 "Color channels", colorchannels);
159 return (0);
160 }
161 break;
162 case PHOTOMETRIC_SEPARATED:
163 {
164 uint16_t inkset;
165 TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
166 if (inkset != INKSET_CMYK)
167 {
168 sprintf(emsg,
169 "Sorry, can not handle separated image with %s=%d",
170 "InkSet", inkset);
171 return (0);
172 }
173 if (img->samplesperpixel != 4)
174 {
175 sprintf(emsg,
176 "Sorry, can not handle separated image with %s=%d",
177 "Samples/pixel", img->samplesperpixel);
178 return (0);
179 }
180 break;
181 }
182 default:
183 sprintf(emsg, "Sorry, can not handle image with %s=%d", photoTag,
184 img->photometric);
185 return (0);
186 }
187 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
188 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
189
190 TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
191 switch (img->orientation)
192 {
193 case ORIENTATION_BOTRIGHT:
194 case ORIENTATION_RIGHTBOT: /* XXX */
195 case ORIENTATION_LEFTBOT: /* XXX */
196 TIFFWarning(TIFFFileName(tif), "using bottom-left orientation");
197 img->orientation = ORIENTATION_BOTLEFT;
198 /* fall through... */
199 case ORIENTATION_BOTLEFT:
200 break;
201 case ORIENTATION_TOPRIGHT:
202 case ORIENTATION_RIGHTTOP: /* XXX */
203 case ORIENTATION_LEFTTOP: /* XXX */
204 default:
205 TIFFWarning(TIFFFileName(tif), "using top-left orientation");
206 img->orientation = ORIENTATION_TOPLEFT;
207 /* fall through... */
208 case ORIENTATION_TOPLEFT:
209 break;
210 }
211
212 img->isContig =
213 !(planarconfig == PLANARCONFIG_SEPARATE && colorchannels > 1);
214 if (img->isContig)
215 {
216 img->get = TIFFIsTiled(tif) ? gtTileContig : gtStripContig;
217 }
218 else
219 {
220 img->get = TIFFIsTiled(tif) ? gtTileSeparate : gtStripSeparate;
221 }
222 return (1);
223 }
224
225 int TIFFImageIterGet(TIFFImageIter *img, void *udata, uint32_t w, uint32_t h)
226 {
227 if (img->get == NULL)
228 {
229 TIFFErrorExtR(img->tif, TIFFFileName(img->tif),
230 "No \"get\" routine setup");
231 return (0);
232 }
233 if (img->callback.any == NULL)
234 {
235 TIFFErrorExtR(
236 img->tif, TIFFFileName(img->tif),
237 "No \"put\" routine setupl; probably can not handle image format");
238 return (0);
239 }
240 return (*img->get)(img, udata, w, h);
241 }
242
243 TIFFImageIterEnd(TIFFImageIter *img)
244 { /* Nothing to free... ? */
245 }
246
247 /*
248 * Read the specified image into an ABGR-format raster.
249 */
250 int TIFFReadImageIter(TIFF *tif, uint32_t rwidth, uint32_t rheight,
251 uint8_t *raster, int stop)
252 {
253 char emsg[1024];
254 TIFFImageIter img;
255 int ok;
256
257 if (TIFFImageIterBegin(&img, tif, stop, emsg))
258 {
259 /* XXX verify rwidth and rheight against width and height */
260 ok = TIFFImageIterGet(&img, raster, rwidth, img.height);
261 TIFFImageIterEnd(&img);
262 }
263 else
264 {
265 TIFFErrorExtR(tif, TIFFFileName(tif), emsg);
266 ok = 0;
267 }
268 return (ok);
269 }
270
271 /*
272 * Get an tile-organized image that has
273 * PlanarConfiguration contiguous if SamplesPerPixel > 1
274 * or
275 * SamplesPerPixel == 1
276 */
277 static int gtTileContig(TIFFImageIter *img, void *udata, uint32_t w, uint32_t h)
278 {
279 TIFF *tif = img->tif;
280 ImageIterTileContigRoutine callback = img->callback.contig;
281 uint16_t orientation;
282 uint32_t col, row;
283 uint32_t tw, th;
284 u_char *buf;
285 int32_t fromskew;
286 uint32_t nrow;
287
288 buf = (u_char *)_TIFFmalloc(TIFFTileSize(tif));
289 if (buf == 0)
290 {
291 TIFFErrorExtR(tif, TIFFFileName(tif), "No space for tile buffer");
292 return (0);
293 }
294 TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
295 TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
296 orientation = img->orientation;
297 for (row = 0; row < h; row += th)
298 {
299 nrow = (row + th > h ? h - row : th);
300 for (col = 0; col < w; col += tw)
301 {
302 if (TIFFReadTile(tif, buf, col, row, 0, 0) < 0 && img->stoponerr)
303 break;
304 if (col + tw > w)
305 {
306 /*
307 * Tile is clipped horizontally. Calculate
308 * visible portion and skewing factors.
309 */
310 uint32_t npix = w - col;
311 fromskew = tw - npix;
312 (*callback)(img, udata, col, row, npix, nrow, fromskew, buf);
313 }
314 else
315 {
316 (*callback)(img, udata, col, row, tw, nrow, 0, buf);
317 }
318 }
319 }
320 _TIFFfree(buf);
321 return (1);
322 }
323
324 /*
325 * Get an tile-organized image that has
326 * SamplesPerPixel > 1
327 * PlanarConfiguration separated
328 * We assume that all such images are RGB.
329 */
330 static int gtTileSeparate(TIFFImageIter *img, void *udata, uint32_t w,
331 uint32_t h)
332 {
333 TIFF *tif = img->tif;
334 ImageIterTileSeparateRoutine callback = img->callback.separate;
335 uint16_t orientation;
336 uint32_t col, row;
337 uint32_t tw, th;
338 u_char *buf;
339 u_char *r;
340 u_char *g;
341 u_char *b;
342 u_char *a;
343 tsize_t tilesize;
344 int32_t fromskew;
345 int alpha = img->alpha;
346 uint32_t nrow;
347
348 tilesize = TIFFTileSize(tif);
349 buf = (u_char *)_TIFFmalloc(4 * tilesize);
350 if (buf == 0)
351 {
352 TIFFErrorExtR(tif, TIFFFileName(tif), "No space for tile buffer");
353 return (0);
354 }
355 r = buf;
356 g = r + tilesize;
357 b = g + tilesize;
358 a = b + tilesize;
359 if (!alpha)
360 memset(a, 0xff, tilesize);
361 TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
362 TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
363 orientation = img->orientation;
364 for (row = 0; row < h; row += th)
365 {
366 nrow = (row + th > h ? h - row : th);
367 for (col = 0; col < w; col += tw)
368 {
369 if (TIFFReadTile(tif, r, col, row, 0, 0) < 0 && img->stoponerr)
370 break;
371 if (TIFFReadTile(tif, g, col, row, 0, 1) < 0 && img->stoponerr)
372 break;
373 if (TIFFReadTile(tif, b, col, row, 0, 2) < 0 && img->stoponerr)
374 break;
375 if (alpha && TIFFReadTile(tif, a, col, row, 0, 3) < 0 &&
376 img->stoponerr)
377 break;
378 if (col + tw > w)
379 {
380 /*
381 * Tile is clipped horizontally. Calculate
382 * visible portion and skewing factors.
383 */
384 uint32_t npix = w - col;
385 fromskew = tw - npix;
386 (*callback)(img, udata, col, row, npix, nrow, fromskew, r, g, b,
387 a);
388 }
389 else
390 {
391 (*callback)(img, udata, col, row, tw, nrow, 0, r, g, b, a);
392 }
393 }
394 }
395 _TIFFfree(buf);
396 return (1);
397 }
398
399 /*
400 * Get a strip-organized image that has
401 * PlanarConfiguration contiguous if SamplesPerPixel > 1
402 * or
403 * SamplesPerPixel == 1
404 */
405 static int gtStripContig(TIFFImageIter *img, void *udata, uint32_t w,
406 uint32_t h)
407 {
408 TIFF *tif = img->tif;
409 ImageIterTileContigRoutine callback = img->callback.contig;
410 uint16_t orientation;
411 uint32_t row, nrow;
412 u_char *buf;
413 uint32_t rowsperstrip;
414 uint32_t imagewidth = img->width;
415 tsize_t scanline;
416 int32_t fromskew;
417
418 buf = (u_char *)_TIFFmalloc(TIFFStripSize(tif));
419 if (buf == 0)
420 {
421 TIFFErrorExtR(tif, TIFFFileName(tif), "No space for strip buffer");
422 return (0);
423 }
424 orientation = img->orientation;
425 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
426 scanline = TIFFScanlineSize(tif);
427 fromskew = (w < imagewidth ? imagewidth - w : 0);
428 for (row = 0; row < h; row += rowsperstrip)
429 {
430 nrow = (row + rowsperstrip > h ? h - row : rowsperstrip);
431 if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 0), buf,
432 nrow * scanline) < 0 &&
433 img->stoponerr)
434 break;
435 (*callback)(img, udata, 0, row, w, nrow, fromskew, buf);
436 }
437 _TIFFfree(buf);
438 return (1);
439 }
440
441 /*
442 * Get a strip-organized image with
443 * SamplesPerPixel > 1
444 * PlanarConfiguration separated
445 * We assume that all such images are RGB.
446 */
447 static int gtStripSeparate(TIFFImageIter *img, void *udata, uint32_t w,
448 uint32_t h)
449 {
450 TIFF *tif = img->tif;
451 ImageIterTileSeparateRoutine callback = img->callback.separate;
452 uint16_t orientation;
453 u_char *buf;
454 u_char *r, *g, *b, *a;
455 uint32_t row, nrow;
456 tsize_t scanline;
457 uint32_t rowsperstrip;
458 uint32_t imagewidth = img->width;
459 tsize_t stripsize;
460 int32_t fromskew;
461 int alpha = img->alpha;
462
463 stripsize = TIFFStripSize(tif);
464 r = buf = (u_char *)_TIFFmalloc(4 * stripsize);
465 if (buf == 0)
466 {
467 TIFFErrorExtR(tif, TIFFFileName(tif), "No space for tile buffer");
468 return (0);
469 }
470 g = r + stripsize;
471 b = g + stripsize;
472 a = b + stripsize;
473 if (!alpha)
474 memset(a, 0xff, stripsize);
475 orientation = img->orientation;
476 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
477 scanline = TIFFScanlineSize(tif);
478 fromskew = (w < imagewidth ? imagewidth - w : 0);
479 for (row = 0; row < h; row += rowsperstrip)
480 {
481 nrow = (row + rowsperstrip > h ? h - row : rowsperstrip);
482 if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 0), r,
483 nrow * scanline) < 0 &&
484 img->stoponerr)
485 break;
486 if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 1), g,
487 nrow * scanline) < 0 &&
488 img->stoponerr)
489 break;
490 if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 2), b,
491 nrow * scanline) < 0 &&
492 img->stoponerr)
493 break;
494 if (alpha && (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 3),
495 a, nrow * scanline) < 0 &&
496 img->stoponerr))
497 break;
498 (*callback)(img, udata, 0, row, w, nrow, fromskew, r, g, b, a);
499 }
500 _TIFFfree(buf);
501 return (1);
502 }
503
504 DECLAREContigCallbackFunc(TestContigCallback)
505 {
506 printf("Contig Callback called with x = %d, y = %d, w = %d, h = %d, "
507 "fromskew = %d\n",
508 x, y, w, h, fromskew);
509 }
510
511 DECLARESepCallbackFunc(TestSepCallback)
512 {
513 printf("Sep Callback called with x = %d, y = %d, w = %d, h = %d, fromskew "
514 "= %d\n",
515 x, y, w, h, fromskew);
516 }
517
518 #ifdef MAIN
519 main(int argc, char **argv)
520 {
521 char emsg[1024];
522 TIFFImageIter img;
523 int ok;
524 int stop = 1;
525
526 TIFF *tif;
527 unsigned long nx, ny;
528 unsigned short BitsPerSample, SamplesPerPixel;
529 int isColorMapped, isPliFile;
530 unsigned char *ColorMap;
531 unsigned char *data;
532
533 if (argc < 2)
534 {
535 fprintf(stderr, "usage: %s tiff_file\n", argv[0]);
536 exit(1);
537 }
538 tif = (TIFF *)PLIGetImage(argv[1], (void *)&data, &ColorMap, &nx, &ny,
539 &BitsPerSample, &SamplesPerPixel, &isColorMapped,
540 &isPliFile);
541 if (tif != NULL)
542 {
543
544 if (TIFFImageIterBegin(&img, tif, stop, emsg))
545 {
546 /* Here need to set data and callback function! */
547 if (img.isContig)
548 {
549 img.callback = TestContigCallback;
550 }
551 else
552 {
553 img.callback = TestSepCallback;
554 }
555 ok = TIFFImageIterGet(&img, NULL, img.width, img.height);
556 TIFFImageIterEnd(&img);
557 }
558 else
559 {
560 TIFFErrorExtR(tif, TIFFFileName(tif), emsg);
561 }
562 }
563 }
564 #endif