/src/freeimage-svn/FreeImage/trunk/Source/LibTIFF4/tif_getimage.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright (c) 1991-1997 Sam Leffler  | 
3  |  |  * Copyright (c) 1991-1997 Silicon Graphics, Inc.  | 
4  |  |  *  | 
5  |  |  * Permission to use, copy, modify, distribute, and sell this software and  | 
6  |  |  * its documentation for any purpose is hereby granted without fee, provided  | 
7  |  |  * that (i) the above copyright notices and this permission notice appear in  | 
8  |  |  * all copies of the software and related documentation, and (ii) the names of  | 
9  |  |  * Sam Leffler and Silicon Graphics may not be used in any advertising or  | 
10  |  |  * publicity relating to the software without the specific, prior written  | 
11  |  |  * permission of Sam Leffler and Silicon Graphics.  | 
12  |  |  *  | 
13  |  |  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,  | 
14  |  |  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY  | 
15  |  |  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  | 
16  |  |  *  | 
17  |  |  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR  | 
18  |  |  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,  | 
19  |  |  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,  | 
20  |  |  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF  | 
21  |  |  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE  | 
22  |  |  * OF THIS SOFTWARE.  | 
23  |  |  */  | 
24  |  |  | 
25  |  | /*  | 
26  |  |  * TIFF Library  | 
27  |  |  *  | 
28  |  |  * Read and return a packed RGBA image.  | 
29  |  |  */  | 
30  |  | #include "tiffiop.h"  | 
31  |  | #include <limits.h>  | 
32  |  | #include <stdio.h>  | 
33  |  |  | 
34  |  | static int gtTileContig(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);  | 
35  |  | static int gtTileSeparate(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);  | 
36  |  | static int gtStripContig(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);  | 
37  |  | static int gtStripSeparate(TIFFRGBAImage *, uint32_t *, uint32_t, uint32_t);  | 
38  |  | static int PickContigCase(TIFFRGBAImage *);  | 
39  |  | static int PickSeparateCase(TIFFRGBAImage *);  | 
40  |  |  | 
41  |  | static int BuildMapUaToAa(TIFFRGBAImage *img);  | 
42  |  | static int BuildMapBitdepth16To8(TIFFRGBAImage *img);  | 
43  |  |  | 
44  |  | static const char photoTag[] = "PhotometricInterpretation";  | 
45  |  |  | 
46  |  | /*  | 
47  |  |  * Helper constants used in Orientation tag handling  | 
48  |  |  */  | 
49  | 0  | #define FLIP_VERTICALLY 0x01  | 
50  | 0  | #define FLIP_HORIZONTALLY 0x02  | 
51  |  |  | 
52  | 0  | #define EMSG_BUF_SIZE 1024  | 
53  |  |  | 
54  |  | /*  | 
55  |  |  * Color conversion constants. We will define display types here.  | 
56  |  |  */  | 
57  |  |  | 
58  |  | static const TIFFDisplay display_sRGB = { | 
59  |  |     {/* XYZ -> luminance matrix */ | 
60  |  |      {3.2410F, -1.5374F, -0.4986F}, | 
61  |  |      {-0.9692F, 1.8760F, 0.0416F}, | 
62  |  |      {0.0556F, -0.2040F, 1.0570F}}, | 
63  |  |     100.0F,  | 
64  |  |     100.0F,  | 
65  |  |     100.0F, /* Light o/p for reference white */  | 
66  |  |     255,  | 
67  |  |     255,  | 
68  |  |     255, /* Pixel values for ref. white */  | 
69  |  |     1.0F,  | 
70  |  |     1.0F,  | 
71  |  |     1.0F, /* Residual light o/p for black pixel */  | 
72  |  |     2.4F,  | 
73  |  |     2.4F,  | 
74  |  |     2.4F, /* Gamma values for the three guns */  | 
75  |  | };  | 
76  |  |  | 
77  |  | /*  | 
78  |  |  * Check the image to see if TIFFReadRGBAImage can deal with it.  | 
79  |  |  * 1/0 is returned according to whether or not the image can  | 
80  |  |  * be handled.  If 0 is returned, emsg contains the reason  | 
81  |  |  * why it is being rejected.  | 
82  |  |  */  | 
83  |  | int TIFFRGBAImageOK(TIFF *tif, char emsg[EMSG_BUF_SIZE])  | 
84  | 0  | { | 
85  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
86  | 0  |     uint16_t photometric;  | 
87  | 0  |     int colorchannels;  | 
88  |  | 
  | 
89  | 0  |     if (!tif->tif_decodestatus)  | 
90  | 0  |     { | 
91  | 0  |         snprintf(emsg, EMSG_BUF_SIZE,  | 
92  | 0  |                  "Sorry, requested compression method is not configured");  | 
93  | 0  |         return (0);  | 
94  | 0  |     }  | 
95  | 0  |     switch (td->td_bitspersample)  | 
96  | 0  |     { | 
97  | 0  |         case 1:  | 
98  | 0  |         case 2:  | 
99  | 0  |         case 4:  | 
100  | 0  |         case 8:  | 
101  | 0  |         case 16:  | 
102  | 0  |             break;  | 
103  | 0  |         default:  | 
104  | 0  |             snprintf(emsg, EMSG_BUF_SIZE,  | 
105  | 0  |                      "Sorry, can not handle images with %" PRIu16  | 
106  | 0  |                      "-bit samples",  | 
107  | 0  |                      td->td_bitspersample);  | 
108  | 0  |             return (0);  | 
109  | 0  |     }  | 
110  | 0  |     if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP)  | 
111  | 0  |     { | 
112  | 0  |         snprintf(  | 
113  | 0  |             emsg, EMSG_BUF_SIZE,  | 
114  | 0  |             "Sorry, can not handle images with IEEE floating-point samples");  | 
115  | 0  |         return (0);  | 
116  | 0  |     }  | 
117  | 0  |     colorchannels = td->td_samplesperpixel - td->td_extrasamples;  | 
118  | 0  |     if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric))  | 
119  | 0  |     { | 
120  | 0  |         switch (colorchannels)  | 
121  | 0  |         { | 
122  | 0  |             case 1:  | 
123  | 0  |                 photometric = PHOTOMETRIC_MINISBLACK;  | 
124  | 0  |                 break;  | 
125  | 0  |             case 3:  | 
126  | 0  |                 photometric = PHOTOMETRIC_RGB;  | 
127  | 0  |                 break;  | 
128  | 0  |             default:  | 
129  | 0  |                 snprintf(emsg, EMSG_BUF_SIZE, "Missing needed %s tag",  | 
130  | 0  |                          photoTag);  | 
131  | 0  |                 return (0);  | 
132  | 0  |         }  | 
133  | 0  |     }  | 
134  | 0  |     switch (photometric)  | 
135  | 0  |     { | 
136  | 0  |         case PHOTOMETRIC_MINISWHITE:  | 
137  | 0  |         case PHOTOMETRIC_MINISBLACK:  | 
138  | 0  |         case PHOTOMETRIC_PALETTE:  | 
139  | 0  |             if (td->td_planarconfig == PLANARCONFIG_CONTIG &&  | 
140  | 0  |                 td->td_samplesperpixel != 1 && td->td_bitspersample < 8)  | 
141  | 0  |             { | 
142  | 0  |                 snprintf(  | 
143  | 0  |                     emsg, EMSG_BUF_SIZE,  | 
144  | 0  |                     "Sorry, can not handle contiguous data with %s=%" PRIu16  | 
145  | 0  |                     ", "  | 
146  | 0  |                     "and %s=%" PRIu16 " and Bits/Sample=%" PRIu16 "",  | 
147  | 0  |                     photoTag, photometric, "Samples/pixel",  | 
148  | 0  |                     td->td_samplesperpixel, td->td_bitspersample);  | 
149  | 0  |                 return (0);  | 
150  | 0  |             }  | 
151  |  |             /*  | 
152  |  |              * We should likely validate that any extra samples are either  | 
153  |  |              * to be ignored, or are alpha, and if alpha we should try to use  | 
154  |  |              * them.  But for now we won't bother with this.  | 
155  |  |              */  | 
156  | 0  |             break;  | 
157  | 0  |         case PHOTOMETRIC_YCBCR:  | 
158  |  |             /*  | 
159  |  |              * TODO: if at all meaningful and useful, make more complete  | 
160  |  |              * support check here, or better still, refactor to let supporting  | 
161  |  |              * code decide whether there is support and what meaningful  | 
162  |  |              * error to return  | 
163  |  |              */  | 
164  | 0  |             break;  | 
165  | 0  |         case PHOTOMETRIC_RGB:  | 
166  | 0  |             if (colorchannels < 3)  | 
167  | 0  |             { | 
168  | 0  |                 snprintf(emsg, EMSG_BUF_SIZE,  | 
169  | 0  |                          "Sorry, can not handle RGB image with %s=%d",  | 
170  | 0  |                          "Color channels", colorchannels);  | 
171  | 0  |                 return (0);  | 
172  | 0  |             }  | 
173  | 0  |             break;  | 
174  | 0  |         case PHOTOMETRIC_SEPARATED:  | 
175  | 0  |         { | 
176  | 0  |             uint16_t inkset;  | 
177  | 0  |             TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);  | 
178  | 0  |             if (inkset != INKSET_CMYK)  | 
179  | 0  |             { | 
180  | 0  |                 snprintf(emsg, EMSG_BUF_SIZE,  | 
181  | 0  |                          "Sorry, can not handle separated image with %s=%d",  | 
182  | 0  |                          "InkSet", inkset);  | 
183  | 0  |                 return 0;  | 
184  | 0  |             }  | 
185  | 0  |             if (td->td_samplesperpixel < 4)  | 
186  | 0  |             { | 
187  | 0  |                 snprintf(  | 
188  | 0  |                     emsg, EMSG_BUF_SIZE,  | 
189  | 0  |                     "Sorry, can not handle separated image with %s=%" PRIu16,  | 
190  | 0  |                     "Samples/pixel", td->td_samplesperpixel);  | 
191  | 0  |                 return 0;  | 
192  | 0  |             }  | 
193  | 0  |             break;  | 
194  | 0  |         }  | 
195  | 0  |         case PHOTOMETRIC_LOGL:  | 
196  | 0  |             if (td->td_compression != COMPRESSION_SGILOG)  | 
197  | 0  |             { | 
198  | 0  |                 snprintf(emsg, EMSG_BUF_SIZE,  | 
199  | 0  |                          "Sorry, LogL data must have %s=%d", "Compression",  | 
200  | 0  |                          COMPRESSION_SGILOG);  | 
201  | 0  |                 return (0);  | 
202  | 0  |             }  | 
203  | 0  |             break;  | 
204  | 0  |         case PHOTOMETRIC_LOGLUV:  | 
205  | 0  |             if (td->td_compression != COMPRESSION_SGILOG &&  | 
206  | 0  |                 td->td_compression != COMPRESSION_SGILOG24)  | 
207  | 0  |             { | 
208  | 0  |                 snprintf(emsg, EMSG_BUF_SIZE,  | 
209  | 0  |                          "Sorry, LogLuv data must have %s=%d or %d",  | 
210  | 0  |                          "Compression", COMPRESSION_SGILOG,  | 
211  | 0  |                          COMPRESSION_SGILOG24);  | 
212  | 0  |                 return (0);  | 
213  | 0  |             }  | 
214  | 0  |             if (td->td_planarconfig != PLANARCONFIG_CONTIG)  | 
215  | 0  |             { | 
216  | 0  |                 snprintf(emsg, EMSG_BUF_SIZE,  | 
217  | 0  |                          "Sorry, can not handle LogLuv images with %s=%" PRIu16,  | 
218  | 0  |                          "Planarconfiguration", td->td_planarconfig);  | 
219  | 0  |                 return (0);  | 
220  | 0  |             }  | 
221  | 0  |             if (td->td_samplesperpixel != 3 || colorchannels != 3)  | 
222  | 0  |             { | 
223  | 0  |                 snprintf(emsg, EMSG_BUF_SIZE,  | 
224  | 0  |                          "Sorry, can not handle image with %s=%" PRIu16  | 
225  | 0  |                          ", %s=%d",  | 
226  | 0  |                          "Samples/pixel", td->td_samplesperpixel,  | 
227  | 0  |                          "colorchannels", colorchannels);  | 
228  | 0  |                 return 0;  | 
229  | 0  |             }  | 
230  | 0  |             break;  | 
231  | 0  |         case PHOTOMETRIC_CIELAB:  | 
232  | 0  |             if (td->td_samplesperpixel != 3 || colorchannels != 3 ||  | 
233  | 0  |                 (td->td_bitspersample != 8 && td->td_bitspersample != 16))  | 
234  | 0  |             { | 
235  | 0  |                 snprintf(emsg, EMSG_BUF_SIZE,  | 
236  | 0  |                          "Sorry, can not handle image with %s=%" PRIu16  | 
237  | 0  |                          ", %s=%d and %s=%" PRIu16,  | 
238  | 0  |                          "Samples/pixel", td->td_samplesperpixel,  | 
239  | 0  |                          "colorchannels", colorchannels, "Bits/sample",  | 
240  | 0  |                          td->td_bitspersample);  | 
241  | 0  |                 return 0;  | 
242  | 0  |             }  | 
243  | 0  |             break;  | 
244  | 0  |         default:  | 
245  | 0  |             snprintf(emsg, EMSG_BUF_SIZE,  | 
246  | 0  |                      "Sorry, can not handle image with %s=%" PRIu16, photoTag,  | 
247  | 0  |                      photometric);  | 
248  | 0  |             return (0);  | 
249  | 0  |     }  | 
250  | 0  |     return (1);  | 
251  | 0  | }  | 
252  |  |  | 
253  |  | void TIFFRGBAImageEnd(TIFFRGBAImage *img)  | 
254  | 0  | { | 
255  | 0  |     if (img->Map)  | 
256  | 0  |     { | 
257  | 0  |         _TIFFfreeExt(img->tif, img->Map);  | 
258  | 0  |         img->Map = NULL;  | 
259  | 0  |     }  | 
260  | 0  |     if (img->BWmap)  | 
261  | 0  |     { | 
262  | 0  |         _TIFFfreeExt(img->tif, img->BWmap);  | 
263  | 0  |         img->BWmap = NULL;  | 
264  | 0  |     }  | 
265  | 0  |     if (img->PALmap)  | 
266  | 0  |     { | 
267  | 0  |         _TIFFfreeExt(img->tif, img->PALmap);  | 
268  | 0  |         img->PALmap = NULL;  | 
269  | 0  |     }  | 
270  | 0  |     if (img->ycbcr)  | 
271  | 0  |     { | 
272  | 0  |         _TIFFfreeExt(img->tif, img->ycbcr);  | 
273  | 0  |         img->ycbcr = NULL;  | 
274  | 0  |     }  | 
275  | 0  |     if (img->cielab)  | 
276  | 0  |     { | 
277  | 0  |         _TIFFfreeExt(img->tif, img->cielab);  | 
278  | 0  |         img->cielab = NULL;  | 
279  | 0  |     }  | 
280  | 0  |     if (img->UaToAa)  | 
281  | 0  |     { | 
282  | 0  |         _TIFFfreeExt(img->tif, img->UaToAa);  | 
283  | 0  |         img->UaToAa = NULL;  | 
284  | 0  |     }  | 
285  | 0  |     if (img->Bitdepth16To8)  | 
286  | 0  |     { | 
287  | 0  |         _TIFFfreeExt(img->tif, img->Bitdepth16To8);  | 
288  | 0  |         img->Bitdepth16To8 = NULL;  | 
289  | 0  |     }  | 
290  |  | 
  | 
291  | 0  |     if (img->redcmap)  | 
292  | 0  |     { | 
293  | 0  |         _TIFFfreeExt(img->tif, img->redcmap);  | 
294  | 0  |         _TIFFfreeExt(img->tif, img->greencmap);  | 
295  | 0  |         _TIFFfreeExt(img->tif, img->bluecmap);  | 
296  | 0  |         img->redcmap = img->greencmap = img->bluecmap = NULL;  | 
297  | 0  |     }  | 
298  | 0  | }  | 
299  |  |  | 
300  |  | static int isCCITTCompression(TIFF *tif)  | 
301  | 0  | { | 
302  | 0  |     uint16_t compress;  | 
303  | 0  |     TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);  | 
304  | 0  |     return (compress == COMPRESSION_CCITTFAX3 ||  | 
305  | 0  |             compress == COMPRESSION_CCITTFAX4 ||  | 
306  | 0  |             compress == COMPRESSION_CCITTRLE ||  | 
307  | 0  |             compress == COMPRESSION_CCITTRLEW);  | 
308  | 0  | }  | 
309  |  |  | 
310  |  | int TIFFRGBAImageBegin(TIFFRGBAImage *img, TIFF *tif, int stop,  | 
311  |  |                        char emsg[EMSG_BUF_SIZE])  | 
312  | 0  | { | 
313  | 0  |     uint16_t *sampleinfo;  | 
314  | 0  |     uint16_t extrasamples;  | 
315  | 0  |     uint16_t planarconfig;  | 
316  | 0  |     uint16_t compress;  | 
317  | 0  |     int colorchannels;  | 
318  | 0  |     uint16_t *red_orig, *green_orig, *blue_orig;  | 
319  | 0  |     int n_color;  | 
320  |  | 
  | 
321  | 0  |     if (!TIFFRGBAImageOK(tif, emsg))  | 
322  | 0  |         return 0;  | 
323  |  |  | 
324  |  |     /* Initialize to normal values */  | 
325  | 0  |     img->row_offset = 0;  | 
326  | 0  |     img->col_offset = 0;  | 
327  | 0  |     img->redcmap = NULL;  | 
328  | 0  |     img->greencmap = NULL;  | 
329  | 0  |     img->bluecmap = NULL;  | 
330  | 0  |     img->Map = NULL;  | 
331  | 0  |     img->BWmap = NULL;  | 
332  | 0  |     img->PALmap = NULL;  | 
333  | 0  |     img->ycbcr = NULL;  | 
334  | 0  |     img->cielab = NULL;  | 
335  | 0  |     img->UaToAa = NULL;  | 
336  | 0  |     img->Bitdepth16To8 = NULL;  | 
337  | 0  |     img->req_orientation = ORIENTATION_BOTLEFT; /* It is the default */  | 
338  |  | 
  | 
339  | 0  |     img->tif = tif;  | 
340  | 0  |     img->stoponerr = stop;  | 
341  | 0  |     TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);  | 
342  | 0  |     switch (img->bitspersample)  | 
343  | 0  |     { | 
344  | 0  |         case 1:  | 
345  | 0  |         case 2:  | 
346  | 0  |         case 4:  | 
347  | 0  |         case 8:  | 
348  | 0  |         case 16:  | 
349  | 0  |             break;  | 
350  | 0  |         default:  | 
351  | 0  |             snprintf(emsg, EMSG_BUF_SIZE,  | 
352  | 0  |                      "Sorry, can not handle images with %" PRIu16  | 
353  | 0  |                      "-bit samples",  | 
354  | 0  |                      img->bitspersample);  | 
355  | 0  |             goto fail_return;  | 
356  | 0  |     }  | 
357  | 0  |     img->alpha = 0;  | 
358  | 0  |     TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);  | 
359  | 0  |     TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &extrasamples,  | 
360  | 0  |                           &sampleinfo);  | 
361  | 0  |     if (extrasamples >= 1)  | 
362  | 0  |     { | 
363  | 0  |         switch (sampleinfo[0])  | 
364  | 0  |         { | 
365  | 0  |             case EXTRASAMPLE_UNSPECIFIED: /* Workaround for some images without  | 
366  |  |                                            */  | 
367  | 0  |                 if (img->samplesperpixel >  | 
368  | 0  |                     3) /* correct info about alpha channel */  | 
369  | 0  |                     img->alpha = EXTRASAMPLE_ASSOCALPHA;  | 
370  | 0  |                 break;  | 
371  | 0  |             case EXTRASAMPLE_ASSOCALPHA: /* data is pre-multiplied */  | 
372  | 0  |             case EXTRASAMPLE_UNASSALPHA: /* data is not pre-multiplied */  | 
373  | 0  |                 img->alpha = sampleinfo[0];  | 
374  | 0  |                 break;  | 
375  | 0  |         }  | 
376  | 0  |     }  | 
377  |  |  | 
378  | 0  | #ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA  | 
379  | 0  |     if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))  | 
380  | 0  |         img->photometric = PHOTOMETRIC_MINISWHITE;  | 
381  |  | 
  | 
382  | 0  |     if (extrasamples == 0 && img->samplesperpixel == 4 &&  | 
383  | 0  |         img->photometric == PHOTOMETRIC_RGB)  | 
384  | 0  |     { | 
385  | 0  |         img->alpha = EXTRASAMPLE_ASSOCALPHA;  | 
386  | 0  |         extrasamples = 1;  | 
387  | 0  |     }  | 
388  | 0  | #endif  | 
389  |  | 
  | 
