~ubuntu-branches/debian/sid/gdal/sid

« back to all changes in this revision

Viewing changes to frmts/gtiff/libtiff/tif_read.c

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2012-05-07 15:04:42 UTC
  • mfrom: (5.5.16 experimental)
  • Revision ID: package-import@ubuntu.com-20120507150442-2eks97loeh6rq005
Tags: 1.9.0-1
* Ready for sid, starting transition.
* All symfiles updated to latest builds.
* Added dh_numpy call in debian/rules to depend on numpy ABI.
* Policy bumped to 3.9.3, no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: tif_read.c,v 1.29 2007/11/23 20:49:43 fwarmerdam Exp $ */
 
1
/* $Id: tif_read.c,v 1.38 2011-12-09 03:29:10 fwarmerdam Exp $ */
2
2
 
3
3
/*
4
4
 * Copyright (c) 1988-1997 Sam Leffler
36
36
static int TIFFStartStrip(TIFF* tif, uint32 strip);
37
37
static int TIFFStartTile(TIFF* tif, uint32 tile);
38
38
static int TIFFCheckRead(TIFF*, int);
 
39
static tmsize_t
 
40
TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,const char* module);
39
41
 
40
42
#define NOSTRIP ((uint32)(-1))       /* undefined state */
41
43
#define NOTILE ((uint32)(-1))         /* undefined state */
42
44
 
 
45
static int
 
46
TIFFFillStripPartial( TIFF *tif, int strip, tmsize_t read_ahead, int restart )
 
47
{
 
48
        static const char module[] = "TIFFFillStripPartial";
 
49
        register TIFFDirectory *td = &tif->tif_dir;
 
50
        uint64 unused_data;
 
51
        uint64 read_offset;
 
52
        tmsize_t cc, to_read;
 
53
        tmsize_t bytecountm;
 
54
        
 
55
        if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
 
56
            return 0;
 
57
        
 
58
        /*
 
59
         * Expand raw data buffer, if needed, to hold data
 
60
         * strip coming from file (perhaps should set upper
 
61
         * bound on the size of a buffer we'll use?).
 
62
         */
 
63
 
 
64
        bytecountm=(tmsize_t) td->td_stripbytecount[strip];
 
65
        if (read_ahead*2 > tif->tif_rawdatasize) {
 
66
                assert( restart );
 
67
                
 
68
                tif->tif_curstrip = NOSTRIP;
 
69
                if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
 
70
                        TIFFErrorExt(tif->tif_clientdata, module,
 
71
                                     "Data buffer too small to hold part of strip %lu",
 
72
                                     (unsigned long) strip);
 
73
                        return (0);
 
74
                }
 
75
                if (!TIFFReadBufferSetup(tif, 0, read_ahead*2))
 
76
                        return (0);
 
77
        }
 
78
 
 
79
        if( restart )
 
80
        {
 
81
                tif->tif_rawdataloaded = 0;
 
82
                tif->tif_rawdataoff = 0;
 
83
        }
 
84
 
 
85
        /*
 
86
        ** If we are reading more data, move any unused data to the
 
87
        ** start of the buffer.
 
88
        */
 
89
        if( tif->tif_rawdataloaded > 0 )
 
90
                unused_data = tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata);
 
91
        else
 
92
                unused_data = 0;
 
93
        
 
94
        if( unused_data > 0 )
 
95
        {
 
96
                memmove( tif->tif_rawdata, tif->tif_rawcp, unused_data );
 
97
        }
 
98
 
 
99
        /*
 
100
        ** Seek to the point in the file where more data should be read.
 
101
        */
 
102
        read_offset = td->td_stripoffset[strip]
 
103
                + tif->tif_rawdataoff + tif->tif_rawdataloaded;
 
104
 
 
105
        if (!SeekOK(tif, read_offset)) {
 
106
                TIFFErrorExt(tif->tif_clientdata, module,
 
107
                             "Seek error at scanline %lu, strip %lu",
 
108
                             (unsigned long) tif->tif_row, (unsigned long) strip);
 
109
                return 0;
 
110
        }
 
111
 
 
112
        /*
 
113
        ** How much do we want to read?
 
114
        */
 
115
        to_read = tif->tif_rawdatasize - unused_data;
 
116
        if( (uint64) to_read > td->td_stripbytecount[strip] 
 
117
            - tif->tif_rawdataoff - tif->tif_rawdataloaded )
 
118
        {
 
119
                to_read = td->td_stripbytecount[strip]
 
120
                        - tif->tif_rawdataoff - tif->tif_rawdataloaded;
 
121
        }
 
