Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / core / src / fxcodec / fx_tiff / tiff_v403 / tif_strip.c
1 /* $Id: tif_strip.c,v 1.35 2012-06-06 05:33:55 fwarmerdam Exp $ */
2
3 /*
4  * Copyright (c) 1991-1997 Sam Leffler
5  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and 
8  * its documentation for any purpose is hereby granted without fee, provided
9  * that (i) the above copyright notices and this permission notice appear in
10  * all copies of the software and related documentation, and (ii) the names of
11  * Sam Leffler and Silicon Graphics may not be used in any advertising or
12  * publicity relating to the software without the specific, prior written
13  * permission of Sam Leffler and Silicon Graphics.
14  * 
15  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
16  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
17  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
18  * 
19  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
24  * OF THIS SOFTWARE.
25  */
26
27 /*
28  * TIFF Library.
29  *
30  * Strip-organized Image Support Routines.
31  */
32 #if (!defined(_FPDFAPI_MINI_) || defined(_TIFF_DECODER_)) && !defined(_USE_ADDIN_) && !defined _FX_NO_ANSIC_ && !defined(_FX_EMB_NOUSE_DECODER_)
33 #include "tiffiop.h"
34
35 /*
36  * Compute which strip a (row,sample) value is in.
37  */
38 uint32
39 TIFFComputeStrip(TIFF* tif, uint32 row, uint16 sample)
40 {
41         static const char module[] = "TIFFComputeStrip";
42         TIFFDirectory *td = &tif->tif_dir;
43         uint32 strip;
44
45         strip = row / td->td_rowsperstrip;
46         if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
47                 if (sample >= td->td_samplesperpixel) {
48                         TIFFErrorExt(tif->tif_clientdata, module,
49                             "%lu: Sample out of range, max %lu",
50                             (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
51                         return (0);
52                 }
53                 strip += (uint32)sample*td->td_stripsperimage;
54         }
55         return (strip);
56 }
57
58 /*
59  * Compute how many strips are in an image.
60  */
61 uint32
62 TIFFNumberOfStrips(TIFF* tif)
63 {
64         TIFFDirectory *td = &tif->tif_dir;
65         uint32 nstrips;
66
67         nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :
68              TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
69         if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
70                 nstrips = _TIFFMultiply32(tif, nstrips, (uint32)td->td_samplesperpixel,
71                     "TIFFNumberOfStrips");
72         return (nstrips);
73 }
74
75 /*
76  * Compute the # bytes in a variable height, row-aligned strip.
77  */
78 uint64
79 TIFFVStripSize64(TIFF* tif, uint32 nrows)
80 {
81         static const char module[] = "TIFFVStripSize64";
82         TIFFDirectory *td = &tif->tif_dir;
83         if (nrows==(uint32)(-1))
84                 nrows=td->td_imagelength;
85         if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
86             (td->td_photometric == PHOTOMETRIC_YCBCR)&&
87             (!isUpSampled(tif)))
88         {
89                 /*
90                  * Packed YCbCr data contain one Cb+Cr for every
91                  * HorizontalSampling*VerticalSampling Y values.
92                  * Must also roundup width and height when calculating
93                  * since images that are not a multiple of the
94                  * horizontal/vertical subsampling area include
95                  * YCbCr data for the extended image.
96                  */
97                 uint16 ycbcrsubsampling[2];
98                 uint16 samplingblock_samples;
99                 uint32 samplingblocks_hor;
100                 uint32 samplingblocks_ver;
101                 uint64 samplingrow_samples;
102                 uint64 samplingrow_size;
103                 if(td->td_samplesperpixel!=3)
104                 {
105                         TIFFErrorExt(tif->tif_clientdata,module,
106                             "Invalid td_samplesperpixel value");
107                         return 0;
108                 }
109                 TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
110                     ycbcrsubsampling+1);
111                 if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && ycbcrsubsampling[0] != 4)
112                     ||(ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && ycbcrsubsampling[1] != 4))
113                 {
114                         TIFFErrorExt(tif->tif_clientdata,module,
115                                      "Invalid YCbCr subsampling (%dx%d)", 
116                                      ycbcrsubsampling[0], 
117                                      ycbcrsubsampling[1] );
118                         return 0;
119                 }
120                 samplingblock_samples=ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
121                 samplingblocks_hor=TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
122                 samplingblocks_ver=TIFFhowmany_32(nrows,ycbcrsubsampling[1]);
123                 samplingrow_samples=_TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
124                 samplingrow_size=TIFFhowmany8_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module));
125                 return(_TIFFMultiply64(tif,samplingrow_size,samplingblocks_ver,module));
126         }
127         else
128                 return(_TIFFMultiply64(tif,nrows,TIFFScanlineSize64(tif),module));
129 }
130 tmsize_t
131 TIFFVStripSize(TIFF* tif, uint32 nrows)
132 {
133         static const char module[] = "TIFFVStripSize";
134         uint64 m;
135         tmsize_t n;
136         m=TIFFVStripSize64(tif,nrows);
137         n=(tmsize_t)m;
138         if ((uint64)n!=m)
139         {
140                 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
141                 n=0;
142         }
143         return(n);
144 }
145
146 /*
147  * Compute the # bytes in a raw strip.
148  */
149 uint64
150 TIFFRawStripSize64(TIFF* tif, uint32 strip)
151 {
152         static const char module[] = "TIFFRawStripSize64";
153         TIFFDirectory* td = &tif->tif_dir;
154         uint64 bytecount = td->td_stripbytecount[strip];
155
156         if (bytecount == 0)
157         {
158 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
159                 TIFFErrorExt(tif->tif_clientdata, module,
160                              "%I64u: Invalid strip byte count, strip %lu",
161                              (unsigned __int64) bytecount,
162                              (unsigned long) strip);
163 #else
164                 TIFFErrorExt(tif->tif_clientdata, module,
165                              "%llu: Invalid strip byte count, strip %lu",
166                              (unsigned long long) bytecount,
167                              (unsigned long) strip);
168 #endif
169                 bytecount = (uint64) -1;
170         }
171
172         return bytecount;
173 }
174 tmsize_t
175 TIFFRawStripSize(TIFF* tif, uint32 strip)
176 {
177         static const char module[] = "TIFFRawStripSize";
178         uint64 m;
179         tmsize_t n;
180         m=TIFFRawStripSize64(tif,strip);
181         if (m==(uint64)(-1))
182                 n=(tmsize_t)(-1);
183         else
184         {
185                 n=(tmsize_t)m;
186                 if ((uint64)n!=m)
187                 {
188                         TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
189                         n=0;
190                 }
191         }
192         return(n);
193 }
194
195 /*
196  * Compute the # bytes in a (row-aligned) strip.
197  *
198  * Note that if RowsPerStrip is larger than the
199  * recorded ImageLength, then the strip size is
200  * truncated to reflect the actual space required
201  * to hold the strip.
202  */
203 uint64
204 TIFFStripSize64(TIFF* tif)
205 {
206         TIFFDirectory* td = &tif->tif_dir;
207         uint32 rps = td->td_rowsperstrip;
208         if (rps > td->td_imagelength)
209                 rps = td->td_imagelength;
210         return (TIFFVStripSize64(tif, rps));
211 }
212 tmsize_t
213 TIFFStripSize(TIFF* tif)
214 {
215         static const char module[] = "TIFFStripSize";
216         uint64 m;
217         tmsize_t n;
218         m=TIFFStripSize64(tif);
219         n=(tmsize_t)m;
220         if ((uint64)n!=m)
221         {
222                 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
223                 n=0;
224         }
225         return(n);
226 }
227
228 /*
229  * Compute a default strip size based on the image
230  * characteristics and a requested value.  If the
231  * request is <1 then we choose a strip size according
232  * to certain heuristics.
233  */
234 uint32
235 TIFFDefaultStripSize(TIFF* tif, uint32 request)
236 {
237         return (*tif->tif_defstripsize)(tif, request);
238 }
239
240 uint32
241 _TIFFDefaultStripSize(TIFF* tif, uint32 s)
242 {
243         if ((int32) s < 1) {
244                 /*
245                  * If RowsPerStrip is unspecified, try to break the
246                  * image up into strips that are approximately
247                  * STRIP_SIZE_DEFAULT bytes long.
248                  */
249                 uint64 scanlinesize;
250                 uint64 rows;
251                 scanlinesize=TIFFScanlineSize64(tif);
252                 if (scanlinesize==0)
253                         scanlinesize=1;
254                 rows=(uint64)STRIP_SIZE_DEFAULT/scanlinesize;
255                 if (rows==0)
256                         rows=1;
257                 else if (rows>0xFFFFFFFF)
258                         rows=0xFFFFFFFF;
259                 s=(uint32)rows;
260         }
261         return (s);
262 }
263
264 /*
265  * Return the number of bytes to read/write in a call to
266  * one of the scanline-oriented i/o routines.  Note that
267  * this number may be 1/samples-per-pixel if data is
268  * stored as separate planes.
269  * The ScanlineSize in case of YCbCrSubsampling is defined as the
270  * strip size divided by the strip height, i.e. the size of a pack of vertical
271  * subsampling lines divided by vertical subsampling. It should thus make
272  * sense when multiplied by a multiple of vertical subsampling.
273  */
274 uint64
275 TIFFScanlineSize64(TIFF* tif)
276 {
277         static const char module[] = "TIFFScanlineSize64";
278         TIFFDirectory *td = &tif->tif_dir;
279         uint64 scanline_size;
280         if (td->td_planarconfig==PLANARCONFIG_CONTIG)
281         {
282                 if ((td->td_photometric==PHOTOMETRIC_YCBCR)&&
283                     (td->td_samplesperpixel==3)&&
284                     (!isUpSampled(tif)))
285                 {
286                         uint16 ycbcrsubsampling[2];
287                         uint16 samplingblock_samples;
288                         uint32 samplingblocks_hor;
289                         uint64 samplingrow_samples;
290                         uint64 samplingrow_size;
291                         if(td->td_samplesperpixel!=3)
292                         {
293                             TIFFErrorExt(tif->tif_clientdata,module,
294                                          "Invalid td_samplesperpixel value");
295                             return 0;
296                         }
297                         TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,
298                                               ycbcrsubsampling+0,
299                                               ycbcrsubsampling+1);
300                         if (((ycbcrsubsampling[0]!=1)&&(ycbcrsubsampling[0]!=2)&&(ycbcrsubsampling[0]!=4)) ||
301                             ((ycbcrsubsampling[1]!=1)&&(ycbcrsubsampling[1]!=2)&&(ycbcrsubsampling[1]!=4)))
302                         {
303                             TIFFErrorExt(tif->tif_clientdata,module,
304                                          "Invalid YCbCr subsampling");
305                             return 0;
306                         }
307                         samplingblock_samples = ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
308                         samplingblocks_hor = TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
309                         samplingrow_samples = _TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
310                         samplingrow_size = TIFFhowmany_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module),8);
311                         scanline_size = (samplingrow_size/ycbcrsubsampling[1]);
312                 }
313                 else
314                 {
315                         uint64 scanline_samples;
316                         scanline_samples=_TIFFMultiply64(tif,td->td_imagewidth,td->td_samplesperpixel,module);
317                         scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,scanline_samples,td->td_bitspersample,module),8);
318                 }
319         }
320         else
321                 scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,td->td_imagewidth,td->td_bitspersample,module),8);
322         return(scanline_size);
323 }
324 tmsize_t
325 TIFFScanlineSize(TIFF* tif)
326 {
327         static const char module[] = "TIFFScanlineSize";
328         uint64 m;
329         tmsize_t n;
330         m=TIFFScanlineSize64(tif);
331         n=(tmsize_t)m;
332         if ((uint64)n!=m)
333         {
334                 TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
335                 n=0;
336         }
337         return(n);
338 }
339
340 /*
341  * Return the number of bytes required to store a complete
342  * decoded and packed raster scanline (as opposed to the
343  * I/O size returned by TIFFScanlineSize which may be less
344  * if data is store as separate planes).
345  */
346 uint64
347 TIFFRasterScanlineSize64(TIFF* tif)
348 {
349         static const char module[] = "TIFFRasterScanlineSize64";
350         TIFFDirectory *td = &tif->tif_dir;
351         uint64 scanline;
352
353         scanline = _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
354         if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
355                 scanline = _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
356                 return (TIFFhowmany8_64(scanline));
357         } else
358                 return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
359                     td->td_samplesperpixel, module));
360 }
361 tmsize_t
362 TIFFRasterScanlineSize(TIFF* tif)
363 {
364         static const char module[] = "TIFFRasterScanlineSize";
365         uint64 m;
366         tmsize_t n;
367         m=TIFFRasterScanlineSize64(tif);
368         n=(tmsize_t)m;
369         if ((uint64)n!=m)
370         {
371                 TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
372                 n=0;
373         }
374         return(n);
375 }
376
377 /* vim: set ts=8 sts=8 sw=8 noet: */
378 /*
379  * Local Variables:
380  * mode: c
381  * c-basic-offset: 8
382  * fill-column: 78
383  * End:
384  */
385 #endif
386