390  | 0  |     colorchannels = img->samplesperpixel - extrasamples;  | 
391  | 0  |     TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);  | 
392  | 0  |     TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);  | 
393  | 0  |     if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))  | 
394  | 0  |     { | 
395  | 0  |         switch (colorchannels)  | 
396  | 0  |         { | 
397  | 0  |             case 1:  | 
398  | 0  |                 if (isCCITTCompression(tif))  | 
399  | 0  |                     img->photometric = PHOTOMETRIC_MINISWHITE;  | 
400  | 0  |                 else  | 
401  | 0  |                     img->photometric = PHOTOMETRIC_MINISBLACK;  | 
402  | 0  |                 break;  | 
403  | 0  |             case 3:  | 
404  | 0  |                 img->photometric = PHOTOMETRIC_RGB;  | 
405  | 0  |                 break;  | 
406  | 0  |             default:  | 
407  | 0  |                 snprintf(emsg, EMSG_BUF_SIZE, "Missing needed %s tag",  | 
408  | 0  |                          photoTag);  | 
409  | 0  |                 goto fail_return;  | 
410  | 0  |         }  | 
411  | 0  |     }  | 
412  | 0  |     switch (img->photometric)  | 
413  | 0  |     { | 
414  | 0  |         case PHOTOMETRIC_PALETTE:  | 
415  | 0  |             if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &red_orig, &green_orig,  | 
416  | 0  |                               &blue_orig))  | 
417  | 0  |             { | 
418  | 0  |                 snprintf(emsg, EMSG_BUF_SIZE,  | 
419  | 0  |                          "Missing required \"Colormap\" tag");  | 
420  | 0  |                 goto fail_return;  | 
421  | 0  |             }  | 
422  |  |  | 
423  |  |             /* copy the colormaps so we can modify them */  | 
424  | 0  |             n_color = (1U << img->bitspersample);  | 
425  | 0  |             img->redcmap =  | 
426  | 0  |                 (uint16_t *)_TIFFmallocExt(tif, sizeof(uint16_t) * n_color);  | 
427  | 0  |             img->greencmap =  | 
428  | 0  |                 (uint16_t *)_TIFFmallocExt(tif, sizeof(uint16_t) * n_color);  | 
429  | 0  |             img->bluecmap =  | 
430  | 0  |                 (uint16_t *)_TIFFmallocExt(tif, sizeof(uint16_t) * n_color);  | 
431  | 0  |             if (!img->redcmap || !img->greencmap || !img->bluecmap)  | 
432  | 0  |             { | 
433  | 0  |                 snprintf(emsg, EMSG_BUF_SIZE,  | 
434  | 0  |                          "Out of memory for colormap copy");  | 
435  | 0  |                 goto fail_return;  | 
436  | 0  |             }  | 
437  |  |  | 
438  | 0  |             _TIFFmemcpy(img->redcmap, red_orig, n_color * 2);  | 
439  | 0  |             _TIFFmemcpy(img->greencmap, green_orig, n_color * 2);  | 
440  | 0  |             _TIFFmemcpy(img->bluecmap, blue_orig, n_color * 2);  | 
441  |  |  | 
442  |  |             /* fall through... */  | 
443  | 0  |         case PHOTOMETRIC_MINISWHITE:  | 
444  | 0  |         case PHOTOMETRIC_MINISBLACK:  | 
445  | 0  |             if (planarconfig == PLANARCONFIG_CONTIG &&  | 
446  | 0  |                 img->samplesperpixel != 1 && img->bitspersample < 8)  | 
447  | 0  |             { | 
448  | 0  |                 snprintf(  | 
449  | 0  |                     emsg, EMSG_BUF_SIZE,  | 
450  | 0  |                     "Sorry, can not handle contiguous data with %s=%" PRIu16  | 
451  | 0  |                     ", "  | 
452  | 0  |                     "and %s=%" PRIu16 " and Bits/Sample=%" PRIu16,  | 
453  | 0  |                     photoTag, img->photometric, "Samples/pixel",  | 
454  | 0  |                     img->samplesperpixel, img->bitspersample);  | 
455  | 0  |                 goto fail_return;  | 
456  | 0  |             }  | 
457  | 0  |             break;  | 
458  | 0  |         case PHOTOMETRIC_YCBCR:  | 
459  |  |             /* It would probably be nice to have a reality check here. */  | 
460  | 0  |             if (planarconfig == PLANARCONFIG_CONTIG)  | 
461  |  |                 /* can rely on libjpeg to convert to RGB */  | 
462  |  |                 /* XXX should restore current state on exit */  | 
463  | 0  |                 switch (compress)  | 
464  | 0  |                 { | 
465  | 0  |                     case COMPRESSION_JPEG:  | 
466  |  |                         /*  | 
467  |  |                          * TODO: when complete tests verify complete  | 
468  |  |                          * desubsampling and YCbCr handling, remove use of  | 
469  |  |                          * TIFFTAG_JPEGCOLORMODE in favor of tif_getimage.c  | 
470  |  |                          * native handling  | 
471  |  |                          */  | 
472  | 0  |                         TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE,  | 
473  | 0  |                                      JPEGCOLORMODE_RGB);  | 
474  | 0  |                         img->photometric = PHOTOMETRIC_RGB;  | 
475  | 0  |                         break;  | 
476  | 0  |                     default:  | 
477  | 0  |                         /* do nothing */;  | 
478  | 0  |                         break;  | 
479  | 0  |                 }  | 
480  |  |             /*  | 
481  |  |              * TODO: if at all meaningful and useful, make more complete  | 
482  |  |              * support check here, or better still, refactor to let supporting  | 
483  |  |              * code decide whether there is support and what meaningful  | 
484  |  |              * error to return  | 
485  |  |              */  | 
486  | 0  |             break;  | 
487  | 0  |         case PHOTOMETRIC_RGB:  | 
488  | 0  |             if (colorchannels < 3)  | 
489  | 0  |             { | 
490  | 0  |                 snprintf(emsg, EMSG_BUF_SIZE,  | 
491  | 0  |                          "Sorry, can not handle RGB image with %s=%d",  | 
492  | 0  |                          "Color channels", colorchannels);  | 
493  | 0  |                 goto fail_return;  | 
494  | 0  |             }  | 
495  | 0  |             break;  | 
496  | 0  |         case PHOTOMETRIC_SEPARATED:  | 
497  | 0  |         { | 
498  | 0  |             uint16_t inkset;  | 
499  | 0  |             TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);  | 
500  | 0  |             if (inkset != INKSET_CMYK)  | 
501  | 0  |             { | 
502  | 0  |                 snprintf(  | 
503  | 0  |                     emsg, EMSG_BUF_SIZE,  | 
504  | 0  |                     "Sorry, can not handle separated image with %s=%" PRIu16,  | 
505  | 0  |                     "InkSet", inkset);  | 
506  | 0  |                 goto fail_return;  | 
507  | 0  |             }  | 
508  | 0  |             if (img->samplesperpixel < 4)  | 
509  | 0  |             { | 
510  | 0  |                 snprintf(  | 
511  | 0  |                     emsg, EMSG_BUF_SIZE,  | 
512  | 0  |                     "Sorry, can not handle separated image with %s=%" PRIu16,  | 
513  | 0  |                     "Samples/pixel", img->samplesperpixel);  | 
514  | 0  |                 goto fail_return;  | 
515  | 0  |             }  | 
516  | 0  |         }  | 
517  | 0  |         break;  | 
518  | 0  |         case PHOTOMETRIC_LOGL:  | 
519  | 0  |             if (compress != COMPRESSION_SGILOG)  | 
520  | 0  |             { | 
521  | 0  |                 snprintf(emsg, EMSG_BUF_SIZE,  | 
522  | 0  |                          "Sorry, LogL data must have %s=%d", "Compression",  | 
523  | 0  |                          COMPRESSION_SGILOG);  | 
524  | 0  |                 goto fail_return;  | 
525  | 0  |             }  | 
526  | 0  |             TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);  | 
527  | 0  |             img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */  | 
528  | 0  |             img->bitspersample = 8;  | 
529  | 0  |             break;  | 
530  | 0  |         case PHOTOMETRIC_LOGLUV:  | 
531  | 0  |             if (compress != COMPRESSION_SGILOG &&  | 
532  | 0  |                 compress != COMPRESSION_SGILOG24)  | 
533  | 0  |             { | 
534  | 0  |                 snprintf(emsg, EMSG_BUF_SIZE,  | 
535  | 0  |                          "Sorry, LogLuv data must have %s=%d or %d",  | 
536  | 0  |                          "Compression", COMPRESSION_SGILOG,  | 
537  | 0  |                          COMPRESSION_SGILOG24);  | 
538  | 0  |                 goto fail_return;  | 
539  | 0  |             }  | 
540  | 0  |             if (planarconfig != PLANARCONFIG_CONTIG)  | 
541  | 0  |             { | 
542  | 0  |                 snprintf(emsg, EMSG_BUF_SIZE,  | 
543  | 0  |                          "Sorry, can not handle LogLuv images with %s=%" PRIu16,  | 
544  | 0  |                          "Planarconfiguration", planarconfig);  | 
545  | 0  |                 return (0);  | 
546  | 0  |             }  | 
547  | 0  |             TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);  | 
548  | 0  |             img->photometric = PHOTOMETRIC_RGB; /* little white lie */  | 
549  | 0  |             img->bitspersample = 8;  | 
550  | 0  |             break;  | 
551  | 0  |         case PHOTOMETRIC_CIELAB:  | 
552  | 0  |             break;  | 
553  | 0  |         default:  | 
554  | 0  |             snprintf(emsg, EMSG_BUF_SIZE,  | 
555  | 0  |                      "Sorry, can not handle image with %s=%" PRIu16, photoTag,  | 
556  | 0  |                      img->photometric);  | 
557  | 0  |             goto fail_return;  | 
558  | 0  |     }  | 
559  | 0  |     TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);  | 
560  | 0  |     TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);  | 
561  | 0  |     TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);  | 
562  | 0  |     img->isContig =  | 
563  | 0  |         !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);  | 
564  | 0  |     if (img->isContig)  | 
565  | 0  |     { | 
566  | 0  |         if (!PickContigCase(img))  | 
567  | 0  |         { | 
568  | 0  |             snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image");  | 
569  | 0  |             goto fail_return;  | 
570  | 0  |         }  | 
571  | 0  |     }  | 
572  | 0  |     else  | 
573  | 0  |     { | 
574  | 0  |         if (!PickSeparateCase(img))  | 
575  | 0  |         { | 
576  | 0  |             snprintf(emsg, EMSG_BUF_SIZE, "Sorry, can not handle image");  | 
577  | 0  |             goto fail_return;  | 
578  | 0  |         }  | 
579  | 0  |     }  | 
580  | 0  |     return 1;  | 
581  |  |  | 
582  | 0  | fail_return:  | 
583  | 0  |     TIFFRGBAImageEnd(img);  | 
584  | 0  |     return 0;  | 
585  | 0  | }  | 
586  |  |  | 
587  |  | int TIFFRGBAImageGet(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,  | 
588  |  |                      uint32_t h)  | 
589  | 0  | { | 
590  | 0  |     if (img->get == NULL)  | 
591  | 0  |     { | 
592  | 0  |         TIFFErrorExtR(img->tif, TIFFFileName(img->tif),  | 
593  | 0  |                       "No \"get\" routine setup");  | 
594  | 0  |         return (0);  | 
595  | 0  |     }  | 
596  | 0  |     if (img->put.any == NULL)  | 
597  | 0  |     { | 
598  | 0  |         TIFFErrorExtR(  | 
599  | 0  |             img->tif, TIFFFileName(img->tif),  | 
600  | 0  |             "No \"put\" routine setupl; probably can not handle image format");  | 
601  | 0  |         return (0);  | 
602  | 0  |     }  | 
603  | 0  |     return (*img->get)(img, raster, w, h);  | 
604  | 0  | }  | 
605  |  |  | 
606  |  | /*  | 
607  |  |  * Read the specified image into an ABGR-format rastertaking in account  | 
608  |  |  * specified orientation.  | 
609  |  |  */  | 
610  |  | int TIFFReadRGBAImageOriented(TIFF *tif, uint32_t rwidth, uint32_t rheight,  | 
611  |  |                               uint32_t *raster, int orientation, int stop)  | 
612  | 0  | { | 
613  | 0  |     char emsg[EMSG_BUF_SIZE] = "";  | 
614  | 0  |     TIFFRGBAImage img;  | 
615  | 0  |     int ok;  | 
616  |  | 
  | 
617  | 0  |     if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, stop, emsg))  | 
618  | 0  |     { | 
619  | 0  |         img.req_orientation = (uint16_t)orientation;  | 
620  |  |         /* XXX verify rwidth and rheight against width and height */  | 
621  | 0  |         ok = TIFFRGBAImageGet(&img, raster + (rheight - img.height) * rwidth,  | 
622  | 0  |                               rwidth, img.height);  | 
623  | 0  |         TIFFRGBAImageEnd(&img);  | 
624  | 0  |     }  | 
625  | 0  |     else  | 
626  | 0  |     { | 
627  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);  | 
628  | 0  |         ok = 0;  | 
629  | 0  |     }  | 
630  | 0  |     return (ok);  | 
631  | 0  | }  | 
632  |  |  | 
633  |  | /*  | 
634  |  |  * Read the specified image into an ABGR-format raster. Use bottom left  | 
635  |  |  * origin for raster by default.  | 
636  |  |  */  | 
637  |  | int TIFFReadRGBAImage(TIFF *tif, uint32_t rwidth, uint32_t rheight,  | 
638  |  |                       uint32_t *raster, int stop)  | 
639  | 0  | { | 
640  | 0  |     return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster,  | 
641  | 0  |                                      ORIENTATION_BOTLEFT, stop);  | 
642  | 0  | }  | 
643  |  |  | 
644  |  | static int setorientation(TIFFRGBAImage *img)  | 
645  | 0  | { | 
646  | 0  |     switch (img->orientation)  | 
647  | 0  |     { | 
648  | 0  |         case ORIENTATION_TOPLEFT:  | 
649  | 0  |         case ORIENTATION_LEFTTOP:  | 
650  | 0  |             if (img->req_orientation == ORIENTATION_TOPRIGHT ||  | 
651  | 0  |                 img->req_orientation == ORIENTATION_RIGHTTOP)  | 
652  | 0  |                 return FLIP_HORIZONTALLY;  | 
653  | 0  |             else if (img->req_orientation == ORIENTATION_BOTRIGHT ||  | 
654  | 0  |                      img->req_orientation == ORIENTATION_RIGHTBOT)  | 
655  | 0  |                 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;  | 
656  | 0  |             else if (img->req_orientation == ORIENTATION_BOTLEFT ||  | 
657  | 0  |                      img->req_orientation == ORIENTATION_LEFTBOT)  | 
658  | 0  |                 return FLIP_VERTICALLY;  | 
659  | 0  |             else  | 
660  | 0  |                 return 0;  | 
661  | 0  |         case ORIENTATION_TOPRIGHT:  | 
662  | 0  |         case ORIENTATION_RIGHTTOP:  | 
663  | 0  |             if (img->req_orientation == ORIENTATION_TOPLEFT ||  | 
664  | 0  |                 img->req_orientation == ORIENTATION_LEFTTOP)  | 
665  | 0  |                 return FLIP_HORIZONTALLY;  | 
666  | 0  |             else if (img->req_orientation == ORIENTATION_BOTRIGHT ||  | 
667  | 0  |                      img->req_orientation == ORIENTATION_RIGHTBOT)  | 
668  | 0  |                 return FLIP_VERTICALLY;  | 
669  | 0  |             else if (img->req_orientation == ORIENTATION_BOTLEFT ||  | 
670  | 0  |                      img->req_orientation == ORIENTATION_LEFTBOT)  | 
671  | 0  |                 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;  | 
672  | 0  |             else  | 
673  | 0  |                 return 0;  | 
674  | 0  |         case ORIENTATION_BOTRIGHT:  | 
675  | 0  |         case ORIENTATION_RIGHTBOT:  | 
676  | 0  |             if (img->req_orientation == ORIENTATION_TOPLEFT ||  | 
677  | 0  |                 img->req_orientation == ORIENTATION_LEFTTOP)  | 
678  | 0  |                 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;  | 
679  | 0  |             else if (img->req_orientation == ORIENTATION_TOPRIGHT ||  | 
680  | 0  |                      img->req_orientation == ORIENTATION_RIGHTTOP)  | 
681  | 0  |                 return FLIP_VERTICALLY;  | 
682  | 0  |             else if (img->req_orientation == ORIENTATION_BOTLEFT ||  | 
683  | 0  |                      img->req_orientation == ORIENTATION_LEFTBOT)  | 
684  | 0  |                 return FLIP_HORIZONTALLY;  | 
685  | 0  |             else  | 
686  | 0  |                 return 0;  | 
687  | 0  |         case ORIENTATION_BOTLEFT:  | 
688  | 0  |         case ORIENTATION_LEFTBOT:  | 
689  | 0  |             if (img->req_orientation == ORIENTATION_TOPLEFT ||  | 
690  | 0  |                 img->req_orientation == ORIENTATION_LEFTTOP)  | 
691  | 0  |                 return FLIP_VERTICALLY;  | 
692  | 0  |             else if (img->req_orientation == ORIENTATION_TOPRIGHT ||  | 
693  | 0  |                      img->req_orientation == ORIENTATION_RIGHTTOP)  | 
694  | 0  |                 return FLIP_HORIZONTALLY | FLIP_VERTICALLY;  | 
695  | 0  |             else if (img->req_orientation == ORIENTATION_BOTRIGHT ||  | 
696  | 0  |                      img->req_orientation == ORIENTATION_RIGHTBOT)  | 
697  | 0  |                 return FLIP_HORIZONTALLY;  | 
698  | 0  |             else  | 
699  | 0  |                 return 0;  | 
700  | 0  |         default: /* NOTREACHED */  | 
701  | 0  |             return 0;  | 
702  | 0  |     }  | 
703  | 0  | }  | 
704  |  |  | 
705  |  | /*  | 
706  |  |  * Get an tile-organized image that has  | 
707  |  |  *  PlanarConfiguration contiguous if SamplesPerPixel > 1  | 
708  |  |  * or  | 
709  |  |  *  SamplesPerPixel == 1  | 
710  |  |  */  | 
711  |  | static int gtTileContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,  | 
712  |  |                         uint32_t h)  | 
713  | 0  | { | 
714  | 0  |     TIFF *tif = img->tif;  | 
715  | 0  |     tileContigRoutine put = img->put.contig;  | 
716  | 0  |     uint32_t col, row, y, rowstoread;  | 
717  | 0  |     tmsize_t pos;  | 
718  | 0  |     uint32_t tw, th;  | 
719  | 0  |     unsigned char *buf = NULL;  | 
720  | 0  |     int32_t fromskew, toskew;  | 
721  | 0  |     uint32_t nrow;  | 
722  | 0  |     int ret = 1, flip;  | 
723  | 0  |     uint32_t this_tw, tocol;  | 
724  | 0  |     int32_t this_toskew, leftmost_toskew;  | 
725  | 0  |     int32_t leftmost_fromskew;  | 
726  | 0  |     uint32_t leftmost_tw;  | 
727  | 0  |     tmsize_t bufsize;  | 
728  |  | 
  | 
729  | 0  |     bufsize = TIFFTileSize(tif);  | 
730  | 0  |     if (bufsize == 0)  | 
731  | 0  |     { | 
732  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif), "%s", "No space for tile buffer");  | 
733  | 0  |         return (0);  | 
734  | 0  |     }  | 
735  |  |  | 
736  | 0  |     TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);  | 
737  | 0  |     TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);  | 
738  |  | 
  | 
739  | 0  |     flip = setorientation(img);  | 
740  | 0  |     if (flip & FLIP_VERTICALLY)  | 
741  | 0  |     { | 
742  | 0  |         if ((tw + w) > INT_MAX)  | 
743  | 0  |         { | 
744  | 0  |             TIFFErrorExtR(tif, TIFFFileName(tif), "%s",  | 
745  | 0  |                           "unsupported tile size (too wide)");  | 
746  | 0  |             return (0);  | 
747  | 0  |         }  | 
748  | 0  |         y = h - 1;  | 
749  | 0  |         toskew = -(int32_t)(tw + w);  | 
750  | 0  |     }  | 
751  | 0  |     else  | 
752  | 0  |     { | 
753  | 0  |         if (tw > (INT_MAX + w))  | 
754  | 0  |         { | 
755  | 0  |             TIFFErrorExtR(tif, TIFFFileName(tif), "%s",  | 
756  | 0  |                           "unsupported tile size (too wide)");  | 
757  | 0  |             return (0);  | 
758  | 0  |         }  | 
759  | 0  |         y = 0;  | 
760  | 0  |         toskew = -(int32_t)(tw - w);  | 
761  | 0  |     }  | 
762  |  |  | 
763  | 0  |     if (tw == 0 || th == 0)  | 
764  | 0  |     { | 
765  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero");  | 
766  | 0  |         return (0);  | 
767  | 0  |     }  | 
768  |  |  | 
769  |  |  | 
770  |  |     /*  | 
771  |  |      *  Leftmost tile is clipped on left side if col_offset > 0.  | 
772  |  |      */  | 
773  | 0  |     leftmost_fromskew = img->col_offset % tw;  | 
774  | 0  |     leftmost_tw = tw - leftmost_fromskew;  | 
775  | 0  |     leftmost_toskew = toskew + leftmost_fromskew;  | 
776  | 0  |     for (row = 0; ret != 0 && row < h; row += nrow)  | 
777  | 0  |     { | 
778  | 0  |         rowstoread = th - (row + img->row_offset) % th;  | 
779  | 0  |         nrow = (row + rowstoread > h ? h - row : rowstoread);  | 
780  | 0  |         fromskew = leftmost_fromskew;  | 
781  | 0  |         this_tw = leftmost_tw;  | 
782  | 0  |         this_toskew = leftmost_toskew;  | 
783  | 0  |         tocol = 0;  | 
784  | 0  |         col = img->col_offset;  | 
785  | 0  |         while (tocol < w)  | 
786  | 0  |         { | 
787  | 0  |             if (_TIFFReadTileAndAllocBuffer(tif, (void **)&buf, bufsize, col,  | 
788  | 0  |                                             row + img->row_offset, 0,  | 
789  | 0  |                                             0) == (tmsize_t)(-1) &&  | 
790  | 0  |                 (buf == NULL || img->stoponerr))  | 
791  | 0  |             { | 
792  | 0  |                 ret = 0;  | 
793  | 0  |                 break;  | 
794  | 0  |             }  | 
795  | 0  |             pos = ((row + img->row_offset) % th) * TIFFTileRowSize(tif) +  | 
796  | 0  |                   ((tmsize_t)fromskew * img->samplesperpixel);  | 
797  | 0  |             if (tocol + this_tw > w)  | 
798  | 0  |             { | 
799  |  |                 /*  | 
800  |  |                  * Rightmost tile is clipped on right side.  | 
801  |  |                  */  | 
802  | 0  |                 fromskew = tw - (w - tocol);  | 
803  | 0  |                 this_tw = tw - fromskew;  | 
804  | 0  |                 this_toskew = toskew + fromskew;  | 
805  | 0  |             }  | 
806  | 0  |             tmsize_t roffset = (tmsize_t)y * w + tocol;  | 
807  | 0  |             (*put)(img, raster + roffset, tocol, y, this_tw, nrow, fromskew,  | 
808  | 0  |                    this_toskew, buf + pos);  | 
809  | 0  |             tocol += this_tw;  | 
810  | 0  |             col += this_tw;  | 
811  |  |             /*  | 
812  |  |              * After the leftmost tile, tiles are no longer clipped on left  | 
813  |  |              * side.  | 
814  |  |              */  | 
815  | 0  |             fromskew = 0;  | 
816  | 0  |             this_tw = tw;  | 
817  | 0  |             this_toskew = toskew;  | 
818  | 0  |         }  | 
819  |  | 
  | 
820  | 0  |         y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow);  | 
821  | 0  |     }  | 
822  | 0  |     _TIFFfreeExt(img->tif, buf);  | 
823  |  | 
  | 
824  | 0  |     if (flip & FLIP_HORIZONTALLY)  | 
825  | 0  |     { | 
826  | 0  |         uint32_t line;  | 
827  |  | 
  | 
828  | 0  |         for (line = 0; line < h; line++)  | 
829  | 0  |         { | 
830  | 0  |             uint32_t *left = raster + (line * w);  | 
831  | 0  |             uint32_t *right = left + w - 1;  | 
832  |  | 
  | 
833  | 0  |             while (left < right)  | 
834  | 0  |             { | 
835  | 0  |                 uint32_t temp = *left;  | 
836  | 0  |                 *left = *right;  | 
837  | 0  |                 *right = temp;  | 
838  | 0  |                 left++;  | 
839  | 0  |                 right--;  | 
840  | 0  |             }  | 
841  | 0  |         }  | 
842  | 0  |     }  | 
843  |  | 
  | 
