1
/* $Id: tif_read.c,v 1.16.2.3 2010-06-09 14:32:47 bfriesen 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
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
54
"%lu: Row out of range, max %lu",
56
(unsigned long) td->td_imagelength);
59
if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
60
if (sample >= td->td_samplesperpixel) {
61
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
62
"%lu: Sample out of range, max %lu",
63
(unsigned long) sample, (unsigned long) td->td_samplesperpixel);
66
strip = sample*td->td_stripsperimage + row/td->td_rowsperstrip;
68
strip = row / td->td_rowsperstrip;
69
if (strip != tif->tif_curstrip) { /* different strip, refill */
70
if (!TIFFFillStrip(tif, strip))
72
} else if (row < tif->tif_row) {
74
* Moving backwards within the same strip: backup
75
* to the start and then decode forward (below).
77
* NB: If you're planning on lots of random access within a
78
* strip, it's better to just read and decode the entire
79
* strip, and then access the decoded data in a random fashion.
81
if (!TIFFStartStrip(tif, strip))
84
if (row != tif->tif_row) {
86
* Seek forward to the desired row.
88
if (!(*tif->tif_seek)(tif, row - tif->tif_row))
96
TIFFReadScanline(TIFF* tif, tdata_t buf, uint32 row, tsample_t sample)
100
if (!TIFFCheckRead(tif, 0))
102
if( (e = TIFFSeek(tif, row, sample)) != 0) {
104
* Decompress desired row into user buffer.
106
e = (*tif->tif_decoderow)
107
(tif, (tidata_t) buf, tif->tif_scanlinesize, sample);
109
/* we are now poised at the beginning of the next row */
110
tif->tif_row = row + 1;
113
(*tif->tif_postdecode)(tif, (tidata_t) buf,
114
tif->tif_scanlinesize);
116
return (e > 0 ? 1 : -1);
120
* Read a strip of data and decompress the specified
121
* amount into the user-supplied buffer.
124
TIFFReadEncodedStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
126
TIFFDirectory *td = &tif->tif_dir;
129
tstrip_t sep_strip, strips_per_sep;
131
if (!TIFFCheckRead(tif, 0))
133
if (strip >= td->td_nstrips) {
134
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
135
"%ld: Strip out of range, max %ld",
136
(long) strip, (long) td->td_nstrips);
140
* Calculate the strip size according to the number of
141
* rows in the strip (check for truncated last strip on any
142
* of the separations).
144
if( td->td_rowsperstrip >= td->td_imagelength )
147
strips_per_sep = (td->td_imagelength+td->td_rowsperstrip-1)
148
/ td->td_rowsperstrip;
150
sep_strip = strip % strips_per_sep;
152
if (sep_strip != strips_per_sep-1 ||
153
(nrows = td->td_imagelength % td->td_rowsperstrip) == 0)
154
nrows = td->td_rowsperstrip;
156
stripsize = TIFFVStripSize(tif, nrows);
157
if (size == (tsize_t) -1)
159
else if (size > stripsize)
161
if (TIFFFillStrip(tif, strip)
162
&& (*tif->tif_decodestrip)(tif, (tidata_t) buf, size,
163
(tsample_t)(strip / td->td_stripsperimage)) > 0 ) {
164
(*tif->tif_postdecode)(tif, (tidata_t) buf, size);
167
return ((tsize_t) -1);
171
TIFFReadRawStrip1(TIFF* tif,
172
tstrip_t strip, tdata_t buf, tsize_t size, const char* module)
174
TIFFDirectory *td = &tif->tif_dir;
176
assert((tif->tif_flags&TIFF_NOREADRAW)==0);
177
if (!isMapped(tif)) {
180
if (!SeekOK(tif, td->td_stripoffset[strip])) {
181
TIFFErrorExt(tif->tif_clientdata, module,
182
"%s: Seek error at scanline %lu, strip %lu",
184
(unsigned long) tif->tif_row, (unsigned long) strip);
187
cc = TIFFReadFile(tif, buf, size);
189
TIFFErrorExt(tif->tif_clientdata, module,
190
"%s: Read error at scanline %lu; got %lu bytes, expected %lu",
192
(unsigned long) tif->tif_row,
194
(unsigned long) size);
198
if (td->td_stripoffset[strip] + size > tif->tif_size) {
199
TIFFErrorExt(tif->tif_clientdata, module,
200
"%s: Read error at scanline %lu, strip %lu; got %lu bytes, expected %lu",
202
(unsigned long) tif->tif_row,
203
(unsigned long) strip,
204
(unsigned long) tif->tif_size - td->td_stripoffset[strip],
205
(unsigned long) size);
208
_TIFFmemcpy(buf, tif->tif_base + td->td_stripoffset[strip],
215
* Read a strip of data from the file.
218
TIFFReadRawStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
220
static const char module[] = "TIFFReadRawStrip";
221
TIFFDirectory *td = &tif->tif_dir;
223
* FIXME: butecount should have tsize_t type, but for now libtiff
224
* defines tsize_t as a signed 32-bit integer and we are losing
225
* ability to read arrays larger than 2^31 bytes. So we are using
226
* uint32 instead of tsize_t here.
230
if (!TIFFCheckRead(tif, 0))
231
return ((tsize_t) -1);
232
if (strip >= td->td_nstrips) {
233
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
234
"%lu: Strip out of range, max %lu",
235
(unsigned long) strip,
236
(unsigned long) td->td_nstrips);
237
return ((tsize_t) -1);
239
if (tif->tif_flags&TIFF_NOREADRAW)
241
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
242
"Compression scheme does not support access to raw uncompressed data");
243
return ((tsize_t) -1);
245
bytecount = td->td_stripbytecount[strip];
246
if (bytecount <= 0) {
247
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
248
"%lu: Invalid strip byte count, strip %lu",
249
(unsigned long) bytecount, (unsigned long) strip);
250
return ((tsize_t) -1);
252
if (size != (tsize_t)-1 && (uint32)size < bytecount)
254
return (TIFFReadRawStrip1(tif, strip, buf, bytecount, module));
258
* Read the specified strip and setup for decoding. The data buffer is
259
* expanded, as necessary, to hold the strip's data.
262
TIFFFillStrip(TIFF* tif, tstrip_t strip)
264
static const char module[] = "TIFFFillStrip";
265
TIFFDirectory *td = &tif->tif_dir;
267
if ((tif->tif_flags&TIFF_NOREADRAW)==0)
270
* FIXME: butecount should have tsize_t type, but for now
271
* libtiff defines tsize_t as a signed 32-bit integer and we
272
* are losing ability to read arrays larger than 2^31 bytes.
273
* So we are using uint32 instead of tsize_t here.
275
uint32 bytecount = td->td_stripbytecount[strip];
276
if (bytecount <= 0) {
277
TIFFErrorExt(tif->tif_clientdata, module,
278
"%s: Invalid strip byte count %lu, strip %lu",
279
tif->tif_name, (unsigned long) bytecount,
280
(unsigned long) strip);
284
(isFillOrder(tif, td->td_fillorder)
285
|| (tif->tif_flags & TIFF_NOBITREV))) {
287
* The image is mapped into memory and we either don't
288
* need to flip bits or the compression routine is
289
* going to handle this operation itself. In this
290
* case, avoid copying the raw data and instead just
291
* reference the data from the memory mapped file
292
* image. This assumes that the decompression
293
* routines do not modify the contents of the raw data
294
* buffer (if they try to, the application will get a
295
* fault since the file is mapped read-only).
297
if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
298
_TIFFfree(tif->tif_rawdata);
299
tif->tif_flags &= ~TIFF_MYBUFFER;
301
* We must check for overflow, potentially causing
302
* an OOB read. Instead of simple
304
* td->td_stripoffset[strip]+bytecount > tif->tif_size
306
* comparison (which can overflow) we do the following
309
if (bytecount > tif->tif_size ||
310
td->td_stripoffset[strip] > tif->tif_size - bytecount) {
312
* This error message might seem strange, but
313
* it's what would happen if a read were done
316
TIFFErrorExt(tif->tif_clientdata, module,
318
"%s: Read error on strip %lu; "
319
"got %lu bytes, expected %lu",
320
tif->tif_name, (unsigned long) strip,
321
(unsigned long) tif->tif_size - td->td_stripoffset[strip],
322
(unsigned long) bytecount);
323
tif->tif_curstrip = NOSTRIP;
326
tif->tif_rawdatasize = bytecount;
327
tif->tif_rawdata = tif->tif_base + td->td_stripoffset[strip];
330
* Expand raw data buffer, if needed, to hold data
331
* strip coming from file (perhaps should set upper
332
* bound on the size of a buffer we'll use?).
334
if (bytecount > (uint32)tif->tif_rawdatasize) {
335
tif->tif_curstrip = NOSTRIP;
336
if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
337
TIFFErrorExt(tif->tif_clientdata,
339
"%s: Data buffer too small to hold strip %lu",
341
(unsigned long) strip);
344
if (!TIFFReadBufferSetup(tif, 0,
345
TIFFroundup(bytecount, 1024)))
348
if ((uint32)TIFFReadRawStrip1(tif, strip,
349
(unsigned char *)tif->tif_rawdata,
350
bytecount, module) != bytecount)
352
if (!isFillOrder(tif, td->td_fillorder) &&
353
(tif->tif_flags & TIFF_NOBITREV) == 0)
354
TIFFReverseBits(tif->tif_rawdata, bytecount);
357
return (TIFFStartStrip(tif, strip));
361
* Tile-oriented Read Support
362
* Contributed by Nancy Cam (Silicon Graphics).
366
* Read and decompress a tile of data. The
367
* tile is selected by the (x,y,z,s) coordinates.
370
TIFFReadTile(TIFF* tif,
371
tdata_t buf, uint32 x, uint32 y, uint32 z, tsample_t s)
373
if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
375
return (TIFFReadEncodedTile(tif,
376
TIFFComputeTile(tif, x, y, z, s), buf, (tsize_t) -1));
380
* Read a tile of data and decompress the specified
381
* amount into the user-supplied buffer.
384
TIFFReadEncodedTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
386
TIFFDirectory *td = &tif->tif_dir;
387
tsize_t tilesize = tif->tif_tilesize;
389
if (!TIFFCheckRead(tif, 1))
391
if (tile >= td->td_nstrips) {
392
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
393
"%ld: Tile out of range, max %ld",
394
(long) tile, (unsigned long) td->td_nstrips);
397
if (size == (tsize_t) -1)
399
else if (size > tilesize)
401
if (TIFFFillTile(tif, tile) && (*tif->tif_decodetile)(tif,
402
(tidata_t) buf, size, (tsample_t)(tile/td->td_stripsperimage))) {
403
(*tif->tif_postdecode)(tif, (tidata_t) buf, size);
410
TIFFReadRawTile1(TIFF* tif,
411
ttile_t tile, tdata_t buf, tsize_t size, const char* module)
413
TIFFDirectory *td = &tif->tif_dir;
415
assert((tif->tif_flags&TIFF_NOREADRAW)==0);
416
if (!isMapped(tif)) {
419
if (!SeekOK(tif, td->td_stripoffset[tile])) {
420
TIFFErrorExt(tif->tif_clientdata, module,
421
"%s: Seek error at row %ld, col %ld, tile %ld",
426
return ((tsize_t) -1);
428
cc = TIFFReadFile(tif, buf, size);
430
TIFFErrorExt(tif->tif_clientdata, module,
431
"%s: Read error at row %ld, col %ld; got %lu bytes, expected %lu",
436
(unsigned long) size);
437
return ((tsize_t) -1);
440
if (td->td_stripoffset[tile] + size > tif->tif_size) {
441
TIFFErrorExt(tif->tif_clientdata, module,
442
"%s: Read error at row %ld, col %ld, tile %ld; got %lu bytes, expected %lu",
447
(unsigned long) tif->tif_size - td->td_stripoffset[tile],
448
(unsigned long) size);
449
return ((tsize_t) -1);
451
_TIFFmemcpy(buf, tif->tif_base + td->td_stripoffset[tile], size);
457
* Read a tile of data from the file.
460
TIFFReadRawTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
462
static const char module[] = "TIFFReadRawTile";
463
TIFFDirectory *td = &tif->tif_dir;
465
* FIXME: butecount should have tsize_t type, but for now libtiff
466
* defines tsize_t as a signed 32-bit integer and we are losing
467
* ability to read arrays larger than 2^31 bytes. So we are using
468
* uint32 instead of tsize_t here.
472
if (!TIFFCheckRead(tif, 1))
473
return ((tsize_t) -1);
474
if (tile >= td->td_nstrips) {
475
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
476
"%lu: Tile out of range, max %lu",
477
(unsigned long) tile, (unsigned long) td->td_nstrips);
478
return ((tsize_t) -1);
480
if (tif->tif_flags&TIFF_NOREADRAW)
482
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
483
"Compression scheme does not support access to raw uncompressed data");
484
return ((tsize_t) -1);
486
bytecount = td->td_stripbytecount[tile];
487
if (size != (tsize_t) -1 && (uint32)size < bytecount)
489
return (TIFFReadRawTile1(tif, tile, buf, bytecount, module));
493
* Read the specified tile and setup for decoding. The data buffer is
494
* expanded, as necessary, to hold the tile's data.
497
TIFFFillTile(TIFF* tif, ttile_t tile)
499
static const char module[] = "TIFFFillTile";
500
TIFFDirectory *td = &tif->tif_dir;
502
if ((tif->tif_flags&TIFF_NOREADRAW)==0)
505
* FIXME: butecount should have tsize_t type, but for now
506
* libtiff defines tsize_t as a signed 32-bit integer and we
507
* are losing ability to read arrays larger than 2^31 bytes.
508
* So we are using uint32 instead of tsize_t here.
510
uint32 bytecount = td->td_stripbytecount[tile];
511
if (bytecount <= 0) {
512
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
513
"%lu: Invalid tile byte count, tile %lu",
514
(unsigned long) bytecount, (unsigned long) tile);
518
(isFillOrder(tif, td->td_fillorder)
519
|| (tif->tif_flags & TIFF_NOBITREV))) {
521
* The image is mapped into memory and we either don't
522
* need to flip bits or the compression routine is
523
* going to handle this operation itself. In this
524
* case, avoid copying the raw data and instead just
525
* reference the data from the memory mapped file
526
* image. This assumes that the decompression
527
* routines do not modify the contents of the raw data
528
* buffer (if they try to, the application will get a
529
* fault since the file is mapped read-only).
531
if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
532
_TIFFfree(tif->tif_rawdata);
533
tif->tif_flags &= ~TIFF_MYBUFFER;
535
* We must check for overflow, potentially causing
536
* an OOB read. Instead of simple
538
* td->td_stripoffset[tile]+bytecount > tif->tif_size
540
* comparison (which can overflow) we do the following
543
if (bytecount > tif->tif_size ||
544
td->td_stripoffset[tile] > tif->tif_size - bytecount) {
545
tif->tif_curtile = NOTILE;
548
tif->tif_rawdatasize = bytecount;
550
tif->tif_base + td->td_stripoffset[tile];
553
* Expand raw data buffer, if needed, to hold data
554
* tile coming from file (perhaps should set upper
555
* bound on the size of a buffer we'll use?).
557
if (bytecount > (uint32)tif->tif_rawdatasize) {
558
tif->tif_curtile = NOTILE;
559
if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
560
TIFFErrorExt(tif->tif_clientdata,
562
"%s: Data buffer too small to hold tile %ld",
567
if (!TIFFReadBufferSetup(tif, 0,
568
TIFFroundup(bytecount, 1024)))
571
if ((uint32)TIFFReadRawTile1(tif, tile,
572
(unsigned char *)tif->tif_rawdata,
573
bytecount, module) != bytecount)
575
if (!isFillOrder(tif, td->td_fillorder) &&
576
(tif->tif_flags & TIFF_NOBITREV) == 0)
577
TIFFReverseBits(tif->tif_rawdata, bytecount);
580
return (TIFFStartTile(tif, tile));
584
* Setup the raw data buffer in preparation for
585
* reading a strip of raw data. If the buffer
586
* is specified as zero, then a buffer of appropriate
587
* size is allocated by the library. Otherwise,
588
* the client must guarantee that the buffer is
589
* large enough to hold any individual strip of
593
TIFFReadBufferSetup(TIFF* tif, tdata_t bp, tsize_t size)
595
static const char module[] = "TIFFReadBufferSetup";
597
assert((tif->tif_flags&TIFF_NOREADRAW)==0);
598
if (tif->tif_rawdata) {
599
if (tif->tif_flags & TIFF_MYBUFFER)
600
_TIFFfree(tif->tif_rawdata);
601
tif->tif_rawdata = NULL;
605
tif->tif_rawdatasize = size;
606
tif->tif_rawdata = (tidata_t) bp;
607
tif->tif_flags &= ~TIFF_MYBUFFER;
609
tif->tif_rawdatasize = TIFFroundup(size, 1024);
610
if (tif->tif_rawdatasize > 0)
611
tif->tif_rawdata = (tidata_t) _TIFFmalloc(tif->tif_rawdatasize);
612
tif->tif_flags |= TIFF_MYBUFFER;
614
if ((tif->tif_rawdata == NULL) || (tif->tif_rawdatasize == 0)) {
615
TIFFErrorExt(tif->tif_clientdata, module,
616
"%s: No space for data buffer at scanline %ld",
617
tif->tif_name, (long) tif->tif_row);
618
tif->tif_rawdatasize = 0;
625
* Set state to appear as if a
626
* strip has just been read in.
629
TIFFStartStrip(TIFF* tif, tstrip_t strip)
631
TIFFDirectory *td = &tif->tif_dir;
633
if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
634
if (!(*tif->tif_setupdecode)(tif))
636
tif->tif_flags |= TIFF_CODERSETUP;
638
tif->tif_curstrip = strip;
639
tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
640
if (tif->tif_flags&TIFF_NOREADRAW)
642
tif->tif_rawcp = NULL;
647
tif->tif_rawcp = tif->tif_rawdata;
648
tif->tif_rawcc = td->td_stripbytecount[strip];
650
return ((*tif->tif_predecode)(tif,
651
(tsample_t)(strip / td->td_stripsperimage)));
655
* Set state to appear as if a
656
* tile has just been read in.
659
TIFFStartTile(TIFF* tif, ttile_t tile)
661
TIFFDirectory *td = &tif->tif_dir;
663
if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
664
if (!(*tif->tif_setupdecode)(tif))
666
tif->tif_flags |= TIFF_CODERSETUP;
668
tif->tif_curtile = tile;
670
(tile % TIFFhowmany(td->td_imagewidth, td->td_tilewidth)) *
673
(tile % TIFFhowmany(td->td_imagelength, td->td_tilelength)) *
675
if (tif->tif_flags&TIFF_NOREADRAW)
677
tif->tif_rawcp = NULL;
682
tif->tif_rawcp = tif->tif_rawdata;
683
tif->tif_rawcc = td->td_stripbytecount[tile];
685
return ((*tif->tif_predecode)(tif,
686
(tsample_t)(tile/td->td_stripsperimage)));
690
TIFFCheckRead(TIFF* tif, int tiles)
692
if (tif->tif_mode == O_WRONLY) {
693
TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "File not open for reading");
696
if (tiles ^ isTiled(tif)) {
697
TIFFErrorExt(tif->tif_clientdata, tif->tif_name, tiles ?
698
"Can not read tiles from a stripped image" :
699
"Can not read scanlines from a tiled image");
706
_TIFFNoPostDecode(TIFF* tif, tidata_t buf, tsize_t cc)
708
(void) tif; (void) buf; (void) cc;
712
_TIFFSwab16BitData(TIFF* tif, tidata_t buf, tsize_t cc)
715
assert((cc & 1) == 0);
716
TIFFSwabArrayOfShort((uint16*) buf, cc/2);
720
_TIFFSwab24BitData(TIFF* tif, tidata_t buf, tsize_t cc)
723
assert((cc % 3) == 0);
724
TIFFSwabArrayOfTriples((uint8*) buf, cc/3);
728
_TIFFSwab32BitData(TIFF* tif, tidata_t buf, tsize_t cc)
731
assert((cc & 3) == 0);
732
TIFFSwabArrayOfLong((uint32*) buf, cc/4);
736
_TIFFSwab64BitData(TIFF* tif, tidata_t buf, tsize_t cc)
739
assert((cc & 7) == 0);
740
TIFFSwabArrayOfDouble((double*) buf, cc/8);
743
/* vim: set ts=8 sts=8 sw=8 noet: */