1 | /* $Header: /afs/dev.mit.edu/source/repository/third/tiff/libtiff/tif_dirread.c,v 1.1.1.1 2002-12-26 02:38:56 ghudson 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 | * |
---|
30 | * Directory Read Support Routines. |
---|
31 | */ |
---|
32 | #include "tiffiop.h" |
---|
33 | |
---|
34 | #define IGNORE 0 /* tag placeholder used below */ |
---|
35 | |
---|
36 | #if HAVE_IEEEFP |
---|
37 | #define TIFFCvtIEEEFloatToNative(tif, n, fp) |
---|
38 | #define TIFFCvtIEEEDoubleToNative(tif, n, dp) |
---|
39 | #else |
---|
40 | extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*); |
---|
41 | extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*); |
---|
42 | #endif |
---|
43 | |
---|
44 | static void EstimateStripByteCounts(TIFF*, TIFFDirEntry*, uint16); |
---|
45 | static void MissingRequired(TIFF*, const char*); |
---|
46 | static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32); |
---|
47 | static tsize_t TIFFFetchData(TIFF*, TIFFDirEntry*, char*); |
---|
48 | static tsize_t TIFFFetchString(TIFF*, TIFFDirEntry*, char*); |
---|
49 | static float TIFFFetchRational(TIFF*, TIFFDirEntry*); |
---|
50 | static int TIFFFetchNormalTag(TIFF*, TIFFDirEntry*); |
---|
51 | static int TIFFFetchPerSampleShorts(TIFF*, TIFFDirEntry*, int*); |
---|
52 | static int TIFFFetchPerSampleAnys(TIFF*, TIFFDirEntry*, double*); |
---|
53 | static int TIFFFetchShortArray(TIFF*, TIFFDirEntry*, uint16*); |
---|
54 | static int TIFFFetchStripThing(TIFF*, TIFFDirEntry*, long, uint32**); |
---|
55 | static int TIFFFetchExtraSamples(TIFF*, TIFFDirEntry*); |
---|
56 | static int TIFFFetchRefBlackWhite(TIFF*, TIFFDirEntry*); |
---|
57 | static float TIFFFetchFloat(TIFF*, TIFFDirEntry*); |
---|
58 | static int TIFFFetchFloatArray(TIFF*, TIFFDirEntry*, float*); |
---|
59 | static int TIFFFetchDoubleArray(TIFF*, TIFFDirEntry*, double*); |
---|
60 | static int TIFFFetchAnyArray(TIFF*, TIFFDirEntry*, double*); |
---|
61 | static int TIFFFetchShortPair(TIFF*, TIFFDirEntry*); |
---|
62 | static void ChopUpSingleUncompressedStrip(TIFF*); |
---|
63 | |
---|
64 | static char * |
---|
65 | CheckMalloc(TIFF* tif, tsize_t n, const char* what) |
---|
66 | { |
---|
67 | char *cp = (char*)_TIFFmalloc(n); |
---|
68 | if (cp == NULL) |
---|
69 | TIFFError(tif->tif_name, "No space %s", what); |
---|
70 | return (cp); |
---|
71 | } |
---|
72 | |
---|
73 | /* |
---|
74 | * Read the next TIFF directory from a file |
---|
75 | * and convert it to the internal format. |
---|
76 | * We read directories sequentially. |
---|
77 | */ |
---|
78 | int |
---|
79 | TIFFReadDirectory(TIFF* tif) |
---|
80 | { |
---|
81 | register TIFFDirEntry* dp; |
---|
82 | register int n; |
---|
83 | register TIFFDirectory* td; |
---|
84 | TIFFDirEntry* dir; |
---|
85 | int iv; |
---|
86 | long v; |
---|
87 | double dv; |
---|
88 | const TIFFFieldInfo* fip; |
---|
89 | int fix; |
---|
90 | uint16 dircount; |
---|
91 | toff_t nextdiroff; |
---|
92 | char* cp; |
---|
93 | int diroutoforderwarning = 0; |
---|
94 | |
---|
95 | tif->tif_diroff = tif->tif_nextdiroff; |
---|
96 | if (tif->tif_diroff == 0) /* no more directories */ |
---|
97 | return (0); |
---|
98 | /* |
---|
99 | * Cleanup any previous compression state. |
---|
100 | */ |
---|
101 | (*tif->tif_cleanup)(tif); |
---|
102 | tif->tif_curdir++; |
---|
103 | nextdiroff = 0; |
---|
104 | if (!isMapped(tif)) { |
---|
105 | if (!SeekOK(tif, tif->tif_diroff)) { |
---|
106 | TIFFError(tif->tif_name, |
---|
107 | "Seek error accessing TIFF directory"); |
---|
108 | return (0); |
---|
109 | } |
---|
110 | if (!ReadOK(tif, &dircount, sizeof (uint16))) { |
---|
111 | TIFFError(tif->tif_name, |
---|
112 | "Can not read TIFF directory count"); |
---|
113 | return (0); |
---|
114 | } |
---|
115 | if (tif->tif_flags & TIFF_SWAB) |
---|
116 | TIFFSwabShort(&dircount); |
---|
117 | dir = (TIFFDirEntry *)CheckMalloc(tif, |
---|
118 | dircount * sizeof (TIFFDirEntry), "to read TIFF directory"); |
---|
119 | if (dir == NULL) |
---|
120 | return (0); |
---|
121 | if (!ReadOK(tif, dir, dircount*sizeof (TIFFDirEntry))) { |
---|
122 | TIFFError(tif->tif_name, "Can not read TIFF directory"); |
---|
123 | goto bad; |
---|
124 | } |
---|
125 | /* |
---|
126 | * Read offset to next directory for sequential scans. |
---|
127 | */ |
---|
128 | (void) ReadOK(tif, &nextdiroff, sizeof (uint32)); |
---|
129 | } else { |
---|
130 | toff_t off = tif->tif_diroff; |
---|
131 | |
---|
132 | if (off + sizeof (uint16) > tif->tif_size) { |
---|
133 | TIFFError(tif->tif_name, |
---|
134 | "Can not read TIFF directory count"); |
---|
135 | return (0); |
---|
136 | } else |
---|
137 | _TIFFmemcpy(&dircount, tif->tif_base + off, sizeof (uint16)); |
---|
138 | off += sizeof (uint16); |
---|
139 | if (tif->tif_flags & TIFF_SWAB) |
---|
140 | TIFFSwabShort(&dircount); |
---|
141 | dir = (TIFFDirEntry *)CheckMalloc(tif, |
---|
142 | dircount * sizeof (TIFFDirEntry), "to read TIFF directory"); |
---|
143 | if (dir == NULL) |
---|
144 | return (0); |
---|
145 | if (off + dircount*sizeof (TIFFDirEntry) > tif->tif_size) { |
---|
146 | TIFFError(tif->tif_name, "Can not read TIFF directory"); |
---|
147 | goto bad; |
---|
148 | } else |
---|
149 | _TIFFmemcpy(dir, tif->tif_base + off, |
---|
150 | dircount*sizeof (TIFFDirEntry)); |
---|
151 | off += dircount* sizeof (TIFFDirEntry); |
---|
152 | if (off + sizeof (uint32) <= tif->tif_size) |
---|
153 | _TIFFmemcpy(&nextdiroff, tif->tif_base+off, sizeof (uint32)); |
---|
154 | } |
---|
155 | if (tif->tif_flags & TIFF_SWAB) |
---|
156 | TIFFSwabLong(&nextdiroff); |
---|
157 | tif->tif_nextdiroff = nextdiroff; |
---|
158 | |
---|
159 | tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */ |
---|
160 | /* |
---|
161 | * Setup default value and then make a pass over |
---|
162 | * the fields to check type and tag information, |
---|
163 | * and to extract info required to size data |
---|
164 | * structures. A second pass is made afterwards |
---|
165 | * to read in everthing not taken in the first pass. |
---|
166 | */ |
---|
167 | td = &tif->tif_dir; |
---|
168 | /* free any old stuff and reinit */ |
---|
169 | TIFFFreeDirectory(tif); |
---|
170 | TIFFDefaultDirectory(tif); |
---|
171 | /* |
---|
172 | * Electronic Arts writes gray-scale TIFF files |
---|
173 | * without a PlanarConfiguration directory entry. |
---|
174 | * Thus we setup a default value here, even though |
---|
175 | * the TIFF spec says there is no default value. |
---|
176 | */ |
---|
177 | TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
---|
178 | |
---|
179 | /* |
---|
180 | * Sigh, we must make a separate pass through the |
---|
181 | * directory for the following reason: |
---|
182 | * |
---|
183 | * We must process the Compression tag in the first pass |
---|
184 | * in order to merge in codec-private tag definitions (otherwise |
---|
185 | * we may get complaints about unknown tags). However, the |
---|
186 | * Compression tag may be dependent on the SamplesPerPixel |
---|
187 | * tag value because older TIFF specs permited Compression |
---|
188 | * to be written as a SamplesPerPixel-count tag entry. |
---|
189 | * Thus if we don't first figure out the correct SamplesPerPixel |
---|
190 | * tag value then we may end up ignoring the Compression tag |
---|
191 | * value because it has an incorrect count value (if the |
---|
192 | * true value of SamplesPerPixel is not 1). |
---|
193 | * |
---|
194 | * It sure would have been nice if Aldus had really thought |
---|
195 | * this stuff through carefully. |
---|
196 | */ |
---|
197 | for (dp = dir, n = dircount; n > 0; n--, dp++) { |
---|
198 | if (tif->tif_flags & TIFF_SWAB) { |
---|
199 | TIFFSwabArrayOfShort(&dp->tdir_tag, 2); |
---|
200 | TIFFSwabArrayOfLong(&dp->tdir_count, 2); |
---|
201 | } |
---|
202 | if (dp->tdir_tag == TIFFTAG_SAMPLESPERPIXEL) { |
---|
203 | if (!TIFFFetchNormalTag(tif, dp)) |
---|
204 | goto bad; |
---|
205 | dp->tdir_tag = IGNORE; |
---|
206 | } |
---|
207 | } |
---|
208 | /* |
---|
209 | * First real pass over the directory. |
---|
210 | */ |
---|
211 | fix = 0; |
---|
212 | for (dp = dir, n = dircount; n > 0; n--, dp++) { |
---|
213 | |
---|
214 | /* |
---|
215 | * Find the field information entry for this tag. |
---|
216 | * Added check for tags to ignore ... [BFC] |
---|
217 | */ |
---|
218 | if( TIFFReassignTagToIgnore(TIS_EXTRACT, dp->tdir_tag) ) |
---|
219 | dp->tdir_tag = IGNORE; |
---|
220 | |
---|
221 | if (dp->tdir_tag == IGNORE) |
---|
222 | continue; |
---|
223 | |
---|
224 | /* |
---|
225 | * Silicon Beach (at least) writes unordered |
---|
226 | * directory tags (violating the spec). Handle |
---|
227 | * it here, but be obnoxious (maybe they'll fix it?). |
---|
228 | */ |
---|
229 | if (dp->tdir_tag < tif->tif_fieldinfo[fix]->field_tag) { |
---|
230 | if (!diroutoforderwarning) { |
---|
231 | TIFFWarning(tif->tif_name, |
---|
232 | "invalid TIFF directory; tags are not sorted in ascending order"); |
---|
233 | diroutoforderwarning = 1; |
---|
234 | } |
---|
235 | fix = 0; /* O(n^2) */ |
---|
236 | } |
---|
237 | while (fix < tif->tif_nfields && |
---|
238 | tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag) |
---|
239 | fix++; |
---|
240 | if (fix == tif->tif_nfields || |
---|
241 | tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) { |
---|
242 | TIFFWarning(tif->tif_name, |
---|
243 | "unknown field with tag %d (0x%x) ignored", |
---|
244 | dp->tdir_tag, dp->tdir_tag); |
---|
245 | dp->tdir_tag = IGNORE; |
---|
246 | fix = 0; /* restart search */ |
---|
247 | continue; |
---|
248 | } |
---|
249 | /* |
---|
250 | * Null out old tags that we ignore. |
---|
251 | */ |
---|
252 | if (tif->tif_fieldinfo[fix]->field_bit == FIELD_IGNORE) { |
---|
253 | ignore: |
---|
254 | dp->tdir_tag = IGNORE; |
---|
255 | continue; |
---|
256 | } |
---|
257 | /* |
---|
258 | * Check data type. |
---|
259 | */ |
---|
260 | fip = tif->tif_fieldinfo[fix]; |
---|
261 | while (dp->tdir_type != (u_short) fip->field_type) { |
---|
262 | if (fip->field_type == TIFF_ANY) /* wildcard */ |
---|
263 | break; |
---|
264 | fip++, fix++; |
---|
265 | if (fix == tif->tif_nfields || |
---|
266 | fip->field_tag != dp->tdir_tag) { |
---|
267 | TIFFWarning(tif->tif_name, |
---|
268 | "wrong data type %d for \"%s\"; tag ignored", |
---|
269 | dp->tdir_type, fip[-1].field_name); |
---|
270 | goto ignore; |
---|
271 | } |
---|
272 | } |
---|
273 | /* |
---|
274 | * Check count if known in advance. |
---|
275 | */ |
---|
276 | if (fip->field_readcount != TIFF_VARIABLE) { |
---|
277 | uint32 expected = (fip->field_readcount == TIFF_SPP) ? |
---|
278 | (uint32) td->td_samplesperpixel : |
---|
279 | (uint32) fip->field_readcount; |
---|
280 | if (!CheckDirCount(tif, dp, expected)) |
---|
281 | goto ignore; |
---|
282 | } |
---|
283 | |
---|
284 | switch (dp->tdir_tag) { |
---|
285 | case TIFFTAG_COMPRESSION: |
---|
286 | /* |
---|
287 | * The 5.0 spec says the Compression tag has |
---|
288 | * one value, while earlier specs say it has |
---|
289 | * one value per sample. Because of this, we |
---|
290 | * accept the tag if one value is supplied. |
---|
291 | */ |
---|
292 | if (dp->tdir_count == 1) { |
---|
293 | v = TIFFExtractData(tif, |
---|
294 | dp->tdir_type, dp->tdir_offset); |
---|
295 | if (!TIFFSetField(tif, dp->tdir_tag, (int)v)) |
---|
296 | goto bad; |
---|
297 | break; |
---|
298 | } |
---|
299 | if (!TIFFFetchPerSampleShorts(tif, dp, &iv) || |
---|
300 | !TIFFSetField(tif, dp->tdir_tag, iv)) |
---|
301 | goto bad; |
---|
302 | dp->tdir_tag = IGNORE; |
---|
303 | break; |
---|
304 | case TIFFTAG_STRIPOFFSETS: |
---|
305 | case TIFFTAG_STRIPBYTECOUNTS: |
---|
306 | case TIFFTAG_TILEOFFSETS: |
---|
307 | case TIFFTAG_TILEBYTECOUNTS: |
---|
308 | TIFFSetFieldBit(tif, fip->field_bit); |
---|
309 | break; |
---|
310 | case TIFFTAG_IMAGEWIDTH: |
---|
311 | case TIFFTAG_IMAGELENGTH: |
---|
312 | case TIFFTAG_IMAGEDEPTH: |
---|
313 | case TIFFTAG_TILELENGTH: |
---|
314 | case TIFFTAG_TILEWIDTH: |
---|
315 | case TIFFTAG_TILEDEPTH: |
---|
316 | case TIFFTAG_PLANARCONFIG: |
---|
317 | case TIFFTAG_ROWSPERSTRIP: |
---|
318 | if (!TIFFFetchNormalTag(tif, dp)) |
---|
319 | goto bad; |
---|
320 | dp->tdir_tag = IGNORE; |
---|
321 | break; |
---|
322 | case TIFFTAG_EXTRASAMPLES: |
---|
323 | (void) TIFFFetchExtraSamples(tif, dp); |
---|
324 | dp->tdir_tag = IGNORE; |
---|
325 | break; |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | /* |
---|
330 | * Allocate directory structure and setup defaults. |
---|
331 | */ |
---|
332 | if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) { |
---|
333 | MissingRequired(tif, "ImageLength"); |
---|
334 | goto bad; |
---|
335 | } |
---|
336 | if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) { |
---|
337 | MissingRequired(tif, "PlanarConfiguration"); |
---|
338 | goto bad; |
---|
339 | } |
---|
340 | /* |
---|
341 | * Setup appropriate structures (by strip or by tile) |
---|
342 | */ |
---|
343 | if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) { |
---|
344 | td->td_nstrips = TIFFNumberOfStrips(tif); |
---|
345 | td->td_tilewidth = td->td_imagewidth; |
---|
346 | td->td_tilelength = td->td_rowsperstrip; |
---|
347 | td->td_tiledepth = td->td_imagedepth; |
---|
348 | tif->tif_flags &= ~TIFF_ISTILED; |
---|
349 | } else { |
---|
350 | td->td_nstrips = TIFFNumberOfTiles(tif); |
---|
351 | tif->tif_flags |= TIFF_ISTILED; |
---|
352 | } |
---|
353 | td->td_stripsperimage = td->td_nstrips; |
---|
354 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
---|
355 | td->td_stripsperimage /= td->td_samplesperpixel; |
---|
356 | if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) { |
---|
357 | MissingRequired(tif, |
---|
358 | isTiled(tif) ? "TileOffsets" : "StripOffsets"); |
---|
359 | goto bad; |
---|
360 | } |
---|
361 | |
---|
362 | /* |
---|
363 | * Second pass: extract other information. |
---|
364 | */ |
---|
365 | for (dp = dir, n = dircount; n > 0; n--, dp++) { |
---|
366 | if (dp->tdir_tag == IGNORE) |
---|
367 | continue; |
---|
368 | switch (dp->tdir_tag) { |
---|
369 | case TIFFTAG_MINSAMPLEVALUE: |
---|
370 | case TIFFTAG_MAXSAMPLEVALUE: |
---|
371 | case TIFFTAG_BITSPERSAMPLE: |
---|
372 | /* |
---|
373 | * The 5.0 spec says the Compression tag has |
---|
374 | * one value, while earlier specs say it has |
---|
375 | * one value per sample. Because of this, we |
---|
376 | * accept the tag if one value is supplied. |
---|
377 | * |
---|
378 | * The MinSampleValue, MaxSampleValue and |
---|
379 | * BitsPerSample tags are supposed to be written |
---|
380 | * as one value/sample, but some vendors incorrectly |
---|
381 | * write one value only -- so we accept that |
---|
382 | * as well (yech). |
---|
383 | */ |
---|
384 | if (dp->tdir_count == 1) { |
---|
385 | v = TIFFExtractData(tif, |
---|
386 | dp->tdir_type, dp->tdir_offset); |
---|
387 | if (!TIFFSetField(tif, dp->tdir_tag, (int)v)) |
---|
388 | goto bad; |
---|
389 | break; |
---|
390 | } |
---|
391 | /* fall thru... */ |
---|
392 | case TIFFTAG_DATATYPE: |
---|
393 | case TIFFTAG_SAMPLEFORMAT: |
---|
394 | if (!TIFFFetchPerSampleShorts(tif, dp, &iv) || |
---|
395 | !TIFFSetField(tif, dp->tdir_tag, iv)) |
---|
396 | goto bad; |
---|
397 | break; |
---|
398 | case TIFFTAG_SMINSAMPLEVALUE: |
---|
399 | case TIFFTAG_SMAXSAMPLEVALUE: |
---|
400 | if (!TIFFFetchPerSampleAnys(tif, dp, &dv) || |
---|
401 | !TIFFSetField(tif, dp->tdir_tag, dv)) |
---|
402 | goto bad; |
---|
403 | break; |
---|
404 | case TIFFTAG_STRIPOFFSETS: |
---|
405 | case TIFFTAG_TILEOFFSETS: |
---|
406 | if (!TIFFFetchStripThing(tif, dp, |
---|
407 | td->td_nstrips, &td->td_stripoffset)) |
---|
408 | goto bad; |
---|
409 | break; |
---|
410 | case TIFFTAG_STRIPBYTECOUNTS: |
---|
411 | case TIFFTAG_TILEBYTECOUNTS: |
---|
412 | if (!TIFFFetchStripThing(tif, dp, |
---|
413 | td->td_nstrips, &td->td_stripbytecount)) |
---|
414 | goto bad; |
---|
415 | break; |
---|
416 | case TIFFTAG_COLORMAP: |
---|
417 | case TIFFTAG_TRANSFERFUNCTION: |
---|
418 | /* |
---|
419 | * TransferFunction can have either 1x or 3x data |
---|
420 | * values; Colormap can have only 3x items. |
---|
421 | */ |
---|
422 | v = 1L<<td->td_bitspersample; |
---|
423 | if (dp->tdir_tag == TIFFTAG_COLORMAP || |
---|
424 | dp->tdir_count != (uint32) v) { |
---|
425 | if (!CheckDirCount(tif, dp, (uint32)(3*v))) |
---|
426 | break; |
---|
427 | } |
---|
428 | v *= sizeof (uint16); |
---|
429 | cp = CheckMalloc(tif, dp->tdir_count * sizeof (uint16), |
---|
430 | "to read \"TransferFunction\" tag"); |
---|
431 | if (cp != NULL) { |
---|
432 | if (TIFFFetchData(tif, dp, cp)) { |
---|
433 | /* |
---|
434 | * This deals with there being only |
---|
435 | * one array to apply to all samples. |
---|
436 | */ |
---|
437 | uint32 c = |
---|
438 | (uint32)1 << td->td_bitspersample; |
---|
439 | if (dp->tdir_count == c) |
---|
440 | v = 0; |
---|
441 | TIFFSetField(tif, dp->tdir_tag, |
---|
442 | cp, cp+v, cp+2*v); |
---|
443 | } |
---|
444 | _TIFFfree(cp); |
---|
445 | } |
---|
446 | break; |
---|
447 | case TIFFTAG_PAGENUMBER: |
---|
448 | case TIFFTAG_HALFTONEHINTS: |
---|
449 | case TIFFTAG_YCBCRSUBSAMPLING: |
---|
450 | case TIFFTAG_DOTRANGE: |
---|
451 | (void) TIFFFetchShortPair(tif, dp); |
---|
452 | break; |
---|
453 | #ifdef COLORIMETRY_SUPPORT |
---|
454 | case TIFFTAG_REFERENCEBLACKWHITE: |
---|
455 | (void) TIFFFetchRefBlackWhite(tif, dp); |
---|
456 | break; |
---|
457 | #endif |
---|
458 | /* BEGIN REV 4.0 COMPATIBILITY */ |
---|
459 | case TIFFTAG_OSUBFILETYPE: |
---|
460 | v = 0; |
---|
461 | switch (TIFFExtractData(tif, dp->tdir_type, |
---|
462 | dp->tdir_offset)) { |
---|
463 | case OFILETYPE_REDUCEDIMAGE: |
---|
464 | v = FILETYPE_REDUCEDIMAGE; |
---|
465 | break; |
---|
466 | case OFILETYPE_PAGE: |
---|
467 | v = FILETYPE_PAGE; |
---|
468 | break; |
---|
469 | } |
---|
470 | if (v) |
---|
471 | (void) TIFFSetField(tif, |
---|
472 | TIFFTAG_SUBFILETYPE, (int)v); |
---|
473 | break; |
---|
474 | /* END REV 4.0 COMPATIBILITY */ |
---|
475 | default: |
---|
476 | (void) TIFFFetchNormalTag(tif, dp); |
---|
477 | break; |
---|
478 | } |
---|
479 | } |
---|
480 | /* |
---|
481 | * Verify Palette image has a Colormap. |
---|
482 | */ |
---|
483 | if (td->td_photometric == PHOTOMETRIC_PALETTE && |
---|
484 | !TIFFFieldSet(tif, FIELD_COLORMAP)) { |
---|
485 | MissingRequired(tif, "Colormap"); |
---|
486 | goto bad; |
---|
487 | } |
---|
488 | /* |
---|
489 | * Attempt to deal with a missing StripByteCounts tag. |
---|
490 | */ |
---|
491 | if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) { |
---|
492 | /* |
---|
493 | * Some manufacturers violate the spec by not giving |
---|
494 | * the size of the strips. In this case, assume there |
---|
495 | * is one uncompressed strip of data. |
---|
496 | */ |
---|
497 | if ((td->td_planarconfig == PLANARCONFIG_CONTIG && |
---|
498 | td->td_nstrips > 1) || |
---|
499 | (td->td_planarconfig == PLANARCONFIG_SEPARATE && |
---|
500 | td->td_nstrips != td->td_samplesperpixel)) { |
---|
501 | MissingRequired(tif, "StripByteCounts"); |
---|
502 | goto bad; |
---|
503 | } |
---|
504 | TIFFWarning(tif->tif_name, |
---|
505 | "TIFF directory is missing required \"%s\" field, calculating from imagelength", |
---|
506 | _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name); |
---|
507 | EstimateStripByteCounts(tif, dir, dircount); |
---|
508 | #define BYTECOUNTLOOKSBAD \ |
---|
509 | ((td->td_stripbytecount[0] == 0 && td->td_stripoffset[0] != 0) || \ |
---|
510 | (td->td_compression == COMPRESSION_NONE && \ |
---|
511 | td->td_stripbytecount[0] > TIFFGetFileSize(tif) - td->td_stripoffset[0])) |
---|
512 | } else if (td->td_nstrips == 1 && BYTECOUNTLOOKSBAD) { |
---|
513 | /* |
---|
514 | * Plexus (and others) sometimes give a value |
---|
515 | * of zero for a tag when they don't know what |
---|
516 | * the correct value is! Try and handle the |
---|
517 | * simple case of estimating the size of a one |
---|
518 | * strip image. |
---|
519 | */ |
---|
520 | TIFFWarning(tif->tif_name, |
---|
521 | "Bogus \"%s\" field, ignoring and calculating from imagelength", |
---|
522 | _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name); |
---|
523 | EstimateStripByteCounts(tif, dir, dircount); |
---|
524 | } |
---|
525 | if (dir) |
---|
526 | _TIFFfree((char *)dir); |
---|
527 | if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE)) |
---|
528 | td->td_maxsamplevalue = (uint16)((1L<<td->td_bitspersample)-1); |
---|
529 | /* |
---|
530 | * Setup default compression scheme. |
---|
531 | */ |
---|
532 | if (!TIFFFieldSet(tif, FIELD_COMPRESSION)) |
---|
533 | TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); |
---|
534 | /* |
---|
535 | * Some manufacturers make life difficult by writing |
---|
536 | * large amounts of uncompressed data as a single strip. |
---|
537 | * This is contrary to the recommendations of the spec. |
---|
538 | * The following makes an attempt at breaking such images |
---|
539 | * into strips closer to the recommended 8k bytes. A |
---|
540 | * side effect, however, is that the RowsPerStrip tag |
---|
541 | * value may be changed. |
---|
542 | */ |
---|
543 | if (td->td_nstrips == 1 && td->td_compression == COMPRESSION_NONE && |
---|
544 | (tif->tif_flags & (TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP) |
---|
545 | ChopUpSingleUncompressedStrip(tif); |
---|
546 | /* |
---|
547 | * Reinitialize i/o since we are starting on a new directory. |
---|
548 | */ |
---|
549 | tif->tif_row = (uint32) -1; |
---|
550 | tif->tif_curstrip = (tstrip_t) -1; |
---|
551 | tif->tif_col = (uint32) -1; |
---|
552 | tif->tif_curtile = (ttile_t) -1; |
---|
553 | tif->tif_tilesize = TIFFTileSize(tif); |
---|
554 | tif->tif_scanlinesize = TIFFScanlineSize(tif); |
---|
555 | return (1); |
---|
556 | bad: |
---|
557 | if (dir) |
---|
558 | _TIFFfree(dir); |
---|
559 | return (0); |
---|
560 | } |
---|
561 | |
---|
562 | static void |
---|
563 | EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount) |
---|
564 | { |
---|
565 | register TIFFDirEntry *dp; |
---|
566 | register TIFFDirectory *td = &tif->tif_dir; |
---|
567 | uint16 i; |
---|
568 | |
---|
569 | if (td->td_stripbytecount) |
---|
570 | _TIFFfree(td->td_stripbytecount); |
---|
571 | td->td_stripbytecount = (uint32*) |
---|
572 | CheckMalloc(tif, td->td_nstrips * sizeof (uint32), |
---|
573 | "for \"StripByteCounts\" array"); |
---|
574 | if (td->td_compression != COMPRESSION_NONE) { |
---|
575 | uint32 space = (uint32)(sizeof (TIFFHeader) |
---|
576 | + sizeof (uint16) |
---|
577 | + (dircount * sizeof (TIFFDirEntry)) |
---|
578 | + sizeof (uint32)); |
---|
579 | toff_t filesize = TIFFGetFileSize(tif); |
---|
580 | uint16 n; |
---|
581 | |
---|
582 | /* calculate amount of space used by indirect values */ |
---|
583 | for (dp = dir, n = dircount; n > 0; n--, dp++) { |
---|
584 | uint32 cc = dp->tdir_count*tiffDataWidth[dp->tdir_type]; |
---|
585 | if (cc > sizeof (uint32)) |
---|
586 | space += cc; |
---|
587 | } |
---|
588 | space = filesize - space; |
---|
589 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
---|
590 | space /= td->td_samplesperpixel; |
---|
591 | for (i = 0; i < td->td_nstrips; i++) |
---|
592 | td->td_stripbytecount[i] = space; |
---|
593 | /* |
---|
594 | * This gross hack handles the case were the offset to |
---|
595 | * the last strip is past the place where we think the strip |
---|
596 | * should begin. Since a strip of data must be contiguous, |
---|
597 | * it's safe to assume that we've overestimated the amount |
---|
598 | * of data in the strip and trim this number back accordingly. |
---|
599 | */ |
---|
600 | i--; |
---|
601 | if (((toff_t)(td->td_stripoffset[i]+td->td_stripbytecount[i])) |
---|
602 | > filesize) |
---|
603 | td->td_stripbytecount[i] = |
---|
604 | filesize - td->td_stripoffset[i]; |
---|
605 | } else { |
---|
606 | uint32 rowbytes = TIFFScanlineSize(tif); |
---|
607 | uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage; |
---|
608 | for (i = 0; i < td->td_nstrips; i++) |
---|
609 | td->td_stripbytecount[i] = rowbytes*rowsperstrip; |
---|
610 | } |
---|
611 | TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS); |
---|
612 | if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP)) |
---|
613 | td->td_rowsperstrip = td->td_imagelength; |
---|
614 | } |
---|
615 | |
---|
616 | static void |
---|
617 | MissingRequired(TIFF* tif, const char* tagname) |
---|
618 | { |
---|
619 | TIFFError(tif->tif_name, |
---|
620 | "TIFF directory is missing required \"%s\" field", tagname); |
---|
621 | } |
---|
622 | |
---|
623 | /* |
---|
624 | * Check the count field of a directory |
---|
625 | * entry against a known value. The caller |
---|
626 | * is expected to skip/ignore the tag if |
---|
627 | * there is a mismatch. |
---|
628 | */ |
---|
629 | static int |
---|
630 | CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count) |
---|
631 | { |
---|
632 | if (count != dir->tdir_count) { |
---|
633 | TIFFWarning(tif->tif_name, |
---|
634 | "incorrect count for field \"%s\" (%lu, expecting %lu); tag ignored", |
---|
635 | _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name, |
---|
636 | dir->tdir_count, count); |
---|
637 | return (0); |
---|
638 | } |
---|
639 | return (1); |
---|
640 | } |
---|
641 | |
---|
642 | /* |
---|
643 | * Fetch a contiguous directory item. |
---|
644 | */ |
---|
645 | static tsize_t |
---|
646 | TIFFFetchData(TIFF* tif, TIFFDirEntry* dir, char* cp) |
---|
647 | { |
---|
648 | int w = tiffDataWidth[dir->tdir_type]; |
---|
649 | tsize_t cc = dir->tdir_count * w; |
---|
650 | |
---|
651 | if (!isMapped(tif)) { |
---|
652 | if (!SeekOK(tif, dir->tdir_offset)) |
---|
653 | goto bad; |
---|
654 | if (!ReadOK(tif, cp, cc)) |
---|
655 | goto bad; |
---|
656 | } else { |
---|
657 | if (dir->tdir_offset + cc > tif->tif_size) |
---|
658 | goto bad; |
---|
659 | _TIFFmemcpy(cp, tif->tif_base + dir->tdir_offset, cc); |
---|
660 | } |
---|
661 | if (tif->tif_flags & TIFF_SWAB) { |
---|
662 | switch (dir->tdir_type) { |
---|
663 | case TIFF_SHORT: |
---|
664 | case TIFF_SSHORT: |
---|
665 | TIFFSwabArrayOfShort((uint16*) cp, dir->tdir_count); |
---|
666 | break; |
---|
667 | case TIFF_LONG: |
---|
668 | case TIFF_SLONG: |
---|
669 | case TIFF_FLOAT: |
---|
670 | TIFFSwabArrayOfLong((uint32*) cp, dir->tdir_count); |
---|
671 | break; |
---|
672 | case TIFF_RATIONAL: |
---|
673 | case TIFF_SRATIONAL: |
---|
674 | TIFFSwabArrayOfLong((uint32*) cp, 2*dir->tdir_count); |
---|
675 | break; |
---|
676 | case TIFF_DOUBLE: |
---|
677 | TIFFSwabArrayOfDouble((double*) cp, dir->tdir_count); |
---|
678 | break; |
---|
679 | } |
---|
680 | } |
---|
681 | return (cc); |
---|
682 | bad: |
---|
683 | TIFFError(tif->tif_name, "Error fetching data for field \"%s\"", |
---|
684 | _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name); |
---|
685 | return ((tsize_t) 0); |
---|
686 | } |
---|
687 | |
---|
688 | /* |
---|
689 | * Fetch an ASCII item from the file. |
---|
690 | */ |
---|
691 | static tsize_t |
---|
692 | TIFFFetchString(TIFF* tif, TIFFDirEntry* dir, char* cp) |
---|
693 | { |
---|
694 | if (dir->tdir_count <= 4) { |
---|
695 | uint32 l = dir->tdir_offset; |
---|
696 | if (tif->tif_flags & TIFF_SWAB) |
---|
697 | TIFFSwabLong(&l); |
---|
698 | _TIFFmemcpy(cp, &l, dir->tdir_count); |
---|
699 | return (1); |
---|
700 | } |
---|
701 | return (TIFFFetchData(tif, dir, cp)); |
---|
702 | } |
---|
703 | |
---|
704 | /* |
---|
705 | * Convert numerator+denominator to float. |
---|
706 | */ |
---|
707 | static int |
---|
708 | cvtRational(TIFF* tif, TIFFDirEntry* dir, uint32 num, uint32 denom, float* rv) |
---|
709 | { |
---|
710 | if (denom == 0) { |
---|
711 | TIFFError(tif->tif_name, |
---|
712 | "%s: Rational with zero denominator (num = %lu)", |
---|
713 | _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name, num); |
---|
714 | return (0); |
---|
715 | } else { |
---|
716 | if (dir->tdir_type == TIFF_RATIONAL) |
---|
717 | *rv = ((float)num / (float)denom); |
---|
718 | else |
---|
719 | *rv = ((float)(int32)num / (float)(int32)denom); |
---|
720 | return (1); |
---|
721 | } |
---|
722 | } |
---|
723 | |
---|
724 | /* |
---|
725 | * Fetch a rational item from the file |
---|
726 | * at offset off and return the value |
---|
727 | * as a floating point number. |
---|
728 | */ |
---|
729 | static float |
---|
730 | TIFFFetchRational(TIFF* tif, TIFFDirEntry* dir) |
---|
731 | { |
---|
732 | uint32 l[2]; |
---|
733 | float v; |
---|
734 | |
---|
735 | return (!TIFFFetchData(tif, dir, (char *)l) || |
---|
736 | !cvtRational(tif, dir, l[0], l[1], &v) ? 1.0f : v); |
---|
737 | } |
---|
738 | |
---|
739 | /* |
---|
740 | * Fetch a single floating point value |
---|
741 | * from the offset field and return it |
---|
742 | * as a native float. |
---|
743 | */ |
---|
744 | static float |
---|
745 | TIFFFetchFloat(TIFF* tif, TIFFDirEntry* dir) |
---|
746 | { |
---|
747 | long l = TIFFExtractData(tif, dir->tdir_type, dir->tdir_offset); |
---|
748 | float v = *(float*) &l; |
---|
749 | TIFFCvtIEEEFloatToNative(tif, 1, &v); |
---|
750 | return (v); |
---|
751 | } |
---|
752 | |
---|
753 | /* |
---|
754 | * Fetch an array of BYTE or SBYTE values. |
---|
755 | */ |
---|
756 | static int |
---|
757 | TIFFFetchByteArray(TIFF* tif, TIFFDirEntry* dir, uint16* v) |
---|
758 | { |
---|
759 | if (dir->tdir_count <= 4) { |
---|
760 | /* |
---|
761 | * Extract data from offset field. |
---|
762 | */ |
---|
763 | if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) { |
---|
764 | switch (dir->tdir_count) { |
---|
765 | case 4: v[3] = (uint16)(dir->tdir_offset & 0xff); |
---|
766 | case 3: v[2] = (uint16)((dir->tdir_offset >> 8) & 0xff); |
---|
767 | case 2: v[1] = (uint16)((dir->tdir_offset >> 16) & 0xff); |
---|
768 | case 1: v[0] = (uint16)(dir->tdir_offset >> 24); |
---|
769 | } |
---|
770 | } else { |
---|
771 | switch (dir->tdir_count) { |
---|
772 | case 4: v[3] = (uint16)(dir->tdir_offset >> 24); |
---|
773 | case 3: v[2] = (uint16)((dir->tdir_offset >> 16) & 0xff); |
---|
774 | case 2: v[1] = (uint16)((dir->tdir_offset >> 8) & 0xff); |
---|
775 | case 1: v[0] = (uint16)(dir->tdir_offset & 0xff); |
---|
776 | } |
---|
777 | } |
---|
778 | return (1); |
---|
779 | } else |
---|
780 | return (TIFFFetchData(tif, dir, (char*) v) != 0); /* XXX */ |
---|
781 | } |
---|
782 | |
---|
783 | /* |
---|
784 | * Fetch an array of SHORT or SSHORT values. |
---|
785 | */ |
---|
786 | static int |
---|
787 | TIFFFetchShortArray(TIFF* tif, TIFFDirEntry* dir, uint16* v) |
---|
788 | { |
---|
789 | if (dir->tdir_count <= 2) { |
---|
790 | if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) { |
---|
791 | switch (dir->tdir_count) { |
---|
792 | case 2: v[1] = (uint16) (dir->tdir_offset & 0xffff); |
---|
793 | case 1: v[0] = (uint16) (dir->tdir_offset >> 16); |
---|
794 | } |
---|
795 | } else { |
---|
796 | switch (dir->tdir_count) { |
---|
797 | case 2: v[1] = (uint16) (dir->tdir_offset >> 16); |
---|
798 | case 1: v[0] = (uint16) (dir->tdir_offset & 0xffff); |
---|
799 | } |
---|
800 | } |
---|
801 | return (1); |
---|
802 | } else |
---|
803 | return (TIFFFetchData(tif, dir, (char *)v) != 0); |
---|
804 | } |
---|
805 | |
---|
806 | /* |
---|
807 | * Fetch a pair of SHORT or BYTE values. |
---|
808 | */ |
---|
809 | static int |
---|
810 | TIFFFetchShortPair(TIFF* tif, TIFFDirEntry* dir) |
---|
811 | { |
---|
812 | uint16 v[2]; |
---|
813 | int ok = 0; |
---|
814 | |
---|
815 | switch (dir->tdir_type) { |
---|
816 | case TIFF_SHORT: |
---|
817 | case TIFF_SSHORT: |
---|
818 | ok = TIFFFetchShortArray(tif, dir, v); |
---|
819 | break; |
---|
820 | case TIFF_BYTE: |
---|
821 | case TIFF_SBYTE: |
---|
822 | ok = TIFFFetchByteArray(tif, dir, v); |
---|
823 | break; |
---|
824 | } |
---|
825 | if (ok) |
---|
826 | TIFFSetField(tif, dir->tdir_tag, v[0], v[1]); |
---|
827 | return (ok); |
---|
828 | } |
---|
829 | |
---|
830 | /* |
---|
831 | * Fetch an array of LONG or SLONG values. |
---|
832 | */ |
---|
833 | static int |
---|
834 | TIFFFetchLongArray(TIFF* tif, TIFFDirEntry* dir, uint32* v) |
---|
835 | { |
---|
836 | if (dir->tdir_count == 1) { |
---|
837 | v[0] = dir->tdir_offset; |
---|
838 | return (1); |
---|
839 | } else |
---|
840 | return (TIFFFetchData(tif, dir, (char*) v) != 0); |
---|
841 | } |
---|
842 | |
---|
843 | /* |
---|
844 | * Fetch an array of RATIONAL or SRATIONAL values. |
---|
845 | */ |
---|
846 | static int |
---|
847 | TIFFFetchRationalArray(TIFF* tif, TIFFDirEntry* dir, float* v) |
---|
848 | { |
---|
849 | int ok = 0; |
---|
850 | uint32* l; |
---|
851 | |
---|
852 | l = (uint32*)CheckMalloc(tif, |
---|
853 | dir->tdir_count*tiffDataWidth[dir->tdir_type], |
---|
854 | "to fetch array of rationals"); |
---|
855 | if (l) { |
---|
856 | if (TIFFFetchData(tif, dir, (char *)l)) { |
---|
857 | uint32 i; |
---|
858 | for (i = 0; i < dir->tdir_count; i++) { |
---|
859 | ok = cvtRational(tif, dir, |
---|
860 | l[2*i+0], l[2*i+1], &v[i]); |
---|
861 | if (!ok) |
---|
862 | break; |
---|
863 | } |
---|
864 | } |
---|
865 | _TIFFfree((char *)l); |
---|
866 | } |
---|
867 | return (ok); |
---|
868 | } |
---|
869 | |
---|
870 | /* |
---|
871 | * Fetch an array of FLOAT values. |
---|
872 | */ |
---|
873 | static int |
---|
874 | TIFFFetchFloatArray(TIFF* tif, TIFFDirEntry* dir, float* v) |
---|
875 | { |
---|
876 | |
---|
877 | if (dir->tdir_count == 1) { |
---|
878 | v[0] = *(float*) &dir->tdir_offset; |
---|
879 | TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v); |
---|
880 | return (1); |
---|
881 | } else if (TIFFFetchData(tif, dir, (char*) v)) { |
---|
882 | TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v); |
---|
883 | return (1); |
---|
884 | } else |
---|
885 | return (0); |
---|
886 | } |
---|
887 | |
---|
888 | /* |
---|
889 | * Fetch an array of DOUBLE values. |
---|
890 | */ |
---|
891 | static int |
---|
892 | TIFFFetchDoubleArray(TIFF* tif, TIFFDirEntry* dir, double* v) |
---|
893 | { |
---|
894 | if (TIFFFetchData(tif, dir, (char*) v)) { |
---|
895 | TIFFCvtIEEEDoubleToNative(tif, dir->tdir_count, v); |
---|
896 | return (1); |
---|
897 | } else |
---|
898 | return (0); |
---|
899 | } |
---|
900 | |
---|
901 | /* |
---|
902 | * Fetch an array of ANY values. The actual values are |
---|
903 | * returned as doubles which should be able hold all the |
---|
904 | * types. Yes, there really should be an tany_t to avoid |
---|
905 | * this potential non-portability ... Note in particular |
---|
906 | * that we assume that the double return value vector is |
---|
907 | * large enough to read in any fundamental type. We use |
---|
908 | * that vector as a buffer to read in the base type vector |
---|
909 | * and then convert it in place to double (from end |
---|
910 | * to front of course). |
---|
911 | */ |
---|
912 | static int |
---|
913 | TIFFFetchAnyArray(TIFF* tif, TIFFDirEntry* dir, double* v) |
---|
914 | { |
---|
915 | int i; |
---|
916 | |
---|
917 | switch (dir->tdir_type) { |
---|
918 | case TIFF_BYTE: |
---|
919 | case TIFF_SBYTE: |
---|
920 | if (!TIFFFetchByteArray(tif, dir, (uint16*) v)) |
---|
921 | return (0); |
---|
922 | if (dir->tdir_type == TIFF_BYTE) { |
---|
923 | uint16* vp = (uint16*) v; |
---|
924 | for (i = dir->tdir_count-1; i >= 0; i--) |
---|
925 | v[i] = vp[i]; |
---|
926 | } else { |
---|
927 | int16* vp = (int16*) v; |
---|
928 | for (i = dir->tdir_count-1; i >= 0; i--) |
---|
929 | v[i] = vp[i]; |
---|
930 | } |
---|
931 | break; |
---|
932 | case TIFF_SHORT: |
---|
933 | case TIFF_SSHORT: |
---|
934 | if (!TIFFFetchShortArray(tif, dir, (uint16*) v)) |
---|
935 | return (0); |
---|
936 | if (dir->tdir_type == TIFF_SHORT) { |
---|
937 | uint16* vp = (uint16*) v; |
---|
938 | for (i = dir->tdir_count-1; i >= 0; i--) |
---|
939 | v[i] = vp[i]; |
---|
940 | } else { |
---|
941 | int16* vp = (int16*) v; |
---|
942 | for (i = dir->tdir_count-1; i >= 0; i--) |
---|
943 | v[i] = vp[i]; |
---|
944 | } |
---|
945 | break; |
---|
946 | case TIFF_LONG: |
---|
947 | case TIFF_SLONG: |
---|
948 | if (!TIFFFetchLongArray(tif, dir, (uint32*) v)) |
---|
949 | return (0); |
---|
950 | if (dir->tdir_type == TIFF_LONG) { |
---|
951 | uint32* vp = (uint32*) v; |
---|
952 | for (i = dir->tdir_count-1; i >= 0; i--) |
---|
953 | v[i] = vp[i]; |
---|
954 | } else { |
---|
955 | int32* vp = (int32*) v; |
---|
956 | for (i = dir->tdir_count-1; i >= 0; i--) |
---|
957 | v[i] = vp[i]; |
---|
958 | } |
---|
959 | break; |
---|
960 | case TIFF_RATIONAL: |
---|
961 | case TIFF_SRATIONAL: |
---|
962 | if (!TIFFFetchRationalArray(tif, dir, (float*) v)) |
---|
963 | return (0); |
---|
964 | { float* vp = (float*) v; |
---|
965 | for (i = dir->tdir_count-1; i >= 0; i--) |
---|
966 | v[i] = vp[i]; |
---|
967 | } |
---|
968 | break; |
---|
969 | case TIFF_FLOAT: |
---|
970 | if (!TIFFFetchFloatArray(tif, dir, (float*) v)) |
---|
971 | return (0); |
---|
972 | { float* vp = (float*) v; |
---|
973 | for (i = dir->tdir_count-1; i >= 0; i--) |
---|
974 | v[i] = vp[i]; |
---|
975 | } |
---|
976 | break; |
---|
977 | case TIFF_DOUBLE: |
---|
978 | return (TIFFFetchDoubleArray(tif, dir, (double*) v)); |
---|
979 | default: |
---|
980 | /* TIFF_NOTYPE */ |
---|
981 | /* TIFF_ASCII */ |
---|
982 | /* TIFF_UNDEFINED */ |
---|
983 | TIFFError(tif->tif_name, |
---|
984 | "Cannot read TIFF_ANY type %d for field \"%s\"", |
---|
985 | _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name); |
---|
986 | return (0); |
---|
987 | } |
---|
988 | return (1); |
---|
989 | } |
---|
990 | |
---|
991 | /* |
---|
992 | * Fetch a tag that is not handled by special case code. |
---|
993 | */ |
---|
994 | static int |
---|
995 | TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp) |
---|
996 | { |
---|
997 | static const char mesg[] = "to fetch tag value"; |
---|
998 | int ok = 0; |
---|
999 | const TIFFFieldInfo* fip = _TIFFFieldWithTag(tif, dp->tdir_tag); |
---|
1000 | |
---|
1001 | if (dp->tdir_count > 1) { /* array of values */ |
---|
1002 | char* cp = NULL; |
---|
1003 | |
---|
1004 | switch (dp->tdir_type) { |
---|
1005 | case TIFF_BYTE: |
---|
1006 | case TIFF_SBYTE: |
---|
1007 | /* NB: always expand BYTE values to shorts */ |
---|
1008 | cp = CheckMalloc(tif, |
---|
1009 | dp->tdir_count * sizeof (uint16), mesg); |
---|
1010 | ok = cp && TIFFFetchByteArray(tif, dp, (uint16*) cp); |
---|
1011 | break; |
---|
1012 | case TIFF_SHORT: |
---|
1013 | case TIFF_SSHORT: |
---|
1014 | cp = CheckMalloc(tif, |
---|
1015 | dp->tdir_count * sizeof (uint16), mesg); |
---|
1016 | ok = cp && TIFFFetchShortArray(tif, dp, (uint16*) cp); |
---|
1017 | break; |
---|
1018 | case TIFF_LONG: |
---|
1019 | case TIFF_SLONG: |
---|
1020 | cp = CheckMalloc(tif, |
---|
1021 | dp->tdir_count * sizeof (uint32), mesg); |
---|
1022 | ok = cp && TIFFFetchLongArray(tif, dp, (uint32*) cp); |
---|
1023 | break; |
---|
1024 | case TIFF_RATIONAL: |
---|
1025 | case TIFF_SRATIONAL: |
---|
1026 | cp = CheckMalloc(tif, |
---|
1027 | dp->tdir_count * sizeof (float), mesg); |
---|
1028 | ok = cp && TIFFFetchRationalArray(tif, dp, (float*) cp); |
---|
1029 | break; |
---|
1030 | case TIFF_FLOAT: |
---|
1031 | cp = CheckMalloc(tif, |
---|
1032 | dp->tdir_count * sizeof (float), mesg); |
---|
1033 | ok = cp && TIFFFetchFloatArray(tif, dp, (float*) cp); |
---|
1034 | break; |
---|
1035 | case TIFF_DOUBLE: |
---|
1036 | cp = CheckMalloc(tif, |
---|
1037 | dp->tdir_count * sizeof (double), mesg); |
---|
1038 | ok = cp && TIFFFetchDoubleArray(tif, dp, (double*) cp); |
---|
1039 | break; |
---|
1040 | case TIFF_ASCII: |
---|
1041 | case TIFF_UNDEFINED: /* bit of a cheat... */ |
---|
1042 | /* |
---|
1043 | * Some vendors write strings w/o the trailing |
---|
1044 | * NULL byte, so always append one just in case. |
---|
1045 | */ |
---|
1046 | cp = CheckMalloc(tif, dp->tdir_count+1, mesg); |
---|
1047 | if( (ok = (cp && TIFFFetchString(tif, dp, cp))) != 0 ) |
---|
1048 | cp[dp->tdir_count] = '\0'; /* XXX */ |
---|
1049 | break; |
---|
1050 | } |
---|
1051 | if (ok) { |
---|
1052 | ok = (fip->field_passcount ? |
---|
1053 | TIFFSetField(tif, dp->tdir_tag, dp->tdir_count, cp) |
---|
1054 | : TIFFSetField(tif, dp->tdir_tag, cp)); |
---|
1055 | } |
---|
1056 | if (cp != NULL) |
---|
1057 | _TIFFfree(cp); |
---|
1058 | } else if (CheckDirCount(tif, dp, 1)) { /* singleton value */ |
---|
1059 | switch (dp->tdir_type) { |
---|
1060 | case TIFF_BYTE: |
---|
1061 | case TIFF_SBYTE: |
---|
1062 | case TIFF_SHORT: |
---|
1063 | case TIFF_SSHORT: |
---|
1064 | /* |
---|
1065 | * If the tag is also acceptable as a LONG or SLONG |
---|
1066 | * then TIFFSetField will expect an uint32 parameter |
---|
1067 | * passed to it (through varargs). Thus, for machines |
---|
1068 | * where sizeof (int) != sizeof (uint32) we must do |
---|
1069 | * a careful check here. It's hard to say if this |
---|
1070 | * is worth optimizing. |
---|
1071 | * |
---|
1072 | * NB: We use TIFFFieldWithTag here knowing that |
---|
1073 | * it returns us the first entry in the table |
---|
1074 | * for the tag and that that entry is for the |
---|
1075 | * widest potential data type the tag may have. |
---|
1076 | */ |
---|
1077 | { TIFFDataType type = fip->field_type; |
---|
1078 | if (type != TIFF_LONG && type != TIFF_SLONG) { |
---|
1079 | uint16 v = (uint16) |
---|
1080 | TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset); |
---|
1081 | ok = (fip->field_passcount ? |
---|
1082 | TIFFSetField(tif, dp->tdir_tag, 1, &v) |
---|
1083 | : TIFFSetField(tif, dp->tdir_tag, v)); |
---|
1084 | break; |
---|
1085 | } |
---|
1086 | } |
---|
1087 | /* fall thru... */ |
---|
1088 | case TIFF_LONG: |
---|
1089 | case TIFF_SLONG: |
---|
1090 | { uint32 v32 = |
---|
1091 | TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset); |
---|
1092 | ok = (fip->field_passcount ? |
---|
1093 | TIFFSetField(tif, dp->tdir_tag, 1, &v32) |
---|
1094 | : TIFFSetField(tif, dp->tdir_tag, v32)); |
---|
1095 | } |
---|
1096 | break; |
---|
1097 | case TIFF_RATIONAL: |
---|
1098 | case TIFF_SRATIONAL: |
---|
1099 | case TIFF_FLOAT: |
---|
1100 | { float v = (dp->tdir_type == TIFF_FLOAT ? |
---|
1101 | TIFFFetchFloat(tif, dp) |
---|
1102 | : TIFFFetchRational(tif, dp)); |
---|
1103 | ok = (fip->field_passcount ? |
---|
1104 | TIFFSetField(tif, dp->tdir_tag, 1, &v) |
---|
1105 | : TIFFSetField(tif, dp->tdir_tag, v)); |
---|
1106 | } |
---|
1107 | break; |
---|
1108 | case TIFF_DOUBLE: |
---|
1109 | { double v; |
---|
1110 | ok = (TIFFFetchDoubleArray(tif, dp, &v) && |
---|
1111 | (fip->field_passcount ? |
---|
1112 | TIFFSetField(tif, dp->tdir_tag, 1, &v) |
---|
1113 | : TIFFSetField(tif, dp->tdir_tag, v)) |
---|
1114 | ); |
---|
1115 | } |
---|
1116 | break; |
---|
1117 | case TIFF_ASCII: |
---|
1118 | case TIFF_UNDEFINED: /* bit of a cheat... */ |
---|
1119 | { char c[2]; |
---|
1120 | if( (ok = (TIFFFetchString(tif, dp, c) != 0)) != 0 ){ |
---|
1121 | c[1] = '\0'; /* XXX paranoid */ |
---|
1122 | ok = TIFFSetField(tif, dp->tdir_tag, c); |
---|
1123 | } |
---|
1124 | } |
---|
1125 | break; |
---|
1126 | } |
---|
1127 | } |
---|
1128 | return (ok); |
---|
1129 | } |
---|
1130 | |
---|
1131 | #define NITEMS(x) (sizeof (x) / sizeof (x[0])) |
---|
1132 | /* |
---|
1133 | * Fetch samples/pixel short values for |
---|
1134 | * the specified tag and verify that |
---|
1135 | * all values are the same. |
---|
1136 | */ |
---|
1137 | static int |
---|
1138 | TIFFFetchPerSampleShorts(TIFF* tif, TIFFDirEntry* dir, int* pl) |
---|
1139 | { |
---|
1140 | int samples = tif->tif_dir.td_samplesperpixel; |
---|
1141 | int status = 0; |
---|
1142 | |
---|
1143 | if (CheckDirCount(tif, dir, (uint32) samples)) { |
---|
1144 | uint16 buf[10]; |
---|
1145 | uint16* v = buf; |
---|
1146 | |
---|
1147 | if (samples > NITEMS(buf)) |
---|
1148 | v = (uint16*) _TIFFmalloc(samples * sizeof (uint16)); |
---|
1149 | if (TIFFFetchShortArray(tif, dir, v)) { |
---|
1150 | int i; |
---|
1151 | for (i = 1; i < samples; i++) |
---|
1152 | if (v[i] != v[0]) { |
---|
1153 | TIFFError(tif->tif_name, |
---|
1154 | "Cannot handle different per-sample values for field \"%s\"", |
---|
1155 | _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name); |
---|
1156 | goto bad; |
---|
1157 | } |
---|
1158 | *pl = v[0]; |
---|
1159 | status = 1; |
---|
1160 | } |
---|
1161 | bad: |
---|
1162 | if (v != buf) |
---|
1163 | _TIFFfree((char*) v); |
---|
1164 | } |
---|
1165 | return (status); |
---|
1166 | } |
---|
1167 | |
---|
1168 | /* |
---|
1169 | * Fetch samples/pixel ANY values for |
---|
1170 | * the specified tag and verify that |
---|
1171 | * all values are the same. |
---|
1172 | */ |
---|
1173 | static int |
---|
1174 | TIFFFetchPerSampleAnys(TIFF* tif, TIFFDirEntry* dir, double* pl) |
---|
1175 | { |
---|
1176 | int samples = (int) tif->tif_dir.td_samplesperpixel; |
---|
1177 | int status = 0; |
---|
1178 | |
---|
1179 | if (CheckDirCount(tif, dir, (uint32) samples)) { |
---|
1180 | double buf[10]; |
---|
1181 | double* v = buf; |
---|
1182 | |
---|
1183 | if (samples > NITEMS(buf)) |
---|
1184 | v = (double*) _TIFFmalloc(samples * sizeof (double)); |
---|
1185 | if (TIFFFetchAnyArray(tif, dir, v)) { |
---|
1186 | int i; |
---|
1187 | for (i = 1; i < samples; i++) |
---|
1188 | if (v[i] != v[0]) { |
---|
1189 | TIFFError(tif->tif_name, |
---|
1190 | "Cannot handle different per-sample values for field \"%s\"", |
---|
1191 | _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name); |
---|
1192 | goto bad; |
---|
1193 | } |
---|
1194 | *pl = v[0]; |
---|
1195 | status = 1; |
---|
1196 | } |
---|
1197 | bad: |
---|
1198 | if (v != buf) |
---|
1199 | _TIFFfree(v); |
---|
1200 | } |
---|
1201 | return (status); |
---|
1202 | } |
---|
1203 | #undef NITEMS |
---|
1204 | |
---|
1205 | /* |
---|
1206 | * Fetch a set of offsets or lengths. |
---|
1207 | * While this routine says "strips", |
---|
1208 | * in fact it's also used for tiles. |
---|
1209 | */ |
---|
1210 | static int |
---|
1211 | TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, long nstrips, uint32** lpp) |
---|
1212 | { |
---|
1213 | register uint32* lp; |
---|
1214 | int status; |
---|
1215 | |
---|
1216 | if (!CheckDirCount(tif, dir, (uint32) nstrips)) |
---|
1217 | return (0); |
---|
1218 | /* |
---|
1219 | * Allocate space for strip information. |
---|
1220 | */ |
---|
1221 | if (*lpp == NULL && |
---|
1222 | (*lpp = (uint32 *)CheckMalloc(tif, |
---|
1223 | nstrips * sizeof (uint32), "for strip array")) == NULL) |
---|
1224 | return (0); |
---|
1225 | lp = *lpp; |
---|
1226 | if (dir->tdir_type == (int)TIFF_SHORT) { |
---|
1227 | /* |
---|
1228 | * Handle uint16->uint32 expansion. |
---|
1229 | */ |
---|
1230 | uint16* dp = (uint16*) CheckMalloc(tif, |
---|
1231 | dir->tdir_count* sizeof (uint16), "to fetch strip tag"); |
---|
1232 | if (dp == NULL) |
---|
1233 | return (0); |
---|
1234 | if( (status = TIFFFetchShortArray(tif, dir, dp)) != 0 ) { |
---|
1235 | register uint16* wp = dp; |
---|
1236 | while (nstrips-- > 0) |
---|
1237 | *lp++ = *wp++; |
---|
1238 | } |
---|
1239 | _TIFFfree((char*) dp); |
---|
1240 | } else |
---|
1241 | status = TIFFFetchLongArray(tif, dir, lp); |
---|
1242 | return (status); |
---|
1243 | } |
---|
1244 | |
---|
1245 | #define NITEMS(x) (sizeof (x) / sizeof (x[0])) |
---|
1246 | /* |
---|
1247 | * Fetch and set the ExtraSamples tag. |
---|
1248 | */ |
---|
1249 | static int |
---|
1250 | TIFFFetchExtraSamples(TIFF* tif, TIFFDirEntry* dir) |
---|
1251 | { |
---|
1252 | uint16 buf[10]; |
---|
1253 | uint16* v = buf; |
---|
1254 | int status; |
---|
1255 | |
---|
1256 | if (dir->tdir_count > NITEMS(buf)) |
---|
1257 | v = (uint16*) _TIFFmalloc(dir->tdir_count * sizeof (uint16)); |
---|
1258 | if (dir->tdir_type == TIFF_BYTE) |
---|
1259 | status = TIFFFetchByteArray(tif, dir, v); |
---|
1260 | else |
---|
1261 | status = TIFFFetchShortArray(tif, dir, v); |
---|
1262 | if (status) |
---|
1263 | status = TIFFSetField(tif, dir->tdir_tag, dir->tdir_count, v); |
---|
1264 | if (v != buf) |
---|
1265 | _TIFFfree((char*) v); |
---|
1266 | return (status); |
---|
1267 | } |
---|
1268 | #undef NITEMS |
---|
1269 | |
---|
1270 | #ifdef COLORIMETRY_SUPPORT |
---|
1271 | /* |
---|
1272 | * Fetch and set the RefBlackWhite tag. |
---|
1273 | */ |
---|
1274 | static int |
---|
1275 | TIFFFetchRefBlackWhite(TIFF* tif, TIFFDirEntry* dir) |
---|
1276 | { |
---|
1277 | static const char mesg[] = "for \"ReferenceBlackWhite\" array"; |
---|
1278 | char* cp; |
---|
1279 | int ok; |
---|
1280 | |
---|
1281 | if (dir->tdir_type == TIFF_RATIONAL) |
---|
1282 | return (TIFFFetchNormalTag(tif, dir)); |
---|
1283 | /* |
---|
1284 | * Handle LONG's for backward compatibility. |
---|
1285 | */ |
---|
1286 | cp = CheckMalloc(tif, dir->tdir_count * sizeof (uint32), mesg); |
---|
1287 | if( (ok = (cp && TIFFFetchLongArray(tif, dir, (uint32*) cp))) != 0) { |
---|
1288 | float* fp = (float*) |
---|
1289 | CheckMalloc(tif, dir->tdir_count * sizeof (float), mesg); |
---|
1290 | if( (ok = (fp != NULL)) != 0 ) { |
---|
1291 | uint32 i; |
---|
1292 | for (i = 0; i < dir->tdir_count; i++) |
---|
1293 | fp[i] = (float)((uint32*) cp)[i]; |
---|
1294 | ok = TIFFSetField(tif, dir->tdir_tag, fp); |
---|
1295 | _TIFFfree((char*) fp); |
---|
1296 | } |
---|
1297 | } |
---|
1298 | if (cp) |
---|
1299 | _TIFFfree(cp); |
---|
1300 | return (ok); |
---|
1301 | } |
---|
1302 | #endif |
---|
1303 | |
---|
1304 | /* |
---|
1305 | * Replace a single strip (tile) of uncompressed data by |
---|
1306 | * multiple strips (tiles), each approximately 8Kbytes. |
---|
1307 | * This is useful for dealing with large images or |
---|
1308 | * for dealing with machines with a limited amount |
---|
1309 | * memory. |
---|
1310 | */ |
---|
1311 | static void |
---|
1312 | ChopUpSingleUncompressedStrip(TIFF* tif) |
---|
1313 | { |
---|
1314 | register TIFFDirectory *td = &tif->tif_dir; |
---|
1315 | uint32 bytecount = td->td_stripbytecount[0]; |
---|
1316 | uint32 offset = td->td_stripoffset[0]; |
---|
1317 | tsize_t rowbytes = TIFFVTileSize(tif, 1), stripbytes; |
---|
1318 | tstrip_t strip, nstrips, rowsperstrip; |
---|
1319 | uint32* newcounts; |
---|
1320 | uint32* newoffsets; |
---|
1321 | |
---|
1322 | /* |
---|
1323 | * Make the rows hold at least one |
---|
1324 | * scanline, but fill 8k if possible. |
---|
1325 | */ |
---|
1326 | if (rowbytes > 8192) { |
---|
1327 | stripbytes = rowbytes; |
---|
1328 | rowsperstrip = 1; |
---|
1329 | } else { |
---|
1330 | rowsperstrip = 8192 / rowbytes; |
---|
1331 | stripbytes = rowbytes * rowsperstrip; |
---|
1332 | } |
---|
1333 | /* never increase the number of strips in an image */ |
---|
1334 | if (rowsperstrip >= td->td_rowsperstrip) |
---|
1335 | return; |
---|
1336 | nstrips = (tstrip_t) TIFFhowmany(bytecount, stripbytes); |
---|
1337 | newcounts = (uint32*) CheckMalloc(tif, nstrips * sizeof (uint32), |
---|
1338 | "for chopped \"StripByteCounts\" array"); |
---|
1339 | newoffsets = (uint32*) CheckMalloc(tif, nstrips * sizeof (uint32), |
---|
1340 | "for chopped \"StripOffsets\" array"); |
---|
1341 | if (newcounts == NULL || newoffsets == NULL) { |
---|
1342 | /* |
---|
1343 | * Unable to allocate new strip information, give |
---|
1344 | * up and use the original one strip information. |
---|
1345 | */ |
---|
1346 | if (newcounts != NULL) |
---|
1347 | _TIFFfree(newcounts); |
---|
1348 | if (newoffsets != NULL) |
---|
1349 | _TIFFfree(newoffsets); |
---|
1350 | return; |
---|
1351 | } |
---|
1352 | /* |
---|
1353 | * Fill the strip information arrays with |
---|
1354 | * new bytecounts and offsets that reflect |
---|
1355 | * the broken-up format. |
---|
1356 | */ |
---|
1357 | for (strip = 0; strip < nstrips; strip++) { |
---|
1358 | if (stripbytes > (tsize_t) bytecount) |
---|
1359 | stripbytes = bytecount; |
---|
1360 | newcounts[strip] = stripbytes; |
---|
1361 | newoffsets[strip] = offset; |
---|
1362 | offset += stripbytes; |
---|
1363 | bytecount -= stripbytes; |
---|
1364 | } |
---|
1365 | /* |
---|
1366 | * Replace old single strip info with multi-strip info. |
---|
1367 | */ |
---|
1368 | td->td_stripsperimage = td->td_nstrips = nstrips; |
---|
1369 | TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip); |
---|
1370 | |
---|
1371 | _TIFFfree(td->td_stripbytecount); |
---|
1372 | _TIFFfree(td->td_stripoffset); |
---|
1373 | td->td_stripbytecount = newcounts; |
---|
1374 | td->td_stripoffset = newoffsets; |
---|
1375 | } |
---|