844  | 0  |     return (ret);  | 
845  | 0  | }  | 
846  |  |  | 
847  |  | /*  | 
848  |  |  * Get an tile-organized image that has  | 
849  |  |  *   SamplesPerPixel > 1  | 
850  |  |  *   PlanarConfiguration separated  | 
851  |  |  * We assume that all such images are RGB.  | 
852  |  |  */  | 
853  |  | static int gtTileSeparate(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,  | 
854  |  |                           uint32_t h)  | 
855  | 0  | { | 
856  | 0  |     TIFF *tif = img->tif;  | 
857  | 0  |     tileSeparateRoutine put = img->put.separate;  | 
858  | 0  |     uint32_t col, row, y, rowstoread;  | 
859  | 0  |     tmsize_t pos;  | 
860  | 0  |     uint32_t tw, th;  | 
861  | 0  |     unsigned char *buf = NULL;  | 
862  | 0  |     unsigned char *p0 = NULL;  | 
863  | 0  |     unsigned char *p1 = NULL;  | 
864  | 0  |     unsigned char *p2 = NULL;  | 
865  | 0  |     unsigned char *pa = NULL;  | 
866  | 0  |     tmsize_t tilesize;  | 
867  | 0  |     tmsize_t bufsize;  | 
868  | 0  |     int32_t fromskew, toskew;  | 
869  | 0  |     int alpha = img->alpha;  | 
870  | 0  |     uint32_t nrow;  | 
871  | 0  |     int ret = 1, flip;  | 
872  | 0  |     uint16_t colorchannels;  | 
873  | 0  |     uint32_t this_tw, tocol;  | 
874  | 0  |     int32_t this_toskew, leftmost_toskew;  | 
875  | 0  |     int32_t leftmost_fromskew;  | 
876  | 0  |     uint32_t leftmost_tw;  | 
877  |  | 
  | 
878  | 0  |     tilesize = TIFFTileSize(tif);  | 
879  | 0  |     bufsize =  | 
880  | 0  |         _TIFFMultiplySSize(tif, alpha ? 4 : 3, tilesize, "gtTileSeparate");  | 
881  | 0  |     if (bufsize == 0)  | 
882  | 0  |     { | 
883  | 0  |         return (0);  | 
884  | 0  |     }  | 
885  |  |  | 
886  | 0  |     TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);  | 
887  | 0  |     TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);  | 
888  |  | 
  | 
889  | 0  |     flip = setorientation(img);  | 
890  | 0  |     if (flip & FLIP_VERTICALLY)  | 
891  | 0  |     { | 
892  | 0  |         if ((tw + w) > INT_MAX)  | 
893  | 0  |         { | 
894  | 0  |             TIFFErrorExtR(tif, TIFFFileName(tif), "%s",  | 
895  | 0  |                           "unsupported tile size (too wide)");  | 
896  | 0  |             return (0);  | 
897  | 0  |         }  | 
898  | 0  |         y = h - 1;  | 
899  | 0  |         toskew = -(int32_t)(tw + w);  | 
900  | 0  |     }  | 
901  | 0  |     else  | 
902  | 0  |     { | 
903  | 0  |         if (tw > (INT_MAX + w))  | 
904  | 0  |         { | 
905  | 0  |             TIFFErrorExtR(tif, TIFFFileName(tif), "%s",  | 
906  | 0  |                           "unsupported tile size (too wide)");  | 
907  | 0  |             return (0);  | 
908  | 0  |         }  | 
909  | 0  |         y = 0;  | 
910  | 0  |         toskew = -(int32_t)(tw - w);  | 
911  | 0  |     }  | 
912  |  |  | 
913  | 0  |     switch (img->photometric)  | 
914  | 0  |     { | 
915  | 0  |         case PHOTOMETRIC_MINISWHITE:  | 
916  | 0  |         case PHOTOMETRIC_MINISBLACK:  | 
917  | 0  |         case PHOTOMETRIC_PALETTE:  | 
918  | 0  |             colorchannels = 1;  | 
919  | 0  |             break;  | 
920  |  |  | 
921  | 0  |         default:  | 
922  | 0  |             colorchannels = 3;  | 
923  | 0  |             break;  | 
924  | 0  |     }  | 
925  |  |  | 
926  | 0  |     if (tw == 0 || th == 0)  | 
927  | 0  |     { | 
928  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero");  | 
929  | 0  |         return (0);  | 
930  | 0  |     }  | 
931  |  |  | 
932  |  |     /*  | 
933  |  |      *  Leftmost tile is clipped on left side if col_offset > 0.  | 
934  |  |      */  | 
935  | 0  |     leftmost_fromskew = img->col_offset % tw;  | 
936  | 0  |     leftmost_tw = tw - leftmost_fromskew;  | 
937  | 0  |     leftmost_toskew = toskew + leftmost_fromskew;  | 
938  | 0  |     for (row = 0; ret != 0 && row < h; row += nrow)  | 
939  | 0  |     { | 
940  | 0  |         rowstoread = th - (row + img->row_offset) % th;  | 
941  | 0  |         nrow = (row + rowstoread > h ? h - row : rowstoread);  | 
942  | 0  |         fromskew = leftmost_fromskew;  | 
943  | 0  |         this_tw = leftmost_tw;  | 
944  | 0  |         this_toskew = leftmost_toskew;  | 
945  | 0  |         tocol = 0;  | 
946  | 0  |         col = img->col_offset;  | 
947  | 0  |         while (tocol < w)  | 
948  | 0  |         { | 
949  | 0  |             if (buf == NULL)  | 
950  | 0  |             { | 
951  | 0  |                 if (_TIFFReadTileAndAllocBuffer(tif, (void **)&buf, bufsize,  | 
952  | 0  |                                                 col, row + img->row_offset, 0,  | 
953  | 0  |                                                 0) == (tmsize_t)(-1) &&  | 
954  | 0  |                     (buf == NULL || img->stoponerr))  | 
955  | 0  |                 { | 
956  | 0  |                     ret = 0;  | 
957  | 0  |                     break;  | 
958  | 0  |                 }  | 
959  | 0  |                 p0 = buf;  | 
960  | 0  |                 if (colorchannels == 1)  | 
961  | 0  |                 { | 
962  | 0  |                     p2 = p1 = p0;  | 
963  | 0  |                     pa = (alpha ? (p0 + 3 * tilesize) : NULL);  | 
964  | 0  |                 }  | 
965  | 0  |                 else  | 
966  | 0  |                 { | 
967  | 0  |                     p1 = p0 + tilesize;  | 
968  | 0  |                     p2 = p1 + tilesize;  | 
969  | 0  |                     pa = (alpha ? (p2 + tilesize) : NULL);  | 
970  | 0  |                 }  | 
971  | 0  |             }  | 
972  | 0  |             else if (TIFFReadTile(tif, p0, col, row + img->row_offset, 0, 0) ==  | 
973  | 0  |                          (tmsize_t)(-1) &&  | 
974  | 0  |                      img->stoponerr)  | 
975  | 0  |             { | 
976  | 0  |                 ret = 0;  | 
977  | 0  |                 break;  | 
978  | 0  |             }  | 
979  | 0  |             if (colorchannels > 1 &&  | 
980  | 0  |                 TIFFReadTile(tif, p1, col, row + img->row_offset, 0, 1) ==  | 
981  | 0  |                     (tmsize_t)(-1) &&  | 
982  | 0  |                 img->stoponerr)  | 
983  | 0  |             { | 
984  | 0  |                 ret = 0;  | 
985  | 0  |                 break;  | 
986  | 0  |             }  | 
987  | 0  |             if (colorchannels > 1 &&  | 
988  | 0  |                 TIFFReadTile(tif, p2, col, row + img->row_offset, 0, 2) ==  | 
989  | 0  |                     (tmsize_t)(-1) &&  | 
990  | 0  |                 img->stoponerr)  | 
991  | 0  |             { | 
992  | 0  |                 ret = 0;  | 
993  | 0  |                 break;  | 
994  | 0  |             }  | 
995  | 0  |             if (alpha &&  | 
996  | 0  |                 TIFFReadTile(tif, pa, col, row + img->row_offset, 0,  | 
997  | 0  |                              colorchannels) == (tmsize_t)(-1) &&  | 
998  | 0  |                 img->stoponerr)  | 
999  | 0  |             { | 
1000  | 0  |                 ret = 0;  | 
1001  | 0  |                 break;  | 
1002  | 0  |             }  | 
1003  |  |  | 
1004  | 0  |             pos = ((row + img->row_offset) % th) * TIFFTileRowSize(tif) +  | 
1005  | 0  |                   ((tmsize_t)fromskew * img->samplesperpixel);  | 
1006  | 0  |             if (tocol + this_tw > w)  | 
1007  | 0  |             { | 
1008  |  |                 /*  | 
1009  |  |                  * Rightmost tile is clipped on right side.  | 
1010  |  |                  */  | 
1011  | 0  |                 fromskew = tw - (w - tocol);  | 
1012  | 0  |                 this_tw = tw - fromskew;  | 
1013  | 0  |                 this_toskew = toskew + fromskew;  | 
1014  | 0  |             }  | 
1015  | 0  |             tmsize_t roffset = (tmsize_t)y * w + tocol;  | 
1016  | 0  |             (*put)(img, raster + roffset, tocol, y, this_tw, nrow, fromskew,  | 
1017  | 0  |                    this_toskew, p0 + pos, p1 + pos, p2 + pos,  | 
1018  | 0  |                    (alpha ? (pa + pos) : NULL));  | 
1019  | 0  |             tocol += this_tw;  | 
1020  | 0  |             col += this_tw;  | 
1021  |  |             /*  | 
1022  |  |              * After the leftmost tile, tiles are no longer clipped on left  | 
1023  |  |              * side.  | 
1024  |  |              */  | 
1025  | 0  |             fromskew = 0;  | 
1026  | 0  |             this_tw = tw;  | 
1027  | 0  |             this_toskew = toskew;  | 
1028  | 0  |         }  | 
1029  |  | 
  | 
1030  | 0  |         y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow);  | 
1031  | 0  |     }  | 
1032  |  | 
  | 
1033  | 0  |     if (flip & FLIP_HORIZONTALLY)  | 
1034  | 0  |     { | 
1035  | 0  |         uint32_t line;  | 
1036  |  | 
  | 
1037  | 0  |         for (line = 0; line < h; line++)  | 
1038  | 0  |         { | 
1039  | 0  |             uint32_t *left = raster + (line * w);  | 
1040  | 0  |             uint32_t *right = left + w - 1;  | 
1041  |  | 
  | 
1042  | 0  |             while (left < right)  | 
1043  | 0  |             { | 
1044  | 0  |                 uint32_t temp = *left;  | 
1045  | 0  |                 *left = *right;  | 
1046  | 0  |                 *right = temp;  | 
1047  | 0  |                 left++;  | 
1048  | 0  |                 right--;  | 
1049  | 0  |             }  | 
1050  | 0  |         }  | 
1051  | 0  |     }  | 
1052  |  | 
  | 
1053  | 0  |     _TIFFfreeExt(img->tif, buf);  | 
1054  | 0  |     return (ret);  | 
1055  | 0  | }  | 
1056  |  |  | 
1057  |  | /*  | 
1058  |  |  * Get a strip-organized image that has  | 
1059  |  |  *  PlanarConfiguration contiguous if SamplesPerPixel > 1  | 
1060  |  |  * or  | 
1061  |  |  *  SamplesPerPixel == 1  | 
1062  |  |  */  | 
1063  |  | static int gtStripContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,  | 
1064  |  |                          uint32_t h)  | 
1065  | 0  | { | 
1066  | 0  |     TIFF *tif = img->tif;  | 
1067  | 0  |     tileContigRoutine put = img->put.contig;  | 
1068  | 0  |     uint32_t row, y, nrow, nrowsub, rowstoread;  | 
1069  | 0  |     tmsize_t pos;  | 
1070  | 0  |     unsigned char *buf = NULL;  | 
1071  | 0  |     uint32_t rowsperstrip;  | 
1072  | 0  |     uint16_t subsamplinghor, subsamplingver;  | 
1073  | 0  |     uint32_t imagewidth = img->width;  | 
1074  | 0  |     tmsize_t scanline;  | 
1075  | 0  |     int32_t fromskew, toskew;  | 
1076  | 0  |     int ret = 1, flip;  | 
1077  | 0  |     tmsize_t maxstripsize;  | 
1078  |  | 
  | 
1079  | 0  |     TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor,  | 
1080  | 0  |                           &subsamplingver);  | 
1081  | 0  |     if (subsamplingver == 0)  | 
1082  | 0  |     { | 
1083  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif),  | 
1084  | 0  |                       "Invalid vertical YCbCr subsampling");  | 
1085  | 0  |         return (0);  | 
1086  | 0  |     }  | 
1087  |  |  | 
1088  | 0  |     maxstripsize = TIFFStripSize(tif);  | 
1089  |  | 
  | 
1090  | 0  |     flip = setorientation(img);  | 
1091  | 0  |     if (flip & FLIP_VERTICALLY)  | 
1092  | 0  |     { | 
1093  | 0  |         if (w > INT_MAX)  | 
1094  | 0  |         { | 
1095  | 0  |             TIFFErrorExtR(tif, TIFFFileName(tif), "Width overflow");  | 
1096  | 0  |             return (0);  | 
1097  | 0  |         }  | 
1098  | 0  |         y = h - 1;  | 
1099  | 0  |         toskew = -(int32_t)(w + w);  | 
1100  | 0  |     }  | 
1101  | 0  |     else  | 
1102  | 0  |     { | 
1103  | 0  |         y = 0;  | 
1104  | 0  |         toskew = -(int32_t)(w - w);  | 
1105  | 0  |     }  | 
1106  |  |  | 
1107  | 0  |     TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);  | 
1108  | 0  |     if (rowsperstrip == 0)  | 
1109  | 0  |     { | 
1110  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");  | 
1111  | 0  |         return (0);  | 
1112  | 0  |     }  | 
1113  |  |  | 
1114  |  |  | 
1115  | 0  |     scanline = TIFFScanlineSize(tif);  | 
1116  | 0  |     fromskew = (w < imagewidth ? imagewidth - w : 0);  | 
1117  | 0  |     for (row = 0; row < h; row += nrow)  | 
1118  | 0  |     { | 
1119  | 0  |         uint32_t temp;  | 
1120  | 0  |         rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;  | 
1121  | 0  |         nrow = (row + rowstoread > h ? h - row : rowstoread);  | 
1122  | 0  |         nrowsub = nrow;  | 
1123  | 0  |         if ((nrowsub % subsamplingver) != 0)  | 
1124  | 0  |             nrowsub += subsamplingver - nrowsub % subsamplingver;  | 
1125  | 0  |         temp = (row + img->row_offset) % rowsperstrip + nrowsub;  | 
1126  | 0  |         if (scanline > 0 && temp > (size_t)(TIFF_TMSIZE_T_MAX / scanline))  | 
1127  | 0  |         { | 
1128  | 0  |             TIFFErrorExtR(tif, TIFFFileName(tif),  | 
1129  | 0  |                           "Integer overflow in gtStripContig");  | 
1130  | 0  |             return 0;  | 
1131  | 0  |         }  | 
1132  | 0  |         if (_TIFFReadEncodedStripAndAllocBuffer(  | 
1133  | 0  |                 tif, TIFFComputeStrip(tif, row + img->row_offset, 0),  | 
1134  | 0  |                 (void **)(&buf), maxstripsize,  | 
1135  | 0  |                 temp * scanline) == (tmsize_t)(-1) &&  | 
1136  | 0  |             (buf == NULL || img->stoponerr))  | 
1137  | 0  |         { | 
1138  | 0  |             ret = 0;  | 
1139  | 0  |             break;  | 
1140  | 0  |         }  | 
1141  |  |  | 
1142  | 0  |         pos = ((row + img->row_offset) % rowsperstrip) * scanline +  | 
1143  | 0  |               ((tmsize_t)img->col_offset * img->samplesperpixel);  | 
1144  | 0  |         tmsize_t roffset = (tmsize_t)y * w;  | 
1145  | 0  |         (*put)(img, raster + roffset, 0, y, w, nrow, fromskew, toskew,  | 
1146  | 0  |                buf + pos);  | 
1147  | 0  |         y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow);  | 
1148  | 0  |     }  | 
1149  |  |  | 
1150  | 0  |     if (flip & FLIP_HORIZONTALLY)  | 
1151  | 0  |     { | 
1152  | 0  |         uint32_t line;  | 
1153  |  | 
  | 
1154  | 0  |         for (line = 0; line < h; line++)  | 
1155  | 0  |         { | 
1156  | 0  |             uint32_t *left = raster + (line * w);  | 
1157  | 0  |             uint32_t *right = left + w - 1;  | 
1158  |  | 
  | 
1159  | 0  |             while (left < right)  | 
1160  | 0  |             { | 
1161  | 0  |                 uint32_t temp = *left;  | 
1162  | 0  |                 *left = *right;  | 
1163  | 0  |                 *right = temp;  | 
1164  | 0  |                 left++;  | 
1165  | 0  |                 right--;  | 
1166  | 0  |             }  | 
1167  | 0  |         }  | 
1168  | 0  |     }  | 
1169  |  | 
  | 
1170  | 0  |     _TIFFfreeExt(img->tif, buf);  | 
1171  | 0  |     return (ret);  | 
1172  | 0  | }  | 
1173  |  |  | 
1174  |  | /*  | 
1175  |  |  * Get a strip-organized image with  | 
1176  |  |  *   SamplesPerPixel > 1  | 
1177  |  |  *   PlanarConfiguration separated  | 
1178  |  |  * We assume that all such images are RGB.  | 
1179  |  |  */  | 
1180  |  | static int gtStripSeparate(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,  | 
1181  |  |                            uint32_t h)  | 
1182  | 0  | { | 
1183  | 0  |     TIFF *tif = img->tif;  | 
1184  | 0  |     tileSeparateRoutine put = img->put.separate;  | 
1185  | 0  |     unsigned char *buf = NULL;  | 
1186  | 0  |     unsigned char *p0 = NULL, *p1 = NULL, *p2 = NULL, *pa = NULL;  | 
1187  | 0  |     uint32_t row, y, nrow, rowstoread;  | 
1188  | 0  |     tmsize_t pos;  | 
1189  | 0  |     tmsize_t scanline;  | 
1190  | 0  |     uint32_t rowsperstrip, offset_row;  | 
1191  | 0  |     uint32_t imagewidth = img->width;  | 
1192  | 0  |     tmsize_t stripsize;  | 
1193  | 0  |     tmsize_t bufsize;  | 
1194  | 0  |     int32_t fromskew, toskew;  | 
1195  | 0  |     int alpha = img->alpha;  | 
1196  | 0  |     int ret = 1, flip;  | 
1197  | 0  |     uint16_t colorchannels;  | 
1198  |  | 
  | 
1199  | 0  |     stripsize = TIFFStripSize(tif);  | 
1200  | 0  |     bufsize =  | 
1201  | 0  |         _TIFFMultiplySSize(tif, alpha ? 4 : 3, stripsize, "gtStripSeparate");  | 
1202  | 0  |     if (bufsize == 0)  | 
1203  | 0  |     { | 
1204  | 0  |         return (0);  | 
1205  | 0  |     }  | 
1206  |  |  | 
1207  | 0  |     flip = setorientation(img);  | 
1208  | 0  |     if (flip & FLIP_VERTICALLY)  | 
1209  | 0  |     { | 
1210  | 0  |         if (w > INT_MAX)  | 
1211  | 0  |         { | 
1212  | 0  |             TIFFErrorExtR(tif, TIFFFileName(tif), "Width overflow");  | 
1213  | 0  |             return (0);  | 
1214  | 0  |         }  | 
1215  | 0  |         y = h - 1;  | 
1216  | 0  |         toskew = -(int32_t)(w + w);  | 
1217  | 0  |     }  | 
1218  | 0  |     else  | 
1219  | 0  |     { | 
1220  | 0  |         y = 0;  | 
1221  | 0  |         toskew = -(int32_t)(w - w);  | 
1222  | 0  |     }  | 
1223  |  |  | 
1224  | 0  |     switch (img->photometric)  | 
1225  | 0  |     { | 
1226  | 0  |         case PHOTOMETRIC_MINISWHITE:  | 
1227  | 0  |         case PHOTOMETRIC_MINISBLACK:  | 
1228  | 0  |         case PHOTOMETRIC_PALETTE:  | 
1229  | 0  |             colorchannels = 1;  | 
1230  | 0  |             break;  | 
1231  |  |  | 
1232  | 0  |         default:  | 
1233  | 0  |             colorchannels = 3;  | 
1234  | 0  |             break;  | 
1235  | 0  |     }  | 
1236  |  |  | 
1237  | 0  |     TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);  | 
1238  | 0  |     if (rowsperstrip == 0)  | 
1239  | 0  |     { | 
1240  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");  | 
1241  | 0  |         return (0);  | 
1242  | 0  |     }  | 
1243  |  |  | 
1244  | 0  |     scanline = TIFFScanlineSize(tif);  | 
1245  | 0  |     fromskew = (w < imagewidth ? imagewidth - w : 0);  | 
1246  | 0  |     for (row = 0; row < h; row += nrow)  | 
1247  | 0  |     { | 
1248  | 0  |         uint32_t temp;  | 
1249  | 0  |         rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;  | 
1250  | 0  |         nrow = (row + rowstoread > h ? h - row : rowstoread);  | 
1251  | 0  |         offset_row = row + img->row_offset;  | 
1252  | 0  |         temp = (row + img->row_offset) % rowsperstrip + nrow;  | 
1253  | 0  |         if (scanline > 0 && temp > (size_t)(TIFF_TMSIZE_T_MAX / scanline))  | 
1254  | 0  |         { | 
1255  | 0  |             TIFFErrorExtR(tif, TIFFFileName(tif),  | 
1256  | 0  |                           "Integer overflow in gtStripSeparate");  | 
1257  | 0  |             return 0;  | 
1258  | 0  |         }  | 
1259  | 0  |         if (buf == NULL)  | 
1260  | 0  |         { | 
1261  | 0  |             if (_TIFFReadEncodedStripAndAllocBuffer(  | 
1262  | 0  |                     tif, TIFFComputeStrip(tif, offset_row, 0), (void **)&buf,  | 
1263  | 0  |                     bufsize, temp * scanline) == (tmsize_t)(-1) &&  | 
1264  | 0  |                 (buf == NULL || img->stoponerr))  | 
1265  | 0  |             { | 
1266  | 0  |                 ret = 0;  | 
1267  | 0  |                 break;  | 
1268  | 0  |             }  | 
1269  | 0  |             p0 = buf;  | 
1270  | 0  |             if (colorchannels == 1)  | 
1271  | 0  |             { | 
1272  | 0  |                 p2 = p1 = p0;  | 
1273  | 0  |                 pa = (alpha ? (p0 + 3 * stripsize) : NULL);  | 
1274  | 0  |             }  | 
1275  | 0  |             else  | 
1276  | 0  |             { | 
1277  | 0  |                 p1 = p0 + stripsize;  | 
1278  | 0  |                 p2 = p1 + stripsize;  | 
1279  | 0  |                 pa = (alpha ? (p2 + stripsize) : NULL);  | 
1280  | 0  |             }  | 
1281  | 0  |         }  | 
1282  | 0  |         else if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),  | 
1283  | 0  |                                       p0, temp * scanline) == (tmsize_t)(-1) &&  | 
1284  | 0  |                  img->stoponerr)  | 
1285  | 0  |         { | 
1286  | 0  |             ret = 0;  | 
1287  | 0  |             break;  | 
1288  | 0  |         }  | 
1289  | 0  |         if (colorchannels > 1 &&  | 
1290  | 0  |             TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1), p1,  | 
1291  | 0  |                                  temp * scanline) == (tmsize_t)(-1) &&  | 
1292  | 0  |             img->stoponerr)  | 
1293  | 0  |         { | 
1294  | 0  |             ret = 0;  | 
1295  | 0  |             break;  | 
1296  | 0  |         }  | 
1297  | 0  |         if (colorchannels > 1 &&  | 
1298  | 0  |             TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2), p2,  | 
1299  | 0  |                                  temp * scanline) == (tmsize_t)(-1) &&  | 
1300  | 0  |             img->stoponerr)  | 
1301  | 0  |         { | 
1302  | 0  |             ret = 0;  | 
1303  | 0  |             break;  | 
1304  | 0  |         }  | 
1305  | 0  |         if (alpha)  | 
1306  | 0  |         { | 
1307  | 0  |             if (TIFFReadEncodedStrip(  | 
1308  | 0  |                     tif, TIFFComputeStrip(tif, offset_row, colorchannels), pa,  | 
1309  | 0  |                     temp * scanline) == (tmsize_t)(-1) &&  | 
1310  | 0  |                 img->stoponerr)  | 
1311  | 0  |             { | 
1312  | 0  |                 ret = 0;  | 
1313  | 0  |                 break;  | 
1314  | 0  |             }  | 
1315  | 0  |         }  | 
1316  |  |  | 
1317  | 0  |         pos = ((row + img->row_offset) % rowsperstrip) * scanline +  | 
1318  | 0  |               ((tmsize_t)img->col_offset * img->samplesperpixel);  | 
1319  | 0  |         tmsize_t roffset = (tmsize_t)y * w;  | 
1320  | 0  |         (*put)(img, raster + roffset, 0, y, w, nrow, fromskew, toskew, p0 + pos,  | 
1321  | 0  |                p1 + pos, p2 + pos, (alpha ? (pa + pos) : NULL));  | 
1322  | 0  |         y += ((flip & FLIP_VERTICALLY) ? -(int32_t)nrow : (int32_t)nrow);  | 
1323  | 0  |     }  | 
1324  |  |  | 
1325  | 0  |     if (flip & FLIP_HORIZONTALLY)  | 
1326  | 0  |     { | 
1327  | 0  |         uint32_t line;  | 
1328  |  | 
  | 
1329  | 0  |         for (line = 0; line < h; line++)  | 
1330  | 0  |         { | 
1331  | 0  |             uint32_t *left = raster + (line * w);  | 
1332  | 0  |             uint32_t *right = left + w - 1;  | 
1333  |  | 
  | 
1334  | 0  |             while (left < right)  | 
1335  | 0  |             { | 
1336  | 0  |                 uint32_t temp = *left;  | 
1337  | 0  |                 *left = *right;  | 
1338  | 0  |                 *right = temp;  | 
1339  | 0  |                 left++;  | 
1340  | 0  |                 right--;  | 
1341  | 0  |             }  | 
1342  | 0  |         }  | 
1343  | 0  |     }  | 
1344  |  | 
  | 
