1 | /* $Header: /afs/dev.mit.edu/source/repository/third/tiff/libtiff/tif_read.c,v 1.1.1.1 2002-12-26 02:38:30 ghudson Exp $ */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (c) 1988-1997 Sam Leffler |
---|
5 | * Copyright (c) 1991-1997 Silicon Graphics, Inc. |
---|
6 | * |
---|
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. |
---|
14 | * |
---|
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. |
---|
18 | * |
---|
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 |
---|
24 | * OF THIS SOFTWARE. |
---|
25 | */ |
---|
26 | |
---|
27 | /* |
---|
28 | * TIFF Library. |
---|
29 | * Scanline-oriented Read Support |
---|
30 | */ |
---|
31 | #include "tiffiop.h" |
---|
32 | #include <stdio.h> |
---|
33 | #include <assert.h> |
---|
34 | |
---|
35 | static int TIFFFillStrip(TIFF*, tstrip_t); |
---|
36 | static int TIFFFillTile(TIFF*, ttile_t); |
---|
37 | static int TIFFStartStrip(TIFF*, tstrip_t); |
---|
38 | static int TIFFStartTile(TIFF*, ttile_t); |
---|
39 | static int TIFFCheckRead(TIFF*, int); |
---|
40 | |
---|
41 | #define NOSTRIP ((tstrip_t) -1) /* undefined state */ |
---|
42 | #define NOTILE ((ttile_t) -1) /* undefined state */ |
---|
43 | |
---|
44 | /* |
---|
45 | * Seek to a random row+sample in a file. |
---|
46 | */ |
---|
47 | static int |
---|
48 | TIFFSeek(TIFF* tif, uint32 row, tsample_t sample) |
---|
49 | { |
---|
50 | register TIFFDirectory *td = &tif->tif_dir; |
---|
51 | tstrip_t strip; |
---|
52 | |
---|
53 | if (row >= td->td_imagelength) { /* out of range */ |
---|
54 | TIFFError(tif->tif_name, "%lu: Row out of range, max %lu", |
---|
55 | (u_long) row, (u_long) td->td_imagelength); |
---|
56 | return (0); |
---|
57 | } |
---|
58 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) { |
---|
59 | if (sample >= td->td_samplesperpixel) { |
---|
60 | TIFFError(tif->tif_name, |
---|
61 | "%lu: Sample out of range, max %lu", |
---|
62 | (u_long) sample, (u_long) td->td_samplesperpixel); |
---|
63 | return (0); |
---|
64 | } |
---|
65 | strip = sample*td->td_stripsperimage + row/td->td_rowsperstrip; |
---|
66 | } else |
---|
67 | strip = row / td->td_rowsperstrip; |
---|
68 | if (strip != tif->tif_curstrip) { /* different strip, refill */ |
---|
69 | if (!TIFFFillStrip(tif, strip)) |
---|
70 | return (0); |
---|
71 | } else if (row < tif->tif_row) { |
---|
72 | /* |
---|
73 | * Moving backwards within the same strip: backup |
---|
74 | * to the start and then decode forward (below). |
---|
75 | * |
---|
76 | * NB: If you're planning on lots of random access within a |
---|
77 | * strip, it's better to just read and decode the entire |
---|
78 | * strip, and then access the decoded data in a random fashion. |
---|
79 | */ |
---|
80 | if (!TIFFStartStrip(tif, strip)) |
---|
81 | return (0); |
---|
82 | } |
---|
83 | if (row != tif->tif_row) { |
---|
84 | /* |
---|
85 | * Seek forward to the desired row. |
---|
86 | */ |
---|
87 | if (!(*tif->tif_seek)(tif, row - tif->tif_row)) |
---|
88 | return (0); |
---|
89 | tif->tif_row = row; |
---|
90 | } |
---|
91 | return (1); |
---|
92 | } |
---|
93 | |
---|
94 | int |
---|
95 | TIFFReadScanline(TIFF* tif, tdata_t buf, uint32 row, tsample_t sample) |
---|
96 | { |
---|
97 | int e; |
---|
98 | |
---|
99 | if (!TIFFCheckRead(tif, 0)) |
---|
100 | return (-1); |
---|
101 | if( (e = TIFFSeek(tif, row, sample)) != 0) { |
---|
102 | /* |
---|
103 | * Decompress desired row into user buffer. |
---|
104 | */ |
---|
105 | e = (*tif->tif_decoderow) |
---|
106 | (tif, (tidata_t) buf, tif->tif_scanlinesize, sample); |
---|
107 | tif->tif_row++; |
---|
108 | if (e) |
---|
109 | (*tif->tif_postdecode)(tif, (tidata_t) buf, |
---|
110 | tif->tif_scanlinesize); |
---|
111 | } |
---|
112 | return (e > 0 ? 1 : -1); |
---|
113 | } |
---|
114 | |
---|
115 | /* |
---|
116 | * Read a strip of data and decompress the specified |
---|
117 | * amount into the user-supplied buffer. |
---|
118 | */ |
---|
119 | tsize_t |
---|
120 | TIFFReadEncodedStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size) |
---|
121 | { |
---|
122 | TIFFDirectory *td = &tif->tif_dir; |
---|
123 | uint32 nrows; |
---|
124 | tsize_t stripsize; |
---|
125 | tstrip_t sep_strip, strips_per_sep; |
---|
126 | |
---|
127 | if (!TIFFCheckRead(tif, 0)) |
---|
128 | return (-1); |
---|
129 | if (strip >= td->td_nstrips) { |
---|
130 | TIFFError(tif->tif_name, "%ld: Strip out of range, max %ld", |
---|
131 | (long) strip, (long) td->td_nstrips); |
---|
132 | return (-1); |
---|
133 | } |
---|
134 | /* |
---|
135 | * Calculate the strip size according to the number of |
---|
136 | * rows in the strip (check for truncated last strip on any |
---|
137 | * of the separations). |
---|
138 | */ |
---|
139 | if( td->td_rowsperstrip >= td->td_imagelength ) |
---|
140 | strips_per_sep = 1; |
---|
141 | else |
---|
142 | strips_per_sep = (td->td_imagelength+td->td_rowsperstrip-1) |
---|
143 | / td->td_rowsperstrip; |
---|
144 | |
---|
145 | sep_strip = strip % strips_per_sep; |
---|
146 | |
---|
147 | if (sep_strip != strips_per_sep-1 || |
---|
148 | (nrows = td->td_imagelength % td->td_rowsperstrip) == 0) |
---|
149 | nrows = td->td_rowsperstrip; |
---|
150 | |
---|
151 | stripsize = TIFFVStripSize(tif, nrows); |
---|
152 | if (size == (tsize_t) -1) |
---|
153 | size = stripsize; |
---|
154 | else if (size > stripsize) |
---|
155 | size = stripsize; |
---|
156 | if (TIFFFillStrip(tif, strip) && (*tif->tif_decodestrip)(tif, |
---|
157 | (tidata_t) buf, size, (tsample_t)(strip / td->td_stripsperimage))) { |
---|
158 | (*tif->tif_postdecode)(tif, (tidata_t) buf, size); |
---|
159 | return (size); |
---|
160 | } else |
---|
161 | return ((tsize_t) -1); |
---|
162 | } |
---|
163 | |
---|
164 | static tsize_t |
---|
165 | TIFFReadRawStrip1(TIFF* tif, |
---|
166 | tstrip_t strip, tdata_t buf, tsize_t size, const char* module) |
---|
167 | { |
---|
168 | TIFFDirectory *td = &tif->tif_dir; |
---|
169 | |
---|
170 | if (!isMapped(tif)) { |
---|
171 | tsize_t cc; |
---|
172 | |
---|
173 | if (!SeekOK(tif, td->td_stripoffset[strip])) { |
---|
174 | TIFFError(module, |
---|
175 | "%s: Seek error at scanline %lu, strip %lu", |
---|
176 | tif->tif_name, |
---|
177 | (u_long) tif->tif_row, (u_long) strip); |
---|
178 | return (-1); |
---|
179 | } |
---|
180 | cc = TIFFReadFile(tif, buf, size); |
---|
181 | if (cc != size) { |
---|
182 | TIFFError(module, |
---|
183 | "%s: Read error at scanline %lu; got %lu bytes, expected %lu", |
---|
184 | tif->tif_name, |
---|
185 | (u_long) tif->tif_row, |
---|
186 | (u_long) cc, |
---|
187 | (u_long) size); |
---|
188 | return (-1); |
---|
189 | } |
---|
190 | } else { |
---|
191 | if (td->td_stripoffset[strip] + size > tif->tif_size) { |
---|
192 | TIFFError(module, |
---|
193 | "%s: Read error at scanline %lu, strip %lu; got %lu bytes, expected %lu", |
---|
194 | tif->tif_name, |
---|
195 | (u_long) tif->tif_row, |
---|
196 | (u_long) strip, |
---|
197 | (u_long) tif->tif_size - td->td_stripoffset[strip], |
---|
198 | (u_long) size); |
---|
199 | return (-1); |
---|
200 | } |
---|
201 | _TIFFmemcpy(buf, tif->tif_base + td->td_stripoffset[strip], size); |
---|
202 | } |
---|
203 | return (size); |
---|
204 | } |
---|
205 | |
---|
206 | /* |
---|
207 | * Read a strip of data from the file. |
---|
208 | */ |
---|
209 | tsize_t |
---|
210 | TIFFReadRawStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size) |
---|
211 | { |
---|
212 | static const char module[] = "TIFFReadRawStrip"; |
---|
213 | TIFFDirectory *td = &tif->tif_dir; |
---|
214 | tsize_t bytecount; |
---|
215 | |
---|
216 | if (!TIFFCheckRead(tif, 0)) |
---|
217 | return ((tsize_t) -1); |
---|
218 | if (strip >= td->td_nstrips) { |
---|
219 | TIFFError(tif->tif_name, "%lu: Strip out of range, max %lu", |
---|
220 | (u_long) strip, (u_long) td->td_nstrips); |
---|
221 | return ((tsize_t) -1); |
---|
222 | } |
---|
223 | bytecount = td->td_stripbytecount[strip]; |
---|
224 | if (bytecount <= 0) { |
---|
225 | TIFFError(tif->tif_name, |
---|
226 | "%lu: Invalid strip byte count, strip %lu", |
---|
227 | (u_long) bytecount, (u_long) strip); |
---|
228 | return ((tsize_t) -1); |
---|
229 | } |
---|
230 | if (size != (tsize_t)-1 && size < bytecount) |
---|
231 | bytecount = size; |
---|
232 | return (TIFFReadRawStrip1(tif, strip, buf, bytecount, module)); |
---|
233 | } |
---|
234 | |
---|
235 | /* |
---|
236 | * Read the specified strip and setup for decoding. |
---|
237 | * The data buffer is expanded, as necessary, to |
---|
238 | * hold the strip's data. |
---|
239 | */ |
---|
240 | static int |
---|
241 | TIFFFillStrip(TIFF* tif, tstrip_t strip) |
---|
242 | { |
---|
243 | static const char module[] = "TIFFFillStrip"; |
---|
244 | TIFFDirectory *td = &tif->tif_dir; |
---|
245 | tsize_t bytecount; |
---|
246 | |
---|
247 | bytecount = td->td_stripbytecount[strip]; |
---|
248 | if (bytecount <= 0) { |
---|
249 | TIFFError(tif->tif_name, |
---|
250 | "%lu: Invalid strip byte count, strip %lu", |
---|
251 | (u_long) bytecount, (u_long) strip); |
---|
252 | return (0); |
---|
253 | } |
---|
254 | if (isMapped(tif) && |
---|
255 | (isFillOrder(tif, td->td_fillorder) || (tif->tif_flags & TIFF_NOBITREV))) { |
---|
256 | /* |
---|
257 | * The image is mapped into memory and we either don't |
---|
258 | * need to flip bits or the compression routine is going |
---|
259 | * to handle this operation itself. In this case, avoid |
---|
260 | * copying the raw data and instead just reference the |
---|
261 | * data from the memory mapped file image. This assumes |
---|
262 | * that the decompression routines do not modify the |
---|
263 | * contents of the raw data buffer (if they try to, |
---|
264 | * the application will get a fault since the file is |
---|
265 | * mapped read-only). |
---|
266 | */ |
---|
267 | if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) |
---|
268 | _TIFFfree(tif->tif_rawdata); |
---|
269 | tif->tif_flags &= ~TIFF_MYBUFFER; |
---|
270 | if ( td->td_stripoffset[strip] + bytecount > tif->tif_size) { |
---|
271 | /* |
---|
272 | * This error message might seem strange, but it's |
---|
273 | * what would happen if a read were done instead. |
---|
274 | */ |
---|
275 | TIFFError(module, |
---|
276 | "%s: Read error on strip %lu; got %lu bytes, expected %lu", |
---|
277 | tif->tif_name, |
---|
278 | (u_long) strip, |
---|
279 | (u_long) tif->tif_size - td->td_stripoffset[strip], |
---|
280 | (u_long) bytecount); |
---|
281 | tif->tif_curstrip = NOSTRIP; |
---|
282 | return (0); |
---|
283 | } |
---|
284 | tif->tif_rawdatasize = bytecount; |
---|
285 | tif->tif_rawdata = tif->tif_base + td->td_stripoffset[strip]; |
---|
286 | } else { |
---|
287 | /* |
---|
288 | * Expand raw data buffer, if needed, to |
---|
289 | * hold data strip coming from file |
---|
290 | * (perhaps should set upper bound on |
---|
291 | * the size of a buffer we'll use?). |
---|
292 | */ |
---|
293 | if (bytecount > tif->tif_rawdatasize) { |
---|
294 | tif->tif_curstrip = NOSTRIP; |
---|
295 | if ((tif->tif_flags & TIFF_MYBUFFER) == 0) { |
---|
296 | TIFFError(module, |
---|
297 | "%s: Data buffer too small to hold strip %lu", |
---|
298 | tif->tif_name, (u_long) strip); |
---|
299 | return (0); |
---|
300 | } |
---|
301 | if (!TIFFReadBufferSetup(tif, 0, |
---|
302 | TIFFroundup(bytecount, 1024))) |
---|
303 | return (0); |
---|
304 | } |
---|
305 | if (TIFFReadRawStrip1(tif, strip, (u_char *)tif->tif_rawdata, |
---|
306 | bytecount, module) != bytecount) |
---|
307 | return (0); |
---|
308 | if (!isFillOrder(tif, td->td_fillorder) && |
---|
309 | (tif->tif_flags & TIFF_NOBITREV) == 0) |
---|
310 | TIFFReverseBits(tif->tif_rawdata, bytecount); |
---|
311 | } |
---|
312 | return (TIFFStartStrip(tif, strip)); |
---|
313 | } |
---|
314 | |
---|
315 | /* |
---|
316 | * Tile-oriented Read Support |
---|
317 | * Contributed by Nancy Cam (Silicon Graphics). |
---|
318 | */ |
---|
319 | |
---|
320 | /* |
---|
321 | * Read and decompress a tile of data. The |
---|
322 | * tile is selected by the (x,y,z,s) coordinates. |
---|
323 | */ |
---|
324 | tsize_t |
---|
325 | TIFFReadTile(TIFF* tif, |
---|
326 | tdata_t buf, uint32 x, uint32 y, uint32 z, tsample_t s) |
---|
327 | { |
---|
328 | if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s)) |
---|
329 | return (-1); |
---|
330 | return (TIFFReadEncodedTile(tif, |
---|
331 | TIFFComputeTile(tif, x, y, z, s), buf, (tsize_t) -1)); |
---|
332 | } |
---|
333 | |
---|
334 | /* |
---|
335 | * Read a tile of data and decompress the specified |
---|
336 | * amount into the user-supplied buffer. |
---|
337 | */ |
---|
338 | tsize_t |
---|
339 | TIFFReadEncodedTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size) |
---|
340 | { |
---|
341 | TIFFDirectory *td = &tif->tif_dir; |
---|
342 | tsize_t tilesize = tif->tif_tilesize; |
---|
343 | |
---|
344 | if (!TIFFCheckRead(tif, 1)) |
---|
345 | return (-1); |
---|
346 | if (tile >= td->td_nstrips) { |
---|
347 | TIFFError(tif->tif_name, "%ld: Tile out of range, max %ld", |
---|
348 | (long) tile, (u_long) td->td_nstrips); |
---|
349 | return (-1); |
---|
350 | } |
---|
351 | if (size == (tsize_t) -1) |
---|
352 | size = tilesize; |
---|
353 | else if (size > tilesize) |
---|
354 | size = tilesize; |
---|
355 | if (TIFFFillTile(tif, tile) && (*tif->tif_decodetile)(tif, |
---|
356 | (tidata_t) buf, size, (tsample_t)(tile/td->td_stripsperimage))) { |
---|
357 | (*tif->tif_postdecode)(tif, (tidata_t) buf, size); |
---|
358 | return (size); |
---|
359 | } else |
---|
360 | return (-1); |
---|
361 | } |
---|
362 | |
---|
363 | static tsize_t |
---|
364 | TIFFReadRawTile1(TIFF* tif, |
---|
365 | ttile_t tile, tdata_t buf, tsize_t size, const char* module) |
---|
366 | { |
---|
367 | TIFFDirectory *td = &tif->tif_dir; |
---|
368 | |
---|
369 | if (!isMapped(tif)) { |
---|
370 | tsize_t cc; |
---|
371 | |
---|
372 | if (!SeekOK(tif, td->td_stripoffset[tile])) { |
---|
373 | TIFFError(module, |
---|
374 | "%s: Seek error at row %ld, col %ld, tile %ld", |
---|
375 | tif->tif_name, |
---|
376 | (long) tif->tif_row, |
---|
377 | (long) tif->tif_col, |
---|
378 | (long) tile); |
---|
379 | return ((tsize_t) -1); |
---|
380 | } |
---|
381 | cc = TIFFReadFile(tif, buf, size); |
---|
382 | if (cc != size) { |
---|
383 | TIFFError(module, |
---|
384 | "%s: Read error at row %ld, col %ld; got %lu bytes, expected %lu", |
---|
385 | tif->tif_name, |
---|
386 | (long) tif->tif_row, |
---|
387 | (long) tif->tif_col, |
---|
388 | (u_long) cc, |
---|
389 | (u_long) size); |
---|
390 | return ((tsize_t) -1); |
---|
391 | } |
---|
392 | } else { |
---|
393 | if (td->td_stripoffset[tile] + size > tif->tif_size) { |
---|
394 | TIFFError(module, |
---|
395 | "%s: Read error at row %ld, col %ld, tile %ld; got %lu bytes, expected %lu", |
---|
396 | tif->tif_name, |
---|
397 | (long) tif->tif_row, |
---|
398 | (long) tif->tif_col, |
---|
399 | (long) tile, |
---|
400 | (u_long) tif->tif_size - td->td_stripoffset[tile], |
---|
401 | (u_long) size); |
---|
402 | return ((tsize_t) -1); |
---|
403 | } |
---|
404 | _TIFFmemcpy(buf, tif->tif_base + td->td_stripoffset[tile], size); |
---|
405 | } |
---|
406 | return (size); |
---|
407 | } |
---|
408 | |
---|
409 | /* |
---|
410 | * Read a tile of data from the file. |
---|
411 | */ |
---|
412 | tsize_t |
---|
413 | TIFFReadRawTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size) |
---|
414 | { |
---|
415 | static const char module[] = "TIFFReadRawTile"; |
---|
416 | TIFFDirectory *td = &tif->tif_dir; |
---|
417 | tsize_t bytecount; |
---|
418 | |
---|
419 | if (!TIFFCheckRead(tif, 1)) |
---|
420 | return ((tsize_t) -1); |
---|
421 | if (tile >= td->td_nstrips) { |
---|
422 | TIFFError(tif->tif_name, "%lu: Tile out of range, max %lu", |
---|
423 | (u_long) tile, (u_long) td->td_nstrips); |
---|
424 | return ((tsize_t) -1); |
---|
425 | } |
---|
426 | bytecount = td->td_stripbytecount[tile]; |
---|
427 | if (size != (tsize_t) -1 && size < bytecount) |
---|
428 | bytecount = size; |
---|
429 | return (TIFFReadRawTile1(tif, tile, buf, bytecount, module)); |
---|
430 | } |
---|
431 | |
---|
432 | /* |
---|
433 | * Read the specified tile and setup for decoding. |
---|
434 | * The data buffer is expanded, as necessary, to |
---|
435 | * hold the tile's data. |
---|
436 | */ |
---|
437 | static int |
---|
438 | TIFFFillTile(TIFF* tif, ttile_t tile) |
---|
439 | { |
---|
440 | static const char module[] = "TIFFFillTile"; |
---|
441 | TIFFDirectory *td = &tif->tif_dir; |
---|
442 | tsize_t bytecount; |
---|
443 | |
---|
444 | bytecount = td->td_stripbytecount[tile]; |
---|
445 | if (bytecount <= 0) { |
---|
446 | TIFFError(tif->tif_name, |
---|
447 | "%lu: Invalid tile byte count, tile %lu", |
---|
448 | (u_long) bytecount, (u_long) tile); |
---|
449 | return (0); |
---|
450 | } |
---|
451 | if (isMapped(tif) && |
---|
452 | (isFillOrder(tif, td->td_fillorder) || (tif->tif_flags & TIFF_NOBITREV))) { |
---|
453 | /* |
---|
454 | * The image is mapped into memory and we either don't |
---|
455 | * need to flip bits or the compression routine is going |
---|
456 | * to handle this operation itself. In this case, avoid |
---|
457 | * copying the raw data and instead just reference the |
---|
458 | * data from the memory mapped file image. This assumes |
---|
459 | * that the decompression routines do not modify the |
---|
460 | * contents of the raw data buffer (if they try to, |
---|
461 | * the application will get a fault since the file is |
---|
462 | * mapped read-only). |
---|
463 | */ |
---|
464 | if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) |
---|
465 | _TIFFfree(tif->tif_rawdata); |
---|
466 | tif->tif_flags &= ~TIFF_MYBUFFER; |
---|
467 | if ( td->td_stripoffset[tile] + bytecount > tif->tif_size) { |
---|
468 | tif->tif_curtile = NOTILE; |
---|
469 | return (0); |
---|
470 | } |
---|
471 | tif->tif_rawdatasize = bytecount; |
---|
472 | tif->tif_rawdata = tif->tif_base + td->td_stripoffset[tile]; |
---|
473 | } else { |
---|
474 | /* |
---|
475 | * Expand raw data buffer, if needed, to |
---|
476 | * hold data tile coming from file |
---|
477 | * (perhaps should set upper bound on |
---|
478 | * the size of a buffer we'll use?). |
---|
479 | */ |
---|
480 | if (bytecount > tif->tif_rawdatasize) { |
---|
481 | tif->tif_curtile = NOTILE; |
---|
482 | if ((tif->tif_flags & TIFF_MYBUFFER) == 0) { |
---|
483 | TIFFError(module, |
---|
484 | "%s: Data buffer too small to hold tile %ld", |
---|
485 | tif->tif_name, (long) tile); |
---|
486 | return (0); |
---|
487 | } |
---|
488 | if (!TIFFReadBufferSetup(tif, 0, |
---|
489 | TIFFroundup(bytecount, 1024))) |
---|
490 | return (0); |
---|
491 | } |
---|
492 | if (TIFFReadRawTile1(tif, tile, (u_char *)tif->tif_rawdata, |
---|
493 | bytecount, module) != bytecount) |
---|
494 | return (0); |
---|
495 | if (!isFillOrder(tif, td->td_fillorder) && |
---|
496 | (tif->tif_flags & TIFF_NOBITREV) == 0) |
---|
497 | TIFFReverseBits(tif->tif_rawdata, bytecount); |
---|
498 | } |
---|
499 | return (TIFFStartTile(tif, tile)); |
---|
500 | } |
---|
501 | |
---|
502 | /* |
---|
503 | * Setup the raw data buffer in preparation for |
---|
504 | * reading a strip of raw data. If the buffer |
---|
505 | * is specified as zero, then a buffer of appropriate |
---|
506 | * size is allocated by the library. Otherwise, |
---|
507 | * the client must guarantee that the buffer is |
---|
508 | * large enough to hold any individual strip of |
---|
509 | * raw data. |
---|
510 | */ |
---|
511 | int |
---|
512 | TIFFReadBufferSetup(TIFF* tif, tdata_t bp, tsize_t size) |
---|
513 | { |
---|
514 | static const char module[] = "TIFFReadBufferSetup"; |
---|
515 | |
---|
516 | if (tif->tif_rawdata) { |
---|
517 | if (tif->tif_flags & TIFF_MYBUFFER) |
---|
518 | _TIFFfree(tif->tif_rawdata); |
---|
519 | tif->tif_rawdata = NULL; |
---|
520 | } |
---|
521 | if (bp) { |
---|
522 | tif->tif_rawdatasize = size; |
---|
523 | tif->tif_rawdata = (tidata_t) bp; |
---|
524 | tif->tif_flags &= ~TIFF_MYBUFFER; |
---|
525 | } else { |
---|
526 | tif->tif_rawdatasize = TIFFroundup(size, 1024); |
---|
527 | tif->tif_rawdata = (tidata_t) _TIFFmalloc(tif->tif_rawdatasize); |
---|
528 | tif->tif_flags |= TIFF_MYBUFFER; |
---|
529 | } |
---|
530 | if (tif->tif_rawdata == NULL) { |
---|
531 | TIFFError(module, |
---|
532 | "%s: No space for data buffer at scanline %ld", |
---|
533 | tif->tif_name, (long) tif->tif_row); |
---|
534 | tif->tif_rawdatasize = 0; |
---|
535 | return (0); |
---|
536 | } |
---|
537 | return (1); |
---|
538 | } |
---|
539 | |
---|
540 | /* |
---|
541 | * Set state to appear as if a |
---|
542 | * strip has just been read in. |
---|
543 | */ |
---|
544 | static int |
---|
545 | TIFFStartStrip(TIFF* tif, tstrip_t strip) |
---|
546 | { |
---|
547 | TIFFDirectory *td = &tif->tif_dir; |
---|
548 | |
---|
549 | if ((tif->tif_flags & TIFF_CODERSETUP) == 0) { |
---|
550 | if (!(*tif->tif_setupdecode)(tif)) |
---|
551 | return (0); |
---|
552 | tif->tif_flags |= TIFF_CODERSETUP; |
---|
553 | } |
---|
554 | tif->tif_curstrip = strip; |
---|
555 | tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; |
---|
556 | tif->tif_rawcp = tif->tif_rawdata; |
---|
557 | tif->tif_rawcc = td->td_stripbytecount[strip]; |
---|
558 | return ((*tif->tif_predecode)(tif, |
---|
559 | (tsample_t)(strip / td->td_stripsperimage))); |
---|
560 | } |
---|
561 | |
---|
562 | /* |
---|
563 | * Set state to appear as if a |
---|
564 | * tile has just been read in. |
---|
565 | */ |
---|
566 | static int |
---|
567 | TIFFStartTile(TIFF* tif, ttile_t tile) |
---|
568 | { |
---|
569 | TIFFDirectory *td = &tif->tif_dir; |
---|
570 | |
---|
571 | if ((tif->tif_flags & TIFF_CODERSETUP) == 0) { |
---|
572 | if (!(*tif->tif_setupdecode)(tif)) |
---|
573 | return (0); |
---|
574 | tif->tif_flags |= TIFF_CODERSETUP; |
---|
575 | } |
---|
576 | tif->tif_curtile = tile; |
---|
577 | tif->tif_row = |
---|
578 | (tile % TIFFhowmany(td->td_imagewidth, td->td_tilewidth)) * |
---|
579 | td->td_tilelength; |
---|
580 | tif->tif_col = |
---|
581 | (tile % TIFFhowmany(td->td_imagelength, td->td_tilelength)) * |
---|
582 | td->td_tilewidth; |
---|
583 | tif->tif_rawcp = tif->tif_rawdata; |
---|
584 | tif->tif_rawcc = td->td_stripbytecount[tile]; |
---|
585 | return ((*tif->tif_predecode)(tif, |
---|
586 | (tsample_t)(tile/td->td_stripsperimage))); |
---|
587 | } |
---|
588 | |
---|
589 | static int |
---|
590 | TIFFCheckRead(TIFF* tif, int tiles) |
---|
591 | { |
---|
592 | if (tif->tif_mode == O_WRONLY) { |
---|
593 | TIFFError(tif->tif_name, "File not open for reading"); |
---|
594 | return (0); |
---|
595 | } |
---|
596 | if (tiles ^ isTiled(tif)) { |
---|
597 | TIFFError(tif->tif_name, tiles ? |
---|
598 | "Can not read tiles from a stripped image" : |
---|
599 | "Can not read scanlines from a tiled image"); |
---|
600 | return (0); |
---|
601 | } |
---|
602 | return (1); |
---|
603 | } |
---|
604 | |
---|
605 | void |
---|
606 | _TIFFNoPostDecode(TIFF* tif, tidata_t buf, tsize_t cc) |
---|
607 | { |
---|
608 | (void) tif; (void) buf; (void) cc; |
---|
609 | } |
---|
610 | |
---|
611 | void |
---|
612 | _TIFFSwab16BitData(TIFF* tif, tidata_t buf, tsize_t cc) |
---|
613 | { |
---|
614 | (void) tif; |
---|
615 | assert((cc & 1) == 0); |
---|
616 | TIFFSwabArrayOfShort((uint16*) buf, cc/2); |
---|
617 | } |
---|
618 | |
---|
619 | void |
---|
620 | _TIFFSwab32BitData(TIFF* tif, tidata_t buf, tsize_t cc) |
---|
621 | { |
---|
622 | (void) tif; |
---|
623 | assert((cc & 3) == 0); |
---|
624 | TIFFSwabArrayOfLong((uint32*) buf, cc/4); |
---|
625 | } |
---|
626 | |
---|
627 | void |
---|
628 | _TIFFSwab64BitData(TIFF* tif, tidata_t buf, tsize_t cc) |
---|
629 | { |
---|
630 | (void) tif; |
---|
631 | assert((cc & 7) == 0); |
---|
632 | TIFFSwabArrayOfDouble((double*) buf, cc/8); |
---|
633 | } |
---|