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.
36 static int gtTileContig(TIFFRGBAImage
*, uint32
*, uint32
, uint32
);
37 static int gtTileSeparate(TIFFRGBAImage
*, uint32
*, uint32
, uint32
);
38 static int gtStripContig(TIFFRGBAImage
*, uint32
*, uint32
, uint32
);
39 static int gtStripSeparate(TIFFRGBAImage
*, uint32
*, uint32
, uint32
);
40 static int pickTileContigCase(TIFFRGBAImage
*);
41 static int pickTileSeparateCase(TIFFRGBAImage
*);
43 static const char photoTag
[] = "PhotometricInterpretation";
46 * Check the image to see if TIFFReadRGBAImage can deal with it.
47 * 1/0 is returned according to whether or not the image can
48 * be handled. If 0 is returned, emsg contains the reason
49 * why it is being rejected.
52 TIFFRGBAImageOK(TIFF
* tif
, char emsg
[1024])
54 TIFFDirectory
* td
= &tif
->tif_dir
;
58 switch (td
->td_bitspersample
) {
59 case 1: case 2: case 4:
63 sprintf(emsg
, "Sorry, can not handle images with %d-bit samples",
64 td
->td_bitspersample
);
67 colorchannels
= td
->td_samplesperpixel
- td
->td_extrasamples
;
68 if (!TIFFGetField(tif
, TIFFTAG_PHOTOMETRIC
, &photometric
)) {
69 switch (colorchannels
) {
71 photometric
= PHOTOMETRIC_MINISBLACK
;
74 photometric
= PHOTOMETRIC_RGB
;
77 sprintf(emsg
, "Missing needed %s tag", photoTag
);
81 switch (photometric
) {
82 case PHOTOMETRIC_MINISWHITE
:
83 case PHOTOMETRIC_MINISBLACK
:
84 case PHOTOMETRIC_PALETTE
:
85 if (td
->td_planarconfig
== PLANARCONFIG_CONTIG
&& td
->td_samplesperpixel
!= 1) {
87 "Sorry, can not handle contiguous data with %s=%d, and %s=%d",
88 photoTag
, photometric
,
89 "Samples/pixel", td
->td_samplesperpixel
);
93 case PHOTOMETRIC_YCBCR
:
94 if (td
->td_planarconfig
!= PLANARCONFIG_CONTIG
) {
95 sprintf(emsg
, "Sorry, can not handle YCbCr images with %s=%d",
96 "Planarconfiguration", td
->td_planarconfig
);
100 case PHOTOMETRIC_RGB
:
101 if (colorchannels
< 3) {
102 sprintf(emsg
, "Sorry, can not handle RGB image with %s=%d",
103 "Color channels", colorchannels
);
108 case PHOTOMETRIC_SEPARATED
:
109 if (td
->td_inkset
!= INKSET_CMYK
) {
110 sprintf(emsg
, "Sorry, can not handle separated image with %s=%d",
111 "InkSet", td
->td_inkset
);
114 if (td
->td_samplesperpixel
!= 4) {
115 sprintf(emsg
, "Sorry, can not handle separated image with %s=%d",
116 "Samples/pixel", td
->td_samplesperpixel
);
121 case PHOTOMETRIC_LOGL
:
122 if (td
->td_compression
!= COMPRESSION_SGILOG
) {
123 sprintf(emsg
, "Sorry, LogL data must have %s=%d",
124 "Compression", COMPRESSION_SGILOG
);
128 case PHOTOMETRIC_LOGLUV
:
129 if (td
->td_compression
!= COMPRESSION_SGILOG
&&
130 td
->td_compression
!= COMPRESSION_SGILOG24
) {
131 sprintf(emsg
, "Sorry, LogLuv data must have %s=%d or %d",
132 "Compression", COMPRESSION_SGILOG
, COMPRESSION_SGILOG24
);
135 if (td
->td_planarconfig
!= PLANARCONFIG_CONTIG
) {
136 sprintf(emsg
, "Sorry, can not handle LogLuv images with %s=%d",
137 "Planarconfiguration", td
->td_planarconfig
);
142 sprintf(emsg
, "Sorry, can not handle image with %s=%d",
143 photoTag
, photometric
);
150 TIFFRGBAImageEnd(TIFFRGBAImage
* img
)
153 _TIFFfree(img
->Map
), img
->Map
= NULL
;
155 _TIFFfree(img
->BWmap
), img
->BWmap
= NULL
;
157 _TIFFfree(img
->PALmap
), img
->PALmap
= NULL
;
159 _TIFFfree(img
->ycbcr
), img
->ycbcr
= NULL
;
162 _TIFFfree( img
->redcmap
);
163 _TIFFfree( img
->greencmap
);
164 _TIFFfree( img
->bluecmap
);
169 isCCITTCompression(TIFF
* tif
)
172 TIFFGetField(tif
, TIFFTAG_COMPRESSION
, &compress
);
173 return (compress
== COMPRESSION_CCITTFAX3
||
174 compress
== COMPRESSION_CCITTFAX4
||
175 compress
== COMPRESSION_CCITTRLE
||
176 compress
== COMPRESSION_CCITTRLEW
);
180 TIFFRGBAImageBegin(TIFFRGBAImage
* img
, TIFF
* tif
, int stop
, char emsg
[1024])
187 uint16
*red_orig
, *green_orig
, *blue_orig
;
190 /* Initialize to normal values */
194 img
->greencmap
= NULL
;
195 img
->bluecmap
= NULL
;
198 img
->stoponerr
= stop
;
199 TIFFGetFieldDefaulted(tif
, TIFFTAG_BITSPERSAMPLE
, &img
->bitspersample
);
200 switch (img
->bitspersample
) {
201 case 1: case 2: case 4:
205 sprintf(emsg
, "Sorry, can not image with %d-bit samples",
210 TIFFGetFieldDefaulted(tif
, TIFFTAG_SAMPLESPERPIXEL
, &img
->samplesperpixel
);
211 TIFFGetFieldDefaulted(tif
, TIFFTAG_EXTRASAMPLES
,
212 &extrasamples
, &sampleinfo
);
213 if (extrasamples
== 1)
214 switch (sampleinfo
[0]) {
215 case EXTRASAMPLE_ASSOCALPHA
: /* data is pre-multiplied */
216 case EXTRASAMPLE_UNASSALPHA
: /* data is not pre-multiplied */
217 img
->alpha
= sampleinfo
[0];
220 colorchannels
= img
->samplesperpixel
- extrasamples
;
221 TIFFGetFieldDefaulted(tif
, TIFFTAG_COMPRESSION
, &compress
);
222 TIFFGetFieldDefaulted(tif
, TIFFTAG_PLANARCONFIG
, &planarconfig
);
223 if (!TIFFGetField(tif
, TIFFTAG_PHOTOMETRIC
, &img
->photometric
)) {
224 switch (colorchannels
) {
226 if (isCCITTCompression(tif
))
227 img
->photometric
= PHOTOMETRIC_MINISWHITE
;
229 img
->photometric
= PHOTOMETRIC_MINISBLACK
;
232 img
->photometric
= PHOTOMETRIC_RGB
;
235 sprintf(emsg
, "Missing needed %s tag", photoTag
);
239 switch (img
->photometric
) {
240 case PHOTOMETRIC_PALETTE
:
241 if (!TIFFGetField(tif
, TIFFTAG_COLORMAP
,
242 &red_orig
, &green_orig
, &blue_orig
)) {
243 TIFFError(TIFFFileName(tif
), "Missing required \"Colormap\" tag");
247 /* copy the colormaps so we can modify them */
248 n_color
= (1L << img
->bitspersample
);
249 img
->redcmap
= (uint16
*) _TIFFmalloc(sizeof(uint16
)*n_color
);
250 img
->greencmap
= (uint16
*) _TIFFmalloc(sizeof(uint16
)*n_color
);
251 img
->bluecmap
= (uint16
*) _TIFFmalloc(sizeof(uint16
)*n_color
);
252 if( !img
->redcmap
|| !img
->greencmap
|| !img
->bluecmap
) {
253 TIFFError(TIFFFileName(tif
), "Out of memory for colormap copy");
257 memcpy( img
->redcmap
, red_orig
, n_color
* 2 );
258 memcpy( img
->greencmap
, green_orig
, n_color
* 2 );
259 memcpy( img
->bluecmap
, blue_orig
, n_color
* 2 );
262 case PHOTOMETRIC_MINISWHITE
:
263 case PHOTOMETRIC_MINISBLACK
:
264 if (planarconfig
== PLANARCONFIG_CONTIG
&& img
->samplesperpixel
!= 1) {
266 "Sorry, can not handle contiguous data with %s=%d, and %s=%d",
267 photoTag
, img
->photometric
,
268 "Samples/pixel", img
->samplesperpixel
);
272 case PHOTOMETRIC_YCBCR
:
273 if (planarconfig
!= PLANARCONFIG_CONTIG
) {
274 sprintf(emsg
, "Sorry, can not handle YCbCr images with %s=%d",
275 "Planarconfiguration", planarconfig
);
278 /* It would probably be nice to have a reality check here. */
279 if (compress
== COMPRESSION_JPEG
&& planarconfig
== PLANARCONFIG_CONTIG
) {
280 /* can rely on libjpeg to convert to RGB */
281 /* XXX should restore current state on exit */
282 TIFFSetField(tif
, TIFFTAG_JPEGCOLORMODE
, JPEGCOLORMODE_RGB
);
283 img
->photometric
= PHOTOMETRIC_RGB
;
286 case PHOTOMETRIC_RGB
:
287 if (colorchannels
< 3) {
288 sprintf(emsg
, "Sorry, can not handle RGB image with %s=%d",
289 "Color channels", colorchannels
);
293 case PHOTOMETRIC_SEPARATED
: {
295 TIFFGetFieldDefaulted(tif
, TIFFTAG_INKSET
, &inkset
);
296 if (inkset
!= INKSET_CMYK
) {
297 sprintf(emsg
, "Sorry, can not handle separated image with %s=%d",
301 if (img
->samplesperpixel
!= 4) {
302 sprintf(emsg
, "Sorry, can not handle separated image with %s=%d",
303 "Samples/pixel", img
->samplesperpixel
);
308 case PHOTOMETRIC_LOGL
:
309 if (compress
!= COMPRESSION_SGILOG
) {
310 sprintf(emsg
, "Sorry, LogL data must have %s=%d",
311 "Compression", COMPRESSION_SGILOG
);
314 TIFFSetField(tif
, TIFFTAG_SGILOGDATAFMT
, SGILOGDATAFMT_8BIT
);
315 img
->photometric
= PHOTOMETRIC_MINISBLACK
; /* little white lie */
316 img
->bitspersample
= 8;
318 case PHOTOMETRIC_LOGLUV
:
319 if (compress
!= COMPRESSION_SGILOG
&& compress
!= COMPRESSION_SGILOG24
) {
320 sprintf(emsg
, "Sorry, LogLuv data must have %s=%d or %d",
321 "Compression", COMPRESSION_SGILOG
, COMPRESSION_SGILOG24
);
324 if (planarconfig
!= PLANARCONFIG_CONTIG
) {
325 sprintf(emsg
, "Sorry, can not handle LogLuv images with %s=%d",
326 "Planarconfiguration", planarconfig
);
329 TIFFSetField(tif
, TIFFTAG_SGILOGDATAFMT
, SGILOGDATAFMT_8BIT
);
330 img
->photometric
= PHOTOMETRIC_RGB
; /* little white lie */
331 img
->bitspersample
= 8;
334 sprintf(emsg
, "Sorry, can not handle image with %s=%d",
335 photoTag
, img
->photometric
);
342 TIFFGetField(tif
, TIFFTAG_IMAGEWIDTH
, &img
->width
);
343 TIFFGetField(tif
, TIFFTAG_IMAGELENGTH
, &img
->height
);
344 TIFFGetFieldDefaulted(tif
, TIFFTAG_ORIENTATION
, &img
->orientation
);
346 !(planarconfig
== PLANARCONFIG_SEPARATE
&& colorchannels
> 1);
348 img
->get
= TIFFIsTiled(tif
) ? gtTileContig
: gtStripContig
;
349 (void) pickTileContigCase(img
);
351 img
->get
= TIFFIsTiled(tif
) ? gtTileSeparate
: gtStripSeparate
;
352 (void) pickTileSeparateCase(img
);
358 TIFFRGBAImageGet(TIFFRGBAImage
* img
, uint32
* raster
, uint32 w
, uint32 h
)
360 if (img
->get
== NULL
) {
361 TIFFError(TIFFFileName(img
->tif
), "No \"get\" routine setup");
364 if (img
->put
.any
== NULL
) {
365 TIFFError(TIFFFileName(img
->tif
),
366 "No \"put\" routine setupl; probably can not handle image format");
369 return (*img
->get
)(img
, raster
, w
, h
);
373 * Read the specified image into an ABGR-format raster.
376 TIFFReadRGBAImage(TIFF
* tif
,
377 uint32 rwidth
, uint32 rheight
, uint32
* raster
, int stop
)
383 if (TIFFRGBAImageBegin(&img
, tif
, stop
, emsg
)) {
384 /* XXX verify rwidth and rheight against width and height */
385 ok
= TIFFRGBAImageGet(&img
, raster
+(rheight
-img
.height
)*rwidth
,
387 TIFFRGBAImageEnd(&img
);
389 TIFFError(TIFFFileName(tif
), emsg
);
396 setorientation(TIFFRGBAImage
* img
, uint32 h
)
398 TIFF
* tif
= img
->tif
;
401 switch (img
->orientation
) {
402 case ORIENTATION_BOTRIGHT
:
403 case ORIENTATION_RIGHTBOT
: /* XXX */
404 case ORIENTATION_LEFTBOT
: /* XXX */
405 TIFFWarning(TIFFFileName(tif
), "using bottom-left orientation");
406 img
->orientation
= ORIENTATION_BOTLEFT
;
408 case ORIENTATION_BOTLEFT
:
411 case ORIENTATION_TOPRIGHT
:
412 case ORIENTATION_RIGHTTOP
: /* XXX */
413 case ORIENTATION_LEFTTOP
: /* XXX */
415 TIFFWarning(TIFFFileName(tif
), "using top-left orientation");
416 img
->orientation
= ORIENTATION_TOPLEFT
;
418 case ORIENTATION_TOPLEFT
:
426 * Get an tile-organized image that has
427 * PlanarConfiguration contiguous if SamplesPerPixel > 1
429 * SamplesPerPixel == 1
432 gtTileContig(TIFFRGBAImage
* img
, uint32
* raster
, uint32 w
, uint32 h
)
434 TIFF
* tif
= img
->tif
;
435 tileContigRoutine put
= img
->put
.contig
;
440 int32 fromskew
, toskew
;
443 buf
= (u_char
*) _TIFFmalloc(TIFFTileSize(tif
));
445 TIFFError(TIFFFileName(tif
), "No space for tile buffer");
448 TIFFGetField(tif
, TIFFTAG_TILEWIDTH
, &tw
);
449 TIFFGetField(tif
, TIFFTAG_TILELENGTH
, &th
);
450 y
= setorientation(img
, h
);
451 orientation
= img
->orientation
;
452 toskew
= -(int32
) (orientation
== ORIENTATION_TOPLEFT
? tw
+w
: tw
-w
);
453 for (row
= 0; row
< h
; row
+= th
) {
454 nrow
= (row
+ th
> h
? h
- row
: th
);
455 for (col
= 0; col
< w
; col
+= tw
) {
456 if (TIFFReadTile(tif
, buf
, col
+img
->col_offset
,
457 row
+img
->row_offset
, 0, 0) < 0 && img
->stoponerr
)
461 * Tile is clipped horizontally. Calculate
462 * visible portion and skewing factors.
464 uint32 npix
= w
- col
;
465 fromskew
= tw
- npix
;
466 (*put
)(img
, raster
+y
*w
+col
, col
, y
,
467 npix
, nrow
, fromskew
, toskew
+ fromskew
, buf
);
469 (*put
)(img
, raster
+y
*w
+col
, col
, y
, tw
, nrow
, 0, toskew
, buf
);
472 y
+= (orientation
== ORIENTATION_TOPLEFT
?
473 -(int32
) nrow
: (int32
) nrow
);
480 * Get an tile-organized image that has
481 * SamplesPerPixel > 1
482 * PlanarConfiguration separated
483 * We assume that all such images are RGB.
486 gtTileSeparate(TIFFRGBAImage
* img
, uint32
* raster
, uint32 w
, uint32 h
)
488 TIFF
* tif
= img
->tif
;
489 tileSeparateRoutine put
= img
->put
.separate
;
499 int32 fromskew
, toskew
;
500 int alpha
= img
->alpha
;
503 tilesize
= TIFFTileSize(tif
);
504 buf
= (u_char
*) _TIFFmalloc(4*tilesize
);
506 TIFFError(TIFFFileName(tif
), "No space for tile buffer");
514 memset(a
, 0xff, tilesize
);
515 TIFFGetField(tif
, TIFFTAG_TILEWIDTH
, &tw
);
516 TIFFGetField(tif
, TIFFTAG_TILELENGTH
, &th
);
517 y
= setorientation(img
, h
);
518 orientation
= img
->orientation
;
519 toskew
= -(int32
) (orientation
== ORIENTATION_TOPLEFT
? tw
+w
: tw
-w
);
520 for (row
= 0; row
< h
; row
+= th
) {
521 nrow
= (row
+ th
> h
? h
- row
: th
);
522 for (col
= 0; col
< w
; col
+= tw
) {
523 if (TIFFReadTile(tif
, r
, col
+img
->col_offset
,
524 row
+img
->row_offset
,0,0) < 0 && img
->stoponerr
)
526 if (TIFFReadTile(tif
, g
, col
+img
->col_offset
,
527 row
+img
->row_offset
,0,1) < 0 && img
->stoponerr
)
529 if (TIFFReadTile(tif
, b
, col
+img
->col_offset
,
530 row
+img
->row_offset
,0,2) < 0 && img
->stoponerr
)
532 if (alpha
&& TIFFReadTile(tif
,a
,col
+img
->col_offset
,
533 row
+img
->row_offset
,0,3) < 0 && img
->stoponerr
)
537 * Tile is clipped horizontally. Calculate
538 * visible portion and skewing factors.
540 uint32 npix
= w
- col
;
541 fromskew
= tw
- npix
;
542 (*put
)(img
, raster
+y
*w
+col
, col
, y
,
543 npix
, nrow
, fromskew
, toskew
+ fromskew
, r
, g
, b
, a
);
545 (*put
)(img
, raster
+y
*w
+col
, col
, y
,
546 tw
, nrow
, 0, toskew
, r
, g
, b
, a
);
549 y
+= (orientation
== ORIENTATION_TOPLEFT
?
550 -(int32
) nrow
: (int32
) nrow
);
557 * Get a strip-organized image that has
558 * PlanarConfiguration contiguous if SamplesPerPixel > 1
560 * SamplesPerPixel == 1
563 gtStripContig(TIFFRGBAImage
* img
, uint32
* raster
, uint32 w
, uint32 h
)
565 TIFF
* tif
= img
->tif
;
566 tileContigRoutine put
= img
->put
.contig
;
571 uint32 imagewidth
= img
->width
;
573 int32 fromskew
, toskew
;
575 buf
= (u_char
*) _TIFFmalloc(TIFFStripSize(tif
));
577 TIFFError(TIFFFileName(tif
), "No space for strip buffer");
580 y
= setorientation(img
, h
);
581 orientation
= img
->orientation
;
582 toskew
= -(int32
) (orientation
== ORIENTATION_TOPLEFT
? w
+w
: w
-w
);
583 TIFFGetFieldDefaulted(tif
, TIFFTAG_ROWSPERSTRIP
, &rowsperstrip
);
584 scanline
= TIFFScanlineSize(tif
);
585 fromskew
= (w
< imagewidth
? imagewidth
- w
: 0);
586 for (row
= 0; row
< h
; row
+= rowsperstrip
) {
587 nrow
= (row
+ rowsperstrip
> h
? h
- row
: rowsperstrip
);
588 if (TIFFReadEncodedStrip(tif
,
589 TIFFComputeStrip(tif
,row
+img
->row_offset
, 0),
590 buf
, nrow
*scanline
) < 0
593 (*put
)(img
, raster
+y
*w
, 0, y
, w
, nrow
, fromskew
, toskew
, buf
);
594 y
+= (orientation
== ORIENTATION_TOPLEFT
?
595 -(int32
) nrow
: (int32
) nrow
);
602 * Get a strip-organized image with
603 * SamplesPerPixel > 1
604 * PlanarConfiguration separated
605 * We assume that all such images are RGB.
608 gtStripSeparate(TIFFRGBAImage
* img
, uint32
* raster
, uint32 w
, uint32 h
)
610 TIFF
* tif
= img
->tif
;
611 tileSeparateRoutine put
= img
->put
.separate
;
614 u_char
*r
, *g
, *b
, *a
;
617 uint32 rowsperstrip
, offset_row
;
618 uint32 imagewidth
= img
->width
;
620 int32 fromskew
, toskew
;
621 int alpha
= img
->alpha
;
623 stripsize
= TIFFStripSize(tif
);
624 r
= buf
= (u_char
*)_TIFFmalloc(4*stripsize
);
626 TIFFError(TIFFFileName(tif
), "No space for tile buffer");
633 memset(a
, 0xff, stripsize
);
634 y
= setorientation(img
, h
);
635 orientation
= img
->orientation
;
636 toskew
= -(int32
) (orientation
== ORIENTATION_TOPLEFT
? w
+w
: w
-w
);
637 TIFFGetFieldDefaulted(tif
, TIFFTAG_ROWSPERSTRIP
, &rowsperstrip
);
638 scanline
= TIFFScanlineSize(tif
);
639 fromskew
= (w
< imagewidth
? imagewidth
- w
: 0);
640 for (row
= 0; row
< h
; row
+= rowsperstrip
) {
641 nrow
= (row
+ rowsperstrip
> h
? h
- row
: rowsperstrip
);
642 offset_row
= row
+ img
->row_offset
;
643 if (TIFFReadEncodedStrip(tif
, TIFFComputeStrip(tif
, offset_row
, 0),
644 r
, nrow
*scanline
) < 0 && img
->stoponerr
)
646 if (TIFFReadEncodedStrip(tif
, TIFFComputeStrip(tif
, offset_row
, 1),
647 g
, nrow
*scanline
) < 0 && img
->stoponerr
)
649 if (TIFFReadEncodedStrip(tif
, TIFFComputeStrip(tif
, offset_row
, 2),
650 b
, nrow
*scanline
) < 0 && img
->stoponerr
)
653 (TIFFReadEncodedStrip(tif
, TIFFComputeStrip(tif
, offset_row
, 3),
654 a
, nrow
*scanline
) < 0 && img
->stoponerr
))
656 (*put
)(img
, raster
+y
*w
, 0, y
, w
, nrow
, fromskew
, toskew
, r
, g
, b
, a
);
657 y
+= (orientation
== ORIENTATION_TOPLEFT
?
658 -(int32
) nrow
: (int32
) nrow
);
665 * The following routines move decoded data returned
666 * from the TIFF library into rasters filled with packed
667 * ABGR pixels (i.e. suitable for passing to lrecwrite.)
669 * The routines have been created according to the most
670 * important cases and optimized. pickTileContigCase and
671 * pickTileSeparateCase analyze the parameters and select
672 * the appropriate "put" routine to use.
674 #define REPEAT8(op) REPEAT4(op); REPEAT4(op)
675 #define REPEAT4(op) REPEAT2(op); REPEAT2(op)
676 #define REPEAT2(op) op; op
677 #define CASE8(x,op) \
679 case 7: op; case 6: op; case 5: op; \
680 case 4: op; case 3: op; case 2: op; \
683 #define CASE4(x,op) switch (x) { case 3: op; case 2: op; case 1: op; }
686 #define UNROLL8(w, op1, op2) { \
688 for (_x = w; _x >= 8; _x -= 8) { \
697 #define UNROLL4(w, op1, op2) { \
699 for (_x = w; _x >= 4; _x -= 4) { \
708 #define UNROLL2(w, op1, op2) { \
710 for (_x = w; _x >= 2; _x -= 2) { \
720 #define SKEW(r,g,b,skew) { r += skew; g += skew; b += skew; }
721 #define SKEW4(r,g,b,a,skew) { r += skew; g += skew; b += skew; a+= skew; }
723 #define A1 ((uint32)(0xffL<<24))
724 #define PACK(r,g,b) \
725 ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|A1)
726 #define PACK4(r,g,b,a) \
727 ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|((uint32)(a)<<24))
728 #define W2B(v) (((v)>>8)&0xff)
729 #define PACKW(r,g,b) \
730 ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|A1)
731 #define PACKW4(r,g,b,a) \
732 ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|((uint32)W2B(a)<<24))
734 #define DECLAREContigPutFunc(name) \
736 TIFFRGBAImage* img, \
738 uint32 x, uint32 y, \
739 uint32 w, uint32 h, \
740 int32 fromskew, int32 toskew, \
745 * 8-bit palette => colormap/RGB
747 DECLAREContigPutFunc(put8bitcmaptile
)
749 uint32
** PALmap
= img
->PALmap
;
753 UNROLL8(w
, NOP
, *cp
++ = PALmap
[*pp
++][0]);
760 * 4-bit palette => colormap/RGB
762 DECLAREContigPutFunc(put4bitcmaptile
)
764 uint32
** PALmap
= img
->PALmap
;
770 UNROLL2(w
, bw
= PALmap
[*pp
++], *cp
++ = *bw
++);
777 * 2-bit palette => colormap/RGB
779 DECLAREContigPutFunc(put2bitcmaptile
)
781 uint32
** PALmap
= img
->PALmap
;
787 UNROLL4(w
, bw
= PALmap
[*pp
++], *cp
++ = *bw
++);
794 * 1-bit palette => colormap/RGB
796 DECLAREContigPutFunc(put1bitcmaptile
)
798 uint32
** PALmap
= img
->PALmap
;
804 UNROLL8(w
, bw
= PALmap
[*pp
++], *cp
++ = *bw
++);
811 * 8-bit greyscale => colormap/RGB
813 DECLAREContigPutFunc(putgreytile
)
815 uint32
** BWmap
= img
->BWmap
;
819 for (x
= w
; x
-- > 0;)
820 *cp
++ = BWmap
[*pp
++][0];
827 * 1-bit bilevel => colormap/RGB
829 DECLAREContigPutFunc(put1bitbwtile
)
831 uint32
** BWmap
= img
->BWmap
;
837 UNROLL8(w
, bw
= BWmap
[*pp
++], *cp
++ = *bw
++);
844 * 2-bit greyscale => colormap/RGB
846 DECLAREContigPutFunc(put2bitbwtile
)
848 uint32
** BWmap
= img
->BWmap
;
854 UNROLL4(w
, bw
= BWmap
[*pp
++], *cp
++ = *bw
++);
861 * 4-bit greyscale => colormap/RGB
863 DECLAREContigPutFunc(put4bitbwtile
)
865 uint32
** BWmap
= img
->BWmap
;
871 UNROLL2(w
, bw
= BWmap
[*pp
++], *cp
++ = *bw
++);
878 * 8-bit packed samples, no Map => RGB
880 DECLAREContigPutFunc(putRGBcontig8bittile
)
882 int samplesperpixel
= img
->samplesperpixel
;
885 fromskew
*= samplesperpixel
;
888 *cp
++ = PACK(pp
[0], pp
[1], pp
[2]);
889 pp
+= samplesperpixel
);
896 * 8-bit packed samples, w/ Map => RGB
898 DECLAREContigPutFunc(putRGBcontig8bitMaptile
)
900 TIFFRGBValue
* Map
= img
->Map
;
901 int samplesperpixel
= img
->samplesperpixel
;
904 fromskew
*= samplesperpixel
;
906 for (x
= w
; x
-- > 0;) {
907 *cp
++ = PACK(Map
[pp
[0]], Map
[pp
[1]], Map
[pp
[2]]);
908 pp
+= samplesperpixel
;
916 * 8-bit packed samples => RGBA w/ associated alpha
917 * (known to have Map == NULL)
919 DECLAREContigPutFunc(putRGBAAcontig8bittile
)
921 int samplesperpixel
= img
->samplesperpixel
;
924 fromskew
*= samplesperpixel
;
927 *cp
++ = PACK4(pp
[0], pp
[1], pp
[2], pp
[3]);
928 pp
+= samplesperpixel
);
935 * 8-bit packed samples => RGBA w/ unassociated alpha
936 * (known to have Map == NULL)
938 DECLAREContigPutFunc(putRGBUAcontig8bittile
)
940 int samplesperpixel
= img
->samplesperpixel
;
943 fromskew
*= samplesperpixel
;
946 for (x
= w
; x
-- > 0;) {
948 r
= (pp
[0] * a
) / 255;
949 g
= (pp
[1] * a
) / 255;
950 b
= (pp
[2] * a
) / 255;
951 *cp
++ = PACK4(r
,g
,b
,a
);
952 pp
+= samplesperpixel
;
960 * 16-bit packed samples => RGB
962 DECLAREContigPutFunc(putRGBcontig16bittile
)
964 int samplesperpixel
= img
->samplesperpixel
;
965 uint16
*wp
= (uint16
*)pp
;
968 fromskew
*= samplesperpixel
;
970 for (x
= w
; x
-- > 0;) {
971 *cp
++ = PACKW(wp
[0], wp
[1], wp
[2]);
972 wp
+= samplesperpixel
;
980 * 16-bit packed samples => RGBA w/ associated alpha
981 * (known to have Map == NULL)
983 DECLAREContigPutFunc(putRGBAAcontig16bittile
)
985 int samplesperpixel
= img
->samplesperpixel
;
986 uint16
*wp
= (uint16
*)pp
;
989 fromskew
*= samplesperpixel
;
991 for (x
= w
; x
-- > 0;) {
992 *cp
++ = PACKW4(wp
[0], wp
[1], wp
[2], wp
[3]);
993 wp
+= samplesperpixel
;
1001 * 16-bit packed samples => RGBA w/ unassociated alpha
1002 * (known to have Map == NULL)
1004 DECLAREContigPutFunc(putRGBUAcontig16bittile
)
1006 int samplesperpixel
= img
->samplesperpixel
;
1007 uint16
*wp
= (uint16
*)pp
;
1010 fromskew
*= samplesperpixel
;
1014 * We shift alpha down four bits just in case unsigned
1015 * arithmetic doesn't handle the full range.
1016 * We still have plenty of accuracy, since the output is 8 bits.
1017 * So we have (r * 0xffff) * (a * 0xfff)) = r*a * (0xffff*0xfff)
1018 * Since we want r*a * 0xff for eight bit output,
1019 * we divide by (0xffff * 0xfff) / 0xff == 0x10eff.
1021 for (x
= w
; x
-- > 0;) {
1023 r
= (wp
[0] * a
) / 0x10eff;
1024 g
= (wp
[1] * a
) / 0x10eff;
1025 b
= (wp
[2] * a
) / 0x10eff;
1026 *cp
++ = PACK4(r
,g
,b
,a
);
1027 wp
+= samplesperpixel
;
1035 * 8-bit packed CMYK samples w/o Map => RGB
1037 * NB: The conversion of CMYK->RGB is *very* crude.
1039 DECLAREContigPutFunc(putRGBcontig8bitCMYKtile
)
1041 int samplesperpixel
= img
->samplesperpixel
;
1045 fromskew
*= samplesperpixel
;
1049 r
= (k
*(255-pp
[0]))/255;
1050 g
= (k
*(255-pp
[1]))/255;
1051 b
= (k
*(255-pp
[2]))/255;
1052 *cp
++ = PACK(r
, g
, b
);
1053 pp
+= samplesperpixel
);
1060 * 8-bit packed CMYK samples w/Map => RGB
1062 * NB: The conversion of CMYK->RGB is *very* crude.
1064 DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile
)
1066 int samplesperpixel
= img
->samplesperpixel
;
1067 TIFFRGBValue
* Map
= img
->Map
;
1071 fromskew
*= samplesperpixel
;
1073 for (x
= w
; x
-- > 0;) {
1075 r
= (k
*(255-pp
[0]))/255;
1076 g
= (k
*(255-pp
[1]))/255;
1077 b
= (k
*(255-pp
[2]))/255;
1078 *cp
++ = PACK(Map
[r
], Map
[g
], Map
[b
]);
1079 pp
+= samplesperpixel
;
1086 #define DECLARESepPutFunc(name) \
1088 TIFFRGBAImage* img,\
1090 uint32 x, uint32 y, \
1091 uint32 w, uint32 h,\
1092 int32 fromskew, int32 toskew,\
1093 u_char* r, u_char* g, u_char* b, u_char* a\
1097 * 8-bit unpacked samples => RGB
1099 DECLARESepPutFunc(putRGBseparate8bittile
)
1101 (void) img
; (void) x
; (void) y
; (void) a
;
1103 UNROLL8(w
, NOP
, *cp
++ = PACK(*r
++, *g
++, *b
++));
1104 SKEW(r
, g
, b
, fromskew
);
1110 * 8-bit unpacked samples => RGB
1112 DECLARESepPutFunc(putRGBseparate8bitMaptile
)
1114 TIFFRGBValue
* Map
= img
->Map
;
1118 for (x
= w
; x
> 0; x
--)
1119 *cp
++ = PACK(Map
[*r
++], Map
[*g
++], Map
[*b
++]);
1120 SKEW(r
, g
, b
, fromskew
);
1126 * 8-bit unpacked samples => RGBA w/ associated alpha
1128 DECLARESepPutFunc(putRGBAAseparate8bittile
)
1130 (void) img
; (void) x
; (void) y
;
1132 UNROLL8(w
, NOP
, *cp
++ = PACK4(*r
++, *g
++, *b
++, *a
++));
1133 SKEW4(r
, g
, b
, a
, fromskew
);
1139 * 8-bit unpacked samples => RGBA w/ unassociated alpha
1141 DECLARESepPutFunc(putRGBUAseparate8bittile
)
1143 (void) img
; (void) y
;
1145 uint32 rv
, gv
, bv
, av
;
1146 for (x
= w
; x
-- > 0;) {
1148 rv
= (*r
++ * av
) / 255;
1149 gv
= (*g
++ * av
) / 255;
1150 bv
= (*b
++ * av
) / 255;
1151 *cp
++ = PACK4(rv
,gv
,bv
,av
);
1153 SKEW4(r
, g
, b
, a
, fromskew
);
1159 * 16-bit unpacked samples => RGB
1161 DECLARESepPutFunc(putRGBseparate16bittile
)
1163 uint16
*wr
= (uint16
*) r
;
1164 uint16
*wg
= (uint16
*) g
;
1165 uint16
*wb
= (uint16
*) b
;
1167 (void) img
; (void) y
; (void) a
;
1169 for (x
= 0; x
< w
; x
++)
1170 *cp
++ = PACKW(*wr
++, *wg
++, *wb
++);
1171 SKEW(wr
, wg
, wb
, fromskew
);
1177 * 16-bit unpacked samples => RGBA w/ associated alpha
1179 DECLARESepPutFunc(putRGBAAseparate16bittile
)
1181 uint16
*wr
= (uint16
*) r
;
1182 uint16
*wg
= (uint16
*) g
;
1183 uint16
*wb
= (uint16
*) b
;
1184 uint16
*wa
= (uint16
*) a
;
1186 (void) img
; (void) y
;
1188 for (x
= 0; x
< w
; x
++)
1189 *cp
++ = PACKW4(*wr
++, *wg
++, *wb
++, *wa
++);
1190 SKEW4(wr
, wg
, wb
, wa
, fromskew
);
1196 * 16-bit unpacked samples => RGBA w/ unassociated alpha
1198 DECLARESepPutFunc(putRGBUAseparate16bittile
)
1200 uint16
*wr
= (uint16
*) r
;
1201 uint16
*wg
= (uint16
*) g
;
1202 uint16
*wb
= (uint16
*) b
;
1203 uint16
*wa
= (uint16
*) a
;
1205 (void) img
; (void) y
;
1209 * We shift alpha down four bits just in case unsigned
1210 * arithmetic doesn't handle the full range.
1211 * We still have plenty of accuracy, since the output is 8 bits.
1212 * So we have (r * 0xffff) * (a * 0xfff)) = r*a * (0xffff*0xfff)
1213 * Since we want r*a * 0xff for eight bit output,
1214 * we divide by (0xffff * 0xfff) / 0xff == 0x10eff.
1216 for (x
= w
; x
-- > 0;) {
1218 r
= (*wr
++ * a
) / 0x10eff;
1219 g
= (*wg
++ * a
) / 0x10eff;
1220 b
= (*wb
++ * a
) / 0x10eff;
1221 *cp
++ = PACK4(r
,g
,b
,a
);
1223 SKEW4(wr
, wg
, wb
, wa
, fromskew
);
1229 * YCbCr -> RGB conversion and packing routines. The colorspace
1230 * conversion algorithm comes from the IJG v5a code; see below
1231 * for more information on how it works.
1234 #define YCbCrtoRGB(dst, yc) { \
1237 clamptab[Y+Crrtab[Cr]], \
1238 clamptab[Y + (int)((Cbgtab[Cb]+Crgtab[Cr])>>16)], \
1239 clamptab[Y+Cbbtab[Cb]]); \
1241 #define YCbCrSetup \
1242 TIFFYCbCrToRGB* ycbcr = img->ycbcr; \
1243 int* Crrtab = ycbcr->Cr_r_tab; \
1244 int* Cbbtab = ycbcr->Cb_b_tab; \
1245 int32* Crgtab = ycbcr->Cr_g_tab; \
1246 int32* Cbgtab = ycbcr->Cb_g_tab; \
1247 TIFFRGBValue* clamptab = ycbcr->clamptab
1250 * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
1252 DECLAREContigPutFunc(putcontig8bitYCbCr44tile
)
1255 uint32
* cp1
= cp
+w
+toskew
;
1256 uint32
* cp2
= cp1
+w
+toskew
;
1257 uint32
* cp3
= cp2
+w
+toskew
;
1258 int32 incr
= 3*w
+4*toskew
;
1261 /* XXX adjust fromskew */
1262 for (; h
>= 4; h
-= 4) {
1268 YCbCrtoRGB(cp
[0], pp
[ 0]);
1269 YCbCrtoRGB(cp
[1], pp
[ 1]);
1270 YCbCrtoRGB(cp
[2], pp
[ 2]);
1271 YCbCrtoRGB(cp
[3], pp
[ 3]);
1272 YCbCrtoRGB(cp1
[0], pp
[ 4]);
1273 YCbCrtoRGB(cp1
[1], pp
[ 5]);
1274 YCbCrtoRGB(cp1
[2], pp
[ 6]);
1275 YCbCrtoRGB(cp1
[3], pp
[ 7]);
1276 YCbCrtoRGB(cp2
[0], pp
[ 8]);
1277 YCbCrtoRGB(cp2
[1], pp
[ 9]);
1278 YCbCrtoRGB(cp2
[2], pp
[10]);
1279 YCbCrtoRGB(cp2
[3], pp
[11]);
1280 YCbCrtoRGB(cp3
[0], pp
[12]);
1281 YCbCrtoRGB(cp3
[1], pp
[13]);
1282 YCbCrtoRGB(cp3
[2], pp
[14]);
1283 YCbCrtoRGB(cp3
[3], pp
[15]);
1285 cp
+= 4, cp1
+= 4, cp2
+= 4, cp3
+= 4;
1288 cp
+= incr
, cp1
+= incr
, cp2
+= incr
, cp3
+= incr
;
1294 * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
1296 DECLAREContigPutFunc(putcontig8bitYCbCr42tile
)
1299 uint32
* cp1
= cp
+w
+toskew
;
1300 int32 incr
= 2*toskew
+w
;
1303 /* XXX adjust fromskew */
1304 for (; h
>= 2; h
-= 2) {
1310 YCbCrtoRGB(cp
[0], pp
[0]);
1311 YCbCrtoRGB(cp
[1], pp
[1]);
1312 YCbCrtoRGB(cp
[2], pp
[2]);
1313 YCbCrtoRGB(cp
[3], pp
[3]);
1314 YCbCrtoRGB(cp1
[0], pp
[4]);
1315 YCbCrtoRGB(cp1
[1], pp
[5]);
1316 YCbCrtoRGB(cp1
[2], pp
[6]);
1317 YCbCrtoRGB(cp1
[3], pp
[7]);
1322 cp
+= incr
, cp1
+= incr
;
1328 * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
1330 DECLAREContigPutFunc(putcontig8bitYCbCr41tile
)
1335 /* XXX adjust fromskew */
1342 YCbCrtoRGB(cp
[0], pp
[0]);
1343 YCbCrtoRGB(cp
[1], pp
[1]);
1344 YCbCrtoRGB(cp
[2], pp
[2]);
1345 YCbCrtoRGB(cp
[3], pp
[3]);
1356 * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
1358 DECLAREContigPutFunc(putcontig8bitYCbCr22tile
)
1361 uint32
* cp1
= cp
+w
+toskew
;
1362 int32 incr
= 2*toskew
+w
;
1365 /* XXX adjust fromskew */
1366 for (; h
>= 2; h
-= 2) {
1372 YCbCrtoRGB(cp
[0], pp
[0]);
1373 YCbCrtoRGB(cp
[1], pp
[1]);
1374 YCbCrtoRGB(cp1
[0], pp
[2]);
1375 YCbCrtoRGB(cp1
[1], pp
[3]);
1380 cp
+= incr
, cp1
+= incr
;
1386 * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
1388 DECLAREContigPutFunc(putcontig8bitYCbCr21tile
)
1393 /* XXX adjust fromskew */
1400 YCbCrtoRGB(cp
[0], pp
[0]);
1401 YCbCrtoRGB(cp
[1], pp
[1]);
1412 * 8-bit packed YCbCr samples w/ no subsampling => RGB
1414 DECLAREContigPutFunc(putcontig8bitYCbCr11tile
)
1419 /* XXX adjust fromskew */
1426 YCbCrtoRGB(*cp
++, pp
[0]);
1437 #define LumaRed coeffs[0]
1438 #define LumaGreen coeffs[1]
1439 #define LumaBlue coeffs[2]
1441 #define FIX(x) ((int32)((x) * (1L<<SHIFT) + 0.5))
1442 #define ONE_HALF ((int32)(1<<(SHIFT-1)))
1445 * Initialize the YCbCr->RGB conversion tables. The conversion
1446 * is done according to the 6.0 spec:
1448 * R = Y + Cr*(2 - 2*LumaRed)
1449 * B = Y + Cb*(2 - 2*LumaBlue)
1451 * - LumaBlue*Cb*(2-2*LumaBlue)/LumaGreen
1452 * - LumaRed*Cr*(2-2*LumaRed)/LumaGreen
1454 * To avoid floating point arithmetic the fractional constants that
1455 * come out of the equations are represented as fixed point values
1456 * in the range 0...2^16. We also eliminate multiplications by
1457 * pre-calculating possible values indexed by Cb and Cr (this code
1458 * assumes conversion is being done for 8-bit samples).
1461 TIFFYCbCrToRGBInit(TIFFYCbCrToRGB
* ycbcr
, TIFF
* tif
)
1463 TIFFRGBValue
* clamptab
;
1467 clamptab
= (TIFFRGBValue
*)(
1468 (tidata_t
) ycbcr
+TIFFroundup(sizeof (TIFFYCbCrToRGB
), sizeof (long)));
1469 _TIFFmemset(clamptab
, 0, 256); /* v < 0 => 0 */
1470 ycbcr
->clamptab
= (clamptab
+= 256);
1471 for (i
= 0; i
< 256; i
++)
1473 _TIFFmemset(clamptab
+256, 255, 2*256); /* v > 255 => 255 */
1474 TIFFGetFieldDefaulted(tif
, TIFFTAG_YCBCRCOEFFICIENTS
, &coeffs
);
1475 _TIFFmemcpy(ycbcr
->coeffs
, coeffs
, 3*sizeof (float));
1476 { float f1
= 2-2*LumaRed
; int32 D1
= FIX(f1
);
1477 float f2
= LumaRed
*f1
/LumaGreen
; int32 D2
= -FIX(f2
);
1478 float f3
= 2-2*LumaBlue
; int32 D3
= FIX(f3
);
1479 float f4
= LumaBlue
*f3
/LumaGreen
; int32 D4
= -FIX(f4
);
1482 ycbcr
->Cr_r_tab
= (int*) (clamptab
+ 3*256);
1483 ycbcr
->Cb_b_tab
= ycbcr
->Cr_r_tab
+ 256;
1484 ycbcr
->Cr_g_tab
= (int32
*) (ycbcr
->Cb_b_tab
+ 256);
1485 ycbcr
->Cb_g_tab
= ycbcr
->Cr_g_tab
+ 256;
1487 * i is the actual input pixel value in the range 0..255
1488 * Cb and Cr values are in the range -128..127 (actually
1489 * they are in a range defined by the ReferenceBlackWhite
1490 * tag) so there is some range shifting to do here when
1491 * constructing tables indexed by the raw pixel data.
1493 * XXX handle ReferenceBlackWhite correctly to calculate
1494 * Cb/Cr values to use in constructing the tables.
1496 for (i
= 0, x
= -128; i
< 256; i
++, x
++) {
1497 ycbcr
->Cr_r_tab
[i
] = (int)((D1
*x
+ ONE_HALF
)>>SHIFT
);
1498 ycbcr
->Cb_b_tab
[i
] = (int)((D3
*x
+ ONE_HALF
)>>SHIFT
);
1499 ycbcr
->Cr_g_tab
[i
] = D2
*x
;
1500 ycbcr
->Cb_g_tab
[i
] = D4
*x
+ ONE_HALF
;
1511 static tileContigRoutine
1512 initYCbCrConversion(TIFFRGBAImage
* img
)
1516 if (img
->ycbcr
== NULL
) {
1517 img
->ycbcr
= (TIFFYCbCrToRGB
*) _TIFFmalloc(
1518 TIFFroundup(sizeof (TIFFYCbCrToRGB
), sizeof (long))
1519 + 4*256*sizeof (TIFFRGBValue
)
1520 + 2*256*sizeof (int)
1521 + 2*256*sizeof (int32
)
1523 if (img
->ycbcr
== NULL
) {
1524 TIFFError(TIFFFileName(img
->tif
),
1525 "No space for YCbCr->RGB conversion state");
1528 TIFFYCbCrToRGBInit(img
->ycbcr
, img
->tif
);
1532 TIFFGetFieldDefaulted(img
->tif
, TIFFTAG_YCBCRCOEFFICIENTS
, &coeffs
);
1533 if (_TIFFmemcmp(coeffs
, img
->ycbcr
->coeffs
, 3*sizeof (float)) != 0)
1534 TIFFYCbCrToRGBInit(img
->ycbcr
, img
->tif
);
1537 * The 6.0 spec says that subsampling must be
1538 * one of 1, 2, or 4, and that vertical subsampling
1539 * must always be <= horizontal subsampling; so
1540 * there are only a few possibilities and we just
1541 * enumerate the cases.
1543 TIFFGetFieldDefaulted(img
->tif
, TIFFTAG_YCBCRSUBSAMPLING
, &hs
, &vs
);
1544 switch ((hs
<<4)|vs
) {
1545 case 0x44: return (putcontig8bitYCbCr44tile
);
1546 case 0x42: return (putcontig8bitYCbCr42tile
);
1547 case 0x41: return (putcontig8bitYCbCr41tile
);
1548 case 0x22: return (putcontig8bitYCbCr22tile
);
1549 case 0x21: return (putcontig8bitYCbCr21tile
);
1550 case 0x11: return (putcontig8bitYCbCr11tile
);
1556 * Greyscale images with less than 8 bits/sample are handled
1557 * with a table to avoid lots of shifts and masks. The table
1558 * is setup so that put*bwtile (below) can retrieve 8/bitspersample
1559 * pixel values simply by indexing into the table with one
1563 makebwmap(TIFFRGBAImage
* img
)
1565 TIFFRGBValue
* Map
= img
->Map
;
1566 int bitspersample
= img
->bitspersample
;
1567 int nsamples
= 8 / bitspersample
;
1571 img
->BWmap
= (uint32
**) _TIFFmalloc(
1572 256*sizeof (uint32
*)+(256*nsamples
*sizeof(uint32
)));
1573 if (img
->BWmap
== NULL
) {
1574 TIFFError(TIFFFileName(img
->tif
), "No space for B&W mapping table");
1577 p
= (uint32
*)(img
->BWmap
+ 256);
1578 for (i
= 0; i
< 256; i
++) {
1581 switch (bitspersample
) {
1582 #define GREY(x) c = Map[x]; *p++ = PACK(c,c,c);
1613 * Construct a mapping table to convert from the range
1614 * of the data samples to [0,255] --for display. This
1615 * process also handles inverting B&W images when needed.
1618 setupMap(TIFFRGBAImage
* img
)
1622 range
= (int32
)((1L<<img
->bitspersample
)-1);
1623 img
->Map
= (TIFFRGBValue
*) _TIFFmalloc((range
+1) * sizeof (TIFFRGBValue
));
1624 if (img
->Map
== NULL
) {
1625 TIFFError(TIFFFileName(img
->tif
),
1626 "No space for photometric conversion table");
1629 if (img
->photometric
== PHOTOMETRIC_MINISWHITE
) {
1630 for (x
= 0; x
<= range
; x
++)
1631 img
->Map
[x
] = ((range
- x
) * 255) / range
;
1633 for (x
= 0; x
<= range
; x
++)
1634 img
->Map
[x
] = (x
* 255) / range
;
1636 if (img
->bitspersample
<= 8 &&
1637 (img
->photometric
== PHOTOMETRIC_MINISBLACK
||
1638 img
->photometric
== PHOTOMETRIC_MINISWHITE
)) {
1640 * Use photometric mapping table to construct
1641 * unpacking tables for samples <= 8 bits.
1643 if (!makebwmap(img
))
1645 /* no longer need Map, free it */
1646 _TIFFfree(img
->Map
), img
->Map
= NULL
;
1652 checkcmap(TIFFRGBAImage
* img
)
1654 uint16
* r
= img
->redcmap
;
1655 uint16
* g
= img
->greencmap
;
1656 uint16
* b
= img
->bluecmap
;
1657 long n
= 1L<<img
->bitspersample
;
1660 if (*r
++ >= 256 || *g
++ >= 256 || *b
++ >= 256)
1666 cvtcmap(TIFFRGBAImage
* img
)
1668 uint16
* r
= img
->redcmap
;
1669 uint16
* g
= img
->greencmap
;
1670 uint16
* b
= img
->bluecmap
;
1673 for (i
= (1L<<img
->bitspersample
)-1; i
>= 0; i
--) {
1674 #define CVT(x) ((uint16)((x)>>8))
1683 * Palette images with <= 8 bits/sample are handled
1684 * with a table to avoid lots of shifts and masks. The table
1685 * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
1686 * pixel values simply by indexing into the table with one
1690 makecmap(TIFFRGBAImage
* img
)
1692 int bitspersample
= img
->bitspersample
;
1693 int nsamples
= 8 / bitspersample
;
1694 uint16
* r
= img
->redcmap
;
1695 uint16
* g
= img
->greencmap
;
1696 uint16
* b
= img
->bluecmap
;
1700 img
->PALmap
= (uint32
**) _TIFFmalloc(
1701 256*sizeof (uint32
*)+(256*nsamples
*sizeof(uint32
)));
1702 if (img
->PALmap
== NULL
) {
1703 TIFFError(TIFFFileName(img
->tif
), "No space for Palette mapping table");
1706 p
= (uint32
*)(img
->PALmap
+ 256);
1707 for (i
= 0; i
< 256; i
++) {
1710 #define CMAP(x) c = x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff);
1711 switch (bitspersample
) {
1742 * Construct any mapping table used
1743 * by the associated put routine.
1746 buildMap(TIFFRGBAImage
* img
)
1748 switch (img
->photometric
) {
1749 case PHOTOMETRIC_RGB
:
1750 case PHOTOMETRIC_YCBCR
:
1751 case PHOTOMETRIC_SEPARATED
:
1752 if (img
->bitspersample
== 8)
1755 case PHOTOMETRIC_MINISBLACK
:
1756 case PHOTOMETRIC_MINISWHITE
:
1760 case PHOTOMETRIC_PALETTE
:
1762 * Convert 16-bit colormap to 8-bit (unless it looks
1763 * like an old-style 8-bit colormap).
1765 if (checkcmap(img
) == 16)
1768 TIFFWarning(TIFFFileName(img
->tif
), "Assuming 8-bit colormap");
1770 * Use mapping table and colormap to construct
1771 * unpacking tables for samples < 8 bits.
1773 if (img
->bitspersample
<= 8 && !makecmap(img
))
1781 * Select the appropriate conversion routine for packed data.
1784 pickTileContigCase(TIFFRGBAImage
* img
)
1786 tileContigRoutine put
= 0;
1788 if (buildMap(img
)) {
1789 switch (img
->photometric
) {
1790 case PHOTOMETRIC_RGB
:
1791 switch (img
->bitspersample
) {
1794 if (img
->alpha
== EXTRASAMPLE_ASSOCALPHA
)
1795 put
= putRGBAAcontig8bittile
;
1796 else if (img
->alpha
== EXTRASAMPLE_UNASSALPHA
)
1797 put
= putRGBUAcontig8bittile
;
1799 put
= putRGBcontig8bittile
;
1801 put
= putRGBcontig8bitMaptile
;
1804 put
= putRGBcontig16bittile
;
1806 if (img
->alpha
== EXTRASAMPLE_ASSOCALPHA
)
1807 put
= putRGBAAcontig16bittile
;
1808 else if (img
->alpha
== EXTRASAMPLE_UNASSALPHA
)
1809 put
= putRGBUAcontig16bittile
;
1814 case PHOTOMETRIC_SEPARATED
:
1815 if (img
->bitspersample
== 8) {
1817 put
= putRGBcontig8bitCMYKtile
;
1819 put
= putRGBcontig8bitCMYKMaptile
;
1822 case PHOTOMETRIC_PALETTE
:
1823 switch (img
->bitspersample
) {
1824 case 8: put
= put8bitcmaptile
; break;
1825 case 4: put
= put4bitcmaptile
; break;
1826 case 2: put
= put2bitcmaptile
; break;
1827 case 1: put
= put1bitcmaptile
; break;
1830 case PHOTOMETRIC_MINISWHITE
:
1831 case PHOTOMETRIC_MINISBLACK
:
1832 switch (img
->bitspersample
) {
1833 case 8: put
= putgreytile
; break;
1834 case 4: put
= put4bitbwtile
; break;
1835 case 2: put
= put2bitbwtile
; break;
1836 case 1: put
= put1bitbwtile
; break;
1839 case PHOTOMETRIC_YCBCR
:
1840 if (img
->bitspersample
== 8)
1841 put
= initYCbCrConversion(img
);
1845 return ((img
->put
.contig
= put
) != 0);
1849 * Select the appropriate conversion routine for unpacked data.
1851 * NB: we assume that unpacked single channel data is directed
1852 * to the "packed routines.
1855 pickTileSeparateCase(TIFFRGBAImage
* img
)
1857 tileSeparateRoutine put
= 0;
1859 if (buildMap(img
)) {
1860 switch (img
->photometric
) {
1861 case PHOTOMETRIC_RGB
:
1862 switch (img
->bitspersample
) {
1865 if (img
->alpha
== EXTRASAMPLE_ASSOCALPHA
)
1866 put
= putRGBAAseparate8bittile
;
1867 else if (img
->alpha
== EXTRASAMPLE_UNASSALPHA
)
1868 put
= putRGBUAseparate8bittile
;
1870 put
= putRGBseparate8bittile
;
1872 put
= putRGBseparate8bitMaptile
;
1875 put
= putRGBseparate16bittile
;
1877 if (img
->alpha
== EXTRASAMPLE_ASSOCALPHA
)
1878 put
= putRGBAAseparate16bittile
;
1879 else if (img
->alpha
== EXTRASAMPLE_UNASSALPHA
)
1880 put
= putRGBUAseparate16bittile
;
1887 return ((img
->put
.separate
= put
) != 0);
1891 * Read a whole strip off data from the file, and convert to RGBA form.
1892 * If this is the last strip, then it will only contain the portion of
1893 * the strip that is actually within the image space. The result is
1894 * organized in bottom to top form.
1899 TIFFReadRGBAStrip(TIFF
* tif
, uint32 row
, uint32
* raster
)
1905 uint32 rowsperstrip
, rows_to_read
;
1907 if( TIFFIsTiled( tif
) )
1909 TIFFError(TIFFFileName(tif
),
1910 "Can't use TIFFReadRGBAStrip() with tiled file.");
1914 TIFFGetFieldDefaulted(tif
, TIFFTAG_ROWSPERSTRIP
, &rowsperstrip
);
1915 if( (row
% rowsperstrip
) != 0 )
1917 TIFFError(TIFFFileName(tif
),
1918 "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
1922 if (TIFFRGBAImageBegin(&img
, tif
, 0, emsg
)) {
1924 img
.row_offset
= row
;
1927 if( row
+ rowsperstrip
> img
.height
)
1928 rows_to_read
= img
.height
- row
;
1930 rows_to_read
= rowsperstrip
;
1932 ok
= TIFFRGBAImageGet(&img
, raster
, img
.width
, rows_to_read
);
1934 TIFFRGBAImageEnd(&img
);
1936 TIFFError(TIFFFileName(tif
), emsg
);
1944 * Read a whole tile off data from the file, and convert to RGBA form.
1945 * The returned RGBA data is organized from bottom to top of tile,
1946 * and may include zeroed areas if the tile extends off the image.
1950 TIFFReadRGBATile(TIFF
* tif
, uint32 col
, uint32 row
, uint32
* raster
)
1956 uint32 tile_xsize
, tile_ysize
;
1957 uint32 read_xsize
, read_ysize
;
1961 * Verify that our request is legal - on a tile file, and on a
1965 if( !TIFFIsTiled( tif
) )
1967 TIFFError(TIFFFileName(tif
),
1968 "Can't use TIFFReadRGBATile() with stripped file.");
1972 TIFFGetFieldDefaulted(tif
, TIFFTAG_TILEWIDTH
, &tile_xsize
);
1973 TIFFGetFieldDefaulted(tif
, TIFFTAG_TILELENGTH
, &tile_ysize
);
1974 if( (col
% tile_xsize
) != 0 || (row
% tile_ysize
) != 0 )
1976 TIFFError(TIFFFileName(tif
),
1977 "Row/col passed to TIFFReadRGBATile() must be top"
1978 "left corner of a tile.");
1983 * Setup the RGBA reader.
1986 if ( !TIFFRGBAImageBegin(&img
, tif
, 0, emsg
)) {
1987 TIFFError(TIFFFileName(tif
), emsg
);
1992 * The TIFFRGBAImageGet() function doesn't allow us to get off the
1993 * edge of the image, even to fill an otherwise valid tile. So we
1994 * figure out how much we can read, and fix up the tile buffer to
1995 * a full tile configuration afterwards.
1998 if( row
+ tile_ysize
> img
.height
)
1999 read_ysize
= img
.height
- row
;
2001 read_ysize
= tile_ysize
;
2003 if( col
+ tile_xsize
> img
.width
)
2004 read_xsize
= img
.width
- col
;
2006 read_xsize
= tile_xsize
;
2009 * Read the chunk of imagery.
2012 img
.row_offset
= row
;
2013 img
.col_offset
= col
;
2015 ok
= TIFFRGBAImageGet(&img
, raster
, read_xsize
, read_ysize
);
2017 TIFFRGBAImageEnd(&img
);
2020 * If our read was incomplete we will need to fix up the tile by
2021 * shifting the data around as if a full tile of data is being returned.
2023 * This is all the more complicated because the image is organized in
2024 * bottom to top format.
2027 if( read_xsize
== tile_xsize
&& read_ysize
== tile_ysize
)
2030 for( i_row
= 0; i_row
< read_ysize
; i_row
++ )
2032 _TIFFmemcpy( raster
+ (tile_ysize
- i_row
- 1) * tile_xsize
,
2033 raster
+ (read_ysize
- i_row
- 1) * read_xsize
,
2034 read_xsize
* sizeof(uint32
) );
2035 _TIFFmemset( raster
+ (tile_ysize
- i_row
- 1) * tile_xsize
+read_xsize
,
2036 0, sizeof(uint32
) * (tile_xsize
- read_xsize
) );
2039 for( i_row
= read_ysize
; i_row
< tile_ysize
; i_row
++ )
2041 _TIFFmemset( raster
+ (tile_ysize
- i_row
- 1) * tile_xsize
,
2042 0, sizeof(uint32
) * tile_xsize
);