3 * Copyright (c) 1991-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
29 * Strip-organized Image Support Routines.
34 * Compute which strip a (row,sample) value is in.
37 TIFFComputeStrip(TIFF
* tif
, uint32 row
, uint16 sample
)
39 static const char module[] = "TIFFComputeStrip";
40 TIFFDirectory
*td
= &tif
->tif_dir
;
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
);
51 strip
+= (uint32
)sample
*td
->td_stripsperimage
;
57 * Compute how many strips are in an image.
60 TIFFNumberOfStrips(TIFF
* tif
)
62 TIFFDirectory
*td
= &tif
->tif_dir
;
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");
74 * Compute the # bytes in a variable height, row-aligned strip.
77 TIFFVStripSize64(TIFF
* tif
, uint32 nrows
)
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
)&&
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.
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)
103 TIFFErrorExt(tif
->tif_clientdata
,module,
104 "Invalid td_samplesperpixel value");
107 TIFFGetFieldDefaulted(tif
,TIFFTAG_YCBCRSUBSAMPLING
,ycbcrsubsampling
+0,
109 if ((ycbcrsubsampling
[0] != 1 && ycbcrsubsampling
[0] != 2 && ycbcrsubsampling
[0] != 4)
110 ||(ycbcrsubsampling
[1] != 1 && ycbcrsubsampling
[1] != 2 && ycbcrsubsampling
[1] != 4))
112 TIFFErrorExt(tif
->tif_clientdata
,module,
113 "Invalid YCbCr subsampling (%dx%d)",
115 ycbcrsubsampling
[1] );
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));
126 return(_TIFFMultiply64(tif
,nrows
,TIFFScanlineSize64(tif
),module));
129 TIFFVStripSize(TIFF
* tif
, uint32 nrows
)
131 static const char module[] = "TIFFVStripSize";
134 m
=TIFFVStripSize64(tif
,nrows
);
138 TIFFErrorExt(tif
->tif_clientdata
,module,"Integer overflow");
145 * Compute the # bytes in a raw strip.
148 TIFFRawStripSize64(TIFF
* tif
, uint32 strip
)
150 static const char module[] = "TIFFRawStripSize64";
151 TIFFDirectory
* td
= &tif
->tif_dir
;
152 uint64 bytecount
= td
->td_stripbytecount
[strip
];
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
);
162 TIFFErrorExt(tif
->tif_clientdata
, module,
163 "%llu: Invalid strip byte count, strip %lu",
164 (unsigned long long) bytecount
,
165 (unsigned long) strip
);
167 bytecount
= (uint64
) -1;
173 TIFFRawStripSize(TIFF
* tif
, uint32 strip
)
175 static const char module[] = "TIFFRawStripSize";
178 m
=TIFFRawStripSize64(tif
,strip
);
186 TIFFErrorExt(tif
->tif_clientdata
,module,"Integer overflow");
194 * Compute the # bytes in a (row-aligned) strip.
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
202 TIFFStripSize64(TIFF
* tif
)
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
));
211 TIFFStripSize(TIFF
* tif
)
213 static const char module[] = "TIFFStripSize";
216 m
=TIFFStripSize64(tif
);
220 TIFFErrorExt(tif
->tif_clientdata
,module,"Integer overflow");
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.
233 TIFFDefaultStripSize(TIFF
* tif
, uint32 request
)
235 return (*tif
->tif_defstripsize
)(tif
, request
);
239 _TIFFDefaultStripSize(TIFF
* tif
, uint32 s
)
243 * If RowsPerStrip is unspecified, try to break the
244 * image up into strips that are approximately
245 * STRIP_SIZE_DEFAULT bytes long.
249 scanlinesize
=TIFFScanlineSize64(tif
);
252 rows
=(uint64
)STRIP_SIZE_DEFAULT
/scanlinesize
;
255 else if (rows
>0xFFFFFFFF)
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.
273 TIFFScanlineSize64(TIFF
* tif
)
275 static const char module[] = "TIFFScanlineSize64";
276 TIFFDirectory
*td
= &tif
->tif_dir
;
277 uint64 scanline_size
;
278 if (td
->td_planarconfig
==PLANARCONFIG_CONTIG
)
280 if ((td
->td_photometric
==PHOTOMETRIC_YCBCR
)&&
281 (td
->td_samplesperpixel
==3)&&
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)
291 TIFFErrorExt(tif
->tif_clientdata
,module,
292 "Invalid td_samplesperpixel value");
295 TIFFGetFieldDefaulted(tif
,TIFFTAG_YCBCRSUBSAMPLING
,
298 if (((ycbcrsubsampling
[0]!=1)&&(ycbcrsubsampling
[0]!=2)&&(ycbcrsubsampling
[0]!=4)) ||
299 ((ycbcrsubsampling
[1]!=1)&&(ycbcrsubsampling
[1]!=2)&&(ycbcrsubsampling
[1]!=4)))
301 TIFFErrorExt(tif
->tif_clientdata
,module,
302 "Invalid YCbCr subsampling");
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]);
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);
319 scanline_size
=TIFFhowmany_64(_TIFFMultiply64(tif
,td
->td_imagewidth
,td
->td_bitspersample
,module),8);
320 return(scanline_size
);
323 TIFFScanlineSize(TIFF
* tif
)
325 static const char module[] = "TIFFScanlineSize";
328 m
=TIFFScanlineSize64(tif
);
332 TIFFErrorExt(tif
->tif_clientdata
,module,"Integer arithmetic overflow");
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).
345 TIFFRasterScanlineSize64(TIFF
* tif
)
347 static const char module[] = "TIFFRasterScanlineSize64";
348 TIFFDirectory
*td
= &tif
->tif_dir
;
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
));
356 return (_TIFFMultiply64(tif
, TIFFhowmany8_64(scanline
),
357 td
->td_samplesperpixel
, module));
360 TIFFRasterScanlineSize(TIFF
* tif
)
362 static const char module[] = "TIFFRasterScanlineSize";
365 m
=TIFFRasterScanlineSize64(tif
);
369 TIFFErrorExt(tif
->tif_clientdata
,module,"Integer arithmetic overflow");
375 /* vim: set ts=8 sts=8 sw=8 noet: */