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