1345  | 0  |     _TIFFfreeExt(img->tif, buf);  | 
1346  | 0  |     return (ret);  | 
1347  | 0  | }  | 
1348  |  |  | 
1349  |  | /*  | 
1350  |  |  * The following routines move decoded data returned  | 
1351  |  |  * from the TIFF library into rasters filled with packed  | 
1352  |  |  * ABGR pixels (i.e. suitable for passing to lrecwrite.)  | 
1353  |  |  *  | 
1354  |  |  * The routines have been created according to the most  | 
1355  |  |  * important cases and optimized.  PickContigCase and  | 
1356  |  |  * PickSeparateCase analyze the parameters and select  | 
1357  |  |  * the appropriate "get" and "put" routine to use.  | 
1358  |  |  */  | 
1359  |  | #define REPEAT8(op)                                                            \  | 
1360  | 0  |     REPEAT4(op);                                                               \  | 
1361  | 0  |     REPEAT4(op)  | 
1362  |  | #define REPEAT4(op)                                                            \  | 
1363  | 0  |     REPEAT2(op);                                                               \  | 
1364  | 0  |     REPEAT2(op)  | 
1365  |  | #define REPEAT2(op)                                                            \  | 
1366  | 0  |     op;                                                                        \  | 
1367  | 0  |     op  | 
1368  |  | #define CASE8(x, op)                                                           \  | 
1369  | 0  |     switch (x)                                                                 \  | 
1370  | 0  |     {                                                                          \ | 
1371  | 0  |         case 7:                                                                \  | 
1372  | 0  |             op; /*-fallthrough*/                                               \  | 
1373  | 0  |         case 6:                                                                \  | 
1374  | 0  |             op; /*-fallthrough*/                                               \  | 
1375  | 0  |         case 5:                                                                \  | 
1376  | 0  |             op; /*-fallthrough*/                                               \  | 
1377  | 0  |         case 4:                                                                \  | 
1378  | 0  |             op; /*-fallthrough*/                                               \  | 
1379  | 0  |         case 3:                                                                \  | 
1380  | 0  |             op; /*-fallthrough*/                                               \  | 
1381  | 0  |         case 2:                                                                \  | 
1382  | 0  |             op; /*-fallthrough*/                                               \  | 
1383  | 0  |         case 1:                                                                \  | 
1384  | 0  |             op;                                                                \  | 
1385  | 0  |     }  | 
1386  |  | #define CASE4(x, op)                                                           \  | 
1387  | 0  |     switch (x)                                                                 \  | 
1388  | 0  |     {                                                                          \ | 
1389  | 0  |         case 3:                                                                \  | 
1390  | 0  |             op; /*-fallthrough*/                                               \  | 
1391  | 0  |         case 2:                                                                \  | 
1392  | 0  |             op; /*-fallthrough*/                                               \  | 
1393  | 0  |         case 1:                                                                \  | 
1394  | 0  |             op;                                                                \  | 
1395  | 0  |     }  | 
1396  |  | #define NOP  | 
1397  |  |  | 
1398  |  | #define UNROLL8(w, op1, op2)                                                   \  | 
1399  | 0  |     {                                                                          \ | 
1400  | 0  |         uint32_t _x;                                                           \  | 
1401  | 0  |         for (_x = w; _x >= 8; _x -= 8)                                         \  | 
1402  | 0  |         {                                                                      \ | 
1403  | 0  |             op1;                                                               \  | 
1404  | 0  |             REPEAT8(op2);                                                      \  | 
1405  | 0  |         }                                                                      \  | 
1406  | 0  |         if (_x > 0)                                                            \  | 
1407  | 0  |         {                                                                      \ | 
1408  | 0  |             op1;                                                               \  | 
1409  | 0  |             CASE8(_x, op2);                                                    \  | 
1410  | 0  |         }                                                                      \  | 
1411  | 0  |     }  | 
1412  |  | #define UNROLL4(w, op1, op2)                                                   \  | 
1413  | 0  |     {                                                                          \ | 
1414  | 0  |         uint32_t _x;                                                           \  | 
1415  | 0  |         for (_x = w; _x >= 4; _x -= 4)                                         \  | 
1416  | 0  |         {                                                                      \ | 
1417  | 0  |             op1;                                                               \  | 
1418  | 0  |             REPEAT4(op2);                                                      \  | 
1419  | 0  |         }                                                                      \  | 
1420  | 0  |         if (_x > 0)                                                            \  | 
1421  | 0  |         {                                                                      \ | 
1422  | 0  |             op1;                                                               \  | 
1423  | 0  |             CASE4(_x, op2);                                                    \  | 
1424  | 0  |         }                                                                      \  | 
1425  | 0  |     }  | 
1426  |  | #define UNROLL2(w, op1, op2)                                                   \  | 
1427  | 0  |     {                                                                          \ | 
1428  | 0  |         uint32_t _x;                                                           \  | 
1429  | 0  |         for (_x = w; _x >= 2; _x -= 2)                                         \  | 
1430  | 0  |         {                                                                      \ | 
1431  | 0  |             op1;                                                               \  | 
1432  | 0  |             REPEAT2(op2);                                                      \  | 
1433  | 0  |         }                                                                      \  | 
1434  | 0  |         if (_x)                                                                \  | 
1435  | 0  |         {                                                                      \ | 
1436  | 0  |             op1;                                                               \  | 
1437  | 0  |             op2;                                                               \  | 
1438  | 0  |         }                                                                      \  | 
1439  | 0  |     }  | 
1440  |  |  | 
1441  |  | #define SKEW(r, g, b, skew)                                                    \  | 
1442  | 0  |     {                                                                          \ | 
1443  | 0  |         r += skew;                                                             \  | 
1444  | 0  |         g += skew;                                                             \  | 
1445  | 0  |         b += skew;                                                             \  | 
1446  | 0  |     }  | 
1447  |  | #define SKEW4(r, g, b, a, skew)                                                \  | 
1448  | 0  |     {                                                                          \ | 
1449  | 0  |         r += skew;                                                             \  | 
1450  | 0  |         g += skew;                                                             \  | 
1451  | 0  |         b += skew;                                                             \  | 
1452  | 0  |         a += skew;                                                             \  | 
1453  | 0  |     }  | 
1454  |  |  | 
1455  | 0  | #define A1 (((uint32_t)0xffL) << 24)  | 
1456  |  | #define PACK(r, g, b)                                                          \  | 
1457  | 0  |     ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) | A1)  | 
1458  |  | #define PACK4(r, g, b, a)                                                      \  | 
1459  | 0  |     ((uint32_t)(r) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 16) |            \  | 
1460  | 0  |      ((uint32_t)(a) << 24))  | 
1461  |  | #define W2B(v) (((v) >> 8) & 0xff)  | 
1462  |  | /* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */  | 
1463  |  | #define PACKW(r, g, b)                                                         \  | 
1464  |  |     ((uint32_t)W2B(r) | ((uint32_t)W2B(g) << 8) | ((uint32_t)W2B(b) << 16) | A1)  | 
1465  |  | #define PACKW4(r, g, b, a)                                                     \  | 
1466  |  |     ((uint32_t)W2B(r) | ((uint32_t)W2B(g) << 8) | ((uint32_t)W2B(b) << 16) |   \  | 
1467  |  |      ((uint32_t)W2B(a) << 24))  | 
1468  |  |  | 
1469  |  | #define DECLAREContigPutFunc(name)                                             \  | 
1470  |  |     static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \  | 
1471  |  |                      uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \  | 
1472  |  |                      unsigned char *pp)  | 
1473  |  |  | 
1474  |  | /*  | 
1475  |  |  * 8-bit palette => colormap/RGB  | 
1476  |  |  */  | 
1477  |  | DECLAREContigPutFunc(put8bitcmaptile)  | 
1478  | 0  | { | 
1479  | 0  |     uint32_t **PALmap = img->PALmap;  | 
1480  | 0  |     int samplesperpixel = img->samplesperpixel;  | 
1481  |  | 
  | 
1482  | 0  |     (void)y;  | 
1483  | 0  |     for (; h > 0; --h)  | 
1484  | 0  |     { | 
1485  | 0  |         for (x = w; x > 0; --x)  | 
1486  | 0  |         { | 
1487  | 0  |             *cp++ = PALmap[*pp][0];  | 
1488  | 0  |             pp += samplesperpixel;  | 
1489  | 0  |         }  | 
1490  | 0  |         cp += toskew;  | 
1491  | 0  |         pp += fromskew;  | 
1492  | 0  |     }  | 
1493  | 0  | }  | 
1494  |  |  | 
1495  |  | /*  | 
1496  |  |  * 4-bit palette => colormap/RGB  | 
1497  |  |  */  | 
1498  |  | DECLAREContigPutFunc(put4bitcmaptile)  | 
1499  | 0  | { | 
1500  | 0  |     uint32_t **PALmap = img->PALmap;  | 
1501  |  | 
  | 
1502  | 0  |     (void)x;  | 
1503  | 0  |     (void)y;  | 
1504  | 0  |     fromskew /= 2;  | 
1505  | 0  |     for (; h > 0; --h)  | 
1506  | 0  |     { | 
1507  | 0  |         uint32_t *bw;  | 
1508  | 0  |         UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);  | 
1509  | 0  |         cp += toskew;  | 
1510  | 0  |         pp += fromskew;  | 
1511  | 0  |     }  | 
1512  | 0  | }  | 
1513  |  |  | 
1514  |  | /*  | 
1515  |  |  * 2-bit palette => colormap/RGB  | 
1516  |  |  */  | 
1517  |  | DECLAREContigPutFunc(put2bitcmaptile)  | 
1518  | 0  | { | 
1519  | 0  |     uint32_t **PALmap = img->PALmap;  | 
1520  |  | 
  | 
1521  | 0  |     (void)x;  | 
1522  | 0  |     (void)y;  | 
1523  | 0  |     fromskew /= 4;  | 
1524  | 0  |     for (; h > 0; --h)  | 
1525  | 0  |     { | 
1526  | 0  |         uint32_t *bw;  | 
1527  | 0  |         UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);  | 
1528  | 0  |         cp += toskew;  | 
1529  | 0  |         pp += fromskew;  | 
1530  | 0  |     }  | 
1531  | 0  | }  | 
1532  |  |  | 
1533  |  | /*  | 
1534  |  |  * 1-bit palette => colormap/RGB  | 
1535  |  |  */  | 
1536  |  | DECLAREContigPutFunc(put1bitcmaptile)  | 
1537  | 0  | { | 
1538  | 0  |     uint32_t **PALmap = img->PALmap;  | 
1539  |  | 
  | 
1540  | 0  |     (void)x;  | 
1541  | 0  |     (void)y;  | 
1542  | 0  |     fromskew /= 8;  | 
1543  | 0  |     for (; h > 0; --h)  | 
1544  | 0  |     { | 
1545  | 0  |         uint32_t *bw;  | 
1546  | 0  |         UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);  | 
1547  | 0  |         cp += toskew;  | 
1548  | 0  |         pp += fromskew;  | 
1549  | 0  |     }  | 
1550  | 0  | }  | 
1551  |  |  | 
1552  |  | /*  | 
1553  |  |  * 8-bit greyscale => colormap/RGB  | 
1554  |  |  */  | 
1555  |  | DECLAREContigPutFunc(putgreytile)  | 
1556  | 0  | { | 
1557  | 0  |     int samplesperpixel = img->samplesperpixel;  | 
1558  | 0  |     uint32_t **BWmap = img->BWmap;  | 
1559  |  | 
  | 
1560  | 0  |     (void)y;  | 
1561  | 0  |     for (; h > 0; --h)  | 
1562  | 0  |     { | 
1563  | 0  |         for (x = w; x > 0; --x)  | 
1564  | 0  |         { | 
1565  | 0  |             *cp++ = BWmap[*pp][0];  | 
1566  | 0  |             pp += samplesperpixel;  | 
1567  | 0  |         }  | 
1568  | 0  |         cp += toskew;  | 
1569  | 0  |         pp += fromskew;  | 
1570  | 0  |     }  | 
1571  | 0  | }  | 
1572  |  |  | 
1573  |  | /*  | 
1574  |  |  * 8-bit greyscale with associated alpha => colormap/RGBA  | 
1575  |  |  */  | 
1576  |  | DECLAREContigPutFunc(putagreytile)  | 
1577  | 0  | { | 
1578  | 0  |     int samplesperpixel = img->samplesperpixel;  | 
1579  | 0  |     uint32_t **BWmap = img->BWmap;  | 
1580  |  | 
  | 
1581  | 0  |     (void)y;  | 
1582  | 0  |     for (; h > 0; --h)  | 
1583  | 0  |     { | 
1584  | 0  |         for (x = w; x > 0; --x)  | 
1585  | 0  |         { | 
1586  | 0  |             *cp++ = BWmap[*pp][0] & ((uint32_t) * (pp + 1) << 24 | ~A1);  | 
1587  | 0  |             pp += samplesperpixel;  | 
1588  | 0  |         }  | 
1589  | 0  |         cp += toskew;  | 
1590  | 0  |         pp += fromskew;  | 
1591  | 0  |     }  | 
1592  | 0  | }  | 
1593  |  |  | 
1594  |  | /*  | 
1595  |  |  * 16-bit greyscale => colormap/RGB  | 
1596  |  |  */  | 
1597  |  | DECLAREContigPutFunc(put16bitbwtile)  | 
1598  | 0  | { | 
1599  | 0  |     int samplesperpixel = img->samplesperpixel;  | 
1600  | 0  |     uint32_t **BWmap = img->BWmap;  | 
1601  |  | 
  | 
1602  | 0  |     (void)y;  | 
1603  | 0  |     for (; h > 0; --h)  | 
1604  | 0  |     { | 
1605  | 0  |         uint16_t *wp = (uint16_t *)pp;  | 
1606  |  | 
  | 
1607  | 0  |         for (x = w; x > 0; --x)  | 
1608  | 0  |         { | 
1609  |  |             /* use high order byte of 16bit value */  | 
1610  |  | 
  | 
1611  | 0  |             *cp++ = BWmap[*wp >> 8][0];  | 
1612  | 0  |             pp += 2 * samplesperpixel;  | 
1613  | 0  |             wp += samplesperpixel;  | 
1614  | 0  |         }  | 
1615  | 0  |         cp += toskew;  | 
1616  | 0  |         pp += fromskew;  | 
1617  | 0  |     }  | 
1618  | 0  | }  | 
1619  |  |  | 
1620  |  | /*  | 
1621  |  |  * 1-bit bilevel => colormap/RGB  | 
1622  |  |  */  | 
1623  |  | DECLAREContigPutFunc(put1bitbwtile)  | 
1624  | 0  | { | 
1625  | 0  |     uint32_t **BWmap = img->BWmap;  | 
1626  |  | 
  | 
1627  | 0  |     (void)x;  | 
1628  | 0  |     (void)y;  | 
1629  | 0  |     fromskew /= 8;  | 
1630  | 0  |     for (; h > 0; --h)  | 
1631  | 0  |     { | 
1632  | 0  |         uint32_t *bw;  | 
1633  | 0  |         UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);  | 
1634  | 0  |         cp += toskew;  | 
1635  | 0  |         pp += fromskew;  | 
1636  | 0  |     }  | 
1637  | 0  | }  | 
1638  |  |  | 
1639  |  | /*  | 
1640  |  |  * 2-bit greyscale => colormap/RGB  | 
1641  |  |  */  | 
1642  |  | DECLAREContigPutFunc(put2bitbwtile)  | 
1643  | 0  | { | 
1644  | 0  |     uint32_t **BWmap = img->BWmap;  | 
1645  |  | 
  | 
1646  | 0  |     (void)x;  | 
1647  | 0  |     (void)y;  | 
1648  | 0  |     fromskew /= 4;  | 
1649  | 0  |     for (; h > 0; --h)  | 
1650  | 0  |     { | 
1651  | 0  |         uint32_t *bw;  | 
1652  | 0  |         UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);  | 
1653  | 0  |         cp += toskew;  | 
1654  | 0  |         pp += fromskew;  | 
1655  | 0  |     }  | 
1656  | 0  | }  | 
1657  |  |  | 
1658  |  | /*  | 
1659  |  |  * 4-bit greyscale => colormap/RGB  | 
1660  |  |  */  | 
1661  |  | DECLAREContigPutFunc(put4bitbwtile)  | 
1662  | 0  | { | 
1663  | 0  |     uint32_t **BWmap = img->BWmap;  | 
1664  |  | 
  | 
1665  | 0  |     (void)x;  | 
1666  | 0  |     (void)y;  | 
1667  | 0  |     fromskew /= 2;  | 
1668  | 0  |     for (; h > 0; --h)  | 
1669  | 0  |     { | 
1670  | 0  |         uint32_t *bw;  | 
1671  | 0  |         UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);  | 
1672  | 0  |         cp += toskew;  | 
1673  | 0  |         pp += fromskew;  | 
1674  | 0  |     }  | 
1675  | 0  | }  | 
1676  |  |  | 
1677  |  | /*  | 
1678  |  |  * 8-bit packed samples, no Map => RGB  | 
1679  |  |  */  | 
1680  |  | DECLAREContigPutFunc(putRGBcontig8bittile)  | 
1681  | 0  | { | 
1682  | 0  |     int samplesperpixel = img->samplesperpixel;  | 
1683  |  | 
  | 
1684  | 0  |     (void)x;  | 
1685  | 0  |     (void)y;  | 
1686  | 0  |     fromskew *= samplesperpixel;  | 
1687  | 0  |     for (; h > 0; --h)  | 
1688  | 0  |     { | 
1689  | 0  |         UNROLL8(w, NOP, *cp++ = PACK(pp[0], pp[1], pp[2]);  | 
1690  | 0  |                 pp += samplesperpixel);  | 
1691  | 0  |         cp += toskew;  | 
1692  | 0  |         pp += fromskew;  | 
1693  | 0  |     }  | 
1694  | 0  | }  | 
1695  |  |  | 
1696  |  | /*  | 
1697  |  |  * 8-bit packed samples => RGBA w/ associated alpha  | 
1698  |  |  * (known to have Map == NULL)  | 
1699  |  |  */  | 
1700  |  | DECLAREContigPutFunc(putRGBAAcontig8bittile)  | 
1701  | 0  | { | 
1702  | 0  |     int samplesperpixel = img->samplesperpixel;  | 
1703  |  | 
  | 
1704  | 0  |     (void)x;  | 
1705  | 0  |     (void)y;  | 
1706  | 0  |     fromskew *= samplesperpixel;  | 
1707  | 0  |     for (; h > 0; --h)  | 
1708  | 0  |     { | 
1709  | 0  |         UNROLL8(w, NOP, *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);  | 
1710  | 0  |                 pp += samplesperpixel);  | 
1711  | 0  |         cp += toskew;  | 
1712  | 0  |         pp += fromskew;  | 
1713  | 0  |     }  | 
1714  | 0  | }  | 
1715  |  |  | 
1716  |  | /*  | 
1717  |  |  * 8-bit packed samples => RGBA w/ unassociated alpha  | 
1718  |  |  * (known to have Map == NULL)  | 
1719  |  |  */  | 
1720  |  | DECLAREContigPutFunc(putRGBUAcontig8bittile)  | 
1721  | 0  | { | 
1722  | 0  |     int samplesperpixel = img->samplesperpixel;  | 
1723  | 0  |     (void)y;  | 
1724  | 0  |     fromskew *= samplesperpixel;  | 
1725  | 0  |     for (; h > 0; --h)  | 
1726  | 0  |     { | 
1727  | 0  |         uint32_t r, g, b, a;  | 
1728  | 0  |         uint8_t *m;  | 
1729  | 0  |         for (x = w; x > 0; --x)  | 
1730  | 0  |         { | 
1731  | 0  |             a = pp[3];  | 
1732  | 0  |             m = img->UaToAa + ((size_t)a << 8);  | 
1733  | 0  |             r = m[pp[0]];  | 
1734  | 0  |             g = m[pp[1]];  | 
1735  | 0  |             b = m[pp[2]];  | 
1736  | 0  |             *cp++ = PACK4(r, g, b, a);  | 
1737  | 0  |             pp += samplesperpixel;  | 
1738  | 0  |         }  | 
1739  | 0  |         cp += toskew;  | 
1740  | 0  |         pp += fromskew;  | 
1741  | 0  |     }  | 
1742  | 0  | }  | 
1743  |  |  | 
1744  |  | /*  | 
1745  |  |  * 16-bit packed samples => RGB  | 
1746  |  |  */  | 
1747  |  | DECLAREContigPutFunc(putRGBcontig16bittile)  | 
1748  | 0  | { | 
1749  | 0  |     int samplesperpixel = img->samplesperpixel;  | 
1750  | 0  |     uint16_t *wp = (uint16_t *)pp;  | 
1751  | 0  |     (void)y;  | 
1752  | 0  |     fromskew *= samplesperpixel;  | 
1753  | 0  |     for (; h > 0; --h)  | 
1754  | 0  |     { | 
1755  | 0  |         for (x = w; x > 0; --x)  | 
1756  | 0  |         { | 
1757  | 0  |             *cp++ = PACK(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]],  | 
1758  | 0  |                          img->Bitdepth16To8[wp[2]]);  | 
1759  | 0  |             wp += samplesperpixel;  | 
1760  | 0  |         }  | 
1761  | 0  |         cp += toskew;  | 
1762  | 0  |         wp += fromskew;  | 
1763  | 0  |     }  | 
1764  | 0  | }  | 
1765  |  |  | 
1766  |  | /*  | 
1767  |  |  * 16-bit packed samples => RGBA w/ associated alpha  | 
1768  |  |  * (known to have Map == NULL)  | 
1769  |  |  */  | 
1770  |  | DECLAREContigPutFunc(putRGBAAcontig16bittile)  | 
1771  | 0  | { | 
1772  | 0  |     int samplesperpixel = img->samplesperpixel;  | 
1773  | 0  |     uint16_t *wp = (uint16_t *)pp;  | 
1774  | 0  |     (void)y;  | 
1775  | 0  |     fromskew *= samplesperpixel;  | 
1776  | 0  |     for (; h > 0; --h)  | 
1777  | 0  |     { | 
1778  | 0  |         for (x = w; x > 0; --x)  | 
1779  | 0  |         { | 
1780  | 0  |             *cp++ = PACK4(img->Bitdepth16To8[wp[0]], img->Bitdepth16To8[wp[1]],  | 
1781  | 0  |                           img->Bitdepth16To8[wp[2]], img->Bitdepth16To8[wp[3]]);  | 
1782  | 0  |             wp += samplesperpixel;  | 
1783  | 0  |         }  | 
1784  | 0  |         cp += toskew;  | 
1785  | 0  |         wp += fromskew;  | 
1786  | 0  |     }  | 
1787  | 0  | }  | 
1788  |  |  | 
1789  |  | /*  | 
1790  |  |  * 16-bit packed samples => RGBA w/ unassociated alpha  | 
1791  |  |  * (known to have Map == NULL)  | 
1792  |  |  */  | 
1793  |  | DECLAREContigPutFunc(putRGBUAcontig16bittile)  | 
1794  | 0  | { | 
1795  | 0  |     int samplesperpixel = img->samplesperpixel;  | 
1796  | 0  |     uint16_t *wp = (uint16_t *)pp;  | 
1797  | 0  |     (void)y;  | 
1798  | 0  |     fromskew *= samplesperpixel;  | 
1799  | 0  |     for (; h > 0; --h)  | 
1800  | 0  |     { | 
1801  | 0  |         uint32_t r, g, b, a;  | 
1802  | 0  |         uint8_t *m;  | 
1803  | 0  |         for (x = w; x > 0; --x)  | 
1804  | 0  |         { | 
1805  | 0  |             a = img->Bitdepth16To8[wp[3]];  | 
1806  | 0  |             m = img->UaToAa + ((size_t)a << 8);  | 
1807  | 0  |             r = m[img->Bitdepth16To8[wp[0]]];  | 
1808  | 0  |             g = m[img->Bitdepth16To8[wp[1]]];  | 
1809  | 0  |             b = m[img->Bitdepth16To8[wp[2]]];  | 
1810  | 0  |             *cp++ = PACK4(r, g, b, a);  | 
1811  | 0  |             wp += samplesperpixel;  | 
1812  | 0  |         }  | 
1813  | 0  |         cp += toskew;  | 
1814  | 0  |         wp += fromskew;  | 
1815  | 0  |     }  | 
1816  | 0  | }  | 
1817  |  |  | 
1818  |  | /*  | 
1819  |  |  * 8-bit packed CMYK samples w/o Map => RGB  | 
1820  |  |  *  | 
1821  |  |  * NB: The conversion of CMYK->RGB is *very* crude.  | 
1822  |  |  */  | 
1823  |  | DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)  | 
1824  | 0  | { | 
1825  | 0  |     int samplesperpixel = img->samplesperpixel;  | 
1826  | 0  |     uint16_t r, g, b, k;  | 
1827  |  | 
  | 
1828  | 0  |     (void)x;  | 
1829  | 0  |     (void)y;  | 
1830  | 0  |     fromskew *= samplesperpixel;  | 
1831  | 0  |     for (; h > 0; --h)  | 
1832  | 0  |     { | 
1833  | 0  |         UNROLL8(w, NOP, k = 255 - pp[3]; r = (k * (255 - pp[0])) / 255;  | 
1834  | 0  |                 g = (k * (255 - pp[1])) / 255; b = (k * (255 - pp[2])) / 255;  | 
1835  | 0  |                 *cp++ = PACK(r, g, b); pp += samplesperpixel);  | 
1836  | 0  |         cp += toskew;  | 
1837  | 0  |         pp += fromskew;  | 
1838  | 0  |     }  | 
1839  | 0  | }  | 
1840  |  |  | 
1841  |  | /*  | 
1842  |  |  * 8-bit packed CMYK samples w/Map => RGB  | 
1843  |  |  *  | 
1844  |  |  * NB: The conversion of CMYK->RGB is *very* crude.  | 
1845  |  |  */  | 
1846  |  | DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)  | 
1847  | 0  | { | 
1848  | 0  |     int samplesperpixel = img->samplesperpixel;  | 
1849  | 0  |     TIFFRGBValue *Map = img->Map;  | 
1850  | 0  |     uint16_t r, g, b, k;  | 
1851  |  | 
  | 
1852  | 0  |     (void)y;  | 
1853  | 0  |     fromskew *= samplesperpixel;  | 
1854  | 0  |     for (; h > 0; --h)  | 
1855  | 0  |     { | 
1856  | 0  |         for (x = w; x > 0; --x)  | 
1857  | 0  |         { | 
1858  | 0  |             k = 255 - pp[3];  | 
1859  | 0  |             r = (k * (255 - pp[0])) / 255;  | 
1860  | 0  |             g = (k * (255 - pp[1])) / 255;  | 
1861  | 0  |             b = (k * (255 - pp[2])) / 255;  | 
1862  | 0  |             *cp++ = PACK(Map[r], Map[g], Map[b]);  | 
1863  | 0  |             pp += samplesperpixel;  | 
1864  | 0  |         }  | 
1865  | 0  |         pp += fromskew;  | 
1866  | 0  |         cp += toskew;  | 
1867  | 0  |     }  | 
1868  | 0  | }  | 
1869  |  |  | 
1870  |  | #define DECLARESepPutFunc(name)                                                \  | 
1871  |  |     static void name(TIFFRGBAImage *img, uint32_t *cp, uint32_t x, uint32_t y, \  | 
1872  |  |                      uint32_t w, uint32_t h, int32_t fromskew, int32_t toskew, \  | 
1873  |  |                      unsigned char *r, unsigned char *g, unsigned char *b,     \  | 
1874  |  |                      unsigned char *a)  | 
1875  |  |  | 
1876  |  | /*  | 
1877  |  |  * 8-bit unpacked samples => RGB  | 
1878  |  |  */  | 
1879  |  | DECLARESepPutFunc(putRGBseparate8bittile)  | 
1880  | 0  | { | 
1881  | 0  |     (void)img;  | 
1882  | 0  |     (void)x;  | 
1883  | 0  |     (void)y;  | 
1884  | 0  |     (void)a;  | 
1885  | 0  |     for (; h > 0; --h)  | 
1886  | 0  |     { | 
1887  | 0  |         UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));  | 
1888  | 0  |         SKEW(r, g, b, fromskew);  | 
1889  | 0  |         cp += toskew;  | 
1890  | 0  |     }  | 
1891  | 0  | }  | 
1892  |  |  | 
1893  |  | /*  | 
1894  |  |  * 8-bit unpacked samples => RGBA w/ associated alpha  | 
1895  |  |  */  | 
1896  |  | DECLARESepPutFunc(putRGBAAseparate8bittile)  | 
1897  | 0  | { | 
1898  | 0  |     (void)img;  | 
1899  | 0  |     (void)x;  | 
1900  | 0  |     (void)y;  | 
1901  | 0  |     for (; h > 0; --h)  | 
1902  | 0  |     { | 
1903  | 0  |         UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));  | 
1904  | 0  |         SKEW4(r, g, b, a, fromskew);  | 
1905  | 0  |         cp += toskew;  | 
1906  | 0  |     }  | 
1907  | 0  | }  | 
1908  |  |  | 
1909  |  | /*  | 
1910  |  |  * 8-bit unpacked CMYK samples => RGBA  | 
1911  |  |  */  | 
1912  |  | DECLARESepPutFunc(putCMYKseparate8bittile)  | 
1913  | 0  | { | 
1914  | 0  |     (void)img;  | 
1915  | 0  |     (void)y;  | 
1916  | 0  |     for (; h > 0; --h)  | 
1917  | 0  |     { | 
1918  | 0  |         uint32_t rv, gv, bv, kv;  | 
1919  | 0  |         for (x = w; x > 0; --x)  | 
1920  | 0  |         { | 
1921  | 0  |             kv = 255 - *a++;  | 
1922  | 0  |             rv = (kv * (255 - *r++)) / 255;  | 
1923  | 0  |             gv = (kv * (255 - *g++)) / 255;  | 
1924  | 0  |             bv = (kv * (255 - *b++)) / 255;  | 
1925  | 0  |             *cp++ = PACK4(rv, gv, bv, 255);  | 
1926  | 0  |         }  | 
1927  | 0  |         SKEW4(r, g, b, a, fromskew);  | 
1928  | 0  |         cp += toskew;  | 
1929  | 0  |     }  | 
1930  | 0  | }  | 
1931  |  |  | 
1932  |  | /*  | 
1933  |  |  * 8-bit unpacked samples => RGBA w/ unassociated alpha  | 
1934  |  |  */  | 
1935  |  | DECLARESepPutFunc(putRGBUAseparate8bittile)  | 
1936  | 0  | { | 
1937  | 0  |     (void)img;  | 
1938  | 0  |     (void)y;  | 
1939  | 0  |     for (; h > 0; --h)  | 
1940  | 0  |     { | 
1941  | 0  |         uint32_t rv, gv, bv, av;  | 
1942  | 0  |         uint8_t *m;  | 
1943  | 0  |         for (x = w; x > 0; --x)  | 
1944  | 0  |         { | 
1945  | 0  |             av = *a++;  | 
1946  | 0  |             m = img->UaToAa + ((size_t)av << 8);  | 
1947  | 0  |             rv = m[*r++];  | 
1948  | 0  |             gv = m[*g++];  | 
1949  | 0  |             bv = m[*b++];  | 
1950  | 0  |             *cp++ = PACK4(rv, gv, bv, av);  | 
1951  | 0  |         }  | 
1952  | 0  |         SKEW4(r, g, b, a, fromskew);  | 
1953  | 0  |         cp += toskew;  | 
1954  | 0  |     }  | 
1955  | 0  | }  | 
1956  |  |  | 
1957  |  | /*  | 
1958  |  |  * 16-bit unpacked samples => RGB  | 
1959  |  |  */  | 
1960  |  | DECLARESepPutFunc(putRGBseparate16bittile)  | 
1961  | 0  | { | 
1962  | 0  |     uint16_t *wr = (uint16_t *)r;  | 
1963  | 0  |     uint16_t *wg = (uint16_t *)g;  | 
1964  | 0  |     uint16_t *wb = (uint16_t *)b;  | 
1965  | 0  |     (void)img;  | 
1966  | 0  |     (void)y;  | 
1967  | 0  |     (void)a;  | 
1968  | 0  |     for (; h > 0; --h)  | 
1969  | 0  |     { | 
1970  | 0  |         for (x = 0; x < w; x++)  | 
1971  | 0  |             *cp++ = PACK(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++],  | 
1972  | 0  |                          img->Bitdepth16To8[*wb++]);  | 
1973  | 0  |         SKEW(wr, wg, wb, fromskew);  | 
1974  | 0  |         cp += toskew;  | 
1975  | 0  |     }  | 
1976  | 0  | }  | 
1977  |  |  | 
1978  |  | /*  | 
1979  |  |  * 16-bit unpacked samples => RGBA w/ associated alpha  | 
1980  |  |  */  | 
1981  |  | DECLARESepPutFunc(putRGBAAseparate16bittile)  | 
1982  | 0  | { | 
1983  | 0  |     uint16_t *wr = (uint16_t *)r;  | 
1984  | 0  |     uint16_t *wg = (uint16_t *)g;  | 
1985  | 0  |     uint16_t *wb = (uint16_t *)b;  | 
1986  | 0  |     uint16_t *wa = (uint16_t *)a;  | 
1987  | 0  |     (void)img;  | 
1988  | 0  |     (void)y;  | 
1989  | 0  |     for (; h > 0; --h)  | 
1990  | 0  |     { | 
1991  | 0  |         for (x = 0; x < w; x++)  | 
1992  | 0  |             *cp++ = PACK4(img->Bitdepth16To8[*wr++], img->Bitdepth16To8[*wg++],  | 
1993  | 0  |                           img->Bitdepth16To8[*wb++], img->Bitdepth16To8[*wa++]);  | 
1994  | 0  |         SKEW4(wr, wg, wb, wa, fromskew);  | 
1995  | 0  |         cp += toskew;  | 
1996  | 0  |     }  | 
1997  | 0  | }  | 
1998  |  |  | 
1999  |  | /*  | 
2000  |  |  * 16-bit unpacked samples => RGBA w/ unassociated alpha  | 
2001  |  |  */  | 
2002  |  | DECLARESepPutFunc(putRGBUAseparate16bittile)  | 
2003  | 0  | { | 
2004  | 0  |     uint16_t *wr = (uint16_t *)r;  | 
2005  | 0  |     uint16_t *wg = (uint16_t *)g;  | 
2006  | 0  |     uint16_t *wb = (uint16_t *)b;  | 
2007  | 0  |     uint16_t *wa = (uint16_t *)a;  | 
2008  | 0  |     (void)img;  | 
2009  | 0  |     (void)y;  | 
2010  | 0  |     for (; h > 0; --h)  | 
2011  | 0  |     { | 
2012  | 0  |         uint32_t r2, g2, b2, a2;  | 
2013  | 0  |         uint8_t *m;  | 
2014  | 0  |         for (x = w; x > 0; --x)  | 
2015  | 0  |         { | 
2016  | 0  |             a2 = img->Bitdepth16To8[*wa++];  | 
2017  | 0  |             m = img->UaToAa + ((size_t)a2 << 8);  | 
2018  | 0  |             r2 = m[img->Bitdepth16To8[*wr++]];  | 
2019  | 0  |             g2 = m[img->Bitdepth16To8[*wg++]];  | 
2020  | 0  |             b2 = m[img->Bitdepth16To8[*wb++]];  | 
2021  | 0  |             *cp++ = PACK4(r2, g2, b2, a2);  | 
2022  | 0  |         }  | 
2023  | 0  |         SKEW4(wr, wg, wb, wa, fromskew);  | 
2024  | 0  |         cp += toskew;  | 
2025  | 0  |     }  | 
2026  | 0  | }  | 
2027  |  |  | 
2028  |  | /*  | 
2029  |  |  * 8-bit packed CIE L*a*b 1976 samples => RGB  | 
2030  |  |  */  | 
2031  |  | DECLAREContigPutFunc(putcontig8bitCIELab8)  | 
2032  | 0  | { | 
2033  | 0  |     float X, Y, Z;  | 
2034  | 0  |     uint32_t r, g, b;  | 
2035  | 0  |     (void)y;  | 
2036  | 0  |     fromskew *= 3;  | 
2037  | 0  |     for (; h > 0; --h)  | 
2038  | 0  |     { | 
2039  | 0  |         for (x = w; x > 0; --x)  | 
2040  | 0  |         { | 
2041  | 0  |             TIFFCIELabToXYZ(img->cielab, (unsigned char)pp[0],  | 
2042  | 0  |                             (signed char)pp[1], (signed char)pp[2], &X, &Y, &Z);  | 
2043  | 0  |             TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);  | 
2044  | 0  |             *cp++ = PACK(r, g, b);  | 
2045  | 0  |             pp += 3;  | 
2046  | 0  |         }  | 
2047  | 0  |         cp += toskew;  | 
2048  | 0  |         pp += fromskew;  | 
2049  | 0  |     }  | 
2050  | 0  | }  | 
2051  |  |  | 
2052  |  | /*  | 
2053  |  |  * 16-bit packed CIE L*a*b 1976 samples => RGB  | 
2054  |  |  */  | 
2055  |  | DECLAREContigPutFunc(putcontig8bitCIELab16)  | 
2056  | 0  | { | 
2057  | 0  |     float X, Y, Z;  | 
2058  | 0  |     uint32_t r, g, b;  | 
2059  | 0  |     uint16_t *wp = (uint16_t *)pp;  | 
2060  | 0  |     (void)y;  | 
2061  | 0  |     fromskew *= 3;  | 
2062  | 0  |     for (; h > 0; --h)  | 
2063  | 0  |     { | 
2064  | 0  |         for (x = w; x > 0; --x)  | 
2065  | 0  |         { | 
2066  | 0  |             TIFFCIELab16ToXYZ(img->cielab, (uint16_t)wp[0], (int16_t)wp[1],  | 
2067  | 0  |                               (int16_t)wp[2], &X, &Y, &Z);  | 
2068  | 0  |             TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);  | 
2069  | 0  |             *cp++ = PACK(r, g, b);  | 
2070  | 0  |             wp += 3;  | 
2071  | 0  |         }  | 
2072  | 0  |         cp += toskew;  | 
2073  | 0  |         wp += fromskew;  | 
2074  | 0  |     }  | 
2075  | 0  | }  | 
2076  |  |  | 
2077  |  | /*  | 
2078  |  |  * YCbCr -> RGB conversion and packing routines.  | 
2079  |  |  */  | 
2080  |  |  | 
2081  |  | #define YCbCrtoRGB(dst, Y)                                                     \  | 
2082  | 0  |     {                                                                          \ | 
2083  | 0  |         uint32_t r, g, b;                                                      \  | 
2084  | 0  |         TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b);                   \  | 
2085  | 0  |         dst = PACK(r, g, b);                                                   \  | 
2086  | 0  |     }  | 
2087  |  |  | 
2088  |  | /*  | 
2089  |  |  * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB  | 
2090  |  |  */  | 
2091  |  | DECLAREContigPutFunc(putcontig8bitYCbCr44tile)  | 
2092  | 0  | { | 
2093  | 0  |     uint32_t *cp1 = cp + w + toskew;  | 
2094  | 0  |     uint32_t *cp2 = cp1 + w + toskew;  | 
2095  | 0  |     uint32_t *cp3 = cp2 + w + toskew;  | 
2096  | 0  |     int32_t incr = 3 * w + 4 * toskew;  | 
2097  |  | 
  | 
2098  | 0  |     (void)y;  | 
2099  |  |     /* adjust fromskew */  | 
2100  | 0  |     fromskew = (fromskew / 4) * (4 * 2 + 2);  | 
2101  | 0  |     if ((h & 3) == 0 && (w & 3) == 0)  | 
2102  | 0  |     { | 
2103  | 0  |         for (; h >= 4; h -= 4)  | 
2104  | 0  |         { | 
2105  | 0  |             x = w >> 2;  | 
2106  | 0  |             do  | 
2107  | 0  |             { | 
2108  | 0  |                 int32_t Cb = pp[16];  | 
2109  | 0  |                 int32_t Cr = pp[17];  | 
2110  |  | 
  | 
2111  | 0  |                 YCbCrtoRGB(cp[0], pp[0]);  | 
2112  | 0  |                 YCbCrtoRGB(cp[1], pp[1]);  | 
2113  | 0  |                 YCbCrtoRGB(cp[2], pp[2]);  | 
2114  | 0  |                 YCbCrtoRGB(cp[3], pp[3]);  | 
2115  | 0  |                 YCbCrtoRGB(cp1[0], pp[4]);  | 
2116  | 0  |                 YCbCrtoRGB(cp1[1], pp[5]);  | 
2117  | 0  |                 YCbCrtoRGB(cp1[2], pp[6]);  | 
2118  | 0  |                 YCbCrtoRGB(cp1[3], pp[7]);  | 
2119  | 0  |                 YCbCrtoRGB(cp2[0], pp[8]);  | 
2120  | 0  |                 YCbCrtoRGB(cp2[1], pp[9]);  | 
2121  | 0  |                 YCbCrtoRGB(cp2[2], pp[10]);  | 
2122  | 0  |                 YCbCrtoRGB(cp2[3], pp[11]);  | 
2123  | 0  |                 YCbCrtoRGB(cp3[0], pp[12]);  | 
2124  | 0  |                 YCbCrtoRGB(cp3[1], pp[13]);  | 
2125  | 0  |                 YCbCrtoRGB(cp3[2], pp[14]);  | 
2126  | 0  |                 YCbCrtoRGB(cp3[3], pp[15]);  | 
2127  |  | 
  | 
2128  | 0  |                 cp += 4;  | 
2129  | 0  |                 cp1 += 4;  | 
2130  | 0  |                 cp2 += 4;  | 
2131  | 0  |                 cp3 += 4;  | 
2132  | 0  |                 pp += 18;  | 
2133  | 0  |             } while (--x);  | 
2134  | 0  |             cp += incr;  | 
2135  | 0  |             cp1 += incr;  | 
2136  | 0  |             cp2 += incr;  | 
2137  | 0  |             cp3 += incr;  | 
2138  | 0  |             pp += fromskew;  | 
2139  | 0  |         }  | 
2140  | 0  |     }  | 
2141  | 0  |     else  | 
2142  | 0  |     { | 
2143  | 0  |         while (h > 0)  | 
2144  | 0  |         { | 
2145  | 0  |             for (x = w; x > 0;)  | 
2146  | 0  |             { | 
2147  | 0  |                 int32_t Cb = pp[16];  | 
2148  | 0  |                 int32_t Cr = pp[17];  | 
2149  | 0  |                 switch (x)  | 
2150  | 0  |                 { | 
2151  | 0  |                     default:  | 
2152  | 0  |                         switch (h)  | 
2153  | 0  |                         { | 
2154  | 0  |                             default:  | 
2155  | 0  |                                 YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */  | 
2156  | 0  |                             case 3:  | 
2157  | 0  |                                 YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */  | 
2158  | 0  |                             case 2:  | 
2159  | 0  |                                 YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */  | 
2160  | 0  |                             case 1:  | 
2161  | 0  |                                 YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */  | 
2162  | 0  |                         }                                 /* FALLTHROUGH */  | 
2163  | 0  |                     case 3:  | 
2164  | 0  |                         switch (h)  | 
2165  | 0  |                         { | 
2166  | 0  |                             default:  | 
2167  | 0  |                                 YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */  | 
2168  | 0  |                             case 3:  | 
2169  | 0  |                                 YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */  | 
2170  | 0  |                             case 2:  | 
2171  | 0  |                                 YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */  | 
2172  | 0  |                             case 1:  | 
2173  | 0  |                                 YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */  | 
2174  | 0  |                         }                                 /* FALLTHROUGH */  | 
2175  | 0  |                     case 2:  | 
2176  | 0  |                         switch (h)  | 
2177  | 0  |                         { | 
2178  | 0  |                             default:  | 
2179  | 0  |                                 YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */  | 
2180  | 0  |                             case 3:  | 
2181  | 0  |                                 YCbCrtoRGB(cp2[1], pp[9]); /* FALLTHROUGH */  | 
2182  | 0  |                             case 2:  | 
2183  | 0  |                                 YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */  | 
2184  | 0  |                             case 1:  | 
2185  | 0  |                                 YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */  | 
2186  | 0  |                         }                                 /* FALLTHROUGH */  | 
2187  | 0  |                     case 1:  | 
2188  | 0  |                         switch (h)  | 
2189  | 0  |                         { | 
2190  | 0  |                             default:  | 
2191  | 0  |                                 YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */  | 
2192  | 0  |                             case 3:  | 
2193  | 0  |                                 YCbCrtoRGB(cp2[0], pp[8]); /* FALLTHROUGH */  | 
2194  | 0  |                             case 2:  | 
2195  | 0  |                                 YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */  | 
2196  | 0  |                             case 1:  | 
2197  | 0  |                                 YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */  | 
2198  | 0  |                         }                                 /* FALLTHROUGH */  | 
2199  | 0  |                 }  | 
2200  | 0  |                 if (x < 4)  | 
2201  | 0  |                 { | 
2202  | 0  |                     cp += x;  | 
2203  | 0  |                     cp1 += x;  | 
2204  | 0  |                     cp2 += x;  | 
2205  | 0  |                     cp3 += x;  | 
2206  | 0  |                     x = 0;  | 
2207  | 0  |                 }  | 
2208  | 0  |                 else  | 
2209  | 0  |                 { | 
2210  | 0  |                     cp += 4;  | 
2211  | 0  |                     cp1 += 4;  | 
2212  | 0  |                     cp2 += 4;  | 
2213  | 0  |                     cp3 += 4;  | 
2214  | 0  |                     x -= 4;  | 
2215  | 0  |                 }  | 
2216  | 0  |                 pp += 18;  | 
2217  | 0  |             }  | 
2218  | 0  |             if (h <= 4)  | 
2219  | 0  |                 break;  | 
2220  | 0  |             h -= 4;  | 
2221  | 0  |             cp += incr;  | 
2222  | 0  |             cp1 += incr;  | 
2223  | 0  |             cp2 += incr;  | 
2224  | 0  |             cp3 += incr;  | 
2225  | 0  |             pp += fromskew;  | 
2226  | 0  |         }  | 
2227  | 0  |     }  | 
2228  | 0  | }  | 
2229  |  |  | 
2230  |  | /*  | 
2231  |  |  * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB  | 
2232  |  |  */  | 
2233  |  | DECLAREContigPutFunc(putcontig8bitYCbCr42tile)  | 
2234  | 0  | { | 
2235  | 0  |     uint32_t *cp1 = cp + w + toskew;  | 
2236  | 0  |     int32_t incr = 2 * toskew + w;  | 
2237  |  | 
  | 
2238  | 0  |     (void)y;  | 
2239  | 0  |     fromskew = (fromskew / 4) * (4 * 2 + 2);  | 
2240  | 0  |     if ((w & 3) == 0 && (h & 1) == 0)  | 
2241  | 0  |     { | 
2242  | 0  |         for (; h >= 2; h -= 2)  | 
2243  | 0  |         { | 
2244  | 0  |             x = w >> 2;  | 
2245  | 0  |             do  | 
2246  | 0  |             { | 
2247  | 0  |                 int32_t Cb = pp[8];  | 
2248  | 0  |                 int32_t Cr = pp[9];  | 
2249  |  | 
  | 
2250  | 0  |                 YCbCrtoRGB(cp[0], pp[0]);  | 
2251  | 0  |                 YCbCrtoRGB(cp[1], pp[1]);  | 
2252  | 0  |                 YCbCrtoRGB(cp[2], pp[2]);  | 
2253  | 0  |                 YCbCrtoRGB(cp[3], pp[3]);  | 
2254  | 0  |                 YCbCrtoRGB(cp1[0], pp[4]);  | 
2255  | 0  |                 YCbCrtoRGB(cp1[1], pp[5]);  | 
2256  | 0  |                 YCbCrtoRGB(cp1[2], pp[6]);  | 
2257  | 0  |                 YCbCrtoRGB(cp1[3], pp[7]);  | 
2258  |  | 
  | 
2259  | 0  |                 cp += 4;  | 
2260  | 0  |                 cp1 += 4;  | 
2261  | 0  |                 pp += 10;  | 
2262  | 0  |             } while (--x);  | 
2263  | 0  |             cp += incr;  | 
2264  | 0  |             cp1 += incr;  | 
2265  | 0  |             pp += fromskew;  | 
2266  | 0  |         }  | 
2267  | 0  |     }  | 
2268  | 0  |     else  | 
2269  | 0  |     { | 
2270  | 0  |         while (h > 0)  | 
2271  | 0  |         { | 
2272  | 0  |             for (x = w; x > 0;)  | 
2273  | 0  |             { | 
2274  | 0  |                 int32_t Cb = pp[8];  | 
2275  | 0  |                 int32_t Cr = pp[9];  | 
2276  | 0  |                 switch (x)  | 
2277  | 0  |                 { | 
2278  | 0  |                     default:  | 
2279  | 0  |                         switch (h)  | 
2280  | 0  |                         { | 
2281  | 0  |                             default:  | 
2282  | 0  |                                 YCbCrtoRGB(cp1[3], pp[7]); /* FALLTHROUGH */  | 
2283  | 0  |                             case 1:  | 
2284  | 0  |                                 YCbCrtoRGB(cp[3], pp[3]); /* FALLTHROUGH */  | 
2285  | 0  |                         }                                 /* FALLTHROUGH */  | 
2286  | 0  |                     case 3:  | 
2287  | 0  |                         switch (h)  | 
2288  | 0  |                         { | 
2289  | 0  |                             default:  | 
2290  | 0  |                                 YCbCrtoRGB(cp1[2], pp[6]); /* FALLTHROUGH */  | 
2291  | 0  |                             case 1:  | 
2292  | 0  |                                 YCbCrtoRGB(cp[2], pp[2]); /* FALLTHROUGH */  | 
2293  | 0  |                         }                                 /* FALLTHROUGH */  | 
2294  | 0  |                     case 2:  | 
2295  | 0  |                         switch (h)  | 
2296  | 0  |                         { | 
2297  | 0  |                             default:  | 
2298  | 0  |                                 YCbCrtoRGB(cp1[1], pp[5]); /* FALLTHROUGH */  | 
2299  | 0  |                             case 1:  | 
2300  | 0  |                                 YCbCrtoRGB(cp[1], pp[1]); /* FALLTHROUGH */  | 
2301  | 0  |                         }                                 /* FALLTHROUGH */  | 
2302  | 0  |                     case 1:  | 
2303  | 0  |                         switch (h)  | 
2304  | 0  |                         { | 
2305  | 0  |                             default:  | 
2306  | 0  |                                 YCbCrtoRGB(cp1[0], pp[4]); /* FALLTHROUGH */  | 
2307  | 0  |                             case 1:  | 
2308  | 0  |                                 YCbCrtoRGB(cp[0], pp[0]); /* FALLTHROUGH */  | 
2309  | 0  |                         }                                 /* FALLTHROUGH */  | 
2310  | 0  |                 }  | 
2311  | 0  |                 if (x < 4)  | 
2312  | 0  |                 { | 
2313  | 0  |                     cp += x;  | 
2314  | 0  |                     cp1 += x;  | 
2315  | 0  |                     x = 0;  | 
2316  | 0  |                 }  | 
2317  | 0  |                 else  | 
2318  | 0  |                 { | 
2319  | 0  |                     cp += 4;  | 
2320  | 0  |                     cp1 += 4;  | 
2321  | 0  |                     x -= 4;  | 
2322  | 0  |                 }  | 
2323  | 0  |                 pp += 10;  | 
2324  | 0  |             }  | 
2325  | 0  |             if (h <= 2)  | 
2326  | 0  |                 break;  | 
2327  | 0  |             h -= 2;  | 
2328  | 0  |             cp += incr;  | 
2329  | 0  |             cp1 += incr;  | 
2330  | 0  |             pp += fromskew;  | 
2331  | 0  |         }  | 
2332  | 0  |     }  | 
2333  | 0  | }  | 
2334  |  |  | 
2335  |  | /*  | 
2336  |  |  * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB  | 
2337  |  |  */  | 
2338  |  | DECLAREContigPutFunc(putcontig8bitYCbCr41tile)  | 
2339  | 0  | { | 
2340  | 0  |     (void)y;  | 
2341  | 0  |     fromskew = (fromskew / 4) * (4 * 1 + 2);  | 
2342  | 0  |     do  | 
2343  | 0  |     { | 
2344  | 0  |         x = w >> 2;  | 
2345  | 0  |         while (x > 0)  | 
2346  | 0  |         { | 
2347  | 0  |             int32_t Cb = pp[4];  | 
2348  | 0  |             int32_t Cr = pp[5];  | 
2349  |  | 
  | 
2350  | 0  |             YCbCrtoRGB(cp[0], pp[0]);  | 
2351  | 0  |             YCbCrtoRGB(cp[1], pp[1]);  | 
2352  | 0  |             YCbCrtoRGB(cp[2], pp[2]);  | 
2353  | 0  |             YCbCrtoRGB(cp[3], pp[3]);  | 
2354  |  | 
  | 
2355  | 0  |             cp += 4;  | 
2356  | 0  |             pp += 6;  | 
2357  | 0  |             x--;  | 
2358  | 0  |         }  | 
2359  |  | 
  | 
2360  | 0  |         if ((w & 3) != 0)  | 
2361  | 0  |         { | 
2362  | 0  |             int32_t Cb = pp[4];  | 
2363  | 0  |             int32_t Cr = pp[5];  | 
2364  |  | 
  | 
2365  | 0  |             switch ((w & 3))  | 
2366  | 0  |             { | 
2367  | 0  |                 case 3:  | 
2368  | 0  |                     YCbCrtoRGB(cp[2], pp[2]); /*-fallthrough*/  | 
2369  | 0  |                 case 2:  | 
2370  | 0  |                     YCbCrtoRGB(cp[1], pp[1]); /*-fallthrough*/  | 
2371  | 0  |                 case 1:  | 
2372  | 0  |                     YCbCrtoRGB(cp[0], pp[0]); /*-fallthrough*/  | 
2373  | 0  |                 case 0:  | 
2374  | 0  |                     break;  | 
2375  | 0  |             }  | 
2376  |  |  | 
2377  | 0  |             cp += (w & 3);  | 
2378  | 0  |             pp += 6;  | 
2379  | 0  |         }  | 
2380  |  |  | 
2381  | 0  |         cp += toskew;  | 
2382  | 0  |         pp += fromskew;  | 
2383  | 0  |     } while (--h);  | 
2384  | 0  | }  | 
2385  |  |  | 
2386  |  | /*  | 
2387  |  |  * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB  | 
2388  |  |  */  | 
2389  |  | DECLAREContigPutFunc(putcontig8bitYCbCr22tile)  | 
2390  | 0  | { | 
2391  | 0  |     uint32_t *cp2;  | 
2392  | 0  |     int32_t incr = 2 * toskew + w;  | 
2393  | 0  |     (void)y;  | 
2394  | 0  |     fromskew = (fromskew / 2) * (2 * 2 + 2);  | 
2395  | 0  |     cp2 = cp + w + toskew;  | 
2396  | 0  |     while (h >= 2)  | 
2397  | 0  |     { | 
2398  | 0  |         x = w;  | 
2399  | 0  |         while (x >= 2)  | 
2400  | 0  |         { | 
2401  | 0  |             uint32_t Cb = pp[4];  | 
2402  | 0  |             uint32_t Cr = pp[5];  | 
2403  | 0  |             YCbCrtoRGB(cp[0], pp[0]);  | 
2404  | 0  |             YCbCrtoRGB(cp[1], pp[1]);  | 
2405  | 0  |             YCbCrtoRGB(cp2[0], pp[2]);  | 
2406  | 0  |             YCbCrtoRGB(cp2[1], pp[3]);  | 
2407  | 0  |             cp += 2;  | 
2408  | 0  |             cp2 += 2;  | 
2409  | 0  |             pp += 6;  | 
2410  | 0  |             x -= 2;  | 
2411  | 0  |         }  | 
2412  | 0  |         if (x == 1)  | 
2413  | 0  |         { | 
2414  | 0  |             uint32_t Cb = pp[4];  | 
2415  | 0  |             uint32_t Cr = pp[5];  | 
2416  | 0  |             YCbCrtoRGB(cp[0], pp[0]);  | 
2417  | 0  |             YCbCrtoRGB(cp2[0], pp[2]);  | 
2418  | 0  |             cp++;  | 
2419  | 0  |             cp2++;  | 
2420  | 0  |             pp += 6;  | 
2421  | 0  |         }  | 
2422  | 0  |         cp += incr;  | 
2423  | 0  |         cp2 += incr;  | 
2424  | 0  |         pp += fromskew;  | 
2425  | 0  |         h -= 2;  | 
2426  | 0  |     }  | 
2427  | 0  |     if (h == 1)  | 
2428  | 0  |     { | 
2429  | 0  |         x = w;  | 
2430  | 0  |         while (x >= 2)  | 
2431  | 0  |         { | 
2432  | 0  |             uint32_t Cb = pp[4];  | 
2433  | 0  |             uint32_t Cr = pp[5];  | 
2434  | 0  |             YCbCrtoRGB(cp[0], pp[0]);  | 
2435  | 0  |             YCbCrtoRGB(cp[1], pp[1]);  | 
2436  | 0  |             cp += 2;  | 
2437  | 0  |             cp2 += 2;  | 
2438  | 0  |             pp += 6;  | 
2439  | 0  |             x -= 2;  | 
2440  | 0  |         }  | 
2441  | 0  |         if (x == 1)  | 
2442  | 0  |         { | 
2443  | 0  |             uint32_t Cb = pp[4];  | 
2444  | 0  |             uint32_t Cr = pp[5];  | 
2445  | 0  |             YCbCrtoRGB(cp[0], pp[0]);  | 
2446  | 0  |         }  | 
2447  | 0  |     }  | 
2448  | 0  | }  | 
2449  |  |  | 
2450  |  | /*  | 
2451  |  |  * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB  | 
2452  |  |  */  | 
2453  |  | DECLAREContigPutFunc(putcontig8bitYCbCr21tile)  | 
2454  | 0  | { | 
2455  | 0  |     (void)y;  | 
2456  | 0  |     fromskew = (fromskew / 2) * (2 * 1 + 2);  | 
2457  | 0  |     do  | 
2458  | 0  |     { | 
2459  | 0  |         x = w >> 1;  | 
2460  | 0  |         while (x > 0)  | 
2461  | 0  |         { | 
2462  | 0  |             int32_t Cb = pp[2];  | 
2463  | 0  |             int32_t Cr = pp[3];  | 
2464  |  | 
  | 
2465  | 0  |             YCbCrtoRGB(cp[0], pp[0]);  | 
2466  | 0  |             YCbCrtoRGB(cp[1], pp[1]);  | 
2467  |  | 
  | 
2468  | 0  |             cp += 2;  | 
2469  | 0  |             pp += 4;  | 
2470  | 0  |             x--;  | 
2471  | 0  |         }  | 
2472  |  | 
  | 
2473  | 0  |         if ((w & 1) != 0)  | 
2474  | 0  |         { | 
2475  | 0  |             int32_t Cb = pp[2];  | 
2476  | 0  |             int32_t Cr = pp[3];  | 
2477  |  | 
  | 
2478  | 0  |             YCbCrtoRGB(cp[0], pp[0]);  | 
2479  |  | 
  | 
2480  | 0  |             cp += 1;  | 
2481  | 0  |             pp += 4;  | 
2482  | 0  |         }  | 
2483  |  | 
  | 
2484  | 0  |         cp += toskew;  | 
2485  | 0  |         pp += fromskew;  | 
2486  | 0  |     } while (--h);  | 
2487  | 0  | }  | 
2488  |  |  | 
2489  |  | /*  | 
2490  |  |  * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB  | 
2491  |  |  */  | 
2492  |  | DECLAREContigPutFunc(putcontig8bitYCbCr12tile)  | 
2493  | 0  | { | 
2494  | 0  |     uint32_t *cp2;  | 
2495  | 0  |     int32_t incr = 2 * toskew + w;  | 
2496  | 0  |     (void)y;  | 
2497  | 0  |     fromskew = (fromskew / 1) * (1 * 2 + 2);  | 
2498  | 0  |     cp2 = cp + w + toskew;  | 
2499  | 0  |     while (h >= 2)  | 
2500  | 0  |     { | 
2501  | 0  |         x = w;  | 
2502  | 0  |         do  | 
2503  | 0  |         { | 
2504  | 0  |             uint32_t Cb = pp[2];  | 
2505  | 0  |             uint32_t Cr = pp[3];  | 
2506  | 0  |             YCbCrtoRGB(cp[0], pp[0]);  | 
2507  | 0  |             YCbCrtoRGB(cp2[0], pp[1]);  | 
2508  | 0  |             cp++;  | 
2509  | 0  |             cp2++;  | 
2510  | 0  |             pp += 4;  | 
2511  | 0  |         } while (--x);  | 
2512  | 0  |         cp += incr;  | 
2513  | 0  |         cp2 += incr;  | 
2514  | 0  |         pp += fromskew;  | 
2515  | 0  |         h -= 2;  | 
2516  | 0  |     }  | 
2517  | 0  |     if (h == 1)  | 
2518  | 0  |     { | 
2519  | 0  |         x = w;  | 
2520  | 0  |         do  | 
2521  | 0  |         { | 
2522  | 0  |             uint32_t Cb = pp[2];  | 
2523  | 0  |             uint32_t Cr = pp[3];  | 
2524  | 0  |             YCbCrtoRGB(cp[0], pp[0]);  | 
2525  | 0  |             cp++;  | 
2526  | 0  |             pp += 4;  | 
2527  | 0  |         } while (--x);  | 
2528  | 0  |     }  | 
2529  | 0  | }  | 
2530  |  |  | 
2531  |  | /*  | 
2532  |  |  * 8-bit packed YCbCr samples w/ no subsampling => RGB  | 
2533  |  |  */  | 
2534  |  | DECLAREContigPutFunc(putcontig8bitYCbCr11tile)  | 
2535  | 0  | { | 
2536  | 0  |     (void)y;  | 
2537  | 0  |     fromskew = (fromskew / 1) * (1 * 1 + 2);  | 
2538  | 0  |     do  | 
2539  | 0  |     { | 
2540  | 0  |         x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */  | 
2541  | 0  |         do  | 
2542  | 0  |         { | 
2543  | 0  |             int32_t Cb = pp[1];  | 
2544  | 0  |             int32_t Cr = pp[2];  | 
2545  |  | 
  | 
2546  | 0  |             YCbCrtoRGB(*cp++, pp[0]);  | 
2547  |  | 
  | 
2548  | 0  |             pp += 3;  | 
2549  | 0  |         } while (--x);  | 
2550  | 0  |         cp += toskew;  | 
2551  | 0  |         pp += fromskew;  | 
2552  | 0  |     } while (--h);  | 
2553  | 0  | }  | 
2554  |  |  | 
2555  |  | /*  | 
2556  |  |  * 8-bit packed YCbCr samples w/ no subsampling => RGB  | 
2557  |  |  */  | 
2558  |  | DECLARESepPutFunc(putseparate8bitYCbCr11tile)  | 
2559  | 0  | { | 
2560  | 0  |     (void)y;  | 
2561  | 0  |     (void)a;  | 
2562  |  |     /* TODO: naming of input vars is still off, change obfuscating declaration  | 
2563  |  |      * inside define, or resolve obfuscation */  | 
2564  | 0  |     for (; h > 0; --h)  | 
2565  | 0  |     { | 
2566  | 0  |         x = w;  | 
2567  | 0  |         do  | 
2568  | 0  |         { | 
2569  | 0  |             uint32_t dr, dg, db;  | 
2570  | 0  |             TIFFYCbCrtoRGB(img->ycbcr, *r++, *g++, *b++, &dr, &dg, &db);  | 
2571  | 0  |             *cp++ = PACK(dr, dg, db);  | 
2572  | 0  |         } while (--x);  | 
2573  | 0  |         SKEW(r, g, b, fromskew);  | 
2574  | 0  |         cp += toskew;  | 
2575  | 0  |     }  | 
2576  | 0  | }  | 
2577  |  | #undef YCbCrtoRGB  | 
2578  |  |  | 
2579  |  | static int isInRefBlackWhiteRange(float f)  | 
2580  | 0  | { | 
2581  | 0  |     return f > (float)(-0x7FFFFFFF + 128) && f < (float)0x7FFFFFFF;  | 
2582  | 0  | }  | 
2583  |  |  | 
2584  |  | static int initYCbCrConversion(TIFFRGBAImage *img)  | 
2585  | 0  | { | 
2586  | 0  |     static const char module[] = "initYCbCrConversion";  | 
2587  |  | 
  | 
2588  | 0  |     float *luma, *refBlackWhite;  | 
2589  |  | 
  | 
2590  | 0  |     if (img->ycbcr == NULL)  | 
2591  | 0  |     { | 
2592  | 0  |         img->ycbcr = (TIFFYCbCrToRGB *)_TIFFmallocExt(  | 
2593  | 0  |             img->tif, TIFFroundup_32(sizeof(TIFFYCbCrToRGB), sizeof(long)) +  | 
2594  | 0  |                           4 * 256 * sizeof(TIFFRGBValue) +  | 
2595  | 0  |                           2 * 256 * sizeof(int) + 3 * 256 * sizeof(int32_t));  | 
2596  | 0  |         if (img->ycbcr == NULL)  | 
2597  | 0  |         { | 
2598  | 0  |             TIFFErrorExtR(img->tif, module,  | 
2599  | 0  |                           "No space for YCbCr->RGB conversion state");  | 
2600  | 0  |             return (0);  | 
2601  | 0  |         }  | 
2602  | 0  |     }  | 
2603  |  |  | 
2604  | 0  |     TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);  | 
2605  | 0  |     TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,  | 
2606  | 0  |                           &refBlackWhite);  | 
2607  |  |  | 
2608  |  |     /* Do some validation to avoid later issues. Detect NaN for now */  | 
2609  |  |     /* and also if lumaGreen is zero since we divide by it later */  | 
2610  | 0  |     if (luma[0] != luma[0] || luma[1] != luma[1] || luma[1] == 0.0 ||  | 
2611  | 0  |         luma[2] != luma[2])  | 
2612  | 0  |     { | 
2613  | 0  |         TIFFErrorExtR(img->tif, module,  | 
2614  | 0  |                       "Invalid values for YCbCrCoefficients tag");  | 
2615  | 0  |         return (0);  | 
2616  | 0  |     }  | 
2617  |  |  | 
2618  | 0  |     if (!isInRefBlackWhiteRange(refBlackWhite[0]) ||  | 
2619  | 0  |         !isInRefBlackWhiteRange(refBlackWhite[1]) ||  | 
2620  | 0  |         !isInRefBlackWhiteRange(refBlackWhite[2]) ||  | 
2621  | 0  |         !isInRefBlackWhiteRange(refBlackWhite[3]) ||  | 
2622  | 0  |         !isInRefBlackWhiteRange(refBlackWhite[4]) ||  | 
2623  | 0  |         !isInRefBlackWhiteRange(refBlackWhite[5]))  | 
2624  | 0  |     { | 
2625  | 0  |         TIFFErrorExtR(img->tif, module,  | 
2626  | 0  |                       "Invalid values for ReferenceBlackWhite tag");  | 
2627  | 0  |         return (0);  | 
2628  | 0  |     }  | 
2629  |  |  | 
2630  | 0  |     if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)  | 
2631  | 0  |         return (0);  | 
2632  | 0  |     return (1);  | 
2633  | 0  | }  | 
2634  |  |  | 
2635  |  | static tileContigRoutine initCIELabConversion(TIFFRGBAImage *img)  | 
2636  | 0  | { | 
2637  | 0  |     static const char module[] = "initCIELabConversion";  | 
2638  |  | 
  | 
2639  | 0  |     float *whitePoint;  | 
2640  | 0  |     float refWhite[3];  | 
2641  |  | 
  | 
2642  | 0  |     TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);  | 
2643  | 0  |     if (whitePoint[1] == 0.0f)  | 
2644  | 0  |     { | 
2645  | 0  |         TIFFErrorExtR(img->tif, module, "Invalid value for WhitePoint tag.");  | 
2646  | 0  |         return NULL;  | 
2647  | 0  |     }  | 
2648  |  |  | 
2649  | 0  |     if (!img->cielab)  | 
2650  | 0  |     { | 
2651  | 0  |         img->cielab = (TIFFCIELabToRGB *)_TIFFmallocExt(  | 
2652  | 0  |             img->tif, sizeof(TIFFCIELabToRGB));  | 
2653  | 0  |         if (!img->cielab)  | 
2654  | 0  |         { | 
2655  | 0  |             TIFFErrorExtR(img->tif, module,  | 
2656  | 0  |                           "No space for CIE L*a*b*->RGB conversion state.");  | 
2657  | 0  |             return NULL;  | 
2658  | 0  |         }  | 
2659  | 0  |     }  | 
2660  |  |  | 
2661  | 0  |     refWhite[1] = 100.0F;  | 
2662  | 0  |     refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];  | 
2663  | 0  |     refWhite[2] =  | 
2664  | 0  |         (1.0F - whitePoint[0] - whitePoint[1]) / whitePoint[1] * refWhite[1];  | 
2665  | 0  |     if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0)  | 
2666  | 0  |     { | 
2667  | 0  |         TIFFErrorExtR(img->tif, module,  | 
2668  | 0  |                       "Failed to initialize CIE L*a*b*->RGB conversion state.");  | 
2669  | 0  |         _TIFFfreeExt(img->tif, img->cielab);  | 
2670  | 0  |         return NULL;  | 
2671  | 0  |     }  | 
2672  |  |  | 
2673  | 0  |     if (img->bitspersample == 8)  | 
2674  | 0  |         return putcontig8bitCIELab8;  | 
2675  | 0  |     else if (img->bitspersample == 16)  | 
2676  | 0  |         return putcontig8bitCIELab16;  | 
2677  | 0  |     return NULL;  | 
2678  | 0  | }  | 
2679  |  |  | 
2680  |  | /*  | 
2681  |  |  * Greyscale images with less than 8 bits/sample are handled  | 
2682  |  |  * with a table to avoid lots of shifts and masks.  The table  | 
2683  |  |  * is setup so that put*bwtile (below) can retrieve 8/bitspersample  | 
2684  |  |  * pixel values simply by indexing into the table with one  | 
2685  |  |  * number.  | 
2686  |  |  */  | 
2687  |  | static int makebwmap(TIFFRGBAImage *img)  | 
2688  | 0  | { | 
2689  | 0  |     TIFFRGBValue *Map = img->Map;  | 
2690  | 0  |     int bitspersample = img->bitspersample;  | 
2691  | 0  |     int nsamples = 8 / bitspersample;  | 
2692  | 0  |     int i;  | 
2693  | 0  |     uint32_t *p;  | 
2694  |  | 
  | 
2695  | 0  |     if (nsamples == 0)  | 
2696  | 0  |         nsamples = 1;  | 
2697  |  | 
  | 
2698  | 0  |     img->BWmap = (uint32_t **)_TIFFmallocExt(  | 
2699  | 0  |         img->tif,  | 
2700  | 0  |         256 * sizeof(uint32_t *) + (256 * nsamples * sizeof(uint32_t)));  | 
2701  | 0  |     if (img->BWmap == NULL)  | 
2702  | 0  |     { | 
2703  | 0  |         TIFFErrorExtR(img->tif, TIFFFileName(img->tif),  | 
2704  | 0  |                       "No space for B&W mapping table");  | 
2705  | 0  |         return (0);  | 
2706  | 0  |     }  | 
2707  | 0  |     p = (uint32_t *)(img->BWmap + 256);  | 
2708  | 0  |     for (i = 0; i < 256; i++)  | 
2709  | 0  |     { | 
2710  | 0  |         TIFFRGBValue c;  | 
2711  | 0  |         img->BWmap[i] = p;  | 
2712  | 0  |         switch (bitspersample)  | 
2713  | 0  |         { | 
2714  | 0  | #define GREY(x)                                                                \  | 
2715  | 0  |     c = Map[x];                                                                \  | 
2716  | 0  |     *p++ = PACK(c, c, c);  | 
2717  | 0  |             case 1:  | 
2718  | 0  |                 GREY(i >> 7);  | 
2719  | 0  |                 GREY((i >> 6) & 1);  | 
2720  | 0  |                 GREY((i >> 5) & 1);  | 
2721  | 0  |                 GREY((i >> 4) & 1);  | 
2722  | 0  |                 GREY((i >> 3) & 1);  | 
2723  | 0  |                 GREY((i >> 2) & 1);  | 
2724  | 0  |                 GREY((i >> 1) & 1);  | 
2725  | 0  |                 GREY(i & 1);  | 
2726  | 0  |                 break;  | 
2727  | 0  |             case 2:  | 
2728  | 0  |                 GREY(i >> 6);  | 
2729  | 0  |                 GREY((i >> 4) & 3);  | 
2730  | 0  |                 GREY((i >> 2) & 3);  | 
2731  | 0  |                 GREY(i & 3);  | 
2732  | 0  |                 break;  | 
2733  | 0  |             case 4:  | 
2734  | 0  |                 GREY(i >> 4);  | 
2735  | 0  |                 GREY(i & 0xf);  | 
2736  | 0  |                 break;  | 
2737  | 0  |             case 8:  | 
2738  | 0  |             case 16:  | 
2739  | 0  |                 GREY(i);  | 
2740  | 0  |                 break;  | 
2741  | 0  |         }  | 
2742  | 0  | #undef GREY  | 
2743  | 0  |     }  | 
2744  | 0  |     return (1);  | 
2745  | 0  | }  | 
2746  |  |  | 
2747  |  | /*  | 
2748  |  |  * Construct a mapping table to convert from the range  | 
2749  |  |  * of the data samples to [0,255] --for display.  This  | 
2750  |  |  * process also handles inverting B&W images when needed.  | 
2751  |  |  */  | 
2752  |  | static int setupMap(TIFFRGBAImage *img)  | 
2753  | 0  | { | 
2754  | 0  |     int32_t x, range;  | 
2755  |  | 
  | 
2756  | 0  |     range = (int32_t)((1L << img->bitspersample) - 1);  | 
2757  |  |  | 
2758  |  |     /* treat 16 bit the same as eight bit */  | 
2759  | 0  |     if (img->bitspersample == 16)  | 
2760  | 0  |         range = (int32_t)255;  | 
2761  |  | 
  | 
2762  | 0  |     img->Map = (TIFFRGBValue *)_TIFFmallocExt(  | 
2763  | 0  |         img->tif, (range + 1) * sizeof(TIFFRGBValue));  | 
2764  | 0  |     if (img->Map == NULL)  | 
2765  | 0  |     { | 
2766  | 0  |         TIFFErrorExtR(img->tif, TIFFFileName(img->tif),  | 
2767  | 0  |                       "No space for photometric conversion table");  | 
2768  | 0  |         return (0);  | 
2769  | 0  |     }  | 
2770  | 0  |     if (img->photometric == PHOTOMETRIC_MINISWHITE)  | 
2771  | 0  |     { | 
2772  | 0  |         for (x = 0; x <= range; x++)  | 
2773  | 0  |             img->Map[x] = (TIFFRGBValue)(((range - x) * 255) / range);  | 
2774  | 0  |     }  | 
2775  | 0  |     else  | 
2776  | 0  |     { | 
2777  | 0  |         for (x = 0; x <= range; x++)  | 
2778  | 0  |             img->Map[x] = (TIFFRGBValue)((x * 255) / range);  | 
2779  | 0  |     }  | 
2780  | 0  |     if (img->bitspersample <= 16 &&  | 
2781  | 0  |         (img->photometric == PHOTOMETRIC_MINISBLACK ||  | 
2782  | 0  |          img->photometric == PHOTOMETRIC_MINISWHITE))  | 
2783  | 0  |     { | 
2784  |  |         /*  | 
2785  |  |          * Use photometric mapping table to construct  | 
2786  |  |          * unpacking tables for samples <= 8 bits.  | 
2787  |  |          */  | 
2788  | 0  |         if (!makebwmap(img))  | 
2789  | 0  |             return (0);  | 
2790  |  |         /* no longer need Map, free it */  | 
2791  | 0  |         _TIFFfreeExt(img->tif, img->Map);  | 
2792  | 0  |         img->Map = NULL;  | 
2793  | 0  |     }  | 
2794  | 0  |     return (1);  | 
2795  | 0  | }  | 
2796  |  |  | 
2797  |  | static int checkcmap(TIFFRGBAImage *img)  | 
2798  | 0  | { | 
2799  | 0  |     uint16_t *r = img->redcmap;  | 
2800  | 0  |     uint16_t *g = img->greencmap;  | 
2801  | 0  |     uint16_t *b = img->bluecmap;  | 
2802  | 0  |     long n = 1L << img->bitspersample;  | 
2803  |  | 
  | 
2804  | 0  |     while (n-- > 0)  | 
2805  | 0  |         if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)  | 
2806  | 0  |             return (16);  | 
2807  | 0  |     return (8);  | 
2808  | 0  | }  | 
2809  |  |  | 
2810  |  | static void cvtcmap(TIFFRGBAImage *img)  | 
2811  | 0  | { | 
2812  | 0  |     uint16_t *r = img->redcmap;  | 
2813  | 0  |     uint16_t *g = img->greencmap;  | 
2814  | 0  |     uint16_t *b = img->bluecmap;  | 
2815  | 0  |     long i;  | 
2816  |  | 
  | 
2817  | 0  |     for (i = (1L << img->bitspersample) - 1; i >= 0; i--)  | 
2818  | 0  |     { | 
2819  | 0  | #define CVT(x) ((uint16_t)((x) >> 8))  | 
2820  | 0  |         r[i] = CVT(r[i]);  | 
2821  | 0  |         g[i] = CVT(g[i]);  | 
2822  | 0  |         b[i] = CVT(b[i]);  | 
2823  | 0  | #undef CVT  | 
2824  | 0  |     }  | 
2825  | 0  | }  | 
2826  |  |  | 
2827  |  | /*  | 
2828  |  |  * Palette images with <= 8 bits/sample are handled  | 
2829  |  |  * with a table to avoid lots of shifts and masks.  The table  | 
2830  |  |  * is setup so that put*cmaptile (below) can retrieve 8/bitspersample  | 
2831  |  |  * pixel values simply by indexing into the table with one  | 
2832  |  |  * number.  | 
2833  |  |  */  | 
2834  |  | static int makecmap(TIFFRGBAImage *img)  | 
2835  | 0  | { | 
2836  | 0  |     int bitspersample = img->bitspersample;  | 
2837  | 0  |     int nsamples = 8 / bitspersample;  | 
2838  | 0  |     uint16_t *r = img->redcmap;  | 
2839  | 0  |     uint16_t *g = img->greencmap;  | 
2840  | 0  |     uint16_t *b = img->bluecmap;  | 
2841  | 0  |     uint32_t *p;  | 
2842  | 0  |     int i;  | 
2843  |  | 
  | 
2844  | 0  |     img->PALmap = (uint32_t **)_TIFFmallocExt(  | 
2845  | 0  |         img->tif,  | 
2846  | 0  |         256 * sizeof(uint32_t *) + (256 * nsamples * sizeof(uint32_t)));  | 
2847  | 0  |     if (img->PALmap == NULL)  | 
2848  | 0  |     { | 
2849  | 0  |         TIFFErrorExtR(img->tif, TIFFFileName(img->tif),  | 
2850  | 0  |                       "No space for Palette mapping table");  | 
2851  | 0  |         return (0);  | 
2852  | 0  |     }  | 
2853  | 0  |     p = (uint32_t *)(img->PALmap + 256);  | 
2854  | 0  |     for (i = 0; i < 256; i++)  | 
2855  | 0  |     { | 
2856  | 0  |         TIFFRGBValue c;  | 
2857  | 0  |         img->PALmap[i] = p;  | 
2858  | 0  | #define CMAP(x)                                                                \  | 
2859  | 0  |     c = (TIFFRGBValue)x;                                                       \  | 
2860  | 0  |     *p++ = PACK(r[c] & 0xff, g[c] & 0xff, b[c] & 0xff);  | 
2861  | 0  |         switch (bitspersample)  | 
2862  | 0  |         { | 
2863  | 0  |             case 1:  | 
2864  | 0  |                 CMAP(i >> 7);  | 
2865  | 0  |                 CMAP((i >> 6) & 1);  | 
2866  | 0  |                 CMAP((i >> 5) & 1);  | 
2867  | 0  |                 CMAP((i >> 4) & 1);  | 
2868  | 0  |                 CMAP((i >> 3) & 1);  | 
2869  | 0  |                 CMAP((i >> 2) & 1);  | 
2870  | 0  |                 CMAP((i >> 1) & 1);  | 
2871  | 0  |                 CMAP(i & 1);  | 
2872  | 0  |                 break;  | 
2873  | 0  |             case 2:  | 
2874  | 0  |                 CMAP(i >> 6);  | 
2875  | 0  |                 CMAP((i >> 4) & 3);  | 
2876  | 0  |                 CMAP((i >> 2) & 3);  | 
2877  | 0  |                 CMAP(i & 3);  | 
2878  | 0  |                 break;  | 
2879  | 0  |             case 4:  | 
2880  | 0  |                 CMAP(i >> 4);  | 
2881  | 0  |                 CMAP(i & 0xf);  | 
2882  | 0  |                 break;  | 
2883  | 0  |             case 8:  | 
2884  | 0  |                 CMAP(i);  | 
2885  | 0  |                 break;  | 
2886  | 0  |         }  | 
2887  | 0  | #undef CMAP  | 
2888  | 0  |     }  | 
2889  | 0  |     return (1);  | 
2890  | 0  | }  | 
2891  |  |  | 
2892  |  | /*  | 
2893  |  |  * Construct any mapping table used  | 
2894  |  |  * by the associated put routine.  | 
2895  |  |  */  | 
2896  |  | static int buildMap(TIFFRGBAImage *img)  | 
2897  | 0  | { | 
2898  | 0  |     switch (img->photometric)  | 
2899  | 0  |     { | 
2900  | 0  |         case PHOTOMETRIC_RGB:  | 
2901  | 0  |         case PHOTOMETRIC_YCBCR:  | 
2902  | 0  |         case PHOTOMETRIC_SEPARATED:  | 
2903  | 0  |             if (img->bitspersample == 8)  | 
2904  | 0  |                 break;  | 
2905  |  |             /* fall through... */  | 
2906  | 0  |         case PHOTOMETRIC_MINISBLACK:  | 
2907  | 0  |         case PHOTOMETRIC_MINISWHITE:  | 
2908  | 0  |             if (!setupMap(img))  | 
2909  | 0  |                 return (0);  | 
2910  | 0  |             break;  | 
2911  | 0  |         case PHOTOMETRIC_PALETTE:  | 
2912  |  |             /*  | 
2913  |  |              * Convert 16-bit colormap to 8-bit (unless it looks  | 
2914  |  |              * like an old-style 8-bit colormap).  | 
2915  |  |              */  | 
2916  | 0  |             if (checkcmap(img) == 16)  | 
2917  | 0  |                 cvtcmap(img);  | 
2918  | 0  |             else  | 
2919  | 0  |                 TIFFWarningExtR(img->tif, TIFFFileName(img->tif),  | 
2920  | 0  |                                 "Assuming 8-bit colormap");  | 
2921  |  |             /*  | 
2922  |  |              * Use mapping table and colormap to construct  | 
2923  |  |              * unpacking tables for samples < 8 bits.  | 
2924  |  |              */  | 
2925  | 0  |             if (img->bitspersample <= 8 && !makecmap(img))  | 
2926  | 0  |                 return (0);  | 
2927  | 0  |             break;  | 
2928  | 0  |     }  | 
2929  | 0  |     return (1);  | 
2930  | 0  | }  | 
2931  |  |  | 
2932  |  | /*  | 
2933  |  |  * Select the appropriate conversion routine for packed data.  | 
2934  |  |  */  | 
2935  |  | static int PickContigCase(TIFFRGBAImage *img)  | 
2936  | 0  | { | 
2937  | 0  |     img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;  | 
2938  | 0  |     img->put.contig = NULL;  | 
2939  | 0  |     switch (img->photometric)  | 
2940  | 0  |     { | 
2941  | 0  |         case PHOTOMETRIC_RGB:  | 
2942  | 0  |             switch (img->bitspersample)  | 
2943  | 0  |             { | 
2944  | 0  |                 case 8:  | 
2945  | 0  |                     if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&  | 
2946  | 0  |                         img->samplesperpixel >= 4)  | 
2947  | 0  |                         img->put.contig = putRGBAAcontig8bittile;  | 
2948  | 0  |                     else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&  | 
2949  | 0  |                              img->samplesperpixel >= 4)  | 
2950  | 0  |                     { | 
2951  | 0  |                         if (BuildMapUaToAa(img))  | 
2952  | 0  |                             img->put.contig = putRGBUAcontig8bittile;  | 
2953  | 0  |                     }  | 
2954  | 0  |                     else if (img->samplesperpixel >= 3)  | 
2955  | 0  |                         img->put.contig = putRGBcontig8bittile;  | 
2956  | 0  |                     break;  | 
2957  | 0  |                 case 16:  | 
2958  | 0  |                     if (img->alpha == EXTRASAMPLE_ASSOCALPHA &&  | 
2959  | 0  |                         img->samplesperpixel >= 4)  | 
2960  | 0  |                     { | 
2961  | 0  |                         if (BuildMapBitdepth16To8(img))  | 
2962  | 0  |                             img->put.contig = putRGBAAcontig16bittile;  | 
2963  | 0  |                     }  | 
2964  | 0  |                     else if (img->alpha == EXTRASAMPLE_UNASSALPHA &&  | 
2965  | 0  |                              img->samplesperpixel >= 4)  | 
2966  | 0  |                     { | 
2967  | 0  |                         if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img))  | 
2968  | 0  |                             img->put.contig = putRGBUAcontig16bittile;  | 
2969  | 0  |                     }  | 
2970  | 0  |                     else if (img->samplesperpixel >= 3)  | 
2971  | 0  |                     { | 
2972  | 0  |                         if (BuildMapBitdepth16To8(img))  | 
2973  | 0  |                             img->put.contig = putRGBcontig16bittile;  | 
2974  | 0  |                     }  | 
2975  | 0  |                     break;  | 
2976  | 0  |             }  | 
2977  | 0  |             break;  | 
2978  | 0  |         case PHOTOMETRIC_SEPARATED:  | 
2979  | 0  |             if (img->samplesperpixel >= 4 && buildMap(img))  | 
2980  | 0  |             { | 
2981  | 0  |                 if (img->bitspersample == 8)  | 
2982  | 0  |                 { | 
2983  | 0  |                     if (!img->Map)  | 
2984  | 0  |                         img->put.contig = putRGBcontig8bitCMYKtile;  | 
2985  | 0  |                     else  | 
2986  | 0  |                         img->put.contig = putRGBcontig8bitCMYKMaptile;  | 
2987  | 0  |                 }  | 
2988  | 0  |             }  | 
2989  | 0  |             break;  | 
2990  | 0  |         case PHOTOMETRIC_PALETTE:  | 
2991  | 0  |             if (buildMap(img))  | 
2992  | 0  |             { | 
2993  | 0  |                 switch (img->bitspersample)  | 
2994  | 0  |                 { | 
2995  | 0  |                     case 8:  | 
2996  | 0  |                         img->put.contig = put8bitcmaptile;  | 
2997  | 0  |                         break;  | 
2998  | 0  |                     case 4:  | 
2999  | 0  |                         img->put.contig = put4bitcmaptile;  | 
3000  | 0  |                         break;  | 
3001  | 0  |                     case 2:  | 
3002  | 0  |                         img->put.contig = put2bitcmaptile;  | 
3003  | 0  |                         break;  | 
3004  | 0  |                     case 1:  | 
3005  | 0  |                         img->put.contig = put1bitcmaptile;  | 
3006  | 0  |                         break;  | 
3007  | 0  |                 }  | 
3008  | 0  |             }  | 
3009  | 0  |             break;  | 
3010  | 0  |         case PHOTOMETRIC_MINISWHITE:  | 
3011  | 0  |         case PHOTOMETRIC_MINISBLACK:  | 
3012  | 0  |             if (buildMap(img))  | 
3013  | 0  |             { | 
3014  | 0  |                 switch (img->bitspersample)  | 
3015  | 0  |                 { | 
3016  | 0  |                     case 16:  | 
3017  | 0  |                         img->put.contig = put16bitbwtile;  | 
3018  | 0  |                         break;  | 
3019  | 0  |                     case 8:  | 
3020  | 0  |                         if (img->alpha && img->samplesperpixel == 2)  | 
3021  | 0  |                             img->put.contig = putagreytile;  | 
3022  | 0  |                         else  | 
3023  | 0  |                             img->put.contig = putgreytile;  | 
3024  | 0  |                         break;  | 
3025  | 0  |                     case 4:  | 
3026  | 0  |                         img->put.contig = put4bitbwtile;  | 
3027  | 0  |                         break;  | 
3028  | 0  |                     case 2:  | 
3029  | 0  |                         img->put.contig = put2bitbwtile;  | 
3030  | 0  |                         break;  | 
3031  | 0  |                     case 1:  | 
3032  | 0  |                         img->put.contig = put1bitbwtile;  | 
3033  | 0  |                         break;  | 
3034  | 0  |                 }  | 
3035  | 0  |             }  | 
3036  | 0  |             break;  | 
3037  | 0  |         case PHOTOMETRIC_YCBCR:  | 
3038  | 0  |             if ((img->bitspersample == 8) && (img->samplesperpixel == 3))  | 
3039  | 0  |             { | 
3040  | 0  |                 if (initYCbCrConversion(img) != 0)  | 
3041  | 0  |                 { | 
3042  |  |                     /*  | 
3043  |  |                      * The 6.0 spec says that subsampling must be  | 
3044  |  |                      * one of 1, 2, or 4, and that vertical subsampling  | 
3045  |  |                      * must always be <= horizontal subsampling; so  | 
3046  |  |                      * there are only a few possibilities and we just  | 
3047  |  |                      * enumerate the cases.  | 
3048  |  |                      * Joris: added support for the [1,2] case, nonetheless, to  | 
3049  |  |                      * accommodate some OJPEG files  | 
3050  |  |                      */  | 
3051  | 0  |                     uint16_t SubsamplingHor;  | 
3052  | 0  |                     uint16_t SubsamplingVer;  | 
3053  | 0  |                     TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING,  | 
3054  | 0  |                                           &SubsamplingHor, &SubsamplingVer);  | 
3055  | 0  |                     switch ((SubsamplingHor << 4) | SubsamplingVer)  | 
3056  | 0  |                     { | 
3057  | 0  |                         case 0x44:  | 
3058  | 0  |                             img->put.contig = putcontig8bitYCbCr44tile;  | 
3059  | 0  |                             break;  | 
3060  | 0  |                         case 0x42:  | 
3061  | 0  |                             img->put.contig = putcontig8bitYCbCr42tile;  | 
3062  | 0  |                             break;  | 
3063  | 0  |                         case 0x41:  | 
3064  | 0  |                             img->put.contig = putcontig8bitYCbCr41tile;  | 
3065  | 0  |                             break;  | 
3066  | 0  |                         case 0x22:  | 
3067  | 0  |                             img->put.contig = putcontig8bitYCbCr22tile;  | 
3068  | 0  |                             break;  | 
3069  | 0  |                         case 0x21:  | 
3070  | 0  |                             img->put.contig = putcontig8bitYCbCr21tile;  | 
3071  | 0  |                             break;  | 
3072  | 0  |                         case 0x12:  | 
3073  | 0  |                             img->put.contig = putcontig8bitYCbCr12tile;  | 
3074  | 0  |                             break;  | 
3075  | 0  |                         case 0x11:  | 
3076  | 0  |                             img->put.contig = putcontig8bitYCbCr11tile;  | 
3077  | 0  |                             break;  | 
3078  | 0  |                     }  | 
3079  | 0  |                 }  | 
3080  | 0  |             }  | 
3081  | 0  |             break;  | 
3082  | 0  |         case PHOTOMETRIC_CIELAB:  | 
3083  | 0  |             if (img->samplesperpixel == 3 && buildMap(img))  | 
3084  | 0  |             { | 
3085  | 0  |                 if (img->bitspersample == 8 || img->bitspersample == 16)  | 
3086  | 0  |                     img->put.contig = initCIELabConversion(img);  | 
3087  | 0  |                 break;  | 
3088  | 0  |             }  | 
3089  | 0  |     }  | 
3090  | 0  |     return ((img->get != NULL) && (img->put.contig != NULL));  | 
3091  | 0  | }  | 
3092  |  |  | 
3093  |  | /*  | 
3094  |  |  * Select the appropriate conversion routine for unpacked data.  | 
3095  |  |  *  | 
3096  |  |  * NB: we assume that unpacked single channel data is directed  | 
3097  |  |  *   to the "packed routines.  | 
3098  |  |  */  | 
3099  |  | static int PickSeparateCase(TIFFRGBAImage *img)  | 
3100  | 0  | { | 
3101  | 0  |     img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;  | 
3102  | 0  |     img->put.separate = NULL;  | 
3103  | 0  |     switch (img->photometric)  | 
3104  | 0  |     { | 
3105  | 0  |         case PHOTOMETRIC_MINISWHITE:  | 
3106  | 0  |         case PHOTOMETRIC_MINISBLACK:  | 
3107  |  |             /* greyscale images processed pretty much as RGB by gtTileSeparate  | 
3108  |  |              */  | 
3109  | 0  |         case PHOTOMETRIC_RGB:  | 
3110  | 0  |             switch (img->bitspersample)  | 
3111  | 0  |             { | 
3112  | 0  |                 case 8:  | 
3113  | 0  |                     if (img->alpha == EXTRASAMPLE_ASSOCALPHA)  | 
3114  | 0  |                         img->put.separate = putRGBAAseparate8bittile;  | 
3115  | 0  |                     else if (img->alpha == EXTRASAMPLE_UNASSALPHA)  | 
3116  | 0  |                     { | 
3117  | 0  |                         if (BuildMapUaToAa(img))  | 
3118  | 0  |                             img->put.separate = putRGBUAseparate8bittile;  | 
3119  | 0  |                     }  | 
3120  | 0  |                     else  | 
3121  | 0  |                         img->put.separate = putRGBseparate8bittile;  | 
3122  | 0  |                     break;  | 
3123  | 0  |                 case 16:  | 
3124  | 0  |                     if (img->alpha == EXTRASAMPLE_ASSOCALPHA)  | 
3125  | 0  |                     { | 
3126  | 0  |                         if (BuildMapBitdepth16To8(img))  | 
3127  | 0  |                             img->put.separate = putRGBAAseparate16bittile;  | 
3128  | 0  |                     }  | 
3129  | 0  |                     else if (img->alpha == EXTRASAMPLE_UNASSALPHA)  | 
3130  | 0  |                     { | 
3131  | 0  |                         if (BuildMapBitdepth16To8(img) && BuildMapUaToAa(img))  | 
3132  | 0  |                             img->put.separate = putRGBUAseparate16bittile;  | 
3133  | 0  |                     }  | 
3134  | 0  |                     else  | 
3135  | 0  |                     { | 
3136  | 0  |                         if (BuildMapBitdepth16To8(img))  | 
3137  | 0  |                             img->put.separate = putRGBseparate16bittile;  | 
3138  | 0  |                     }  | 
3139  | 0  |                     break;  | 
3140  | 0  |             }  | 
3141  | 0  |             break;  | 
3142  | 0  |         case PHOTOMETRIC_SEPARATED:  | 
3143  | 0  |             if (img->bitspersample == 8 && img->samplesperpixel == 4)  | 
3144  | 0  |             { | 
3145  | 0  |                 img->alpha =  | 
3146  | 0  |                     1; // Not alpha, but seems like the only way to get 4th band  | 
3147  | 0  |                 img->put.separate = putCMYKseparate8bittile;  | 
3148  | 0  |             }  | 
3149  | 0  |             break;  | 
3150  | 0  |         case PHOTOMETRIC_YCBCR:  | 
3151  | 0  |             if ((img->bitspersample == 8) && (img->samplesperpixel == 3))  | 
3152  | 0  |             { | 
3153  | 0  |                 if (initYCbCrConversion(img) != 0)  | 
3154  | 0  |                 { | 
3155  | 0  |                     uint16_t hs, vs;  | 
3156  | 0  |                     TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING,  | 
3157  | 0  |                                           &hs, &vs);  | 
3158  | 0  |                     switch ((hs << 4) | vs)  | 
3159  | 0  |                     { | 
3160  | 0  |                         case 0x11:  | 
3161  | 0  |                             img->put.separate = putseparate8bitYCbCr11tile;  | 
3162  | 0  |                             break;  | 
3163  |  |                             /* TODO: add other cases here */  | 
3164  | 0  |                     }  | 
3165  | 0  |                 }  | 
3166  | 0  |             }  | 
3167  | 0  |             break;  | 
3168  | 0  |     }  | 
3169  | 0  |     return ((img->get != NULL) && (img->put.separate != NULL));  | 
3170  | 0  | }  | 
3171  |  |  | 
3172  |  | static int BuildMapUaToAa(TIFFRGBAImage *img)  | 
3173  | 0  | { | 
3174  | 0  |     static const char module[] = "BuildMapUaToAa";  | 
3175  | 0  |     uint8_t *m;  | 
3176  | 0  |     uint16_t na, nv;  | 
3177  | 0  |     assert(img->UaToAa == NULL);  | 
3178  | 0  |     img->UaToAa = _TIFFmallocExt(img->tif, 65536);  | 
3179  | 0  |     if (img->UaToAa == NULL)  | 
3180  | 0  |     { | 
3181  | 0  |         TIFFErrorExtR(img->tif, module, "Out of memory");  | 
3182  | 0  |         return (0);  | 
3183  | 0  |     }  | 
3184  | 0  |     m = img->UaToAa;  | 
3185  | 0  |     for (na = 0; na < 256; na++)  | 
3186  | 0  |     { | 
3187  | 0  |         for (nv = 0; nv < 256; nv++)  | 
3188  | 0  |             *m++ = (uint8_t)((nv * na + 127) / 255);  | 
3189  | 0  |     }  | 
3190  | 0  |     return (1);  | 
3191  | 0  | }  | 
3192  |  |  | 
3193  |  | static int BuildMapBitdepth16To8(TIFFRGBAImage *img)  | 
3194  | 0  | { | 
3195  | 0  |     static const char module[] = "BuildMapBitdepth16To8";  | 
3196  | 0  |     uint8_t *m;  | 
3197  | 0  |     uint32_t n;  | 
3198  | 0  |     assert(img->Bitdepth16To8 == NULL);  | 
3199  | 0  |     img->Bitdepth16To8 = _TIFFmallocExt(img->tif, 65536);  | 
3200  | 0  |     if (img->Bitdepth16To8 == NULL)  | 
3201  | 0  |     { | 
3202  | 0  |         TIFFErrorExtR(img->tif, module, "Out of memory");  | 
3203  | 0  |         return (0);  | 
3204  | 0  |     }  | 
3205  | 0  |     m = img->Bitdepth16To8;  | 
3206  | 0  |     for (n = 0; n < 65536; n++)  | 
3207  | 0  |         *m++ = (uint8_t)((n + 128) / 257);  | 
3208  | 0  |     return (1);  | 
3209  | 0  | }  | 
3210  |  |  | 
3211  |  | /*  | 
3212  |  |  * Read a whole strip off data from the file, and convert to RGBA form.  | 
3213  |  |  * If this is the last strip, then it will only contain the portion of  | 
3214  |  |  * the strip that is actually within the image space.  The result is  | 
3215  |  |  * organized in bottom to top form.  | 
3216  |  |  */  | 
3217  |  |  | 
3218  |  | int TIFFReadRGBAStrip(TIFF *tif, uint32_t row, uint32_t *raster)  | 
3219  |  |  | 
3220  | 0  | { | 
3221  | 0  |     return TIFFReadRGBAStripExt(tif, row, raster, 0);  | 
3222  | 0  | }  | 
3223  |  |  | 
3224  |  | int TIFFReadRGBAStripExt(TIFF *tif, uint32_t row, uint32_t *raster,  | 
3225  |  |                          int stop_on_error)  | 
3226  |  |  | 
3227  | 0  | { | 
3228  | 0  |     char emsg[EMSG_BUF_SIZE] = "";  | 
3229  | 0  |     TIFFRGBAImage img;  | 
3230  | 0  |     int ok;  | 
3231  | 0  |     uint32_t rowsperstrip, rows_to_read;  | 
3232  |  | 
  | 
3233  | 0  |     if (TIFFIsTiled(tif))  | 
3234  | 0  |     { | 
3235  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif),  | 
3236  | 0  |                       "Can't use TIFFReadRGBAStrip() with tiled file.");  | 
3237  | 0  |         return (0);  | 
3238  | 0  |     }  | 
3239  |  |  | 
3240  | 0  |     TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);  | 
3241  |  | 
  | 
