1 /* $Id: tif_getimage.c,v 1.78 2011-02-23 21:46:09 fwarmerdam 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);
186 case PHOTOMETRIC_CIELAB:
189 sprintf(emsg, "Sorry, can not handle image with %s=%d",
190 photoTag, photometric);
197 TIFFRGBAImageEnd(TIFFRGBAImage* img)
200 _TIFFfree(img->Map), img->Map = NULL;
202 _TIFFfree(img->BWmap), img->BWmap = NULL;
204 _TIFFfree(img->PALmap), img->PALmap = NULL;
206 _TIFFfree(img->ycbcr), img->ycbcr = NULL;
208 _TIFFfree(img->cielab), img->cielab = NULL;
210 _TIFFfree(img->UaToAa), img->UaToAa = NULL;
211 if (img->Bitdepth16To8)
212 _TIFFfree(img->Bitdepth16To8), img->Bitdepth16To8 = NULL;
215 _TIFFfree( img->redcmap );
216 _TIFFfree( img->greencmap );
217 _TIFFfree( img->bluecmap );
218 img->redcmap = img->greencmap = img->bluecmap = NULL;
223 isCCITTCompression(TIFF* tif)
226 TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
227 return (compress == COMPRESSION_CCITTFAX3 ||
228 compress == COMPRESSION_CCITTFAX4 ||
229 compress == COMPRESSION_CCITTRLE ||
230 compress == COMPRESSION_CCITTRLEW);
234 TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
241 uint16 *red_orig, *green_orig, *blue_orig;
244 /* Initialize to normal values */
248 img->greencmap = NULL;
249 img->bluecmap = NULL;
250 img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */
253 img->stoponerr = stop;
254 TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
255 switch (img->bitspersample) {
263 sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
268 TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
269 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
270 &extrasamples, &sampleinfo);
271 if (extrasamples >= 1)
273 switch (sampleinfo[0]) {
274 case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without */
275 if (img->samplesperpixel > 3) /* correct info about alpha channel */
276 img->alpha = EXTRASAMPLE_ASSOCALPHA;
278 case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */
279 case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */
280 img->alpha = sampleinfo[0];
285 #ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
286 if( !TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
287 img->photometric = PHOTOMETRIC_MINISWHITE;
289 if( extrasamples == 0
290 && img->samplesperpixel == 4
291 && img->photometric == PHOTOMETRIC_RGB )
293 img->alpha = EXTRASAMPLE_ASSOCALPHA;
298 colorchannels = img->samplesperpixel - extrasamples;
299 TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
300 TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
301 if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
302 switch (colorchannels) {
304 if (isCCITTCompression(tif))
305 img->photometric = PHOTOMETRIC_MINISWHITE;
307 img->photometric = PHOTOMETRIC_MINISBLACK;
310 img->photometric = PHOTOMETRIC_RGB;
313 sprintf(emsg, "Missing needed %s tag", photoTag);
317 switch (img->photometric) {
318 case PHOTOMETRIC_PALETTE:
319 if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
320 &red_orig, &green_orig, &blue_orig)) {
321 sprintf(emsg, "Missing required \"Colormap\" tag");
325 /* copy the colormaps so we can modify them */
326 n_color = (1L << img->bitspersample);
327 img->redcmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
328 img->greencmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
329 img->bluecmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
330 if( !img->redcmap || !img->greencmap || !img->bluecmap ) {
331 sprintf(emsg, "Out of memory for colormap copy");
335 _TIFFmemcpy( img->redcmap, red_orig, n_color * 2 );
336 _TIFFmemcpy( img->greencmap, green_orig, n_color * 2 );
337 _TIFFmemcpy( img->bluecmap, blue_orig, n_color * 2 );
340 case PHOTOMETRIC_MINISWHITE:
341 case PHOTOMETRIC_MINISBLACK:
342 if (planarconfig == PLANARCONFIG_CONTIG
343 && img->samplesperpixel != 1
344 && img->bitspersample < 8 ) {
346 "Sorry, can not handle contiguous data with %s=%d, "
347 "and %s=%d and Bits/Sample=%d",
348 photoTag, img->photometric,
349 "Samples/pixel", img->samplesperpixel,
354 case PHOTOMETRIC_YCBCR:
355 /* It would probably be nice to have a reality check here. */
356 if (planarconfig == PLANARCONFIG_CONTIG)
357 /* can rely on libjpeg to convert to RGB */
358 /* XXX should restore current state on exit */
360 case COMPRESSION_JPEG:
362 * TODO: when complete tests verify complete desubsampling
363 * and YCbCr handling, remove use of TIFFTAG_JPEGCOLORMODE in
364 * favor of tif_getimage.c native handling
366 TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
367 img->photometric = PHOTOMETRIC_RGB;
374 * TODO: if at all meaningful and useful, make more complete
375 * support check here, or better still, refactor to let supporting
376 * code decide whether there is support and what meaningfull
380 case PHOTOMETRIC_RGB:
381 if (colorchannels < 3) {
382 sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
383 "Color channels", colorchannels);
387 case PHOTOMETRIC_SEPARATED:
390 TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
391 if (inkset != INKSET_CMYK) {
392 sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
396 if (img->samplesperpixel < 4) {
397 sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
398 "Samples/pixel", img->samplesperpixel);
403 case PHOTOMETRIC_LOGL:
404 if (compress != COMPRESSION_SGILOG) {
405 sprintf(emsg, "Sorry, LogL data must have %s=%d",
406 "Compression", COMPRESSION_SGILOG);
409 TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
410 img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */
411 img->bitspersample = 8;
413 case PHOTOMETRIC_LOGLUV:
414 if (compress != COMPRESSION_SGILOG && compress != COMPRESSION_SGILOG24) {
415 sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
416 "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
419 if (planarconfig != PLANARCONFIG_CONTIG) {
420 sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
421 "Planarconfiguration", planarconfig);
424 TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
425 img->photometric = PHOTOMETRIC_RGB; /* little white lie */
426 img->bitspersample = 8;
428 case PHOTOMETRIC_CIELAB:
431 sprintf(emsg, "Sorry, can not handle image with %s=%d",
432 photoTag, img->photometric);
441 img->Bitdepth16To8 = NULL;
442 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
443 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
444 TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
446 !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);
448 if (!PickContigCase(img)) {
449 sprintf(emsg, "Sorry, can not handle image");
453 if (!PickSeparateCase(img)) {
454 sprintf(emsg, "Sorry, can not handle image");
461 _TIFFfree( img->redcmap );
462 _TIFFfree( img->greencmap );
463 _TIFFfree( img->bluecmap );
464 img->redcmap = img->greencmap = img->bluecmap = NULL;
469 TIFFRGBAImageGet(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
471 if (img->get == NULL) {
472 TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No \"get\" routine setup");
475 if (img->put.any == NULL) {
476 TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
477 "No \"put\" routine setupl; probably can not handle image format");
480 return (*img->get)(img, raster, w, h);
484 * Read the specified image into an ABGR-format rastertaking in account
485 * specified orientation.
488 TIFFReadRGBAImageOriented(TIFF* tif,
489 uint32 rwidth, uint32 rheight, uint32* raster,
490 int orientation, int stop)
492 char emsg[1024] = "";
496 if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, stop, emsg)) {
497 img.req_orientation = orientation;
498 /* XXX verify rwidth and rheight against width and height */
499 ok = TIFFRGBAImageGet(&img, raster+(rheight-img.height)*rwidth,
501 TIFFRGBAImageEnd(&img);
503 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
510 * Read the specified image into an ABGR-format raster. Use bottom left
511 * origin for raster by default.
514 TIFFReadRGBAImage(TIFF* tif,
515 uint32 rwidth, uint32 rheight, uint32* raster, int stop)
517 return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster,
518 ORIENTATION_BOTLEFT, stop);
522 setorientation(TIFFRGBAImage* img)
524 switch (img->orientation) {
525 case ORIENTATION_TOPLEFT:
526 case ORIENTATION_LEFTTOP:
527 if (img->req_orientation == ORIENTATION_TOPRIGHT ||
528 img->req_orientation == ORIENTATION_RIGHTTOP)
529 return FLIP_HORIZONTALLY;
530 else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
531 img->req_orientation == ORIENTATION_RIGHTBOT)
532 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
533 else if (img->req_orientation == ORIENTATION_BOTLEFT ||
534 img->req_orientation == ORIENTATION_LEFTBOT)
535 return FLIP_VERTICALLY;
538 case ORIENTATION_TOPRIGHT:
539 case ORIENTATION_RIGHTTOP:
540 if (img->req_orientation == ORIENTATION_TOPLEFT ||
541 img->req_orientation == ORIENTATION_LEFTTOP)
542 return FLIP_HORIZONTALLY;
543 else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
544 img->req_orientation == ORIENTATION_RIGHTBOT)
545 return FLIP_VERTICALLY;
546 else if (img->req_orientation == ORIENTATION_BOTLEFT ||
547 img->req_orientation == ORIENTATION_LEFTBOT)
548 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
551 case ORIENTATION_BOTRIGHT:
552 case ORIENTATION_RIGHTBOT:
553 if (img->req_orientation == ORIENTATION_TOPLEFT ||
554 img->req_orientation == ORIENTATION_LEFTTOP)
555 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
556 else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
557 img->req_orientation == ORIENTATION_RIGHTTOP)
558 return FLIP_VERTICALLY;
559 else if (img->req_orientation == ORIENTATION_BOTLEFT ||
560 img->req_orientation == ORIENTATION_LEFTBOT)
561 return FLIP_HORIZONTALLY;
564 case ORIENTATION_BOTLEFT:
565 case ORIENTATION_LEFTBOT:
566 if (img->req_orientation == ORIENTATION_TOPLEFT ||
567 img->req_orientation == ORIENTATION_LEFTTOP)
568 return FLIP_VERTICALLY;
569 else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
570 img->req_orientation == ORIENTATION_RIGHTTOP)
571 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
572 else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
573 img->req_orientation == ORIENTATION_RIGHTBOT)
574 return FLIP_HORIZONTALLY;
577 default: /* NOTREACHED */
583 * Get an tile-organized image that has
584 * PlanarConfiguration contiguous if SamplesPerPixel > 1
586 * SamplesPerPixel == 1
589 gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
591 TIFF* tif = img->tif;
592 tileContigRoutine put = img->put.contig;
593 uint32 col, row, y, rowstoread;
597 int32 fromskew, toskew;
601 buf = (unsigned char*) _TIFFmalloc(TIFFTileSize(tif));
603 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
606 _TIFFmemset(buf, 0, TIFFTileSize(tif));
607 TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
608 TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
610 flip = setorientation(img);
611 if (flip & FLIP_VERTICALLY) {
613 toskew = -(int32)(tw + w);
617 toskew = -(int32)(tw - w);
620 for (row = 0; row < h; row += nrow)
622 rowstoread = th - (row + img->row_offset) % th;
623 nrow = (row + rowstoread > h ? h - row : rowstoread);
624 for (col = 0; col < w; col += tw)
626 if (TIFFReadTile(tif, buf, col+img->col_offset,
627 row+img->row_offset, 0, 0)==(tmsize_t)(-1) && img->stoponerr)
633 pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);
638 * Tile is clipped horizontally. Calculate
639 * visible portion and skewing factors.
641 uint32 npix = w - col;
642 fromskew = tw - npix;
643 (*put)(img, raster+y*w+col, col, y,
644 npix, nrow, fromskew, toskew + fromskew, buf + pos);
648 (*put)(img, raster+y*w+col, col, y, tw, nrow, 0, toskew, buf + pos);
652 y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
656 if (flip & FLIP_HORIZONTALLY) {
659 for (line = 0; line < h; line++) {
660 uint32 *left = raster + (line * w);
661 uint32 *right = left + w - 1;
663 while ( left < right ) {
676 * Get an tile-organized image that has
677 * SamplesPerPixel > 1
678 * PlanarConfiguration separated
679 * We assume that all such images are RGB.
682 gtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
684 TIFF* tif = img->tif;
685 tileSeparateRoutine put = img->put.separate;
686 uint32 col, row, y, rowstoread;
695 int32 fromskew, toskew;
696 int alpha = img->alpha;
701 tilesize = TIFFTileSize(tif);
702 buf = (unsigned char*) _TIFFmalloc((alpha?4:3)*tilesize);
704 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
707 _TIFFmemset(buf, 0, (alpha?4:3)*tilesize);
711 pa = (alpha?(p2+tilesize):NULL);
712 TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
713 TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
715 flip = setorientation(img);
716 if (flip & FLIP_VERTICALLY) {
718 toskew = -(int32)(tw + w);
722 toskew = -(int32)(tw - w);
725 switch( img->photometric )
727 case PHOTOMETRIC_MINISWHITE:
728 case PHOTOMETRIC_MINISBLACK:
729 case PHOTOMETRIC_PALETTE:
739 for (row = 0; row < h; row += nrow)
741 rowstoread = th - (row + img->row_offset) % th;
742 nrow = (row + rowstoread > h ? h - row : rowstoread);
743 for (col = 0; col < w; col += tw)
745 if (TIFFReadTile(tif, p0, col+img->col_offset,
746 row+img->row_offset,0,0)==(tmsize_t)(-1) && img->stoponerr)
751 if (colorchannels > 1
752 && TIFFReadTile(tif, p1, col+img->col_offset,
753 row+img->row_offset,0,1) == (tmsize_t)(-1)
759 if (colorchannels > 1
760 && TIFFReadTile(tif, p2, col+img->col_offset,
761 row+img->row_offset,0,2) == (tmsize_t)(-1)
768 && TIFFReadTile(tif,pa,col+img->col_offset,
769 row+img->row_offset,0,colorchannels) == (tmsize_t)(-1)
776 pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);
781 * Tile is clipped horizontally. Calculate
782 * visible portion and skewing factors.
784 uint32 npix = w - col;
785 fromskew = tw - npix;
786 (*put)(img, raster+y*w+col, col, y,
787 npix, nrow, fromskew, toskew + fromskew,
788 p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
790 (*put)(img, raster+y*w+col, col, y,
791 tw, nrow, 0, toskew, p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
795 y += (flip & FLIP_VERTICALLY ?-(int32) nrow : (int32) nrow);
798 if (flip & FLIP_HORIZONTALLY) {
801 for (line = 0; line < h; line++) {
802 uint32 *left = raster + (line * w);
803 uint32 *right = left + w - 1;
805 while ( left < right ) {
819 * Get a strip-organized image that has
820 * PlanarConfiguration contiguous if SamplesPerPixel > 1
822 * SamplesPerPixel == 1
825 gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
827 TIFF* tif = img->tif;
828 tileContigRoutine put = img->put.contig;
829 uint32 row, y, nrow, nrowsub, rowstoread;
833 uint16 subsamplinghor,subsamplingver;
834 uint32 imagewidth = img->width;
836 int32 fromskew, toskew;
839 buf = (unsigned char*) _TIFFmalloc(TIFFStripSize(tif));
841 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
844 _TIFFmemset(buf, 0, TIFFStripSize(tif));
846 flip = setorientation(img);
847 if (flip & FLIP_VERTICALLY) {
849 toskew = -(int32)(w + w);
852 toskew = -(int32)(w - w);
855 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
856 TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor, &subsamplingver);
857 scanline = TIFFScanlineSize(tif);
858 fromskew = (w < imagewidth ? imagewidth - w : 0);
859 for (row = 0; row < h; row += nrow)
861 rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
862 nrow = (row + rowstoread > h ? h - row : rowstoread);
864 if ((nrowsub%subsamplingver)!=0)
865 nrowsub+=subsamplingver-nrowsub%subsamplingver;
866 if (TIFFReadEncodedStrip(tif,
867 TIFFComputeStrip(tif,row+img->row_offset, 0),
869 ((row + img->row_offset)%rowsperstrip + nrowsub) * scanline)==(tmsize_t)(-1)
876 pos = ((row + img->row_offset) % rowsperstrip) * scanline;
877 (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf + pos);
878 y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
881 if (flip & FLIP_HORIZONTALLY) {
884 for (line = 0; line < h; line++) {
885 uint32 *left = raster + (line * w);
886 uint32 *right = left + w - 1;
888 while ( left < right ) {
902 * Get a strip-organized image with
903 * SamplesPerPixel > 1
904 * PlanarConfiguration separated
905 * We assume that all such images are RGB.
908 gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
910 TIFF* tif = img->tif;
911 tileSeparateRoutine put = img->put.separate;
913 unsigned char *p0, *p1, *p2, *pa;
914 uint32 row, y, nrow, rowstoread;
917 uint32 rowsperstrip, offset_row;
918 uint32 imagewidth = img->width;
920 int32 fromskew, toskew;
921 int alpha = img->alpha;
922 int ret = 1, flip, colorchannels;
924 stripsize = TIFFStripSize(tif);
925 p0 = buf = (unsigned char *)_TIFFmalloc((alpha?4:3)*stripsize);
927 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
930 _TIFFmemset(buf, 0, (alpha?4:3)*stripsize);
933 pa = (alpha?(p2+stripsize):NULL);
935 flip = setorientation(img);
936 if (flip & FLIP_VERTICALLY) {
938 toskew = -(int32)(w + w);
942 toskew = -(int32)(w - w);
945 switch( img->photometric )
947 case PHOTOMETRIC_MINISWHITE:
948 case PHOTOMETRIC_MINISBLACK:
949 case PHOTOMETRIC_PALETTE:
959 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
960 scanline = TIFFScanlineSize(tif);
961 fromskew = (w < imagewidth ? imagewidth - w : 0);
962 for (row = 0; row < h; row += nrow)
964 rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
965 nrow = (row + rowstoread > h ? h - row : rowstoread);
966 offset_row = row + img->row_offset;
967 if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
968 p0, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
974 if (colorchannels > 1
975 && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),
976 p1, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
982 if (colorchannels > 1
983 && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),
984 p2, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
992 if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, colorchannels),
993 pa, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
1001 pos = ((row + img->row_offset) % rowsperstrip) * scanline;
1002 (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, p0 + pos, p1 + pos,
1003 p2 + pos, (alpha?(pa+pos):NULL));
1004 y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
1007 if (flip & FLIP_HORIZONTALLY) {
1010 for (line = 0; line < h; line++) {
1011 uint32 *left = raster + (line * w);
1012 uint32 *right = left + w - 1;
1014 while ( left < right ) {
1015 uint32 temp = *left;
1028 * The following routines move decoded data returned
1029 * from the TIFF library into rasters filled with packed
1030 * ABGR pixels (i.e. suitable for passing to lrecwrite.)
1032 * The routines have been created according to the most
1033 * important cases and optimized. PickContigCase and
1034 * PickSeparateCase analyze the parameters and select
1035 * the appropriate "get" and "put" routine to use.
1037 #define REPEAT8(op) REPEAT4(op); REPEAT4(op)
1038 #define REPEAT4(op) REPEAT2(op); REPEAT2(op)
1039 #define REPEAT2(op) op; op
1040 #define CASE8(x,op) \
1042 case 7: op; case 6: op; case 5: op; \
1043 case 4: op; case 3: op; case 2: op; \
1046 #define CASE4(x,op) switch (x) { case 3: op; case 2: op; case 1: op; }
1049 #define UNROLL8(w, op1, op2) { \
1051 for (_x = w; _x >= 8; _x -= 8) { \
1060 #define UNROLL4(w, op1, op2) { \
1062 for (_x = w; _x >= 4; _x -= 4) { \
1071 #define UNROLL2(w, op1, op2) { \
1073 for (_x = w; _x >= 2; _x -= 2) { \
1083 #define SKEW(r,g,b,skew) { r += skew; g += skew; b += skew; }
1084 #define SKEW4(r,g,b,a,skew) { r += skew; g += skew; b += skew; a+= skew; }
1086 #define A1 (((uint32)0xffL)<<24)
1087 #define PACK(r,g,b) \
1088 ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|A1)
1089 #define PACK4(r,g,b,a) \
1090 ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|((uint32)(a)<<24))
1091 #define W2B(v) (((v)>>8)&0xff)
1092 /* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */
1093 #define PACKW(r,g,b) \
1094 ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|A1)
1095 #define PACKW4(r,g,b,a) \
1096 ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|((uint32)W2B(a)<<24))
1098 #define DECLAREContigPutFunc(name) \
1100 TIFFRGBAImage* img, \
1102 uint32 x, uint32 y, \
1103 uint32 w, uint32 h, \
1104 int32 fromskew, int32 toskew, \
1109 * 8-bit palette => colormap/RGB
1111 DECLAREContigPutFunc(put8bitcmaptile)
1113 uint32** PALmap = img->PALmap;
1114 int samplesperpixel = img->samplesperpixel;
1118 for (x = w; x-- > 0;)
1120 *cp++ = PALmap[*pp][0];
1121 pp += samplesperpixel;
1129 * 4-bit palette => colormap/RGB
1131 DECLAREContigPutFunc(put4bitcmaptile)
1133 uint32** PALmap = img->PALmap;
1139 UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
1146 * 2-bit palette => colormap/RGB
1148 DECLAREContigPutFunc(put2bitcmaptile)
1150 uint32** PALmap = img->PALmap;
1156 UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
1163 * 1-bit palette => colormap/RGB
1165 DECLAREContigPutFunc(put1bitcmaptile)
1167 uint32** PALmap = img->PALmap;
1173 UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
1180 * 8-bit greyscale => colormap/RGB
1182 DECLAREContigPutFunc(putgreytile)
1184 int samplesperpixel = img->samplesperpixel;
1185 uint32** BWmap = img->BWmap;
1189 for (x = w; x-- > 0;)
1191 *cp++ = BWmap[*pp][0];
1192 pp += samplesperpixel;
1200 * 16-bit greyscale => colormap/RGB
1202 DECLAREContigPutFunc(put16bitbwtile)
1204 int samplesperpixel = img->samplesperpixel;
1205 uint32** BWmap = img->BWmap;
1209 uint16 *wp = (uint16 *) pp;
1211 for (x = w; x-- > 0;)
1213 /* use high order byte of 16bit value */
1215 *cp++ = BWmap[*wp >> 8][0];
1216 pp += 2 * samplesperpixel;
1217 wp += samplesperpixel;
1225 * 1-bit bilevel => colormap/RGB
1227 DECLAREContigPutFunc(put1bitbwtile)
1229 uint32** BWmap = img->BWmap;
1235 UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
1242 * 2-bit greyscale => colormap/RGB
1244 DECLAREContigPutFunc(put2bitbwtile)
1246 uint32** BWmap = img->BWmap;
1252 UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
1259 * 4-bit greyscale => colormap/RGB
1261 DECLAREContigPutFunc(put4bitbwtile)
1263 uint32** BWmap = img->BWmap;
1269 UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
1276 * 8-bit packed samples, no Map => RGB
1278 DECLAREContigPutFunc(putRGBcontig8bittile)
1280 int samplesperpixel = img->samplesperpixel;
1283 fromskew *= samplesperpixel;
1286 *cp++ = PACK(pp[0], pp[1], pp[2]);
1287 pp += samplesperpixel);
1294 * 8-bit packed samples => RGBA w/ associated alpha
1295 * (known to have Map == NULL)
1297 DECLAREContigPutFunc(putRGBAAcontig8bittile)
1299 int samplesperpixel = img->samplesperpixel;
1302 fromskew *= samplesperpixel;
1305 *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
1306 pp += samplesperpixel);
1313 * 8-bit packed samples => RGBA w/ unassociated alpha
1314 * (known to have Map == NULL)
1316 DECLAREContigPutFunc(putRGBUAcontig8bittile)
1318 int samplesperpixel = img->samplesperpixel;
1320 fromskew *= samplesperpixel;
1324 for (x = w; x-- > 0;) {
1326 m = img->UaToAa+(a<<8);
1330 *cp++ = PACK4(r,g,b,a);
1331 pp += samplesperpixel;
1339 * 16-bit packed samples => RGB
1341 DECLAREContigPutFunc(putRGBcontig16bittile)
1343 int samplesperpixel = img->samplesperpixel;
1344 uint16 *wp = (uint16 *)pp;
1346 fromskew *= samplesperpixel;
1348 for (x = w; x-- > 0;) {
1349 *cp++ = PACK(img->Bitdepth16To8[wp[0]],
1350 img->Bitdepth16To8[wp[1]],
1351 img->Bitdepth16To8[wp[2]]);
1352 wp += samplesperpixel;
1360 * 16-bit packed samples => RGBA w/ associated alpha
1361 * (known to have Map == NULL)
1363 DECLAREContigPutFunc(putRGBAAcontig16bittile)
1365 int samplesperpixel = img->samplesperpixel;
1366 uint16 *wp = (uint16 *)pp;
1368 fromskew *= samplesperpixel;
1370 for (x = w; x-- > 0;) {
1371 *cp++ = PACK4(img->Bitdepth16To8[wp[0]],
1372 img->Bitdepth16To8[wp[1]],
1373 img->Bitdepth16To8[wp[2]],
1374 img->Bitdepth16To8[wp[3]]);
1375 wp += samplesperpixel;
1383 * 16-bit packed samples => RGBA w/ unassociated alpha
1384 * (known to have Map == NULL)
1386 DECLAREContigPutFunc(putRGBUAcontig16bittile)
1388 int samplesperpixel = img->samplesperpixel;
1389 uint16 *wp = (uint16 *)pp;
1391 fromskew *= samplesperpixel;
1395 for (x = w; x-- > 0;) {
1396 a = img->Bitdepth16To8[wp[3]];
1397 m = img->UaToAa+(a<<8);
1398 r = m[img->Bitdepth16To8[wp[0]]];
1399 g = m[img->Bitdepth16To8[wp[1]]];
1400 b = m[img->Bitdepth16To8[wp[2]]];
1401 *cp++ = PACK4(r,g,b,a);
1402 wp += samplesperpixel;
1410 * 8-bit packed CMYK samples w/o Map => RGB
1412 * NB: The conversion of CMYK->RGB is *very* crude.
1414 DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
1416 int samplesperpixel = img->samplesperpixel;
1420 fromskew *= samplesperpixel;
1424 r = (k*(255-pp[0]))/255;
1425 g = (k*(255-pp[1]))/255;
1426 b = (k*(255-pp[2]))/255;
1427 *cp++ = PACK(r, g, b);
1428 pp += samplesperpixel);
1435 * 8-bit packed CMYK samples w/Map => RGB
1437 * NB: The conversion of CMYK->RGB is *very* crude.
1439 DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
1441 int samplesperpixel = img->samplesperpixel;
1442 TIFFRGBValue* Map = img->Map;
1446 fromskew *= samplesperpixel;
1448 for (x = w; x-- > 0;) {
1450 r = (k*(255-pp[0]))/255;
1451 g = (k*(255-pp[1]))/255;
1452 b = (k*(255-pp[2]))/255;
1453 *cp++ = PACK(Map[r], Map[g], Map[b]);
1454 pp += samplesperpixel;
1461 #define DECLARESepPutFunc(name) \
1463 TIFFRGBAImage* img,\
1465 uint32 x, uint32 y, \
1466 uint32 w, uint32 h,\
1467 int32 fromskew, int32 toskew,\
1468 unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a\
1472 * 8-bit unpacked samples => RGB
1474 DECLARESepPutFunc(putRGBseparate8bittile)
1476 (void) img; (void) x; (void) y; (void) a;
1478 UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
1479 SKEW(r, g, b, fromskew);
1485 * 8-bit unpacked samples => RGBA w/ associated alpha
1487 DECLARESepPutFunc(putRGBAAseparate8bittile)
1489 (void) img; (void) x; (void) y;
1491 UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
1492 SKEW4(r, g, b, a, fromskew);
1498 * 8-bit unpacked samples => RGBA w/ unassociated alpha
1500 DECLARESepPutFunc(putRGBUAseparate8bittile)
1502 (void) img; (void) y;
1504 uint32 rv, gv, bv, av;
1506 for (x = w; x-- > 0;) {
1508 m = img->UaToAa+(av<<8);
1512 *cp++ = PACK4(rv,gv,bv,av);
1514 SKEW4(r, g, b, a, fromskew);
1520 * 16-bit unpacked samples => RGB
1522 DECLARESepPutFunc(putRGBseparate16bittile)
1524 uint16 *wr = (uint16*) r;
1525 uint16 *wg = (uint16*) g;
1526 uint16 *wb = (uint16*) b;
1527 (void) img; (void) y; (void) a;
1529 for (x = 0; x < w; x++)
1530 *cp++ = PACK(img->Bitdepth16To8[*wr++],
1531 img->Bitdepth16To8[*wg++],
1532 img->Bitdepth16To8[*wb++]);
1533 SKEW(wr, wg, wb, fromskew);
1539 * 16-bit unpacked samples => RGBA w/ associated alpha
1541 DECLARESepPutFunc(putRGBAAseparate16bittile)
1543 uint16 *wr = (uint16*) r;
1544 uint16 *wg = (uint16*) g;
1545 uint16 *wb = (uint16*) b;
1546 uint16 *wa = (uint16*) a;
1547 (void) img; (void) y;
1549 for (x = 0; x < w; x++)
1550 *cp++ = PACK4(img->Bitdepth16To8[*wr++],
1551 img->Bitdepth16To8[*wg++],
1552 img->Bitdepth16To8[*wb++],
1553 img->Bitdepth16To8[*wa++]);
1554 SKEW4(wr, wg, wb, wa, fromskew);
1560 * 16-bit unpacked samples => RGBA w/ unassociated alpha
1562 DECLARESepPutFunc(putRGBUAseparate16bittile)
1564 uint16 *wr = (uint16*) r;
1565 uint16 *wg = (uint16*) g;
1566 uint16 *wb = (uint16*) b;
1567 uint16 *wa = (uint16*) a;
1568 (void) img; (void) y;
1572 for (x = w; x-- > 0;) {
1573 a = img->Bitdepth16To8[*wa++];
1574 m = img->UaToAa+(a<<8);
1575 r = m[img->Bitdepth16To8[*wr++]];
1576 g = m[img->Bitdepth16To8[*wg++]];
1577 b = m[img->Bitdepth16To8[*wb++]];
1578 *cp++ = PACK4(r,g,b,a);
1580 SKEW4(wr, wg, wb, wa, fromskew);
1586 * 8-bit packed CIE L*a*b 1976 samples => RGB
1588 DECLAREContigPutFunc(putcontig8bitCIELab)
1595 for (x = w; x-- > 0;) {
1596 TIFFCIELabToXYZ(img->cielab,
1597 (unsigned char)pp[0],
1601 TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
1602 *cp++ = PACK(r, g, b);
1611 * YCbCr -> RGB conversion and packing routines.
1614 #define YCbCrtoRGB(dst, Y) { \
1616 TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b); \
1617 dst = PACK(r, g, b); \
1621 * 8-bit packed YCbCr samples => RGB
1622 * This function is generic for different sampling sizes,
1623 * and can handle blocks sizes that aren't multiples of the
1624 * sampling size. However, it is substantially less optimized
1625 * than the specific sampling cases. It is used as a fallback
1626 * for difficult blocks.
1629 static void putcontig8bitYCbCrGenericTile(
1634 int32 fromskew, int32 toskew,
1640 uint32* cp1 = cp+w+toskew;
1641 uint32* cp2 = cp1+w+toskew;
1642 uint32* cp3 = cp2+w+toskew;
1643 int32 incr = 3*w+4*toskew;
1645 int group_size = v_group * h_group + 2;
1648 fromskew = (fromskew * group_size) / h_group;
1650 for( yy = 0; yy < h; yy++ )
1652 unsigned char *pp_line;
1653 int y_line_group = yy / v_group;
1654 int y_remainder = yy - y_line_group * v_group;
1656 pp_line = pp + v_line_group *
1659 for( xx = 0; xx < w; xx++ )
1664 for (; h >= 4; h -= 4) {
1670 YCbCrtoRGB(cp [0], pp[ 0]);
1671 YCbCrtoRGB(cp [1], pp[ 1]);
1672 YCbCrtoRGB(cp [2], pp[ 2]);
1673 YCbCrtoRGB(cp [3], pp[ 3]);
1674 YCbCrtoRGB(cp1[0], pp[ 4]);
1675 YCbCrtoRGB(cp1[1], pp[ 5]);
1676 YCbCrtoRGB(cp1[2], pp[ 6]);
1677 YCbCrtoRGB(cp1[3], pp[ 7]);
1678 YCbCrtoRGB(cp2[0], pp[ 8]);
1679 YCbCrtoRGB(cp2[1], pp[ 9]);
1680 YCbCrtoRGB(cp2[2], pp[10]);
1681 YCbCrtoRGB(cp2[3], pp[11]);
1682 YCbCrtoRGB(cp3[0], pp[12]);
1683 YCbCrtoRGB(cp3[1], pp[13]);
1684 YCbCrtoRGB(cp3[2], pp[14]);
1685 YCbCrtoRGB(cp3[3], pp[15]);
1687 cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
1690 cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
1697 * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
1699 DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
1701 uint32* cp1 = cp+w+toskew;
1702 uint32* cp2 = cp1+w+toskew;
1703 uint32* cp3 = cp2+w+toskew;
1704 int32 incr = 3*w+4*toskew;
1707 /* adjust fromskew */
1708 fromskew = (fromskew * 18) / 4;
1709 if ((h & 3) == 0 && (w & 3) == 0) {
1710 for (; h >= 4; h -= 4) {
1716 YCbCrtoRGB(cp [0], pp[ 0]);
1717 YCbCrtoRGB(cp [1], pp[ 1]);
1718 YCbCrtoRGB(cp [2], pp[ 2]);
1719 YCbCrtoRGB(cp [3], pp[ 3]);
1720 YCbCrtoRGB(cp1[0], pp[ 4]);
1721 YCbCrtoRGB(cp1[1], pp[ 5]);
1722 YCbCrtoRGB(cp1[2], pp[ 6]);
1723 YCbCrtoRGB(cp1[3], pp[ 7]);
1724 YCbCrtoRGB(cp2[0], pp[ 8]);
1725 YCbCrtoRGB(cp2[1], pp[ 9]);
1726 YCbCrtoRGB(cp2[2], pp[10]);
1727 YCbCrtoRGB(cp2[3], pp[11]);
1728 YCbCrtoRGB(cp3[0], pp[12]);
1729 YCbCrtoRGB(cp3[1], pp[13]);
1730 YCbCrtoRGB(cp3[2], pp[14]);
1731 YCbCrtoRGB(cp3[3], pp[15]);
1733 cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
1736 cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
1741 for (x = w; x > 0;) {
1747 default: YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
1748 case 3: YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
1749 case 2: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
1750 case 1: YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
1754 default: YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
1755 case 3: YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
1756 case 2: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
1757 case 1: YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
1761 default: YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
1762 case 3: YCbCrtoRGB(cp2[1], pp[ 9]); /* FALLTHROUGH */
1763 case 2: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
1764 case 1: YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
1768 default: YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
1769 case 3: YCbCrtoRGB(cp2[0], pp[ 8]); /* FALLTHROUGH */
1770 case 2: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
1771 case 1: YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
1775 cp += x; cp1 += x; cp2 += x; cp3 += x;
1779 cp += 4; cp1 += 4; cp2 += 4; cp3 += 4;
1787 cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
1794 * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
1796 DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
1798 uint32* cp1 = cp+w+toskew;
1799 int32 incr = 2*toskew+w;
1802 fromskew = (fromskew * 10) / 4;
1803 if ((h & 3) == 0 && (w & 1) == 0) {
1804 for (; h >= 2; h -= 2) {
1810 YCbCrtoRGB(cp [0], pp[0]);
1811 YCbCrtoRGB(cp [1], pp[1]);
1812 YCbCrtoRGB(cp [2], pp[2]);
1813 YCbCrtoRGB(cp [3], pp[3]);
1814 YCbCrtoRGB(cp1[0], pp[4]);
1815 YCbCrtoRGB(cp1[1], pp[5]);
1816 YCbCrtoRGB(cp1[2], pp[6]);
1817 YCbCrtoRGB(cp1[3], pp[7]);
1822 cp += incr, cp1 += incr;
1827 for (x = w; x > 0;) {
1833 default: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
1834 case 1: YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
1838 default: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
1839 case 1: YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
1843 default: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
1844 case 1: YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
1848 default: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
1849 case 1: YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
1865 cp += incr, cp1 += incr;
1872 * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
1874 DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
1877 /* XXX adjust fromskew */
1884 YCbCrtoRGB(cp [0], pp[0]);
1885 YCbCrtoRGB(cp [1], pp[1]);
1886 YCbCrtoRGB(cp [2], pp[2]);
1887 YCbCrtoRGB(cp [3], pp[3]);
1899 case 3: YCbCrtoRGB(cp [2], pp[2]);
1900 case 2: YCbCrtoRGB(cp [1], pp[1]);
1901 case 1: YCbCrtoRGB(cp [0], pp[0]);
1916 * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
1918 DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
1921 int32 incr = 2*toskew+w;
1923 fromskew = (fromskew / 2) * 6;
1930 YCbCrtoRGB(cp[0], pp[0]);
1931 YCbCrtoRGB(cp[1], pp[1]);
1932 YCbCrtoRGB(cp2[0], pp[2]);
1933 YCbCrtoRGB(cp2[1], pp[3]);
1942 YCbCrtoRGB(cp[0], pp[0]);
1943 YCbCrtoRGB(cp2[0], pp[2]);
1958 YCbCrtoRGB(cp[0], pp[0]);
1959 YCbCrtoRGB(cp[1], pp[1]);
1968 YCbCrtoRGB(cp[0], pp[0]);
1974 * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
1976 DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
1979 fromskew = (fromskew * 4) / 2;
1986 YCbCrtoRGB(cp[0], pp[0]);
1987 YCbCrtoRGB(cp[1], pp[1]);
1998 YCbCrtoRGB(cp[0], pp[0]);
2010 * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
2012 DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
2015 int32 incr = 2*toskew+w;
2017 fromskew = (fromskew / 2) * 4;
2024 YCbCrtoRGB(cp[0], pp[0]);
2025 YCbCrtoRGB(cp2[0], pp[1]);
2040 YCbCrtoRGB(cp[0], pp[0]);
2048 * 8-bit packed YCbCr samples w/ no subsampling => RGB
2050 DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
2055 x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
2060 YCbCrtoRGB(*cp++, pp[0]);
2070 * 8-bit packed YCbCr samples w/ no subsampling => RGB
2072 DECLARESepPutFunc(putseparate8bitYCbCr11tile)
2076 /* TODO: naming of input vars is still off, change obfuscating declaration inside define, or resolve obfuscation */
2081 TIFFYCbCrtoRGB(img->ycbcr,*r++,*g++,*b++,&dr,&dg,&db);
2082 *cp++ = PACK(dr,dg,db);
2084 SKEW(r, g, b, fromskew);
2091 initYCbCrConversion(TIFFRGBAImage* img)
2093 static const char module[] = "initYCbCrConversion";
2095 float *luma, *refBlackWhite;
2097 if (img->ycbcr == NULL) {
2098 img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(
2099 TIFFroundup_32(sizeof (TIFFYCbCrToRGB), sizeof (long))
2100 + 4*256*sizeof (TIFFRGBValue)
2101 + 2*256*sizeof (int)
2102 + 3*256*sizeof (int32)
2104 if (img->ycbcr == NULL) {
2105 TIFFErrorExt(img->tif->tif_clientdata, module,
2106 "No space for YCbCr->RGB conversion state");
2111 TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
2112 TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
2114 if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
2119 static tileContigRoutine
2120 initCIELabConversion(TIFFRGBAImage* img)
2122 static const char module[] = "initCIELabConversion";
2128 img->cielab = (TIFFCIELabToRGB *)
2129 _TIFFmalloc(sizeof(TIFFCIELabToRGB));
2131 TIFFErrorExt(img->tif->tif_clientdata, module,
2132 "No space for CIE L*a*b*->RGB conversion state.");
2137 TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
2138 refWhite[1] = 100.0F;
2139 refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
2140 refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1])
2141 / whitePoint[1] * refWhite[1];
2142 if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0) {
2143 TIFFErrorExt(img->tif->tif_clientdata, module,
2144 "Failed to initialize CIE L*a*b*->RGB conversion state.");
2145 _TIFFfree(img->cielab);
2149 return putcontig8bitCIELab;
2153 * Greyscale images with less than 8 bits/sample are handled
2154 * with a table to avoid lots of shifts and masks. The table
2155 * is setup so that put*bwtile (below) can retrieve 8/bitspersample
2156 * pixel values simply by indexing into the table with one
2160 makebwmap(TIFFRGBAImage* img)
2162 TIFFRGBValue* Map = img->Map;
2163 int bitspersample = img->bitspersample;
2164 int nsamples = 8 / bitspersample;
2171 img->BWmap = (uint32**) _TIFFmalloc(
2172 256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
2173 if (img->BWmap == NULL) {
2174 TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for B&W mapping table");
2177 p = (uint32*)(img->BWmap + 256);
2178 for (i = 0; i < 256; i++) {
2181 switch (bitspersample) {
2182 #define GREY(x) c = Map[x]; *p++ = PACK(c,c,c);
2214 * Construct a mapping table to convert from the range
2215 * of the data samples to [0,255] --for display. This
2216 * process also handles inverting B&W images when needed.
2219 setupMap(TIFFRGBAImage* img)
2223 range = (int32)((1L<<img->bitspersample)-1);
2225 /* treat 16 bit the same as eight bit */
2226 if( img->bitspersample == 16 )
2227 range = (int32) 255;
2229 img->Map = (TIFFRGBValue*) _TIFFmalloc((range+1) * sizeof (TIFFRGBValue));
2230 if (img->Map == NULL) {
2231 TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
2232 "No space for photometric conversion table");
2235 if (img->photometric == PHOTOMETRIC_MINISWHITE) {
2236 for (x = 0; x <= range; x++)
2237 img->Map[x] = (TIFFRGBValue) (((range - x) * 255) / range);
2239 for (x = 0; x <= range; x++)
2240 img->Map[x] = (TIFFRGBValue) ((x * 255) / range);
2242 if (img->bitspersample <= 16 &&
2243 (img->photometric == PHOTOMETRIC_MINISBLACK ||
2244 img->photometric == PHOTOMETRIC_MINISWHITE)) {
2246 * Use photometric mapping table to construct
2247 * unpacking tables for samples <= 8 bits.
2249 if (!makebwmap(img))
2251 /* no longer need Map, free it */
2252 _TIFFfree(img->Map), img->Map = NULL;
2258 checkcmap(TIFFRGBAImage* img)
2260 uint16* r = img->redcmap;
2261 uint16* g = img->greencmap;
2262 uint16* b = img->bluecmap;
2263 long n = 1L<<img->bitspersample;
2266 if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
2272 cvtcmap(TIFFRGBAImage* img)
2274 uint16* r = img->redcmap;
2275 uint16* g = img->greencmap;
2276 uint16* b = img->bluecmap;
2279 for (i = (1L<<img->bitspersample)-1; i >= 0; i--) {
2280 #define CVT(x) ((uint16)((x)>>8))
2289 * Palette images with <= 8 bits/sample are handled
2290 * with a table to avoid lots of shifts and masks. The table
2291 * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
2292 * pixel values simply by indexing into the table with one
2296 makecmap(TIFFRGBAImage* img)
2298 int bitspersample = img->bitspersample;
2299 int nsamples = 8 / bitspersample;
2300 uint16* r = img->redcmap;
2301 uint16* g = img->greencmap;
2302 uint16* b = img->bluecmap;
2306 img->PALmap = (uint32**) _TIFFmalloc(
2307 256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
2308 if (img->PALmap == NULL) {
2309 TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for Palette mapping table");
2312 p = (uint32*)(img->PALmap + 256);
2313 for (i = 0; i < 256; i++) {
2316 #define CMAP(x) c = (TIFFRGBValue) x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff);
2317 switch (bitspersample) {
2348 * Construct any mapping table used
2349 * by the associated put routine.
2352 buildMap(TIFFRGBAImage* img)
2354 switch (img->photometric) {
2355 case PHOTOMETRIC_RGB:
2356 case PHOTOMETRIC_YCBCR:
2357 case PHOTOMETRIC_SEPARATED:
2358 if (img->bitspersample == 8)
2361 case PHOTOMETRIC_MINISBLACK:
2362 case PHOTOMETRIC_MINISWHITE:
2366 case PHOTOMETRIC_PALETTE:
2368 * Convert 16-bit colormap to 8-bit (unless it looks
2369 * like an old-style 8-bit colormap).
2371 if (checkcmap(img) == 16)
2374 TIFFWarningExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "Assuming 8-bit colormap");
2376 * Use mapping table and colormap to construct
2377 * unpacking tables for samples < 8 bits.
2379 if (img->bitspersample <= 8 && !makecmap(img))
2387 * Select the appropriate conversion routine for packed data.
2390 PickContigCase(TIFFRGBAImage* img)
2392 img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
2393 img->put.contig = NULL;
2394 switch (img->photometric) {
2395 case PHOTOMETRIC_RGB:
2396 switch (img->bitspersample) {
2398 if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
2399 img->put.contig = putRGBAAcontig8bittile;
2400 else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
2402 if (BuildMapUaToAa(img))
2403 img->put.contig = putRGBUAcontig8bittile;
2406 img->put.contig = putRGBcontig8bittile;
2409 if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
2411 if (BuildMapBitdepth16To8(img))
2412 img->put.contig = putRGBAAcontig16bittile;
2414 else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
2416 if (BuildMapBitdepth16To8(img) &&
2417 BuildMapUaToAa(img))
2418 img->put.contig = putRGBUAcontig16bittile;
2422 if (BuildMapBitdepth16To8(img))
2423 img->put.contig = putRGBcontig16bittile;
2428 case PHOTOMETRIC_SEPARATED:
2429 if (buildMap(img)) {
2430 if (img->bitspersample == 8) {
2432 img->put.contig = putRGBcontig8bitCMYKtile;
2434 img->put.contig = putRGBcontig8bitCMYKMaptile;
2438 case PHOTOMETRIC_PALETTE:
2439 if (buildMap(img)) {
2440 switch (img->bitspersample) {
2442 img->put.contig = put8bitcmaptile;
2445 img->put.contig = put4bitcmaptile;
2448 img->put.contig = put2bitcmaptile;
2451 img->put.contig = put1bitcmaptile;
2456 case PHOTOMETRIC_MINISWHITE:
2457 case PHOTOMETRIC_MINISBLACK:
2458 if (buildMap(img)) {
2459 switch (img->bitspersample) {
2461 img->put.contig = put16bitbwtile;
2464 img->put.contig = putgreytile;
2467 img->put.contig = put4bitbwtile;
2470 img->put.contig = put2bitbwtile;
2473 img->put.contig = put1bitbwtile;
2478 case PHOTOMETRIC_YCBCR:
2479 if ((img->bitspersample==8) && (img->samplesperpixel==3))
2481 if (initYCbCrConversion(img)!=0)
2484 * The 6.0 spec says that subsampling must be
2485 * one of 1, 2, or 4, and that vertical subsampling
2486 * must always be <= horizontal subsampling; so
2487 * there are only a few possibilities and we just
2488 * enumerate the cases.
2489 * Joris: added support for the [1,2] case, nonetheless, to accomodate
2492 uint16 SubsamplingHor;
2493 uint16 SubsamplingVer;
2494 TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &SubsamplingHor, &SubsamplingVer);
2495 switch ((SubsamplingHor<<4)|SubsamplingVer) {
2497 img->put.contig = putcontig8bitYCbCr44tile;
2500 img->put.contig = putcontig8bitYCbCr42tile;
2503 img->put.contig = putcontig8bitYCbCr41tile;
2506 img->put.contig = putcontig8bitYCbCr22tile;
2509 img->put.contig = putcontig8bitYCbCr21tile;
2512 img->put.contig = putcontig8bitYCbCr12tile;
2515 img->put.contig = putcontig8bitYCbCr11tile;
2521 case PHOTOMETRIC_CIELAB:
2522 if (buildMap(img)) {
2523 if (img->bitspersample == 8)
2524 img->put.contig = initCIELabConversion(img);
2528 return ((img->get!=NULL) && (img->put.contig!=NULL));
2532 * Select the appropriate conversion routine for unpacked data.
2534 * NB: we assume that unpacked single channel data is directed
2535 * to the "packed routines.
2538 PickSeparateCase(TIFFRGBAImage* img)
2540 img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
2541 img->put.separate = NULL;
2542 switch (img->photometric) {
2543 case PHOTOMETRIC_MINISWHITE:
2544 case PHOTOMETRIC_MINISBLACK:
2545 /* greyscale images processed pretty much as RGB by gtTileSeparate */
2546 case PHOTOMETRIC_RGB:
2547 switch (img->bitspersample) {
2549 if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
2550 img->put.separate = putRGBAAseparate8bittile;
2551 else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
2553 if (BuildMapUaToAa(img))
2554 img->put.separate = putRGBUAseparate8bittile;
2557 img->put.separate = putRGBseparate8bittile;
2560 if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
2562 if (BuildMapBitdepth16To8(img))
2563 img->put.separate = putRGBAAseparate16bittile;
2565 else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
2567 if (BuildMapBitdepth16To8(img) &&
2568 BuildMapUaToAa(img))
2569 img->put.separate = putRGBUAseparate16bittile;
2573 if (BuildMapBitdepth16To8(img))
2574 img->put.separate = putRGBseparate16bittile;
2579 case PHOTOMETRIC_YCBCR:
2580 if ((img->bitspersample==8) && (img->samplesperpixel==3))
2582 if (initYCbCrConversion(img)!=0)
2585 TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
2586 switch ((hs<<4)|vs) {
2588 img->put.separate = putseparate8bitYCbCr11tile;
2590 /* TODO: add other cases here */
2596 return ((img->get!=NULL) && (img->put.separate!=NULL));
2600 BuildMapUaToAa(TIFFRGBAImage* img)
2602 static const char module[]="BuildMapUaToAa";
2605 assert(img->UaToAa==NULL);
2606 img->UaToAa=_TIFFmalloc(65536);
2607 if (img->UaToAa==NULL)
2609 TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
2613 for (na=0; na<256; na++)
2615 for (nv=0; nv<256; nv++)
2616 *m++=(nv*na+127)/255;
2622 BuildMapBitdepth16To8(TIFFRGBAImage* img)
2624 static const char module[]="BuildMapBitdepth16To8";
2627 assert(img->Bitdepth16To8==NULL);
2628 img->Bitdepth16To8=_TIFFmalloc(65536);
2629 if (img->Bitdepth16To8==NULL)
2631 TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
2634 m=img->Bitdepth16To8;
2635 for (n=0; n<65536; n++)
2642 * Read a whole strip off data from the file, and convert to RGBA form.
2643 * If this is the last strip, then it will only contain the portion of
2644 * the strip that is actually within the image space. The result is
2645 * organized in bottom to top form.
2650 TIFFReadRGBAStrip(TIFF* tif, uint32 row, uint32 * raster )
2653 char emsg[1024] = "";
2656 uint32 rowsperstrip, rows_to_read;
2658 if( TIFFIsTiled( tif ) )
2660 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
2661 "Can't use TIFFReadRGBAStrip() with tiled file.");
2665 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
2666 if( (row % rowsperstrip) != 0 )
2668 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
2669 "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
2673 if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
2675 img.row_offset = row;
2678 if( row + rowsperstrip > img.height )
2679 rows_to_read = img.height - row;
2681 rows_to_read = rowsperstrip;
2683 ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read );
2685 TIFFRGBAImageEnd(&img);
2687 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
2695 * Read a whole tile off data from the file, and convert to RGBA form.
2696 * The returned RGBA data is organized from bottom to top of tile,
2697 * and may include zeroed areas if the tile extends off the image.
2701 TIFFReadRGBATile(TIFF* tif, uint32 col, uint32 row, uint32 * raster)
2704 char emsg[1024] = "";
2707 uint32 tile_xsize, tile_ysize;
2708 uint32 read_xsize, read_ysize;
2712 * Verify that our request is legal - on a tile file, and on a
2716 if( !TIFFIsTiled( tif ) )
2718 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
2719 "Can't use TIFFReadRGBATile() with stripped file.");
2723 TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
2724 TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
2725 if( (col % tile_xsize) != 0 || (row % tile_ysize) != 0 )
2727 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
2728 "Row/col passed to TIFFReadRGBATile() must be top"
2729 "left corner of a tile.");
2734 * Setup the RGBA reader.
2737 if (!TIFFRGBAImageOK(tif, emsg)
2738 || !TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
2739 TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
2744 * The TIFFRGBAImageGet() function doesn't allow us to get off the
2745 * edge of the image, even to fill an otherwise valid tile. So we
2746 * figure out how much we can read, and fix up the tile buffer to
2747 * a full tile configuration afterwards.
2750 if( row + tile_ysize > img.height )
2751 read_ysize = img.height - row;
2753 read_ysize = tile_ysize;
2755 if( col + tile_xsize > img.width )
2756 read_xsize = img.width - col;
2758 read_xsize = tile_xsize;
2761 * Read the chunk of imagery.
2764 img.row_offset = row;
2765 img.col_offset = col;
2767 ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize );
2769 TIFFRGBAImageEnd(&img);
2772 * If our read was incomplete we will need to fix up the tile by
2773 * shifting the data around as if a full tile of data is being returned.
2775 * This is all the more complicated because the image is organized in
2776 * bottom to top format.
2779 if( read_xsize == tile_xsize && read_ysize == tile_ysize )
2782 for( i_row = 0; i_row < read_ysize; i_row++ ) {
2783 memmove( raster + (tile_ysize - i_row - 1) * tile_xsize,
2784 raster + (read_ysize - i_row - 1) * read_xsize,
2785 read_xsize * sizeof(uint32) );
2786 _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize+read_xsize,
2787 0, sizeof(uint32) * (tile_xsize - read_xsize) );
2790 for( i_row = read_ysize; i_row < tile_ysize; i_row++ ) {
2791 _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize,
2792 0, sizeof(uint32) * tile_xsize );
2798 /* vim: set ts=8 sts=8 sw=8 noet: */