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 (%dx%d)",
116 ycbcrsubsampling
[1] );
119 samplingblock_samples
=ycbcrsubsampling
[0]*ycbcrsubsampling
[1]+2;
120 samplingblocks_hor
=TIFFhowmany_32(td
->td_imagewidth
,ycbcrsubsampling
[0]);
121 samplingblocks_ver
=TIFFhowmany_32(nrows
,ycbcrsubsampling
[1]);
122 samplingrow_samples
=_TIFFMultiply64(tif
,samplingblocks_hor
,samplingblock_samples
,module);
123 samplingrow_size
=TIFFhowmany8_64(_TIFFMultiply64(tif
,samplingrow_samples
,td
->td_bitspersample
,module));
124 return(_TIFFMultiply64(tif
,samplingrow_size
,samplingblocks_ver
,module));
127 return(_TIFFMultiply64(tif
,nrows
,TIFFScanlineSize64(tif
),module));
130 TIFFVStripSize(TIFF
* tif
, uint32 nrows
)
132 static const char module[] = "TIFFVStripSize";
135 m
=TIFFVStripSize64(tif
,nrows
);
139 TIFFErrorExt(tif
->tif_clientdata
,module,"Integer overflow");
146 * Compute the # bytes in a raw strip.
149 TIFFRawStripSize64(TIFF
* tif
, uint32 strip
)
151 static const char module[] = "TIFFRawStripSize64";
152 TIFFDirectory
* td
= &tif
->tif_dir
;
153 uint64 bytecount
= td
->td_stripbytecount
[strip
];
157 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
158 TIFFErrorExt(tif
->tif_clientdata
, module,
159 "%I64u: Invalid strip byte count, strip %lu",
160 (unsigned __int64
) bytecount
,
161 (unsigned long) strip
);
163 TIFFErrorExt(tif
->tif_clientdata
, module,
164 "%llu: Invalid strip byte count, strip %lu",
165 (unsigned long long) bytecount
,
166 (unsigned long) strip
);
168 bytecount
= (uint64
) -1;
174 TIFFRawStripSize(TIFF
* tif
, uint32 strip
)
176 static const char module[] = "TIFFRawStripSize";
179 m
=TIFFRawStripSize64(tif
,strip
);
187 TIFFErrorExt(tif
->tif_clientdata
,module,"Integer overflow");
195 * Compute the # bytes in a (row-aligned) strip.
197 * Note that if RowsPerStrip is larger than the
198 * recorded ImageLength, then the strip size is
199 * truncated to reflect the actual space required
203 TIFFStripSize64(TIFF
* tif
)
205 TIFFDirectory
* td
= &tif
->tif_dir
;
206 uint32 rps
= td
->td_rowsperstrip
;
207 if (rps
> td
->td_imagelength
)
208 rps
= td
->td_imagelength
;
209 return (TIFFVStripSize64(tif
, rps
));
212 TIFFStripSize(TIFF
* tif
)
214 static const char module[] = "TIFFStripSize";
217 m
=TIFFStripSize64(tif
);
221 TIFFErrorExt(tif
->tif_clientdata
,module,"Integer overflow");
228 * Compute a default strip size based on the image
229 * characteristics and a requested value. If the
230 * request is <1 then we choose a strip size according
231 * to certain heuristics.
234 TIFFDefaultStripSize(TIFF
* tif
, uint32 request
)
236 return (*tif
->tif_defstripsize
)(tif
, request
);
240 _TIFFDefaultStripSize(TIFF
* tif
, uint32 s
)
244 * If RowsPerStrip is unspecified, try to break the
245 * image up into strips that are approximately
246 * STRIP_SIZE_DEFAULT bytes long.
250 scanlinesize
=TIFFScanlineSize64(tif
);
253 rows
=(uint64
)STRIP_SIZE_DEFAULT
/scanlinesize
;
256 else if (rows
>0xFFFFFFFF)
264 * Return the number of bytes to read/write in a call to
265 * one of the scanline-oriented i/o routines. Note that
266 * this number may be 1/samples-per-pixel if data is
267 * stored as separate planes.
268 * The ScanlineSize in case of YCbCrSubsampling is defined as the
269 * strip size divided by the strip height, i.e. the size of a pack of vertical
270 * subsampling lines divided by vertical subsampling. It should thus make
271 * sense when multiplied by a multiple of vertical subsampling.
274 TIFFScanlineSize64(TIFF
* tif
)
276 static const char module[] = "TIFFScanlineSize64";
277 TIFFDirectory
*td
= &tif
->tif_dir
;
278 uint64 scanline_size
;
279 if (td
->td_planarconfig
==PLANARCONFIG_CONTIG
)
281 if ((td
->td_photometric
==PHOTOMETRIC_YCBCR
)&&
282 (td
->td_samplesperpixel
==3)&&
285 uint16 ycbcrsubsampling
[2];
286 uint16 samplingblock_samples
;
287 uint32 samplingblocks_hor
;
288 uint64 samplingrow_samples
;
289 uint64 samplingrow_size
;
290 if(td
->td_samplesperpixel
!=3)
292 TIFFErrorExt(tif
->tif_clientdata
,module,
293 "Invalid td_samplesperpixel value");
296 TIFFGetFieldDefaulted(tif
,TIFFTAG_YCBCRSUBSAMPLING
,
299 if (((ycbcrsubsampling
[0]!=1)&&(ycbcrsubsampling
[0]!=2)&&(ycbcrsubsampling
[0]!=4)) ||
300 ((ycbcrsubsampling
[1]!=1)&&(ycbcrsubsampling
[1]!=2)&&(ycbcrsubsampling
[1]!=4)))
302 TIFFErrorExt(tif
->tif_clientdata
,module,
303 "Invalid YCbCr subsampling");
306 samplingblock_samples
= ycbcrsubsampling
[0]*ycbcrsubsampling
[1]+2;
307 samplingblocks_hor
= TIFFhowmany_32(td
->td_imagewidth
,ycbcrsubsampling
[0]);
308 samplingrow_samples
= _TIFFMultiply64(tif
,samplingblocks_hor
,samplingblock_samples
,module);
309 samplingrow_size
= TIFFhowmany_64(_TIFFMultiply64(tif
,samplingrow_samples
,td
->td_bitspersample
,module),8);
310 scanline_size
= (samplingrow_size
/ycbcrsubsampling
[1]);
314 uint64 scanline_samples
;
315 scanline_samples
=_TIFFMultiply64(tif
,td
->td_imagewidth
,td
->td_samplesperpixel
,module);
316 scanline_size
=TIFFhowmany_64(_TIFFMultiply64(tif
,scanline_samples
,td
->td_bitspersample
,module),8);
320 scanline_size
=TIFFhowmany_64(_TIFFMultiply64(tif
,td
->td_imagewidth
,td
->td_bitspersample
,module),8);
321 return(scanline_size
);
324 TIFFScanlineSize(TIFF
* tif
)
326 static const char module[] = "TIFFScanlineSize";
329 m
=TIFFScanlineSize64(tif
);
333 TIFFErrorExt(tif
->tif_clientdata
,module,"Integer arithmetic overflow");
340 * Return the number of bytes required to store a complete
341 * decoded and packed raster scanline (as opposed to the
342 * I/O size returned by TIFFScanlineSize which may be less
343 * if data is store as separate planes).
346 TIFFRasterScanlineSize64(TIFF
* tif
)
348 static const char module[] = "TIFFRasterScanlineSize64";
349 TIFFDirectory
*td
= &tif
->tif_dir
;
352 scanline
= _TIFFMultiply64(tif
, td
->td_bitspersample
, td
->td_imagewidth
, module);
353 if (td
->td_planarconfig
== PLANARCONFIG_CONTIG
) {
354 scanline
= _TIFFMultiply64(tif
, scanline
, td
->td_samplesperpixel
, module);
355 return (TIFFhowmany8_64(scanline
));
357 return (_TIFFMultiply64(tif
, TIFFhowmany8_64(scanline
),
358 td
->td_samplesperpixel
, module));
361 TIFFRasterScanlineSize(TIFF
* tif
)
363 static const char module[] = "TIFFRasterScanlineSize";
366 m
=TIFFRasterScanlineSize64(tif
);
370 TIFFErrorExt(tif
->tif_clientdata
,module,"Integer arithmetic overflow");
376 /* vim: set ts=8 sts=8 sw=8 noet: */