3242  | 0  |     if (rowsperstrip == 0)  | 
3243  | 0  |     { | 
3244  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");  | 
3245  | 0  |         return (0);  | 
3246  | 0  |     }  | 
3247  |  |  | 
3248  | 0  |     if ((row % rowsperstrip) != 0)  | 
3249  | 0  |     { | 
3250  | 0  |         TIFFErrorExtR(  | 
3251  | 0  |             tif, TIFFFileName(tif),  | 
3252  | 0  |             "Row passed to TIFFReadRGBAStrip() must be first in a strip.");  | 
3253  | 0  |         return (0);  | 
3254  | 0  |     }  | 
3255  |  |  | 
3256  | 0  |     if (TIFFRGBAImageOK(tif, emsg) &&  | 
3257  | 0  |         TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg))  | 
3258  | 0  |     { | 
3259  | 0  |         if (row >= img.height)  | 
3260  | 0  |         { | 
3261  | 0  |             TIFFErrorExtR(tif, TIFFFileName(tif),  | 
3262  | 0  |                           "Invalid row passed to TIFFReadRGBAStrip().");  | 
3263  | 0  |             TIFFRGBAImageEnd(&img);  | 
3264  | 0  |             return (0);  | 
3265  | 0  |         }  | 
3266  |  |  | 
3267  | 0  |         img.row_offset = row;  | 
3268  | 0  |         img.col_offset = 0;  | 
3269  |  | 
  | 
3270  | 0  |         if (row + rowsperstrip > img.height)  | 
3271  | 0  |             rows_to_read = img.height - row;  | 
3272  | 0  |         else  | 
3273  | 0  |             rows_to_read = rowsperstrip;  | 
3274  |  | 
  | 
3275  | 0  |         ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read);  | 
3276  |  | 
  | 
