1 /* $Id: tif_dirread.c,v 1.1 2005/06/17 13:54:52 vp153 Exp $ */
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
30 * Directory Read Support Routines.
34 #define IGNORE 0 /* tag placeholder used below */
37 #define TIFFCvtIEEEFloatToNative(tif, n, fp)
38 #define TIFFCvtIEEEDoubleToNative(tif, n, dp)
40 extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*);
41 extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*);
44 static int 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*, uint16*);
52 static int TIFFFetchPerSampleLongs(TIFF*, TIFFDirEntry*, uint32*);
53 static int TIFFFetchPerSampleAnys(TIFF*, TIFFDirEntry*, double*);
54 static int TIFFFetchShortArray(TIFF*, TIFFDirEntry*, uint16*);
55 static int TIFFFetchStripThing(TIFF*, TIFFDirEntry*, long, uint32**);
56 static int TIFFFetchExtraSamples(TIFF*, TIFFDirEntry*);
57 static int TIFFFetchRefBlackWhite(TIFF*, TIFFDirEntry*);
58 static float TIFFFetchFloat(TIFF*, TIFFDirEntry*);
59 static int TIFFFetchFloatArray(TIFF*, TIFFDirEntry*, float*);
60 static int TIFFFetchDoubleArray(TIFF*, TIFFDirEntry*, double*);
61 static int TIFFFetchAnyArray(TIFF*, TIFFDirEntry*, double*);
62 static int TIFFFetchShortPair(TIFF*, TIFFDirEntry*);
63 static void ChopUpSingleUncompressedStrip(TIFF*);
66 CheckMalloc(TIFF* tif, size_t nmemb, size_t elem_size, const char* what)
69 tsize_t bytes = nmemb * elem_size;
72 * XXX: Check for integer overflow.
74 if (nmemb && elem_size && bytes / elem_size == nmemb)
75 cp = (char*)_TIFFmalloc(bytes);
78 TIFFError(tif->tif_name, "No space %s", what);
84 * Read the next TIFF directory from a file
85 * and convert it to the internal format.
86 * We read directories sequentially.
89 TIFFReadDirectory(TIFF* tif)
91 static const char module[] = "TIFFReadDirectory";
93 register TIFFDirEntry* dp;
95 register TIFFDirectory* td;
100 const TIFFFieldInfo* fip;
105 int diroutoforderwarning = 0;
108 tif->tif_diroff = tif->tif_nextdiroff;
109 if (tif->tif_diroff == 0) /* no more directories */
113 * XXX: Trick to prevent IFD looping. The one can create TIFF file
114 * with looped directory pointers. We will maintain a list of already
115 * seen directories and check every IFD offset against this list.
117 for (n = 0; n < tif->tif_dirnumber; n++) {
118 if (tif->tif_dirlist[n] == tif->tif_diroff)
121 tif->tif_dirnumber++;
122 new_dirlist = _TIFFrealloc(tif->tif_dirlist,
123 tif->tif_dirnumber * sizeof(toff_t));
126 "%s: Failed to allocate space for IFD list",
130 tif->tif_dirlist = new_dirlist;
131 tif->tif_dirlist[tif->tif_dirnumber - 1] = tif->tif_diroff;
134 * Cleanup any previous compression state.
136 (*tif->tif_cleanup)(tif);
139 if (!isMapped(tif)) {
140 if (!SeekOK(tif, tif->tif_diroff)) {
142 "%s: Seek error accessing TIFF directory",
146 if (!ReadOK(tif, &dircount, sizeof (uint16))) {
148 "%s: Can not read TIFF directory count",
152 if (tif->tif_flags & TIFF_SWAB)
153 TIFFSwabShort(&dircount);
154 dir = (TIFFDirEntry *)CheckMalloc(tif,
156 sizeof (TIFFDirEntry),
157 "to read TIFF directory");
160 if (!ReadOK(tif, dir, dircount*sizeof (TIFFDirEntry))) {
162 "%.100s: Can not read TIFF directory",
167 * Read offset to next directory for sequential scans.
169 (void) ReadOK(tif, &nextdiroff, sizeof (uint32));
171 toff_t off = tif->tif_diroff;
173 if (off + sizeof (uint16) > tif->tif_size) {
175 "%s: Can not read TIFF directory count",
179 _TIFFmemcpy(&dircount, tif->tif_base + off, sizeof (uint16));
180 off += sizeof (uint16);
181 if (tif->tif_flags & TIFF_SWAB)
182 TIFFSwabShort(&dircount);
183 dir = (TIFFDirEntry *)CheckMalloc(tif,
184 dircount, sizeof (TIFFDirEntry), "to read TIFF directory");
187 if (off + dircount*sizeof (TIFFDirEntry) > tif->tif_size) {
189 "%s: Can not read TIFF directory",
193 _TIFFmemcpy(dir, tif->tif_base + off,
194 dircount*sizeof (TIFFDirEntry));
196 off += dircount* sizeof (TIFFDirEntry);
197 if (off + sizeof (uint32) <= tif->tif_size)
198 _TIFFmemcpy(&nextdiroff, tif->tif_base+off, sizeof (uint32));
200 if (tif->tif_flags & TIFF_SWAB)
201 TIFFSwabLong(&nextdiroff);
202 tif->tif_nextdiroff = nextdiroff;
204 tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
206 * Setup default value and then make a pass over
207 * the fields to check type and tag information,
208 * and to extract info required to size data
209 * structures. A second pass is made afterwards
210 * to read in everthing not taken in the first pass.
213 /* free any old stuff and reinit */
214 TIFFFreeDirectory(tif);
215 TIFFDefaultDirectory(tif);
217 * Electronic Arts writes gray-scale TIFF files
218 * without a PlanarConfiguration directory entry.
219 * Thus we setup a default value here, even though
220 * the TIFF spec says there is no default value.
222 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
225 * Sigh, we must make a separate pass through the
226 * directory for the following reason:
228 * We must process the Compression tag in the first pass
229 * in order to merge in codec-private tag definitions (otherwise
230 * we may get complaints about unknown tags). However, the
231 * Compression tag may be dependent on the SamplesPerPixel
232 * tag value because older TIFF specs permited Compression
233 * to be written as a SamplesPerPixel-count tag entry.
234 * Thus if we don't first figure out the correct SamplesPerPixel
235 * tag value then we may end up ignoring the Compression tag
236 * value because it has an incorrect count value (if the
237 * true value of SamplesPerPixel is not 1).
239 * It sure would have been nice if Aldus had really thought
240 * this stuff through carefully.
242 for (dp = dir, n = dircount; n > 0; n--, dp++) {
243 if (tif->tif_flags & TIFF_SWAB) {
244 TIFFSwabArrayOfShort(&dp->tdir_tag, 2);
245 TIFFSwabArrayOfLong(&dp->tdir_count, 2);
247 if (dp->tdir_tag == TIFFTAG_SAMPLESPERPIXEL) {
248 if (!TIFFFetchNormalTag(tif, dp))
250 dp->tdir_tag = IGNORE;
254 * First real pass over the directory.
257 for (dp = dir, n = dircount; n > 0; n--, dp++) {
259 if (fix >= tif->tif_nfields || dp->tdir_tag == IGNORE)
263 * Silicon Beach (at least) writes unordered
264 * directory tags (violating the spec). Handle
265 * it here, but be obnoxious (maybe they'll fix it?).
267 if (dp->tdir_tag < tif->tif_fieldinfo[fix]->field_tag) {
268 if (!diroutoforderwarning) {
270 "%s: invalid TIFF directory; tags are not sorted in ascending order",
272 diroutoforderwarning = 1;
274 fix = 0; /* O(n^2) */
276 while (fix < tif->tif_nfields &&
277 tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
279 if (fix >= tif->tif_nfields ||
280 tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) {
283 "%s: unknown field with tag %d (0x%x) encountered",
284 tif->tif_name, dp->tdir_tag, dp->tdir_tag,
287 TIFFMergeFieldInfo( tif,
288 _TIFFCreateAnonFieldInfo( tif,
290 (TIFFDataType) dp->tdir_type ),
293 while (fix < tif->tif_nfields &&
294 tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
298 * Null out old tags that we ignore.
300 if (tif->tif_fieldinfo[fix]->field_bit == FIELD_IGNORE) {
302 dp->tdir_tag = IGNORE;
308 fip = tif->tif_fieldinfo[fix];
309 while (dp->tdir_type != (unsigned short) fip->field_type
310 && fix < tif->tif_nfields) {
311 if (fip->field_type == TIFF_ANY) /* wildcard */
313 fip = tif->tif_fieldinfo[++fix];
314 if (fix >= tif->tif_nfields ||
315 fip->field_tag != dp->tdir_tag) {
317 "%s: wrong data type %d for \"%s\"; tag ignored",
318 tif->tif_name, dp->tdir_type,
319 tif->tif_fieldinfo[fix-1]->field_name);
324 * Check count if known in advance.
326 if (fip->field_readcount != TIFF_VARIABLE
327 && fip->field_readcount != TIFF_VARIABLE2) {
328 uint32 expected = (fip->field_readcount == TIFF_SPP) ?
329 (uint32) td->td_samplesperpixel :
330 (uint32) fip->field_readcount;
331 if (!CheckDirCount(tif, dp, expected))
335 switch (dp->tdir_tag) {
336 case TIFFTAG_COMPRESSION:
338 * The 5.0 spec says the Compression tag has
339 * one value, while earlier specs say it has
340 * one value per sample. Because of this, we
341 * accept the tag if one value is supplied.
343 if (dp->tdir_count == 1) {
344 v = TIFFExtractData(tif,
345 dp->tdir_type, dp->tdir_offset);
346 if (!TIFFSetField(tif, dp->tdir_tag, (uint16)v))
349 /* XXX: workaround for broken TIFFs */
350 } else if (dp->tdir_type == TIFF_LONG) {
351 if (!TIFFFetchPerSampleLongs(tif, dp, &v) ||
352 !TIFFSetField(tif, dp->tdir_tag, (uint16)v))
355 if (!TIFFFetchPerSampleShorts(tif, dp, &iv)
356 || !TIFFSetField(tif, dp->tdir_tag, iv))
359 dp->tdir_tag = IGNORE;
361 case TIFFTAG_STRIPOFFSETS:
362 case TIFFTAG_STRIPBYTECOUNTS:
363 case TIFFTAG_TILEOFFSETS:
364 case TIFFTAG_TILEBYTECOUNTS:
365 TIFFSetFieldBit(tif, fip->field_bit);
367 case TIFFTAG_IMAGEWIDTH:
368 case TIFFTAG_IMAGELENGTH:
369 case TIFFTAG_IMAGEDEPTH:
370 case TIFFTAG_TILELENGTH:
371 case TIFFTAG_TILEWIDTH:
372 case TIFFTAG_TILEDEPTH:
373 case TIFFTAG_PLANARCONFIG:
374 case TIFFTAG_ROWSPERSTRIP:
375 if (!TIFFFetchNormalTag(tif, dp))
377 dp->tdir_tag = IGNORE;
379 case TIFFTAG_EXTRASAMPLES:
380 (void) TIFFFetchExtraSamples(tif, dp);
381 dp->tdir_tag = IGNORE;
387 * Allocate directory structure and setup defaults.
389 if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {
390 MissingRequired(tif, "ImageLength");
393 if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {
394 MissingRequired(tif, "PlanarConfiguration");
398 * Setup appropriate structures (by strip or by tile)
400 if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
401 td->td_nstrips = TIFFNumberOfStrips(tif);
402 td->td_tilewidth = td->td_imagewidth;
403 td->td_tilelength = td->td_rowsperstrip;
404 td->td_tiledepth = td->td_imagedepth;
405 tif->tif_flags &= ~TIFF_ISTILED;
407 td->td_nstrips = TIFFNumberOfTiles(tif);
408 tif->tif_flags |= TIFF_ISTILED;
410 if (!td->td_nstrips) {
411 TIFFError(module, "%s: cannot handle zero number of %s",
412 tif->tif_name, isTiled(tif) ? "tiles" : "strips");
415 td->td_stripsperimage = td->td_nstrips;
416 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
417 td->td_stripsperimage /= td->td_samplesperpixel;
418 if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
420 isTiled(tif) ? "TileOffsets" : "StripOffsets");
425 * Second pass: extract other information.
427 for (dp = dir, n = dircount; n > 0; n--, dp++) {
428 if (dp->tdir_tag == IGNORE)
430 switch (dp->tdir_tag) {
431 case TIFFTAG_MINSAMPLEVALUE:
432 case TIFFTAG_MAXSAMPLEVALUE:
433 case TIFFTAG_BITSPERSAMPLE:
434 case TIFFTAG_DATATYPE:
435 case TIFFTAG_SAMPLEFORMAT:
437 * The 5.0 spec says the Compression tag has
438 * one value, while earlier specs say it has
439 * one value per sample. Because of this, we
440 * accept the tag if one value is supplied.
442 * The MinSampleValue, MaxSampleValue, BitsPerSample
443 * DataType and SampleFormat tags are supposed to be
444 * written as one value/sample, but some vendors
445 * incorrectly write one value only -- so we accept
446 * that as well (yech). Other vendors write correct
447 * value for NumberOfSamples, but incorrect one for
448 * BitsPerSample and friends, and we will read this
451 if (dp->tdir_count == 1) {
452 v = TIFFExtractData(tif,
453 dp->tdir_type, dp->tdir_offset);
454 if (!TIFFSetField(tif, dp->tdir_tag, (uint16)v))
456 /* XXX: workaround for broken TIFFs */
457 } else if (dp->tdir_tag == TIFFTAG_BITSPERSAMPLE
458 && dp->tdir_type == TIFF_LONG) {
459 if (!TIFFFetchPerSampleLongs(tif, dp, &v) ||
460 !TIFFSetField(tif, dp->tdir_tag, (uint16)v))
463 if (!TIFFFetchPerSampleShorts(tif, dp, &iv) ||
464 !TIFFSetField(tif, dp->tdir_tag, iv))
468 case TIFFTAG_SMINSAMPLEVALUE:
469 case TIFFTAG_SMAXSAMPLEVALUE:
470 if (!TIFFFetchPerSampleAnys(tif, dp, &dv) ||
471 !TIFFSetField(tif, dp->tdir_tag, dv))
474 case TIFFTAG_STRIPOFFSETS:
475 case TIFFTAG_TILEOFFSETS:
476 if (!TIFFFetchStripThing(tif, dp,
477 td->td_nstrips, &td->td_stripoffset))
480 case TIFFTAG_STRIPBYTECOUNTS:
481 case TIFFTAG_TILEBYTECOUNTS:
482 if (!TIFFFetchStripThing(tif, dp,
483 td->td_nstrips, &td->td_stripbytecount))
486 case TIFFTAG_COLORMAP:
487 case TIFFTAG_TRANSFERFUNCTION:
489 * TransferFunction can have either 1x or 3x data
490 * values; Colormap can have only 3x items.
492 v = 1L<<td->td_bitspersample;
493 if (dp->tdir_tag == TIFFTAG_COLORMAP ||
494 dp->tdir_count != v) {
495 if (!CheckDirCount(tif, dp, 3 * v))
499 cp = CheckMalloc(tif, dp->tdir_count, sizeof (uint16),
500 "to read \"TransferFunction\" tag");
502 if (TIFFFetchData(tif, dp, cp)) {
504 * This deals with there being only
505 * one array to apply to all samples.
507 uint32 c = 1L << td->td_bitspersample;
508 if (dp->tdir_count == c)
510 TIFFSetField(tif, dp->tdir_tag,
516 case TIFFTAG_PAGENUMBER:
517 case TIFFTAG_HALFTONEHINTS:
518 case TIFFTAG_YCBCRSUBSAMPLING:
519 case TIFFTAG_DOTRANGE:
520 (void) TIFFFetchShortPair(tif, dp);
522 case TIFFTAG_REFERENCEBLACKWHITE:
523 (void) TIFFFetchRefBlackWhite(tif, dp);
525 /* BEGIN REV 4.0 COMPATIBILITY */
526 case TIFFTAG_OSUBFILETYPE:
528 switch (TIFFExtractData(tif, dp->tdir_type,
530 case OFILETYPE_REDUCEDIMAGE:
531 v = FILETYPE_REDUCEDIMAGE;
538 TIFFSetField(tif, TIFFTAG_SUBFILETYPE, v);
540 /* END REV 4.0 COMPATIBILITY */
542 (void) TIFFFetchNormalTag(tif, dp);
547 * Verify Palette image has a Colormap.
549 if (td->td_photometric == PHOTOMETRIC_PALETTE &&
550 !TIFFFieldSet(tif, FIELD_COLORMAP)) {
551 MissingRequired(tif, "Colormap");
555 * Attempt to deal with a missing StripByteCounts tag.
557 if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
559 * Some manufacturers violate the spec by not giving
560 * the size of the strips. In this case, assume there
561 * is one uncompressed strip of data.
563 if ((td->td_planarconfig == PLANARCONFIG_CONTIG &&
564 td->td_nstrips > 1) ||
565 (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
566 td->td_nstrips != td->td_samplesperpixel)) {
567 MissingRequired(tif, "StripByteCounts");
571 "%s: TIFF directory is missing required "
572 "\"%s\" field, calculating from imagelength",
574 _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
575 if (EstimateStripByteCounts(tif, dir, dircount) < 0)
578 * Assume we have wrong StripByteCount value (in case of single strip) in
580 * - it is equal to zero along with StripOffset;
581 * - it is larger than file itself (in case of uncompressed image);
582 * - it is smaller than the size of the bytes per row multiplied on the
583 * number of rows. The last case should not be checked in the case of
584 * writing new image, because we may do not know the exact strip size
585 * until the whole image will be written and directory dumped out.
587 #define BYTECOUNTLOOKSBAD \
588 ( (td->td_stripbytecount[0] == 0 && td->td_stripoffset[0] != 0) || \
589 (td->td_compression == COMPRESSION_NONE && \
590 td->td_stripbytecount[0] > TIFFGetFileSize(tif) - td->td_stripoffset[0]) || \
591 (tif->tif_mode == O_RDONLY && \
592 td->td_compression == COMPRESSION_NONE && \
593 td->td_stripbytecount[0] < TIFFScanlineSize(tif) * td->td_imagelength) )
594 } else if (td->td_nstrips == 1 && BYTECOUNTLOOKSBAD) {
596 * Plexus (and others) sometimes give a value
597 * of zero for a tag when they don't know what
598 * the correct value is! Try and handle the
599 * simple case of estimating the size of a one
603 "%s: Bogus \"%s\" field, ignoring and calculating from imagelength",
605 _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
606 if(EstimateStripByteCounts(tif, dir, dircount) < 0)
610 _TIFFfree((char *)dir);
613 if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
614 td->td_maxsamplevalue = (uint16)((1L<<td->td_bitspersample)-1);
616 * Setup default compression scheme.
620 * XXX: We can optimize checking for the strip bounds using the sorted
621 * bytecounts array. See also comments for TIFFAppendToStrip()
622 * function in tif_write.c.
624 if (td->td_nstrips > 1) {
627 td->td_stripbytecountsorted = 1;
628 for (strip = 1; strip < td->td_nstrips; strip++) {
629 if (td->td_stripoffset[strip - 1] >
630 td->td_stripoffset[strip]) {
631 td->td_stripbytecountsorted = 0;
637 if (!TIFFFieldSet(tif, FIELD_COMPRESSION))
638 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
640 * Some manufacturers make life difficult by writing
641 * large amounts of uncompressed data as a single strip.
642 * This is contrary to the recommendations of the spec.
643 * The following makes an attempt at breaking such images
644 * into strips closer to the recommended 8k bytes. A
645 * side effect, however, is that the RowsPerStrip tag
646 * value may be changed.
648 if (td->td_nstrips == 1 && td->td_compression == COMPRESSION_NONE &&
649 (tif->tif_flags & (TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP)
650 ChopUpSingleUncompressedStrip(tif);
653 * Reinitialize i/o since we are starting on a new directory.
655 tif->tif_row = (uint32) -1;
656 tif->tif_curstrip = (tstrip_t) -1;
657 tif->tif_col = (uint32) -1;
658 tif->tif_curtile = (ttile_t) -1;
659 tif->tif_tilesize = (tsize_t) -1;
661 tif->tif_scanlinesize = TIFFScanlineSize(tif);
662 if (!tif->tif_scanlinesize) {
663 TIFFError(module, "%s: cannot handle zero scanline size",
669 tif->tif_tilesize = TIFFTileSize(tif);
670 if (!tif->tif_tilesize) {
671 TIFFError(module, "%s: cannot handle zero tile size",
676 if (!TIFFStripSize(tif)) {
677 TIFFError(module, "%s: cannot handle zero strip size",
690 EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
692 static const char module[] = "EstimateStripByteCounts";
694 register TIFFDirEntry *dp;
695 register TIFFDirectory *td = &tif->tif_dir;
698 if (td->td_stripbytecount)
699 _TIFFfree(td->td_stripbytecount);
700 td->td_stripbytecount = (uint32*)
701 CheckMalloc(tif, td->td_nstrips, sizeof (uint32),
702 "for \"StripByteCounts\" array");
703 if (td->td_compression != COMPRESSION_NONE) {
704 uint32 space = (uint32)(sizeof (TIFFHeader)
706 + (dircount * sizeof (TIFFDirEntry))
708 toff_t filesize = TIFFGetFileSize(tif);
711 /* calculate amount of space used by indirect values */
712 for (dp = dir, n = dircount; n > 0; n--, dp++)
714 uint32 cc = TIFFDataWidth((TIFFDataType) dp->tdir_type);
717 "%s: Cannot determine size of unknown tag type %d",
718 tif->tif_name, dp->tdir_type);
721 cc = cc * dp->tdir_count;
722 if (cc > sizeof (uint32))
725 space = filesize - space;
726 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
727 space /= td->td_samplesperpixel;
728 for (i = 0; i < td->td_nstrips; i++)
729 td->td_stripbytecount[i] = space;
731 * This gross hack handles the case were the offset to
732 * the last strip is past the place where we think the strip
733 * should begin. Since a strip of data must be contiguous,
734 * it's safe to assume that we've overestimated the amount
735 * of data in the strip and trim this number back accordingly.
738 if (((toff_t)(td->td_stripoffset[i]+td->td_stripbytecount[i]))
740 td->td_stripbytecount[i] =
741 filesize - td->td_stripoffset[i];
743 uint32 rowbytes = TIFFScanlineSize(tif);
744 uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
745 for (i = 0; i < td->td_nstrips; i++)
746 td->td_stripbytecount[i] = rowbytes*rowsperstrip;
748 TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
749 if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
750 td->td_rowsperstrip = td->td_imagelength;
755 MissingRequired(TIFF* tif, const char* tagname)
757 static const char module[] = "MissingRequired";
760 "%s: TIFF directory is missing required \"%s\" field",
761 tif->tif_name, tagname);
765 * Check the count field of a directory
766 * entry against a known value. The caller
767 * is expected to skip/ignore the tag if
768 * there is a mismatch.
771 CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
773 if (count > dir->tdir_count) {
774 TIFFWarning(tif->tif_name,
775 "incorrect count for field \"%s\" (%lu, expecting %lu); tag ignored",
776 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
777 dir->tdir_count, count);
779 } else if (count < dir->tdir_count) {
780 TIFFWarning(tif->tif_name,
781 "incorrect count for field \"%s\" (%lu, expecting %lu); tag trimmed",
782 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
783 dir->tdir_count, count);
790 * Fetch a contiguous directory item.
793 TIFFFetchData(TIFF* tif, TIFFDirEntry* dir, char* cp)
795 int w = TIFFDataWidth((TIFFDataType) dir->tdir_type);
796 tsize_t cc = dir->tdir_count * w;
798 if (!isMapped(tif)) {
799 if (!SeekOK(tif, dir->tdir_offset))
801 if (!ReadOK(tif, cp, cc))
804 if (dir->tdir_offset + cc > tif->tif_size)
806 _TIFFmemcpy(cp, tif->tif_base + dir->tdir_offset, cc);
808 if (tif->tif_flags & TIFF_SWAB) {
809 switch (dir->tdir_type) {
812 TIFFSwabArrayOfShort((uint16*) cp, dir->tdir_count);
817 TIFFSwabArrayOfLong((uint32*) cp, dir->tdir_count);
821 TIFFSwabArrayOfLong((uint32*) cp, 2*dir->tdir_count);
824 TIFFSwabArrayOfDouble((double*) cp, dir->tdir_count);
830 TIFFError(tif->tif_name, "Error fetching data for field \"%s\"",
831 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
832 return ((tsize_t) 0);
836 * Fetch an ASCII item from the file.
839 TIFFFetchString(TIFF* tif, TIFFDirEntry* dir, char* cp)
841 if (dir->tdir_count <= 4) {
842 uint32 l = dir->tdir_offset;
843 if (tif->tif_flags & TIFF_SWAB)
845 _TIFFmemcpy(cp, &l, dir->tdir_count);
848 return (TIFFFetchData(tif, dir, cp));
852 * Convert numerator+denominator to float.
855 cvtRational(TIFF* tif, TIFFDirEntry* dir, uint32 num, uint32 denom, float* rv)
858 TIFFError(tif->tif_name,
859 "%s: Rational with zero denominator (num = %lu)",
860 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name, num);
863 if (dir->tdir_type == TIFF_RATIONAL)
864 *rv = ((float)num / (float)denom);
866 *rv = ((float)(int32)num / (float)(int32)denom);
872 * Fetch a rational item from the file
873 * at offset off and return the value
874 * as a floating point number.
877 TIFFFetchRational(TIFF* tif, TIFFDirEntry* dir)
882 return (!TIFFFetchData(tif, dir, (char *)l) ||
883 !cvtRational(tif, dir, l[0], l[1], &v) ? 1.0f : v);
887 * Fetch a single floating point value
888 * from the offset field and return it
892 TIFFFetchFloat(TIFF* tif, TIFFDirEntry* dir)
895 int32 l = TIFFExtractData(tif, dir->tdir_type, dir->tdir_offset);
896 _TIFFmemcpy(&v, &l, sizeof(float));
897 TIFFCvtIEEEFloatToNative(tif, 1, &v);
902 * Fetch an array of BYTE or SBYTE values.
905 TIFFFetchByteArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
907 if (dir->tdir_count <= 4) {
909 * Extract data from offset field.
911 if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
912 if (dir->tdir_type == TIFF_SBYTE)
913 switch (dir->tdir_count) {
914 case 4: v[3] = (signed char)(dir->tdir_offset & 0xff);
915 case 3: v[2] = (signed char)((dir->tdir_offset >> 8) & 0xff);
916 case 2: v[1] = (signed char)((dir->tdir_offset >> 16) & 0xff);
917 case 1: v[0] = (signed char)(dir->tdir_offset >> 24);
920 switch (dir->tdir_count) {
921 case 4: v[3] = (uint16)(dir->tdir_offset & 0xff);
922 case 3: v[2] = (uint16)((dir->tdir_offset >> 8) & 0xff);
923 case 2: v[1] = (uint16)((dir->tdir_offset >> 16) & 0xff);
924 case 1: v[0] = (uint16)(dir->tdir_offset >> 24);
927 if (dir->tdir_type == TIFF_SBYTE)
928 switch (dir->tdir_count) {
929 case 4: v[3] = (signed char)(dir->tdir_offset >> 24);
930 case 3: v[2] = (signed char)((dir->tdir_offset >> 16) & 0xff);
931 case 2: v[1] = (signed char)((dir->tdir_offset >> 8) & 0xff);
932 case 1: v[0] = (signed char)(dir->tdir_offset & 0xff);
935 switch (dir->tdir_count) {
936 case 4: v[3] = (uint16)(dir->tdir_offset >> 24);
937 case 3: v[2] = (uint16)((dir->tdir_offset >> 16) & 0xff);
938 case 2: v[1] = (uint16)((dir->tdir_offset >> 8) & 0xff);
939 case 1: v[0] = (uint16)(dir->tdir_offset & 0xff);
944 return (TIFFFetchData(tif, dir, (char*) v) != 0); /* XXX */
948 * Fetch an array of SHORT or SSHORT values.
951 TIFFFetchShortArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
953 if (dir->tdir_count <= 2) {
954 if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
955 switch (dir->tdir_count) {
956 case 2: v[1] = (uint16) (dir->tdir_offset & 0xffff);
957 case 1: v[0] = (uint16) (dir->tdir_offset >> 16);
960 switch (dir->tdir_count) {
961 case 2: v[1] = (uint16) (dir->tdir_offset >> 16);
962 case 1: v[0] = (uint16) (dir->tdir_offset & 0xffff);
967 return (TIFFFetchData(tif, dir, (char *)v) != 0);
971 * Fetch a pair of SHORT or BYTE values.
974 TIFFFetchShortPair(TIFF* tif, TIFFDirEntry* dir)
979 switch (dir->tdir_type) {
982 ok = TIFFFetchShortArray(tif, dir, v);
986 ok = TIFFFetchByteArray(tif, dir, v);
990 TIFFSetField(tif, dir->tdir_tag, v[0], v[1]);
995 * Fetch an array of LONG or SLONG values.
998 TIFFFetchLongArray(TIFF* tif, TIFFDirEntry* dir, uint32* v)
1000 if (dir->tdir_count == 1) {
1001 v[0] = dir->tdir_offset;
1004 return (TIFFFetchData(tif, dir, (char*) v) != 0);
1008 * Fetch an array of RATIONAL or SRATIONAL values.
1011 TIFFFetchRationalArray(TIFF* tif, TIFFDirEntry* dir, float* v)
1016 l = (uint32*)CheckMalloc(tif,
1017 dir->tdir_count, TIFFDataWidth((TIFFDataType) dir->tdir_type),
1018 "to fetch array of rationals");
1020 if (TIFFFetchData(tif, dir, (char *)l)) {
1022 for (i = 0; i < dir->tdir_count; i++) {
1023 ok = cvtRational(tif, dir,
1024 l[2*i+0], l[2*i+1], &v[i]);
1029 _TIFFfree((char *)l);
1035 * Fetch an array of FLOAT values.
1038 TIFFFetchFloatArray(TIFF* tif, TIFFDirEntry* dir, float* v)
1041 if (dir->tdir_count == 1) {
1042 v[0] = *(float*) &dir->tdir_offset;
1043 TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v);
1045 } else if (TIFFFetchData(tif, dir, (char*) v)) {
1046 TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v);
1053 * Fetch an array of DOUBLE values.
1056 TIFFFetchDoubleArray(TIFF* tif, TIFFDirEntry* dir, double* v)
1058 if (TIFFFetchData(tif, dir, (char*) v)) {
1059 TIFFCvtIEEEDoubleToNative(tif, dir->tdir_count, v);
1066 * Fetch an array of ANY values. The actual values are
1067 * returned as doubles which should be able hold all the
1068 * types. Yes, there really should be an tany_t to avoid
1069 * this potential non-portability ... Note in particular
1070 * that we assume that the double return value vector is
1071 * large enough to read in any fundamental type. We use
1072 * that vector as a buffer to read in the base type vector
1073 * and then convert it in place to double (from end
1074 * to front of course).
1077 TIFFFetchAnyArray(TIFF* tif, TIFFDirEntry* dir, double* v)
1081 switch (dir->tdir_type) {
1084 if (!TIFFFetchByteArray(tif, dir, (uint16*) v))
1086 if (dir->tdir_type == TIFF_BYTE) {
1087 uint16* vp = (uint16*) v;
1088 for (i = dir->tdir_count-1; i >= 0; i--)
1091 int16* vp = (int16*) v;
1092 for (i = dir->tdir_count-1; i >= 0; i--)
1098 if (!TIFFFetchShortArray(tif, dir, (uint16*) v))
1100 if (dir->tdir_type == TIFF_SHORT) {
1101 uint16* vp = (uint16*) v;
1102 for (i = dir->tdir_count-1; i >= 0; i--)
1105 int16* vp = (int16*) v;
1106 for (i = dir->tdir_count-1; i >= 0; i--)
1112 if (!TIFFFetchLongArray(tif, dir, (uint32*) v))
1114 if (dir->tdir_type == TIFF_LONG) {
1115 uint32* vp = (uint32*) v;
1116 for (i = dir->tdir_count-1; i >= 0; i--)
1119 int32* vp = (int32*) v;
1120 for (i = dir->tdir_count-1; i >= 0; i--)
1125 case TIFF_SRATIONAL:
1126 if (!TIFFFetchRationalArray(tif, dir, (float*) v))
1128 { float* vp = (float*) v;
1129 for (i = dir->tdir_count-1; i >= 0; i--)
1134 if (!TIFFFetchFloatArray(tif, dir, (float*) v))
1136 { float* vp = (float*) v;
1137 for (i = dir->tdir_count-1; i >= 0; i--)
1142 return (TIFFFetchDoubleArray(tif, dir, (double*) v));
1146 /* TIFF_UNDEFINED */
1147 TIFFError(tif->tif_name,
1148 "cannot read TIFF_ANY type %d for field \"%s\"",
1149 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
1156 * Fetch a tag that is not handled by special case code.
1159 TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp)
1161 static const char mesg[] = "to fetch tag value";
1163 const TIFFFieldInfo* fip = _TIFFFieldWithTag(tif, dp->tdir_tag);
1165 if (dp->tdir_count > 1) { /* array of values */
1168 switch (dp->tdir_type) {
1171 /* NB: always expand BYTE values to shorts */
1172 cp = CheckMalloc(tif,
1173 dp->tdir_count, sizeof (uint16), mesg);
1174 ok = cp && TIFFFetchByteArray(tif, dp, (uint16*) cp);
1178 cp = CheckMalloc(tif,
1179 dp->tdir_count, sizeof (uint16), mesg);
1180 ok = cp && TIFFFetchShortArray(tif, dp, (uint16*) cp);
1184 cp = CheckMalloc(tif,
1185 dp->tdir_count, sizeof (uint32), mesg);
1186 ok = cp && TIFFFetchLongArray(tif, dp, (uint32*) cp);
1189 case TIFF_SRATIONAL:
1190 cp = CheckMalloc(tif,
1191 dp->tdir_count, sizeof (float), mesg);
1192 ok = cp && TIFFFetchRationalArray(tif, dp, (float*) cp);
1195 cp = CheckMalloc(tif,
1196 dp->tdir_count, sizeof (float), mesg);
1197 ok = cp && TIFFFetchFloatArray(tif, dp, (float*) cp);
1200 cp = CheckMalloc(tif,
1201 dp->tdir_count, sizeof (double), mesg);
1202 ok = cp && TIFFFetchDoubleArray(tif, dp, (double*) cp);
1205 case TIFF_UNDEFINED: /* bit of a cheat... */
1207 * Some vendors write strings w/o the trailing
1208 * NULL byte, so always append one just in case.
1210 cp = CheckMalloc(tif, dp->tdir_count+1, 1, mesg);
1211 if( (ok = (cp && TIFFFetchString(tif, dp, cp))) != 0 )
1212 cp[dp->tdir_count] = '\0'; /* XXX */
1216 ok = (fip->field_passcount ?
1217 TIFFSetField(tif, dp->tdir_tag, dp->tdir_count, cp)
1218 : TIFFSetField(tif, dp->tdir_tag, cp));
1222 } else if (CheckDirCount(tif, dp, 1)) { /* singleton value */
1223 switch (dp->tdir_type) {
1229 * If the tag is also acceptable as a LONG or SLONG
1230 * then TIFFSetField will expect an uint32 parameter
1231 * passed to it (through varargs). Thus, for machines
1232 * where sizeof (int) != sizeof (uint32) we must do
1233 * a careful check here. It's hard to say if this
1234 * is worth optimizing.
1236 * NB: We use TIFFFieldWithTag here knowing that
1237 * it returns us the first entry in the table
1238 * for the tag and that that entry is for the
1239 * widest potential data type the tag may have.
1241 { TIFFDataType type = fip->field_type;
1242 if (type != TIFF_LONG && type != TIFF_SLONG) {
1244 TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset);
1245 ok = (fip->field_passcount ?
1246 TIFFSetField(tif, dp->tdir_tag, 1, &v)
1247 : TIFFSetField(tif, dp->tdir_tag, v));
1255 TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset);
1256 ok = (fip->field_passcount ?
1257 TIFFSetField(tif, dp->tdir_tag, 1, &v32)
1258 : TIFFSetField(tif, dp->tdir_tag, v32));
1262 case TIFF_SRATIONAL:
1264 { float v = (dp->tdir_type == TIFF_FLOAT ?
1265 TIFFFetchFloat(tif, dp)
1266 : TIFFFetchRational(tif, dp));
1267 ok = (fip->field_passcount ?
1268 TIFFSetField(tif, dp->tdir_tag, 1, &v)
1269 : TIFFSetField(tif, dp->tdir_tag, v));
1274 ok = (TIFFFetchDoubleArray(tif, dp, &v) &&
1275 (fip->field_passcount ?
1276 TIFFSetField(tif, dp->tdir_tag, 1, &v)
1277 : TIFFSetField(tif, dp->tdir_tag, v))
1282 case TIFF_UNDEFINED: /* bit of a cheat... */
1284 if( (ok = (TIFFFetchString(tif, dp, c) != 0)) != 0 ) {
1285 c[1] = '\0'; /* XXX paranoid */
1286 ok = (fip->field_passcount ?
1287 TIFFSetField(tif, dp->tdir_tag, 1, c)
1288 : TIFFSetField(tif, dp->tdir_tag, c));
1297 #define NITEMS(x) (sizeof (x) / sizeof (x[0]))
1299 * Fetch samples/pixel short values for
1300 * the specified tag and verify that
1301 * all values are the same.
1304 TIFFFetchPerSampleShorts(TIFF* tif, TIFFDirEntry* dir, uint16* pl)
1306 uint16 samples = tif->tif_dir.td_samplesperpixel;
1309 if (CheckDirCount(tif, dir, (uint32) samples)) {
1313 if (samples > NITEMS(buf))
1314 v = (uint16*) CheckMalloc(tif, samples, sizeof(uint16),
1315 "to fetch per-sample values");
1316 if (v && TIFFFetchShortArray(tif, dir, v)) {
1318 for (i = 1; i < samples; i++)
1320 TIFFError(tif->tif_name,
1321 "Cannot handle different per-sample values for field \"%s\"",
1322 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
1336 * Fetch samples/pixel long values for
1337 * the specified tag and verify that
1338 * all values are the same.
1341 TIFFFetchPerSampleLongs(TIFF* tif, TIFFDirEntry* dir, uint32* pl)
1343 uint16 samples = tif->tif_dir.td_samplesperpixel;
1346 if (CheckDirCount(tif, dir, (uint32) samples)) {
1350 if (samples > NITEMS(buf))
1351 v = (uint32*) CheckMalloc(tif, samples, sizeof(uint32),
1352 "to fetch per-sample values");
1353 if (v && TIFFFetchLongArray(tif, dir, v)) {
1355 for (i = 1; i < samples; i++)
1357 TIFFError(tif->tif_name,
1358 "Cannot handle different per-sample values for field \"%s\"",
1359 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
1373 * Fetch samples/pixel ANY values for
1374 * the specified tag and verify that
1375 * all values are the same.
1378 TIFFFetchPerSampleAnys(TIFF* tif, TIFFDirEntry* dir, double* pl)
1380 uint16 samples = tif->tif_dir.td_samplesperpixel;
1383 if (CheckDirCount(tif, dir, (uint32) samples)) {
1387 if (samples > NITEMS(buf))
1388 v = (double*) CheckMalloc(tif, samples, sizeof (double),
1389 "to fetch per-sample values");
1390 if (v && TIFFFetchAnyArray(tif, dir, v)) {
1392 for (i = 1; i < samples; i++)
1394 TIFFError(tif->tif_name,
1395 "Cannot handle different per-sample values for field \"%s\"",
1396 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
1411 * Fetch a set of offsets or lengths.
1412 * While this routine says "strips", in fact it's also used for tiles.
1415 TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, long nstrips, uint32** lpp)
1417 register uint32* lp;
1420 CheckDirCount(tif, dir, (uint32) nstrips);
1423 * Allocate space for strip information.
1426 (*lpp = (uint32 *)CheckMalloc(tif,
1427 nstrips, sizeof (uint32), "for strip array")) == NULL)
1430 _TIFFmemset( lp, 0, sizeof(uint32) * nstrips );
1432 if (dir->tdir_type == (int)TIFF_SHORT) {
1434 * Handle uint16->uint32 expansion.
1436 uint16* dp = (uint16*) CheckMalloc(tif,
1437 dir->tdir_count, sizeof (uint16), "to fetch strip tag");
1440 if( (status = TIFFFetchShortArray(tif, dir, dp)) != 0 ) {
1443 for( i = 0; i < nstrips && i < (int) dir->tdir_count; i++ )
1448 _TIFFfree((char*) dp);
1450 } else if( nstrips != (int) dir->tdir_count ) {
1451 /* Special case to correct length */
1453 uint32* dp = (uint32*) CheckMalloc(tif,
1454 dir->tdir_count, sizeof (uint32), "to fetch strip tag");
1458 status = TIFFFetchLongArray(tif, dir, dp);
1462 for( i = 0; i < nstrips && i < (int) dir->tdir_count; i++ )
1468 _TIFFfree( (char *) dp );
1470 status = TIFFFetchLongArray(tif, dir, lp);
1475 #define NITEMS(x) (sizeof (x) / sizeof (x[0]))
1477 * Fetch and set the ExtraSamples tag.
1480 TIFFFetchExtraSamples(TIFF* tif, TIFFDirEntry* dir)
1486 if (dir->tdir_count > NITEMS(buf)) {
1487 v = (uint16*) CheckMalloc(tif, dir->tdir_count, sizeof (uint16),
1488 "to fetch extra samples");
1492 if (dir->tdir_type == TIFF_BYTE)
1493 status = TIFFFetchByteArray(tif, dir, v);
1495 status = TIFFFetchShortArray(tif, dir, v);
1497 status = TIFFSetField(tif, dir->tdir_tag, dir->tdir_count, v);
1499 _TIFFfree((char*) v);
1505 * Fetch and set the RefBlackWhite tag.
1508 TIFFFetchRefBlackWhite(TIFF* tif, TIFFDirEntry* dir)
1510 static const char mesg[] = "for \"ReferenceBlackWhite\" array";
1514 if (dir->tdir_type == TIFF_RATIONAL)
1515 return (TIFFFetchNormalTag(tif, dir));
1517 * Handle LONG's for backward compatibility.
1519 cp = CheckMalloc(tif, dir->tdir_count, sizeof (uint32), mesg);
1520 if( (ok = (cp && TIFFFetchLongArray(tif, dir, (uint32*) cp))) != 0) {
1521 float* fp = (float*)
1522 CheckMalloc(tif, dir->tdir_count, sizeof (float), mesg);
1523 if( (ok = (fp != NULL)) != 0 ) {
1525 for (i = 0; i < dir->tdir_count; i++)
1526 fp[i] = (float)((uint32*) cp)[i];
1527 ok = TIFFSetField(tif, dir->tdir_tag, fp);
1528 _TIFFfree((char*) fp);
1537 * Replace a single strip (tile) of uncompressed data by
1538 * multiple strips (tiles), each approximately 8Kbytes.
1539 * This is useful for dealing with large images or
1540 * for dealing with machines with a limited amount
1544 ChopUpSingleUncompressedStrip(TIFF* tif)
1546 register TIFFDirectory *td = &tif->tif_dir;
1547 uint32 bytecount = td->td_stripbytecount[0];
1548 uint32 offset = td->td_stripoffset[0];
1549 tsize_t rowbytes = TIFFVTileSize(tif, 1), stripbytes;
1550 tstrip_t strip, nstrips, rowsperstrip;
1555 * Make the rows hold at least one
1556 * scanline, but fill 8k if possible.
1558 if (rowbytes > 8192) {
1559 stripbytes = rowbytes;
1561 } else if (rowbytes > 0 ) {
1562 rowsperstrip = 8192 / rowbytes;
1563 stripbytes = rowbytes * rowsperstrip;
1568 /* never increase the number of strips in an image */
1569 if (rowsperstrip >= td->td_rowsperstrip)
1571 nstrips = (tstrip_t) TIFFhowmany(bytecount, stripbytes);
1572 newcounts = (uint32*) CheckMalloc(tif, nstrips, sizeof (uint32),
1573 "for chopped \"StripByteCounts\" array");
1574 newoffsets = (uint32*) CheckMalloc(tif, nstrips, sizeof (uint32),
1575 "for chopped \"StripOffsets\" array");
1576 if (newcounts == NULL || newoffsets == NULL) {
1578 * Unable to allocate new strip information, give
1579 * up and use the original one strip information.
1581 if (newcounts != NULL)
1582 _TIFFfree(newcounts);
1583 if (newoffsets != NULL)
1584 _TIFFfree(newoffsets);
1588 * Fill the strip information arrays with
1589 * new bytecounts and offsets that reflect
1590 * the broken-up format.
1592 for (strip = 0; strip < nstrips; strip++) {
1593 if (stripbytes > (tsize_t) bytecount)
1594 stripbytes = bytecount;
1595 newcounts[strip] = stripbytes;
1596 newoffsets[strip] = offset;
1597 offset += stripbytes;
1598 bytecount -= stripbytes;
1601 * Replace old single strip info with multi-strip info.
1603 td->td_stripsperimage = td->td_nstrips = nstrips;
1604 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
1606 _TIFFfree(td->td_stripbytecount);
1607 _TIFFfree(td->td_stripoffset);
1608 td->td_stripbytecount = newcounts;
1609 td->td_stripoffset = newoffsets;
1610 td->td_stripbytecountsorted = 1;
1613 /* vim: set ts=8 sts=8 sw=8 noet: */