1 /* $Header: /usr/people/sam/tiff/libtiff/RCS/tif_read.c,v 1.62 1994/09/17 23:22:37 sam Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994 Sam Leffler
5 * Copyright (c) 1991, 1992, 1993, 1994 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
TIFFSeek(TIFF * tif,uint32 row,tsample_t sample)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 if (tif->tif_seek) {
85 /*
86 * Seek forward to the desired row.
87 */
88 if (!(*tif->tif_seek)(tif, row - tif->tif_row))
89 return (0);
90 tif->tif_row = row;
91 } else {
92 TIFFError(tif->tif_name,
93 "Compression algorithm does not support random access");
94 return (0);
95 }
96 }
97 return (1);
98 }
99
100 int
TIFFReadScanline(TIFF * tif,tdata_t buf,uint32 row,tsample_t sample)101 TIFFReadScanline(TIFF* tif, tdata_t buf, uint32 row, tsample_t sample)
102 {
103 int e;
104
105 if (!TIFFCheckRead(tif, 0))
106 return (-1);
107 if (e = TIFFSeek(tif, row, sample)) {
108 /*
109 * Decompress desired row into user buffer.
110 */
111 e = (*tif->tif_decoderow)
112 (tif, (tidata_t) buf, tif->tif_scanlinesize, sample);
113 tif->tif_row++;
114 if (e)
115 (*tif->tif_postdecode)(tif, (tidata_t) buf,
116 tif->tif_scanlinesize);
117 }
118 return (e ? 1 : -1);
119 }
120
121 /*
122 * Read a strip of data and decompress the specified
123 * amount into the user-supplied buffer.
124 */
125 tsize_t
TIFFReadEncodedStrip(TIFF * tif,tstrip_t strip,tdata_t buf,tsize_t size)126 TIFFReadEncodedStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
127 {
128 TIFFDirectory *td = &tif->tif_dir;
129 uint32 nrows;
130 tsize_t stripsize;
131
132 if (!TIFFCheckRead(tif, 0))
133 return (-1);
134 if (strip >= td->td_nstrips) {
135 TIFFError(tif->tif_name, "%ld: Strip out of range, max %ld",
136 (long) strip, (long) td->td_nstrips);
137 return (-1);
138 }
139 /*
140 * Calculate the strip size according to the number of
141 * rows in the strip (check for truncated last strip).
142 */
143 if (strip != td->td_nstrips-1 ||
144 (nrows = td->td_imagelength % td->td_rowsperstrip) == 0)
145 nrows = td->td_rowsperstrip;
146 stripsize = TIFFVStripSize(tif, nrows);
147 if (size == (tsize_t) -1)
148 size = stripsize;
149 else if (size > stripsize)
150 size = stripsize;
151 if (TIFFFillStrip(tif, strip) && (*tif->tif_decodestrip)(tif,
152 (tidata_t) buf, size, (tsample_t)(strip / td->td_stripsperimage))) {
153 (*tif->tif_postdecode)(tif, (tidata_t) buf, size);
154 return (size);
155 } else
156 return ((tsize_t) -1);
157 }
158
159 static tsize_t
TIFFReadRawStrip1(TIFF * tif,tstrip_t strip,tdata_t buf,tsize_t size,const char * module)160 TIFFReadRawStrip1(TIFF* tif,
161 tstrip_t strip, tdata_t buf, tsize_t size, const char* module)
162 {
163 TIFFDirectory *td = &tif->tif_dir;
164
165 if (!isMapped(tif)) {
166 if (!SeekOK(tif, td->td_stripoffset[strip])) {
167 TIFFError(module,
168 "%s: Seek error at scanline %lu, strip %lu",
169 tif->tif_name,
170 (u_long) tif->tif_row, (u_long) strip);
171 return (-1);
172 }
173 if (!ReadOK(tif, buf, size)) {
174 TIFFError(module, "%s: Read error at scanline %lu",
175 tif->tif_name, (u_long) tif->tif_row);
176 return (-1);
177 }
178 } else {
179 if (td->td_stripoffset[strip] + size > tif->tif_size) {
180 TIFFError(module,
181 "%s: Seek error at scanline %lu, strip %lu",
182 tif->tif_name,
183 (u_long) tif->tif_row, (u_long) strip);
184 return (-1);
185 }
186 _TIFFmemcpy(buf, tif->tif_base + td->td_stripoffset[strip], size);
187 }
188 return (size);
189 }
190
191 /*
192 * Read a strip of data from the file.
193 */
194 tsize_t
TIFFReadRawStrip(TIFF * tif,tstrip_t strip,tdata_t buf,tsize_t size)195 TIFFReadRawStrip(TIFF* tif, tstrip_t strip, tdata_t buf, tsize_t size)
196 {
197 static const char module[] = "TIFFReadRawStrip";
198 TIFFDirectory *td = &tif->tif_dir;
199 tsize_t bytecount;
200
201 if (!TIFFCheckRead(tif, 0))
202 return ((tsize_t) -1);
203 if (strip >= td->td_nstrips) {
204 TIFFError(tif->tif_name, "%lu: Strip out of range, max %lu",
205 (u_long) strip, (u_long) td->td_nstrips);
206 return ((tsize_t) -1);
207 }
208 bytecount = td->td_stripbytecount[strip];
209 if (bytecount <= 0) {
210 TIFFError(tif->tif_name,
211 "%lu: Invalid strip byte count, strip %lu",
212 (u_long) bytecount, (u_long) strip);
213 return ((tsize_t) -1);
214 }
215 if (size != (tsize_t)-1 && size < bytecount)
216 bytecount = size;
217 return (TIFFReadRawStrip1(tif, strip, buf, bytecount, module));
218 }
219
220 /*
221 * Read the specified strip and setup for decoding.
222 * The data buffer is expanded, as necessary, to
223 * hold the strip's data.
224 */
225 static int
TIFFFillStrip(TIFF * tif,tstrip_t strip)226 TIFFFillStrip(TIFF* tif, tstrip_t strip)
227 {
228 static const char module[] = "TIFFFillStrip";
229 TIFFDirectory *td = &tif->tif_dir;
230 tsize_t bytecount;
231
232 bytecount = td->td_stripbytecount[strip];
233 if (bytecount <= 0) {
234 TIFFError(tif->tif_name,
235 "%lu: Invalid strip byte count, strip %lu",
236 (u_long) bytecount, (u_long) strip);
237 return (0);
238 }
239 if (isMapped(tif) &&
240 (td->td_fillorder == tif->tif_fillorder || (tif->tif_flags & TIFF_NOBITREV))) {
241 /*
242 * The image is mapped into memory and we either don't
243 * need to flip bits or the compression routine is going
244 * to handle this operation itself. In this case, avoid
245 * copying the raw data and instead just reference the
246 * data from the memory mapped file image. This assumes
247 * that the decompression routines do not modify the
248 * contents of the raw data buffer (if they try to,
249 * the application will get a fault since the file is
250 * mapped read-only).
251 */
252 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
253 _TIFFfree(tif->tif_rawdata);
254 tif->tif_flags &= ~TIFF_MYBUFFER;
255 if (td->td_stripoffset[strip] + bytecount > tif->tif_size) {
256 /*
257 * This error message might seem strange, but it's
258 * what would happen if a read were done instead.
259 */
260 TIFFError(module, "%s: Read error on strip %lu",
261 tif->tif_name, (u_long) strip);
262 tif->tif_curstrip = NOSTRIP;
263 return (0);
264 }
265 tif->tif_rawdatasize = bytecount;
266 tif->tif_rawdata = tif->tif_base + td->td_stripoffset[strip];
267 } else {
268 /*
269 * Expand raw data buffer, if needed, to
270 * hold data strip coming from file
271 * (perhaps should set upper bound on
272 * the size of a buffer we'll use?).
273 */
274 if (bytecount > tif->tif_rawdatasize) {
275 tif->tif_curstrip = NOSTRIP;
276 if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
277 TIFFError(module,
278 "%s: Data buffer too small to hold strip %lu",
279 tif->tif_name, (u_long) strip);
280 return (0);
281 }
282 if (!TIFFReadBufferSetup(tif, 0,
283 roundup(bytecount, 1024)))
284 return (0);
285 }
286 if (TIFFReadRawStrip1(tif, strip, (u_char *)tif->tif_rawdata,
287 bytecount, module) != bytecount)
288 return (0);
289 if (td->td_fillorder != tif->tif_fillorder &&
290 (tif->tif_flags & TIFF_NOBITREV) == 0)
291 TIFFReverseBits(tif->tif_rawdata, bytecount);
292 }
293 return (TIFFStartStrip(tif, strip));
294 }
295
296 /*
297 * Tile-oriented Read Support
298 * Contributed by Nancy Cam (Silicon Graphics).
299 */
300
301 /*
302 * Read and decompress a tile of data. The
303 * tile is selected by the (x,y,z,s) coordinates.
304 */
305 tsize_t
TIFFReadTile(TIFF * tif,tdata_t buf,uint32 x,uint32 y,uint32 z,tsample_t s)306 TIFFReadTile(TIFF* tif,
307 tdata_t buf, uint32 x, uint32 y, uint32 z, tsample_t s)
308 {
309 if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
310 return (-1);
311 return (TIFFReadEncodedTile(tif,
312 TIFFComputeTile(tif, x, y, z, s), buf, (tsize_t) -1));
313 }
314
315 /*
316 * Read a tile of data and decompress the specified
317 * amount into the user-supplied buffer.
318 */
319 tsize_t
TIFFReadEncodedTile(TIFF * tif,ttile_t tile,tdata_t buf,tsize_t size)320 TIFFReadEncodedTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
321 {
322 TIFFDirectory *td = &tif->tif_dir;
323 tsize_t tilesize = tif->tif_tilesize;
324
325 if (!TIFFCheckRead(tif, 1))
326 return (-1);
327 if (tile >= td->td_nstrips) {
328 TIFFError(tif->tif_name, "%ld: Tile out of range, max %ld",
329 (long) tile, (u_long) td->td_nstrips);
330 return (-1);
331 }
332 if (size == (tsize_t) -1)
333 size = tilesize;
334 else if (size > tilesize)
335 size = tilesize;
336 if (TIFFFillTile(tif, tile) && (*tif->tif_decodetile)(tif,
337 (tidata_t) buf, size, (tsample_t)(tile/td->td_stripsperimage))) {
338 (*tif->tif_postdecode)(tif, (tidata_t) buf, size);
339 return (size);
340 } else
341 return (-1);
342 }
343
344 static tsize_t
TIFFReadRawTile1(TIFF * tif,ttile_t tile,tdata_t buf,tsize_t size,const char * module)345 TIFFReadRawTile1(TIFF* tif,
346 ttile_t tile, tdata_t buf, tsize_t size, const char* module)
347 {
348 TIFFDirectory *td = &tif->tif_dir;
349
350 if (!isMapped(tif)) {
351 if (!SeekOK(tif, td->td_stripoffset[tile])) {
352 TIFFError(module,
353 "%s: Seek error at row %ld, col %ld, tile %ld",
354 tif->tif_name,
355 (long) tif->tif_row,
356 (long) tif->tif_col,
357 (long) tile);
358 return ((tsize_t) -1);
359 }
360 if (!ReadOK(tif, buf, size)) {
361 TIFFError(module, "%s: Read error at row %ld, col %ld",
362 tif->tif_name,
363 (long) tif->tif_row,
364 (long) tif->tif_col);
365 return ((tsize_t) -1);
366 }
367 } else {
368 if (td->td_stripoffset[tile] + size > tif->tif_size) {
369 TIFFError(module,
370 "%s: Seek error at row %ld, col %ld, tile %ld",
371 tif->tif_name,
372 (long) tif->tif_row,
373 (long) tif->tif_col,
374 (long) tile);
375 return ((tsize_t) -1);
376 }
377 _TIFFmemcpy(buf, tif->tif_base + td->td_stripoffset[tile], size);
378 }
379 return (size);
380 }
381
382 /*
383 * Read a tile of data from the file.
384 */
385 tsize_t
TIFFReadRawTile(TIFF * tif,ttile_t tile,tdata_t buf,tsize_t size)386 TIFFReadRawTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
387 {
388 static const char module[] = "TIFFReadRawTile";
389 TIFFDirectory *td = &tif->tif_dir;
390 tsize_t bytecount;
391
392 if (!TIFFCheckRead(tif, 1))
393 return ((tsize_t) -1);
394 if (tile >= td->td_nstrips) {
395 TIFFError(tif->tif_name, "%lu: Tile out of range, max %lu",
396 (u_long) tile, (u_long) td->td_nstrips);
397 return ((tsize_t) -1);
398 }
399 bytecount = td->td_stripbytecount[tile];
400 if (size != (tsize_t) -1 && size < bytecount)
401 bytecount = size;
402 return (TIFFReadRawTile1(tif, tile, buf, bytecount, module));
403 }
404
405 /*
406 * Read the specified tile and setup for decoding.
407 * The data buffer is expanded, as necessary, to
408 * hold the tile's data.
409 */
410 static int
TIFFFillTile(TIFF * tif,ttile_t tile)411 TIFFFillTile(TIFF* tif, ttile_t tile)
412 {
413 static const char module[] = "TIFFFillTile";
414 TIFFDirectory *td = &tif->tif_dir;
415 tsize_t bytecount;
416
417 bytecount = td->td_stripbytecount[tile];
418 if (bytecount <= 0) {
419 TIFFError(tif->tif_name,
420 "%lu: Invalid tile byte count, tile %lu",
421 (u_long) bytecount, (u_long) tile);
422 return (0);
423 }
424 if (isMapped(tif) &&
425 (td->td_fillorder == tif->tif_fillorder || (tif->tif_flags & TIFF_NOBITREV))) {
426 /*
427 * The image is mapped into memory and we either don't
428 * need to flip bits or the compression routine is going
429 * to handle this operation itself. In this case, avoid
430 * copying the raw data and instead just reference the
431 * data from the memory mapped file image. This assumes
432 * that the decompression routines do not modify the
433 * contents of the raw data buffer (if they try to,
434 * the application will get a fault since the file is
435 * mapped read-only).
436 */
437 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
438 _TIFFfree(tif->tif_rawdata);
439 tif->tif_flags &= ~TIFF_MYBUFFER;
440 if (td->td_stripoffset[tile] + bytecount > tif->tif_size) {
441 tif->tif_curtile = NOTILE;
442 return (0);
443 }
444 tif->tif_rawdatasize = bytecount;
445 tif->tif_rawdata = tif->tif_base + td->td_stripoffset[tile];
446 } else {
447 /*
448 * Expand raw data buffer, if needed, to
449 * hold data tile coming from file
450 * (perhaps should set upper bound on
451 * the size of a buffer we'll use?).
452 */
453 if (bytecount > tif->tif_rawdatasize) {
454 tif->tif_curtile = NOTILE;
455 if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
456 TIFFError(module,
457 "%s: Data buffer too small to hold tile %ld",
458 tif->tif_name, (long) tile);
459 return (0);
460 }
461 if (!TIFFReadBufferSetup(tif, 0,
462 roundup(bytecount, 1024)))
463 return (0);
464 }
465 if (TIFFReadRawTile1(tif, tile, (u_char *)tif->tif_rawdata,
466 bytecount, module) != bytecount)
467 return (0);
468 if (td->td_fillorder != tif->tif_fillorder &&
469 (tif->tif_flags & TIFF_NOBITREV) == 0)
470 TIFFReverseBits(tif->tif_rawdata, bytecount);
471 }
472 return (TIFFStartTile(tif, tile));
473 }
474
475 /*
476 * Setup the raw data buffer in preparation for
477 * reading a strip of raw data. If the buffer
478 * is specified as zero, then a buffer of appropriate
479 * size is allocated by the library. Otherwise,
480 * the client must guarantee that the buffer is
481 * large enough to hold any individual strip of
482 * raw data.
483 */
484 int
TIFFReadBufferSetup(TIFF * tif,tdata_t bp,tsize_t size)485 TIFFReadBufferSetup(TIFF* tif, tdata_t bp, tsize_t size)
486 {
487 static const char module[] = "TIFFReadBufferSetup";
488
489 if (tif->tif_rawdata) {
490 if (tif->tif_flags & TIFF_MYBUFFER)
491 _TIFFfree(tif->tif_rawdata);
492 tif->tif_rawdata = NULL;
493 }
494 if (bp) {
495 tif->tif_rawdatasize = size;
496 tif->tif_rawdata = (tidata_t) bp;
497 tif->tif_flags &= ~TIFF_MYBUFFER;
498 } else {
499 tif->tif_rawdatasize = roundup(size, 1024);
500 tif->tif_rawdata = (tidata_t) _TIFFmalloc(tif->tif_rawdatasize);
501 tif->tif_flags |= TIFF_MYBUFFER;
502 }
503 if (tif->tif_rawdata == NULL) {
504 TIFFError(module,
505 "%s: No space for data buffer at scanline %ld",
506 tif->tif_name, (long) tif->tif_row);
507 tif->tif_rawdatasize = 0;
508 return (0);
509 }
510 return (1);
511 }
512
513 /*
514 * Set state to appear as if a
515 * strip has just been read in.
516 */
517 static int
TIFFStartStrip(TIFF * tif,tstrip_t strip)518 TIFFStartStrip(TIFF* tif, tstrip_t strip)
519 {
520 TIFFDirectory *td = &tif->tif_dir;
521
522 tif->tif_curstrip = strip;
523 tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
524 tif->tif_rawcp = tif->tif_rawdata;
525 tif->tif_rawcc = td->td_stripbytecount[strip];
526 return (tif->tif_predecode == NULL || (*tif->tif_predecode)(tif));
527 }
528
529 /*
530 * Set state to appear as if a
531 * tile has just been read in.
532 */
533 static int
TIFFStartTile(TIFF * tif,ttile_t tile)534 TIFFStartTile(TIFF* tif, ttile_t tile)
535 {
536 TIFFDirectory *td = &tif->tif_dir;
537
538 tif->tif_curtile = tile;
539 tif->tif_row =
540 (tile % howmany(td->td_imagewidth, td->td_tilewidth)) *
541 td->td_tilelength;
542 tif->tif_col =
543 (tile % howmany(td->td_imagelength, td->td_tilelength)) *
544 td->td_tilewidth;
545 tif->tif_rawcp = tif->tif_rawdata;
546 tif->tif_rawcc = td->td_stripbytecount[tile];
547 return (tif->tif_predecode == NULL || (*tif->tif_predecode)(tif));
548 }
549
550 static int
TIFFCheckRead(TIFF * tif,int tiles)551 TIFFCheckRead(TIFF* tif, int tiles)
552 {
553 if (tif->tif_mode == O_WRONLY) {
554 TIFFError(tif->tif_name, "File not open for reading");
555 return (0);
556 }
557 if (tiles ^ isTiled(tif)) {
558 TIFFError(tif->tif_name, tiles ?
559 "Can not read tiles from a stripped image" :
560 "Can not read scanlines from a tiled image");
561 return (0);
562 }
563 return (1);
564 }
565
566 void
TIFFNoPostDecode(TIFF * tif,tidata_t buf,tsize_t cc)567 TIFFNoPostDecode(TIFF* tif, tidata_t buf, tsize_t cc)
568 {
569 }
570
571 void
TIFFSwab16BitData(TIFF * tif,tidata_t buf,tsize_t cc)572 TIFFSwab16BitData(TIFF* tif, tidata_t buf, tsize_t cc)
573 {
574 assert((cc & 1) == 0);
575 TIFFSwabArrayOfShort((uint16*) buf, cc/2);
576 }
577
578 void
TIFFSwab32BitData(TIFF * tif,tidata_t buf,tsize_t cc)579 TIFFSwab32BitData(TIFF* tif, tidata_t buf, tsize_t cc)
580 {
581 assert((cc & 3) == 0);
582 TIFFSwabArrayOfLong((uint32*) buf, cc/4);
583 }
584