3277  | 0  |         TIFFRGBAImageEnd(&img);  | 
3278  | 0  |     }  | 
3279  | 0  |     else  | 
3280  | 0  |     { | 
3281  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);  | 
3282  | 0  |         ok = 0;  | 
3283  | 0  |     }  | 
3284  |  |  | 
3285  | 0  |     return (ok);  | 
3286  | 0  | }  | 
3287  |  |  | 
3288  |  | /*  | 
3289  |  |  * Read a whole tile off data from the file, and convert to RGBA form.  | 
3290  |  |  * The returned RGBA data is organized from bottom to top of tile,  | 
3291  |  |  * and may include zeroed areas if the tile extends off the image.  | 
3292  |  |  */  | 
3293  |  |  | 
3294  |  | int TIFFReadRGBATile(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster)  | 
3295  |  |  | 
3296  | 0  | { | 
3297  | 0  |     return TIFFReadRGBATileExt(tif, col, row, raster, 0);  | 
3298  | 0  | }  | 
3299  |  |  | 
3300  |  | int TIFFReadRGBATileExt(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster,  | 
3301  |  |                         int stop_on_error)  | 
3302  | 0  | { | 
3303  | 0  |     char emsg[EMSG_BUF_SIZE] = "";  | 
3304  | 0  |     TIFFRGBAImage img;  | 
3305  | 0  |     int ok;  | 
3306  | 0  |     uint32_t tile_xsize, tile_ysize;  | 
3307  | 0  |     uint32_t read_xsize, read_ysize;  | 
3308  | 0  |     uint32_t i_row;  | 
3309  |  |  | 
3310  |  |     /*  | 
3311  |  |      * Verify that our request is legal - on a tile file, and on a  | 
3312  |  |      * tile boundary.  | 
3313  |  |      */  | 
3314  |  | 
  | 
3315  | 0  |     if (!TIFFIsTiled(tif))  | 
3316  | 0  |     { | 
3317  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif),  | 
3318  | 0  |                       "Can't use TIFFReadRGBATile() with striped file.");  | 
3319  | 0  |         return (0);  | 
3320  | 0  |     }  | 
3321  |  |  | 
3322  | 0  |     TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);  | 
3323  | 0  |     TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);  | 
3324  | 0  |     if (tile_xsize == 0 || tile_ysize == 0)  | 
3325  | 0  |     { | 
3326  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif), "tile_xsize or tile_ysize is zero");  | 
3327  | 0  |         return (0);  | 
3328  | 0  |     }  | 
3329  |  |  | 
3330  | 0  |     if ((col % tile_xsize) != 0 || (row % tile_ysize) != 0)  | 
3331  | 0  |     { | 
3332  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif),  | 
3333  | 0  |                       "Row/col passed to TIFFReadRGBATile() must be top"  | 
3334  | 0  |                       "left corner of a tile.");  | 
3335  | 0  |         return (0);  | 
3336  | 0  |     }  | 
3337  |  |  | 
3338  |  |     /*  | 
3339  |  |      * Setup the RGBA reader.  | 
3340  |  |      */  | 
3341  |  |  | 
3342  | 0  |     if (!TIFFRGBAImageOK(tif, emsg) ||  | 
3343  | 0  |         !TIFFRGBAImageBegin(&img, tif, stop_on_error, emsg))  | 
3344  | 0  |     { | 
3345  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif), "%s", emsg);  | 
3346  | 0  |         return (0);  | 
3347  | 0  |     }  | 
3348  |  |  | 
3349  | 0  |     if (col >= img.width || row >= img.height)  | 
3350  | 0  |     { | 
3351  | 0  |         TIFFErrorExtR(tif, TIFFFileName(tif),  | 
3352  | 0  |                       "Invalid row/col passed to TIFFReadRGBATile().");  | 
3353  | 0  |         TIFFRGBAImageEnd(&img);  | 
3354  | 0  |         return (0);  | 
3355  | 0  |     }  | 
3356  |  |  | 
3357  |  |     /*  | 
3358  |  |      * The TIFFRGBAImageGet() function doesn't allow us to get off the  | 
3359  |  |      * edge of the image, even to fill an otherwise valid tile.  So we  | 
3360  |  |      * figure out how much we can read, and fix up the tile buffer to  | 
3361  |  |      * a full tile configuration afterwards.  | 
3362  |  |      */  | 
3363  |  |  | 
3364  | 0  |     if (row + tile_ysize > img.height)  | 
3365  | 0  |         read_ysize = img.height - row;  | 
3366  | 0  |     else  | 
3367  | 0  |         read_ysize = tile_ysize;  | 
3368  |  | 
  | 
