1 /* $Id: tif_strip.c,v 1.1 2005/06/17 13:54:52 vp153 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.
35 summarize(TIFF* tif, size_t summand1, size_t summand2, const char* where)
38 * XXX: We are using casting to uint32 here, bacause sizeof(size_t)
39 * may be larger than sizeof(uint32) on 64-bit architectures.
41 uint32 bytes = summand1 + summand2;
43 if (bytes - summand1 != summand2) {
44 TIFFError(tif->tif_name, "Integer overflow in %s", where);
52 multiply(TIFF* tif, size_t nmemb, size_t elem_size, const char* where)
54 uint32 bytes = nmemb * elem_size;
56 if (elem_size && bytes / elem_size != nmemb) {
57 TIFFError(tif->tif_name, "Integer overflow in %s", where);
65 * Compute which strip a (row,sample) value is in.
68 TIFFComputeStrip(TIFF* tif, uint32 row, tsample_t sample)
70 TIFFDirectory *td = &tif->tif_dir;
73 strip = row / td->td_rowsperstrip;
74 if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
75 if (sample >= td->td_samplesperpixel) {
76 TIFFError(tif->tif_name,
77 "%lu: Sample out of range, max %lu",
78 (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
79 return ((tstrip_t) 0);
81 strip += sample*td->td_stripsperimage;
87 * Compute how many strips are in an image.
90 TIFFNumberOfStrips(TIFF* tif)
92 TIFFDirectory *td = &tif->tif_dir;
95 nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :
96 TIFFhowmany(td->td_imagelength, td->td_rowsperstrip));
97 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
98 nstrips = multiply(tif, nstrips, td->td_samplesperpixel,
99 "TIFFNumberOfStrips");
104 * Compute the # bytes in a variable height, row-aligned strip.
107 TIFFVStripSize(TIFF* tif, uint32 nrows)
109 TIFFDirectory *td = &tif->tif_dir;
111 if (nrows == (uint32) -1)
112 nrows = td->td_imagelength;
113 if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
114 td->td_photometric == PHOTOMETRIC_YCBCR &&
117 * Packed YCbCr data contain one Cb+Cr for every
118 * HorizontalSampling*VerticalSampling Y values.
119 * Must also roundup width and height when calculating
120 * since images that are not a multiple of the
121 * horizontal/vertical subsampling area include
122 * YCbCr data for the extended image.
124 uint16 ycbcrsubsampling[2];
125 tsize_t w, scanline, samplingarea;
127 TIFFGetField( tif, TIFFTAG_YCBCRSUBSAMPLING,
128 ycbcrsubsampling + 0,
129 ycbcrsubsampling + 1 );
131 samplingarea = ycbcrsubsampling[0]*ycbcrsubsampling[1];
132 if (samplingarea == 0) {
133 TIFFError(tif->tif_name, "Invalid YCbCr subsampling");
137 w = TIFFroundup(td->td_imagewidth, ycbcrsubsampling[0]);
138 scanline = TIFFhowmany8(multiply(tif, w, td->td_bitspersample,
140 nrows = TIFFroundup(nrows, ycbcrsubsampling[1]);
141 /* NB: don't need TIFFhowmany here 'cuz everything is rounded */
142 scanline = multiply(tif, nrows, scanline, "TIFFVStripSize");
144 summarize(tif, scanline,
145 multiply(tif, 2, scanline / samplingarea,
146 "TIFFVStripSize"), "TIFFVStripSize"));
148 return ((tsize_t) multiply(tif, nrows, TIFFScanlineSize(tif),
154 * Compute the # bytes in a raw strip.
157 TIFFRawStripSize(TIFF* tif, tstrip_t strip)
159 TIFFDirectory* td = &tif->tif_dir;
160 tsize_t bytecount = td->td_stripbytecount[strip];
162 if (bytecount <= 0) {
163 TIFFError(tif->tif_name,
164 "%lu: Invalid strip byte count, strip %lu",
165 (unsigned long) bytecount, (unsigned long) strip);
166 bytecount = (tsize_t) -1;
173 * Compute the # bytes in a (row-aligned) strip.
175 * Note that if RowsPerStrip is larger than the
176 * recorded ImageLength, then the strip size is
177 * truncated to reflect the actual space required
181 TIFFStripSize(TIFF* tif)
183 TIFFDirectory* td = &tif->tif_dir;
184 uint32 rps = td->td_rowsperstrip;
185 if (rps > td->td_imagelength)
186 rps = td->td_imagelength;
187 return (TIFFVStripSize(tif, rps));
191 * Compute a default strip size based on the image
192 * characteristics and a requested value. If the
193 * request is <1 then we choose a strip size according
194 * to certain heuristics.
197 TIFFDefaultStripSize(TIFF* tif, uint32 request)
199 return (*tif->tif_defstripsize)(tif, request);
203 _TIFFDefaultStripSize(TIFF* tif, uint32 s)
207 * If RowsPerStrip is unspecified, try to break the
208 * image up into strips that are approximately 8Kbytes.
210 tsize_t scanline = TIFFScanlineSize(tif);
211 s = (uint32)(8*1024) / (scanline == 0 ? 1 : scanline);
212 if (s == 0) /* very wide images */
219 * Return the number of bytes to read/write in a call to
220 * one of the scanline-oriented i/o routines. Note that
221 * this number may be 1/samples-per-pixel if data is
222 * stored as separate planes.
225 TIFFScanlineSize(TIFF* tif)
227 TIFFDirectory *td = &tif->tif_dir;
230 scanline = multiply (tif, td->td_bitspersample, td->td_imagewidth,
232 if (td->td_planarconfig == PLANARCONFIG_CONTIG)
233 scanline = multiply (tif, scanline, td->td_samplesperpixel,
235 return ((tsize_t) TIFFhowmany8(scanline));
239 * Return the number of bytes required to store a complete
240 * decoded and packed raster scanline (as opposed to the
241 * I/O size returned by TIFFScanlineSize which may be less
242 * if data is store as separate planes).
245 TIFFRasterScanlineSize(TIFF* tif)
247 TIFFDirectory *td = &tif->tif_dir;
250 scanline = multiply (tif, td->td_bitspersample, td->td_imagewidth,
251 "TIFFRasterScanlineSize");
252 if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
253 scanline = multiply (tif, scanline, td->td_samplesperpixel,
254 "TIFFRasterScanlineSize");
255 return ((tsize_t) TIFFhowmany8(scanline));
257 return ((tsize_t) multiply (tif, TIFFhowmany8(scanline),
258 td->td_samplesperpixel,
259 "TIFFRasterScanlineSize"));
262 /* vim: set ts=8 sts=8 sw=8 noet: */