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
35 static int TIFFFillStrip(TIFF
*, tstrip_t
);
36 static int TIFFFillTile(TIFF
*, ttile_t
);
37 static int TIFFStartStrip(TIFF
*, tstrip_t
);
38 static int TIFFStartTile(TIFF
*, ttile_t
);
39 static int TIFFCheckRead(TIFF
*, int);
41 #define NOSTRIP ((tstrip_t) -1) /* undefined state */
42 #define NOTILE ((ttile_t) -1) /* undefined state */
45 * Seek to a random row+sample in a file.
48 TIFFSeek(TIFF
* tif
, uint32 row
, tsample_t sample
)
50 register TIFFDirectory
*td
= &tif
->tif_dir
;
53 if (row
>= td
->td_imagelength
) { /* out of range */
54 TIFFError(tif
->tif_name
, "%lu: Row out of range, max %lu",
55 (u_long
) row
, (u_long
) td
->td_imagelength
);
58 if (td
->td_planarconfig
== PLANARCONFIG_SEPARATE
) {
59 if (sample
>= td
->td_samplesperpixel
) {
60 TIFFError(tif
->tif_name
,
61 "%lu: Sample out of range, max %lu",
62 (u_long
) sample
, (u_long
) td
->td_samplesperpixel
);
65 strip
= sample
*td
->td_stripsperimage
+ row
/td
->td_rowsperstrip
;
67 strip
= row
/ td
->td_rowsperstrip
;
68 if (strip
!= tif
->tif_curstrip
) { /* different strip, refill */
69 if (!TIFFFillStrip(tif
, strip
))
71 } else if (row
< tif
->tif_row
) {
73 * Moving backwards within the same strip: backup
74 * to the start and then decode forward (below).
76 * NB: If you're planning on lots of random access within a
77 * strip, it's better to just read and decode the entire
78 * strip, and then access the decoded data in a random fashion.
80 if (!TIFFStartStrip(tif
, strip
))
83 if (row
!= tif
->tif_row
) {
85 * Seek forward to the desired row.
87 if (!(*tif
->tif_seek
)(tif
, row
- tif
->tif_row
))
95 TIFFReadScanline(TIFF
* tif
, tdata_t buf
, uint32 row
, tsample_t sample
)
99 if (!TIFFCheckRead(tif
, 0))
101 if( (e
= TIFFSeek(tif
, row
, sample
)) != 0) {
103 * Decompress desired row into user buffer.
105 e
= (*tif
->tif_decoderow
)
106 (tif
, (tidata_t
) buf
, tif
->tif_scanlinesize
, sample
);
109 (*tif
->tif_postdecode
)(tif
, (tidata_t
) buf
,
110 tif
->tif_scanlinesize
);
112 return (e
> 0 ? 1 : -1);
116 * Read a strip of data and decompress the specified
117 * amount into the user-supplied buffer.
120 TIFFReadEncodedStrip(TIFF
* tif
, tstrip_t strip
, tdata_t buf
, tsize_t size
)
122 TIFFDirectory
*td
= &tif
->tif_dir
;
126 if (!TIFFCheckRead(tif
, 0))
128 if (strip
>= td
->td_nstrips
) {
129 TIFFError(tif
->tif_name
, "%ld: Strip out of range, max %ld",
130 (long) strip
, (long) td
->td_nstrips
);
134 * Calculate the strip size according to the number of
135 * rows in the strip (check for truncated last strip).
137 if (strip
!= td
->td_nstrips
-1 ||
138 (nrows
= td
->td_imagelength
% td
->td_rowsperstrip
) == 0)
139 nrows
= td
->td_rowsperstrip
;
140 stripsize
= TIFFVStripSize(tif
, nrows
);
141 if (size
== (tsize_t
) -1)
143 else if (size
> stripsize
)
145 if (TIFFFillStrip(tif
, strip
) && (*tif
->tif_decodestrip
)(tif
,
146 (tidata_t
) buf
, size
, (tsample_t
)(strip
/ td
->td_stripsperimage
))) {
147 (*tif
->tif_postdecode
)(tif
, (tidata_t
) buf
, size
);
150 return ((tsize_t
) -1);
154 TIFFReadRawStrip1(TIFF
* tif
,
155 tstrip_t strip
, tdata_t buf
, tsize_t size
, const char* module)
157 TIFFDirectory
*td
= &tif
->tif_dir
;
159 if (!isMapped(tif
)) {
162 if (!SeekOK(tif
, td
->td_stripoffset
[strip
])) {
164 "%s: Seek error at scanline %lu, strip %lu",
166 (u_long
) tif
->tif_row
, (u_long
) strip
);
169 cc
= TIFFReadFile(tif
, buf
, size
);
172 "%s: Read error at scanline %lu; got %lu bytes, expected %lu",
174 (u_long
) tif
->tif_row
,
180 if (td
->td_stripoffset
[strip
] + size
> tif
->tif_size
) {
182 "%s: Read error at scanline %lu, strip %lu; got %lu bytes, expected %lu",
184 (u_long
) tif
->tif_row
,
186 (u_long
) tif
->tif_size
- td
->td_stripoffset
[strip
],
190 _TIFFmemcpy(buf
, tif
->tif_base
+ td
->td_stripoffset
[strip
], size
);
196 * Read a strip of data from the file.
199 TIFFReadRawStrip(TIFF
* tif
, tstrip_t strip
, tdata_t buf
, tsize_t size
)
201 static const char module[] = "TIFFReadRawStrip";
202 TIFFDirectory
*td
= &tif
->tif_dir
;
205 if (!TIFFCheckRead(tif
, 0))
206 return ((tsize_t
) -1);
207 if (strip
>= td
->td_nstrips
) {
208 TIFFError(tif
->tif_name
, "%lu: Strip out of range, max %lu",
209 (u_long
) strip
, (u_long
) td
->td_nstrips
);
210 return ((tsize_t
) -1);
212 bytecount
= td
->td_stripbytecount
[strip
];
213 if (bytecount
<= 0) {
214 TIFFError(tif
->tif_name
,
215 "%lu: Invalid strip byte count, strip %lu",
216 (u_long
) bytecount
, (u_long
) strip
);
217 return ((tsize_t
) -1);
219 if (size
!= (tsize_t
)-1 && size
< bytecount
)
221 return (TIFFReadRawStrip1(tif
, strip
, buf
, bytecount
, module));
225 * Read the specified strip and setup for decoding.
226 * The data buffer is expanded, as necessary, to
227 * hold the strip's data.
230 TIFFFillStrip(TIFF
* tif
, tstrip_t strip
)
232 static const char module[] = "TIFFFillStrip";
233 TIFFDirectory
*td
= &tif
->tif_dir
;
236 bytecount
= td
->td_stripbytecount
[strip
];
237 if (bytecount
<= 0) {
238 TIFFError(tif
->tif_name
,
239 "%lu: Invalid strip byte count, strip %lu",
240 (u_long
) bytecount
, (u_long
) strip
);
244 (isFillOrder(tif
, td
->td_fillorder
) || (tif
->tif_flags
& TIFF_NOBITREV
))) {
246 * The image is mapped into memory and we either don't
247 * need to flip bits or the compression routine is going
248 * to handle this operation itself. In this case, avoid
249 * copying the raw data and instead just reference the
250 * data from the memory mapped file image. This assumes
251 * that the decompression routines do not modify the
252 * contents of the raw data buffer (if they try to,
253 * the application will get a fault since the file is
256 if ((tif
->tif_flags
& TIFF_MYBUFFER
) && tif
->tif_rawdata
)
257 _TIFFfree(tif
->tif_rawdata
);
258 tif
->tif_flags
&= ~TIFF_MYBUFFER
;
259 if (td
->td_stripoffset
[strip
] + bytecount
> tif
->tif_size
) {
261 * This error message might seem strange, but it's
262 * what would happen if a read were done instead.
265 "%s: Read error on strip %lu; got %lu bytes, expected %lu",
268 (u_long
) tif
->tif_size
- td
->td_stripoffset
[strip
],
270 tif
->tif_curstrip
= NOSTRIP
;
273 tif
->tif_rawdatasize
= bytecount
;
274 tif
->tif_rawdata
= tif
->tif_base
+ td
->td_stripoffset
[strip
];
277 * Expand raw data buffer, if needed, to
278 * hold data strip coming from file
279 * (perhaps should set upper bound on
280 * the size of a buffer we'll use?).
282 if (bytecount
> tif
->tif_rawdatasize
) {
283 tif
->tif_curstrip
= NOSTRIP
;
284 if ((tif
->tif_flags
& TIFF_MYBUFFER
) == 0) {
286 "%s: Data buffer too small to hold strip %lu",
287 tif
->tif_name
, (u_long
) strip
);
290 if (!TIFFReadBufferSetup(tif
, 0,
291 TIFFroundup(bytecount
, 1024)))
294 if (TIFFReadRawStrip1(tif
, strip
, (u_char
*)tif
->tif_rawdata
,
295 bytecount
, module) != bytecount
)
297 if (!isFillOrder(tif
, td
->td_fillorder
) &&
298 (tif
->tif_flags
& TIFF_NOBITREV
) == 0)
299 TIFFReverseBits(tif
->tif_rawdata
, bytecount
);
301 return (TIFFStartStrip(tif
, strip
));
305 * Tile-oriented Read Support
306 * Contributed by Nancy Cam (Silicon Graphics).
310 * Read and decompress a tile of data. The
311 * tile is selected by the (x,y,z,s) coordinates.
314 TIFFReadTile(TIFF
* tif
,
315 tdata_t buf
, uint32 x
, uint32 y
, uint32 z
, tsample_t s
)
317 if (!TIFFCheckRead(tif
, 1) || !TIFFCheckTile(tif
, x
, y
, z
, s
))
319 return (TIFFReadEncodedTile(tif
,
320 TIFFComputeTile(tif
, x
, y
, z
, s
), buf
, (tsize_t
) -1));
324 * Read a tile of data and decompress the specified
325 * amount into the user-supplied buffer.
328 TIFFReadEncodedTile(TIFF
* tif
, ttile_t tile
, tdata_t buf
, tsize_t size
)
330 TIFFDirectory
*td
= &tif
->tif_dir
;
331 tsize_t tilesize
= tif
->tif_tilesize
;
333 if (!TIFFCheckRead(tif
, 1))
335 if (tile
>= td
->td_nstrips
) {
336 TIFFError(tif
->tif_name
, "%ld: Tile out of range, max %ld",
337 (long) tile
, (u_long
) td
->td_nstrips
);
340 if (size
== (tsize_t
) -1)
342 else if (size
> tilesize
)
344 if (TIFFFillTile(tif
, tile
) && (*tif
->tif_decodetile
)(tif
,
345 (tidata_t
) buf
, size
, (tsample_t
)(tile
/td
->td_stripsperimage
))) {
346 (*tif
->tif_postdecode
)(tif
, (tidata_t
) buf
, size
);
353 TIFFReadRawTile1(TIFF
* tif
,
354 ttile_t tile
, tdata_t buf
, tsize_t size
, const char* module)
356 TIFFDirectory
*td
= &tif
->tif_dir
;
358 if (!isMapped(tif
)) {
361 if (!SeekOK(tif
, td
->td_stripoffset
[tile
])) {
363 "%s: Seek error at row %ld, col %ld, tile %ld",
368 return ((tsize_t
) -1);
370 cc
= TIFFReadFile(tif
, buf
, size
);
373 "%s: Read error at row %ld, col %ld; got %lu bytes, expected %lu",
379 return ((tsize_t
) -1);
382 if (td
->td_stripoffset
[tile
] + size
> tif
->tif_size
) {
384 "%s: Read error at row %ld, col %ld, tile %ld; got %lu bytes, expected %lu",
389 (u_long
) tif
->tif_size
- td
->td_stripoffset
[tile
],
391 return ((tsize_t
) -1);
393 _TIFFmemcpy(buf
, tif
->tif_base
+ td
->td_stripoffset
[tile
], size
);
399 * Read a tile of data from the file.
402 TIFFReadRawTile(TIFF
* tif
, ttile_t tile
, tdata_t buf
, tsize_t size
)
404 static const char module[] = "TIFFReadRawTile";
405 TIFFDirectory
*td
= &tif
->tif_dir
;
408 if (!TIFFCheckRead(tif
, 1))
409 return ((tsize_t
) -1);
410 if (tile
>= td
->td_nstrips
) {
411 TIFFError(tif
->tif_name
, "%lu: Tile out of range, max %lu",
412 (u_long
) tile
, (u_long
) td
->td_nstrips
);
413 return ((tsize_t
) -1);
415 bytecount
= td
->td_stripbytecount
[tile
];
416 if (size
!= (tsize_t
) -1 && size
< bytecount
)
418 return (TIFFReadRawTile1(tif
, tile
, buf
, bytecount
, module));
422 * Read the specified tile and setup for decoding.
423 * The data buffer is expanded, as necessary, to
424 * hold the tile's data.
427 TIFFFillTile(TIFF
* tif
, ttile_t tile
)
429 static const char module[] = "TIFFFillTile";
430 TIFFDirectory
*td
= &tif
->tif_dir
;
433 bytecount
= td
->td_stripbytecount
[tile
];
434 if (bytecount
<= 0) {
435 TIFFError(tif
->tif_name
,
436 "%lu: Invalid tile byte count, tile %lu",
437 (u_long
) bytecount
, (u_long
) tile
);
441 (isFillOrder(tif
, td
->td_fillorder
) || (tif
->tif_flags
& TIFF_NOBITREV
))) {
443 * The image is mapped into memory and we either don't
444 * need to flip bits or the compression routine is going
445 * to handle this operation itself. In this case, avoid
446 * copying the raw data and instead just reference the
447 * data from the memory mapped file image. This assumes
448 * that the decompression routines do not modify the
449 * contents of the raw data buffer (if they try to,
450 * the application will get a fault since the file is
453 if ((tif
->tif_flags
& TIFF_MYBUFFER
) && tif
->tif_rawdata
)
454 _TIFFfree(tif
->tif_rawdata
);
455 tif
->tif_flags
&= ~TIFF_MYBUFFER
;
456 if (td
->td_stripoffset
[tile
] + bytecount
> tif
->tif_size
) {
457 tif
->tif_curtile
= NOTILE
;
460 tif
->tif_rawdatasize
= bytecount
;
461 tif
->tif_rawdata
= tif
->tif_base
+ td
->td_stripoffset
[tile
];
464 * Expand raw data buffer, if needed, to
465 * hold data tile coming from file
466 * (perhaps should set upper bound on
467 * the size of a buffer we'll use?).
469 if (bytecount
> tif
->tif_rawdatasize
) {
470 tif
->tif_curtile
= NOTILE
;
471 if ((tif
->tif_flags
& TIFF_MYBUFFER
) == 0) {
473 "%s: Data buffer too small to hold tile %ld",
474 tif
->tif_name
, (long) tile
);
477 if (!TIFFReadBufferSetup(tif
, 0,
478 TIFFroundup(bytecount
, 1024)))
481 if (TIFFReadRawTile1(tif
, tile
, (u_char
*)tif
->tif_rawdata
,
482 bytecount
, module) != bytecount
)
484 if (!isFillOrder(tif
, td
->td_fillorder
) &&
485 (tif
->tif_flags
& TIFF_NOBITREV
) == 0)
486 TIFFReverseBits(tif
->tif_rawdata
, bytecount
);
488 return (TIFFStartTile(tif
, tile
));
492 * Setup the raw data buffer in preparation for
493 * reading a strip of raw data. If the buffer
494 * is specified as zero, then a buffer of appropriate
495 * size is allocated by the library. Otherwise,
496 * the client must guarantee that the buffer is
497 * large enough to hold any individual strip of
501 TIFFReadBufferSetup(TIFF
* tif
, tdata_t bp
, tsize_t size
)
503 static const char module[] = "TIFFReadBufferSetup";
505 if (tif
->tif_rawdata
) {
506 if (tif
->tif_flags
& TIFF_MYBUFFER
)
507 _TIFFfree(tif
->tif_rawdata
);
508 tif
->tif_rawdata
= NULL
;
511 tif
->tif_rawdatasize
= size
;
512 tif
->tif_rawdata
= (tidata_t
) bp
;
513 tif
->tif_flags
&= ~TIFF_MYBUFFER
;
515 tif
->tif_rawdatasize
= TIFFroundup(size
, 1024);
516 tif
->tif_rawdata
= (tidata_t
) _TIFFmalloc(tif
->tif_rawdatasize
);
517 tif
->tif_flags
|= TIFF_MYBUFFER
;
519 if (tif
->tif_rawdata
== NULL
) {
521 "%s: No space for data buffer at scanline %ld",
522 tif
->tif_name
, (long) tif
->tif_row
);
523 tif
->tif_rawdatasize
= 0;
530 * Set state to appear as if a
531 * strip has just been read in.
534 TIFFStartStrip(TIFF
* tif
, tstrip_t strip
)
536 TIFFDirectory
*td
= &tif
->tif_dir
;
538 if ((tif
->tif_flags
& TIFF_CODERSETUP
) == 0) {
539 if (!(*tif
->tif_setupdecode
)(tif
))
541 tif
->tif_flags
|= TIFF_CODERSETUP
;
543 tif
->tif_curstrip
= strip
;
544 tif
->tif_row
= (strip
% td
->td_stripsperimage
) * td
->td_rowsperstrip
;
545 tif
->tif_rawcp
= tif
->tif_rawdata
;
546 tif
->tif_rawcc
= td
->td_stripbytecount
[strip
];
547 return ((*tif
->tif_predecode
)(tif
,
548 (tsample_t
)(strip
/ td
->td_stripsperimage
)));
552 * Set state to appear as if a
553 * tile has just been read in.
556 TIFFStartTile(TIFF
* tif
, ttile_t tile
)
558 TIFFDirectory
*td
= &tif
->tif_dir
;
560 if ((tif
->tif_flags
& TIFF_CODERSETUP
) == 0) {
561 if (!(*tif
->tif_setupdecode
)(tif
))
563 tif
->tif_flags
|= TIFF_CODERSETUP
;
565 tif
->tif_curtile
= tile
;
567 (tile
% TIFFhowmany(td
->td_imagewidth
, td
->td_tilewidth
)) *
570 (tile
% TIFFhowmany(td
->td_imagelength
, td
->td_tilelength
)) *
572 tif
->tif_rawcp
= tif
->tif_rawdata
;
573 tif
->tif_rawcc
= td
->td_stripbytecount
[tile
];
574 return ((*tif
->tif_predecode
)(tif
,
575 (tsample_t
)(tile
/td
->td_stripsperimage
)));
579 TIFFCheckRead(TIFF
* tif
, int tiles
)
581 if (tif
->tif_mode
== O_WRONLY
) {
582 TIFFError(tif
->tif_name
, "File not open for reading");
585 if (tiles
^ isTiled(tif
)) {
586 TIFFError(tif
->tif_name
, tiles
?
587 "Can not read tiles from a stripped image" :
588 "Can not read scanlines from a tiled image");
595 _TIFFNoPostDecode(TIFF
* tif
, tidata_t buf
, tsize_t cc
)
597 (void) tif
; (void) buf
; (void) cc
;
601 _TIFFSwab16BitData(TIFF
* tif
, tidata_t buf
, tsize_t cc
)
604 assert((cc
& 1) == 0);
605 TIFFSwabArrayOfShort((uint16
*) buf
, cc
/2);
609 _TIFFSwab32BitData(TIFF
* tif
, tidata_t buf
, tsize_t cc
)
612 assert((cc
& 3) == 0);
613 TIFFSwabArrayOfLong((uint32
*) buf
, cc
/4);
617 _TIFFSwab64BitData(TIFF
* tif
, tidata_t buf
, tsize_t cc
)
620 assert((cc
& 7) == 0);
621 TIFFSwabArrayOfDouble((double*) buf
, cc
/8);