[LIBTIFF]
[reactos.git] / reactos / dll / 3rdparty / libtiff / tif_strip.c
1 /* $Id: tif_strip.c,v 1.37 2016-11-09 23:00:49 erouault 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
33 #include <precomp.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 /* If the value was already computed and store in td_nstrips, then return it,
68 since ChopUpSingleUncompressedStrip might have altered and resized the
69 since the td_stripbytecount and td_stripoffset arrays to the new value
70 after the initial affectation of td_nstrips = TIFFNumberOfStrips() in
71 tif_dirread.c ~line 3612.
72 See http://bugzilla.maptools.org/show_bug.cgi?id=2587 */
73 if( td->td_nstrips )
74 return td->td_nstrips;
75
76 nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :
77 TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
78 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
79 nstrips = _TIFFMultiply32(tif, nstrips, (uint32)td->td_samplesperpixel,
80 "TIFFNumberOfStrips");
81 return (nstrips);
82 }
83
84 /*
85 * Compute the # bytes in a variable height, row-aligned strip.
86 */
87 uint64
88 TIFFVStripSize64(TIFF* tif, uint32 nrows)
89 {
90 static const char module[] = "TIFFVStripSize64";
91 TIFFDirectory *td = &tif->tif_dir;
92 if (nrows==(uint32)(-1))
93 nrows=td->td_imagelength;
94 if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
95 (td->td_photometric == PHOTOMETRIC_YCBCR)&&
96 (!isUpSampled(tif)))
97 {
98 /*
99 * Packed YCbCr data contain one Cb+Cr for every
100 * HorizontalSampling*VerticalSampling Y values.
101 * Must also roundup width and height when calculating
102 * since images that are not a multiple of the
103 * horizontal/vertical subsampling area include
104 * YCbCr data for the extended image.
105 */
106 uint16 ycbcrsubsampling[2];
107 uint16 samplingblock_samples;
108 uint32 samplingblocks_hor;
109 uint32 samplingblocks_ver;
110 uint64 samplingrow_samples;
111 uint64 samplingrow_size;
112 if(td->td_samplesperpixel!=3)
113 {
114 TIFFErrorExt(tif->tif_clientdata,module,
115 "Invalid td_samplesperpixel value");
116 return 0;
117 }
118 TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
119 ycbcrsubsampling+1);
120 if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && ycbcrsubsampling[0] != 4)
121 ||(ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && ycbcrsubsampling[1] != 4))
122 {
123 TIFFErrorExt(tif->tif_clientdata,module,
124 "Invalid YCbCr subsampling (%dx%d)",
125 ycbcrsubsampling[0],
126 ycbcrsubsampling[1] );
127 return 0;
128 }
129 samplingblock_samples=ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
130 samplingblocks_hor=TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
131 samplingblocks_ver=TIFFhowmany_32(nrows,ycbcrsubsampling[1]);
132 samplingrow_samples=_TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
133 samplingrow_size=TIFFhowmany8_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module));
134 return(_TIFFMultiply64(tif,samplingrow_size,samplingblocks_ver,module));
135 }
136 else
137 return(_TIFFMultiply64(tif,nrows,TIFFScanlineSize64(tif),module));
138 }
139 tmsize_t
140 TIFFVStripSize(TIFF* tif, uint32 nrows)
141 {
142 static const char module[] = "TIFFVStripSize";
143 uint64 m;
144 tmsize_t n;
145 m=TIFFVStripSize64(tif,nrows);
146 n=(tmsize_t)m;
147 if ((uint64)n!=m)
148 {
149 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
150 n=0;
151 }
152 return(n);
153 }
154
155 /*
156 * Compute the # bytes in a raw strip.
157 */
158 uint64
159 TIFFRawStripSize64(TIFF* tif, uint32 strip)
160 {
161 static const char module[] = "TIFFRawStripSize64";
162 TIFFDirectory* td = &tif->tif_dir;
163 uint64 bytecount = td->td_stripbytecount[strip];
164
165 if (bytecount == 0)
166 {
167 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
168 TIFFErrorExt(tif->tif_clientdata, module,
169 "%I64u: Invalid strip byte count, strip %lu",
170 (unsigned __int64) bytecount,
171 (unsigned long) strip);
172 #else
173 TIFFErrorExt(tif->tif_clientdata, module,
174 "%llu: Invalid strip byte count, strip %lu",
175 (unsigned long long) bytecount,
176 (unsigned long) strip);
177 #endif
178 bytecount = (uint64) -1;
179 }
180
181 return bytecount;
182 }
183 tmsize_t
184 TIFFRawStripSize(TIFF* tif, uint32 strip)
185 {
186 static const char module[] = "TIFFRawStripSize";
187 uint64 m;
188 tmsize_t n;
189 m=TIFFRawStripSize64(tif,strip);
190 if (m==(uint64)(-1))
191 n=(tmsize_t)(-1);
192 else
193 {
194 n=(tmsize_t)m;
195 if ((uint64)n!=m)
196 {
197 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
198 n=0;
199 }
200 }
201 return(n);
202 }
203
204 /*
205 * Compute the # bytes in a (row-aligned) strip.
206 *
207 * Note that if RowsPerStrip is larger than the
208 * recorded ImageLength, then the strip size is
209 * truncated to reflect the actual space required
210 * to hold the strip.
211 */
212 uint64
213 TIFFStripSize64(TIFF* tif)
214 {
215 TIFFDirectory* td = &tif->tif_dir;
216 uint32 rps = td->td_rowsperstrip;
217 if (rps > td->td_imagelength)
218 rps = td->td_imagelength;
219 return (TIFFVStripSize64(tif, rps));
220 }
221 tmsize_t
222 TIFFStripSize(TIFF* tif)
223 {
224 static const char module[] = "TIFFStripSize";
225 uint64 m;
226 tmsize_t n;
227 m=TIFFStripSize64(tif);
228 n=(tmsize_t)m;
229 if ((uint64)n!=m)
230 {
231 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
232 n=0;
233 }
234 return(n);
235 }
236
237 /*
238 * Compute a default strip size based on the image
239 * characteristics and a requested value. If the
240 * request is <1 then we choose a strip size according
241 * to certain heuristics.
242 */
243 uint32
244 TIFFDefaultStripSize(TIFF* tif, uint32 request)
245 {
246 return (*tif->tif_defstripsize)(tif, request);
247 }
248
249 uint32
250 _TIFFDefaultStripSize(TIFF* tif, uint32 s)
251 {
252 if ((int32) s < 1) {
253 /*
254 * If RowsPerStrip is unspecified, try to break the
255 * image up into strips that are approximately
256 * STRIP_SIZE_DEFAULT bytes long.
257 */
258 uint64 scanlinesize;
259 uint64 rows;
260 scanlinesize=TIFFScanlineSize64(tif);
261 if (scanlinesize==0)
262 scanlinesize=1;
263 rows=(uint64)STRIP_SIZE_DEFAULT/scanlinesize;
264 if (rows==0)
265 rows=1;
266 else if (rows>0xFFFFFFFF)
267 rows=0xFFFFFFFF;
268 s=(uint32)rows;
269 }
270 return (s);
271 }
272
273 /*
274 * Return the number of bytes to read/write in a call to
275 * one of the scanline-oriented i/o routines. Note that
276 * this number may be 1/samples-per-pixel if data is
277 * stored as separate planes.
278 * The ScanlineSize in case of YCbCrSubsampling is defined as the
279 * strip size divided by the strip height, i.e. the size of a pack of vertical
280 * subsampling lines divided by vertical subsampling. It should thus make
281 * sense when multiplied by a multiple of vertical subsampling.
282 */
283 uint64
284 TIFFScanlineSize64(TIFF* tif)
285 {
286 static const char module[] = "TIFFScanlineSize64";
287 TIFFDirectory *td = &tif->tif_dir;
288 uint64 scanline_size;
289 if (td->td_planarconfig==PLANARCONFIG_CONTIG)
290 {
291 if ((td->td_photometric==PHOTOMETRIC_YCBCR)&&
292 (td->td_samplesperpixel==3)&&
293 (!isUpSampled(tif)))
294 {
295 uint16 ycbcrsubsampling[2];
296 uint16 samplingblock_samples;
297 uint32 samplingblocks_hor;
298 uint64 samplingrow_samples;
299 uint64 samplingrow_size;
300 if(td->td_samplesperpixel!=3)
301 {
302 TIFFErrorExt(tif->tif_clientdata,module,
303 "Invalid td_samplesperpixel value");
304 return 0;
305 }
306 TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,
307 ycbcrsubsampling+0,
308 ycbcrsubsampling+1);
309 if (((ycbcrsubsampling[0]!=1)&&(ycbcrsubsampling[0]!=2)&&(ycbcrsubsampling[0]!=4)) ||
310 ((ycbcrsubsampling[1]!=1)&&(ycbcrsubsampling[1]!=2)&&(ycbcrsubsampling[1]!=4)))
311 {
312 TIFFErrorExt(tif->tif_clientdata,module,
313 "Invalid YCbCr subsampling");
314 return 0;
315 }
316 samplingblock_samples = ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
317 samplingblocks_hor = TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
318 samplingrow_samples = _TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
319 samplingrow_size = TIFFhowmany_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module),8);
320 scanline_size = (samplingrow_size/ycbcrsubsampling[1]);
321 }
322 else
323 {
324 uint64 scanline_samples;
325 scanline_samples=_TIFFMultiply64(tif,td->td_imagewidth,td->td_samplesperpixel,module);
326 scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,scanline_samples,td->td_bitspersample,module),8);
327 }
328 }
329 else
330 {
331 scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,td->td_imagewidth,td->td_bitspersample,module),8);
332 }
333 if (scanline_size == 0)
334 {
335 TIFFErrorExt(tif->tif_clientdata,module,"Computed scanline size is zero");
336 return 0;
337 }
338 return(scanline_size);
339 }
340 tmsize_t
341 TIFFScanlineSize(TIFF* tif)
342 {
343 static const char module[] = "TIFFScanlineSize";
344 uint64 m;
345 tmsize_t n;
346 m=TIFFScanlineSize64(tif);
347 n=(tmsize_t)m;
348 if ((uint64)n!=m) {
349 TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
350 n=0;
351 }
352 return(n);
353 }
354
355 /*
356 * Return the number of bytes required to store a complete
357 * decoded and packed raster scanline (as opposed to the
358 * I/O size returned by TIFFScanlineSize which may be less
359 * if data is store as separate planes).
360 */
361 uint64
362 TIFFRasterScanlineSize64(TIFF* tif)
363 {
364 static const char module[] = "TIFFRasterScanlineSize64";
365 TIFFDirectory *td = &tif->tif_dir;
366 uint64 scanline;
367
368 scanline = _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
369 if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
370 scanline = _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
371 return (TIFFhowmany8_64(scanline));
372 } else
373 return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
374 td->td_samplesperpixel, module));
375 }
376 tmsize_t
377 TIFFRasterScanlineSize(TIFF* tif)
378 {
379 static const char module[] = "TIFFRasterScanlineSize";
380 uint64 m;
381 tmsize_t n;
382 m=TIFFRasterScanlineSize64(tif);
383 n=(tmsize_t)m;
384 if ((uint64)n!=m)
385 {
386 TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
387 n=0;
388 }
389 return(n);
390 }
391
392 /* vim: set ts=8 sts=8 sw=8 noet: */
393 /*
394 * Local Variables:
395 * mode: c
396 * c-basic-offset: 8
397 * fill-column: 78
398 * End:
399 */