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