1 /* $Id: tif_strip.c,v 1.34 2011-04-02 20:54:09 bfriesen Exp $ */
4 * Copyright (c) 1991-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
30 * Strip-organized Image Support Routines.
35 * Compute which strip a (row,sample) value is in.
38 TIFFComputeStrip(TIFF* tif, uint32 row, uint16 sample)
40 static const char module[] = "TIFFComputeStrip";
41 TIFFDirectory *td = &tif->tif_dir;
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);
52 strip += (uint32)sample*td->td_stripsperimage;
58 * Compute how many strips are in an image.
61 TIFFNumberOfStrips(TIFF* tif)
63 TIFFDirectory *td = &tif->tif_dir;
66 nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :
67 TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
68 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
69 nstrips = _TIFFMultiply32(tif, nstrips, (uint32)td->td_samplesperpixel,
70 "TIFFNumberOfStrips");
75 * Compute the # bytes in a variable height, row-aligned strip.
78 TIFFVStripSize64(TIFF* tif, uint32 nrows)
80 static const char module[] = "TIFFVStripSize64";
81 TIFFDirectory *td = &tif->tif_dir;
82 if (nrows==(uint32)(-1))
83 nrows=td->td_imagelength;
84 if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
85 (td->td_photometric == PHOTOMETRIC_YCBCR)&&
89 * Packed YCbCr data contain one Cb+Cr for every
90 * HorizontalSampling*VerticalSampling Y values.
91 * Must also roundup width and height when calculating
92 * since images that are not a multiple of the
93 * horizontal/vertical subsampling area include
94 * YCbCr data for the extended image.
96 uint16 ycbcrsubsampling[2];
97 uint16 samplingblock_samples;
98 uint32 samplingblocks_hor;
99 uint32 samplingblocks_ver;
100 uint64 samplingrow_samples;
101 uint64 samplingrow_size;
102 if(td->td_samplesperpixel!=3)
104 TIFFErrorExt(tif->tif_clientdata,module,
105 "Invalid td_samplesperpixel value");
108 TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
110 if (((ycbcrsubsampling[0]!=1)&&(ycbcrsubsampling[0]!=2)&&(ycbcrsubsampling[0]!=4)) ||
111 ((ycbcrsubsampling[1]!=1)&&(ycbcrsubsampling[1]!=2)&&(ycbcrsubsampling[1]!=4)))
113 TIFFErrorExt(tif->tif_clientdata,module,
114 "Invalid YCbCr subsampling");
117 samplingblock_samples=ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
118 samplingblocks_hor=TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
119 samplingblocks_ver=TIFFhowmany_32(nrows,ycbcrsubsampling[1]);
120 samplingrow_samples=_TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
121 samplingrow_size=TIFFhowmany8_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module));
122 return(_TIFFMultiply64(tif,samplingrow_size,samplingblocks_ver,module));
125 return(_TIFFMultiply64(tif,nrows,TIFFScanlineSize64(tif),module));
128 TIFFVStripSize(TIFF* tif, uint32 nrows)
130 static const char module[] = "TIFFVStripSize";
133 m=TIFFVStripSize64(tif,nrows);
137 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
144 * Compute the # bytes in a raw strip.
147 TIFFRawStripSize64(TIFF* tif, uint32 strip)
149 static const char module[] = "TIFFRawStripSize64";
150 TIFFDirectory* td = &tif->tif_dir;
151 uint64 bytecount = td->td_stripbytecount[strip];
155 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
156 TIFFErrorExt(tif->tif_clientdata, module,
157 "%I64u: Invalid strip byte count, strip %lu",
158 (unsigned __int64) bytecount,
159 (unsigned long) strip);
161 TIFFErrorExt(tif->tif_clientdata, module,
162 "%llu: Invalid strip byte count, strip %lu",
163 (unsigned long long) bytecount,
164 (unsigned long) strip);
166 bytecount = (uint64) -1;
172 TIFFRawStripSize(TIFF* tif, uint32 strip)
174 static const char module[] = "TIFFRawStripSize";
177 m=TIFFRawStripSize64(tif,strip);
185 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
193 * Compute the # bytes in a (row-aligned) strip.
195 * Note that if RowsPerStrip is larger than the
196 * recorded ImageLength, then the strip size is
197 * truncated to reflect the actual space required
201 TIFFStripSize64(TIFF* tif)
203 TIFFDirectory* td = &tif->tif_dir;
204 uint32 rps = td->td_rowsperstrip;
205 if (rps > td->td_imagelength)
206 rps = td->td_imagelength;
207 return (TIFFVStripSize64(tif, rps));
210 TIFFStripSize(TIFF* tif)
212 static const char module[] = "TIFFStripSize";
215 m=TIFFStripSize64(tif);
219 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
226 * Compute a default strip size based on the image
227 * characteristics and a requested value. If the
228 * request is <1 then we choose a strip size according
229 * to certain heuristics.
232 TIFFDefaultStripSize(TIFF* tif, uint32 request)
234 return (*tif->tif_defstripsize)(tif, request);
238 _TIFFDefaultStripSize(TIFF* tif, uint32 s)
242 * If RowsPerStrip is unspecified, try to break the
243 * image up into strips that are approximately
244 * STRIP_SIZE_DEFAULT bytes long.
248 scanlinesize=TIFFScanlineSize64(tif);
251 rows=(uint64)STRIP_SIZE_DEFAULT/scanlinesize;
254 else if (rows>0xFFFFFFFF)
262 * Return the number of bytes to read/write in a call to
263 * one of the scanline-oriented i/o routines. Note that
264 * this number may be 1/samples-per-pixel if data is
265 * stored as separate planes.
266 * The ScanlineSize in case of YCbCrSubsampling is defined as the
267 * strip size divided by the strip height, i.e. the size of a pack of vertical
268 * subsampling lines divided by vertical subsampling. It should thus make
269 * sense when multiplied by a multiple of vertical subsampling.
272 TIFFScanlineSize64(TIFF* tif)
274 static const char module[] = "TIFFScanlineSize64";
275 TIFFDirectory *td = &tif->tif_dir;
276 uint64 scanline_size;
277 if (td->td_planarconfig==PLANARCONFIG_CONTIG)
279 if ((td->td_photometric==PHOTOMETRIC_YCBCR)&&
280 (td->td_samplesperpixel==3)&&
283 uint16 ycbcrsubsampling[2];
284 uint16 samplingblock_samples;
285 uint32 samplingblocks_hor;
286 uint64 samplingrow_samples;
287 uint64 samplingrow_size;
288 if(td->td_samplesperpixel!=3)
290 TIFFErrorExt(tif->tif_clientdata,module,
291 "Invalid td_samplesperpixel value");
294 TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,
297 if (((ycbcrsubsampling[0]!=1)&&(ycbcrsubsampling[0]!=2)&&(ycbcrsubsampling[0]!=4)) ||
298 ((ycbcrsubsampling[1]!=1)&&(ycbcrsubsampling[1]!=2)&&(ycbcrsubsampling[1]!=4)))
300 TIFFErrorExt(tif->tif_clientdata,module,
301 "Invalid YCbCr subsampling");
304 samplingblock_samples = ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
305 samplingblocks_hor = TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
306 samplingrow_samples = _TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
307 samplingrow_size = TIFFhowmany_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module),8);
308 scanline_size = (samplingrow_size/ycbcrsubsampling[1]);
312 uint64 scanline_samples;
313 scanline_samples=_TIFFMultiply64(tif,td->td_imagewidth,td->td_samplesperpixel,module);
314 scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,scanline_samples,td->td_bitspersample,module),8);
318 scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,td->td_imagewidth,td->td_bitspersample,module),8);
319 return(scanline_size);
322 TIFFScanlineSize(TIFF* tif)
324 static const char module[] = "TIFFScanlineSize";
327 m=TIFFScanlineSize64(tif);
331 TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
338 * Return the number of bytes required to store a complete
339 * decoded and packed raster scanline (as opposed to the
340 * I/O size returned by TIFFScanlineSize which may be less
341 * if data is store as separate planes).
344 TIFFRasterScanlineSize64(TIFF* tif)
346 static const char module[] = "TIFFRasterScanlineSize64";
347 TIFFDirectory *td = &tif->tif_dir;
350 scanline = _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
351 if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
352 scanline = _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
353 return (TIFFhowmany8_64(scanline));
355 return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
356 td->td_samplesperpixel, module));
359 TIFFRasterScanlineSize(TIFF* tif)
361 static const char module[] = "TIFFRasterScanlineSize";
364 m=TIFFRasterScanlineSize64(tif);
368 TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
374 /* vim: set ts=8 sts=8 sw=8 noet: */