1 /* $Id: tif_read.c,v 1.1 2005-06-17 13:54:52 vp153 Exp $ */
4 * Copyright (c) 1988-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
29 * Scanline-oriented Read Support
34 int TIFFFillStrip(TIFF*, tstrip_t);
35 int TIFFFillTile(TIFF*, ttile_t);
36 static int TIFFStartStrip(TIFF*, tstrip_t);
37 static int TIFFStartTile(TIFF*, ttile_t);
38 static int TIFFCheckRead(TIFF*, int);
40 #define NOSTRIP ((tstrip_t) -1) /* undefined state */
41 #define NOTILE ((ttile_t) -1) /* undefined state */
44 * Seek to a random row+sample in a file.
47 TIFFSeek(TIFF* tif, uint32 row, tsample_t sample)
49 register TIFFDirectory *td = &tif->tif_dir;
52 if (row >= td->td_imagelength) { /* out of range */
53 TIFFError(tif->tif_name, "%lu: Row out of range, max %lu",
54 (unsigned long) row, (unsigned long) td->td_imagelength);
57 if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
58 if (sample >= td->td_samplesperpixel) {
59 TIFFError(tif->tif_name,
60 "%lu: Sample out of range, max %lu",
61 (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
64 strip = sample*td->td_stripsperimage + row/td->td_rowsperstrip;
66 strip = row / td->td_rowsperstrip;
67 if (strip != tif->tif_curstrip) { /* different strip, refill */
68 if (!TIFFFillStrip(tif, strip))
70 } else if (row < tif->tif_row) {
72 * Moving backwards within the same strip: backup
73 * to the start and then decode forward (below).
75 * NB: If you're planning on lots of random access within a
76 * strip, it's better to just read and decode the entire
77 * strip, and then access the decoded data in a random fashion.
79 if (!TIFFStartStrip(tif, strip))
82 if (row != tif->tif_row) {
84 * Seek forward to the desired row.
86 if (!(*tif->tif_seek)(tif, row - tif->tif_row))
94 TIFFReadScanline(TIFF* tif, tdata_t buf, uint32 row, tsample_t sample)
98 if (!TIFFCheckRead(tif, 0))
100 if( (e = TIFFSeek(tif, row, sample)) != 0) {
102 * Decompress desired row into user buffer.
104 e = (*tif->tif_decoderow)
105 (tif, (tidata_t) buf, tif->tif_scanlinesize, sample);
107 /* we are now poised at the beginning of the next row */
108 tif->tif_row = row + 1;
111 (*tif->tif_postdecode)(tif, (tidata_t) buf,
112 tif->tif_scanlinesize);
114 return (e > 0 ? 1 : -1);
118 * Read a strip of data and decompress the specified
119 * amount into the user-supplied buffer.
122 TIFFReadEncodedStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
124 TIFFDirectory *td = &tif->tif_dir;
127 tstrip_t sep_strip, strips_per_sep;
129 if (!TIFFCheckRead(tif, 0))
131 if (strip >= td->td_nstrips) {
132 TIFFError(tif->tif_name, "%ld: Strip out of range, max %ld",
133 (long) strip, (long) td->td_nstrips);
137 * Calculate the strip size according to the number of
138 * rows in the strip (check for truncated last strip on any
139 * of the separations).
141 if( td->td_rowsperstrip >= td->td_imagelength )
144 strips_per_sep = (td->td_imagelength+td->td_rowsperstrip-1)
145 / td->td_rowsperstrip;
147 sep_strip = strip % strips_per_sep;
149 if (sep_strip != strips_per_sep-1 ||
150 (nrows = td->td_imagelength % td->td_rowsperstrip) == 0)
151 nrows = td->td_rowsperstrip;
153 stripsize = TIFFVStripSize(tif, nrows);
154 if (size == (tsize_t) -1)
156 else if (size > stripsize)
158 if (TIFFFillStrip(tif, strip)
159 && (*tif->tif_decodestrip)(tif, (tidata_t) buf, size,
160 (tsample_t)(strip / td->td_stripsperimage)) > 0 ) {
161 (*tif->tif_postdecode)(tif, (tidata_t) buf, size);
164 return ((tsize_t) -1);
168 TIFFReadRawStrip1(TIFF* tif,
169 tstrip_t strip, tdata_t buf, tsize_t size, const char* module)
171 TIFFDirectory *td = &tif->tif_dir;
173 if (!isMapped(tif)) {
176 if (!SeekOK(tif, td->td_stripoffset[strip])) {
178 "%s: Seek error at scanline %lu, strip %lu",
180 (unsigned long) tif->tif_row, (unsigned long) strip);
183 cc = TIFFReadFile(tif, buf, size);
186 "%s: Read error at scanline %lu; got %lu bytes, expected %lu",
188 (unsigned long) tif->tif_row,
190 (unsigned long) size);
194 if (td->td_stripoffset[strip] + size > tif->tif_size) {
196 "%s: Read error at scanline %lu, strip %lu; got %lu bytes, expected %lu",
198 (unsigned long) tif->tif_row,
199 (unsigned long) strip,
200 (unsigned long) tif->tif_size - td->td_stripoffset[strip],
201 (unsigned long) size);
204 _TIFFmemcpy(buf, tif->tif_base + td->td_stripoffset[strip],
211 * Read a strip of data from the file.
214 TIFFReadRawStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
216 static const char module[] = "TIFFReadRawStrip";
217 TIFFDirectory *td = &tif->tif_dir;
220 if (!TIFFCheckRead(tif, 0))
221 return ((tsize_t) -1);
222 if (strip >= td->td_nstrips) {
223 TIFFError(tif->tif_name, "%lu: Strip out of range, max %lu",
224 (unsigned long) strip, (unsigned long) td->td_nstrips);
225 return ((tsize_t) -1);
227 bytecount = td->td_stripbytecount[strip];
228 if (bytecount <= 0) {
229 TIFFError(tif->tif_name,
230 "%lu: Invalid strip byte count, strip %lu",
231 (unsigned long) bytecount, (unsigned long) strip);
232 return ((tsize_t) -1);
234 if (size != (tsize_t)-1 && size < bytecount)
236 return (TIFFReadRawStrip1(tif, strip, buf, bytecount, module));
240 * Read the specified strip and setup for decoding.
241 * The data buffer is expanded, as necessary, to
242 * hold the strip's data.
245 TIFFFillStrip(TIFF* tif, tstrip_t strip)
247 static const char module[] = "TIFFFillStrip";
248 TIFFDirectory *td = &tif->tif_dir;
251 bytecount = td->td_stripbytecount[strip];
252 if (bytecount <= 0) {
253 TIFFError(tif->tif_name,
254 "%lu: Invalid strip byte count, strip %lu",
255 (unsigned long) bytecount, (unsigned long) strip);
259 (isFillOrder(tif, td->td_fillorder)
260 || (tif->tif_flags & TIFF_NOBITREV))) {
262 * The image is mapped into memory and we either don't
263 * need to flip bits or the compression routine is going
264 * to handle this operation itself. In this case, avoid
265 * copying the raw data and instead just reference the
266 * data from the memory mapped file image. This assumes
267 * that the decompression routines do not modify the
268 * contents of the raw data buffer (if they try to,
269 * the application will get a fault since the file is
272 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
273 _TIFFfree(tif->tif_rawdata);
274 tif->tif_flags &= ~TIFF_MYBUFFER;
275 if ( td->td_stripoffset[strip] + bytecount > tif->tif_size) {
277 * This error message might seem strange, but it's
278 * what would happen if a read were done instead.
281 "%s: Read error on strip %lu; got %lu bytes, expected %lu",
283 (unsigned long) strip,
284 (unsigned long) tif->tif_size - td->td_stripoffset[strip],
285 (unsigned long) bytecount);
286 tif->tif_curstrip = NOSTRIP;
289 tif->tif_rawdatasize = bytecount;
290 tif->tif_rawdata = tif->tif_base + td->td_stripoffset[strip];
293 * Expand raw data buffer, if needed, to
294 * hold data strip coming from file
295 * (perhaps should set upper bound on
296 * the size of a buffer we'll use?).
298 if (bytecount > tif->tif_rawdatasize) {
299 tif->tif_curstrip = NOSTRIP;
300 if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
302 "%s: Data buffer too small to hold strip %lu",
303 tif->tif_name, (unsigned long) strip);
306 if (!TIFFReadBufferSetup(tif, 0,
307 TIFFroundup(bytecount, 1024)))
310 if (TIFFReadRawStrip1(tif, strip, (unsigned char *)tif->tif_rawdata,
311 bytecount, module) != bytecount)
313 if (!isFillOrder(tif, td->td_fillorder) &&
314 (tif->tif_flags & TIFF_NOBITREV) == 0)
315 TIFFReverseBits(tif->tif_rawdata, bytecount);
317 return (TIFFStartStrip(tif, strip));
321 * Tile-oriented Read Support
322 * Contributed by Nancy Cam (Silicon Graphics).
326 * Read and decompress a tile of data. The
327 * tile is selected by the (x,y,z,s) coordinates.
330 TIFFReadTile(TIFF* tif,
331 tdata_t buf, uint32 x, uint32 y, uint32 z, tsample_t s)
333 if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
335 return (TIFFReadEncodedTile(tif,
336 TIFFComputeTile(tif, x, y, z, s), buf, (tsize_t) -1));
340 * Read a tile of data and decompress the specified
341 * amount into the user-supplied buffer.
344 TIFFReadEncodedTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
346 TIFFDirectory *td = &tif->tif_dir;
347 tsize_t tilesize = tif->tif_tilesize;
349 if (!TIFFCheckRead(tif, 1))
351 if (tile >= td->td_nstrips) {
352 TIFFError(tif->tif_name, "%ld: Tile out of range, max %ld",
353 (long) tile, (unsigned long) td->td_nstrips);
356 if (size == (tsize_t) -1)
358 else if (size > tilesize)
360 if (TIFFFillTile(tif, tile) && (*tif->tif_decodetile)(tif,
361 (tidata_t) buf, size, (tsample_t)(tile/td->td_stripsperimage))) {
362 (*tif->tif_postdecode)(tif, (tidata_t) buf, size);
369 TIFFReadRawTile1(TIFF* tif,
370 ttile_t tile, tdata_t buf, tsize_t size, const char* module)
372 TIFFDirectory *td = &tif->tif_dir;
374 if (!isMapped(tif)) {
377 if (!SeekOK(tif, td->td_stripoffset[tile])) {
379 "%s: Seek error at row %ld, col %ld, tile %ld",
384 return ((tsize_t) -1);
386 cc = TIFFReadFile(tif, buf, size);
389 "%s: Read error at row %ld, col %ld; got %lu bytes, expected %lu",
394 (unsigned long) size);
395 return ((tsize_t) -1);
398 if (td->td_stripoffset[tile] + size > tif->tif_size) {
400 "%s: Read error at row %ld, col %ld, tile %ld; got %lu bytes, expected %lu",
405 (unsigned long) tif->tif_size - td->td_stripoffset[tile],
406 (unsigned long) size);
407 return ((tsize_t) -1);
409 _TIFFmemcpy(buf, tif->tif_base + td->td_stripoffset[tile], size);
415 * Read a tile of data from the file.
418 TIFFReadRawTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
420 static const char module[] = "TIFFReadRawTile";
421 TIFFDirectory *td = &tif->tif_dir;
424 if (!TIFFCheckRead(tif, 1))
425 return ((tsize_t) -1);
426 if (tile >= td->td_nstrips) {
427 TIFFError(tif->tif_name, "%lu: Tile out of range, max %lu",
428 (unsigned long) tile, (unsigned long) td->td_nstrips);
429 return ((tsize_t) -1);
431 bytecount = td->td_stripbytecount[tile];
432 if (size != (tsize_t) -1 && size < bytecount)
434 return (TIFFReadRawTile1(tif, tile, buf, bytecount, module));
438 * Read the specified tile and setup for decoding.
439 * The data buffer is expanded, as necessary, to
440 * hold the tile's data.
443 TIFFFillTile(TIFF* tif, ttile_t tile)
445 static const char module[] = "TIFFFillTile";
446 TIFFDirectory *td = &tif->tif_dir;
449 bytecount = td->td_stripbytecount[tile];
450 if (bytecount <= 0) {
451 TIFFError(tif->tif_name,
452 "%lu: Invalid tile byte count, tile %lu",
453 (unsigned long) bytecount, (unsigned long) tile);
457 (isFillOrder(tif, td->td_fillorder)
458 || (tif->tif_flags & TIFF_NOBITREV))) {
460 * The image is mapped into memory and we either don't
461 * need to flip bits or the compression routine is going
462 * to handle this operation itself. In this case, avoid
463 * copying the raw data and instead just reference the
464 * data from the memory mapped file image. This assumes
465 * that the decompression routines do not modify the
466 * contents of the raw data buffer (if they try to,
467 * the application will get a fault since the file is
470 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
471 _TIFFfree(tif->tif_rawdata);
472 tif->tif_flags &= ~TIFF_MYBUFFER;
473 if ( td->td_stripoffset[tile] + bytecount > tif->tif_size) {
474 tif->tif_curtile = NOTILE;
477 tif->tif_rawdatasize = bytecount;
478 tif->tif_rawdata = tif->tif_base + td->td_stripoffset[tile];
481 * Expand raw data buffer, if needed, to
482 * hold data tile coming from file
483 * (perhaps should set upper bound on
484 * the size of a buffer we'll use?).
486 if (bytecount > tif->tif_rawdatasize) {
487 tif->tif_curtile = NOTILE;
488 if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
490 "%s: Data buffer too small to hold tile %ld",
491 tif->tif_name, (long) tile);
494 if (!TIFFReadBufferSetup(tif, 0,
495 TIFFroundup(bytecount, 1024)))
498 if (TIFFReadRawTile1(tif, tile,
499 (unsigned char *)tif->tif_rawdata,
500 bytecount, module) != bytecount)
502 if (!isFillOrder(tif, td->td_fillorder) &&
503 (tif->tif_flags & TIFF_NOBITREV) == 0)
504 TIFFReverseBits(tif->tif_rawdata, bytecount);
506 return (TIFFStartTile(tif, tile));
510 * Setup the raw data buffer in preparation for
511 * reading a strip of raw data. If the buffer
512 * is specified as zero, then a buffer of appropriate
513 * size is allocated by the library. Otherwise,
514 * the client must guarantee that the buffer is
515 * large enough to hold any individual strip of
519 TIFFReadBufferSetup(TIFF* tif, tdata_t bp, tsize_t size)
521 static const char module[] = "TIFFReadBufferSetup";
523 if (tif->tif_rawdata) {
524 if (tif->tif_flags & TIFF_MYBUFFER)
525 _TIFFfree(tif->tif_rawdata);
526 tif->tif_rawdata = NULL;
529 tif->tif_rawdatasize = size;
530 tif->tif_rawdata = (tidata_t) bp;
531 tif->tif_flags &= ~TIFF_MYBUFFER;
533 tif->tif_rawdatasize = TIFFroundup(size, 1024);
534 tif->tif_rawdata = (tidata_t) _TIFFmalloc(tif->tif_rawdatasize);
535 tif->tif_flags |= TIFF_MYBUFFER;
537 if (tif->tif_rawdata == NULL) {
539 "%s: No space for data buffer at scanline %ld",
540 tif->tif_name, (long) tif->tif_row);
541 tif->tif_rawdatasize = 0;
548 * Set state to appear as if a
549 * strip has just been read in.
552 TIFFStartStrip(TIFF* tif, tstrip_t strip)
554 TIFFDirectory *td = &tif->tif_dir;
556 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
557 if (!(*tif->tif_setupdecode)(tif))
559 tif->tif_flags |= TIFF_CODERSETUP;
561 tif->tif_curstrip = strip;
562 tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
563 tif->tif_rawcp = tif->tif_rawdata;
564 tif->tif_rawcc = td->td_stripbytecount[strip];
565 return ((*tif->tif_predecode)(tif,
566 (tsample_t)(strip / td->td_stripsperimage)));
570 * Set state to appear as if a
571 * tile has just been read in.
574 TIFFStartTile(TIFF* tif, ttile_t tile)
576 TIFFDirectory *td = &tif->tif_dir;
578 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
579 if (!(*tif->tif_setupdecode)(tif))
581 tif->tif_flags |= TIFF_CODERSETUP;
583 tif->tif_curtile = tile;
585 (tile % TIFFhowmany(td->td_imagewidth, td->td_tilewidth)) *
588 (tile % TIFFhowmany(td->td_imagelength, td->td_tilelength)) *
590 tif->tif_rawcp = tif->tif_rawdata;
591 tif->tif_rawcc = td->td_stripbytecount[tile];
592 return ((*tif->tif_predecode)(tif,
593 (tsample_t)(tile/td->td_stripsperimage)));
597 TIFFCheckRead(TIFF* tif, int tiles)
599 if (tif->tif_mode == O_WRONLY) {
600 TIFFError(tif->tif_name, "File not open for reading");
603 if (tiles ^ isTiled(tif)) {
604 TIFFError(tif->tif_name, tiles ?
605 "Can not read tiles from a stripped image" :
606 "Can not read scanlines from a tiled image");
613 _TIFFNoPostDecode(TIFF* tif, tidata_t buf, tsize_t cc)
615 (void) tif; (void) buf; (void) cc;
619 _TIFFSwab16BitData(TIFF* tif, tidata_t buf, tsize_t cc)
622 assert((cc & 1) == 0);
623 TIFFSwabArrayOfShort((uint16*) buf, cc/2);
627 _TIFFSwab32BitData(TIFF* tif, tidata_t buf, tsize_t cc)
630 assert((cc & 3) == 0);
631 TIFFSwabArrayOfLong((uint32*) buf, cc/4);
635 _TIFFSwab64BitData(TIFF* tif, tidata_t buf, tsize_t cc)
638 assert((cc & 7) == 0);
639 TIFFSwabArrayOfDouble((double*) buf, cc/8);
642 /* vim: set ts=8 sts=8 sw=8 noet: */