/src/freeimage-svn/FreeImage/trunk/Source/LibTIFF4/tif_strip.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  |  |  * Strip-organized Image Support Routines.  | 
29  |  |  */  | 
30  |  | #include "tiffiop.h"  | 
31  |  |  | 
32  |  | /*  | 
33  |  |  * Compute which strip a (row,sample) value is in.  | 
34  |  |  */  | 
35  |  | uint32_t TIFFComputeStrip(TIFF *tif, uint32_t row, uint16_t sample)  | 
36  | 0  | { | 
37  | 0  |     static const char module[] = "TIFFComputeStrip";  | 
38  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
39  | 0  |     uint32_t strip;  | 
40  |  | 
  | 
41  | 0  |     strip = row / td->td_rowsperstrip;  | 
42  | 0  |     if (td->td_planarconfig == PLANARCONFIG_SEPARATE)  | 
43  | 0  |     { | 
44  | 0  |         if (sample >= td->td_samplesperpixel)  | 
45  | 0  |         { | 
46  | 0  |             TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu",  | 
47  | 0  |                           (unsigned long)sample,  | 
48  | 0  |                           (unsigned long)td->td_samplesperpixel);  | 
49  | 0  |             return (0);  | 
50  | 0  |         }  | 
51  | 0  |         strip += (uint32_t)sample * td->td_stripsperimage;  | 
52  | 0  |     }  | 
53  | 0  |     return (strip);  | 
54  | 0  | }  | 
55  |  |  | 
56  |  | /*  | 
57  |  |  * Compute how many strips are in an image.  | 
58  |  |  */  | 
59  |  | uint32_t TIFFNumberOfStrips(TIFF *tif)  | 
60  | 0  | { | 
61  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
62  | 0  |     uint32_t nstrips;  | 
63  |  | 
  | 
64  | 0  |     if (td->td_rowsperstrip == 0)  | 
65  | 0  |     { | 
66  | 0  |         TIFFWarningExtR(tif, "TIFFNumberOfStrips", "RowsPerStrip is zero");  | 
67  | 0  |         return 0;  | 
68  | 0  |     }  | 
69  | 0  |     nstrips = (td->td_rowsperstrip == (uint32_t)-1  | 
70  | 0  |                    ? 1  | 
71  | 0  |                    : TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));  | 
72  | 0  |     if (td->td_planarconfig == PLANARCONFIG_SEPARATE)  | 
73  | 0  |         nstrips =  | 
74  | 0  |             _TIFFMultiply32(tif, nstrips, (uint32_t)td->td_samplesperpixel,  | 
75  | 0  |                             "TIFFNumberOfStrips");  | 
76  | 0  |     return (nstrips);  | 
77  | 0  | }  | 
78  |  |  | 
79  |  | /*  | 
80  |  |  * Compute the # bytes in a variable height, row-aligned strip.  | 
81  |  |  */  | 
82  |  | uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows)  | 
83  | 0  | { | 
84  | 0  |     static const char module[] = "TIFFVStripSize64";  | 
85  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
86  | 0  |     if (nrows == (uint32_t)(-1))  | 
87  | 0  |         nrows = td->td_imagelength;  | 
88  | 0  |     if ((td->td_planarconfig == PLANARCONFIG_CONTIG) &&  | 
89  | 0  |         (td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))  | 
90  | 0  |     { | 
91  |  |         /*  | 
92  |  |          * Packed YCbCr data contain one Cb+Cr for every  | 
93  |  |          * HorizontalSampling*VerticalSampling Y values.  | 
94  |  |          * Must also roundup width and height when calculating  | 
95  |  |          * since images that are not a multiple of the  | 
96  |  |          * horizontal/vertical subsampling area include  | 
97  |  |          * YCbCr data for the extended image.  | 
98  |  |          */  | 
99  | 0  |         uint16_t ycbcrsubsampling[2];  | 
100  | 0  |         uint16_t samplingblock_samples;  | 
101  | 0  |         uint32_t samplingblocks_hor;  | 
102  | 0  |         uint32_t samplingblocks_ver;  | 
103  | 0  |         uint64_t samplingrow_samples;  | 
104  | 0  |         uint64_t samplingrow_size;  | 
105  | 0  |         if (td->td_samplesperpixel != 3)  | 
106  | 0  |         { | 
107  | 0  |             TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");  | 
108  | 0  |             return 0;  | 
109  | 0  |         }  | 
110  | 0  |         TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,  | 
111  | 0  |                               ycbcrsubsampling + 0, ycbcrsubsampling + 1);  | 
112  | 0  |         if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 &&  | 
113  | 0  |              ycbcrsubsampling[0] != 4) ||  | 
114  | 0  |             (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 &&  | 
115  | 0  |              ycbcrsubsampling[1] != 4) ||  | 
116  | 0  |             (ycbcrsubsampling[0] == 0 || ycbcrsubsampling[1] == 0))  | 
117  | 0  |         { | 
118  | 0  |             TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling (%dx%d)",  | 
119  | 0  |                           ycbcrsubsampling[0], ycbcrsubsampling[1]);  | 
120  | 0  |             return 0;  | 
121  | 0  |         }  | 
122  | 0  |         samplingblock_samples = ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2;  | 
123  | 0  |         samplingblocks_hor =  | 
124  | 0  |             TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]);  | 
125  | 0  |         samplingblocks_ver = TIFFhowmany_32(nrows, ycbcrsubsampling[1]);  | 
126  | 0  |         samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor,  | 
127  | 0  |                                               samplingblock_samples, module);  | 
128  | 0  |         samplingrow_size = TIFFhowmany8_64(_TIFFMultiply64(  | 
129  | 0  |             tif, samplingrow_samples, td->td_bitspersample, module));  | 
130  | 0  |         return (  | 
131  | 0  |             _TIFFMultiply64(tif, samplingrow_size, samplingblocks_ver, module));  | 
132  | 0  |     }  | 
133  | 0  |     else  | 
134  | 0  |         return (_TIFFMultiply64(tif, nrows, TIFFScanlineSize64(tif), module));  | 
135  | 0  | }  | 
136  |  | tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows)  | 
137  | 0  | { | 
138  | 0  |     static const char module[] = "TIFFVStripSize";  | 
139  | 0  |     uint64_t m;  | 
140  | 0  |     m = TIFFVStripSize64(tif, nrows);  | 
141  | 0  |     return _TIFFCastUInt64ToSSize(tif, m, module);  | 
142  | 0  | }  | 
143  |  |  | 
144  |  | /*  | 
145  |  |  * Compute the # bytes in a raw strip.  | 
146  |  |  */  | 
147  |  | uint64_t TIFFRawStripSize64(TIFF *tif, uint32_t strip)  | 
148  | 0  | { | 
149  | 0  |     static const char module[] = "TIFFRawStripSize64";  | 
150  | 0  |     uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);  | 
151  |  | 
  | 
