1 /* $Id: tif_read.c,v 1.38 2011-12-09 03:29:10 fwarmerdam 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* tif, uint32 strip);
35 int TIFFFillTile(TIFF* tif, uint32 tile);
36 static int TIFFStartStrip(TIFF* tif, uint32 strip);
37 static int TIFFStartTile(TIFF* tif, uint32 tile);
38 static int TIFFCheckRead(TIFF*, int);
40 TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,const char* module);
42 #define NOSTRIP ((uint32)(-1)) /* undefined state */
43 #define NOTILE ((uint32)(-1)) /* undefined state */
46 TIFFFillStripPartial( TIFF *tif, int strip, tmsize_t read_ahead, int restart )
48 static const char module[] = "TIFFFillStripPartial";
49 register TIFFDirectory *td = &tif->tif_dir;
55 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
59 * Expand raw data buffer, if needed, to hold data
60 * strip coming from file (perhaps should set upper
61 * bound on the size of a buffer we'll use?).
64 bytecountm=(tmsize_t) td->td_stripbytecount[strip];
65 if (read_ahead*2 > tif->tif_rawdatasize) {
68 tif->tif_curstrip = NOSTRIP;
69 if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
70 TIFFErrorExt(tif->tif_clientdata, module,
71 "Data buffer too small to hold part of strip %lu",
72 (unsigned long) strip);
75 if (!TIFFReadBufferSetup(tif, 0, read_ahead*2))
81 tif->tif_rawdataloaded = 0;
82 tif->tif_rawdataoff = 0;
86 ** If we are reading more data, move any unused data to the
87 ** start of the buffer.
89 if( tif->tif_rawdataloaded > 0 )
90 unused_data = tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata);
96 memmove( tif->tif_rawdata, tif->tif_rawcp, unused_data );
100 ** Seek to the point in the file where more data should be read.
102 read_offset = td->td_stripoffset[strip]
103 + tif->tif_rawdataoff + tif->tif_rawdataloaded;
105 if (!SeekOK(tif, read_offset)) {
106 TIFFErrorExt(tif->tif_clientdata, module,
107 "Seek error at scanline %lu, strip %lu",
108 (unsigned long) tif->tif_row, (unsigned long) strip);
113 ** How much do we want to read?
115 to_read = tif->tif_rawdatasize - unused_data;
116 if( (uint64) to_read > td->td_stripbytecount[strip]
117 - tif->tif_rawdataoff - tif->tif_rawdataloaded )
119 to_read = td->td_stripbytecount[strip]
120 - tif->tif_rawdataoff - tif->tif_rawdataloaded;
123 cc = TIFFReadFile(tif, tif->tif_rawdata + unused_data, to_read);
126 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
127 TIFFErrorExt(tif->tif_clientdata, module,
128 "Read error at scanline %lu; got %I64u bytes, expected %I64u",
129 (unsigned long) tif->tif_row,
130 (unsigned __int64) cc,
131 (unsigned __int64) to_read);
133 TIFFErrorExt(tif->tif_clientdata, module,
134 "Read error at scanline %lu; got %llu bytes, expected %llu",
135 (unsigned long) tif->tif_row,
136 (unsigned long long) cc,
137 (unsigned long long) to_read);
142 tif->tif_rawdataoff = tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data ;
143 tif->tif_rawdataloaded = unused_data + to_read;
145 tif->tif_rawcp = tif->tif_rawdata;
147 if (!isFillOrder(tif, td->td_fillorder) &&
148 (tif->tif_flags & TIFF_NOBITREV) == 0)
149 TIFFReverseBits(tif->tif_rawdata + unused_data, to_read );
152 ** When starting a strip from the beginning we need to
153 ** restart the decoder.
156 return TIFFStartStrip(tif, strip);
162 * Seek to a random row+sample in a file.
164 * Only used by TIFFReadScanline, and is only used on
165 * strip organized files. We do some tricky stuff to try
166 * and avoid reading the whole compressed raw data for big
170 TIFFSeek(TIFF* tif, uint32 row, uint16 sample )
172 register TIFFDirectory *td = &tif->tif_dir;
175 tmsize_t read_ahead = 0;
178 ** Establish what strip we are working from.
180 if (row >= td->td_imagelength) { /* out of range */
181 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
182 "%lu: Row out of range, max %lu",
184 (unsigned long) td->td_imagelength);
187 if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
188 if (sample >= td->td_samplesperpixel) {
189 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
190 "%lu: Sample out of range, max %lu",
191 (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
194 strip = (uint32)sample*td->td_stripsperimage + row/td->td_rowsperstrip;
196 strip = row / td->td_rowsperstrip;
199 * Do we want to treat this strip as one whole chunk or
200 * read it a few lines at a time?
202 #if defined(CHUNKY_STRIP_READ_SUPPORT)
203 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
205 whole_strip = tif->tif_dir.td_stripbytecount[strip] < 10
213 read_ahead = tif->tif_scanlinesize * 16 + 5000;
217 * If we haven't loaded this strip, do so now, possibly
218 * only reading the first part.
220 if (strip != tif->tif_curstrip) { /* different strip, refill */
224 if (!TIFFFillStrip(tif, strip))
229 if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
235 ** If we already have some data loaded, do we need to read some more?
237 else if( !whole_strip )
239 if( ((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) < read_ahead
240 && (uint64) tif->tif_rawdataoff+tif->tif_rawdataloaded < td->td_stripbytecount[strip] )
242 if( !TIFFFillStripPartial(tif,strip,read_ahead,0) )
247 if (row < tif->tif_row) {
249 * Moving backwards within the same strip: backup
250 * to the start and then decode forward (below).
252 * NB: If you're planning on lots of random access within a
253 * strip, it's better to just read and decode the entire
254 * strip, and then access the decoded data in a random fashion.
257 if( tif->tif_rawdataoff != 0 )
259 if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
264 if (!TIFFStartStrip(tif, strip))
269 if (row != tif->tif_row) {
271 * Seek forward to the desired row.
274 /* TODO: Will this really work with partial buffers? */
276 if (!(*tif->tif_seek)(tif, row - tif->tif_row))
285 TIFFReadScanline(TIFF* tif, void* buf, uint32 row, uint16 sample)
289 if (!TIFFCheckRead(tif, 0))
291 if( (e = TIFFSeek(tif, row, sample)) != 0) {
293 * Decompress desired row into user buffer.
295 e = (*tif->tif_decoderow)
296 (tif, (uint8*) buf, tif->tif_scanlinesize, sample);
298 /* we are now poised at the beginning of the next row */
299 tif->tif_row = row + 1;
302 (*tif->tif_postdecode)(tif, (uint8*) buf,
303 tif->tif_scanlinesize);
305 return (e > 0 ? 1 : -1);
309 * Read a strip of data and decompress the specified
310 * amount into the user-supplied buffer.
313 TIFFReadEncodedStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
315 static const char module[] = "TIFFReadEncodedStrip";
316 TIFFDirectory *td = &tif->tif_dir;
318 uint32 stripsperplane;
323 if (!TIFFCheckRead(tif,0))
324 return((tmsize_t)(-1));
325 if (strip>=td->td_nstrips)
327 TIFFErrorExt(tif->tif_clientdata,module,
328 "%lu: Strip out of range, max %lu",(unsigned long)strip,
329 (unsigned long)td->td_nstrips);
330 return((tmsize_t)(-1));
333 * Calculate the strip size according to the number of
334 * rows in the strip (check for truncated last strip on any
335 * of the separations).
337 rowsperstrip=td->td_rowsperstrip;
338 if (rowsperstrip>td->td_imagelength)
339 rowsperstrip=td->td_imagelength;
340 stripsperplane=((td->td_imagelength+rowsperstrip-1)/rowsperstrip);
341 stripinplane=(strip%stripsperplane);
342 plane=(strip/stripsperplane);
343 rows=td->td_imagelength-stripinplane*rowsperstrip;
344 if (rows>rowsperstrip)
346 stripsize=TIFFVStripSize(tif,rows);
348 return((tmsize_t)(-1));
349 if ((size!=(tmsize_t)(-1))&&(size<stripsize))
351 if (!TIFFFillStrip(tif,strip))
352 return((tmsize_t)(-1));
353 if ((*tif->tif_decodestrip)(tif,buf,stripsize,plane)<=0)
354 return((tmsize_t)(-1));
355 (*tif->tif_postdecode)(tif,buf,stripsize);
360 TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,
363 TIFFDirectory *td = &tif->tif_dir;
365 if (!_TIFFFillStriles( tif ))
366 return ((tmsize_t)(-1));
368 assert((tif->tif_flags&TIFF_NOREADRAW)==0);
369 if (!isMapped(tif)) {
372 if (!SeekOK(tif, td->td_stripoffset[strip])) {
373 TIFFErrorExt(tif->tif_clientdata, module,
374 "Seek error at scanline %lu, strip %lu",
375 (unsigned long) tif->tif_row, (unsigned long) strip);
376 return ((tmsize_t)(-1));
378 cc = TIFFReadFile(tif, buf, size);
380 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
381 TIFFErrorExt(tif->tif_clientdata, module,
382 "Read error at scanline %lu; got %I64u bytes, expected %I64u",
383 (unsigned long) tif->tif_row,
384 (unsigned __int64) cc,
385 (unsigned __int64) size);
387 TIFFErrorExt(tif->tif_clientdata, module,
388 "Read error at scanline %lu; got %llu bytes, expected %llu",
389 (unsigned long) tif->tif_row,
390 (unsigned long long) cc,
391 (unsigned long long) size);
393 return ((tmsize_t)(-1));
398 ma=(tmsize_t)td->td_stripoffset[strip];
400 if (((uint64)ma!=td->td_stripoffset[strip])||(ma>tif->tif_size))
402 else if ((mb<ma)||(mb<size)||(mb>tif->tif_size))
407 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
408 TIFFErrorExt(tif->tif_clientdata, module,
409 "Read error at scanline %lu, strip %lu; got %I64u bytes, expected %I64u",
410 (unsigned long) tif->tif_row,
411 (unsigned long) strip,
412 (unsigned __int64) n,
413 (unsigned __int64) size);
415 TIFFErrorExt(tif->tif_clientdata, module,
416 "Read error at scanline %lu, strip %lu; got %llu bytes, expected %llu",
417 (unsigned long) tif->tif_row,
418 (unsigned long) strip,
419 (unsigned long long) n,
420 (unsigned long long) size);
422 return ((tmsize_t)(-1));
424 _TIFFmemcpy(buf, tif->tif_base + ma,
431 * Read a strip of data from the file.
434 TIFFReadRawStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
436 static const char module[] = "TIFFReadRawStrip";
437 TIFFDirectory *td = &tif->tif_dir;
441 if (!TIFFCheckRead(tif, 0))
442 return ((tmsize_t)(-1));
443 if (strip >= td->td_nstrips) {
444 TIFFErrorExt(tif->tif_clientdata, module,
445 "%lu: Strip out of range, max %lu",
446 (unsigned long) strip,
447 (unsigned long) td->td_nstrips);
448 return ((tmsize_t)(-1));
450 if (tif->tif_flags&TIFF_NOREADRAW)
452 TIFFErrorExt(tif->tif_clientdata, module,
453 "Compression scheme does not support access to raw uncompressed data");
454 return ((tmsize_t)(-1));
456 bytecount = td->td_stripbytecount[strip];
457 if (bytecount <= 0) {
458 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
459 TIFFErrorExt(tif->tif_clientdata, module,
460 "%I64u: Invalid strip byte count, strip %lu",
461 (unsigned __int64) bytecount,
462 (unsigned long) strip);
464 TIFFErrorExt(tif->tif_clientdata, module,
465 "%llu: Invalid strip byte count, strip %lu",
466 (unsigned long long) bytecount,
467 (unsigned long) strip);
469 return ((tmsize_t)(-1));
471 bytecountm = (tmsize_t)bytecount;
472 if ((uint64)bytecountm!=bytecount) {
473 TIFFErrorExt(tif->tif_clientdata, module, "Integer overflow");
474 return ((tmsize_t)(-1));
476 if (size != (tmsize_t)(-1) && size < bytecountm)
478 return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module));
482 * Read the specified strip and setup for decoding. The data buffer is
483 * expanded, as necessary, to hold the strip's data.
486 TIFFFillStrip(TIFF* tif, uint32 strip)
488 static const char module[] = "TIFFFillStrip";
489 TIFFDirectory *td = &tif->tif_dir;
491 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
494 if ((tif->tif_flags&TIFF_NOREADRAW)==0)
496 uint64 bytecount = td->td_stripbytecount[strip];
497 if (bytecount <= 0) {
498 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
499 TIFFErrorExt(tif->tif_clientdata, module,
500 "Invalid strip byte count %I64u, strip %lu",
501 (unsigned __int64) bytecount,
502 (unsigned long) strip);
504 TIFFErrorExt(tif->tif_clientdata, module,
505 "Invalid strip byte count %llu, strip %lu",
506 (unsigned long long) bytecount,
507 (unsigned long) strip);
512 (isFillOrder(tif, td->td_fillorder)
513 || (tif->tif_flags & TIFF_NOBITREV))) {
515 * The image is mapped into memory and we either don't
516 * need to flip bits or the compression routine is
517 * going to handle this operation itself. In this
518 * case, avoid copying the raw data and instead just
519 * reference the data from the memory mapped file
520 * image. This assumes that the decompression
521 * routines do not modify the contents of the raw data
522 * buffer (if they try to, the application will get a
523 * fault since the file is mapped read-only).
525 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
526 _TIFFfree(tif->tif_rawdata);
527 tif->tif_flags &= ~TIFF_MYBUFFER;
529 * We must check for overflow, potentially causing
530 * an OOB read. Instead of simple
532 * td->td_stripoffset[strip]+bytecount > tif->tif_size
534 * comparison (which can overflow) we do the following
537 if (bytecount > (uint64)tif->tif_size ||
538 td->td_stripoffset[strip] > (uint64)tif->tif_size - bytecount) {
540 * This error message might seem strange, but
541 * it's what would happen if a read were done
544 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
545 TIFFErrorExt(tif->tif_clientdata, module,
547 "Read error on strip %lu; "
548 "got %I64u bytes, expected %I64u",
549 (unsigned long) strip,
550 (unsigned __int64) tif->tif_size - td->td_stripoffset[strip],
551 (unsigned __int64) bytecount);
553 TIFFErrorExt(tif->tif_clientdata, module,
555 "Read error on strip %lu; "
556 "got %llu bytes, expected %llu",
557 (unsigned long) strip,
558 (unsigned long long) tif->tif_size - td->td_stripoffset[strip],
559 (unsigned long long) bytecount);
561 tif->tif_curstrip = NOSTRIP;
564 tif->tif_rawdatasize = (tmsize_t)bytecount;
565 tif->tif_rawdata = tif->tif_base + (tmsize_t)td->td_stripoffset[strip];
566 tif->tif_rawdataoff = 0;
567 tif->tif_rawdataloaded = (tmsize_t) bytecount;
570 * Expand raw data buffer, if needed, to hold data
571 * strip coming from file (perhaps should set upper
572 * bound on the size of a buffer we'll use?).
575 bytecountm=(tmsize_t)bytecount;
576 if ((uint64)bytecountm!=bytecount)
578 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
581 if (bytecountm > tif->tif_rawdatasize) {
582 tif->tif_curstrip = NOSTRIP;
583 if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
584 TIFFErrorExt(tif->tif_clientdata, module,
585 "Data buffer too small to hold strip %lu",
586 (unsigned long) strip);
589 if (!TIFFReadBufferSetup(tif, 0, bytecountm))
592 if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata,
593 bytecountm, module) != bytecountm)
596 tif->tif_rawdataoff = 0;
597 tif->tif_rawdataloaded = bytecountm;
599 if (!isFillOrder(tif, td->td_fillorder) &&
600 (tif->tif_flags & TIFF_NOBITREV) == 0)
601 TIFFReverseBits(tif->tif_rawdata, bytecountm);
604 return (TIFFStartStrip(tif, strip));
608 * Tile-oriented Read Support
609 * Contributed by Nancy Cam (Silicon Graphics).
613 * Read and decompress a tile of data. The
614 * tile is selected by the (x,y,z,s) coordinates.
617 TIFFReadTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
619 if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
620 return ((tmsize_t)(-1));
621 return (TIFFReadEncodedTile(tif,
622 TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));
626 * Read a tile of data and decompress the specified
627 * amount into the user-supplied buffer.
630 TIFFReadEncodedTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)
632 static const char module[] = "TIFFReadEncodedTile";
633 TIFFDirectory *td = &tif->tif_dir;
634 tmsize_t tilesize = tif->tif_tilesize;
636 if (!TIFFCheckRead(tif, 1))
637 return ((tmsize_t)(-1));
638 if (tile >= td->td_nstrips) {
639 TIFFErrorExt(tif->tif_clientdata, module,
640 "%lu: Tile out of range, max %lu",
641 (unsigned long) tile, (unsigned long) td->td_nstrips);
642 return ((tmsize_t)(-1));
644 if (size == (tmsize_t)(-1))
646 else if (size > tilesize)
648 if (TIFFFillTile(tif, tile) && (*tif->tif_decodetile)(tif,
649 (uint8*) buf, size, (uint16)(tile/td->td_stripsperimage))) {
650 (*tif->tif_postdecode)(tif, (uint8*) buf, size);
653 return ((tmsize_t)(-1));
657 TIFFReadRawTile1(TIFF* tif, uint32 tile, void* buf, tmsize_t size, const char* module)
659 TIFFDirectory *td = &tif->tif_dir;
661 if (!_TIFFFillStriles( tif ))
662 return ((tmsize_t)(-1));
664 assert((tif->tif_flags&TIFF_NOREADRAW)==0);
665 if (!isMapped(tif)) {
668 if (!SeekOK(tif, td->td_stripoffset[tile])) {
669 TIFFErrorExt(tif->tif_clientdata, module,
670 "Seek error at row %lu, col %lu, tile %lu",
671 (unsigned long) tif->tif_row,
672 (unsigned long) tif->tif_col,
673 (unsigned long) tile);
674 return ((tmsize_t)(-1));
676 cc = TIFFReadFile(tif, buf, size);
678 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
679 TIFFErrorExt(tif->tif_clientdata, module,
680 "Read error at row %lu, col %lu; got %I64u bytes, expected %I64u",
681 (unsigned long) tif->tif_row,
682 (unsigned long) tif->tif_col,
683 (unsigned __int64) cc,
684 (unsigned __int64) size);
686 TIFFErrorExt(tif->tif_clientdata, module,
687 "Read error at row %lu, col %lu; got %llu bytes, expected %llu",
688 (unsigned long) tif->tif_row,
689 (unsigned long) tif->tif_col,
690 (unsigned long long) cc,
691 (unsigned long long) size);
693 return ((tmsize_t)(-1));
698 ma=(tmsize_t)td->td_stripoffset[tile];
700 if (((uint64)ma!=td->td_stripoffset[tile])||(ma>tif->tif_size))
702 else if ((mb<ma)||(mb<size)||(mb>tif->tif_size))
707 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
708 TIFFErrorExt(tif->tif_clientdata, module,
709 "Read error at row %lu, col %lu, tile %lu; got %I64u bytes, expected %I64u",
710 (unsigned long) tif->tif_row,
711 (unsigned long) tif->tif_col,
712 (unsigned long) tile,
713 (unsigned __int64) n,
714 (unsigned __int64) size);
716 TIFFErrorExt(tif->tif_clientdata, module,
717 "Read error at row %lu, col %lu, tile %lu; got %llu bytes, expected %llu",
718 (unsigned long) tif->tif_row,
719 (unsigned long) tif->tif_col,
720 (unsigned long) tile,
721 (unsigned long long) n,
722 (unsigned long long) size);
724 return ((tmsize_t)(-1));
726 _TIFFmemcpy(buf, tif->tif_base + ma, size);
732 * Read a tile of data from the file.
735 TIFFReadRawTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)
737 static const char module[] = "TIFFReadRawTile";
738 TIFFDirectory *td = &tif->tif_dir;
742 if (!TIFFCheckRead(tif, 1))
743 return ((tmsize_t)(-1));
744 if (tile >= td->td_nstrips) {
745 TIFFErrorExt(tif->tif_clientdata, module,
746 "%lu: Tile out of range, max %lu",
747 (unsigned long) tile, (unsigned long) td->td_nstrips);
748 return ((tmsize_t)(-1));
750 if (tif->tif_flags&TIFF_NOREADRAW)
752 TIFFErrorExt(tif->tif_clientdata, module,
753 "Compression scheme does not support access to raw uncompressed data");
754 return ((tmsize_t)(-1));
756 bytecount64 = td->td_stripbytecount[tile];
757 if (size != (tmsize_t)(-1) && (uint64)size < bytecount64)
758 bytecount64 = (uint64)size;
759 bytecountm = (tmsize_t)bytecount64;
760 if ((uint64)bytecountm!=bytecount64)
762 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
763 return ((tmsize_t)(-1));
765 return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module));
769 * Read the specified tile and setup for decoding. The data buffer is
770 * expanded, as necessary, to hold the tile's data.
773 TIFFFillTile(TIFF* tif, uint32 tile)
775 static const char module[] = "TIFFFillTile";
776 TIFFDirectory *td = &tif->tif_dir;
778 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
781 if ((tif->tif_flags&TIFF_NOREADRAW)==0)
783 uint64 bytecount = td->td_stripbytecount[tile];
784 if (bytecount <= 0) {
785 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
786 TIFFErrorExt(tif->tif_clientdata, module,
787 "%I64u: Invalid tile byte count, tile %lu",
788 (unsigned __int64) bytecount,
789 (unsigned long) tile);
791 TIFFErrorExt(tif->tif_clientdata, module,
792 "%llu: Invalid tile byte count, tile %lu",
793 (unsigned long long) bytecount,
794 (unsigned long) tile);
799 (isFillOrder(tif, td->td_fillorder)
800 || (tif->tif_flags & TIFF_NOBITREV))) {
802 * The image is mapped into memory and we either don't
803 * need to flip bits or the compression routine is
804 * going to handle this operation itself. In this
805 * case, avoid copying the raw data and instead just
806 * reference the data from the memory mapped file
807 * image. This assumes that the decompression
808 * routines do not modify the contents of the raw data
809 * buffer (if they try to, the application will get a
810 * fault since the file is mapped read-only).
812 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
813 _TIFFfree(tif->tif_rawdata);
814 tif->tif_flags &= ~TIFF_MYBUFFER;
816 * We must check for overflow, potentially causing
817 * an OOB read. Instead of simple
819 * td->td_stripoffset[tile]+bytecount > tif->tif_size
821 * comparison (which can overflow) we do the following
824 if (bytecount > (uint64)tif->tif_size ||
825 td->td_stripoffset[tile] > (uint64)tif->tif_size - bytecount) {
826 tif->tif_curtile = NOTILE;
829 tif->tif_rawdatasize = (tmsize_t)bytecount;
831 tif->tif_base + (tmsize_t)td->td_stripoffset[tile];
832 tif->tif_rawdataoff = 0;
833 tif->tif_rawdataloaded = (tmsize_t) bytecount;
836 * Expand raw data buffer, if needed, to hold data
837 * tile coming from file (perhaps should set upper
838 * bound on the size of a buffer we'll use?).
841 bytecountm=(tmsize_t)bytecount;
842 if ((uint64)bytecountm!=bytecount)
844 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
847 if (bytecountm > tif->tif_rawdatasize) {
848 tif->tif_curtile = NOTILE;
849 if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
850 TIFFErrorExt(tif->tif_clientdata, module,
851 "Data buffer too small to hold tile %lu",
852 (unsigned long) tile);
855 if (!TIFFReadBufferSetup(tif, 0, bytecountm))
858 if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata,
859 bytecountm, module) != bytecountm)
862 tif->tif_rawdataoff = 0;
863 tif->tif_rawdataloaded = bytecountm;
865 if (!isFillOrder(tif, td->td_fillorder) &&
866 (tif->tif_flags & TIFF_NOBITREV) == 0)
867 TIFFReverseBits(tif->tif_rawdata,
868 tif->tif_rawdataloaded);
871 return (TIFFStartTile(tif, tile));
875 * Setup the raw data buffer in preparation for
876 * reading a strip of raw data. If the buffer
877 * is specified as zero, then a buffer of appropriate
878 * size is allocated by the library. Otherwise,
879 * the client must guarantee that the buffer is
880 * large enough to hold any individual strip of
884 TIFFReadBufferSetup(TIFF* tif, void* bp, tmsize_t size)
886 static const char module[] = "TIFFReadBufferSetup";
888 assert((tif->tif_flags&TIFF_NOREADRAW)==0);
889 if (tif->tif_rawdata) {
890 if (tif->tif_flags & TIFF_MYBUFFER)
891 _TIFFfree(tif->tif_rawdata);
892 tif->tif_rawdata = NULL;
895 tif->tif_rawdatasize = size;
896 tif->tif_rawdata = (uint8*) bp;
897 tif->tif_flags &= ~TIFF_MYBUFFER;
899 tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64)size, 1024);
900 if (tif->tif_rawdatasize==0)
901 tif->tif_rawdatasize=(tmsize_t)(-1);
902 tif->tif_rawdata = (uint8*) _TIFFmalloc(tif->tif_rawdatasize);
903 tif->tif_flags |= TIFF_MYBUFFER;
905 if (tif->tif_rawdata == NULL) {
906 TIFFErrorExt(tif->tif_clientdata, module,
907 "No space for data buffer at scanline %lu",
908 (unsigned long) tif->tif_row);
909 tif->tif_rawdatasize = 0;
916 * Set state to appear as if a
917 * strip has just been read in.
920 TIFFStartStrip(TIFF* tif, uint32 strip)
922 TIFFDirectory *td = &tif->tif_dir;
924 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
927 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
928 if (!(*tif->tif_setupdecode)(tif))
930 tif->tif_flags |= TIFF_CODERSETUP;
932 tif->tif_curstrip = strip;
933 tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
934 tif->tif_flags &= ~TIFF_BUF4WRITE;
936 if (tif->tif_flags&TIFF_NOREADRAW)
938 tif->tif_rawcp = NULL;
943 tif->tif_rawcp = tif->tif_rawdata;
944 tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[strip];
946 return ((*tif->tif_predecode)(tif,
947 (uint16)(strip / td->td_stripsperimage)));
951 * Set state to appear as if a
952 * tile has just been read in.
955 TIFFStartTile(TIFF* tif, uint32 tile)
957 TIFFDirectory *td = &tif->tif_dir;
959 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
962 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
963 if (!(*tif->tif_setupdecode)(tif))
965 tif->tif_flags |= TIFF_CODERSETUP;
967 tif->tif_curtile = tile;
969 (tile % TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth)) *
972 (tile % TIFFhowmany_32(td->td_imagelength, td->td_tilelength)) *
974 tif->tif_flags &= ~TIFF_BUF4WRITE;
975 if (tif->tif_flags&TIFF_NOREADRAW)
977 tif->tif_rawcp = NULL;
982 tif->tif_rawcp = tif->tif_rawdata;
983 tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[tile];
985 return ((*tif->tif_predecode)(tif,
986 (uint16)(tile/td->td_stripsperimage)));
990 TIFFCheckRead(TIFF* tif, int tiles)
992 if (tif->tif_mode == O_WRONLY) {
993 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "File not open for reading");
996 if (tiles ^ isTiled(tif)) {
997 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, tiles ?
998 "Can not read tiles from a stripped image" :
999 "Can not read scanlines from a tiled image");
1006 _TIFFNoPostDecode(TIFF* tif, uint8* buf, tmsize_t cc)
1008 (void) tif; (void) buf; (void) cc;
1012 _TIFFSwab16BitData(TIFF* tif, uint8* buf, tmsize_t cc)
1015 assert((cc & 1) == 0);
1016 TIFFSwabArrayOfShort((uint16*) buf, cc/2);
1020 _TIFFSwab24BitData(TIFF* tif, uint8* buf, tmsize_t cc)
1023 assert((cc % 3) == 0);
1024 TIFFSwabArrayOfTriples((uint8*) buf, cc/3);
1028 _TIFFSwab32BitData(TIFF* tif, uint8* buf, tmsize_t cc)
1031 assert((cc & 3) == 0);
1032 TIFFSwabArrayOfLong((uint32*) buf, cc/4);
1036 _TIFFSwab64BitData(TIFF* tif, uint8* buf, tmsize_t cc)
1039 assert((cc & 7) == 0);
1040 TIFFSwabArrayOfDouble((double*) buf, cc/8);
1043 /* vim: set ts=8 sts=8 sw=8 noet: */