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