122
 
 
123
        cc = TIFFReadFile(tif, tif->tif_rawdata + unused_data, to_read);
 
124
 
 
125
        if (cc != to_read) {
 
126
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
 
127
                TIFFErrorExt(tif->tif_clientdata, module,
 
128
                             "Read error at scanline %lu; got %I64u bytes, expected %I64u",
 
129
                             (unsigned long) tif->tif_row,
 
130
                             (unsigned __int64) cc,
 
131
                             (unsigned __int64) to_read);
 
132
#else
 
133
                TIFFErrorExt(tif->tif_clientdata, module,
 
134
                             "Read error at scanline %lu; got %llu bytes, expected %llu",
 
135
                             (unsigned long) tif->tif_row,
 
136
                             (unsigned long long) cc,
 
137
                             (unsigned long long) to_read);
 
138
#endif
 
139
                return 0;
 
140
        }
 
141
        
 
142
        tif->tif_rawdataoff = tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data ;
 
143
        tif->tif_rawdataloaded = unused_data + to_read;
 
144
 
 
145
        tif->tif_rawcp = tif->tif_rawdata;
 
146
                        
 
147
        if (!isFillOrder(tif, td->td_fillorder) &&
 
148
            (tif->tif_flags & TIFF_NOBITREV) == 0)
 
149
                TIFFReverseBits(tif->tif_rawdata + unused_data, to_read );
 
150
 
 
151
        /*
 
152
        ** When starting a strip from the beginning we need to
 
153
        ** restart the decoder.
 
154
        */
 
155
        if( restart )
 
156
                return TIFFStartStrip(tif, strip);
 
157
        else
 
158
                return 1;
 
159
}
 
160
 
43
161
/*
44
162
 * Seek to a random row+sample in a file.
 
163
 *
 
164
 * Only used by TIFFReadScanline, and is only used on
 
165
 * strip organized files.  We do some tricky stuff to try
 
166
 * and avoid reading the whole compressed raw data for big
 
167
 * strips.
45
168
 */
46
169
static int
47
 
TIFFSeek(TIFF* tif, uint32 row, uint16 sample)
 
