Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/tiff/libtiff/tif_strip.c
8524 views
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 TIFFComputeStrip(TIFF *tif, uint32_t row, uint16_t sample)
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
{
43
TIFFErrorExtR(tif, module,
44
"Cannot compute strip: RowsPerStrip is zero");
45
return 0;
46
}
47
strip = row / td->td_rowsperstrip;
48
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
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
*/
65
uint32_t TIFFNumberOfStrips(TIFF *tif)
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
77
: TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
78
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
79
nstrips =
80
_TIFFMultiply32(tif, nstrips, (uint32_t)td->td_samplesperpixel,
81
"TIFFNumberOfStrips");
82
return (nstrips);
83
}
84
85
/*
86
* Compute the # bytes in a variable height, row-aligned strip.
87
*/
88
uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows)
89
{
90
static const char module[] = "TIFFVStripSize64";
91
TIFFDirectory *td = &tif->tif_dir;
92
if (nrows == (uint32_t)(-1))
93
nrows = td->td_imagelength;
94
if ((td->td_planarconfig == PLANARCONFIG_CONTIG) &&
95
(td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
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
}
116
TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
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
}
142
tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows)
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
*/
153
uint64_t TIFFRawStripSize64(TIFF *tif, uint32_t strip)
154
{
155
static const char module[] = "TIFFRawStripSize64";
156
uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
157
158
if (bytecount == 0)
159
{
160
TIFFErrorExtR(tif, module,
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
}
168
tmsize_t TIFFRawStripSize(TIFF *tif, uint32_t strip)
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
*/
196
uint64_t TIFFStripSize64(TIFF *tif)
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
}
204
tmsize_t TIFFStripSize(TIFF *tif)
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
*/
218
uint32_t TIFFDefaultStripSize(TIFF *tif, uint32_t request)
219
{
220
return (*tif->tif_defstripsize)(tif, request);
221
}
222
223
uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s)
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
*/
257
uint64_t TIFFScanlineSize64(TIFF *tif)
258
{
259
static const char module[] = "TIFFScanlineSize64";
260
TIFFDirectory *td = &tif->tif_dir;
261
uint64_t scanline_size;
262
if (td->td_planarconfig == PLANARCONFIG_CONTIG)
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
}
277
TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
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,
296
td->td_bitspersample, module),
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
309
if (td->td_photometric == PHOTOMETRIC_YCBCR)
310
{
311
uint16_t subsampling_hor;
312
uint16_t ignored;
313
TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
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,
322
td->td_samplesperpixel, module);
323
scanline_size =
324
TIFFhowmany_64(_TIFFMultiply64(tif, scanline_samples,
325
td->td_bitspersample, module),
326
8);
327
}
328
}
329
else
330
{
331
scanline_size =
332
TIFFhowmany_64(_TIFFMultiply64(tif, td->td_imagewidth,
333
td->td_bitspersample, module),
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
}
343
tmsize_t TIFFScanlineSize(TIFF *tif)
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
*/
357
uint64_t TIFFRasterScanlineSize64(TIFF *tif)
358
{
359
static const char module[] = "TIFFRasterScanlineSize64";
360
TIFFDirectory *td = &tif->tif_dir;
361
uint64_t scanline;
362
363
scanline =
364
_TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
365
if (td->td_planarconfig == PLANARCONFIG_CONTIG)
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),
373
td->td_samplesperpixel, module));
374
}
375
tmsize_t TIFFRasterScanlineSize(TIFF *tif)
376
{
377
static const char module[] = "TIFFRasterScanlineSize";
378
uint64_t m;
379
m = TIFFRasterScanlineSize64(tif);
380
return _TIFFCastUInt64ToSSize(tif, m, module);
381
}
382
383