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