ReactOS 0.4.16-dev-2613-g9533ad7
tif_strip.c
Go to the documentation of this file.
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 */
36{
37 static const char module[] = "TIFFComputeStrip";
38 TIFFDirectory *td = &tif->tif_dir;
39 uint32_t strip;
40
41 if (td->td_rowsperstrip == 0)
42 {
44 "Cannot compute strip: RowsPerStrip is zero");
45 return 0;
46 }
47 strip = row / td->td_rowsperstrip;
49 {
50 if (sample >= td->td_samplesperpixel)
51 {
52 TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu",
53 (unsigned long)sample,
54 (unsigned long)td->td_samplesperpixel);
55 return (0);
56 }
57 strip += (uint32_t)sample * td->td_stripsperimage;
58 }
59 return (strip);
60}
61
62/*
63 * Compute how many strips are in an image.
64 */
66{
67 TIFFDirectory *td = &tif->tif_dir;
68 uint32_t nstrips;
69
70 if (td->td_rowsperstrip == 0)
71 {
72 TIFFWarningExtR(tif, "TIFFNumberOfStrips", "RowsPerStrip is zero");
73 return 0;
74 }
75 nstrips = (td->td_rowsperstrip == (uint32_t)-1
76 ? 1
79 nstrips =
81 "TIFFNumberOfStrips");
82 return (nstrips);
83}
84
85/*
86 * Compute the # bytes in a variable height, row-aligned strip.
87 */
89{
90 static const char module[] = "TIFFVStripSize64";
91 TIFFDirectory *td = &tif->tif_dir;
92 if (nrows == (uint32_t)(-1))
93 nrows = td->td_imagelength;
96 {
97 /*
98 * Packed YCbCr data contain one Cb+Cr for every
99 * HorizontalSampling*VerticalSampling Y values.
100 * Must also roundup width and height when calculating
101 * since images that are not a multiple of the
102 * horizontal/vertical subsampling area include
103 * YCbCr data for the extended image.
104 */
105 uint16_t ycbcrsubsampling[2];
106 uint16_t samplingblock_samples;
107 uint32_t samplingblocks_hor;
108 uint32_t samplingblocks_ver;
109 uint64_t samplingrow_samples;
110 uint64_t samplingrow_size;
111 if (td->td_samplesperpixel != 3)
112 {
113 TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
114 return 0;
115 }
117 ycbcrsubsampling + 0, ycbcrsubsampling + 1);
118 if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 &&
119 ycbcrsubsampling[0] != 4) ||
120 (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 &&
121 ycbcrsubsampling[1] != 4) ||
122 (ycbcrsubsampling[0] == 0 || ycbcrsubsampling[1] == 0))
123 {
124 TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling (%dx%d)",
125 ycbcrsubsampling[0], ycbcrsubsampling[1]);
126 return 0;
127 }
128 samplingblock_samples = ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2;
129 samplingblocks_hor =
130 TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]);
131 samplingblocks_ver = TIFFhowmany_32(nrows, ycbcrsubsampling[1]);
132 samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor,
133 samplingblock_samples, module);
134 samplingrow_size = TIFFhowmany8_64(_TIFFMultiply64(
135 tif, samplingrow_samples, td->td_bitspersample, module));
136 return (
137 _TIFFMultiply64(tif, samplingrow_size, samplingblocks_ver, module));
138 }
139 else
140 return (_TIFFMultiply64(tif, nrows, TIFFScanlineSize64(tif), module));
141}
143{
144 static const char module[] = "TIFFVStripSize";
145 uint64_t m;
146 m = TIFFVStripSize64(tif, nrows);
147 return _TIFFCastUInt64ToSSize(tif, m, module);
148}
149
150/*
151 * Compute the # bytes in a raw strip.
152 */
154{
155 static const char module[] = "TIFFRawStripSize64";
156 uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
157
158 if (bytecount == 0)
159 {
161 "%" PRIu64 ": Invalid strip byte count, strip %lu",
162 (uint64_t)bytecount, (unsigned long)strip);
163 bytecount = (uint64_t)-1;
164 }
165
166 return bytecount;
167}
169{
170 static const char module[] = "TIFFRawStripSize";
171 uint64_t m;
172 tmsize_t n;
173 m = TIFFRawStripSize64(tif, strip);
174 if (m == (uint64_t)(-1))
175 n = (tmsize_t)(-1);
176 else
177 {
178 n = (tmsize_t)m;
179 if ((uint64_t)n != m)
180 {
181 TIFFErrorExtR(tif, module, "Integer overflow");
182 n = 0;
183 }
184 }
185 return (n);
186}
187
188/*
189 * Compute the # bytes in a (row-aligned) strip.
190 *
191 * Note that if RowsPerStrip is larger than the
192 * recorded ImageLength, then the strip size is
193 * truncated to reflect the actual space required
194 * to hold the strip.
195 */
197{
198 TIFFDirectory *td = &tif->tif_dir;
199 uint32_t rps = td->td_rowsperstrip;
200 if (rps > td->td_imagelength)
201 rps = td->td_imagelength;
202 return (TIFFVStripSize64(tif, rps));
203}
205{
206 static const char module[] = "TIFFStripSize";
207 uint64_t m;
208 m = TIFFStripSize64(tif);
209 return _TIFFCastUInt64ToSSize(tif, m, module);
210}
211
212/*
213 * Compute a default strip size based on the image
214 * characteristics and a requested value. If the
215 * request is <1 then we choose a strip size according
216 * to certain heuristics.
217 */
219{
220 return (*tif->tif_defstripsize)(tif, request);
221}
222
224{
225 if ((int32_t)s < 1)
226 {
227 /*
228 * If RowsPerStrip is unspecified, try to break the
229 * image up into strips that are approximately
230 * STRIP_SIZE_DEFAULT bytes long.
231 */
232 uint64_t scanlinesize;
233 uint64_t rows;
234 scanlinesize = TIFFScanlineSize64(tif);
235 if (scanlinesize == 0)
236 scanlinesize = 1;
237 rows = (uint64_t)STRIP_SIZE_DEFAULT / scanlinesize;
238 if (rows == 0)
239 rows = 1;
240 else if (rows > 0xFFFFFFFF)
241 rows = 0xFFFFFFFF;
242 s = (uint32_t)rows;
243 }
244 return (s);
245}
246
247/*
248 * Return the number of bytes to read/write in a call to
249 * one of the scanline-oriented i/o routines. Note that
250 * this number may be 1/samples-per-pixel if data is
251 * stored as separate planes.
252 * The ScanlineSize in case of YCbCrSubsampling is defined as the
253 * strip size divided by the strip height, i.e. the size of a pack of vertical
254 * subsampling lines divided by vertical subsampling. It should thus make
255 * sense when multiplied by a multiple of vertical subsampling.
256 */
258{
259 static const char module[] = "TIFFScanlineSize64";
260 TIFFDirectory *td = &tif->tif_dir;
261 uint64_t scanline_size;
263 {
264 if ((td->td_photometric == PHOTOMETRIC_YCBCR) &&
265 (td->td_samplesperpixel == 3) && (!isUpSampled(tif)))
266 {
267 uint16_t ycbcrsubsampling[2];
268 uint16_t samplingblock_samples;
269 uint32_t samplingblocks_hor;
270 uint64_t samplingrow_samples;
271 uint64_t samplingrow_size;
272 if (td->td_samplesperpixel != 3)
273 {
274 TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
275 return 0;
276 }
278 ycbcrsubsampling + 0, ycbcrsubsampling + 1);
279 if (((ycbcrsubsampling[0] != 1) && (ycbcrsubsampling[0] != 2) &&
280 (ycbcrsubsampling[0] != 4)) ||
281 ((ycbcrsubsampling[1] != 1) && (ycbcrsubsampling[1] != 2) &&
282 (ycbcrsubsampling[1] != 4)) ||
283 ((ycbcrsubsampling[0] == 0) || (ycbcrsubsampling[1] == 0)))
284 {
285 TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling");
286 return 0;
287 }
288 samplingblock_samples =
289 ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2;
290 samplingblocks_hor =
291 TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]);
292 samplingrow_samples = _TIFFMultiply64(
293 tif, samplingblocks_hor, samplingblock_samples, module);
294 samplingrow_size =
295 TIFFhowmany_64(_TIFFMultiply64(tif, samplingrow_samples,
297 8);
298 scanline_size = (samplingrow_size / ycbcrsubsampling[1]);
299 }
300 else
301 {
302 uint64_t scanline_samples;
303 uint32_t scanline_width = td->td_imagewidth;
304
305#if 0
306 // Tries to fix https://gitlab.com/libtiff/libtiff/-/merge_requests/564
307 // but causes regression when decoding legit files with tiffcp -c none
308 // Cf https://gitlab.com/libtiff/libtiff/-/merge_requests/644
310 {
311 uint16_t subsampling_hor;
314 &subsampling_hor, &ignored);
315 if (subsampling_hor > 1) // roundup width for YCbCr
316 scanline_width =
317 TIFFroundup_32(scanline_width, subsampling_hor);
318 }
319#endif
320
321 scanline_samples = _TIFFMultiply64(tif, scanline_width,
323 scanline_size =
324 TIFFhowmany_64(_TIFFMultiply64(tif, scanline_samples,
326 8);
327 }
328 }
329 else
330 {
331 scanline_size =
334 8);
335 }
336 if (scanline_size == 0)
337 {
338 TIFFErrorExtR(tif, module, "Computed scanline size is zero");
339 return 0;
340 }
341 return (scanline_size);
342}
344{
345 static const char module[] = "TIFFScanlineSize";
346 uint64_t m;
347 m = TIFFScanlineSize64(tif);
348 return _TIFFCastUInt64ToSSize(tif, m, module);
349}
350
351/*
352 * Return the number of bytes required to store a complete
353 * decoded and packed raster scanline (as opposed to the
354 * I/O size returned by TIFFScanlineSize which may be less
355 * if data is store as separate planes).
356 */
358{
359 static const char module[] = "TIFFRasterScanlineSize64";
360 TIFFDirectory *td = &tif->tif_dir;
361 uint64_t scanline;
362
363 scanline =
366 {
367 scanline =
368 _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
369 return (TIFFhowmany8_64(scanline));
370 }
371 else
372 return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
374}
376{
377 static const char module[] = "TIFFRasterScanlineSize";
378 uint64_t m;
380 return _TIFFCastUInt64ToSSize(tif, m, module);
381}
INT32 int32_t
Definition: types.h:71
UINT32 uint32_t
Definition: types.h:75
UINT64 uint64_t
Definition: types.h:77
#define PRIu64
Definition: inttypes.h:28
unsigned short uint16_t
Definition: stdint.h:35
struct png_info_def *typedef unsigned char **typedef struct png_info_def *typedef struct png_info_def *typedef struct png_info_def *typedef unsigned char ** row
Definition: typeof.h:78
uint8_t ignored[3]
Definition: fsck.fat.h:0
GLdouble s
Definition: gl.h:2039
GLdouble n
Definition: glext.h:7729
const GLfloat * m
Definition: glext.h:10848
#define uint32_t
Definition: nsiface.idl:61
#define uint64_t
Definition: nsiface.idl:62
uint32_t td_rowsperstrip
Definition: tif_dir.h:94
uint32_t td_imagewidth
Definition: tif_dir.h:83
uint16_t td_photometric
Definition: tif_dir.h:89
uint16_t td_bitspersample
Definition: tif_dir.h:86
uint32_t td_stripsperimage
Definition: tif_dir.h:110
uint16_t td_planarconfig
Definition: tif_dir.h:100
uint16_t td_samplesperpixel
Definition: tif_dir.h:93
uint32_t td_imagelength
Definition: tif_dir.h:83
Definition: tftpd.h:86
Definition: tiffiop.h:113
TIFFStripMethod tif_defstripsize
Definition: tiffiop.h:214
TIFFDirectory tif_dir
Definition: tiffiop.h:157
int TIFFGetFieldDefaulted(TIFF *tif, uint32_t tag,...)
Definition: tif_aux.c:380
uint32_t _TIFFMultiply32(TIFF *tif, uint32_t first, uint32_t second, const char *where)
Definition: tif_aux.c:35
tmsize_t _TIFFCastUInt64ToSSize(TIFF *tif, uint64_t val, const char *module)
Definition: tif_aux.c:84
uint64_t _TIFFMultiply64(TIFF *tif, uint64_t first, uint64_t second, const char *where)
Definition: tif_aux.c:47
#define STRIP_SIZE_DEFAULT
Definition: tif_config.h:127
uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile)
Definition: tif_dirread.c:8366
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Definition: tif_error.c:107
tmsize_t TIFFStripSize(TIFF *tif)
Definition: tif_strip.c:204
uint64_t TIFFRawStripSize64(TIFF *tif, uint32_t strip)
Definition: tif_strip.c:153
tmsize_t TIFFScanlineSize(TIFF *tif)
Definition: tif_strip.c:343
tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows)
Definition: tif_strip.c:142
uint64_t TIFFScanlineSize64(TIFF *tif)
Definition: tif_strip.c:257
uint32_t TIFFNumberOfStrips(TIFF *tif)
Definition: tif_strip.c:65
uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s)
Definition: tif_strip.c:223
uint32_t TIFFComputeStrip(TIFF *tif, uint32_t row, uint16_t sample)
Definition: tif_strip.c:35
uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows)
Definition: tif_strip.c:88
tmsize_t TIFFRawStripSize(TIFF *tif, uint32_t strip)
Definition: tif_strip.c:168
uint64_t TIFFStripSize64(TIFF *tif)
Definition: tif_strip.c:196
uint32_t TIFFDefaultStripSize(TIFF *tif, uint32_t request)
Definition: tif_strip.c:218
uint64_t TIFFRasterScanlineSize64(TIFF *tif)
Definition: tif_strip.c:357
tmsize_t TIFFRasterScanlineSize(TIFF *tif)
Definition: tif_strip.c:375
void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt,...)
Definition: tif_warning.c:80
#define PLANARCONFIG_SEPARATE
Definition: tiff.h:266
#define PHOTOMETRIC_YCBCR
Definition: tiff.h:227
#define TIFFTAG_YCBCRSUBSAMPLING
Definition: tiff.h:388
#define PLANARCONFIG_CONTIG
Definition: tiff.h:265
TIFF_SSIZE_T tmsize_t
Definition: tiffio.h:67
#define TIFFhowmany_64(x, y)
Definition: tiffiop.h:317
#define isUpSampled(tif)
Definition: tiffiop.h:277
#define TIFFroundup_32(x, y)
Definition: tiffiop.h:316
#define TIFFhowmany_32(x, y)
Definition: tiffiop.h:305
#define TIFFhowmany8_64(x)
Definition: tiffiop.h:319