36
36
static int TIFFStartStrip(TIFF* tif, uint32 strip);
37
37
static int TIFFStartTile(TIFF* tif, uint32 tile);
38
38
static int TIFFCheckRead(TIFF*, int);
40
TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,const char* module);
40
42
#define NOSTRIP ((uint32)(-1)) /* undefined state */
41
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);
44
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
47
TIFFSeek(TIFF* tif, uint32 row, uint16 sample)
170
TIFFSeek(TIFF* tif, uint32 row, uint16 sample )
49
172
register TIFFDirectory *td = &tif->tif_dir;
175
tmsize_t read_ahead = 0;
178
** Establish what strip we are working from.
52
180
if (row >= td->td_imagelength) { /* out of range */
53
181
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
54
182
"%lu: Row out of range, max %lu",
66
194
strip = (uint32)sample*td->td_stripsperimage + row/td->td_rowsperstrip;
68
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.
69
220
if (strip != tif->tif_curstrip) { /* different strip, refill */
70
if (!TIFFFillStrip(tif, strip))
72
} else if (row < tif->tif_row) {
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) {
74
249
* Moving backwards within the same strip: backup
75
250
* to the start and then decode forward (below).