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
;
696 int32 fromskew
, toskew
;
697 int alpha
= img
->alpha
;
702 tilesize
= TIFFTileSize(tif
);
703 bufsize
= TIFFSafeMultiply(tmsize_t
,alpha
?4:3,tilesize
);
705 TIFFErrorExt(tif
->tif_clientdata
, TIFFFileName(tif
), "Integer overflow in %s", "gtTileSeparate");
708 buf
= (unsigned char*) _TIFFmalloc(bufsize
);
710 TIFFErrorExt(tif
->tif_clientdata
, TIFFFileName(tif
), "%s", "No space for tile buffer");
713 _TIFFmemset(buf
, 0, bufsize
);
717 pa
= (alpha
?(p2
+tilesize
):NULL
);
718 TIFFGetField(tif
, TIFFTAG_TILEWIDTH
, &tw
);
719 TIFFGetField(tif
, TIFFTAG_TILELENGTH
, &th
);
721 flip
= setorientation(img
);
722 if (flip
& FLIP_VERTICALLY
) {
724 toskew
= -(int32
)(tw
+ w
);
728 toskew
= -(int32
)(tw
- w
);
731 switch( img
->photometric
)
733 case PHOTOMETRIC_MINISWHITE
:
734 case PHOTOMETRIC_MINISBLACK
:
735 case PHOTOMETRIC_PALETTE
:
745 for (row
= 0; row
< h
; row
+= nrow
)
747 rowstoread
= th
- (row
+ img
->row_offset
) % th
;
748 nrow
= (row
+ rowstoread
> h
? h
- row
: rowstoread
);
749 for (col
= 0; col
< w
; col
+= tw
)
751 if (TIFFReadTile(tif
, p0
, col
+img
->col_offset
,
752 row
+img
->row_offset
,0,0)==(tmsize_t
)(-1) && img
->stoponerr
)
757 if (colorchannels
> 1
758 && TIFFReadTile(tif
, p1
, col
+img
->col_offset
,
759 row
+img
->row_offset
,0,1) == (tmsize_t
)(-1)
765 if (colorchannels
> 1
766 && TIFFReadTile(tif
, p2
, col
+img
->col_offset
,
767 row
+img
->row_offset
,0,2) == (tmsize_t
)(-1)
774 && TIFFReadTile(tif
,pa
,col
+img
->col_offset
,
775 row
+img
->row_offset
,0,colorchannels
) == (tmsize_t
)(-1)
782 pos
= ((row
+img
->row_offset
) % th
) * TIFFTileRowSize(tif
);
787 * Tile is clipped horizontally. Calculate
788 * visible portion and skewing factors.
790 uint32 npix
= w
- col
;
791 fromskew
= tw
- npix
;
792 (*put
)(img
, raster
+y
*w
+col
, col
, y
,
793 npix
, nrow
, fromskew
, toskew
+ fromskew
,
794 p0
+ pos
, p1
+ pos
, p2
+ pos
, (alpha
?(pa
+pos
):NULL
));
796 (*put
)(img
, raster
+y
*w
+col
, col
, y
,
797 tw
, nrow
, 0, toskew
, p0
+ pos
, p1
+ pos
, p2
+ pos
, (alpha
?(pa
+pos
):NULL
));
801 y
+= (flip
& FLIP_VERTICALLY
?-(int32
) nrow
: (int32
) nrow
);
804 if (flip
& FLIP_HORIZONTALLY
) {
807 for (line
= 0; line
< h
; line
++) {
808 uint32
*left
= raster
+ (line
* w
);
809 uint32
*right
= left
+ w
- 1;
811 while ( left
< right
) {
825 * Get a strip-organized image that has
826 * PlanarConfiguration contiguous if SamplesPerPixel > 1
828 * SamplesPerPixel == 1
831 gtStripContig(TIFFRGBAImage
* img
, uint32
* raster
, uint32 w
, uint32 h
)
833 TIFF
* tif
= img
->tif
;
834 tileContigRoutine put
= img
->put
.contig
;
835 uint32 row
, y
, nrow
, nrowsub
, rowstoread
;
839 uint16 subsamplinghor
,subsamplingver
;
840 uint32 imagewidth
= img
->width
;
842 int32 fromskew
, toskew
;
845 buf
= (unsigned char*) _TIFFmalloc(TIFFStripSize(tif
));
847 TIFFErrorExt(tif
->tif_clientdata
, TIFFFileName(tif
), "No space for strip buffer");
850 _TIFFmemset(buf
, 0, TIFFStripSize(tif
));
852 flip
= setorientation(img
);
853 if (flip
& FLIP_VERTICALLY
) {
855 toskew
= -(int32
)(w
+ w
);
858 toskew
= -(int32
)(w
- w
);
861 TIFFGetFieldDefaulted(tif
, TIFFTAG_ROWSPERSTRIP
, &rowsperstrip
);
862 TIFFGetFieldDefaulted(tif
, TIFFTAG_YCBCRSUBSAMPLING
, &subsamplinghor
, &subsamplingver
);
863 scanline
= TIFFScanlineSize(tif
);
864 fromskew
= (w
< imagewidth
? imagewidth
- w
: 0);
865 for (row
= 0; row
< h
; row
+= nrow
)
867 rowstoread
= rowsperstrip
- (row
+ img
->row_offset
) % rowsperstrip
;
868 nrow
= (row
+ rowstoread
> h
? h
- row
: rowstoread
);
870 if ((nrowsub%subsamplingver
)!=0)
871 nrowsub
+=subsamplingver
-nrowsub%subsamplingver
;
872 if (TIFFReadEncodedStrip(tif
,
873 TIFFComputeStrip(tif
,row
+img
->row_offset
, 0),
875 ((row
+ img
->row_offset
)%rowsperstrip
+ nrowsub
) * scanline
)==(tmsize_t
)(-1)
882 pos
= ((row
+ img
->row_offset
) % rowsperstrip
) * scanline
;
883 (*put
)(img
, raster
+y
*w
, 0, y
, w
, nrow
, fromskew
, toskew
, buf
+ pos
);
884 y
+= (flip
& FLIP_VERTICALLY
? -(int32
) nrow
: (int32
) nrow
);
887 if (flip
& FLIP_HORIZONTALLY
) {
890 for (line
= 0; line
< h
; line
++) {
891 uint32
*left
= raster
+ (line
* w
);
892 uint32
*right
= left
+ w
- 1;
894 while ( left
< right
) {
908 * Get a strip-organized image with
909 * SamplesPerPixel > 1
910 * PlanarConfiguration separated
911 * We assume that all such images are RGB.
914 gtStripSeparate(TIFFRGBAImage
* img
, uint32
* raster
, uint32 w
, uint32 h
)
916 TIFF
* tif
= img
->tif
;
917 tileSeparateRoutine put
= img
->put
.separate
;
919 unsigned char *p0
, *p1
, *p2
, *pa
;
920 uint32 row
, y
, nrow
, rowstoread
;
923 uint32 rowsperstrip
, offset_row
;
924 uint32 imagewidth
= img
->width
;
927 int32 fromskew
, toskew
;
928 int alpha
= img
->alpha
;
929 int ret
= 1, flip
, colorchannels
;
931 stripsize
= TIFFStripSize(tif
);
932 bufsize
= TIFFSafeMultiply(tmsize_t
,alpha
?4:3,stripsize
);
934 TIFFErrorExt(tif
->tif_clientdata
, TIFFFileName(tif
), "Integer overflow in %s", "gtStripSeparate");
937 p0
= buf
= (unsigned char *)_TIFFmalloc(bufsize
);
939 TIFFErrorExt(tif
->tif_clientdata
, TIFFFileName(tif
), "No space for tile buffer");
942 _TIFFmemset(buf
, 0, bufsize
);
945 pa
= (alpha
?(p2
+stripsize
):NULL
);
947 flip
= setorientation(img
);
948 if (flip
& FLIP_VERTICALLY
) {
950 toskew
= -(int32
)(w
+ w
);
954 toskew
= -(int32
)(w
- w
);
957 switch( img
->photometric
)
959 case PHOTOMETRIC_MINISWHITE
:
960 case PHOTOMETRIC_MINISBLACK
:
961 case PHOTOMETRIC_PALETTE
:
971 TIFFGetFieldDefaulted(tif
, TIFFTAG_ROWSPERSTRIP
, &rowsperstrip
);
972 scanline
= TIFFScanlineSize(tif
);
973 fromskew
= (w
< imagewidth
? imagewidth
- w
: 0);
974 for (row
= 0; row
< h
; row
+= nrow
)
976 rowstoread
= rowsperstrip
- (row
+ img
->row_offset
) % rowsperstrip
;
977 nrow
= (row
+ rowstoread
> h
? h
- row
: rowstoread
);
978 offset_row
= row
+ img
->row_offset
;
979 if (TIFFReadEncodedStrip(tif
, TIFFComputeStrip(tif
, offset_row
, 0),
980 p0
, ((row
+ img
->row_offset
)%rowsperstrip
+ nrow
) * scanline
)==(tmsize_t
)(-1)
986 if (colorchannels
> 1
987 && TIFFReadEncodedStrip(tif
, TIFFComputeStrip(tif
, offset_row
, 1),
988 p1
, ((row
+ img
->row_offset
)%rowsperstrip
+ nrow
) * scanline
) == (tmsize_t
)(-1)
994 if (colorchannels
> 1
995 && TIFFReadEncodedStrip(tif
, TIFFComputeStrip(tif
, offset_row
, 2),
996 p2
, ((row
+ img
->row_offset
)%rowsperstrip
+ nrow
) * scanline
) == (tmsize_t
)(-1)
1004 if (TIFFReadEncodedStrip(tif
, TIFFComputeStrip(tif
, offset_row
, colorchannels
),
1005 pa
, ((row
+ img
->row_offset
)%rowsperstrip
+ nrow
) * scanline
)==(tmsize_t
)(-1)
1013 pos
= ((row
+ img
->row_offset
) % rowsperstrip
) * scanline
;
1014 (*put
)(img
, raster
+y
*w
, 0, y
, w
, nrow
, fromskew
, toskew
, p0
+ pos
, p1
+ pos
,
1015 p2
+ pos
, (alpha
?(pa
+pos
):NULL
));
1016 y
+= (flip
& FLIP_VERTICALLY
? -(int32
) nrow
: (int32
) nrow
);
1019 if (flip
& FLIP_HORIZONTALLY
) {
1022 for (line
= 0; line
< h
; line
++) {
1023 uint32
*left
= raster
+ (line
* w
);
1024 uint32
*right
= left
+ w
- 1;
1026 while ( left
< right
) {
1027 uint32 temp
= *left
;
1040 * The following routines move decoded data returned
1041 * from the TIFF library into rasters filled with packed
1042 * ABGR pixels (i.e. suitable for passing to lrecwrite.)
1044 * The routines have been created according to the most
1045 * important cases and optimized. PickContigCase and
1046 * PickSeparateCase analyze the parameters and select
1047 * the appropriate "get" and "put" routine to use.
1049 #define REPEAT8(op) REPEAT4(op); REPEAT4(op)
1050 #define REPEAT4(op) REPEAT2(op); REPEAT2(op)
1051 #define REPEAT2(op) op; op
1052 #define CASE8(x,op) \
1054 case 7: op; case 6: op; case 5: op; \
1055 case 4: op; case 3: op; case 2: op; \
1058 #define CASE4(x,op) switch (x) { case 3: op; case 2: op; case 1: op; }
1061 #define UNROLL8(w, op1, op2) { \
1063 for (_x = w; _x >= 8; _x -= 8) { \
1072 #define UNROLL4(w, op1, op2) { \
1074 for (_x = w; _x >= 4; _x -= 4) { \
1083 #define UNROLL2(w, op1, op2) { \
1085 for (_x = w; _x >= 2; _x -= 2) { \
1095 #define SKEW(r,g,b,skew) { r += skew; g += skew; b += skew; }
1096 #define SKEW4(r,g,b,a,skew) { r += skew; g += skew; b += skew; a+= skew; }
1098 #define A1 (((uint32)0xffL)<<24)
1099 #define PACK(r,g,b) \
1100 ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|A1)
1101 #define PACK4(r,g,b,a) \
1102 ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|((uint32)(a)<<24))
1103 #define W2B(v) (((v)>>8)&0xff)
1104 /* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */
1105 #define PACKW(r,g,b) \
1106 ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|A1)
1107 #define PACKW4(r,g,b,a) \
1108 ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|((uint32)W2B(a)<<24))
1110 #define DECLAREContigPutFunc(name) \
1112 TIFFRGBAImage* img, \
1114 uint32 x, uint32 y, \
1115 uint32 w, uint32 h, \
1116 int32 fromskew, int32 toskew, \
1121 * 8-bit palette => colormap/RGB
1123 DECLAREContigPutFunc(put8bitcmaptile
)
1125 uint32
** PALmap
= img
->PALmap
;
1126 int samplesperpixel
= img
->samplesperpixel
;
1130 for (x
= w
; x
-- > 0;)
1132 *cp
++ = PALmap
[*pp
][0];
1133 pp
+= samplesperpixel
;
1141 * 4-bit palette => colormap/RGB
1143 DECLAREContigPutFunc(put4bitcmaptile
)
1145 uint32
** PALmap
= img
->PALmap
;
1151 UNROLL2(w
, bw
= PALmap
[*pp
++], *cp
++ = *bw
++);
1158 * 2-bit palette => colormap/RGB
1160 DECLAREContigPutFunc(put2bitcmaptile
)
1162 uint32
** PALmap
= img
->PALmap
;
1168 UNROLL4(w
, bw
= PALmap
[*pp
++], *cp
++ = *bw
++);
1175 * 1-bit palette => colormap/RGB
1177 DECLAREContigPutFunc(put1bitcmaptile
)
1179 uint32
** PALmap
= img
->PALmap
;
1185 UNROLL8(w
, bw
= PALmap
[*pp
++], *cp
++ = *bw
++);
1192 * 8-bit greyscale => colormap/RGB
1194 DECLAREContigPutFunc(putgreytile
)
1196 int samplesperpixel
= img
->samplesperpixel
;
1197 uint32
** BWmap
= img
->BWmap
;
1201 for (x
= w
; x
-- > 0;)
1203 *cp
++ = BWmap
[*pp
][0];
1204 pp
+= samplesperpixel
;
1212 * 8-bit greyscale with associated alpha => colormap/RGBA
1214 DECLAREContigPutFunc(putagreytile
)
1216 int samplesperpixel
= img
->samplesperpixel
;
1217 uint32
** BWmap
= img
->BWmap
;
1221 for (x
= w
; x
-- > 0;)
1223 *cp
++ = BWmap
[*pp
][0] & (*(pp
+1) << 24 | ~A1
);
1224 pp
+= samplesperpixel
;
1232 * 16-bit greyscale => colormap/RGB
1234 DECLAREContigPutFunc(put16bitbwtile
)
1236 int samplesperpixel
= img
->samplesperpixel
;
1237 uint32
** BWmap
= img
->BWmap
;
1241 uint16
*wp
= (uint16
*) pp
;
1243 for (x
= w
; x
-- > 0;)
1245 /* use high order byte of 16bit value */
1247 *cp
++ = BWmap
[*wp
>> 8][0];
1248 pp
+= 2 * samplesperpixel
;
1249 wp
+= samplesperpixel
;
1257 * 1-bit bilevel => colormap/RGB
1259 DECLAREContigPutFunc(put1bitbwtile
)
1261 uint32
** BWmap
= img
->BWmap
;
1267 UNROLL8(w
, bw
= BWmap
[*pp
++], *cp
++ = *bw
++);
1274 * 2-bit greyscale => colormap/RGB
1276 DECLAREContigPutFunc(put2bitbwtile
)
1278 uint32
** BWmap
= img
->BWmap
;
1284 UNROLL4(w
, bw
= BWmap
[*pp
++], *cp
++ = *bw
++);
1291 * 4-bit greyscale => colormap/RGB
1293 DECLAREContigPutFunc(put4bitbwtile
)
1295 uint32
** BWmap
= img
->BWmap
;
1301 UNROLL2(w
, bw
= BWmap
[*pp
++], *cp
++ = *bw
++);
1308 * 8-bit packed samples, no Map => RGB
1310 DECLAREContigPutFunc(putRGBcontig8bittile
)
1312 int samplesperpixel
= img
->samplesperpixel
;
1315 fromskew
*= samplesperpixel
;
1318 *cp
++ = PACK(pp
[0], pp
[1], pp
[2]);
1319 pp
+= samplesperpixel
);
1326 * 8-bit packed samples => RGBA w/ associated alpha
1327 * (known to have Map == NULL)
1329 DECLAREContigPutFunc(putRGBAAcontig8bittile
)
1331 int samplesperpixel
= img
->samplesperpixel
;
1334 fromskew
*= samplesperpixel
;
1337 *cp
++ = PACK4(pp
[0], pp
[1], pp
[2], pp
[3]);
1338 pp
+= samplesperpixel
);
1345 * 8-bit packed samples => RGBA w/ unassociated alpha
1346 * (known to have Map == NULL)
1348 DECLAREContigPutFunc(putRGBUAcontig8bittile
)
1350 int samplesperpixel
= img
->samplesperpixel
;
1352 fromskew
*= samplesperpixel
;
1356 for (x
= w
; x
-- > 0;) {
1358 m
= img
->UaToAa
+(a
<<8);
1362 *cp
++ = PACK4(r
,g
,b
,a
);
1363 pp
+= samplesperpixel
;
1371 * 16-bit packed samples => RGB
1373 DECLAREContigPutFunc(putRGBcontig16bittile
)
1375 int samplesperpixel
= img
->samplesperpixel
;
1376 uint16
*wp
= (uint16
*)pp
;
1378 fromskew
*= samplesperpixel
;
1380 for (x
= w
; x
-- > 0;) {
1381 *cp
++ = PACK(img
->Bitdepth16To8
[wp
[0]],
1382 img
->Bitdepth16To8
[wp
[1]],
1383 img
->Bitdepth16To8
[wp
[2]]);
1384 wp
+= samplesperpixel
;
1392 * 16-bit packed samples => RGBA w/ associated alpha
1393 * (known to have Map == NULL)
1395 DECLAREContigPutFunc(putRGBAAcontig16bittile
)
1397 int samplesperpixel
= img
->samplesperpixel
;
1398 uint16
*wp
= (uint16
*)pp
;
1400 fromskew
*= samplesperpixel
;
1402 for (x
= w
; x
-- > 0;) {
1403 *cp
++ = PACK4(img
->Bitdepth16To8
[wp
[0]],
1404 img
->Bitdepth16To8
[wp
[1]],
1405 img
->Bitdepth16To8
[wp
[2]],
1406 img
->Bitdepth16To8
[wp
[3]]);
1407 wp
+= samplesperpixel
;
1415 * 16-bit packed samples => RGBA w/ unassociated alpha
1416 * (known to have Map == NULL)
1418 DECLAREContigPutFunc(putRGBUAcontig16bittile
)
1420 int samplesperpixel
= img
->samplesperpixel
;
1421 uint16
*wp
= (uint16
*)pp
;
1423 fromskew
*= samplesperpixel
;
1427 for (x
= w
; x
-- > 0;) {
1428 a
= img
->Bitdepth16To8
[wp
[3]];
1429 m
= img
->UaToAa
+(a
<<8);
1430 r
= m
[img
->Bitdepth16To8
[wp
[0]]];
1431 g
= m
[img
->Bitdepth16To8
[wp
[1]]];
1432 b
= m
[img
->Bitdepth16To8
[wp
[2]]];
1433 *cp
++ = PACK4(r
,g
,b
,a
);
1434 wp
+= samplesperpixel
;
1442 * 8-bit packed CMYK samples w/o Map => RGB
1444 * NB: The conversion of CMYK->RGB is *very* crude.
1446 DECLAREContigPutFunc(putRGBcontig8bitCMYKtile
)
1448 int samplesperpixel
= img
->samplesperpixel
;
1452 fromskew
*= samplesperpixel
;
1456 r
= (k
*(255-pp
[0]))/255;
1457 g
= (k
*(255-pp
[1]))/255;
1458 b
= (k
*(255-pp
[2]))/255;
1459 *cp
++ = PACK(r
, g
, b
);
1460 pp
+= samplesperpixel
);
1467 * 8-bit packed CMYK samples w/Map => RGB
1469 * NB: The conversion of CMYK->RGB is *very* crude.
1471 DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile
)
1473 int samplesperpixel
= img
->samplesperpixel
;
1474 TIFFRGBValue
* Map
= img
->Map
;
1478 fromskew
*= samplesperpixel
;
1480 for (x
= w
; x
-- > 0;) {
1482 r
= (k
*(255-pp
[0]))/255;
1483 g
= (k
*(255-pp
[1]))/255;
1484 b
= (k
*(255-pp
[2]))/255;
1485 *cp
++ = PACK(Map
[r
], Map
[g
], Map
[b
]);
1486 pp
+= samplesperpixel
;
1493 #define DECLARESepPutFunc(name) \
1495 TIFFRGBAImage* img,\
1497 uint32 x, uint32 y, \
1498 uint32 w, uint32 h,\
1499 int32 fromskew, int32 toskew,\
1500 unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a\
1504 * 8-bit unpacked samples => RGB
1506 DECLARESepPutFunc(putRGBseparate8bittile
)
1508 (void) img
; (void) x
; (void) y
; (void) a
;
1510 UNROLL8(w
, NOP
, *cp
++ = PACK(*r
++, *g
++, *b
++));
1511 SKEW(r
, g
, b
, fromskew
);
1517 * 8-bit unpacked samples => RGBA w/ associated alpha
1519 DECLARESepPutFunc(putRGBAAseparate8bittile
)
1521 (void) img
; (void) x
; (void) y
;
1523 UNROLL8(w
, NOP
, *cp
++ = PACK4(*r
++, *g
++, *b
++, *a
++));
1524 SKEW4(r
, g
, b
, a
, fromskew
);
1530 * 8-bit unpacked CMYK samples => RGBA
1532 DECLARESepPutFunc(putCMYKseparate8bittile
)
1534 (void) img
; (void) y
;
1536 uint32 rv
, gv
, bv
, kv
;
1537 for (x
= w
; x
-- > 0;) {
1539 rv
= (kv
*(255-*r
++))/255;
1540 gv
= (kv
*(255-*g
++))/255;
1541 bv
= (kv
*(255-*b
++))/255;
1542 *cp
++ = PACK4(rv
,gv
,bv
,255);
1544 SKEW4(r
, g
, b
, a
, fromskew
);
1550 * 8-bit unpacked samples => RGBA w/ unassociated alpha
1552 DECLARESepPutFunc(putRGBUAseparate8bittile
)
1554 (void) img
; (void) y
;
1556 uint32 rv
, gv
, bv
, av
;
1558 for (x
= w
; x
-- > 0;) {
1560 m
= img
->UaToAa
+(av
<<8);
1564 *cp
++ = PACK4(rv
,gv
,bv
,av
);
1566 SKEW4(r
, g
, b
, a
, fromskew
);
1572 * 16-bit unpacked samples => RGB
1574 DECLARESepPutFunc(putRGBseparate16bittile
)
1576 uint16
*wr
= (uint16
*) r
;
1577 uint16
*wg
= (uint16
*) g
;
1578 uint16
*wb
= (uint16
*) b
;
1579 (void) img
; (void) y
; (void) a
;
1581 for (x
= 0; x
< w
; x
++)
1582 *cp
++ = PACK(img
->Bitdepth16To8
[*wr
++],
1583 img
->Bitdepth16To8
[*wg
++],
1584 img
->Bitdepth16To8
[*wb
++]);
1585 SKEW(wr
, wg
, wb
, fromskew
);
1591 * 16-bit unpacked samples => RGBA w/ associated alpha
1593 DECLARESepPutFunc(putRGBAAseparate16bittile
)
1595 uint16
*wr
= (uint16
*) r
;
1596 uint16
*wg
= (uint16
*) g
;
1597 uint16
*wb
= (uint16
*) b
;
1598 uint16
*wa
= (uint16
*) a
;
1599 (void) img
; (void) y
;
1601 for (x
= 0; x
< w
; x
++)
1602 *cp
++ = PACK4(img
->Bitdepth16To8
[*wr
++],
1603 img
->Bitdepth16To8
[*wg
++],
1604 img
->Bitdepth16To8
[*wb
++],
1605 img
->Bitdepth16To8
[*wa
++]);
1606 SKEW4(wr
, wg
, wb
, wa
, fromskew
);
1612 * 16-bit unpacked samples => RGBA w/ unassociated alpha
1614 DECLARESepPutFunc(putRGBUAseparate16bittile
)
1616 uint16
*wr
= (uint16
*) r
;
1617 uint16
*wg
= (uint16
*) g
;
1618 uint16
*wb
= (uint16
*) b
;
1619 uint16
*wa
= (uint16
*) a
;
1620 (void) img
; (void) y
;
1624 for (x
= w
; x
-- > 0;) {
1625 a
= img
->Bitdepth16To8
[*wa
++];
1626 m
= img
->UaToAa
+(a
<<8);
1627 r
= m
[img
->Bitdepth16To8
[*wr
++]];
1628 g
= m
[img
->Bitdepth16To8
[*wg
++]];
1629 b
= m
[img
->Bitdepth16To8
[*wb
++]];
1630 *cp
++ = PACK4(r
,g
,b
,a
);
1632 SKEW4(wr
, wg
, wb
, wa
, fromskew
);
1638 * 8-bit packed CIE L*a*b 1976 samples => RGB
1640 DECLAREContigPutFunc(putcontig8bitCIELab
)
1647 for (x
= w
; x
-- > 0;) {
1648 TIFFCIELabToXYZ(img
->cielab
,
1649 (unsigned char)pp
[0],
1653 TIFFXYZToRGB(img
->cielab
, X
, Y
, Z
, &r
, &g
, &b
);
1654 *cp
++ = PACK(r
, g
, b
);
1663 * YCbCr -> RGB conversion and packing routines.
1666 #define YCbCrtoRGB(dst, Y) { \
1668 TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b); \
1669 dst = PACK(r, g, b); \
1673 * 8-bit packed YCbCr samples => RGB
1674 * This function is generic for different sampling sizes,
1675 * and can handle blocks sizes that aren't multiples of the
1676 * sampling size. However, it is substantially less optimized
1677 * than the specific sampling cases. It is used as a fallback
1678 * for difficult blocks.
1681 static void putcontig8bitYCbCrGenericTile(
1686 int32 fromskew
, int32 toskew
,
1692 uint32
* cp1
= cp
+w
+toskew
;
1693 uint32
* cp2
= cp1
+w
+toskew
;
1694 uint32
* cp3
= cp2
+w
+toskew
;
1695 int32 incr
= 3*w
+4*toskew
;
1697 int group_size
= v_group
* h_group
+ 2;
1700 fromskew
= (fromskew
* group_size
) / h_group
;
1702 for( yy
= 0; yy
< h
; yy
++ )
1704 unsigned char *pp_line
;
1705 int y_line_group
= yy
/ v_group
;
1706 int y_remainder
= yy
- y_line_group
* v_group
;
1708 pp_line
= pp
+ v_line_group
*
1711 for( xx
= 0; xx
< w
; xx
++ )
1716 for (; h
>= 4; h
-= 4) {
1722 YCbCrtoRGB(cp
[0], pp
[ 0]);
1723 YCbCrtoRGB(cp
[1], pp
[ 1]);
1724 YCbCrtoRGB(cp
[2], pp
[ 2]);
1725 YCbCrtoRGB(cp
[3], pp
[ 3]);
1726 YCbCrtoRGB(cp1
[0], pp
[ 4]);
1727 YCbCrtoRGB(cp1
[1], pp
[ 5]);
1728 YCbCrtoRGB(cp1
[2], pp
[ 6]);
1729 YCbCrtoRGB(cp1
[3], pp
[ 7]);
1730 YCbCrtoRGB(cp2
[0], pp
[ 8]);
1731 YCbCrtoRGB(cp2
[1], pp
[ 9]);
1732 YCbCrtoRGB(cp2
[2], pp
[10]);
1733 YCbCrtoRGB(cp2
[3], pp
[11]);
1734 YCbCrtoRGB(cp3
[0], pp
[12]);
1735 YCbCrtoRGB(cp3
[1], pp
[13]);
1736 YCbCrtoRGB(cp3
[2], pp
[14]);
1737 YCbCrtoRGB(cp3
[3], pp
[15]);
1739 cp
+= 4, cp1
+= 4, cp2
+= 4, cp3
+= 4;
1742 cp
+= incr
, cp1
+= incr
, cp2
+= incr
, cp3
+= incr
;
1749 * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
1751 DECLAREContigPutFunc(putcontig8bitYCbCr44tile
)
1753 uint32
* cp1
= cp
+w
+toskew
;
1754 uint32
* cp2
= cp1
+w
+toskew
;
1755 uint32
* cp3
= cp2
+w
+toskew
;
1756 int32 incr
= 3*w
+4*toskew
;
1759 /* adjust fromskew */
1760 fromskew
= (fromskew
* 18) / 4;
1761 if ((h
& 3) == 0 && (w
& 3) == 0) {
1762 for (; h
>= 4; h
-= 4) {
1768 YCbCrtoRGB(cp
[0], pp
[ 0]);
1769 YCbCrtoRGB(cp
[1], pp
[ 1]);
1770 YCbCrtoRGB(cp
[2], pp
[ 2]);
1771 YCbCrtoRGB(cp
[3], pp
[ 3]);
1772 YCbCrtoRGB(cp1
[0], pp
[ 4]);
1773 YCbCrtoRGB(cp1
[1], pp
[ 5]);
1774 YCbCrtoRGB(cp1
[2], pp
[ 6]);
1775 YCbCrtoRGB(cp1
[3], pp
[ 7]);
1776 YCbCrtoRGB(cp2
[0], pp
[ 8]);
1777 YCbCrtoRGB(cp2
[1], pp
[ 9]);
1778 YCbCrtoRGB(cp2
[2], pp
[10]);
1779 YCbCrtoRGB(cp2
[3], pp
[11]);
1780 YCbCrtoRGB(cp3
[0], pp
[12]);
1781 YCbCrtoRGB(cp3
[1], pp
[13]);
1782 YCbCrtoRGB(cp3
[2], pp
[14]);
1783 YCbCrtoRGB(cp3
[3], pp
[15]);
1785 cp
+= 4, cp1
+= 4, cp2
+= 4, cp3
+= 4;
1788 cp
+= incr
, cp1
+= incr
, cp2
+= incr
, cp3
+= incr
;
1793 for (x
= w
; x
> 0;) {
1799 default: YCbCrtoRGB(cp3
[3], pp
[15]); /* FALLTHROUGH */
1800 case 3: YCbCrtoRGB(cp2
[3], pp
[11]); /* FALLTHROUGH */
1801 case 2: YCbCrtoRGB(cp1
[3], pp
[ 7]); /* FALLTHROUGH */
1802 case 1: YCbCrtoRGB(cp
[3], pp
[ 3]); /* FALLTHROUGH */
1806 default: YCbCrtoRGB(cp3
[2], pp
[14]); /* FALLTHROUGH */
1807 case 3: YCbCrtoRGB(cp2
[2], pp
[10]); /* FALLTHROUGH */
1808 case 2: YCbCrtoRGB(cp1
[2], pp
[ 6]); /* FALLTHROUGH */
1809 case 1: YCbCrtoRGB(cp
[2], pp
[ 2]); /* FALLTHROUGH */
1813 default: YCbCrtoRGB(cp3
[1], pp
[13]); /* FALLTHROUGH */
1814 case 3: YCbCrtoRGB(cp2
[1], pp
[ 9]); /* FALLTHROUGH */
1815 case 2: YCbCrtoRGB(cp1
[1], pp
[ 5]); /* FALLTHROUGH */
1816 case 1: YCbCrtoRGB(cp
[1], pp
[ 1]); /* FALLTHROUGH */
1820 default: YCbCrtoRGB(cp3
[0], pp
[12]); /* FALLTHROUGH */
1821 case 3: YCbCrtoRGB(cp2
[0], pp
[ 8]); /* FALLTHROUGH */
1822 case 2: YCbCrtoRGB(cp1
[0], pp
[ 4]); /* FALLTHROUGH */
1823 case 1: YCbCrtoRGB(cp
[0], pp
[ 0]); /* FALLTHROUGH */
1827 cp
+= x
; cp1
+= x
; cp2
+= x
; cp3
+= x
;
1831 cp
+= 4; cp1
+= 4; cp2
+= 4; cp3
+= 4;
1839 cp
+= incr
, cp1
+= incr
, cp2
+= incr
, cp3
+= incr
;
1846 * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
1848 DECLAREContigPutFunc(putcontig8bitYCbCr42tile
)
1850 uint32
* cp1
= cp
+w
+toskew
;
1851 int32 incr
= 2*toskew
+w
;
1854 fromskew
= (fromskew
* 10) / 4;
1855 if ((h
& 3) == 0 && (w
& 1) == 0) {
1856 for (; h
>= 2; h
-= 2) {
1862 YCbCrtoRGB(cp
[0], pp
[0]);
1863 YCbCrtoRGB(cp
[1], pp
[1]);
1864 YCbCrtoRGB(cp
[2], pp
[2]);
1865 YCbCrtoRGB(cp
[3], pp
[3]);
1866 YCbCrtoRGB(cp1
[0], pp
[4]);
1867 YCbCrtoRGB(cp1
[1], pp
[5]);
1868 YCbCrtoRGB(cp1
[2], pp
[6]);
1869 YCbCrtoRGB(cp1
[3], pp
[7]);
1874 cp
+= incr
, cp1
+= incr
;
1879 for (x
= w
; x
> 0;) {
1885 default: YCbCrtoRGB(cp1
[3], pp
[ 7]); /* FALLTHROUGH */
1886 case 1: YCbCrtoRGB(cp
[3], pp
[ 3]); /* FALLTHROUGH */
1890 default: YCbCrtoRGB(cp1
[2], pp
[ 6]); /* FALLTHROUGH */
1891 case 1: YCbCrtoRGB(cp
[2], pp
[ 2]); /* FALLTHROUGH */
1895 default: YCbCrtoRGB(cp1
[1], pp
[ 5]); /* FALLTHROUGH */
1896 case 1: YCbCrtoRGB(cp
[1], pp
[ 1]); /* FALLTHROUGH */
1900 default: YCbCrtoRGB(cp1
[0], pp
[ 4]); /* FALLTHROUGH */
1901 case 1: YCbCrtoRGB(cp
[0], pp
[ 0]); /* FALLTHROUGH */
1917 cp
+= incr
, cp1
+= incr
;
1924 * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
1926 DECLAREContigPutFunc(putcontig8bitYCbCr41tile
)
1929 /* XXX adjust fromskew */
1936 YCbCrtoRGB(cp
[0], pp
[0]);
1937 YCbCrtoRGB(cp
[1], pp
[1]);
1938 YCbCrtoRGB(cp
[2], pp
[2]);
1939 YCbCrtoRGB(cp
[3], pp
[3]);
1951 case 3: YCbCrtoRGB(cp
[2], pp
[2]);
1952 case 2: YCbCrtoRGB(cp
[1], pp
[1]);
1953 case 1: YCbCrtoRGB(cp
[0], pp
[0]);
1968 * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
1970 DECLAREContigPutFunc(putcontig8bitYCbCr22tile
)
1973 int32 incr
= 2*toskew
+w
;
1975 fromskew
= (fromskew
/ 2) * 6;
1982 YCbCrtoRGB(cp
[0], pp
[0]);
1983 YCbCrtoRGB(cp
[1], pp
[1]);
1984 YCbCrtoRGB(cp2
[0], pp
[2]);
1985 YCbCrtoRGB(cp2
[1], pp
[3]);
1994 YCbCrtoRGB(cp
[0], pp
[0]);
1995 YCbCrtoRGB(cp2
[0], pp
[2]);
2010 YCbCrtoRGB(cp
[0], pp
[0]);
2011 YCbCrtoRGB(cp
[1], pp
[1]);
2020 YCbCrtoRGB(cp
[0], pp
[0]);
2026 * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
2028 DECLAREContigPutFunc(putcontig8bitYCbCr21tile
)
2031 fromskew
= (fromskew
* 4) / 2;
2038 YCbCrtoRGB(cp
[0], pp
[0]);
2039 YCbCrtoRGB(cp
[1], pp
[1]);
2050 YCbCrtoRGB(cp
[0], pp
[0]);
2062 * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
2064 DECLAREContigPutFunc(putcontig8bitYCbCr12tile
)
2067 int32 incr
= 2*toskew
+w
;
2069 fromskew
= (fromskew
/ 2) * 4;
2076 YCbCrtoRGB(cp
[0], pp
[0]);
2077 YCbCrtoRGB(cp2
[0], pp
[1]);
2092 YCbCrtoRGB(cp
[0], pp
[0]);
2100 * 8-bit packed YCbCr samples w/ no subsampling => RGB
2102 DECLAREContigPutFunc(putcontig8bitYCbCr11tile
)
2107 x
= w
; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
2112 YCbCrtoRGB(*cp
++, pp
[0]);
2122 * 8-bit packed YCbCr samples w/ no subsampling => RGB
2124 DECLARESepPutFunc(putseparate8bitYCbCr11tile
)
2128 /* TODO: naming of input vars is still off, change obfuscating declaration inside define, or resolve obfuscation */
2133 TIFFYCbCrtoRGB(img
->ycbcr
,*r
++,*g
++,*b
++,&dr
,&dg
,&db
);
2134 *cp
++ = PACK(dr
,dg
,db
);
2136 SKEW(r
, g
, b
, fromskew
);
2143 initYCbCrConversion(TIFFRGBAImage
* img
)
2145 static const char module[] = "initYCbCrConversion";
2147 float *luma
, *refBlackWhite
;
2149 if (img
->ycbcr
== NULL
) {
2150 img
->ycbcr
= (TIFFYCbCrToRGB
*) _TIFFmalloc(
2151 TIFFroundup_32(sizeof (TIFFYCbCrToRGB
), sizeof (long))
2152 + 4*256*sizeof (TIFFRGBValue
)
2153 + 2*256*sizeof (int)
2154 + 3*256*sizeof (int32
)
2156 if (img
->ycbcr
== NULL
) {
2157 TIFFErrorExt(img
->tif
->tif_clientdata
, module,
2158 "No space for YCbCr->RGB conversion state");
2163 TIFFGetFieldDefaulted(img
->tif
, TIFFTAG_YCBCRCOEFFICIENTS
, &luma
);
2164 TIFFGetFieldDefaulted(img
->tif
, TIFFTAG_REFERENCEBLACKWHITE
,
2166 if (TIFFYCbCrToRGBInit(img
->ycbcr
, luma
, refBlackWhite
) < 0)
2171 static tileContigRoutine
2172 initCIELabConversion(TIFFRGBAImage
* img
)
2174 static const char module[] = "initCIELabConversion";
2180 img
->cielab
= (TIFFCIELabToRGB
*)
2181 _TIFFmalloc(sizeof(TIFFCIELabToRGB
));
2183 TIFFErrorExt(img
->tif
->tif_clientdata
, module,
2184 "No space for CIE L*a*b*->RGB conversion state.");
2189 TIFFGetFieldDefaulted(img
->tif
, TIFFTAG_WHITEPOINT
, &whitePoint
);
2190 refWhite
[1] = 100.0F
;
2191 refWhite
[0] = whitePoint
[0] / whitePoint
[1] * refWhite
[1];
2192 refWhite
[2] = (1.0F
- whitePoint
[0] - whitePoint
[1])
2193 / whitePoint
[1] * refWhite
[1];
2194 if (TIFFCIELabToRGBInit(img
->cielab
, &display_sRGB
, refWhite
) < 0) {
2195 TIFFErrorExt(img
->tif
->tif_clientdata
, module,
2196 "Failed to initialize CIE L*a*b*->RGB conversion state.");
2197 _TIFFfree(img
->cielab
);
2201 return &putcontig8bitCIELab
;
2205 * Greyscale images with less than 8 bits/sample are handled
2206 * with a table to avoid lots of shifts and masks. The table
2207 * is setup so that put*bwtile (below) can retrieve 8/bitspersample
2208 * pixel values simply by indexing into the table with one
2212 makebwmap(TIFFRGBAImage
* img
)
2214 TIFFRGBValue
* Map
= img
->Map
;
2215 int bitspersample
= img
->bitspersample
;
2216 int nsamples
= 8 / bitspersample
;
2223 img
->BWmap
= (uint32
**) _TIFFmalloc(
2224 256*sizeof (uint32
*)+(256*nsamples
*sizeof(uint32
)));
2225 if (img
->BWmap
== NULL
) {
2226 TIFFErrorExt(img
->tif
->tif_clientdata
, TIFFFileName(img
->tif
), "No space for B&W mapping table");
2229 p
= (uint32
*)(img
->BWmap
+ 256);
2230 for (i
= 0; i
< 256; i
++) {
2233 switch (bitspersample
) {
2234 #define GREY(x) c = Map[x]; *p++ = PACK(c,c,c);
2266 * Construct a mapping table to convert from the range
2267 * of the data samples to [0,255] --for display. This
2268 * process also handles inverting B&W images when needed.
2271 setupMap(TIFFRGBAImage
* img
)
2275 range
= (int32
)((1L<<img
->bitspersample
)-1);
2277 /* treat 16 bit the same as eight bit */
2278 if( img
->bitspersample
== 16 )
2279 range
= (int32
) 255;
2281 img
->Map
= (TIFFRGBValue
*) _TIFFmalloc((range
+1) * sizeof (TIFFRGBValue
));
2282 if (img
->Map
== NULL
) {
2283 TIFFErrorExt(img
->tif
->tif_clientdata
, TIFFFileName(img
->tif
),
2284 "No space for photometric conversion table");
2287 if (img
->photometric
== PHOTOMETRIC_MINISWHITE
) {
2288 for (x
= 0; x
<= range
; x
++)
2289 img
->Map
[x
] = (TIFFRGBValue
) (((range
- x
) * 255) / range
);
2291 for (x
= 0; x
<= range
; x
++)
2292 img
->Map
[x
] = (TIFFRGBValue
) ((x
* 255) / range
);
2294 if (img
->bitspersample
<= 16 &&
2295 (img
->photometric
== PHOTOMETRIC_MINISBLACK
||
2296 img
->photometric
== PHOTOMETRIC_MINISWHITE
)) {
2298 * Use photometric mapping table to construct
2299 * unpacking tables for samples <= 8 bits.
2301 if (!makebwmap(img
))
2303 /* no longer need Map, free it */
2304 _TIFFfree(img
->Map
), img
->Map
= NULL
;
2310 checkcmap(TIFFRGBAImage
* img
)
2312 uint16
* r
= img
->redcmap
;
2313 uint16
* g
= img
->greencmap
;
2314 uint16
* b
= img
->bluecmap
;
2315 long n
= 1L<<img
->bitspersample
;
2318 if (*r
++ >= 256 || *g
++ >= 256 || *b
++ >= 256)
2324 cvtcmap(TIFFRGBAImage
* img
)
2326 uint16
* r
= img
->redcmap
;
2327 uint16
* g
= img
->greencmap
;
2328 uint16
* b
= img
->bluecmap
;
2331 for (i
= (1L<<img
->bitspersample
)-1; i
>= 0; i
--) {
2332 #define CVT(x) ((uint16)((x)>>8))
2341 * Palette images with <= 8 bits/sample are handled
2342 * with a table to avoid lots of shifts and masks. The table
2343 * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
2344 * pixel values simply by indexing into the table with one
2348 makecmap(TIFFRGBAImage
* img
)
2350 int bitspersample
= img
->bitspersample
;
2351 int nsamples
= 8 / bitspersample
;
2352 uint16
* r
= img
->redcmap
;
2353 uint16
* g
= img
->greencmap
;
2354 uint16
* b
= img
->bluecmap
;
2358 img
->PALmap
= (uint32
**) _TIFFmalloc(
2359 256*sizeof (uint32
*)+(256*nsamples
*sizeof(uint32
)));
2360 if (img
->PALmap
== NULL
) {
2361 TIFFErrorExt(img
->tif
->tif_clientdata
, TIFFFileName(img
->tif
), "No space for Palette mapping table");
2364 p
= (uint32
*)(img
->PALmap
+ 256);
2365 for (i
= 0; i
< 256; i
++) {
2368 #define CMAP(x) c = (TIFFRGBValue) x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff);
2369 switch (bitspersample
) {
2400 * Construct any mapping table used
2401 * by the associated put routine.
2404 buildMap(TIFFRGBAImage
* img
)
2406 switch (img
->photometric
) {
2407 case PHOTOMETRIC_RGB
:
2408 case PHOTOMETRIC_YCBCR
:
2409 case PHOTOMETRIC_SEPARATED
:
2410 if (img
->bitspersample
== 8)
2413 case PHOTOMETRIC_MINISBLACK
:
2414 case PHOTOMETRIC_MINISWHITE
:
2418 case PHOTOMETRIC_PALETTE
:
2420 * Convert 16-bit colormap to 8-bit (unless it looks
2421 * like an old-style 8-bit colormap).
2423 if (checkcmap(img
) == 16)
2426 TIFFWarningExt(img
->tif
->tif_clientdata
, TIFFFileName(img
->tif
), "Assuming 8-bit colormap");
2428 * Use mapping table and colormap to construct
2429 * unpacking tables for samples < 8 bits.
2431 if (img
->bitspersample
<= 8 && !makecmap(img
))
2439 * Select the appropriate conversion routine for packed data.
2442 PickContigCase(TIFFRGBAImage
* img
)
2444 img
->get
= TIFFIsTiled(img
->tif
) ? gtTileContig
: gtStripContig
;
2445 img
->put
.contig
= NULL
;
2446 switch (img
->photometric
) {
2447 case PHOTOMETRIC_RGB
:
2448 switch (img
->bitspersample
) {
2450 if (img
->alpha
== EXTRASAMPLE_ASSOCALPHA
)
2451 img
->put
.contig
= putRGBAAcontig8bittile
;
2452 else if (img
->alpha
== EXTRASAMPLE_UNASSALPHA
)
2454 if (BuildMapUaToAa(img
))
2455 img
->put
.contig
= putRGBUAcontig8bittile
;
2458 img
->put
.contig
= putRGBcontig8bittile
;
2461 if (img
->alpha
== EXTRASAMPLE_ASSOCALPHA
)
2463 if (BuildMapBitdepth16To8(img
))
2464 img
->put
.contig
= putRGBAAcontig16bittile
;
2466 else if (img
->alpha
== EXTRASAMPLE_UNASSALPHA
)
2468 if (BuildMapBitdepth16To8(img
) &&
2469 BuildMapUaToAa(img
))
2470 img
->put
.contig
= putRGBUAcontig16bittile
;
2474 if (BuildMapBitdepth16To8(img
))
2475 img
->put
.contig
= putRGBcontig16bittile
;
2480 case PHOTOMETRIC_SEPARATED
:
2481 if (buildMap(img
)) {
2482 if (img
->bitspersample
== 8) {
2484 img
->put
.contig
= putRGBcontig8bitCMYKtile
;
2486 img
->put
.contig
= putRGBcontig8bitCMYKMaptile
;
2490 case PHOTOMETRIC_PALETTE
:
2491 if (buildMap(img
)) {
2492 switch (img
->bitspersample
) {
2494 img
->put
.contig
= put8bitcmaptile
;
2497 img
->put
.contig
= put4bitcmaptile
;
2500 img
->put
.contig
= put2bitcmaptile
;
2503 img
->put
.contig
= put1bitcmaptile
;
2508 case PHOTOMETRIC_MINISWHITE
:
2509 case PHOTOMETRIC_MINISBLACK
:
2510 if (buildMap(img
)) {
2511 switch (img
->bitspersample
) {
2513 img
->put
.contig
= put16bitbwtile
;
2516 if (img
->alpha
&& img
->samplesperpixel
== 2)
2517 img
->put
.contig
= putagreytile
;
2519 img
->put
.contig
= putgreytile
;
2522 img
->put
.contig
= put4bitbwtile
;
2525 img
->put
.contig
= put2bitbwtile
;
2528 img
->put
.contig
= put1bitbwtile
;
2533 case PHOTOMETRIC_YCBCR
:
2534 if ((img
->bitspersample
==8) && (img
->samplesperpixel
==3))
2536 if (initYCbCrConversion(img
)!=0)
2539 * The 6.0 spec says that subsampling must be
2540 * one of 1, 2, or 4, and that vertical subsampling
2541 * must always be <= horizontal subsampling; so
2542 * there are only a few possibilities and we just
2543 * enumerate the cases.
2544 * Joris: added support for the [1,2] case, nonetheless, to accomodate
2547 uint16 SubsamplingHor
;
2548 uint16 SubsamplingVer
;
2549 TIFFGetFieldDefaulted(img
->tif
, TIFFTAG_YCBCRSUBSAMPLING
, &SubsamplingHor
, &SubsamplingVer
);
2550 switch ((SubsamplingHor
<<4)|SubsamplingVer
) {
2552 img
->put
.contig
= putcontig8bitYCbCr44tile
;
2555 img
->put
.contig
= putcontig8bitYCbCr42tile
;
2558 img
->put
.contig
= putcontig8bitYCbCr41tile
;
2561 img
->put
.contig
= putcontig8bitYCbCr22tile
;
2564 img
->put
.contig
= putcontig8bitYCbCr21tile
;
2567 img
->put
.contig
= putcontig8bitYCbCr12tile
;
2570 img
->put
.contig
= putcontig8bitYCbCr11tile
;
2576 case PHOTOMETRIC_CIELAB
:
2577 if (buildMap(img
)) {
2578 if (img
->bitspersample
== 8)
2579 img
->put
.contig
= initCIELabConversion(img
);
2583 return ((img
->get
!=NULL
) && (img
->put
.contig
!=NULL
));
2587 * Select the appropriate conversion routine for unpacked data.
2589 * NB: we assume that unpacked single channel data is directed
2590 * to the "packed routines.
2593 PickSeparateCase(TIFFRGBAImage
* img
)
2595 img
->get
= TIFFIsTiled(img
->tif
) ? gtTileSeparate
: gtStripSeparate
;
2596 img
->put
.separate
= NULL
;
2597 switch (img
->photometric
) {
2598 case PHOTOMETRIC_MINISWHITE
:
2599 case PHOTOMETRIC_MINISBLACK
:
2600 /* greyscale images processed pretty much as RGB by gtTileSeparate */
2601 case PHOTOMETRIC_RGB
:
2602 switch (img
->bitspersample
) {
2604 if (img
->alpha
== EXTRASAMPLE_ASSOCALPHA
)
2605 img
->put
.separate
= putRGBAAseparate8bittile
;
2606 else if (img
->alpha
== EXTRASAMPLE_UNASSALPHA
)
2608 if (BuildMapUaToAa(img
))
2609 img
->put
.separate
= putRGBUAseparate8bittile
;
2612 img
->put
.separate
= putRGBseparate8bittile
;
2615 if (img
->alpha
== EXTRASAMPLE_ASSOCALPHA
)
2617 if (BuildMapBitdepth16To8(img
))
2618 img
->put
.separate
= putRGBAAseparate16bittile
;
2620 else if (img
->alpha
== EXTRASAMPLE_UNASSALPHA
)
2622 if (BuildMapBitdepth16To8(img
) &&
2623 BuildMapUaToAa(img
))
2624 img
->put
.separate
= putRGBUAseparate16bittile
;
2628 if (BuildMapBitdepth16To8(img
))
2629 img
->put
.separate
= putRGBseparate16bittile
;
2634 case PHOTOMETRIC_SEPARATED
:
2635 if (img
->bitspersample
== 8 && img
->samplesperpixel
== 4)
2637 img
->alpha
= 1; // Not alpha, but seems like the only way to get 4th band
2638 img
->put
.separate
= putCMYKseparate8bittile
;
2641 case PHOTOMETRIC_YCBCR
:
2642 if ((img
->bitspersample
==8) && (img
->samplesperpixel
==3))
2644 if (initYCbCrConversion(img
)!=0)
2647 TIFFGetFieldDefaulted(img
->tif
, TIFFTAG_YCBCRSUBSAMPLING
, &hs
, &vs
);
2648 switch ((hs
<<4)|vs
) {
2650 img
->put
.separate
= putseparate8bitYCbCr11tile
;
2652 /* TODO: add other cases here */
2658 return ((img
->get
!=NULL
) && (img
->put
.separate
!=NULL
));
2662 BuildMapUaToAa(TIFFRGBAImage
* img
)
2664 static const char module[]="BuildMapUaToAa";
2667 assert(img
->UaToAa
==NULL
);
2668 img
->UaToAa
=_TIFFmalloc(65536);
2669 if (img
->UaToAa
==NULL
)
2671 TIFFErrorExt(img
->tif
->tif_clientdata
,module,"Out of memory");
2675 for (na
=0; na
<256; na
++)
2677 for (nv
=0; nv
<256; nv
++)
2678 *m
++=(nv
*na
+127)/255;
2684 BuildMapBitdepth16To8(TIFFRGBAImage
* img
)
2686 static const char module[]="BuildMapBitdepth16To8";
2689 assert(img
->Bitdepth16To8
==NULL
);
2690 img
->Bitdepth16To8
=_TIFFmalloc(65536);
2691 if (img
->Bitdepth16To8
==NULL
)
2693 TIFFErrorExt(img
->tif
->tif_clientdata
,module,"Out of memory");
2696 m
=img
->Bitdepth16To8
;
2697 for (n
=0; n
<65536; n
++)
2704 * Read a whole strip off data from the file, and convert to RGBA form.
2705 * If this is the last strip, then it will only contain the portion of
2706 * the strip that is actually within the image space. The result is
2707 * organized in bottom to top form.
2712 TIFFReadRGBAStrip(TIFF
* tif
, uint32 row
, uint32
* raster
)
2715 char emsg
[1024] = "";
2718 uint32 rowsperstrip
, rows_to_read
;
2720 if( TIFFIsTiled( tif
) )
2722 TIFFErrorExt(tif
->tif_clientdata
, TIFFFileName(tif
),
2723 "Can't use TIFFReadRGBAStrip() with tiled file.");
2727 TIFFGetFieldDefaulted(tif
, TIFFTAG_ROWSPERSTRIP
, &rowsperstrip
);
2728 if( (row
% rowsperstrip
) != 0 )
2730 TIFFErrorExt(tif
->tif_clientdata
, TIFFFileName(tif
),
2731 "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
2735 if (TIFFRGBAImageOK(tif
, emsg
) && TIFFRGBAImageBegin(&img
, tif
, 0, emsg
)) {
2737 img
.row_offset
= row
;
2740 if( row
+ rowsperstrip
> img
.height
)
2741 rows_to_read
= img
.height
- row
;
2743 rows_to_read
= rowsperstrip
;
2745 ok
= TIFFRGBAImageGet(&img
, raster
, img
.width
, rows_to_read
);
2747 TIFFRGBAImageEnd(&img
);
2749 TIFFErrorExt(tif
->tif_clientdata
, TIFFFileName(tif
), "%s", emsg
);
2757 * Read a whole tile off data from the file, and convert to RGBA form.
2758 * The returned RGBA data is organized from bottom to top of tile,
2759 * and may include zeroed areas if the tile extends off the image.
2763 TIFFReadRGBATile(TIFF
* tif
, uint32 col
, uint32 row
, uint32
* raster
)
2766 char emsg
[1024] = "";
2769 uint32 tile_xsize
, tile_ysize
;
2770 uint32 read_xsize
, read_ysize
;
2774 * Verify that our request is legal - on a tile file, and on a
2778 if( !TIFFIsTiled( tif
) )
2780 TIFFErrorExt(tif
->tif_clientdata
, TIFFFileName(tif
),
2781 "Can't use TIFFReadRGBATile() with stripped file.");
2785 TIFFGetFieldDefaulted(tif
, TIFFTAG_TILEWIDTH
, &tile_xsize
);
2786 TIFFGetFieldDefaulted(tif
, TIFFTAG_TILELENGTH
, &tile_ysize
);
2787 if( (col
% tile_xsize
) != 0 || (row
% tile_ysize
) != 0 )
2789 TIFFErrorExt(tif
->tif_clientdata
, TIFFFileName(tif
),
2790 "Row/col passed to TIFFReadRGBATile() must be top"
2791 "left corner of a tile.");
2796 * Setup the RGBA reader.
2799 if (!TIFFRGBAImageOK(tif
, emsg
)
2800 || !TIFFRGBAImageBegin(&img
, tif
, 0, emsg
)) {
2801 TIFFErrorExt(tif
->tif_clientdata
, TIFFFileName(tif
), "%s", emsg
);
2806 * The TIFFRGBAImageGet() function doesn't allow us to get off the
2807 * edge of the image, even to fill an otherwise valid tile. So we
2808 * figure out how much we can read, and fix up the tile buffer to
2809 * a full tile configuration afterwards.
2812 if( row
+ tile_ysize
> img
.height
)
2813 read_ysize
= img
.height
- row
;
2815 read_ysize
= tile_ysize
;
2817 if( col
+ tile_xsize
> img
.width
)
2818 read_xsize
= img
.width
- col
;
2820 read_xsize
= tile_xsize
;
2823 * Read the chunk of imagery.
2826 img
.row_offset
= row
;
2827 img
.col_offset
= col
;
2829 ok
= TIFFRGBAImageGet(&img
, raster
, read_xsize
, read_ysize
);
2831 TIFFRGBAImageEnd(&img
);
2834 * If our read was incomplete we will need to fix up the tile by
2835 * shifting the data around as if a full tile of data is being returned.
2837 * This is all the more complicated because the image is organized in
2838 * bottom to top format.
2841 if( read_xsize
== tile_xsize
&& read_ysize
== tile_ysize
)
2844 for( i_row
= 0; i_row
< read_ysize
; i_row
++ ) {
2845 memmove( raster
+ (tile_ysize
- i_row
- 1) * tile_xsize
,
2846 raster
+ (read_ysize
- i_row
- 1) * read_xsize
,
2847 read_xsize
* sizeof(uint32
) );
2848 _TIFFmemset( raster
+ (tile_ysize
- i_row
- 1) * tile_xsize
+read_xsize
,
2849 0, sizeof(uint32
) * (tile_xsize
- read_xsize
) );
2852 for( i_row
= read_ysize
; i_row
< tile_ysize
; i_row
++ ) {
2853 _TIFFmemset( raster
+ (tile_ysize
- i_row
- 1) * tile_xsize
,
2854 0, sizeof(uint32
) * tile_xsize
);
2860 /* vim: set ts=8 sts=8 sw=8 noet: */