1 /* $Id: tif_getimage.c,v 1.90 2015-06-17 01:34:08 bfriesen Exp $ */
4 * Copyright (c) 1991-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
30 * Read and return a packed RGBA image.
35 static int gtTileContig(TIFFRGBAImage*, uint32*, uint32, uint32);
36 static int gtTileSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
37 static int gtStripContig(TIFFRGBAImage*, uint32*, uint32, uint32);
38 static int gtStripSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
39 static int PickContigCase(TIFFRGBAImage*);
40 static int PickSeparateCase(TIFFRGBAImage*);
42 static int BuildMapUaToAa(TIFFRGBAImage* img);
43 static int BuildMapBitdepth16To8(TIFFRGBAImage* img);
45 static const char photoTag[] = "PhotometricInterpretation";
48 * Helper constants used in Orientation tag handling
50 #define FLIP_VERTICALLY 0x01
51 #define FLIP_HORIZONTALLY 0x02
54 * Color conversion constants. We will define display types here.
57 static const TIFFDisplay display_sRGB = {
58 { /* XYZ -> luminance matrix */
59 { 3.2410F, -1.5374F, -0.4986F },
60 { -0.9692F, 1.8760F, 0.0416F },
61 { 0.0556F, -0.2040F, 1.0570F }
63 100.0F, 100.0F, 100.0F, /* Light o/p for reference white */
64 255, 255, 255, /* Pixel values for ref. white */
65 1.0F, 1.0F, 1.0F, /* Residual light o/p for black pixel */
66 2.4F, 2.4F, 2.4F, /* Gamma values for the three guns */
70 * Check the image to see if TIFFReadRGBAImage can deal with it.
71 * 1/0 is returned according to whether or not the image can
72 * be handled. If 0 is returned, emsg contains the reason
73 * why it is being rejected.
76 TIFFRGBAImageOK(TIFF* tif, char emsg[1024])
78 TIFFDirectory* td = &tif->tif_dir;
82 if (!tif->tif_decodestatus) {
83 sprintf(emsg, "Sorry, requested compression method is not configured");
86 switch (td->td_bitspersample) {
94 sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
95 td->td_bitspersample);
98 colorchannels = td->td_samplesperpixel - td->td_extrasamples;
99 if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) {
100 switch (colorchannels) {
102 photometric = PHOTOMETRIC_MINISBLACK;
105 photometric = PHOTOMETRIC_RGB;
108 sprintf(emsg, "Missing needed %s tag", photoTag);
112 switch (photometric) {
113 case PHOTOMETRIC_MINISWHITE:
114 case PHOTOMETRIC_MINISBLACK:
115 case PHOTOMETRIC_PALETTE:
116 if (td->td_planarconfig == PLANARCONFIG_CONTIG
117 && td->td_samplesperpixel != 1
118 && td->td_bitspersample < 8 ) {
120 "Sorry, can not handle contiguous data with %s=%d, "
121 "and %s=%d and Bits/Sample=%d",
122 photoTag, photometric,
123 "Samples/pixel", td->td_samplesperpixel,
124 td->td_bitspersample);
128 * We should likely validate that any extra samples are either
129 * to be ignored, or are alpha, and if alpha we should try to use
130 * them. But for now we won't bother with this.
133 case PHOTOMETRIC_YCBCR:
135 * TODO: if at all meaningful and useful, make more complete
136 * support check here, or better still, refactor to let supporting
137 * code decide whether there is support and what meaningfull
141 case PHOTOMETRIC_RGB:
142 if (colorchannels < 3) {
143 sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
144 "Color channels", colorchannels);
148 case PHOTOMETRIC_SEPARATED:
151 TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
152 if (inkset != INKSET_CMYK) {
154 "Sorry, can not handle separated image with %s=%d",
158 if (td->td_samplesperpixel < 4) {
160 "Sorry, can not handle separated image with %s=%d",
161 "Samples/pixel", td->td_samplesperpixel);
166 case PHOTOMETRIC_LOGL:
167 if (td->td_compression != COMPRESSION_SGILOG) {
168 sprintf(emsg, "Sorry, LogL data must have %s=%d",
169 "Compression", COMPRESSION_SGILOG);
173 case PHOTOMETRIC_LOGLUV:
174 if (td->td_compression != COMPRESSION_SGILOG &&
175 td->td_compression != COMPRESSION_SGILOG24) {
176 sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
177 "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
180 if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
181 sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
182 "Planarconfiguration", td->td_planarconfig);
185 if( td->td_samplesperpixel != 3 )
188 "Sorry, can not handle image with %s=%d",
189 "Samples/pixel", td->td_samplesperpixel);
193 case PHOTOMETRIC_CIELAB:
194 if( td->td_samplesperpixel != 3 || td->td_bitspersample != 8 )
197 "Sorry, can not handle image with %s=%d and %s=%d",
198 "Samples/pixel", td->td_samplesperpixel,
199 "Bits/sample", td->td_bitspersample);
204 sprintf(emsg, "Sorry, can not handle image with %s=%d",
205 photoTag, photometric);
212 TIFFRGBAImageEnd(TIFFRGBAImage* img)
215 _TIFFfree(img->Map), img->Map = NULL;
217 _TIFFfree(img->BWmap), img->BWmap = NULL;
219 _TIFFfree(img->PALmap), img->PALmap = NULL;
221 _TIFFfree(img->ycbcr), img->ycbcr = NULL;
223 _TIFFfree(img->cielab), img->cielab = NULL;
225 _TIFFfree(img->UaToAa), img->UaToAa = NULL;
226 if (img->Bitdepth16To8)
227 _TIFFfree(img->Bitdepth16To8), img->Bitdepth16To8 = NULL;
230 _TIFFfree( img->redcmap );
231 _TIFFfree( img->greencmap );
232 _TIFFfree( img->bluecmap );
233 img->redcmap = img->greencmap = img->bluecmap = NULL;
238 isCCITTCompression(TIFF* tif)
241 TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
242 return (compress == COMPRESSION_CCITTFAX3 ||
243 compress == COMPRESSION_CCITTFAX4 ||
244 compress == COMPRESSION_CCITTRLE ||
245 compress == COMPRESSION_CCITTRLEW);
249 TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
256 uint16 *red_orig, *green_orig, *blue_orig;
259 /* Initialize to normal values */
263 img->greencmap = NULL;
264 img->bluecmap = NULL;
265 img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */
268 img->stoponerr = stop;
269 TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
270 switch (img->bitspersample) {
278 sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
283 TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
284 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
285 &extrasamples, &sampleinfo);
286 if (extrasamples >= 1)
288 switch (sampleinfo[0]) {
289 case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without */
290 if (img->samplesperpixel > 3) /* correct info about alpha channel */
291 img->alpha = EXTRASAMPLE_ASSOCALPHA;
293 case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */
294 case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */
295 img->alpha = sampleinfo[0];
300 #ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
301 if( !TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
302 img->photometric = PHOTOMETRIC_MINISWHITE;
304 if( extrasamples == 0
305 && img->samplesperpixel == 4
306 && img->photometric == PHOTOMETRIC_RGB )
308 img->alpha = EXTRASAMPLE_ASSOCALPHA;
313 colorchannels = img->samplesperpixel - extrasamples;
314 TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
315 TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
316 if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
317 switch (colorchannels) {
319 if (isCCITTCompression(tif))
320 img->photometric = PHOTOMETRIC_MINISWHITE;
322 img->photometric = PHOTOMETRIC_MINISBLACK;
325 img->photometric = PHOTOMETRIC_RGB;
328 sprintf(emsg, "Missing needed %s tag", photoTag);
332 switch (img->photometric) {
333 case PHOTOMETRIC_PALETTE:
334 if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
335 &red_orig, &green_orig, &blue_orig)) {
336 sprintf(emsg, "Missing required \"Colormap\" tag");
340 /* copy the colormaps so we can modify them */
341 n_color = (1L << img->bitspersample);
342 img->redcmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
343 img->greencmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
344 img->bluecmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
345 if( !img->redcmap || !img->greencmap || !img->bluecmap ) {
346 sprintf(emsg, "Out of memory for colormap copy");
350 _TIFFmemcpy( img->redcmap, red_orig, n_color * 2 );
351 _TIFFmemcpy( img->greencmap, green_orig, n_color * 2 );
352 _TIFFmemcpy( img->bluecmap, blue_orig, n_color * 2 );
355 case PHOTOMETRIC_MINISWHITE:
356 case PHOTOMETRIC_MINISBLACK:
357 if (planarconfig == PLANARCONFIG_CONTIG
358 && img->samplesperpixel != 1
359 && img->bitspersample < 8 ) {
361 "Sorry, can not handle contiguous data with %s=%d, "
362 "and %s=%d and Bits/Sample=%d",
363 photoTag, img->photometric,
364 "Samples/pixel", img->samplesperpixel,
369 case PHOTOMETRIC_YCBCR:
370 /* It would probably be nice to have a reality check here. */
371 if (planarconfig == PLANARCONFIG_CONTIG)
372 /* can rely on libjpeg to convert to RGB */
373 /* XXX should restore current state on exit */
375 case COMPRESSION_JPEG:
377 * TODO: when complete tests verify complete desubsampling
378 * and YCbCr handling, remove use of TIFFTAG_JPEGCOLORMODE in
379 * favor of tif_getimage.c native handling
381 TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
382 img->photometric = PHOTOMETRIC_RGB;
389 * TODO: if at all meaningful and useful, make more complete
390 * support check here, or better still, refactor to let supporting
391 * code decide whether there is support and what meaningfull
395 case PHOTOMETRIC_RGB:
396 if (colorchannels < 3) {
397 sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
398 "Color channels", colorchannels);
402 case PHOTOMETRIC_SEPARATED:
405 TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
406 if (inkset != INKSET_CMYK) {
407 sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
411 if (img->samplesperpixel < 4) {
412 sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
413 "Samples/pixel", img->samplesperpixel);
418 case PHOTOMETRIC_LOGL:
419 if (compress != COMPRESSION_SGILOG) {
420 sprintf(emsg, "Sorry, LogL data must have %s=%d",
421 "Compression", COMPRESSION_SGILOG);
424 TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
425 img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */
426 img->bitspersample = 8;
428 case PHOTOMETRIC_LOGLUV:
429 if (compress != COMPRESSION_SGILOG && compress != COMPRESSION_SGILOG24) {
430 sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
431 "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
434 if (planarconfig != PLANARCONFIG_CONTIG) {
435 sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
436 "Planarconfiguration", planarconfig);
439 TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
440 img->photometric = PHOTOMETRIC_RGB; /* little white lie */
441 img->bitspersample = 8;
443 case PHOTOMETRIC_CIELAB:
446 sprintf(emsg, "Sorry, can not handle image with %s=%d",
447 photoTag, img->photometric);
456 img->Bitdepth16To8 = NULL;
457 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
458 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
459 TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
461 !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);
463 if (!PickContigCase(img)) {
464 sprintf(emsg, "Sorry, can not handle image");
468 if (!PickSeparateCase(img)) {
469 sprintf(emsg, "Sorry, can not handle image");
476 _TIFFfree( img->redcmap );
477 _TIFFfree( img->greencmap );
478 _TIFFfree( img->bluecmap );
479 img->redcmap = img->greencmap = img->bluecmap = NULL;
484 TIFFRGBAImageGet(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
486 if (img->get == NULL) {
487 TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No \"get\" routine setup");
490 if (img->put.any == NULL) {
491 TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
492 "No \"put\" routine setupl; probably can not handle image format");
495 return (*img->get)(img, raster, w, h);
499 * Read the specified image into an ABGR-format rastertaking in account
500 * specified orientation.
503 TIFFReadRGBAImageOriented(TIFF* tif,
504 uint32 rwidth, uint32 rheight, uint32* raster,
505 int orientation, int stop)
507 char emsg[1024] = "";
511 if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, stop, emsg)) {
512 img.req_orientation = orientation;
513 /* XXX verify rwidth and rheight against width and height */
514 ok = TIFFRGBAImageGet(&img, raster+(rheight-img.height)*rwidth,
516 TIFFRGBAImageEnd(&img);
518 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
525 * Read the specified image into an ABGR-format raster. Use bottom left
526 * origin for raster by default.
529 TIFFReadRGBAImage(TIFF* tif,
530 uint32 rwidth, uint32 rheight, uint32* raster, int stop)
532 return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster,
533 ORIENTATION_BOTLEFT, stop);
537 setorientation(TIFFRGBAImage* img)
539 switch (img->orientation) {
540 case ORIENTATION_TOPLEFT:
541 case ORIENTATION_LEFTTOP:
542 if (img->req_orientation == ORIENTATION_TOPRIGHT ||
543 img->req_orientation == ORIENTATION_RIGHTTOP)
544 return FLIP_HORIZONTALLY;
545 else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
546 img->req_orientation == ORIENTATION_RIGHTBOT)
547 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
548 else if (img->req_orientation == ORIENTATION_BOTLEFT ||
549 img->req_orientation == ORIENTATION_LEFTBOT)
550 return FLIP_VERTICALLY;
553 case ORIENTATION_TOPRIGHT:
554 case ORIENTATION_RIGHTTOP:
555 if (img->req_orientation == ORIENTATION_TOPLEFT ||
556 img->req_orientation == ORIENTATION_LEFTTOP)
557 return FLIP_HORIZONTALLY;
558 else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
559 img->req_orientation == ORIENTATION_RIGHTBOT)
560 return FLIP_VERTICALLY;
561 else if (img->req_orientation == ORIENTATION_BOTLEFT ||
562 img->req_orientation == ORIENTATION_LEFTBOT)
563 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
566 case ORIENTATION_BOTRIGHT:
567 case ORIENTATION_RIGHTBOT:
568 if (img->req_orientation == ORIENTATION_TOPLEFT ||
569 img->req_orientation == ORIENTATION_LEFTTOP)
570 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
571 else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
572 img->req_orientation == ORIENTATION_RIGHTTOP)
573 return FLIP_VERTICALLY;
574 else if (img->req_orientation == ORIENTATION_BOTLEFT ||
575 img->req_orientation == ORIENTATION_LEFTBOT)
576 return FLIP_HORIZONTALLY;
579 case ORIENTATION_BOTLEFT:
580 case ORIENTATION_LEFTBOT:
581 if (img->req_orientation == ORIENTATION_TOPLEFT ||
582 img->req_orientation == ORIENTATION_LEFTTOP)
583 return FLIP_VERTICALLY;
584 else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
585 img->req_orientation == ORIENTATION_RIGHTTOP)
586 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
587 else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
588 img->req_orientation == ORIENTATION_RIGHTBOT)
589 return FLIP_HORIZONTALLY;
592 default: /* NOTREACHED */
598 * Get an tile-organized image that has
599 * PlanarConfiguration contiguous if SamplesPerPixel > 1
601 * SamplesPerPixel == 1
604 gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
606 TIFF* tif = img->tif;
607 tileContigRoutine put = img->put.contig;
608 uint32 col, row, y, rowstoread;
612 int32 fromskew, toskew;
615 uint32 this_tw, tocol;
616 int32 this_toskew, leftmost_toskew;
617 int32 leftmost_fromskew;
620 buf = (unsigned char*) _TIFFmalloc(TIFFTileSize(tif));
622 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
625 _TIFFmemset(buf, 0, TIFFTileSize(tif));
626 TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
627 TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
629 flip = setorientation(img);
630 if (flip & FLIP_VERTICALLY) {
632 toskew = -(int32)(tw + w);
636 toskew = -(int32)(tw - w);
640 * Leftmost tile is clipped on left side if col_offset > 0.
642 leftmost_fromskew = img->col_offset % tw;
643 leftmost_tw = tw - leftmost_fromskew;
644 leftmost_toskew = toskew + leftmost_fromskew;
645 for (row = 0; row < h; row += nrow)
647 rowstoread = th - (row + img->row_offset) % th;
648 nrow = (row + rowstoread > h ? h - row : rowstoread);
649 fromskew = leftmost_fromskew;
650 this_tw = leftmost_tw;
651 this_toskew = leftmost_toskew;
653 col = img->col_offset;
656 if (TIFFReadTile(tif, buf, col,
657 row+img->row_offset, 0, 0)==(tmsize_t)(-1) && img->stoponerr)
662 pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif) + \
663 ((tmsize_t) fromskew * img->samplesperpixel);
664 if (tocol + this_tw > w)
667 * Rightmost tile is clipped on right side.
669 fromskew = tw - (w - tocol);
670 this_tw = tw - fromskew;
671 this_toskew = toskew + fromskew;
673 (*put)(img, raster+y*w+tocol, tocol, y, this_tw, nrow, fromskew, this_toskew, buf + pos);
677 * After the leftmost tile, tiles are no longer clipped on left side.
681 this_toskew = toskew;
684 y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
688 if (flip & FLIP_HORIZONTALLY) {
691 for (line = 0; line < h; line++) {
692 uint32 *left = raster + (line * w);
693 uint32 *right = left + w - 1;
695 while ( left < right ) {
708 * Get an tile-organized image that has
709 * SamplesPerPixel > 1
710 * PlanarConfiguration separated
711 * We assume that all such images are RGB.
714 gtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
716 TIFF* tif = img->tif;
717 tileSeparateRoutine put = img->put.separate;
718 uint32 col, row, y, rowstoread;
728 int32 fromskew, toskew;
729 int alpha = img->alpha;
733 uint32 this_tw, tocol;
734 int32 this_toskew, leftmost_toskew;
735 int32 leftmost_fromskew;
738 tilesize = TIFFTileSize(tif);
739 bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,tilesize);
741 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtTileSeparate");
744 buf = (unsigned char*) _TIFFmalloc(bufsize);
746 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
749 _TIFFmemset(buf, 0, bufsize);
753 pa = (alpha?(p2+tilesize):NULL);
754 TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
755 TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
757 flip = setorientation(img);
758 if (flip & FLIP_VERTICALLY) {
760 toskew = -(int32)(tw + w);
764 toskew = -(int32)(tw - w);
767 switch( img->photometric )
769 case PHOTOMETRIC_MINISWHITE:
770 case PHOTOMETRIC_MINISBLACK:
771 case PHOTOMETRIC_PALETTE:
782 * Leftmost tile is clipped on left side if col_offset > 0.
784 leftmost_fromskew = img->col_offset % tw;
785 leftmost_tw = tw - leftmost_fromskew;
786 leftmost_toskew = toskew + leftmost_fromskew;
787 for (row = 0; row < h; row += nrow)
789 rowstoread = th - (row + img->row_offset) % th;
790 nrow = (row + rowstoread > h ? h - row : rowstoread);
791 fromskew = leftmost_fromskew;
792 this_tw = leftmost_tw;
793 this_toskew = leftmost_toskew;
795 col = img->col_offset;
798 if (TIFFReadTile(tif, p0, col,
799 row+img->row_offset,0,0)==(tmsize_t)(-1) && img->stoponerr)
804 if (colorchannels > 1
805 && TIFFReadTile(tif, p1, col,
806 row+img->row_offset,0,1) == (tmsize_t)(-1)
812 if (colorchannels > 1
813 && TIFFReadTile(tif, p2, col,
814 row+img->row_offset,0,2) == (tmsize_t)(-1)
821 && TIFFReadTile(tif,pa,col,
822 row+img->row_offset,0,colorchannels) == (tmsize_t)(-1)
829 pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif) + \
830 ((tmsize_t) fromskew * img->samplesperpixel);
831 if (tocol + this_tw > w)
834 * Rightmost tile is clipped on right side.
836 fromskew = tw - (w - tocol);
837 this_tw = tw - fromskew;
838 this_toskew = toskew + fromskew;
840 (*put)(img, raster+y*w+tocol, tocol, y, this_tw, nrow, fromskew, this_toskew, \
841 p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
845 * After the leftmost tile, tiles are no longer clipped on left side.
849 this_toskew = toskew;
852 y += (flip & FLIP_VERTICALLY ?-(int32) nrow : (int32) nrow);
855 if (flip & FLIP_HORIZONTALLY) {
858 for (line = 0; line < h; line++) {
859 uint32 *left = raster + (line * w);
860 uint32 *right = left + w - 1;
862 while ( left < right ) {
876 * Get a strip-organized image that has
877 * PlanarConfiguration contiguous if SamplesPerPixel > 1
879 * SamplesPerPixel == 1
882 gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
884 TIFF* tif = img->tif;
885 tileContigRoutine put = img->put.contig;
886 uint32 row, y, nrow, nrowsub, rowstoread;
890 uint16 subsamplinghor,subsamplingver;
891 uint32 imagewidth = img->width;
893 int32 fromskew, toskew;
896 TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor, &subsamplingver);
897 if( subsamplingver == 0 ) {
898 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Invalid vertical YCbCr subsampling");
902 buf = (unsigned char*) _TIFFmalloc(TIFFStripSize(tif));
904 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
907 _TIFFmemset(buf, 0, TIFFStripSize(tif));
909 flip = setorientation(img);
910 if (flip & FLIP_VERTICALLY) {
912 toskew = -(int32)(w + w);
915 toskew = -(int32)(w - w);
918 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
920 scanline = TIFFScanlineSize(tif);
921 fromskew = (w < imagewidth ? imagewidth - w : 0);
922 for (row = 0; row < h; row += nrow)
924 rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
925 nrow = (row + rowstoread > h ? h - row : rowstoread);
927 if ((nrowsub%subsamplingver)!=0)
928 nrowsub+=subsamplingver-nrowsub%subsamplingver;
929 if (TIFFReadEncodedStrip(tif,
930 TIFFComputeStrip(tif,row+img->row_offset, 0),
932 ((row + img->row_offset)%rowsperstrip + nrowsub) * scanline)==(tmsize_t)(-1)
939 pos = ((row + img->row_offset) % rowsperstrip) * scanline + \
940 ((tmsize_t) img->col_offset * img->samplesperpixel);
941 (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf + pos);
942 y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
945 if (flip & FLIP_HORIZONTALLY) {
948 for (line = 0; line < h; line++) {
949 uint32 *left = raster + (line * w);
950 uint32 *right = left + w - 1;
952 while ( left < right ) {
966 * Get a strip-organized image with
967 * SamplesPerPixel > 1
968 * PlanarConfiguration separated
969 * We assume that all such images are RGB.
972 gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
974 TIFF* tif = img->tif;
975 tileSeparateRoutine put = img->put.separate;
977 unsigned char *p0, *p1, *p2, *pa;
978 uint32 row, y, nrow, rowstoread;
981 uint32 rowsperstrip, offset_row;
982 uint32 imagewidth = img->width;
985 int32 fromskew, toskew;
986 int alpha = img->alpha;
987 int ret = 1, flip, colorchannels;
989 stripsize = TIFFStripSize(tif);
990 bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,stripsize);
992 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtStripSeparate");
995 p0 = buf = (unsigned char *)_TIFFmalloc(bufsize);
997 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
1000 _TIFFmemset(buf, 0, bufsize);
1001 p1 = p0 + stripsize;
1002 p2 = p1 + stripsize;
1003 pa = (alpha?(p2+stripsize):NULL);
1005 flip = setorientation(img);
1006 if (flip & FLIP_VERTICALLY) {
1008 toskew = -(int32)(w + w);
1012 toskew = -(int32)(w - w);
1015 switch( img->photometric )
1017 case PHOTOMETRIC_MINISWHITE:
1018 case PHOTOMETRIC_MINISBLACK:
1019 case PHOTOMETRIC_PALETTE:
1029 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
1030 scanline = TIFFScanlineSize(tif);
1031 fromskew = (w < imagewidth ? imagewidth - w : 0);
1032 for (row = 0; row < h; row += nrow)
1034 rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
1035 nrow = (row + rowstoread > h ? h - row : rowstoread);
1036 offset_row = row + img->row_offset;
1037 if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
1038 p0, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
1044 if (colorchannels > 1
1045 && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),
1046 p1, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
1052 if (colorchannels > 1
1053 && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),
1054 p2, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
1062 if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, colorchannels),
1063 pa, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
1071 pos = ((row + img->row_offset) % rowsperstrip) * scanline + \
1072 ((tmsize_t) img->col_offset * img->samplesperpixel);
1073 (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, p0 + pos, p1 + pos,
1074 p2 + pos, (alpha?(pa+pos):NULL));
1075 y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
1078 if (flip & FLIP_HORIZONTALLY) {
1081 for (line = 0; line < h; line++) {
1082 uint32 *left = raster + (line * w);
1083 uint32 *right = left + w - 1;
1085 while ( left < right ) {
1086 uint32 temp = *left;
1099 * The following routines move decoded data returned
1100 * from the TIFF library into rasters filled with packed
1101 * ABGR pixels (i.e. suitable for passing to lrecwrite.)
1103 * The routines have been created according to the most
1104 * important cases and optimized. PickContigCase and
1105 * PickSeparateCase analyze the parameters and select
1106 * the appropriate "get" and "put" routine to use.
1108 #define REPEAT8(op) REPEAT4(op); REPEAT4(op)
1109 #define REPEAT4(op) REPEAT2(op); REPEAT2(op)
1110 #define REPEAT2(op) op; op
1111 #define CASE8(x,op) \
1113 case 7: op; case 6: op; case 5: op; \
1114 case 4: op; case 3: op; case 2: op; \
1117 #define CASE4(x,op) switch (x) { case 3: op; case 2: op; case 1: op; }
1120 #define UNROLL8(w, op1, op2) { \
1122 for (_x = w; _x >= 8; _x -= 8) { \
1131 #define UNROLL4(w, op1, op2) { \
1133 for (_x = w; _x >= 4; _x -= 4) { \
1142 #define UNROLL2(w, op1, op2) { \
1144 for (_x = w; _x >= 2; _x -= 2) { \
1154 #define SKEW(r,g,b,skew) { r += skew; g += skew; b += skew; }
1155 #define SKEW4(r,g,b,a,skew) { r += skew; g += skew; b += skew; a+= skew; }
1157 #define A1 (((uint32)0xffL)<<24)
1158 #define PACK(r,g,b) \
1159 ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|A1)
1160 #define PACK4(r,g,b,a) \
1161 ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|((uint32)(a)<<24))
1162 #define W2B(v) (((v)>>8)&0xff)
1163 /* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */
1164 #define PACKW(r,g,b) \
1165 ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|A1)
1166 #define PACKW4(r,g,b,a) \
1167 ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|((uint32)W2B(a)<<24))
1169 #define DECLAREContigPutFunc(name) \
1171 TIFFRGBAImage* img, \
1173 uint32 x, uint32 y, \
1174 uint32 w, uint32 h, \
1175 int32 fromskew, int32 toskew, \
1180 * 8-bit palette => colormap/RGB
1182 DECLAREContigPutFunc(put8bitcmaptile)
1184 uint32** PALmap = img->PALmap;
1185 int samplesperpixel = img->samplesperpixel;
1189 for (x = w; x-- > 0;)
1191 *cp++ = PALmap[*pp][0];
1192 pp += samplesperpixel;
1200 * 4-bit palette => colormap/RGB
1202 DECLAREContigPutFunc(put4bitcmaptile)
1204 uint32** PALmap = img->PALmap;
1210 UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
1217 * 2-bit palette => colormap/RGB
1219 DECLAREContigPutFunc(put2bitcmaptile)
1221 uint32** PALmap = img->PALmap;
1227 UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
1234 * 1-bit palette => colormap/RGB
1236 DECLAREContigPutFunc(put1bitcmaptile)
1238 uint32** PALmap = img->PALmap;
1244 UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
1251 * 8-bit greyscale => colormap/RGB
1253 DECLAREContigPutFunc(putgreytile)
1255 int samplesperpixel = img->samplesperpixel;
1256 uint32** BWmap = img->BWmap;
1260 for (x = w; x-- > 0;)
1262 *cp++ = BWmap[*pp][0];
1263 pp += samplesperpixel;
1271 * 8-bit greyscale with associated alpha => colormap/RGBA
1273 DECLAREContigPutFunc(putagreytile)
1275 int samplesperpixel = img->samplesperpixel;
1276 uint32** BWmap = img->BWmap;
1280 for (x = w; x-- > 0;)
1282 *cp++ = BWmap[*pp][0] & (*(pp+1) << 24 | ~A1);
1283 pp += samplesperpixel;
1291 * 16-bit greyscale => colormap/RGB
1293 DECLAREContigPutFunc(put16bitbwtile)
1295 int samplesperpixel = img->samplesperpixel;
1296 uint32** BWmap = img->BWmap;
1300 uint16 *wp = (uint16 *) pp;
1302 for (x = w; x-- > 0;)
1304 /* use high order byte of 16bit value */
1306 *cp++ = BWmap[*wp >> 8][0];
1307 pp += 2 * samplesperpixel;
1308 wp += samplesperpixel;
1316 * 1-bit bilevel => colormap/RGB
1318 DECLAREContigPutFunc(put1bitbwtile)
1320 uint32** BWmap = img->BWmap;
1326 UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
1333 * 2-bit greyscale => colormap/RGB
1335 DECLAREContigPutFunc(put2bitbwtile)
1337 uint32** BWmap = img->BWmap;
1343 UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
1350 * 4-bit greyscale => colormap/RGB
1352 DECLAREContigPutFunc(put4bitbwtile)
1354 uint32** BWmap = img->BWmap;
1360 UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
1367 * 8-bit packed samples, no Map => RGB
1369 DECLAREContigPutFunc(putRGBcontig8bittile)
1371 int samplesperpixel = img->samplesperpixel;
1374 fromskew *= samplesperpixel;
1377 *cp++ = PACK(pp[0], pp[1], pp[2]);
1378 pp += samplesperpixel);
1385 * 8-bit packed samples => RGBA w/ associated alpha
1386 * (known to have Map == NULL)
1388 DECLAREContigPutFunc(putRGBAAcontig8bittile)
1390 int samplesperpixel = img->samplesperpixel;
1393 fromskew *= samplesperpixel;
1396 *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
1397 pp += samplesperpixel);
1404 * 8-bit packed samples => RGBA w/ unassociated alpha
1405 * (known to have Map == NULL)
1407 DECLAREContigPutFunc(putRGBUAcontig8bittile)
1409 int samplesperpixel = img->samplesperpixel;
1411 fromskew *= samplesperpixel;
1415 for (x = w; x-- > 0;) {
1417 m = img->UaToAa+(a<<8);
1421 *cp++ = PACK4(r,g,b,a);
1422 pp += samplesperpixel;
1430 * 16-bit packed samples => RGB
1432 DECLAREContigPutFunc(putRGBcontig16bittile)
1434 int samplesperpixel = img->samplesperpixel;
1435 uint16 *wp = (uint16 *)pp;
1437 fromskew *= samplesperpixel;
1439 for (x = w; x-- > 0;) {
1440 *cp++ = PACK(img->Bitdepth16To8[wp[0]],
1441 img->Bitdepth16To8[wp[1]],
1442 img->Bitdepth16To8[wp[2]]);
1443 wp += samplesperpixel;
1451 * 16-bit packed samples => RGBA w/ associated alpha
1452 * (known to have Map == NULL)
1454 DECLAREContigPutFunc(putRGBAAcontig16bittile)
1456 int samplesperpixel = img->samplesperpixel;
1457 uint16 *wp = (uint16 *)pp;
1459 fromskew *= samplesperpixel;
1461 for (x = w; x-- > 0;) {
1462 *cp++ = PACK4(img->Bitdepth16To8[wp[0]],
1463 img->Bitdepth16To8[wp[1]],
1464 img->Bitdepth16To8[wp[2]],
1465 img->Bitdepth16To8[wp[3]]);
1466 wp += samplesperpixel;
1474 * 16-bit packed samples => RGBA w/ unassociated alpha
1475 * (known to have Map == NULL)
1477 DECLAREContigPutFunc(putRGBUAcontig16bittile)
1479 int samplesperpixel = img->samplesperpixel;
1480 uint16 *wp = (uint16 *)pp;
1482 fromskew *= samplesperpixel;
1486 for (x = w; x-- > 0;) {
1487 a = img->Bitdepth16To8[wp[3]];
1488 m = img->UaToAa+(a<<8);
1489 r = m[img->Bitdepth16To8[wp[0]]];
1490 g = m[img->Bitdepth16To8[wp[1]]];
1491 b = m[img->Bitdepth16To8[wp[2]]];
1492 *cp++ = PACK4(r,g,b,a);
1493 wp += samplesperpixel;
1501 * 8-bit packed CMYK samples w/o Map => RGB
1503 * NB: The conversion of CMYK->RGB is *very* crude.
1505 DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
1507 int samplesperpixel = img->samplesperpixel;
1511 fromskew *= samplesperpixel;
1515 r = (k*(255-pp[0]))/255;
1516 g = (k*(255-pp[1]))/255;
1517 b = (k*(255-pp[2]))/255;
1518 *cp++ = PACK(r, g, b);
1519 pp += samplesperpixel);
1526 * 8-bit packed CMYK samples w/Map => RGB
1528 * NB: The conversion of CMYK->RGB is *very* crude.
1530 DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
1532 int samplesperpixel = img->samplesperpixel;
1533 TIFFRGBValue* Map = img->Map;
1537 fromskew *= samplesperpixel;
1539 for (x = w; x-- > 0;) {
1541 r = (k*(255-pp[0]))/255;
1542 g = (k*(255-pp[1]))/255;
1543 b = (k*(255-pp[2]))/255;
1544 *cp++ = PACK(Map[r], Map[g], Map[b]);
1545 pp += samplesperpixel;
1552 #define DECLARESepPutFunc(name) \
1554 TIFFRGBAImage* img,\
1556 uint32 x, uint32 y, \
1557 uint32 w, uint32 h,\
1558 int32 fromskew, int32 toskew,\
1559 unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a\
1563 * 8-bit unpacked samples => RGB
1565 DECLARESepPutFunc(putRGBseparate8bittile)
1567 (void) img; (void) x; (void) y; (void) a;
1569 UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
1570 SKEW(r, g, b, fromskew);
1576 * 8-bit unpacked samples => RGBA w/ associated alpha
1578 DECLARESepPutFunc(putRGBAAseparate8bittile)
1580 (void) img; (void) x; (void) y;
1582 UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
1583 SKEW4(r, g, b, a, fromskew);
1589 * 8-bit unpacked CMYK samples => RGBA
1591 DECLARESepPutFunc(putCMYKseparate8bittile)
1593 (void) img; (void) y;
1595 uint32 rv, gv, bv, kv;
1596 for (x = w; x-- > 0;) {
1598 rv = (kv*(255-*r++))/255;
1599 gv = (kv*(255-*g++))/255;
1600 bv = (kv*(255-*b++))/255;
1601 *cp++ = PACK4(rv,gv,bv,255);
1603 SKEW4(r, g, b, a, fromskew);
1609 * 8-bit unpacked samples => RGBA w/ unassociated alpha
1611 DECLARESepPutFunc(putRGBUAseparate8bittile)
1613 (void) img; (void) y;
1615 uint32 rv, gv, bv, av;
1617 for (x = w; x-- > 0;) {
1619 m = img->UaToAa+(av<<8);
1623 *cp++ = PACK4(rv,gv,bv,av);
1625 SKEW4(r, g, b, a, fromskew);
1631 * 16-bit unpacked samples => RGB
1633 DECLARESepPutFunc(putRGBseparate16bittile)
1635 uint16 *wr = (uint16*) r;
1636 uint16 *wg = (uint16*) g;
1637 uint16 *wb = (uint16*) b;
1638 (void) img; (void) y; (void) a;
1640 for (x = 0; x < w; x++)
1641 *cp++ = PACK(img->Bitdepth16To8[*wr++],
1642 img->Bitdepth16To8[*wg++],
1643 img->Bitdepth16To8[*wb++]);
1644 SKEW(wr, wg, wb, fromskew);
1650 * 16-bit unpacked samples => RGBA w/ associated alpha
1652 DECLARESepPutFunc(putRGBAAseparate16bittile)
1654 uint16 *wr = (uint16*) r;
1655 uint16 *wg = (uint16*) g;
1656 uint16 *wb = (uint16*) b;
1657 uint16 *wa = (uint16*) a;
1658 (void) img; (void) y;
1660 for (x = 0; x < w; x++)
1661 *cp++ = PACK4(img->Bitdepth16To8[*wr++],
1662 img->Bitdepth16To8[*wg++],
1663 img->Bitdepth16To8[*wb++],
1664 img->Bitdepth16To8[*wa++]);
1665 SKEW4(wr, wg, wb, wa, fromskew);
1671 * 16-bit unpacked samples => RGBA w/ unassociated alpha
1673 DECLARESepPutFunc(putRGBUAseparate16bittile)
1675 uint16 *wr = (uint16*) r;
1676 uint16 *wg = (uint16*) g;
1677 uint16 *wb = (uint16*) b;
1678 uint16 *wa = (uint16*) a;
1679 (void) img; (void) y;
1683 for (x = w; x-- > 0;) {
1684 a = img->Bitdepth16To8[*wa++];
1685 m = img->UaToAa+(a<<8);
1686 r = m[img->Bitdepth16To8[*wr++]];
1687 g = m[img->Bitdepth16To8[*wg++]];
1688 b = m[img->Bitdepth16To8[*wb++]];
1689 *cp++ = PACK4(r,g,b,a);
1691 SKEW4(wr, wg, wb, wa, fromskew);
1697 * 8-bit packed CIE L*a*b 1976 samples => RGB
1699 DECLAREContigPutFunc(putcontig8bitCIELab)
1706 for (x = w; x-- > 0;) {
1707 TIFFCIELabToXYZ(img->cielab,
1708 (unsigned char)pp[0],
1712 TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
1713 *cp++ = PACK(r, g, b);
1722 * YCbCr -> RGB conversion and packing routines.
1725 #define YCbCrtoRGB(dst, Y) { \
1727 TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b); \
1728 dst = PACK(r, g, b); \
1732 * 8-bit packed YCbCr samples => RGB
1733 * This function is generic for different sampling sizes,
1734 * and can handle blocks sizes that aren't multiples of the
1735 * sampling size. However, it is substantially less optimized
1736 * than the specific sampling cases. It is used as a fallback
1737 * for difficult blocks.
1740 static void putcontig8bitYCbCrGenericTile(
1745 int32 fromskew, int32 toskew,
1751 uint32* cp1 = cp+w+toskew;
1752 uint32* cp2 = cp1+w+toskew;
1753 uint32* cp3 = cp2+w+toskew;
1754 int32 incr = 3*w+4*toskew;
1756 int group_size = v_group * h_group + 2;
1759 fromskew = (fromskew * group_size) / h_group;
1761 for( yy = 0; yy < h; yy++ )
1763 unsigned char *pp_line;
1764 int y_line_group = yy / v_group;
1765 int y_remainder = yy - y_line_group * v_group;
1767 pp_line = pp + v_line_group *
1770 for( xx = 0; xx < w; xx++ )
1775 for (; h >= 4; h -= 4) {
1781 YCbCrtoRGB(cp [0], pp[ 0]);
1782 YCbCrtoRGB(cp [1], pp[ 1]);
1783 YCbCrtoRGB(cp [2], pp[ 2]);
1784 YCbCrtoRGB(cp [3], pp[ 3]);
1785 YCbCrtoRGB(cp1[0], pp[ 4]);
1786 YCbCrtoRGB(cp1[1], pp[ 5]);
1787 YCbCrtoRGB(cp1[2], pp[ 6]);
1788 YCbCrtoRGB(cp1[3], pp[ 7]);
1789 YCbCrtoRGB(cp2[0], pp[ 8]);
1790 YCbCrtoRGB(cp2[1], pp[ 9]);
1791 YCbCrtoRGB(cp2[2], pp[10]);
1792 YCbCrtoRGB(cp2[3], pp[11]);
1793 YCbCrtoRGB(cp3[0], pp[12]);
1794 YCbCrtoRGB(cp3[1], pp[13]);
1795 YCbCrtoRGB(cp3[2], pp[14]);
1796 YCbCrtoRGB(cp3[3], pp[15]);
1798 cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
1801 cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
1808 * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
1810 DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
1812 uint32* cp1 = cp+w+toskew;
1813 uint32* cp2 = cp1+w+toskew;
1814 uint32* cp3 = cp2+w+toskew;
1815 int32 incr = 3*w+4*toskew;
1818 /* adjust fromskew */
1819 fromskew = (fromskew * 18) / 4;
1820 if ((h & 3) == 0 && (w & 3) == 0) {
1821 for (; h >= 4; h -= 4) {
1827 YCbCrtoRGB(cp [0], pp[ 0]);
1828 YCbCrtoRGB(cp [1], pp[ 1]);
1829 YCbCrtoRGB(cp [2], pp[ 2]);
1830 YCbCrtoRGB(cp [3], pp[ 3]);
1831 YCbCrtoRGB(cp1[0], pp[ 4]);
1832 YCbCrtoRGB(cp1[1], pp[ 5]);
1833 YCbCrtoRGB(cp1[2], pp[ 6]);
1834 YCbCrtoRGB(cp1[3], pp[ 7]);
1835 YCbCrtoRGB(cp2[0], pp[ 8]);
1836 YCbCrtoRGB(cp2[1], pp[ 9]);
1837 YCbCrtoRGB(cp2[2], pp[10]);
1838 YCbCrtoRGB(cp2[3], pp[11]);
1839 YCbCrtoRGB(cp3[0], pp[12]);
1840 YCbCrtoRGB(cp3[1], pp[13]);
1841 YCbCrtoRGB(cp3[2], pp[14]);
1842 YCbCrtoRGB(cp3[3], pp[15]);
1844 cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
1847 cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
1852 for (x = w; x > 0;) {
1858 default: YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
1859 case 3: YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
1860 case 2: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
1861 case 1: YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
1865 default: YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
1866 case 3: YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
1867 case 2: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
1868 case 1: YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
1872 default: YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
1873 case 3: YCbCrtoRGB(cp2[1], pp[ 9]); /* FALLTHROUGH */
1874 case 2: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
1875 case 1: YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
1879 default: YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
1880 case 3: YCbCrtoRGB(cp2[0], pp[ 8]); /* FALLTHROUGH */
1881 case 2: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
1882 case 1: YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
1886 cp += x; cp1 += x; cp2 += x; cp3 += x;
1890 cp += 4; cp1 += 4; cp2 += 4; cp3 += 4;
1898 cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
1905 * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
1907 DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
1909 uint32* cp1 = cp+w+toskew;
1910 int32 incr = 2*toskew+w;
1913 fromskew = (fromskew * 10) / 4;
1914 if ((w & 3) == 0 && (h & 1) == 0) {
1915 for (; h >= 2; h -= 2) {
1921 YCbCrtoRGB(cp [0], pp[0]);
1922 YCbCrtoRGB(cp [1], pp[1]);
1923 YCbCrtoRGB(cp [2], pp[2]);
1924 YCbCrtoRGB(cp [3], pp[3]);
1925 YCbCrtoRGB(cp1[0], pp[4]);
1926 YCbCrtoRGB(cp1[1], pp[5]);
1927 YCbCrtoRGB(cp1[2], pp[6]);
1928 YCbCrtoRGB(cp1[3], pp[7]);
1933 cp += incr, cp1 += incr;
1938 for (x = w; x > 0;) {
1944 default: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
1945 case 1: YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
1949 default: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
1950 case 1: YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
1954 default: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
1955 case 1: YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
1959 default: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
1960 case 1: YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
1976 cp += incr, cp1 += incr;
1983 * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
1985 DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
1988 /* XXX adjust fromskew */
1995 YCbCrtoRGB(cp [0], pp[0]);
1996 YCbCrtoRGB(cp [1], pp[1]);
1997 YCbCrtoRGB(cp [2], pp[2]);
1998 YCbCrtoRGB(cp [3], pp[3]);
2011 case 3: YCbCrtoRGB(cp [2], pp[2]);
2012 case 2: YCbCrtoRGB(cp [1], pp[1]);
2013 case 1: YCbCrtoRGB(cp [0], pp[0]);
2028 * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
2030 DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
2033 int32 incr = 2*toskew+w;
2035 fromskew = (fromskew / 2) * 6;
2042 YCbCrtoRGB(cp[0], pp[0]);
2043 YCbCrtoRGB(cp[1], pp[1]);
2044 YCbCrtoRGB(cp2[0], pp[2]);
2045 YCbCrtoRGB(cp2[1], pp[3]);
2054 YCbCrtoRGB(cp[0], pp[0]);
2055 YCbCrtoRGB(cp2[0], pp[2]);
2070 YCbCrtoRGB(cp[0], pp[0]);
2071 YCbCrtoRGB(cp[1], pp[1]);
2080 YCbCrtoRGB(cp[0], pp[0]);
2086 * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
2088 DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
2091 fromskew = (fromskew * 4) / 2;
2098 YCbCrtoRGB(cp[0], pp[0]);
2099 YCbCrtoRGB(cp[1], pp[1]);
2111 YCbCrtoRGB(cp[0], pp[0]);
2123 * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
2125 DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
2128 int32 incr = 2*toskew+w;
2130 fromskew = (fromskew / 2) * 4;
2137 YCbCrtoRGB(cp[0], pp[0]);
2138 YCbCrtoRGB(cp2[0], pp[1]);
2153 YCbCrtoRGB(cp[0], pp[0]);
2161 * 8-bit packed YCbCr samples w/ no subsampling => RGB
2163 DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
2168 x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
2173 YCbCrtoRGB(*cp++, pp[0]);
2183 * 8-bit packed YCbCr samples w/ no subsampling => RGB
2185 DECLARESepPutFunc(putseparate8bitYCbCr11tile)
2189 /* TODO: naming of input vars is still off, change obfuscating declaration inside define, or resolve obfuscation */
2194 TIFFYCbCrtoRGB(img->ycbcr,*r++,*g++,*b++,&dr,&dg,&db);
2195 *cp++ = PACK(dr,dg,db);
2197 SKEW(r, g, b, fromskew);
2204 initYCbCrConversion(TIFFRGBAImage* img)
2206 static const char module[] = "initYCbCrConversion";
2208 float *luma, *refBlackWhite;
2210 if (img->ycbcr == NULL) {
2211 img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(
2212 TIFFroundup_32(sizeof (TIFFYCbCrToRGB), sizeof (long))
2213 + 4*256*sizeof (TIFFRGBValue)
2214 + 2*256*sizeof (int)
2215 + 3*256*sizeof (int32)
2217 if (img->ycbcr == NULL) {
2218 TIFFErrorExt(img->tif->tif_clientdata, module,
2219 "No space for YCbCr->RGB conversion state");
2224 TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
2225 TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
2227 if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
2232 static tileContigRoutine
2233 initCIELabConversion(TIFFRGBAImage* img)
2235 static const char module[] = "initCIELabConversion";
2241 img->cielab = (TIFFCIELabToRGB *)
2242 _TIFFmalloc(sizeof(TIFFCIELabToRGB));
2244 TIFFErrorExt(img->tif->tif_clientdata, module,
2245 "No space for CIE L*a*b*->RGB conversion state.");
2250 TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
2251 refWhite[1] = 100.0F;
2252 refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
2253 refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1])
2254 / whitePoint[1] * refWhite[1];
2255 if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0) {
2256 TIFFErrorExt(img->tif->tif_clientdata, module,
2257 "Failed to initialize CIE L*a*b*->RGB conversion state.");
2258 _TIFFfree(img->cielab);
2262 return putcontig8bitCIELab;
2266 * Greyscale images with less than 8 bits/sample are handled
2267 * with a table to avoid lots of shifts and masks. The table
2268 * is setup so that put*bwtile (below) can retrieve 8/bitspersample
2269 * pixel values simply by indexing into the table with one
2273 makebwmap(TIFFRGBAImage* img)
2275 TIFFRGBValue* Map = img->Map;
2276 int bitspersample = img->bitspersample;
2277 int nsamples = 8 / bitspersample;
2284 img->BWmap = (uint32**) _TIFFmalloc(
2285 256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
2286 if (img->BWmap == NULL) {
2287 TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for B&W mapping table");
2290 p = (uint32*)(img->BWmap + 256);
2291 for (i = 0; i < 256; i++) {
2294 switch (bitspersample) {
2295 #define GREY(x) c = Map[x]; *p++ = PACK(c,c,c);
2327 * Construct a mapping table to convert from the range
2328 * of the data samples to [0,255] --for display. This
2329 * process also handles inverting B&W images when needed.
2332 setupMap(TIFFRGBAImage* img)
2336 range = (int32)((1L<<img->bitspersample)-1);
2338 /* treat 16 bit the same as eight bit */
2339 if( img->bitspersample == 16 )
2340 range = (int32) 255;
2342 img->Map = (TIFFRGBValue*) _TIFFmalloc((range+1) * sizeof (TIFFRGBValue));
2343 if (img->Map == NULL) {
2344 TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
2345 "No space for photometric conversion table");
2348 if (img->photometric == PHOTOMETRIC_MINISWHITE) {
2349 for (x = 0; x <= range; x++)
2350 img->Map[x] = (TIFFRGBValue) (((range - x) * 255) / range);
2352 for (x = 0; x <= range; x++)
2353 img->Map[x] = (TIFFRGBValue) ((x * 255) / range);
2355 if (img->bitspersample <= 16 &&
2356 (img->photometric == PHOTOMETRIC_MINISBLACK ||
2357 img->photometric == PHOTOMETRIC_MINISWHITE)) {
2359 * Use photometric mapping table to construct
2360 * unpacking tables for samples <= 8 bits.
2362 if (!makebwmap(img))
2364 /* no longer need Map, free it */
2365 _TIFFfree(img->Map), img->Map = NULL;
2371 checkcmap(TIFFRGBAImage* img)
2373 uint16* r = img->redcmap;
2374 uint16* g = img->greencmap;
2375 uint16* b = img->bluecmap;
2376 long n = 1L<<img->bitspersample;
2379 if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
2385 cvtcmap(TIFFRGBAImage* img)
2387 uint16* r = img->redcmap;
2388 uint16* g = img->greencmap;
2389 uint16* b = img->bluecmap;
2392 for (i = (1L<<img->bitspersample)-1; i >= 0; i--) {
2393 #define CVT(x) ((uint16)((x)>>8))
2402 * Palette images with <= 8 bits/sample are handled
2403 * with a table to avoid lots of shifts and masks. The table
2404 * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
2405 * pixel values simply by indexing into the table with one
2409 makecmap(TIFFRGBAImage* img)
2411 int bitspersample = img->bitspersample;
2412 int nsamples = 8 / bitspersample;
2413 uint16* r = img->redcmap;
2414 uint16* g = img->greencmap;
2415 uint16* b = img->bluecmap;
2419 img->PALmap = (uint32**) _TIFFmalloc(
2420 256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
2421 if (img->PALmap == NULL) {
2422 TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for Palette mapping table");
2425 p = (uint32*)(img->PALmap + 256);
2426 for (i = 0; i < 256; i++) {
2429 #define CMAP(x) c = (TIFFRGBValue) x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff);
2430 switch (bitspersample) {
2461 * Construct any mapping table used
2462 * by the associated put routine.
2465 buildMap(TIFFRGBAImage* img)
2467 switch (img->photometric) {
2468 case PHOTOMETRIC_RGB:
2469 case PHOTOMETRIC_YCBCR:
2470 case PHOTOMETRIC_SEPARATED:
2471 if (img->bitspersample == 8)
2474 case PHOTOMETRIC_MINISBLACK:
2475 case PHOTOMETRIC_MINISWHITE:
2479 case PHOTOMETRIC_PALETTE:
2481 * Convert 16-bit colormap to 8-bit (unless it looks
2482 * like an old-style 8-bit colormap).
2484 if (checkcmap(img) == 16)
2487 TIFFWarningExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "Assuming 8-bit colormap");
2489 * Use mapping table and colormap to construct
2490 * unpacking tables for samples < 8 bits.
2492 if (img->bitspersample <= 8 && !makecmap(img))
2500 * Select the appropriate conversion routine for packed data.
2503 PickContigCase(TIFFRGBAImage* img)
2505 img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
2506 img->put.contig = NULL;
2507 switch (img->photometric) {
2508 case PHOTOMETRIC_RGB:
2509 switch (img->bitspersample) {
2511 if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
2512 img->put.contig = putRGBAAcontig8bittile;
2513 else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
2515 if (BuildMapUaToAa(img))
2516 img->put.contig = putRGBUAcontig8bittile;
2519 img->put.contig = putRGBcontig8bittile;
2522 if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
2524 if (BuildMapBitdepth16To8(img))
2525 img->put.contig = putRGBAAcontig16bittile;
2527 else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
2529 if (BuildMapBitdepth16To8(img) &&
2530 BuildMapUaToAa(img))
2531 img->put.contig = putRGBUAcontig16bittile;
2535 if (BuildMapBitdepth16To8(img))
2536 img->put.contig = putRGBcontig16bittile;
2541 case PHOTOMETRIC_SEPARATED:
2542 if (buildMap(img)) {
2543 if (img->bitspersample == 8) {
2545 img->put.contig = putRGBcontig8bitCMYKtile;
2547 img->put.contig = putRGBcontig8bitCMYKMaptile;
2551 case PHOTOMETRIC_PALETTE:
2552 if (buildMap(img)) {
2553 switch (img->bitspersample) {
2555 img->put.contig = put8bitcmaptile;
2558 img->put.contig = put4bitcmaptile;
2561 img->put.contig = put2bitcmaptile;
2564 img->put.contig = put1bitcmaptile;
2569 case PHOTOMETRIC_MINISWHITE:
2570 case PHOTOMETRIC_MINISBLACK:
2571 if (buildMap(img)) {
2572 switch (img->bitspersample) {
2574 img->put.contig = put16bitbwtile;
2577 if (img->alpha && img->samplesperpixel == 2)
2578 img->put.contig = putagreytile;
2580 img->put.contig = putgreytile;
2583 img->put.contig = put4bitbwtile;
2586 img->put.contig = put2bitbwtile;
2589 img->put.contig = put1bitbwtile;
2594 case PHOTOMETRIC_YCBCR:
2595 if ((img->bitspersample==8) && (img->samplesperpixel==3))
2597 if (initYCbCrConversion(img)!=0)
2600 * The 6.0 spec says that subsampling must be
2601 * one of 1, 2, or 4, and that vertical subsampling
2602 * must always be <= horizontal subsampling; so
2603 * there are only a few possibilities and we just
2604 * enumerate the cases.
2605 * Joris: added support for the [1,2] case, nonetheless, to accommodate
2608 uint16 SubsamplingHor;
2609 uint16 SubsamplingVer;
2610 TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &SubsamplingHor, &SubsamplingVer);
2611 switch ((SubsamplingHor<<4)|SubsamplingVer) {
2613 img->put.contig = putcontig8bitYCbCr44tile;
2616 img->put.contig = putcontig8bitYCbCr42tile;
2619 img->put.contig = putcontig8bitYCbCr41tile;
2622 img->put.contig = putcontig8bitYCbCr22tile;
2625 img->put.contig = putcontig8bitYCbCr21tile;
2628 img->put.contig = putcontig8bitYCbCr12tile;
2631 img->put.contig = putcontig8bitYCbCr11tile;
2637 case PHOTOMETRIC_CIELAB:
2638 if (buildMap(img)) {
2639 if (img->bitspersample == 8)
2640 img->put.contig = initCIELabConversion(img);
2644 return ((img->get!=NULL) && (img->put.contig!=NULL));
2648 * Select the appropriate conversion routine for unpacked data.
2650 * NB: we assume that unpacked single channel data is directed
2651 * to the "packed routines.
2654 PickSeparateCase(TIFFRGBAImage* img)
2656 img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
2657 img->put.separate = NULL;
2658 switch (img->photometric) {
2659 case PHOTOMETRIC_MINISWHITE:
2660 case PHOTOMETRIC_MINISBLACK:
2661 /* greyscale images processed pretty much as RGB by gtTileSeparate */
2662 case PHOTOMETRIC_RGB:
2663 switch (img->bitspersample) {
2665 if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
2666 img->put.separate = putRGBAAseparate8bittile;
2667 else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
2669 if (BuildMapUaToAa(img))
2670 img->put.separate = putRGBUAseparate8bittile;
2673 img->put.separate = putRGBseparate8bittile;
2676 if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
2678 if (BuildMapBitdepth16To8(img))
2679 img->put.separate = putRGBAAseparate16bittile;
2681 else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
2683 if (BuildMapBitdepth16To8(img) &&
2684 BuildMapUaToAa(img))
2685 img->put.separate = putRGBUAseparate16bittile;
2689 if (BuildMapBitdepth16To8(img))
2690 img->put.separate = putRGBseparate16bittile;
2695 case PHOTOMETRIC_SEPARATED:
2696 if (img->bitspersample == 8 && img->samplesperpixel == 4)
2698 img->alpha = 1; // Not alpha, but seems like the only way to get 4th band
2699 img->put.separate = putCMYKseparate8bittile;
2702 case PHOTOMETRIC_YCBCR:
2703 if ((img->bitspersample==8) && (img->samplesperpixel==3))
2705 if (initYCbCrConversion(img)!=0)
2708 TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
2709 switch ((hs<<4)|vs) {
2711 img->put.separate = putseparate8bitYCbCr11tile;
2713 /* TODO: add other cases here */
2719 return ((img->get!=NULL) && (img->put.separate!=NULL));
2723 BuildMapUaToAa(TIFFRGBAImage* img)
2725 static const char module[]="BuildMapUaToAa";
2728 assert(img->UaToAa==NULL);
2729 img->UaToAa=_TIFFmalloc(65536);
2730 if (img->UaToAa==NULL)
2732 TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
2736 for (na=0; na<256; na++)
2738 for (nv=0; nv<256; nv++)
2739 *m++=(nv*na+127)/255;
2745 BuildMapBitdepth16To8(TIFFRGBAImage* img)
2747 static const char module[]="BuildMapBitdepth16To8";
2750 assert(img->Bitdepth16To8==NULL);
2751 img->Bitdepth16To8=_TIFFmalloc(65536);
2752 if (img->Bitdepth16To8==NULL)
2754 TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
2757 m=img->Bitdepth16To8;
2758 for (n=0; n<65536; n++)
2765 * Read a whole strip off data from the file, and convert to RGBA form.
2766 * If this is the last strip, then it will only contain the portion of
2767 * the strip that is actually within the image space. The result is
2768 * organized in bottom to top form.
2773 TIFFReadRGBAStrip(TIFF* tif, uint32 row, uint32 * raster )
2776 char emsg[1024] = "";
2779 uint32 rowsperstrip, rows_to_read;
2781 if( TIFFIsTiled( tif ) )
2783 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
2784 "Can't use TIFFReadRGBAStrip() with tiled file.");
2788 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
2789 if( (row % rowsperstrip) != 0 )
2791 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
2792 "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
2796 if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
2798 img.row_offset = row;
2801 if( row + rowsperstrip > img.height )
2802 rows_to_read = img.height - row;
2804 rows_to_read = rowsperstrip;
2806 ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read );
2808 TIFFRGBAImageEnd(&img);
2810 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
2818 * Read a whole tile off data from the file, and convert to RGBA form.
2819 * The returned RGBA data is organized from bottom to top of tile,
2820 * and may include zeroed areas if the tile extends off the image.
2824 TIFFReadRGBATile(TIFF* tif, uint32 col, uint32 row, uint32 * raster)
2827 char emsg[1024] = "";
2830 uint32 tile_xsize, tile_ysize;
2831 uint32 read_xsize, read_ysize;
2835 * Verify that our request is legal - on a tile file, and on a
2839 if( !TIFFIsTiled( tif ) )
2841 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
2842 "Can't use TIFFReadRGBATile() with stripped file.");
2846 TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
2847 TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
2848 if( (col % tile_xsize) != 0 || (row % tile_ysize) != 0 )
2850 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
2851 "Row/col passed to TIFFReadRGBATile() must be top"
2852 "left corner of a tile.");
2857 * Setup the RGBA reader.
2860 if (!TIFFRGBAImageOK(tif, emsg)
2861 || !TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
2862 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
2867 * The TIFFRGBAImageGet() function doesn't allow us to get off the
2868 * edge of the image, even to fill an otherwise valid tile. So we
2869 * figure out how much we can read, and fix up the tile buffer to
2870 * a full tile configuration afterwards.
2873 if( row + tile_ysize > img.height )
2874 read_ysize = img.height - row;
2876 read_ysize = tile_ysize;
2878 if( col + tile_xsize > img.width )
2879 read_xsize = img.width - col;
2881 read_xsize = tile_xsize;
2884 * Read the chunk of imagery.
2887 img.row_offset = row;
2888 img.col_offset = col;
2890 ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize );
2892 TIFFRGBAImageEnd(&img);
2895 * If our read was incomplete we will need to fix up the tile by
2896 * shifting the data around as if a full tile of data is being returned.
2898 * This is all the more complicated because the image is organized in
2899 * bottom to top format.
2902 if( read_xsize == tile_xsize && read_ysize == tile_ysize )
2905 for( i_row = 0; i_row < read_ysize; i_row++ ) {
2906 memmove( raster + (tile_ysize - i_row - 1) * tile_xsize,
2907 raster + (read_ysize - i_row - 1) * read_xsize,
2908 read_xsize * sizeof(uint32) );
2909 _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize+read_xsize,
2910 0, sizeof(uint32) * (tile_xsize - read_xsize) );
2913 for( i_row = read_ysize; i_row < tile_ysize; i_row++ ) {
2914 _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize,
2915 0, sizeof(uint32) * tile_xsize );
2921 /* vim: set ts=8 sts=8 sw=8 noet: */