ReactOS 0.4.16-dev-297-gc569aee
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 <precomp.h>
31
32/*
33 * Compute which strip a (row,sample) value is in.
34 */
37{
38 static const char module[] = "TIFFComputeStrip";
39 TIFFDirectory *td = &tif->tif_dir;
40 uint32 strip;
41
42 strip = row / td->td_rowsperstrip;
44 if (sample >= td->td_samplesperpixel) {
46 "%lu: Sample out of range, max %lu",
47 (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
48 return (0);
49 }
50 strip += (uint32)sample*td->td_stripsperimage;
51 }
52 return (strip);
53}
54
55/*
56 * Compute how many strips are in an image.
57 */
60{
61 TIFFDirectory *td = &tif->tif_dir;
62 uint32 nstrips;
63
64 nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :
67 nstrips = _TIFFMultiply32(tif, nstrips, (uint32)td->td_samplesperpixel,
68 "TIFFNumberOfStrips");
69 return (nstrips);
70}
71
72/*
73 * Compute the # bytes in a variable height, row-aligned strip.
74 */
77{
78 static const char module[] = "TIFFVStripSize64";
79 TIFFDirectory *td = &tif->tif_dir;
80 if (nrows==(uint32)(-1))
81 nrows=td->td_imagelength;
84 (!isUpSampled(tif)))
85 {
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 uint16 ycbcrsubsampling[2];
95 uint16 samplingblock_samples;
96 uint32 samplingblocks_hor;
97 uint32 samplingblocks_ver;
98 uint64 samplingrow_samples;
99 uint64 samplingrow_size;
100 if(td->td_samplesperpixel!=3)
101 {
103 "Invalid td_samplesperpixel value");
104 return 0;
105 }
106 TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
107 ycbcrsubsampling+1);
108 if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && ycbcrsubsampling[0] != 4)
109 ||(ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && ycbcrsubsampling[1] != 4))
110 {
112 "Invalid YCbCr subsampling (%dx%d)",
113 ycbcrsubsampling[0],
114 ycbcrsubsampling[1] );
115 return 0;
116 }
117 samplingblock_samples=ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
118 samplingblocks_hor=TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
119 samplingblocks_ver=TIFFhowmany_32(nrows,ycbcrsubsampling[1]);
120 samplingrow_samples=_TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
121 samplingrow_size=TIFFhowmany8_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module));
122 return(_TIFFMultiply64(tif,samplingrow_size,samplingblocks_ver,module));
123 }
124 else
125 return(_TIFFMultiply64(tif,nrows,TIFFScanlineSize64(tif),module));
126}
129{
130 static const char module[] = "TIFFVStripSize";
131 uint64 m;
132 m=TIFFVStripSize64(tif,nrows);
133 return _TIFFCastUInt64ToSSize(tif, m, module);
134}
135
136/*
137 * Compute the # bytes in a raw strip.
138 */
139uint64
141{
142 static const char module[] = "TIFFRawStripSize64";
143 uint64 bytecount = TIFFGetStrileByteCount(tif, strip);
144
145 if (bytecount == 0)
146 {
147#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
149 "%I64u: Invalid strip byte count, strip %lu",
150 (unsigned __int64) bytecount,
151 (unsigned long) strip);
152#else
154 "%llu: Invalid strip byte count, strip %lu",
155 (unsigned long long) bytecount,
156 (unsigned long) strip);
157#endif
158 bytecount = (uint64) -1;
159 }
160
161 return bytecount;
162}
165{
166 static const char module[] = "TIFFRawStripSize";
167 uint64 m;
168 tmsize_t n;
169 m=TIFFRawStripSize64(tif,strip);
170 if (m==(uint64)(-1))
171 n=(tmsize_t)(-1);
172 else
173 {
174 n=(tmsize_t)m;
175 if ((uint64)n!=m)
176 {
177 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
178 n=0;
179 }
180 }
181 return(n);
182}
183
184/*
185 * Compute the # bytes in a (row-aligned) strip.
186 *
187 * Note that if RowsPerStrip is larger than the
188 * recorded ImageLength, then the strip size is
189 * truncated to reflect the actual space required
190 * to hold the strip.
191 */
192uint64
194{
195 TIFFDirectory* td = &tif->tif_dir;
196 uint32 rps = td->td_rowsperstrip;
197 if (rps > td->td_imagelength)
198 rps = td->td_imagelength;
199 return (TIFFVStripSize64(tif, rps));
200}
203{
204 static const char module[] = "TIFFStripSize";
205 uint64 m;
206 m=TIFFStripSize64(tif);
207 return _TIFFCastUInt64ToSSize(tif, m, module);
208}
209
210/*
211 * Compute a default strip size based on the image
212 * characteristics and a requested value. If the
213 * request is <1 then we choose a strip size according
214 * to certain heuristics.
215 */
216uint32
218{
219 return (*tif->tif_defstripsize)(tif, request);
220}
221
222uint32
224{
225 if ((int32) s < 1) {
226 /*
227 * If RowsPerStrip is unspecified, try to break the
228 * image up into strips that are approximately
229 * STRIP_SIZE_DEFAULT bytes long.
230 */
231 uint64 scanlinesize;
232 uint64 rows;
233 scanlinesize=TIFFScanlineSize64(tif);
234 if (scanlinesize==0)
235 scanlinesize=1;
236 rows=(uint64)STRIP_SIZE_DEFAULT/scanlinesize;
237 if (rows==0)
238 rows=1;
239 else if (rows>0xFFFFFFFF)
240 rows=0xFFFFFFFF;
241 s=(uint32)rows;
242 }
243 return (s);
244}
245
246/*
247 * Return the number of bytes to read/write in a call to
248 * one of the scanline-oriented i/o routines. Note that
249 * this number may be 1/samples-per-pixel if data is
250 * stored as separate planes.
251 * The ScanlineSize in case of YCbCrSubsampling is defined as the
252 * strip size divided by the strip height, i.e. the size of a pack of vertical
253 * subsampling lines divided by vertical subsampling. It should thus make
254 * sense when multiplied by a multiple of vertical subsampling.
255 */
256uint64
258{
259 static const char module[] = "TIFFScanlineSize64";
260 TIFFDirectory *td = &tif->tif_dir;
261 uint64 scanline_size;
263 {
265 (td->td_samplesperpixel==3)&&
266 (!isUpSampled(tif)))
267 {
268 uint16 ycbcrsubsampling[2];
269 uint16 samplingblock_samples;
270 uint32 samplingblocks_hor;
271 uint64 samplingrow_samples;
272 uint64 samplingrow_size;
273 if(td->td_samplesperpixel!=3)
274 {
276 "Invalid td_samplesperpixel value");
277 return 0;
278 }
280 ycbcrsubsampling+0,
281 ycbcrsubsampling+1);
282 if (((ycbcrsubsampling[0]!=1)&&(ycbcrsubsampling[0]!=2)&&(ycbcrsubsampling[0]!=4)) ||
283 ((ycbcrsubsampling[1]!=1)&&(ycbcrsubsampling[1]!=2)&&(ycbcrsubsampling[1]!=4)))
284 {
286 "Invalid YCbCr subsampling");
287 return 0;
288 }
289 samplingblock_samples = ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
290 samplingblocks_hor = TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
291 samplingrow_samples = _TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
292 samplingrow_size = TIFFhowmany_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module),8);
293 scanline_size = (samplingrow_size/ycbcrsubsampling[1]);
294 }
295 else
296 {
297 uint64 scanline_samples;
298 scanline_samples=_TIFFMultiply64(tif,td->td_imagewidth,td->td_samplesperpixel,module);
299 scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,scanline_samples,td->td_bitspersample,module),8);
300 }
301 }
302 else
303 {
305 }
306 if (scanline_size == 0)
307 {
308 TIFFErrorExt(tif->tif_clientdata,module,"Computed scanline size is zero");
309 return 0;
310 }
311 return(scanline_size);
312}
315{
316 static const char module[] = "TIFFScanlineSize";
317 uint64 m;
319 return _TIFFCastUInt64ToSSize(tif, m, module);
320}
321
322/*
323 * Return the number of bytes required to store a complete
324 * decoded and packed raster scanline (as opposed to the
325 * I/O size returned by TIFFScanlineSize which may be less
326 * if data is store as separate planes).
327 */
328uint64
330{
331 static const char module[] = "TIFFRasterScanlineSize64";
332 TIFFDirectory *td = &tif->tif_dir;
333 uint64 scanline;
334
335 scanline = _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
337 scanline = _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
338 return (TIFFhowmany8_64(scanline));
339 } else
340 return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
342}
345{
346 static const char module[] = "TIFFRasterScanlineSize";
347 uint64 m;
349 return _TIFFCastUInt64ToSSize(tif, m, module);
350}
351
352/* vim: set ts=8 sts=8 sw=8 noet: */
353/*
354 * Local Variables:
355 * mode: c
356 * c-basic-offset: 8
357 * fill-column: 78
358 * End:
359 */
unsigned short uint16
Definition: types.h:30
unsigned int uint32
Definition: types.h:32
#define __int64
Definition: basetyps.h:16
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
unsigned long long uint64
Definition: platform.h:18
long int32
Definition: platform.h:12
GLdouble s
Definition: gl.h:2039
GLdouble n
Definition: glext.h:7729
const GLfloat * m
Definition: glext.h:10848
uint16 td_samplesperpixel
Definition: tif_dir.h:82
uint16 td_bitspersample
Definition: tif_dir.h:75
uint32 td_imagewidth
Definition: tif_dir.h:72
uint32 td_rowsperstrip
Definition: tif_dir.h:83
uint16 td_photometric
Definition: tif_dir.h:78
uint16 td_planarconfig
Definition: tif_dir.h:89
uint32 td_stripsperimage
Definition: tif_dir.h:99
uint32 td_imagelength
Definition: tif_dir.h:72
Definition: tftpd.h:86
Definition: tiffiop.h:115
thandle_t tif_clientdata
Definition: tiffiop.h:207
TIFFStripMethod tif_defstripsize
Definition: tiffiop.h:189
TIFFDirectory tif_dir
Definition: tiffiop.h:151
uint64 _TIFFMultiply64(TIFF *tif, uint64 first, uint64 second, const char *where)
Definition: tif_aux.c:48
int TIFFGetFieldDefaulted(TIFF *tif, uint32 tag,...)
Definition: tif_aux.c:345
uint32 _TIFFMultiply32(TIFF *tif, uint32 first, uint32 second, const char *where)
Definition: tif_aux.c:37
tmsize_t _TIFFCastUInt64ToSSize(TIFF *tif, uint64 val, const char *module)
Definition: tif_aux.c:83
uint64 TIFFGetStrileByteCount(TIFF *tif, uint32 strile)
Definition: tif_dirread.c:6305
void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt,...)
Definition: tif_error.c:65
uint32 TIFFComputeStrip(TIFF *tif, uint32 row, uint16 sample)
Definition: tif_strip.c:36
tmsize_t TIFFStripSize(TIFF *tif)
Definition: tif_strip.c:202
tmsize_t TIFFScanlineSize(TIFF *tif)
Definition: tif_strip.c:314
tmsize_t TIFFRawStripSize(TIFF *tif, uint32 strip)
Definition: tif_strip.c:164
uint64 TIFFScanlineSize64(TIFF *tif)
Definition: tif_strip.c:257
uint64 TIFFRawStripSize64(TIFF *tif, uint32 strip)
Definition: tif_strip.c:140
uint64 TIFFRasterScanlineSize64(TIFF *tif)
Definition: tif_strip.c:329
uint32 TIFFDefaultStripSize(TIFF *tif, uint32 request)
Definition: tif_strip.c:217
uint64 TIFFVStripSize64(TIFF *tif, uint32 nrows)
Definition: tif_strip.c:76
tmsize_t TIFFVStripSize(TIFF *tif, uint32 nrows)
Definition: tif_strip.c:128
tmsize_t TIFFRasterScanlineSize(TIFF *tif)
Definition: tif_strip.c:344
uint32 _TIFFDefaultStripSize(TIFF *tif, uint32 s)
Definition: tif_strip.c:223
uint64 TIFFStripSize64(TIFF *tif)
Definition: tif_strip.c:193
uint32 TIFFNumberOfStrips(TIFF *tif)
Definition: tif_strip.c:59
#define PLANARCONFIG_SEPARATE
Definition: tiff.h:240
#define PHOTOMETRIC_YCBCR
Definition: tiff.h:201
#define TIFFTAG_YCBCRSUBSAMPLING
Definition: tiff.h:362
#define PLANARCONFIG_CONTIG
Definition: tiff.h:239
TIFF_SSIZE_T tmsize_t
Definition: tiffio.h:65
#define TIFFhowmany_64(x, y)
Definition: tiffiop.h:273
#define isUpSampled(tif)
Definition: tiffiop.h:232
#define STRIP_SIZE_DEFAULT
Definition: tiffiop.h:69
#define TIFFhowmany_32(x, y)
Definition: tiffiop.h:264
#define TIFFhowmany8_64(x)
Definition: tiffiop.h:274