]>
Commit | Line | Data |
---|---|---|
8414a40c VZ |
1 | |
2 | /* | |
3 | * Copyright (c) 1988-1997 Sam Leffler | |
4 | * Copyright (c) 1991-1997 Silicon Graphics, Inc. | |
5 | * | |
6 | * Permission to use, copy, modify, distribute, and sell this software and | |
7 | * its documentation for any purpose is hereby granted without fee, provided | |
8 | * that (i) the above copyright notices and this permission notice appear in | |
9 | * all copies of the software and related documentation, and (ii) the names of | |
10 | * Sam Leffler and Silicon Graphics may not be used in any advertising or | |
11 | * publicity relating to the software without the specific, prior written | |
12 | * permission of Sam Leffler and Silicon Graphics. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, | |
15 | * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY | |
16 | * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. | |
17 | * | |
18 | * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR | |
19 | * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, | |
20 | * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | |
21 | * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF | |
22 | * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE | |
23 | * OF THIS SOFTWARE. | |
24 | */ | |
25 | ||
26 | /* | |
27 | * TIFF Library. | |
28 | * Scanline-oriented Read Support | |
29 | */ | |
30 | #include "tiffiop.h" | |
31 | #include <stdio.h> | |
32 | ||
80ed523f VZ |
33 | int TIFFFillStrip(TIFF* tif, uint32 strip); |
34 | int TIFFFillTile(TIFF* tif, uint32 tile); | |
35 | static int TIFFStartStrip(TIFF* tif, uint32 strip); | |
36 | static int TIFFStartTile(TIFF* tif, uint32 tile); | |
37 | static int TIFFCheckRead(TIFF*, int); | |
38 | static tmsize_t | |
39 | TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,const char* module); | |
8414a40c | 40 | |
80ed523f VZ |
41 | #define NOSTRIP ((uint32)(-1)) /* undefined state */ |
42 | #define NOTILE ((uint32)(-1)) /* undefined state */ | |
43 | ||
44 | static int | |
45 | TIFFFillStripPartial( TIFF *tif, int strip, tmsize_t read_ahead, int restart ) | |
46 | { | |
47 | static const char module[] = "TIFFFillStripPartial"; | |
48 | register TIFFDirectory *td = &tif->tif_dir; | |
d1c629ce | 49 | tmsize_t unused_data; |
80ed523f VZ |
50 | uint64 read_offset; |
51 | tmsize_t cc, to_read; | |
52 | /* tmsize_t bytecountm; */ | |
53 | ||
54 | if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount) | |
55 | return 0; | |
56 | ||
57 | /* | |
58 | * Expand raw data buffer, if needed, to hold data | |
59 | * strip coming from file (perhaps should set upper | |
60 | * bound on the size of a buffer we'll use?). | |
61 | */ | |
62 | ||
63 | /* bytecountm=(tmsize_t) td->td_stripbytecount[strip]; */ | |
64 | if (read_ahead*2 > tif->tif_rawdatasize) { | |
65 | assert( restart ); | |
66 | ||
67 | tif->tif_curstrip = NOSTRIP; | |
68 | if ((tif->tif_flags & TIFF_MYBUFFER) == 0) { | |
69 | TIFFErrorExt(tif->tif_clientdata, module, | |
70 | "Data buffer too small to hold part of strip %lu", | |
71 | (unsigned long) strip); | |
72 | return (0); | |
73 | } | |
74 | if (!TIFFReadBufferSetup(tif, 0, read_ahead*2)) | |
75 | return (0); | |
76 | } | |
77 | ||
78 | if( restart ) | |
79 | { | |
80 | tif->tif_rawdataloaded = 0; | |
81 | tif->tif_rawdataoff = 0; | |
82 | } | |
83 | ||
84 | /* | |
85 | ** If we are reading more data, move any unused data to the | |
86 | ** start of the buffer. | |
87 | */ | |
88 | if( tif->tif_rawdataloaded > 0 ) | |
89 | unused_data = tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata); | |
90 | else | |
91 | unused_data = 0; | |
92 | ||
93 | if( unused_data > 0 ) | |
94 | { | |
95 | assert((tif->tif_flags&TIFF_BUFFERMMAP)==0); | |
96 | memmove( tif->tif_rawdata, tif->tif_rawcp, unused_data ); | |
97 | } | |
98 | ||
99 | /* | |
100 | ** Seek to the point in the file where more data should be read. | |
101 | */ | |
102 | read_offset = td->td_stripoffset[strip] | |
103 | + tif->tif_rawdataoff + tif->tif_rawdataloaded; | |
104 | ||
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); | |
109 | return 0; | |
110 | } | |
111 | ||
112 | /* | |
113 | ** How much do we want to read? | |
114 | */ | |
115 | to_read = tif->tif_rawdatasize - unused_data; | |
116 | if( (uint64) to_read > td->td_stripbytecount[strip] | |
117 | - tif->tif_rawdataoff - tif->tif_rawdataloaded ) | |
118 | { | |
d1c629ce | 119 | to_read = (tmsize_t)td->td_stripbytecount[strip] |
80ed523f VZ |
120 | - tif->tif_rawdataoff - tif->tif_rawdataloaded; |
121 | } | |
122 | ||
123 | assert((tif->tif_flags&TIFF_BUFFERMMAP)==0); | |
124 | cc = TIFFReadFile(tif, tif->tif_rawdata + unused_data, to_read); | |
125 | ||
126 | if (cc != to_read) { | |
127 | #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__)) | |
128 | TIFFErrorExt(tif->tif_clientdata, module, | |
129 | "Read error at scanline %lu; got %I64u bytes, expected %I64u", | |
130 | (unsigned long) tif->tif_row, | |
131 | (unsigned __int64) cc, | |
132 | (unsigned __int64) to_read); | |
133 | #else | |
134 | TIFFErrorExt(tif->tif_clientdata, module, | |
135 | "Read error at scanline %lu; got %llu bytes, expected %llu", | |
136 | (unsigned long) tif->tif_row, | |
137 | (unsigned long long) cc, | |
138 | (unsigned long long) to_read); | |
139 | #endif | |
140 | return 0; | |
141 | } | |
142 | ||
143 | tif->tif_rawdataoff = tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data ; | |
144 | tif->tif_rawdataloaded = unused_data + to_read; | |
145 | ||
146 | tif->tif_rawcp = tif->tif_rawdata; | |
147 | ||
148 | if (!isFillOrder(tif, td->td_fillorder) && | |
149 | (tif->tif_flags & TIFF_NOBITREV) == 0) { | |
150 | assert((tif->tif_flags&TIFF_BUFFERMMAP)==0); | |
151 | TIFFReverseBits(tif->tif_rawdata + unused_data, to_read ); | |
152 | } | |
153 | ||
154 | /* | |
155 | ** When starting a strip from the beginning we need to | |
156 | ** restart the decoder. | |
157 | */ | |
158 | if( restart ) | |
159 | return TIFFStartStrip(tif, strip); | |
160 | else | |
161 | return 1; | |
162 | } | |
8414a40c VZ |
163 | |
164 | /* | |
165 | * Seek to a random row+sample in a file. | |
80ed523f VZ |
166 | * |
167 | * Only used by TIFFReadScanline, and is only used on | |
168 | * strip organized files. We do some tricky stuff to try | |
169 | * and avoid reading the whole compressed raw data for big | |
170 | * strips. | |
8414a40c VZ |
171 | */ |
172 | static int | |
80ed523f | 173 | TIFFSeek(TIFF* tif, uint32 row, uint16 sample ) |
8414a40c VZ |
174 | { |
175 | register TIFFDirectory *td = &tif->tif_dir; | |
80ed523f VZ |
176 | uint32 strip; |
177 | int whole_strip; | |
178 | tmsize_t read_ahead = 0; | |
8414a40c | 179 | |
80ed523f VZ |
180 | /* |
181 | ** Establish what strip we are working from. | |
182 | */ | |
8414a40c | 183 | if (row >= td->td_imagelength) { /* out of range */ |
80ed523f VZ |
184 | TIFFErrorExt(tif->tif_clientdata, tif->tif_name, |
185 | "%lu: Row out of range, max %lu", | |
186 | (unsigned long) row, | |
187 | (unsigned long) td->td_imagelength); | |
8414a40c VZ |
188 | return (0); |
189 | } | |
190 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) { | |
191 | if (sample >= td->td_samplesperpixel) { | |
192 | TIFFErrorExt(tif->tif_clientdata, tif->tif_name, | |
193 | "%lu: Sample out of range, max %lu", | |
194 | (unsigned long) sample, (unsigned long) td->td_samplesperpixel); | |
195 | return (0); | |
196 | } | |
80ed523f | 197 | strip = (uint32)sample*td->td_stripsperimage + row/td->td_rowsperstrip; |
8414a40c VZ |
198 | } else |
199 | strip = row / td->td_rowsperstrip; | |
80ed523f VZ |
200 | |
201 | /* | |
202 | * Do we want to treat this strip as one whole chunk or | |
203 | * read it a few lines at a time? | |
204 | */ | |
205 | #if defined(CHUNKY_STRIP_READ_SUPPORT) | |
206 | if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount) | |
207 | return 0; | |
208 | whole_strip = tif->tif_dir.td_stripbytecount[strip] < 10 | |
209 | || isMapped(tif); | |
210 | #else | |
211 | whole_strip = 1; | |
212 | #endif | |
213 | ||
214 | if( !whole_strip ) | |
215 | { | |
216 | read_ahead = tif->tif_scanlinesize * 16 + 5000; | |
217 | } | |
218 | ||
219 | /* | |
220 | * If we haven't loaded this strip, do so now, possibly | |
221 | * only reading the first part. | |
222 | */ | |
223 | if (strip != tif->tif_curstrip) { /* different strip, refill */ | |
224 | ||
225 | if( whole_strip ) | |
226 | { | |
227 | if (!TIFFFillStrip(tif, strip)) | |
228 | return (0); | |
229 | } | |
230 | else | |
231 | { | |
232 | if( !TIFFFillStripPartial(tif,strip,read_ahead,1) ) | |
233 | return 0; | |
234 | } | |
235 | } | |
236 | ||
237 | /* | |
238 | ** If we already have some data loaded, do we need to read some more? | |
239 | */ | |
240 | else if( !whole_strip ) | |
241 | { | |
242 | if( ((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) < read_ahead | |
243 | && (uint64) tif->tif_rawdataoff+tif->tif_rawdataloaded < td->td_stripbytecount[strip] ) | |
244 | { | |
245 | if( !TIFFFillStripPartial(tif,strip,read_ahead,0) ) | |
246 | return 0; | |
247 | } | |
248 | } | |
249 | ||
250 | if (row < tif->tif_row) { | |
8414a40c VZ |
251 | /* |
252 | * Moving backwards within the same strip: backup | |
253 | * to the start and then decode forward (below). | |
254 | * | |
255 | * NB: If you're planning on lots of random access within a | |
256 | * strip, it's better to just read and decode the entire | |
257 | * strip, and then access the decoded data in a random fashion. | |
258 | */ | |
80ed523f VZ |
259 | |
260 | if( tif->tif_rawdataoff != 0 ) | |
261 | { | |
262 | if( !TIFFFillStripPartial(tif,strip,read_ahead,1) ) | |
263 | return 0; | |
264 | } | |
265 | else | |
266 | { | |
267 | if (!TIFFStartStrip(tif, strip)) | |
268 | return (0); | |
269 | } | |
8414a40c | 270 | } |
80ed523f | 271 | |
8414a40c VZ |
272 | if (row != tif->tif_row) { |
273 | /* | |
274 | * Seek forward to the desired row. | |
275 | */ | |
80ed523f VZ |
276 | |
277 | /* TODO: Will this really work with partial buffers? */ | |
278 | ||
8414a40c VZ |
279 | if (!(*tif->tif_seek)(tif, row - tif->tif_row)) |
280 | return (0); | |
281 | tif->tif_row = row; | |
282 | } | |
80ed523f | 283 | |
8414a40c VZ |
284 | return (1); |
285 | } | |
286 | ||
287 | int | |
80ed523f | 288 | TIFFReadScanline(TIFF* tif, void* buf, uint32 row, uint16 sample) |
8414a40c VZ |
289 | { |
290 | int e; | |
291 | ||
292 | if (!TIFFCheckRead(tif, 0)) | |
293 | return (-1); | |
294 | if( (e = TIFFSeek(tif, row, sample)) != 0) { | |
295 | /* | |
296 | * Decompress desired row into user buffer. | |
297 | */ | |
298 | e = (*tif->tif_decoderow) | |
80ed523f | 299 | (tif, (uint8*) buf, tif->tif_scanlinesize, sample); |
8414a40c | 300 | |
80ed523f VZ |
301 | /* we are now poised at the beginning of the next row */ |
302 | tif->tif_row = row + 1; | |
8414a40c VZ |
303 | |
304 | if (e) | |
80ed523f VZ |
305 | (*tif->tif_postdecode)(tif, (uint8*) buf, |
306 | tif->tif_scanlinesize); | |
8414a40c VZ |
307 | } |
308 | return (e > 0 ? 1 : -1); | |
309 | } | |
310 | ||
311 | /* | |
312 | * Read a strip of data and decompress the specified | |
313 | * amount into the user-supplied buffer. | |
314 | */ | |
80ed523f VZ |
315 | tmsize_t |
316 | TIFFReadEncodedStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size) | |
8414a40c | 317 | { |
80ed523f | 318 | static const char module[] = "TIFFReadEncodedStrip"; |
8414a40c | 319 | TIFFDirectory *td = &tif->tif_dir; |
80ed523f VZ |
320 | uint32 rowsperstrip; |
321 | uint32 stripsperplane; | |
322 | uint32 stripinplane; | |
323 | uint16 plane; | |
324 | uint32 rows; | |
325 | tmsize_t stripsize; | |
326 | if (!TIFFCheckRead(tif,0)) | |
327 | return((tmsize_t)(-1)); | |
328 | if (strip>=td->td_nstrips) | |
329 | { | |
330 | TIFFErrorExt(tif->tif_clientdata,module, | |
331 | "%lu: Strip out of range, max %lu",(unsigned long)strip, | |
332 | (unsigned long)td->td_nstrips); | |
333 | return((tmsize_t)(-1)); | |
8414a40c VZ |
334 | } |
335 | /* | |
336 | * Calculate the strip size according to the number of | |
337 | * rows in the strip (check for truncated last strip on any | |
80ed523f | 338 | * of the separations). |
8414a40c | 339 | */ |
80ed523f VZ |
340 | rowsperstrip=td->td_rowsperstrip; |
341 | if (rowsperstrip>td->td_imagelength) | |
342 | rowsperstrip=td->td_imagelength; | |
343 | stripsperplane=((td->td_imagelength+rowsperstrip-1)/rowsperstrip); | |
344 | stripinplane=(strip%stripsperplane); | |
345 | plane=(strip/stripsperplane); | |
346 | rows=td->td_imagelength-stripinplane*rowsperstrip; | |
347 | if (rows>rowsperstrip) | |
348 | rows=rowsperstrip; | |
349 | stripsize=TIFFVStripSize(tif,rows); | |
350 | if (stripsize==0) | |
351 | return((tmsize_t)(-1)); | |
352 | if ((size!=(tmsize_t)(-1))&&(size<stripsize)) | |
353 | stripsize=size; | |
354 | if (!TIFFFillStrip(tif,strip)) | |
355 | return((tmsize_t)(-1)); | |
356 | if ((*tif->tif_decodestrip)(tif,buf,stripsize,plane)<=0) | |
357 | return((tmsize_t)(-1)); | |
358 | (*tif->tif_postdecode)(tif,buf,stripsize); | |
359 | return(stripsize); | |
8414a40c VZ |
360 | } |
361 | ||
80ed523f VZ |
362 | static tmsize_t |
363 | TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size, | |
364 | const char* module) | |
8414a40c VZ |
365 | { |
366 | TIFFDirectory *td = &tif->tif_dir; | |
367 | ||
80ed523f VZ |
368 | if (!_TIFFFillStriles( tif )) |
369 | return ((tmsize_t)(-1)); | |
370 | ||
371 | assert((tif->tif_flags&TIFF_NOREADRAW)==0); | |
8414a40c | 372 | if (!isMapped(tif)) { |
80ed523f | 373 | tmsize_t cc; |
8414a40c VZ |
374 | |
375 | if (!SeekOK(tif, td->td_stripoffset[strip])) { | |
376 | TIFFErrorExt(tif->tif_clientdata, module, | |
80ed523f | 377 | "Seek error at scanline %lu, strip %lu", |
8414a40c | 378 | (unsigned long) tif->tif_row, (unsigned long) strip); |
80ed523f | 379 | return ((tmsize_t)(-1)); |
8414a40c VZ |
380 | } |
381 | cc = TIFFReadFile(tif, buf, size); | |
382 | if (cc != size) { | |
80ed523f | 383 | #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__)) |
8414a40c | 384 | TIFFErrorExt(tif->tif_clientdata, module, |
80ed523f VZ |
385 | "Read error at scanline %lu; got %I64u bytes, expected %I64u", |
386 | (unsigned long) tif->tif_row, | |
387 | (unsigned __int64) cc, | |
388 | (unsigned __int64) size); | |
389 | #else | |
390 | TIFFErrorExt(tif->tif_clientdata, module, | |
391 | "Read error at scanline %lu; got %llu bytes, expected %llu", | |
392 | (unsigned long) tif->tif_row, | |
393 | (unsigned long long) cc, | |
394 | (unsigned long long) size); | |
395 | #endif | |
396 | return ((tmsize_t)(-1)); | |
8414a40c VZ |
397 | } |
398 | } else { | |
80ed523f VZ |
399 | tmsize_t ma,mb; |
400 | tmsize_t n; | |
401 | ma=(tmsize_t)td->td_stripoffset[strip]; | |
402 | mb=ma+size; | |
403 | if (((uint64)ma!=td->td_stripoffset[strip])||(ma>tif->tif_size)) | |
404 | n=0; | |
405 | else if ((mb<ma)||(mb<size)||(mb>tif->tif_size)) | |
406 | n=tif->tif_size-ma; | |
407 | else | |
408 | n=size; | |
409 | if (n!=size) { | |
410 | #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__)) | |
8414a40c | 411 | TIFFErrorExt(tif->tif_clientdata, module, |
80ed523f VZ |
412 | "Read error at scanline %lu, strip %lu; got %I64u bytes, expected %I64u", |
413 | (unsigned long) tif->tif_row, | |
414 | (unsigned long) strip, | |
415 | (unsigned __int64) n, | |
416 | (unsigned __int64) size); | |
417 | #else | |
418 | TIFFErrorExt(tif->tif_clientdata, module, | |
419 | "Read error at scanline %lu, strip %lu; got %llu bytes, expected %llu", | |
420 | (unsigned long) tif->tif_row, | |
421 | (unsigned long) strip, | |
422 | (unsigned long long) n, | |
423 | (unsigned long long) size); | |
424 | #endif | |
425 | return ((tmsize_t)(-1)); | |
8414a40c | 426 | } |
80ed523f VZ |
427 | _TIFFmemcpy(buf, tif->tif_base + ma, |
428 | size); | |
8414a40c VZ |
429 | } |
430 | return (size); | |
431 | } | |
432 | ||
433 | /* | |
434 | * Read a strip of data from the file. | |
435 | */ | |
80ed523f VZ |
436 | tmsize_t |
437 | TIFFReadRawStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size) | |
8414a40c VZ |
438 | { |
439 | static const char module[] = "TIFFReadRawStrip"; | |
440 | TIFFDirectory *td = &tif->tif_dir; | |
80ed523f VZ |
441 | uint64 bytecount; |
442 | tmsize_t bytecountm; | |
8414a40c VZ |
443 | |
444 | if (!TIFFCheckRead(tif, 0)) | |
80ed523f | 445 | return ((tmsize_t)(-1)); |
8414a40c | 446 | if (strip >= td->td_nstrips) { |
80ed523f VZ |
447 | TIFFErrorExt(tif->tif_clientdata, module, |
448 | "%lu: Strip out of range, max %lu", | |
449 | (unsigned long) strip, | |
450 | (unsigned long) td->td_nstrips); | |
451 | return ((tmsize_t)(-1)); | |
452 | } | |
453 | if (tif->tif_flags&TIFF_NOREADRAW) | |
454 | { | |
455 | TIFFErrorExt(tif->tif_clientdata, module, | |
456 | "Compression scheme does not support access to raw uncompressed data"); | |
457 | return ((tmsize_t)(-1)); | |
8414a40c VZ |
458 | } |
459 | bytecount = td->td_stripbytecount[strip]; | |
460 | if (bytecount <= 0) { | |
80ed523f VZ |
461 | #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__)) |
462 | TIFFErrorExt(tif->tif_clientdata, module, | |
463 | "%I64u: Invalid strip byte count, strip %lu", | |
464 | (unsigned __int64) bytecount, | |
465 | (unsigned long) strip); | |
466 | #else | |
467 | TIFFErrorExt(tif->tif_clientdata, module, | |
468 | "%llu: Invalid strip byte count, strip %lu", | |
469 | (unsigned long long) bytecount, | |
470 | (unsigned long) strip); | |
471 | #endif | |
472 | return ((tmsize_t)(-1)); | |
473 | } | |
474 | bytecountm = (tmsize_t)bytecount; | |
475 | if ((uint64)bytecountm!=bytecount) { | |
476 | TIFFErrorExt(tif->tif_clientdata, module, "Integer overflow"); | |
477 | return ((tmsize_t)(-1)); | |
8414a40c | 478 | } |
80ed523f VZ |
479 | if (size != (tmsize_t)(-1) && size < bytecountm) |
480 | bytecountm = size; | |
481 | return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module)); | |
8414a40c VZ |
482 | } |
483 | ||
484 | /* | |
80ed523f VZ |
485 | * Read the specified strip and setup for decoding. The data buffer is |
486 | * expanded, as necessary, to hold the strip's data. | |
8414a40c VZ |
487 | */ |
488 | int | |
80ed523f | 489 | TIFFFillStrip(TIFF* tif, uint32 strip) |
8414a40c VZ |
490 | { |
491 | static const char module[] = "TIFFFillStrip"; | |
492 | TIFFDirectory *td = &tif->tif_dir; | |
8414a40c | 493 | |
80ed523f VZ |
494 | if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount) |
495 | return 0; | |
496 | ||
497 | if ((tif->tif_flags&TIFF_NOREADRAW)==0) | |
498 | { | |
499 | uint64 bytecount = td->td_stripbytecount[strip]; | |
500 | if (bytecount <= 0) { | |
501 | #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__)) | |
502 | TIFFErrorExt(tif->tif_clientdata, module, | |
503 | "Invalid strip byte count %I64u, strip %lu", | |
504 | (unsigned __int64) bytecount, | |
505 | (unsigned long) strip); | |
506 | #else | |
8414a40c | 507 | TIFFErrorExt(tif->tif_clientdata, module, |
80ed523f VZ |
508 | "Invalid strip byte count %llu, strip %lu", |
509 | (unsigned long long) bytecount, | |
510 | (unsigned long) strip); | |
511 | #endif | |
8414a40c VZ |
512 | return (0); |
513 | } | |
80ed523f VZ |
514 | if (isMapped(tif) && |
515 | (isFillOrder(tif, td->td_fillorder) | |
516 | || (tif->tif_flags & TIFF_NOBITREV))) { | |
517 | /* | |
518 | * The image is mapped into memory and we either don't | |
519 | * need to flip bits or the compression routine is | |
520 | * going to handle this operation itself. In this | |
521 | * case, avoid copying the raw data and instead just | |
522 | * reference the data from the memory mapped file | |
523 | * image. This assumes that the decompression | |
524 | * routines do not modify the contents of the raw data | |
525 | * buffer (if they try to, the application will get a | |
526 | * fault since the file is mapped read-only). | |
527 | */ | |
528 | if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) { | |
529 | _TIFFfree(tif->tif_rawdata); | |
530 | tif->tif_rawdata = NULL; | |
531 | tif->tif_rawdatasize = 0; | |
532 | } | |
533 | tif->tif_flags &= ~TIFF_MYBUFFER; | |
534 | /* | |
535 | * We must check for overflow, potentially causing | |
536 | * an OOB read. Instead of simple | |
537 | * | |
538 | * td->td_stripoffset[strip]+bytecount > tif->tif_size | |
539 | * | |
540 | * comparison (which can overflow) we do the following | |
541 | * two comparisons: | |
542 | */ | |
543 | if (bytecount > (uint64)tif->tif_size || | |
544 | td->td_stripoffset[strip] > (uint64)tif->tif_size - bytecount) { | |
545 | /* | |
546 | * This error message might seem strange, but | |
547 | * it's what would happen if a read were done | |
548 | * instead. | |
549 | */ | |
550 | #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__)) | |
8414a40c | 551 | TIFFErrorExt(tif->tif_clientdata, module, |
80ed523f VZ |
552 | |
553 | "Read error on strip %lu; " | |
554 | "got %I64u bytes, expected %I64u", | |
555 | (unsigned long) strip, | |
556 | (unsigned __int64) tif->tif_size - td->td_stripoffset[strip], | |
557 | (unsigned __int64) bytecount); | |
558 | #else | |
559 | TIFFErrorExt(tif->tif_clientdata, module, | |
560 | ||
561 | "Read error on strip %lu; " | |
562 | "got %llu bytes, expected %llu", | |
563 | (unsigned long) strip, | |
564 | (unsigned long long) tif->tif_size - td->td_stripoffset[strip], | |
565 | (unsigned long long) bytecount); | |
566 | #endif | |
567 | tif->tif_curstrip = NOSTRIP; | |
8414a40c VZ |
568 | return (0); |
569 | } | |
80ed523f VZ |
570 | tif->tif_rawdatasize = (tmsize_t)bytecount; |
571 | tif->tif_rawdata = tif->tif_base + (tmsize_t)td->td_stripoffset[strip]; | |
572 | tif->tif_rawdataoff = 0; | |
573 | tif->tif_rawdataloaded = (tmsize_t) bytecount; | |
574 | ||
575 | /* | |
576 | * When we have tif_rawdata reference directly into the memory mapped file | |
577 | * we need to be pretty careful about how we use the rawdata. It is not | |
578 | * a general purpose working buffer as it normally otherwise is. So we | |
579 | * keep track of this fact to avoid using it improperly. | |
580 | */ | |
581 | tif->tif_flags |= TIFF_BUFFERMMAP; | |
582 | } else { | |
583 | /* | |
584 | * Expand raw data buffer, if needed, to hold data | |
585 | * strip coming from file (perhaps should set upper | |
586 | * bound on the size of a buffer we'll use?). | |
587 | */ | |
588 | tmsize_t bytecountm; | |
589 | bytecountm=(tmsize_t)bytecount; | |
590 | if ((uint64)bytecountm!=bytecount) | |
591 | { | |
592 | TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow"); | |
593 | return(0); | |
594 | } | |
595 | if (bytecountm > tif->tif_rawdatasize) { | |
596 | tif->tif_curstrip = NOSTRIP; | |
597 | if ((tif->tif_flags & TIFF_MYBUFFER) == 0) { | |
598 | TIFFErrorExt(tif->tif_clientdata, module, | |
599 | "Data buffer too small to hold strip %lu", | |
600 | (unsigned long) strip); | |
601 | return (0); | |
602 | } | |
603 | if (!TIFFReadBufferSetup(tif, 0, bytecountm)) | |
604 | return (0); | |
605 | } | |
606 | if (tif->tif_flags&TIFF_BUFFERMMAP) { | |
607 | tif->tif_curstrip = NOSTRIP; | |
608 | if (!TIFFReadBufferSetup(tif, 0, bytecountm)) | |
609 | return (0); | |
610 | } | |
611 | if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata, | |
612 | bytecountm, module) != bytecountm) | |
8414a40c | 613 | return (0); |
80ed523f VZ |
614 | |
615 | tif->tif_rawdataoff = 0; | |
616 | tif->tif_rawdataloaded = bytecountm; | |
617 | ||
618 | if (!isFillOrder(tif, td->td_fillorder) && | |
619 | (tif->tif_flags & TIFF_NOBITREV) == 0) | |
620 | TIFFReverseBits(tif->tif_rawdata, bytecountm); | |
621 | } | |
8414a40c VZ |
622 | } |
623 | return (TIFFStartStrip(tif, strip)); | |
624 | } | |
625 | ||
626 | /* | |
627 | * Tile-oriented Read Support | |
628 | * Contributed by Nancy Cam (Silicon Graphics). | |
629 | */ | |
630 | ||
631 | /* | |
632 | * Read and decompress a tile of data. The | |
633 | * tile is selected by the (x,y,z,s) coordinates. | |
634 | */ | |
80ed523f VZ |
635 | tmsize_t |
636 | TIFFReadTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s) | |
8414a40c VZ |
637 | { |
638 | if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s)) | |
80ed523f | 639 | return ((tmsize_t)(-1)); |
8414a40c | 640 | return (TIFFReadEncodedTile(tif, |
80ed523f | 641 | TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1))); |
8414a40c VZ |
642 | } |
643 | ||
644 | /* | |
645 | * Read a tile of data and decompress the specified | |
646 | * amount into the user-supplied buffer. | |
647 | */ | |
80ed523f VZ |
648 | tmsize_t |
649 | TIFFReadEncodedTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size) | |
8414a40c | 650 | { |
80ed523f | 651 | static const char module[] = "TIFFReadEncodedTile"; |
8414a40c | 652 | TIFFDirectory *td = &tif->tif_dir; |
80ed523f | 653 | tmsize_t tilesize = tif->tif_tilesize; |
8414a40c VZ |
654 | |
655 | if (!TIFFCheckRead(tif, 1)) | |
80ed523f | 656 | return ((tmsize_t)(-1)); |
8414a40c | 657 | if (tile >= td->td_nstrips) { |
80ed523f VZ |
658 | TIFFErrorExt(tif->tif_clientdata, module, |
659 | "%lu: Tile out of range, max %lu", | |
660 | (unsigned long) tile, (unsigned long) td->td_nstrips); | |
661 | return ((tmsize_t)(-1)); | |
8414a40c | 662 | } |
80ed523f | 663 | if (size == (tmsize_t)(-1)) |
8414a40c VZ |
664 | size = tilesize; |
665 | else if (size > tilesize) | |
666 | size = tilesize; | |
667 | if (TIFFFillTile(tif, tile) && (*tif->tif_decodetile)(tif, | |
80ed523f VZ |
668 | (uint8*) buf, size, (uint16)(tile/td->td_stripsperimage))) { |
669 | (*tif->tif_postdecode)(tif, (uint8*) buf, size); | |
8414a40c VZ |
670 | return (size); |
671 | } else | |
80ed523f | 672 | return ((tmsize_t)(-1)); |
8414a40c VZ |
673 | } |
674 | ||
80ed523f VZ |
675 | static tmsize_t |
676 | TIFFReadRawTile1(TIFF* tif, uint32 tile, void* buf, tmsize_t size, const char* module) | |
8414a40c VZ |
677 | { |
678 | TIFFDirectory *td = &tif->tif_dir; | |
679 | ||
80ed523f VZ |
680 | if (!_TIFFFillStriles( tif )) |
681 | return ((tmsize_t)(-1)); | |
682 | ||
683 | assert((tif->tif_flags&TIFF_NOREADRAW)==0); | |
8414a40c | 684 | if (!isMapped(tif)) { |
80ed523f | 685 | tmsize_t cc; |
8414a40c VZ |
686 | |
687 | if (!SeekOK(tif, td->td_stripoffset[tile])) { | |
688 | TIFFErrorExt(tif->tif_clientdata, module, | |
80ed523f VZ |
689 | "Seek error at row %lu, col %lu, tile %lu", |
690 | (unsigned long) tif->tif_row, | |
691 | (unsigned long) tif->tif_col, | |
692 | (unsigned long) tile); | |
693 | return ((tmsize_t)(-1)); | |
8414a40c VZ |
694 | } |
695 | cc = TIFFReadFile(tif, buf, size); | |
696 | if (cc != size) { | |
80ed523f VZ |
697 | #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__)) |
698 | TIFFErrorExt(tif->tif_clientdata, module, | |
699 | "Read error at row %lu, col %lu; got %I64u bytes, expected %I64u", | |
700 | (unsigned long) tif->tif_row, | |
701 | (unsigned long) tif->tif_col, | |
702 | (unsigned __int64) cc, | |
703 | (unsigned __int64) size); | |
704 | #else | |
8414a40c | 705 | TIFFErrorExt(tif->tif_clientdata, module, |
80ed523f VZ |
706 | "Read error at row %lu, col %lu; got %llu bytes, expected %llu", |
707 | (unsigned long) tif->tif_row, | |
708 | (unsigned long) tif->tif_col, | |
709 | (unsigned long long) cc, | |
710 | (unsigned long long) size); | |
711 | #endif | |
712 | return ((tmsize_t)(-1)); | |
8414a40c VZ |
713 | } |
714 | } else { | |
80ed523f VZ |
715 | tmsize_t ma,mb; |
716 | tmsize_t n; | |
717 | ma=(tmsize_t)td->td_stripoffset[tile]; | |
718 | mb=ma+size; | |
719 | if (((uint64)ma!=td->td_stripoffset[tile])||(ma>tif->tif_size)) | |
720 | n=0; | |
721 | else if ((mb<ma)||(mb<size)||(mb>tif->tif_size)) | |
722 | n=tif->tif_size-ma; | |
723 | else | |
724 | n=size; | |
725 | if (n!=size) { | |
726 | #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__)) | |
8414a40c | 727 | TIFFErrorExt(tif->tif_clientdata, module, |
80ed523f VZ |
728 | "Read error at row %lu, col %lu, tile %lu; got %I64u bytes, expected %I64u", |
729 | (unsigned long) tif->tif_row, | |
730 | (unsigned long) tif->tif_col, | |
731 | (unsigned long) tile, | |
732 | (unsigned __int64) n, | |
733 | (unsigned __int64) size); | |
734 | #else | |
735 | TIFFErrorExt(tif->tif_clientdata, module, | |
736 | "Read error at row %lu, col %lu, tile %lu; got %llu bytes, expected %llu", | |
737 | (unsigned long) tif->tif_row, | |
738 | (unsigned long) tif->tif_col, | |
739 | (unsigned long) tile, | |
740 | (unsigned long long) n, | |
741 | (unsigned long long) size); | |
742 | #endif | |
743 | return ((tmsize_t)(-1)); | |
8414a40c | 744 | } |
80ed523f | 745 | _TIFFmemcpy(buf, tif->tif_base + ma, size); |
8414a40c VZ |
746 | } |
747 | return (size); | |
748 | } | |
749 | ||
750 | /* | |
751 | * Read a tile of data from the file. | |
752 | */ | |
80ed523f VZ |
753 | tmsize_t |
754 | TIFFReadRawTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size) | |
8414a40c VZ |
755 | { |
756 | static const char module[] = "TIFFReadRawTile"; | |
757 | TIFFDirectory *td = &tif->tif_dir; | |
80ed523f VZ |
758 | uint64 bytecount64; |
759 | tmsize_t bytecountm; | |
8414a40c VZ |
760 | |
761 | if (!TIFFCheckRead(tif, 1)) | |
80ed523f | 762 | return ((tmsize_t)(-1)); |
8414a40c | 763 | if (tile >= td->td_nstrips) { |
80ed523f VZ |
764 | TIFFErrorExt(tif->tif_clientdata, module, |
765 | "%lu: Tile out of range, max %lu", | |
8414a40c | 766 | (unsigned long) tile, (unsigned long) td->td_nstrips); |
80ed523f | 767 | return ((tmsize_t)(-1)); |
8414a40c | 768 | } |
80ed523f VZ |
769 | if (tif->tif_flags&TIFF_NOREADRAW) |
770 | { | |
771 | TIFFErrorExt(tif->tif_clientdata, module, | |
772 | "Compression scheme does not support access to raw uncompressed data"); | |
773 | return ((tmsize_t)(-1)); | |
774 | } | |
775 | bytecount64 = td->td_stripbytecount[tile]; | |
776 | if (size != (tmsize_t)(-1) && (uint64)size < bytecount64) | |
777 | bytecount64 = (uint64)size; | |
778 | bytecountm = (tmsize_t)bytecount64; | |
779 | if ((uint64)bytecountm!=bytecount64) | |
780 | { | |
781 | TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow"); | |
782 | return ((tmsize_t)(-1)); | |
783 | } | |
784 | return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module)); | |
8414a40c VZ |
785 | } |
786 | ||
787 | /* | |
80ed523f VZ |
788 | * Read the specified tile and setup for decoding. The data buffer is |
789 | * expanded, as necessary, to hold the tile's data. | |
8414a40c VZ |
790 | */ |
791 | int | |
80ed523f | 792 | TIFFFillTile(TIFF* tif, uint32 tile) |
8414a40c VZ |
793 | { |
794 | static const char module[] = "TIFFFillTile"; | |
795 | TIFFDirectory *td = &tif->tif_dir; | |
8414a40c | 796 | |
80ed523f VZ |
797 | if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount) |
798 | return 0; | |
799 | ||
800 | if ((tif->tif_flags&TIFF_NOREADRAW)==0) | |
801 | { | |
802 | uint64 bytecount = td->td_stripbytecount[tile]; | |
803 | if (bytecount <= 0) { | |
804 | #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__)) | |
805 | TIFFErrorExt(tif->tif_clientdata, module, | |
806 | "%I64u: Invalid tile byte count, tile %lu", | |
807 | (unsigned __int64) bytecount, | |
808 | (unsigned long) tile); | |
809 | #else | |
810 | TIFFErrorExt(tif->tif_clientdata, module, | |
811 | "%llu: Invalid tile byte count, tile %lu", | |
812 | (unsigned long long) bytecount, | |
813 | (unsigned long) tile); | |
814 | #endif | |
8414a40c VZ |
815 | return (0); |
816 | } | |
80ed523f VZ |
817 | if (isMapped(tif) && |
818 | (isFillOrder(tif, td->td_fillorder) | |
819 | || (tif->tif_flags & TIFF_NOBITREV))) { | |
820 | /* | |
821 | * The image is mapped into memory and we either don't | |
822 | * need to flip bits or the compression routine is | |
823 | * going to handle this operation itself. In this | |
824 | * case, avoid copying the raw data and instead just | |
825 | * reference the data from the memory mapped file | |
826 | * image. This assumes that the decompression | |
827 | * routines do not modify the contents of the raw data | |
828 | * buffer (if they try to, the application will get a | |
829 | * fault since the file is mapped read-only). | |
830 | */ | |
831 | if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) { | |
832 | _TIFFfree(tif->tif_rawdata); | |
833 | tif->tif_rawdata = NULL; | |
834 | tif->tif_rawdatasize = 0; | |
835 | } | |
836 | tif->tif_flags &= ~TIFF_MYBUFFER; | |
837 | /* | |
838 | * We must check for overflow, potentially causing | |
839 | * an OOB read. Instead of simple | |
840 | * | |
841 | * td->td_stripoffset[tile]+bytecount > tif->tif_size | |
842 | * | |
843 | * comparison (which can overflow) we do the following | |
844 | * two comparisons: | |
845 | */ | |
846 | if (bytecount > (uint64)tif->tif_size || | |
847 | td->td_stripoffset[tile] > (uint64)tif->tif_size - bytecount) { | |
848 | tif->tif_curtile = NOTILE; | |
8414a40c VZ |
849 | return (0); |
850 | } | |
80ed523f VZ |
851 | tif->tif_rawdatasize = (tmsize_t)bytecount; |
852 | tif->tif_rawdata = | |
853 | tif->tif_base + (tmsize_t)td->td_stripoffset[tile]; | |
854 | tif->tif_rawdataoff = 0; | |
855 | tif->tif_rawdataloaded = (tmsize_t) bytecount; | |
856 | tif->tif_flags |= TIFF_BUFFERMMAP; | |
857 | } else { | |
858 | /* | |
859 | * Expand raw data buffer, if needed, to hold data | |
860 | * tile coming from file (perhaps should set upper | |
861 | * bound on the size of a buffer we'll use?). | |
862 | */ | |
863 | tmsize_t bytecountm; | |
864 | bytecountm=(tmsize_t)bytecount; | |
865 | if ((uint64)bytecountm!=bytecount) | |
866 | { | |
867 | TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow"); | |
868 | return(0); | |
869 | } | |
870 | if (bytecountm > tif->tif_rawdatasize) { | |
871 | tif->tif_curtile = NOTILE; | |
872 | if ((tif->tif_flags & TIFF_MYBUFFER) == 0) { | |
873 | TIFFErrorExt(tif->tif_clientdata, module, | |
874 | "Data buffer too small to hold tile %lu", | |
875 | (unsigned long) tile); | |
876 | return (0); | |
877 | } | |
878 | if (!TIFFReadBufferSetup(tif, 0, bytecountm)) | |
879 | return (0); | |
880 | } | |
881 | if (tif->tif_flags&TIFF_BUFFERMMAP) { | |
882 | tif->tif_curtile = NOTILE; | |
883 | if (!TIFFReadBufferSetup(tif, 0, bytecountm)) | |
884 | return (0); | |
885 | } | |
886 | ||
887 | if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata, | |
888 | bytecountm, module) != bytecountm) | |
8414a40c | 889 | return (0); |
80ed523f VZ |
890 | |
891 | tif->tif_rawdataoff = 0; | |
892 | tif->tif_rawdataloaded = bytecountm; | |
893 | ||
894 | if (!isFillOrder(tif, td->td_fillorder) && | |
895 | (tif->tif_flags & TIFF_NOBITREV) == 0) | |
896 | TIFFReverseBits(tif->tif_rawdata, | |
897 | tif->tif_rawdataloaded); | |
8414a40c | 898 | } |
8414a40c VZ |
899 | } |
900 | return (TIFFStartTile(tif, tile)); | |
901 | } | |
902 | ||
903 | /* | |
904 | * Setup the raw data buffer in preparation for | |
905 | * reading a strip of raw data. If the buffer | |
906 | * is specified as zero, then a buffer of appropriate | |
907 | * size is allocated by the library. Otherwise, | |
908 | * the client must guarantee that the buffer is | |
909 | * large enough to hold any individual strip of | |
910 | * raw data. | |
911 | */ | |
912 | int | |
80ed523f | 913 | TIFFReadBufferSetup(TIFF* tif, void* bp, tmsize_t size) |
8414a40c VZ |
914 | { |
915 | static const char module[] = "TIFFReadBufferSetup"; | |
916 | ||
80ed523f VZ |
917 | assert((tif->tif_flags&TIFF_NOREADRAW)==0); |
918 | tif->tif_flags &= ~TIFF_BUFFERMMAP; | |
919 | ||
8414a40c VZ |
920 | if (tif->tif_rawdata) { |
921 | if (tif->tif_flags & TIFF_MYBUFFER) | |
922 | _TIFFfree(tif->tif_rawdata); | |
923 | tif->tif_rawdata = NULL; | |
80ed523f | 924 | tif->tif_rawdatasize = 0; |
8414a40c VZ |
925 | } |
926 | if (bp) { | |
927 | tif->tif_rawdatasize = size; | |
80ed523f | 928 | tif->tif_rawdata = (uint8*) bp; |
8414a40c VZ |
929 | tif->tif_flags &= ~TIFF_MYBUFFER; |
930 | } else { | |
80ed523f VZ |
931 | tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64)size, 1024); |
932 | if (tif->tif_rawdatasize==0) | |
933 | tif->tif_rawdatasize=(tmsize_t)(-1); | |
934 | tif->tif_rawdata = (uint8*) _TIFFmalloc(tif->tif_rawdatasize); | |
8414a40c VZ |
935 | tif->tif_flags |= TIFF_MYBUFFER; |
936 | } | |
937 | if (tif->tif_rawdata == NULL) { | |
938 | TIFFErrorExt(tif->tif_clientdata, module, | |
80ed523f VZ |
939 | "No space for data buffer at scanline %lu", |
940 | (unsigned long) tif->tif_row); | |
8414a40c VZ |
941 | tif->tif_rawdatasize = 0; |
942 | return (0); | |
943 | } | |
944 | return (1); | |
945 | } | |
946 | ||
947 | /* | |
948 | * Set state to appear as if a | |
949 | * strip has just been read in. | |
950 | */ | |
951 | static int | |
80ed523f | 952 | TIFFStartStrip(TIFF* tif, uint32 strip) |
8414a40c VZ |
953 | { |
954 | TIFFDirectory *td = &tif->tif_dir; | |
955 | ||
80ed523f VZ |
956 | if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount) |
957 | return 0; | |
958 | ||
8414a40c VZ |
959 | if ((tif->tif_flags & TIFF_CODERSETUP) == 0) { |
960 | if (!(*tif->tif_setupdecode)(tif)) | |
961 | return (0); | |
962 | tif->tif_flags |= TIFF_CODERSETUP; | |
963 | } | |
964 | tif->tif_curstrip = strip; | |
965 | tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; | |
80ed523f VZ |
966 | tif->tif_flags &= ~TIFF_BUF4WRITE; |
967 | ||
968 | if (tif->tif_flags&TIFF_NOREADRAW) | |
969 | { | |
970 | tif->tif_rawcp = NULL; | |
971 | tif->tif_rawcc = 0; | |
972 | } | |
973 | else | |
974 | { | |
975 | tif->tif_rawcp = tif->tif_rawdata; | |
976 | tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[strip]; | |
977 | } | |
8414a40c | 978 | return ((*tif->tif_predecode)(tif, |
80ed523f | 979 | (uint16)(strip / td->td_stripsperimage))); |
8414a40c VZ |
980 | } |
981 | ||
982 | /* | |
983 | * Set state to appear as if a | |
984 | * tile has just been read in. | |
985 | */ | |
986 | static int | |
80ed523f | 987 | TIFFStartTile(TIFF* tif, uint32 tile) |
8414a40c VZ |
988 | { |
989 | TIFFDirectory *td = &tif->tif_dir; | |
990 | ||
80ed523f VZ |
991 | if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount) |
992 | return 0; | |
993 | ||
8414a40c VZ |
994 | if ((tif->tif_flags & TIFF_CODERSETUP) == 0) { |
995 | if (!(*tif->tif_setupdecode)(tif)) | |
996 | return (0); | |
997 | tif->tif_flags |= TIFF_CODERSETUP; | |
998 | } | |
999 | tif->tif_curtile = tile; | |
1000 | tif->tif_row = | |
80ed523f | 1001 | (tile % TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth)) * |
8414a40c VZ |
1002 | td->td_tilelength; |
1003 | tif->tif_col = | |
80ed523f | 1004 | (tile % TIFFhowmany_32(td->td_imagelength, td->td_tilelength)) * |
8414a40c | 1005 | td->td_tilewidth; |
80ed523f VZ |
1006 | tif->tif_flags &= ~TIFF_BUF4WRITE; |
1007 | if (tif->tif_flags&TIFF_NOREADRAW) | |
1008 | { | |
1009 | tif->tif_rawcp = NULL; | |
1010 | tif->tif_rawcc = 0; | |
1011 | } | |
1012 | else | |
1013 | { | |
1014 | tif->tif_rawcp = tif->tif_rawdata; | |
1015 | tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[tile]; | |
1016 | } | |
8414a40c | 1017 | return ((*tif->tif_predecode)(tif, |
80ed523f | 1018 | (uint16)(tile/td->td_stripsperimage))); |
8414a40c VZ |
1019 | } |
1020 | ||
1021 | static int | |
1022 | TIFFCheckRead(TIFF* tif, int tiles) | |
1023 | { | |
1024 | if (tif->tif_mode == O_WRONLY) { | |
1025 | TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "File not open for reading"); | |
1026 | return (0); | |
1027 | } | |
1028 | if (tiles ^ isTiled(tif)) { | |
1029 | TIFFErrorExt(tif->tif_clientdata, tif->tif_name, tiles ? | |
1030 | "Can not read tiles from a stripped image" : | |
1031 | "Can not read scanlines from a tiled image"); | |
1032 | return (0); | |
1033 | } | |
1034 | return (1); | |
1035 | } | |
1036 | ||
1037 | void | |
80ed523f | 1038 | _TIFFNoPostDecode(TIFF* tif, uint8* buf, tmsize_t cc) |
8414a40c VZ |
1039 | { |
1040 | (void) tif; (void) buf; (void) cc; | |
1041 | } | |
1042 | ||
1043 | void | |
80ed523f | 1044 | _TIFFSwab16BitData(TIFF* tif, uint8* buf, tmsize_t cc) |
8414a40c VZ |
1045 | { |
1046 | (void) tif; | |
1047 | assert((cc & 1) == 0); | |
1048 | TIFFSwabArrayOfShort((uint16*) buf, cc/2); | |
1049 | } | |
1050 | ||
1051 | void | |
80ed523f | 1052 | _TIFFSwab24BitData(TIFF* tif, uint8* buf, tmsize_t cc) |
8414a40c VZ |
1053 | { |
1054 | (void) tif; | |
1055 | assert((cc % 3) == 0); | |
1056 | TIFFSwabArrayOfTriples((uint8*) buf, cc/3); | |
1057 | } | |
1058 | ||
1059 | void | |
80ed523f | 1060 | _TIFFSwab32BitData(TIFF* tif, uint8* buf, tmsize_t cc) |
8414a40c VZ |
1061 | { |
1062 | (void) tif; | |
1063 | assert((cc & 3) == 0); | |
1064 | TIFFSwabArrayOfLong((uint32*) buf, cc/4); | |
1065 | } | |
1066 | ||
1067 | void | |
80ed523f | 1068 | _TIFFSwab64BitData(TIFF* tif, uint8* buf, tmsize_t cc) |
8414a40c VZ |
1069 | { |
1070 | (void) tif; | |
1071 | assert((cc & 7) == 0); | |
1072 | TIFFSwabArrayOfDouble((double*) buf, cc/8); | |
1073 | } | |
1074 | ||
1075 | /* vim: set ts=8 sts=8 sw=8 noet: */ | |
80ed523f VZ |
1076 | /* |
1077 | * Local Variables: | |
1078 | * mode: c | |
1079 | * c-basic-offset: 8 | |
1080 | * fill-column: 78 | |
1081 | * End: | |
1082 | */ |