152  | 0  |     if (bytecount == 0)  | 
153  | 0  |     { | 
154  | 0  |         TIFFErrorExtR(tif, module,  | 
155  | 0  |                       "%" PRIu64 ": Invalid strip byte count, strip %lu",  | 
156  | 0  |                       (uint64_t)bytecount, (unsigned long)strip);  | 
157  | 0  |         bytecount = (uint64_t)-1;  | 
158  | 0  |     }  | 
159  |  | 
  | 
160  | 0  |     return bytecount;  | 
161  | 0  | }  | 
162  |  | tmsize_t TIFFRawStripSize(TIFF *tif, uint32_t strip)  | 
163  | 0  | { | 
164  | 0  |     static const char module[] = "TIFFRawStripSize";  | 
165  | 0  |     uint64_t m;  | 
166  | 0  |     tmsize_t n;  | 
167  | 0  |     m = TIFFRawStripSize64(tif, strip);  | 
168  | 0  |     if (m == (uint64_t)(-1))  | 
169  | 0  |         n = (tmsize_t)(-1);  | 
170  | 0  |     else  | 
171  | 0  |     { | 
172  | 0  |         n = (tmsize_t)m;  | 
173  | 0  |         if ((uint64_t)n != m)  | 
174  | 0  |         { | 
175  | 0  |             TIFFErrorExtR(tif, module, "Integer overflow");  | 
176  | 0  |             n = 0;  | 
177  | 0  |         }  | 
178  | 0  |     }  | 
179  | 0  |     return (n);  | 
180  | 0  | }  | 
181  |  |  | 
182  |  | /*  | 
183  |  |  * Compute the # bytes in a (row-aligned) strip.  | 
184  |  |  *  | 
185  |  |  * Note that if RowsPerStrip is larger than the  | 
186  |  |  * recorded ImageLength, then the strip size is  | 
187  |  |  * truncated to reflect the actual space required  | 
188  |  |  * to hold the strip.  | 
189  |  |  */  | 
190  |  | uint64_t TIFFStripSize64(TIFF *tif)  | 
191  | 0  | { | 
192  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
193  | 0  |     uint32_t rps = td->td_rowsperstrip;  | 
194  | 0  |     if (rps > td->td_imagelength)  | 
195  | 0  |         rps = td->td_imagelength;  | 
196  | 0  |     return (TIFFVStripSize64(tif, rps));  | 
197  | 0  | }  | 
198  |  | tmsize_t TIFFStripSize(TIFF *tif)  | 
199  | 0  | { | 
200  | 0  |     static const char module[] = "TIFFStripSize";  | 
201  | 0  |     uint64_t m;  | 
202  | 0  |     m = TIFFStripSize64(tif);  | 
203  | 0  |     return _TIFFCastUInt64ToSSize(tif, m, module);  | 
204  | 0  | }  | 
205  |  |  | 
206  |  | /*  | 
207  |  |  * Compute a default strip size based on the image  | 
208  |  |  * characteristics and a requested value.  If the  | 
209  |  |  * request is <1 then we choose a strip size according  | 
210  |  |  * to certain heuristics.  | 
211  |  |  */  | 
212  |  | uint32_t TIFFDefaultStripSize(TIFF *tif, uint32_t request)  | 
213  | 0  | { | 
214  | 0  |     return (*tif->tif_defstripsize)(tif, request);  | 
215  | 0  | }  | 
216  |  |  | 
217  |  | uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s)  | 
218  | 0  | { | 
219  | 0  |     if ((int32_t)s < 1)  | 
220  | 0  |     { | 
221  |  |         /*  | 
222  |  |          * If RowsPerStrip is unspecified, try to break the  | 
223  |  |          * image up into strips that are approximately  | 
224  |  |          * STRIP_SIZE_DEFAULT bytes long.  | 
225  |  |          */  | 
226  | 0  |         uint64_t scanlinesize;  | 
227  | 0  |         uint64_t rows;  | 
228  | 0  |         scanlinesize = TIFFScanlineSize64(tif);  | 
229  | 0  |         if (scanlinesize == 0)  | 
230  | 0  |             scanlinesize = 1;  | 
231  | 0  |         rows = (uint64_t)STRIP_SIZE_DEFAULT / scanlinesize;  | 
232  | 0  |         if (rows == 0)  | 
233  | 0  |             rows = 1;  | 
234  | 0  |         else if (rows > 0xFFFFFFFF)  | 
235  | 0  |             rows = 0xFFFFFFFF;  | 
236  | 0  |         s = (uint32_t)rows;  | 
237  | 0  |     }  | 
238  | 0  |     return (s);  | 
239  | 0  | }  | 
240  |  |  | 
241  |  | /*  | 
242  |  |  * Return the number of bytes to read/write in a call to  | 
243  |  |  * one of the scanline-oriented i/o routines.  Note that  | 
244  |  |  * this number may be 1/samples-per-pixel if data is  | 
245  |  |  * stored as separate planes.  | 
246  |  |  * The ScanlineSize in case of YCbCrSubsampling is defined as the  | 
247  |  |  * strip size divided by the strip height, i.e. the size of a pack of vertical  | 
248  |  |  * subsampling lines divided by vertical subsampling. It should thus make  | 
249  |  |  * sense when multiplied by a multiple of vertical subsampling.  | 
250  |  |  */  | 
251  |  | uint64_t TIFFScanlineSize64(TIFF *tif)  | 
252  | 0  | { | 
253  | 0  |     static const char module[] = "TIFFScanlineSize64";  | 
254  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
255  | 0  |     uint64_t scanline_size;  | 
256  | 0  |     if (td->td_planarconfig == PLANARCONFIG_CONTIG)  | 
257  | 0  |     { | 
258  | 0  |         if ((td->td_photometric == PHOTOMETRIC_YCBCR) &&  | 
259  | 0  |             (td->td_samplesperpixel == 3) && (!isUpSampled(tif)))  | 
260  | 0  |         { | 
261  | 0  |             uint16_t ycbcrsubsampling[2];  | 
262  | 0  |             uint16_t samplingblock_samples;  | 
263  | 0  |             uint32_t samplingblocks_hor;  | 
264  | 0  |             uint64_t samplingrow_samples;  | 
265  | 0  |             uint64_t samplingrow_size;  | 
266  | 0  |             if (td->td_samplesperpixel != 3)  | 
267  | 0  |             { | 
268  | 0  |                 TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");  | 
269  | 0  |                 return 0;  | 
270  | 0  |             }  | 
271  | 0  |             TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,  | 
272  | 0  |                                   ycbcrsubsampling + 0, ycbcrsubsampling + 1);  | 
273  | 0  |             if (((ycbcrsubsampling[0] != 1) && (ycbcrsubsampling[0] != 2) &&  | 
274  | 0  |                  (ycbcrsubsampling[0] != 4)) ||  | 
275  | 0  |                 ((ycbcrsubsampling[1] != 1) && (ycbcrsubsampling[1] != 2) &&  | 
276  | 0  |                  (ycbcrsubsampling[1] != 4)) ||  | 
277  | 0  |                 ((ycbcrsubsampling[0] == 0) || (ycbcrsubsampling[1] == 0)))  | 
278  | 0  |             { | 
279  | 0  |                 TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling");  | 
280  | 0  |                 return 0;  | 
281  | 0  |             }  | 
282  | 0  |             samplingblock_samples =  | 
283  | 0  |                 ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2;  | 
284  | 0  |             samplingblocks_hor =  | 
285  | 0  |                 TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]);  | 
286  | 0  |             samplingrow_samples = _TIFFMultiply64(  | 
287  | 0  |                 tif, samplingblocks_hor, samplingblock_samples, module);  | 
288  | 0  |             samplingrow_size =  | 
289  | 0  |                 TIFFhowmany_64(_TIFFMultiply64(tif, samplingrow_samples,  | 
290  | 0  |                                                td->td_bitspersample, module),  | 
291  | 0  |                                8);  | 
292  | 0  |             scanline_size = (samplingrow_size / ycbcrsubsampling[1]);  | 
293  | 0  |         }  | 
294  | 0  |         else  | 
295  | 0  |         { | 
296  | 0  |             uint64_t scanline_samples;  | 
297  | 0  |             scanline_samples = _TIFFMultiply64(tif, td->td_imagewidth,  | 
298  | 0  |                                                td->td_samplesperpixel, module);  | 
299  | 0  |             scanline_size =  | 
300  | 0  |                 TIFFhowmany_64(_TIFFMultiply64(tif, scanline_samples,  | 
301  | 0  |                                                td->td_bitspersample, module),  | 
302  | 0  |                                8);  | 
303  | 0  |         }  | 
304  | 0  |     }  | 
305  | 0  |     else  | 
306  | 0  |     { | 
307  | 0  |         scanline_size =  | 
308  | 0  |             TIFFhowmany_64(_TIFFMultiply64(tif, td->td_imagewidth,  | 
309  | 0  |                                            td->td_bitspersample, module),  | 
310  | 0  |                            8);  | 
311  | 0  |     }  | 
312  | 0  |     if (scanline_size == 0)  | 
313  | 0  |     { | 
314  | 0  |         TIFFErrorExtR(tif, module, "Computed scanline size is zero");  | 
315  | 0  |         return 0;  | 
316  | 0  |     }  | 
317  | 0  |     return (scanline_size);  | 
318  | 0  | }  | 
319  |  | tmsize_t TIFFScanlineSize(TIFF *tif)  | 
320  | 0  | { | 
321  | 0  |     static const char module[] = "TIFFScanlineSize";  | 
322  | 0  |     uint64_t m;  | 
323  | 0  |     m = TIFFScanlineSize64(tif);  | 
324  | 0  |     return _TIFFCastUInt64ToSSize(tif, m, module);  | 
325  | 0  | }  | 
326  |  |  | 
327  |  | /*  | 
328  |  |  * Return the number of bytes required to store a complete  | 
329  |  |  * decoded and packed raster scanline (as opposed to the  | 
330  |  |  * I/O size returned by TIFFScanlineSize which may be less  | 
331  |  |  * if data is store as separate planes).  | 
332  |  |  */  | 
333  |  | uint64_t TIFFRasterScanlineSize64(TIFF *tif)  | 
334  | 0  | { | 
335  | 0  |     static const char module[] = "TIFFRasterScanlineSize64";  | 
336  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
337  | 0  |     uint64_t scanline;  | 
338  |  | 
  | 
339  | 0  |     scanline =  | 
340  | 0  |         _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);  | 
341  | 0  |     if (td->td_planarconfig == PLANARCONFIG_CONTIG)  | 
342  | 0  |     { | 
343  | 0  |         scanline =  | 
344  | 0  |             _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);  | 
345  | 0  |         return (TIFFhowmany8_64(scanline));  | 
346  | 0  |     }  | 
347  | 0  |     else  | 
348  | 0  |         return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),  | 
349  | 0  |                                 td->td_samplesperpixel, module));  | 
350  | 0  | }  | 
351  |  | tmsize_t TIFFRasterScanlineSize(TIFF *tif)  | 
352  | 0  | { | 
353  | 0  |     static const char module[] = "TIFFRasterScanlineSize";  | 
354  | 0  |     uint64_t m;  | 
355  | 0  |     m = TIFFRasterScanlineSize64(tif);  | 
356  | 0  |     return _TIFFCastUInt64ToSSize(tif, m, module);  | 
357  | 0  | }  |