1 /* $Id: tif_strip.c,v 1.37 2016-11-09 23:00:49 erouault 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.
36 * Compute which strip a (row,sample) value is in.
39 TIFFComputeStrip(TIFF
* tif
, uint32 row
, uint16 sample
)
41 static const char module
[] = "TIFFComputeStrip";
42 TIFFDirectory
*td
= &tif
->tif_dir
;
45 strip
= row
/ td
->td_rowsperstrip
;
46 if (td
->td_planarconfig
== PLANARCONFIG_SEPARATE
) {
47 if (sample
>= td
->td_samplesperpixel
) {
48 TIFFErrorExt(tif
->tif_clientdata
, module
,
49 "%lu: Sample out of range, max %lu",
50 (unsigned long) sample
, (unsigned long) td
->td_samplesperpixel
);
53 strip
+= (uint32
)sample
*td
->td_stripsperimage
;
59 * Compute how many strips are in an image.
62 TIFFNumberOfStrips(TIFF
* tif
)
64 TIFFDirectory
*td
= &tif
->tif_dir
;
67 /* If the value was already computed and store in td_nstrips, then return it,
68 since ChopUpSingleUncompressedStrip might have altered and resized the
69 since the td_stripbytecount and td_stripoffset arrays to the new value
70 after the initial affectation of td_nstrips = TIFFNumberOfStrips() in
71 tif_dirread.c ~line 3612.
72 See http://bugzilla.maptools.org/show_bug.cgi?id=2587 */
74 return td
->td_nstrips
;
76 nstrips
= (td
->td_rowsperstrip
== (uint32
) -1 ? 1 :
77 TIFFhowmany_32(td
->td_imagelength
, td
->td_rowsperstrip
));
78 if (td
->td_planarconfig
== PLANARCONFIG_SEPARATE
)
79 nstrips
= _TIFFMultiply32(tif
, nstrips
, (uint32
)td
->td_samplesperpixel
,
80 "TIFFNumberOfStrips");
85 * Compute the # bytes in a variable height, row-aligned strip.
88 TIFFVStripSize64(TIFF
* tif
, uint32 nrows
)
90 static const char module
[] = "TIFFVStripSize64";
91 TIFFDirectory
*td
= &tif
->tif_dir
;
92 if (nrows
==(uint32
)(-1))
93 nrows
=td
->td_imagelength
;
94 if ((td
->td_planarconfig
==PLANARCONFIG_CONTIG
)&&
95 (td
->td_photometric
== PHOTOMETRIC_YCBCR
)&&
99 * Packed YCbCr data contain one Cb+Cr for every
100 * HorizontalSampling*VerticalSampling Y values.
101 * Must also roundup width and height when calculating
102 * since images that are not a multiple of the
103 * horizontal/vertical subsampling area include
104 * YCbCr data for the extended image.
106 uint16 ycbcrsubsampling
[2];
107 uint16 samplingblock_samples
;
108 uint32 samplingblocks_hor
;
109 uint32 samplingblocks_ver
;
110 uint64 samplingrow_samples
;
111 uint64 samplingrow_size
;
112 if(td
->td_samplesperpixel
!=3)
114 TIFFErrorExt(tif
->tif_clientdata
,module
,
115 "Invalid td_samplesperpixel value");
118 TIFFGetFieldDefaulted(tif
,TIFFTAG_YCBCRSUBSAMPLING
,ycbcrsubsampling
+0,
120 if ((ycbcrsubsampling
[0] != 1 && ycbcrsubsampling
[0] != 2 && ycbcrsubsampling
[0] != 4)
121 ||(ycbcrsubsampling
[1] != 1 && ycbcrsubsampling
[1] != 2 && ycbcrsubsampling
[1] != 4))
123 TIFFErrorExt(tif
->tif_clientdata
,module
,
124 "Invalid YCbCr subsampling (%dx%d)",
126 ycbcrsubsampling
[1] );
129 samplingblock_samples
=ycbcrsubsampling
[0]*ycbcrsubsampling
[1]+2;
130 samplingblocks_hor
=TIFFhowmany_32(td
->td_imagewidth
,ycbcrsubsampling
[0]);
131 samplingblocks_ver
=TIFFhowmany_32(nrows
,ycbcrsubsampling
[1]);
132 samplingrow_samples
=_TIFFMultiply64(tif
,samplingblocks_hor
,samplingblock_samples
,module
);
133 samplingrow_size
=TIFFhowmany8_64(_TIFFMultiply64(tif
,samplingrow_samples
,td
->td_bitspersample
,module
));
134 return(_TIFFMultiply64(tif
,samplingrow_size
,samplingblocks_ver
,module
));
137 return(_TIFFMultiply64(tif
,nrows
,TIFFScanlineSize64(tif
),module
));
140 TIFFVStripSize(TIFF
* tif
, uint32 nrows
)
142 static const char module
[] = "TIFFVStripSize";
145 m
=TIFFVStripSize64(tif
,nrows
);
149 TIFFErrorExt(tif
->tif_clientdata
,module
,"Integer overflow");
156 * Compute the # bytes in a raw strip.
159 TIFFRawStripSize64(TIFF
* tif
, uint32 strip
)
161 static const char module
[] = "TIFFRawStripSize64";
162 TIFFDirectory
* td
= &tif
->tif_dir
;
163 uint64 bytecount
= td
->td_stripbytecount
[strip
];
167 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
168 TIFFErrorExt(tif
->tif_clientdata
, module
,
169 "%I64u: Invalid strip byte count, strip %lu",
170 (unsigned __int64
) bytecount
,
171 (unsigned long) strip
);
173 TIFFErrorExt(tif
->tif_clientdata
, module
,
174 "%llu: Invalid strip byte count, strip %lu",
175 (unsigned long long) bytecount
,
176 (unsigned long) strip
);
178 bytecount
= (uint64
) -1;
184 TIFFRawStripSize(TIFF
* tif
, uint32 strip
)
186 static const char module
[] = "TIFFRawStripSize";
189 m
=TIFFRawStripSize64(tif
,strip
);
197 TIFFErrorExt(tif
->tif_clientdata
,module
,"Integer overflow");
205 * Compute the # bytes in a (row-aligned) strip.
207 * Note that if RowsPerStrip is larger than the
208 * recorded ImageLength, then the strip size is
209 * truncated to reflect the actual space required
213 TIFFStripSize64(TIFF
* tif
)
215 TIFFDirectory
* td
= &tif
->tif_dir
;
216 uint32 rps
= td
->td_rowsperstrip
;
217 if (rps
> td
->td_imagelength
)
218 rps
= td
->td_imagelength
;
219 return (TIFFVStripSize64(tif
, rps
));
222 TIFFStripSize(TIFF
* tif
)
224 static const char module
[] = "TIFFStripSize";
227 m
=TIFFStripSize64(tif
);
231 TIFFErrorExt(tif
->tif_clientdata
,module
,"Integer overflow");
238 * Compute a default strip size based on the image
239 * characteristics and a requested value. If the
240 * request is <1 then we choose a strip size according
241 * to certain heuristics.
244 TIFFDefaultStripSize(TIFF
* tif
, uint32 request
)
246 return (*tif
->tif_defstripsize
)(tif
, request
);
250 _TIFFDefaultStripSize(TIFF
* tif
, uint32 s
)
254 * If RowsPerStrip is unspecified, try to break the
255 * image up into strips that are approximately
256 * STRIP_SIZE_DEFAULT bytes long.
260 scanlinesize
=TIFFScanlineSize64(tif
);
263 rows
=(uint64
)STRIP_SIZE_DEFAULT
/scanlinesize
;
266 else if (rows
>0xFFFFFFFF)
274 * Return the number of bytes to read/write in a call to
275 * one of the scanline-oriented i/o routines. Note that
276 * this number may be 1/samples-per-pixel if data is
277 * stored as separate planes.
278 * The ScanlineSize in case of YCbCrSubsampling is defined as the
279 * strip size divided by the strip height, i.e. the size of a pack of vertical
280 * subsampling lines divided by vertical subsampling. It should thus make
281 * sense when multiplied by a multiple of vertical subsampling.
284 TIFFScanlineSize64(TIFF
* tif
)
286 static const char module
[] = "TIFFScanlineSize64";
287 TIFFDirectory
*td
= &tif
->tif_dir
;
288 uint64 scanline_size
;
289 if (td
->td_planarconfig
==PLANARCONFIG_CONTIG
)
291 if ((td
->td_photometric
==PHOTOMETRIC_YCBCR
)&&
292 (td
->td_samplesperpixel
==3)&&
295 uint16 ycbcrsubsampling
[2];
296 uint16 samplingblock_samples
;
297 uint32 samplingblocks_hor
;
298 uint64 samplingrow_samples
;
299 uint64 samplingrow_size
;
300 if(td
->td_samplesperpixel
!=3)
302 TIFFErrorExt(tif
->tif_clientdata
,module
,
303 "Invalid td_samplesperpixel value");
306 TIFFGetFieldDefaulted(tif
,TIFFTAG_YCBCRSUBSAMPLING
,
309 if (((ycbcrsubsampling
[0]!=1)&&(ycbcrsubsampling
[0]!=2)&&(ycbcrsubsampling
[0]!=4)) ||
310 ((ycbcrsubsampling
[1]!=1)&&(ycbcrsubsampling
[1]!=2)&&(ycbcrsubsampling
[1]!=4)))
312 TIFFErrorExt(tif
->tif_clientdata
,module
,
313 "Invalid YCbCr subsampling");
316 samplingblock_samples
= ycbcrsubsampling
[0]*ycbcrsubsampling
[1]+2;
317 samplingblocks_hor
= TIFFhowmany_32(td
->td_imagewidth
,ycbcrsubsampling
[0]);
318 samplingrow_samples
= _TIFFMultiply64(tif
,samplingblocks_hor
,samplingblock_samples
,module
);
319 samplingrow_size
= TIFFhowmany_64(_TIFFMultiply64(tif
,samplingrow_samples
,td
->td_bitspersample
,module
),8);
320 scanline_size
= (samplingrow_size
/ycbcrsubsampling
[1]);
324 uint64 scanline_samples
;
325 scanline_samples
=_TIFFMultiply64(tif
,td
->td_imagewidth
,td
->td_samplesperpixel
,module
);
326 scanline_size
=TIFFhowmany_64(_TIFFMultiply64(tif
,scanline_samples
,td
->td_bitspersample
,module
),8);
331 scanline_size
=TIFFhowmany_64(_TIFFMultiply64(tif
,td
->td_imagewidth
,td
->td_bitspersample
,module
),8);
333 if (scanline_size
== 0)
335 TIFFErrorExt(tif
->tif_clientdata
,module
,"Computed scanline size is zero");
338 return(scanline_size
);
341 TIFFScanlineSize(TIFF
* tif
)
343 static const char module
[] = "TIFFScanlineSize";
346 m
=TIFFScanlineSize64(tif
);
349 TIFFErrorExt(tif
->tif_clientdata
,module
,"Integer arithmetic overflow");
356 * Return the number of bytes required to store a complete
357 * decoded and packed raster scanline (as opposed to the
358 * I/O size returned by TIFFScanlineSize which may be less
359 * if data is store as separate planes).
362 TIFFRasterScanlineSize64(TIFF
* tif
)
364 static const char module
[] = "TIFFRasterScanlineSize64";
365 TIFFDirectory
*td
= &tif
->tif_dir
;
368 scanline
= _TIFFMultiply64(tif
, td
->td_bitspersample
, td
->td_imagewidth
, module
);
369 if (td
->td_planarconfig
== PLANARCONFIG_CONTIG
) {
370 scanline
= _TIFFMultiply64(tif
, scanline
, td
->td_samplesperpixel
, module
);
371 return (TIFFhowmany8_64(scanline
));
373 return (_TIFFMultiply64(tif
, TIFFhowmany8_64(scanline
),
374 td
->td_samplesperpixel
, module
));
377 TIFFRasterScanlineSize(TIFF
* tif
)
379 static const char module
[] = "TIFFRasterScanlineSize";
382 m
=TIFFRasterScanlineSize64(tif
);
386 TIFFErrorExt(tif
->tif_clientdata
,module
,"Integer arithmetic overflow");
392 /* vim: set ts=8 sts=8 sw=8 noet: */