3369  | 0  |     if (col + tile_xsize > img.width)  | 
3370  | 0  |         read_xsize = img.width - col;  | 
3371  | 0  |     else  | 
3372  | 0  |         read_xsize = tile_xsize;  | 
3373  |  |  | 
3374  |  |     /*  | 
3375  |  |      * Read the chunk of imagery.  | 
3376  |  |      */  | 
3377  |  | 
  | 
3378  | 0  |     img.row_offset = row;  | 
3379  | 0  |     img.col_offset = col;  | 
3380  |  | 
  | 
3381  | 0  |     ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize);  | 
3382  |  | 
  | 
3383  | 0  |     TIFFRGBAImageEnd(&img);  | 
3384  |  |  | 
3385  |  |     /*  | 
3386  |  |      * If our read was incomplete we will need to fix up the tile by  | 
3387  |  |      * shifting the data around as if a full tile of data is being returned.  | 
3388  |  |      *  | 
3389  |  |      * This is all the more complicated because the image is organized in  | 
3390  |  |      * bottom to top format.  | 
3391  |  |      */  | 
3392  |  | 
  | 
3393  | 0  |     if (read_xsize == tile_xsize && read_ysize == tile_ysize)  | 
3394  | 0  |         return (ok);  | 
3395  |  |  | 
3396  | 0  |     for (i_row = 0; i_row < read_ysize; i_row++)  | 
3397  | 0  |     { | 
3398  | 0  |         memmove(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize,  | 
3399  | 0  |                 raster + (size_t)(read_ysize - i_row - 1) * read_xsize,  | 
3400  | 0  |                 read_xsize * sizeof(uint32_t));  | 
3401  | 0  |         _TIFFmemset(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize +  | 
3402  | 0  |                         read_xsize,  | 
3403  | 0  |                     0, sizeof(uint32_t) * (tile_xsize - read_xsize));  | 
3404  | 0  |     }  | 
3405  |  | 
  | 
3406  | 0  |     for (i_row = read_ysize; i_row < tile_ysize; i_row++)  | 
3407  | 0  |     { | 
3408  | 0  |         _TIFFmemset(raster + (size_t)(tile_ysize - i_row - 1) * tile_xsize, 0,  | 
3409  | 0  |                     sizeof(uint32_t) * tile_xsize);  | 
3410  | 0  |     }  | 
3411  |  | 
  | 
3412  | 0  |     return (ok);  | 
3413  | 0  | }  |