170
TIFFSeek(TIFF* tif, uint32 row, uint16 sample )
48
171
{
49
172
        register TIFFDirectory *td = &tif->tif_dir;
50
173
        uint32 strip;
 
174
        int    whole_strip;
 
175
        tmsize_t read_ahead = 0;
51
176
 
 
177
        /*
 
178
        ** Establish what strip we are working from.
 
179
        */
52
180
        if (row >= td->td_imagelength) {        /* out of range */
53
181
                TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
54
182
                    "%lu: Row out of range, max %lu",
66
194
                strip = (uint32)sample*td->td_stripsperimage + row/td->td_rowsperstrip;
67
195
        } else
68
196
                strip = row / td->td_rowsperstrip;
 
197
 
 
198
        /*
 
199
         * Do we want to treat this strip as one whole chunk or
 
200
         * read it a few lines at a time?
 
201
         */
 
202
#if defined(CHUNKY_STRIP_READ_SUPPORT)
 
203
        if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
 
204
            return 0;
 
205
        whole_strip = tif->tif_dir.td_stripbytecount[strip] < 10
 
206
                || isMapped(tif);
 
207
#else
 
208
        whole_strip = 1;
 
209
#endif
 
210
        
 
211
        if( !whole_strip )
 
212
        {
 
213
                read_ahead = tif->tif_scanlinesize * 16 + 5000;
 
214
        }
 
215
 
 
216
        /*
 
217
         * If we haven't loaded this strip, do so now, possibly
 
218
         * only reading the first part.
 
219
         */
69
220
        if (strip != tif->tif_curstrip) {       /* different strip, refill */
70
 
                if (!TIFFFillStrip(tif, strip))
71
 
                        return (0);
72
 
        } else if (row < tif->tif_row) {
 
221
                
 
222
                if( whole_strip )
 
223
                {
 
224
                        if (!TIFFFillStrip(tif, strip))
 
225
                                return (0);
 
226
                }
 
227
                else
 
228
                {
 
229
                        if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
 
230
                                return 0;
 
231
                }
 
232
        }
 
233
 
 
234
        /*
 
235
        ** If we already have some data loaded, do we need to read some more?
 
236
        */
 
237
        else if( !whole_strip )
 
238
        {
 
239
                if( ((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) < read_ahead 
 
240
                    && (uint64) tif->tif_rawdataoff+tif->tif_rawdataloaded < td->td_stripbytecount[strip] )
 
241
                {
 
242
                        if( !TIFFFillStripPartial(tif,strip,read_ahead,0) )
 
243
                                return 0;
 
244
                }
 
245
        }
 
246
 
 
247
        if (row < tif->tif_row) {
73
248
                /*
74
249
                 * Moving backwards within the same strip: backup
75
250
                 * to the start and then decode forward (below).
78
253
                 * strip, it's better to just read and decode the entire
79
254
                 * strip, and then access the decoded data in a random fashion.
80
255
                 */
81
 
                if (!TIFFStartStrip(tif, strip))
82
 
                        return (0);
 
256
 
 
257
                if( tif->tif_rawdataoff != 0 )
 
258
                {
 
259
                        if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
 
260
                                return 0;
 
261
                }
 
262
                else
 
263
                {
 
264
                        if (!TIFFStartStrip(tif, strip))
 
265
                                return (0);
 
266
                }
83
267
        }
 
268
        
84
269
        if (row != tif->tif_row) {
85
270
                /*
86
271
                 * Seek forward to the desired row.
87
272
                 */
 
273
 
 
274
                /* TODO: Will this really work with partial buffers? */
 
275
                
88
276
                if (!(*tif->tif_seek)(tif, row - tif->tif_row))
89
277
                        return (0);
90
278
                tif->tif_row = row;
91
279
        }
 
280
 
92
281
        return (1);
93
282
}
94
283
 
173
362
{
174
363
        TIFFDirectory *td = &tif->tif_dir;
175
364
 
 
365
    if (!_TIFFFillStriles( tif ))
 
366
        return ((tmsize_t)(-1));
 
367
        
176
368
        assert((tif->tif_flags&TIFF_NOREADRAW)==0);
177
369
        if (!isMapped(tif)) {
178
370
                tmsize_t cc;
185
377
                }
186
378
                cc = TIFFReadFile(tif, buf, size);
187
379
                if (cc != size) {
188
 
#if defined(__WIN32__) && defined(_MSC_VER)
 
380
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
189
381
                        TIFFErrorExt(tif->tif_clientdata, module,
190
382
                "Read error at scanline %lu; got %I64u bytes, expected %I64u",
191
383
                                     (unsigned long) tif->tif_row,
212
404
                else
213
405
                        n=size;
214
406
                if (n!=size) {
215
 
#if defined(__WIN32__) && defined(_MSC_VER)
 
407
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
216
408
                        TIFFErrorExt(tif->tif_clientdata, module,
217
409
        "Read error at scanline %lu, strip %lu; got %I64u bytes, expected %I64u",
218
410
                                     (unsigned long) tif->tif_row,
263
455
        }
264
456
        bytecount = td->td_stripbytecount[strip];
265
457
        if (bytecount <= 0) {
266
 
#if defined(__WIN32__) && defined(_MSC_VER)
 
458
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
267
459
                TIFFErrorExt(tif->tif_clientdata, module,
268
460
                             "%I64u: Invalid strip byte count, strip %lu",
269
461
                             (unsigned __int64) bytecount,
296
488
        static const char module[] = "TIFFFillStrip";
297
489
        TIFFDirectory *td = &tif->tif_dir;
298
490
 
 
491
    if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
 
492
        return 0;
 
493
        
299
494
        if ((tif->tif_flags&TIFF_NOREADRAW)==0)
300
495
        {
301
496
                uint64 bytecount = td->td_stripbytecount[strip];
302
497
                if (bytecount <= 0) {
303
 
#if defined(__WIN32__) && defined(_MSC_VER)
 
498
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
304
499
                        TIFFErrorExt(tif->tif_clientdata, module,
305
500
                                "Invalid strip byte count %I64u, strip %lu",
306
501
                                     (unsigned __int64) bytecount,
346
541
                                 * it's what would happen if a read were done
347
542
                                 * instead.
348
543
                                 */
349
 
#if defined(__WIN32__) && defined(_MSC_VER)
 
544
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
350
545
                                TIFFErrorExt(tif->tif_clientdata, module,
351
546
 
352
547
                                        "Read error on strip %lu; "
368
563
                        }
369
564
                        tif->tif_rawdatasize = (tmsize_t)bytecount;
370
565
                        tif->tif_rawdata = tif->tif_base + (tmsize_t)td->td_stripoffset[strip];
 
566
                        tif->tif_rawdataoff = 0;
 
567
                        tif->tif_rawdataloaded = (tmsize_t) bytecount;
371
568
                } else {
372
569
                        /*
373
570
                         * Expand raw data buffer, if needed, to hold data
395
592
                        if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata,
396
593
                                bytecountm, module) != bytecountm)
397
594
                                return (0);
 
595
 
 
596
                        tif->tif_rawdataoff = 0;
 
597
                        tif->tif_rawdataloaded = bytecountm;
 
598
                        
398
599
                        if (!isFillOrder(tif, td->td_fillorder) &&
399
600
                            (tif->tif_flags & TIFF_NOBITREV) == 0)
400
601
                                TIFFReverseBits(tif->tif_rawdata, bytecountm);
401
 
                }
 
602
                }
402
603
        }
403
604
        return (TIFFStartStrip(tif, strip));
404
605
}
457
658
{
458
659
        TIFFDirectory *td = &tif->tif_dir;
459
660
 
 
661
    if (!_TIFFFillStriles( tif ))
 
662
        return ((tmsize_t)(-1));
 
663
 
460
664
        assert((tif->tif_flags&TIFF_NOREADRAW)==0);
461
665
        if (!isMapped(tif)) {
462
666
                tmsize_t cc;
471
675
                }
472
676
                cc = TIFFReadFile(tif, buf, size);
473
677
                if (cc != size) {
474
 
#if defined(__WIN32__) && defined(_MSC_VER)
 
678
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
475
679
                        TIFFErrorExt(tif->tif_clientdata, module,
476
680
        "Read error at row %lu, col %lu; got %I64u bytes, expected %I64u",
477
681
                                     (unsigned long) tif->tif_row,
500
704
                else
501
705
                        n=size;
502
706
                if (n!=size) {
503
 
#if defined(__WIN32__) && defined(_MSC_VER)
 
707
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
504
708
                        TIFFErrorExt(tif->tif_clientdata, module,
505
709
"Read error at row %lu, col %lu, tile %lu; got %I64u bytes, expected %I64u",
506
710
                                     (unsigned long) tif->tif_row,
571
775
        static const char module[] = "TIFFFillTile";
572
776
        TIFFDirectory *td = &tif->tif_dir;
573
777
 
 
778
    if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
 
779
        return 0;
 
780
        
574
781
        if ((tif->tif_flags&TIFF_NOREADRAW)==0)
575
782
        {
576
783
                uint64 bytecount = td->td_stripbytecount[tile];
577
784
                if (bytecount <= 0) {
578
 
#if defined(__WIN32__) && defined(_MSC_VER)
 
785
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
579
786
                        TIFFErrorExt(tif->tif_clientdata, module,
580
787
                                "%I64u: Invalid tile byte count, tile %lu",
581
788
                                     (unsigned __int64) bytecount,
622
829
                        tif->tif_rawdatasize = (tmsize_t)bytecount;
623
830
                        tif->tif_rawdata =
624
831
                                tif->tif_base + (tmsize_t)td->td_stripoffset[tile];
 
832
                        tif->tif_rawdataoff = 0;
 
833
                        tif->tif_rawdataloaded = (tmsize_t) bytecount;
625
834
                } else {
626
835
                        /*
627
836
                         * Expand raw data buffer, if needed, to hold data
649
858
                        if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata,
650
859
                            bytecountm, module) != bytecountm)
651
860
                                return (0);
 
861
 
 
862
                        tif->tif_rawdataoff = 0;
 
863
                        tif->tif_rawdataloaded = bytecountm;
 
864
                        
652
865
                        if (!isFillOrder(tif, td->td_fillorder) &&
653
866
                            (tif->tif_flags & TIFF_NOBITREV) == 0)
654
 
                                TIFFReverseBits(tif->tif_rawdata, bytecountm);
 
867
                                TIFFReverseBits(tif->tif_rawdata,
 
868
                                                tif->tif_rawdataloaded);
655
869
                }
656
870
        }
657
871
        return (TIFFStartTile(tif, tile));
707
921
{
708
922
        TIFFDirectory *td = &tif->tif_dir;
709
923
 
 
924
    if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
 
925
        return 0;
 
926
 
710
927
        if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
711
928
                if (!(*tif->tif_setupdecode)(tif))
712
929
                        return (0);
739
956
{
740
957
        TIFFDirectory *td = &tif->tif_dir;
741
958
 
 
959
    if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
 
960
        return 0;
 
961
 
742
962
        if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
743
963
                if (!(*tif->tif_setupdecode)(tif))
744
964
                        return (0);
821
1041
}
822
1042
 
823
1043
/* vim: set ts=8 sts=8 sw=8 noet: */
 
1044
/*
 
1045
 * Local Variables:
 
1046
 * mode: c
 
1047
 * c-basic-offset: 8
 
1048
 * fill-column: 78
 
1049
 * End:
 
1050
 */