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