Removed the libs directory containing win32 compiled versions of libpng, libtiff...
[openjpeg.git] / thirdparty / libtiff / tif_read.c
1 /* $Id: tif_read.c,v 1.16.2.3 2010-06-09 14:32:47 bfriesen 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
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
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,
54                              "%lu: Row out of range, max %lu",
55                              (unsigned long) row,
56                              (unsigned long) td->td_imagelength);
57                 return (0);
58         }
59         if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
60                 if (sample >= td->td_samplesperpixel) {
61                         TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
62                             "%lu: Sample out of range, max %lu",
63                             (unsigned long) sample, (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
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
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                 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
135                              "%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 on any
142          * of the separations).
143          */
144         if( td->td_rowsperstrip >= td->td_imagelength )
145                 strips_per_sep = 1;
146         else
147                 strips_per_sep = (td->td_imagelength+td->td_rowsperstrip-1)
148                     / td->td_rowsperstrip;
149
150         sep_strip = strip % strips_per_sep;
151
152         if (sep_strip != strips_per_sep-1 ||
153             (nrows = td->td_imagelength % td->td_rowsperstrip) == 0)
154                 nrows = td->td_rowsperstrip;
155
156         stripsize = TIFFVStripSize(tif, nrows);
157         if (size == (tsize_t) -1)
158                 size = stripsize;
159         else if (size > stripsize)
160                 size = stripsize;
161         if (TIFFFillStrip(tif, strip)
162             && (*tif->tif_decodestrip)(tif, (tidata_t) buf, size,   
163             (tsample_t)(strip / td->td_stripsperimage)) > 0 ) {
164                 (*tif->tif_postdecode)(tif, (tidata_t) buf, size);
165                 return (size);
166         } else
167                 return ((tsize_t) -1);
168 }
169
170 static tsize_t
171 TIFFReadRawStrip1(TIFF* tif,
172     tstrip_t strip, tdata_t buf, tsize_t size, const char* module)
173 {
174         TIFFDirectory *td = &tif->tif_dir;
175
176         assert((tif->tif_flags&TIFF_NOREADRAW)==0);
177         if (!isMapped(tif)) {
178                 tsize_t cc;
179
180                 if (!SeekOK(tif, td->td_stripoffset[strip])) {
181                         TIFFErrorExt(tif->tif_clientdata, module,
182                             "%s: Seek error at scanline %lu, strip %lu",
183                             tif->tif_name,
184                             (unsigned long) tif->tif_row, (unsigned long) strip);
185                         return (-1);
186                 }
187                 cc = TIFFReadFile(tif, buf, size);
188                 if (cc != size) {
189                         TIFFErrorExt(tif->tif_clientdata, module,
190                 "%s: Read error at scanline %lu; got %lu bytes, expected %lu",
191                             tif->tif_name,
192                             (unsigned long) tif->tif_row,
193                             (unsigned long) cc,
194                             (unsigned long) size);
195                         return (-1);
196                 }
197         } else {
198                 if (td->td_stripoffset[strip] + size > tif->tif_size) {
199                         TIFFErrorExt(tif->tif_clientdata, module,
200     "%s: Read error at scanline %lu, strip %lu; got %lu bytes, expected %lu",
201                             tif->tif_name,
202                             (unsigned long) tif->tif_row,
203                             (unsigned long) strip,
204                             (unsigned long) tif->tif_size - 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
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         /*
223          * FIXME: butecount should have tsize_t type, but for now libtiff
224          * defines tsize_t as a signed 32-bit integer and we are losing
225          * ability to read arrays larger than 2^31 bytes. So we are using
226          * uint32 instead of tsize_t here.
227          */
228         uint32 bytecount;
229
230         if (!TIFFCheckRead(tif, 0))
231                 return ((tsize_t) -1);
232         if (strip >= td->td_nstrips) {
233                 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
234                              "%lu: Strip out of range, max %lu",
235                              (unsigned long) strip,
236                              (unsigned long) td->td_nstrips);
237                 return ((tsize_t) -1);
238         }
239         if (tif->tif_flags&TIFF_NOREADRAW)
240         {
241                 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
242         "Compression scheme does not support access to raw uncompressed data");
243                 return ((tsize_t) -1);
244         }
245         bytecount = td->td_stripbytecount[strip];
246         if (bytecount <= 0) {
247                 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
248                     "%lu: Invalid strip byte count, strip %lu",
249                     (unsigned long) bytecount, (unsigned long) strip);
250                 return ((tsize_t) -1);
251         }
252         if (size != (tsize_t)-1 && (uint32)size < bytecount)
253                 bytecount = size;
254         return (TIFFReadRawStrip1(tif, strip, buf, bytecount, module));
255 }
256
257 /*
258  * Read the specified strip and setup for decoding. The data buffer is
259  * expanded, as necessary, to hold the strip's data.
260  */
261 int
262 TIFFFillStrip(TIFF* tif, tstrip_t strip)
263 {
264         static const char module[] = "TIFFFillStrip";
265         TIFFDirectory *td = &tif->tif_dir;
266
267         if ((tif->tif_flags&TIFF_NOREADRAW)==0)
268         {
269                 /*
270                  * FIXME: butecount should have tsize_t type, but for now
271                  * libtiff defines tsize_t as a signed 32-bit integer and we
272                  * are losing ability to read arrays larger than 2^31 bytes.
273                  * So we are using uint32 instead of tsize_t here.
274                  */
275                 uint32 bytecount = td->td_stripbytecount[strip];
276                 if (bytecount <= 0) {
277                         TIFFErrorExt(tif->tif_clientdata, module,
278                             "%s: Invalid strip byte count %lu, strip %lu",
279                             tif->tif_name, (unsigned long) bytecount,
280                             (unsigned long) strip);
281                         return (0);
282                 }
283                 if (isMapped(tif) &&
284                     (isFillOrder(tif, td->td_fillorder)
285                     || (tif->tif_flags & TIFF_NOBITREV))) {
286                         /*
287                          * The image is mapped into memory and we either don't
288                          * need to flip bits or the compression routine is
289                          * going to handle this operation itself.  In this
290                          * case, avoid copying the raw data and instead just
291                          * reference the data from the memory mapped file
292                          * image.  This assumes that the decompression
293                          * routines do not modify the contents of the raw data
294                          * buffer (if they try to, the application will get a
295                          * fault since the file is mapped read-only).
296                          */
297                         if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
298                                 _TIFFfree(tif->tif_rawdata);
299                         tif->tif_flags &= ~TIFF_MYBUFFER;
300                         /*
301                          * We must check for overflow, potentially causing
302                          * an OOB read. Instead of simple
303                          *
304                          *  td->td_stripoffset[strip]+bytecount > tif->tif_size
305                          *
306                          * comparison (which can overflow) we do the following
307                          * two comparisons:
308                          */
309                         if (bytecount > tif->tif_size ||
310                             td->td_stripoffset[strip] > tif->tif_size - bytecount) {
311                                 /*
312                                  * This error message might seem strange, but
313                                  * it's what would happen if a read were done
314                                  * instead.
315                                  */
316                                 TIFFErrorExt(tif->tif_clientdata, module,
317
318                                         "%s: Read error on strip %lu; "
319                                         "got %lu bytes, expected %lu",
320                                         tif->tif_name, (unsigned long) strip,
321                                         (unsigned long) tif->tif_size - td->td_stripoffset[strip],
322                                         (unsigned long) bytecount);
323                                 tif->tif_curstrip = NOSTRIP;
324                                 return (0);
325                         }
326                         tif->tif_rawdatasize = bytecount;
327                         tif->tif_rawdata = tif->tif_base + td->td_stripoffset[strip];
328                 } else {
329                         /*
330                          * Expand raw data buffer, if needed, to hold data
331                          * strip coming from file (perhaps should set upper
332                          * bound on the size of a buffer we'll use?).
333                          */
334                         if (bytecount > (uint32)tif->tif_rawdatasize) {
335                                 tif->tif_curstrip = NOSTRIP;
336                                 if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
337                                         TIFFErrorExt(tif->tif_clientdata,
338                                                      module,
339                                 "%s: Data buffer too small to hold strip %lu",
340                                                      tif->tif_name,
341                                                      (unsigned long) strip);
342                                         return (0);
343                                 }
344                                 if (!TIFFReadBufferSetup(tif, 0,
345                                     TIFFroundup(bytecount, 1024)))
346                                         return (0);
347                         }
348                         if ((uint32)TIFFReadRawStrip1(tif, strip,
349                                 (unsigned char *)tif->tif_rawdata,
350                                 bytecount, module) != bytecount)
351                                 return (0);
352                         if (!isFillOrder(tif, td->td_fillorder) &&
353                             (tif->tif_flags & TIFF_NOBITREV) == 0)
354                                 TIFFReverseBits(tif->tif_rawdata, bytecount);
355                 }
356         }
357         return (TIFFStartStrip(tif, strip));
358 }
359
360 /*
361  * Tile-oriented Read Support
362  * Contributed by Nancy Cam (Silicon Graphics).
363  */
364
365 /*
366  * Read and decompress a tile of data.  The
367  * tile is selected by the (x,y,z,s) coordinates.
368  */
369 tsize_t
370 TIFFReadTile(TIFF* tif,
371     tdata_t buf, uint32 x, uint32 y, uint32 z, tsample_t s)
372 {
373         if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
374                 return (-1);
375         return (TIFFReadEncodedTile(tif,
376             TIFFComputeTile(tif, x, y, z, s), buf, (tsize_t) -1));
377 }
378
379 /*
380  * Read a tile of data and decompress the specified
381  * amount into the user-supplied buffer.
382  */
383 tsize_t
384 TIFFReadEncodedTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
385 {
386         TIFFDirectory *td = &tif->tif_dir;
387         tsize_t tilesize = tif->tif_tilesize;
388
389         if (!TIFFCheckRead(tif, 1))
390                 return (-1);
391         if (tile >= td->td_nstrips) {
392                 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
393                              "%ld: Tile out of range, max %ld",
394                              (long) tile, (unsigned long) td->td_nstrips);
395                 return (-1);
396         }
397         if (size == (tsize_t) -1)
398                 size = tilesize;
399         else if (size > tilesize)
400                 size = tilesize;
401         if (TIFFFillTile(tif, tile) && (*tif->tif_decodetile)(tif,
402             (tidata_t) buf, size, (tsample_t)(tile/td->td_stripsperimage))) {
403                 (*tif->tif_postdecode)(tif, (tidata_t) buf, size);
404                 return (size);
405         } else
406                 return (-1);
407 }
408
409 static tsize_t
410 TIFFReadRawTile1(TIFF* tif,
411     ttile_t tile, tdata_t buf, tsize_t size, const char* module)
412 {
413         TIFFDirectory *td = &tif->tif_dir;
414
415         assert((tif->tif_flags&TIFF_NOREADRAW)==0);
416         if (!isMapped(tif)) {
417                 tsize_t cc;
418
419                 if (!SeekOK(tif, td->td_stripoffset[tile])) {
420                         TIFFErrorExt(tif->tif_clientdata, module,
421                             "%s: Seek error at row %ld, col %ld, tile %ld",
422                             tif->tif_name,
423                             (long) tif->tif_row,
424                             (long) tif->tif_col,
425                             (long) tile);
426                         return ((tsize_t) -1);
427                 }
428                 cc = TIFFReadFile(tif, buf, size);
429                 if (cc != size) {
430                         TIFFErrorExt(tif->tif_clientdata, module,
431             "%s: Read error at row %ld, col %ld; got %lu bytes, expected %lu",
432                             tif->tif_name,
433                             (long) tif->tif_row,
434                             (long) tif->tif_col,
435                             (unsigned long) cc,
436                             (unsigned long) size);
437                         return ((tsize_t) -1);
438                 }
439         } else {
440                 if (td->td_stripoffset[tile] + size > tif->tif_size) {
441                         TIFFErrorExt(tif->tif_clientdata, module,
442     "%s: Read error at row %ld, col %ld, tile %ld; got %lu bytes, expected %lu",
443                             tif->tif_name,
444                             (long) tif->tif_row,
445                             (long) tif->tif_col,
446                             (long) tile,
447                             (unsigned long) tif->tif_size - td->td_stripoffset[tile],
448                             (unsigned long) size);
449                         return ((tsize_t) -1);
450                 }
451                 _TIFFmemcpy(buf, tif->tif_base + td->td_stripoffset[tile], size);
452         }
453         return (size);
454 }
455
456 /*
457  * Read a tile of data from the file.
458  */
459 tsize_t
460 TIFFReadRawTile(TIFF* tif, ttile_t tile, tdata_t buf, tsize_t size)
461 {
462         static const char module[] = "TIFFReadRawTile";
463         TIFFDirectory *td = &tif->tif_dir;
464         /*
465          * FIXME: butecount should have tsize_t type, but for now libtiff
466          * defines tsize_t as a signed 32-bit integer and we are losing
467          * ability to read arrays larger than 2^31 bytes. So we are using
468          * uint32 instead of tsize_t here.
469          */
470         uint32 bytecount;
471
472         if (!TIFFCheckRead(tif, 1))
473                 return ((tsize_t) -1);
474         if (tile >= td->td_nstrips) {
475                 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
476                              "%lu: Tile out of range, max %lu",
477                     (unsigned long) tile, (unsigned long) td->td_nstrips);
478                 return ((tsize_t) -1);
479         }
480         if (tif->tif_flags&TIFF_NOREADRAW)
481         {
482                 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
483         "Compression scheme does not support access to raw uncompressed data");
484                 return ((tsize_t) -1);
485         }
486         bytecount = td->td_stripbytecount[tile];
487         if (size != (tsize_t) -1 && (uint32)size < bytecount)
488                 bytecount = size;
489         return (TIFFReadRawTile1(tif, tile, buf, bytecount, module));
490 }
491
492 /*
493  * Read the specified tile and setup for decoding. The data buffer is
494  * expanded, as necessary, to hold the tile's data.
495  */
496 int
497 TIFFFillTile(TIFF* tif, ttile_t tile)
498 {
499         static const char module[] = "TIFFFillTile";
500         TIFFDirectory *td = &tif->tif_dir;
501
502         if ((tif->tif_flags&TIFF_NOREADRAW)==0)
503         {
504                 /*
505                  * FIXME: butecount should have tsize_t type, but for now
506                  * libtiff defines tsize_t as a signed 32-bit integer and we
507                  * are losing ability to read arrays larger than 2^31 bytes.
508                  * So we are using uint32 instead of tsize_t here.
509                  */
510                 uint32 bytecount = td->td_stripbytecount[tile];
511                 if (bytecount <= 0) {
512                         TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
513                             "%lu: Invalid tile byte count, tile %lu",
514                             (unsigned long) bytecount, (unsigned long) tile);
515                         return (0);
516                 }
517                 if (isMapped(tif) &&
518                     (isFillOrder(tif, td->td_fillorder)
519                      || (tif->tif_flags & TIFF_NOBITREV))) {
520                         /*
521                          * The image is mapped into memory and we either don't
522                          * need to flip bits or the compression routine is
523                          * going to handle this operation itself.  In this
524                          * case, avoid copying the raw data and instead just
525                          * reference the data from the memory mapped file
526                          * image.  This assumes that the decompression
527                          * routines do not modify the contents of the raw data
528                          * buffer (if they try to, the application will get a
529                          * fault since the file is mapped read-only).
530                          */
531                         if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
532                                 _TIFFfree(tif->tif_rawdata);
533                         tif->tif_flags &= ~TIFF_MYBUFFER;
534                         /*
535                          * We must check for overflow, potentially causing
536                          * an OOB read. Instead of simple
537                          *
538                          *  td->td_stripoffset[tile]+bytecount > tif->tif_size
539                          *
540                          * comparison (which can overflow) we do the following
541                          * two comparisons:
542                          */
543                         if (bytecount > tif->tif_size ||
544                             td->td_stripoffset[tile] > tif->tif_size - bytecount) {
545                                 tif->tif_curtile = NOTILE;
546                                 return (0);
547                         }
548                         tif->tif_rawdatasize = bytecount;
549                         tif->tif_rawdata =
550                                 tif->tif_base + td->td_stripoffset[tile];
551                 } else {
552                         /*
553                          * Expand raw data buffer, if needed, to hold data
554                          * tile coming from file (perhaps should set upper
555                          * bound on the size of a buffer we'll use?).
556                          */
557                         if (bytecount > (uint32)tif->tif_rawdatasize) {
558                                 tif->tif_curtile = NOTILE;
559                                 if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
560                                         TIFFErrorExt(tif->tif_clientdata,
561                                                      module,
562                                 "%s: Data buffer too small to hold tile %ld",
563                                                      tif->tif_name,
564                                                      (long) tile);
565                                         return (0);
566                                 }
567                                 if (!TIFFReadBufferSetup(tif, 0,
568                                     TIFFroundup(bytecount, 1024)))
569                                         return (0);
570                         }
571                         if ((uint32)TIFFReadRawTile1(tif, tile,
572                                 (unsigned char *)tif->tif_rawdata,
573                                 bytecount, module) != bytecount)
574                                 return (0);
575                         if (!isFillOrder(tif, td->td_fillorder) &&
576                             (tif->tif_flags & TIFF_NOBITREV) == 0)
577                                 TIFFReverseBits(tif->tif_rawdata, bytecount);
578                 }
579         }
580         return (TIFFStartTile(tif, tile));
581 }
582
583 /*
584  * Setup the raw data buffer in preparation for
585  * reading a strip of raw data.  If the buffer
586  * is specified as zero, then a buffer of appropriate
587  * size is allocated by the library.  Otherwise,
588  * the client must guarantee that the buffer is
589  * large enough to hold any individual strip of
590  * raw data.
591  */
592 int
593 TIFFReadBufferSetup(TIFF* tif, tdata_t bp, tsize_t size)
594 {
595         static const char module[] = "TIFFReadBufferSetup";
596
597         assert((tif->tif_flags&TIFF_NOREADRAW)==0);
598         if (tif->tif_rawdata) {
599                 if (tif->tif_flags & TIFF_MYBUFFER)
600                         _TIFFfree(tif->tif_rawdata);
601                 tif->tif_rawdata = NULL;
602         }
603
604         if (bp) {
605                 tif->tif_rawdatasize = size;
606                 tif->tif_rawdata = (tidata_t) bp;
607                 tif->tif_flags &= ~TIFF_MYBUFFER;
608         } else {
609                 tif->tif_rawdatasize = TIFFroundup(size, 1024);
610                 if (tif->tif_rawdatasize > 0)
611                         tif->tif_rawdata = (tidata_t) _TIFFmalloc(tif->tif_rawdatasize);
612                 tif->tif_flags |= TIFF_MYBUFFER;
613         }
614         if ((tif->tif_rawdata == NULL) || (tif->tif_rawdatasize == 0)) {
615                 TIFFErrorExt(tif->tif_clientdata, module,
616                     "%s: No space for data buffer at scanline %ld",
617                     tif->tif_name, (long) tif->tif_row);
618                 tif->tif_rawdatasize = 0;
619                 return (0);
620         }
621         return (1);
622 }
623
624 /*
625  * Set state to appear as if a
626  * strip has just been read in.
627  */
628 static int
629 TIFFStartStrip(TIFF* tif, tstrip_t strip)
630 {
631         TIFFDirectory *td = &tif->tif_dir;
632
633         if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
634                 if (!(*tif->tif_setupdecode)(tif))
635                         return (0);
636                 tif->tif_flags |= TIFF_CODERSETUP;
637         }
638         tif->tif_curstrip = strip;
639         tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
640         if (tif->tif_flags&TIFF_NOREADRAW)
641         {
642                 tif->tif_rawcp = NULL;
643                 tif->tif_rawcc = 0;
644         }
645         else
646         {
647                 tif->tif_rawcp = tif->tif_rawdata;
648                 tif->tif_rawcc = td->td_stripbytecount[strip];
649         }
650         return ((*tif->tif_predecode)(tif,
651                         (tsample_t)(strip / td->td_stripsperimage)));
652 }
653
654 /*
655  * Set state to appear as if a
656  * tile has just been read in.
657  */
658 static int
659 TIFFStartTile(TIFF* tif, ttile_t tile)
660 {
661         TIFFDirectory *td = &tif->tif_dir;
662
663         if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
664                 if (!(*tif->tif_setupdecode)(tif))
665                         return (0);
666                 tif->tif_flags |= TIFF_CODERSETUP;
667         }
668         tif->tif_curtile = tile;
669         tif->tif_row =
670             (tile % TIFFhowmany(td->td_imagewidth, td->td_tilewidth)) *
671                 td->td_tilelength;
672         tif->tif_col =
673             (tile % TIFFhowmany(td->td_imagelength, td->td_tilelength)) *
674                 td->td_tilewidth;
675         if (tif->tif_flags&TIFF_NOREADRAW)
676         {
677                 tif->tif_rawcp = NULL;
678                 tif->tif_rawcc = 0;
679         }
680         else
681         {
682                 tif->tif_rawcp = tif->tif_rawdata;
683                 tif->tif_rawcc = td->td_stripbytecount[tile];
684         }
685         return ((*tif->tif_predecode)(tif,
686                         (tsample_t)(tile/td->td_stripsperimage)));
687 }
688
689 static int
690 TIFFCheckRead(TIFF* tif, int tiles)
691 {
692         if (tif->tif_mode == O_WRONLY) {
693                 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "File not open for reading");
694                 return (0);
695         }
696         if (tiles ^ isTiled(tif)) {
697                 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, tiles ?
698                     "Can not read tiles from a stripped image" :
699                     "Can not read scanlines from a tiled image");
700                 return (0);
701         }
702         return (1);
703 }
704
705 void
706 _TIFFNoPostDecode(TIFF* tif, tidata_t buf, tsize_t cc)
707 {
708     (void) tif; (void) buf; (void) cc;
709 }
710
711 void
712 _TIFFSwab16BitData(TIFF* tif, tidata_t buf, tsize_t cc)
713 {
714     (void) tif;
715     assert((cc & 1) == 0);
716     TIFFSwabArrayOfShort((uint16*) buf, cc/2);
717 }
718
719 void
720 _TIFFSwab24BitData(TIFF* tif, tidata_t buf, tsize_t cc)
721 {
722     (void) tif;
723     assert((cc % 3) == 0);
724     TIFFSwabArrayOfTriples((uint8*) buf, cc/3);
725 }
726
727 void
728 _TIFFSwab32BitData(TIFF* tif, tidata_t buf, tsize_t cc)
729 {
730     (void) tif;
731     assert((cc & 3) == 0);
732     TIFFSwabArrayOfLong((uint32*) buf, cc/4);
733 }
734
735 void
736 _TIFFSwab64BitData(TIFF* tif, tidata_t buf, tsize_t cc)
737 {
738     (void) tif;
739     assert((cc & 7) == 0);
740     TIFFSwabArrayOfDouble((double*) buf, cc/8);
741 }
742
743 /* vim: set ts=8 sts=8 sw=8 noet: */
744 /*
745  * Local Variables:
746  * mode: c
747  * c-basic-offset: 8
748  * fill-column: 78
749  * End:
750  */