📄 tif_pdsdirread.c
字号:
/* $Header: /cvs/maptools/cvsroot/libtiff/contrib/pds/tif_pdsdirread.c,v 1.2 2003/11/17 15:09:39 dron Exp $ *//* * Copyright (c) 1988-1996 Sam Leffler * Copyright (c) 1991-1996 Silicon Graphics, Inc. * Copyright (c( 1996 USAF Phillips Laboratory * * Permission to use, copy, modify, distribute, and sell this software and * its documentation for any purpose is hereby granted without fee, provided * that (i) the above copyright notices and this permission notice appear in * all copies of the software and related documentation, and (ii) the names of * Sam Leffler and Silicon Graphics may not be used in any advertising or * publicity relating to the software without the specific, prior written * permission of Sam Leffler and Silicon Graphics. * * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. * * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THIS SOFTWARE. *//* * TIFF Library. * * These routines written by Conrad J. Poelman on a single late-night of * March 20-21, 1996. * * The entire purpose of this file is to provide a single external function, * TIFFReadPrivateDataSubDirectory(). This function is intended for use in reading a * private subdirectory from a TIFF file into a private structure. The * actual writing of data into the structure is handled by the setFieldFn(), * which is passed to TIFFReadPrivateDataSubDirectory() as a parameter. The idea is to * enable any application wishing to store private subdirectories to do so * easily using this function, without modifying the TIFF library. * * The astute observer will notice that only two functions are at all different * from the original tif_dirread.c file: TIFFReadPrivateDataSubDirectory() and * TIFFFetchNormalSubTag(). All the other stuff that makes this file so huge * is only necessary because all of those functions are declared static in * tif_dirread.c, so we have to totally duplicate them in order to use them. * * Oh, also note the bug fix in TIFFFetchFloat(). * */#include "tiffiop.h"#define IGNORE 0 /* tag placeholder used below */#if HAVE_IEEEFP#define TIFFCvtIEEEFloatToNative(tif, n, fp)#define TIFFCvtIEEEDoubleToNative(tif, n, dp)#elseextern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*);extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*);#endifstatic void EstimateStripByteCounts(TIFF*, TIFFDirEntry*, uint16);static void MissingRequired(TIFF*, const char*);static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);static tsize_t TIFFFetchData(TIFF*, TIFFDirEntry*, char*);static tsize_t TIFFFetchString(TIFF*, TIFFDirEntry*, char*);static float TIFFFetchRational(TIFF*, TIFFDirEntry*);static int TIFFFetchNormalSubTag(TIFF*, TIFFDirEntry*, const TIFFFieldInfo*, int (*getFieldFn)(TIFF *tif,ttag_t tag,...));static int TIFFFetchPerSampleShorts(TIFF*, TIFFDirEntry*, int*);static int TIFFFetchPerSampleAnys(TIFF*, TIFFDirEntry*, double*);static int TIFFFetchShortArray(TIFF*, TIFFDirEntry*, uint16*);static int TIFFFetchStripThing(TIFF*, TIFFDirEntry*, long, uint32**);static int TIFFFetchExtraSamples(TIFF*, TIFFDirEntry*);static int TIFFFetchRefBlackWhite(TIFF*, TIFFDirEntry*);static float TIFFFetchFloat(TIFF*, TIFFDirEntry*);static int TIFFFetchFloatArray(TIFF*, TIFFDirEntry*, float*);static int TIFFFetchDoubleArray(TIFF*, TIFFDirEntry*, double*);static int TIFFFetchAnyArray(TIFF*, TIFFDirEntry*, double*);static int TIFFFetchShortPair(TIFF*, TIFFDirEntry*);#if STRIPCHOP_SUPPORTstatic void ChopUpSingleUncompressedStrip(TIFF*);#endifstatic char *CheckMalloc(TIFF* tif, tsize_t n, const char* what){ char *cp = (char*)_TIFFmalloc(n); if (cp == NULL) TIFFError(tif->tif_name, "No space %s", what); return (cp);}/* Just as was done with TIFFWritePrivateDataSubDirectory(), here we implement TIFFReadPrivateDataSubDirectory() which takes an offset into the TIFF file, a TIFFFieldInfo structure specifying the types of the various tags, and a function to use to set individual tags when they are encountered. The data is read from the file, translated using the TIFF library's built-in machine-independent conversion functions, and filled into private subdirectory structure. This code was written by copying the original TIFFReadDirectory() function from tif_dirread.c and paring it down to what is needed for this. It is the caller's responsibility to allocate and initialize the internal structure that setFieldFn() will be writing into. If this function is being called more than once before closing the file, the caller also must be careful to free data in the structure before re-initializing. It is also the caller's responsibility to verify the presence of any required fields after reading the directory in.*/intTIFFReadPrivateDataSubDirectory(TIFF* tif, toff_t pdir_offset, TIFFFieldInfo *field_info, int (*setFieldFn)(TIFF *tif, ttag_t tag, ...)){ register TIFFDirEntry* dp; register int n; register TIFFDirectory* td; TIFFDirEntry* dir; int iv; long v; double dv; const TIFFFieldInfo* fip; int fix; uint16 dircount; uint32 nextdiroff; char* cp; int diroutoforderwarning = 0; /* Skipped part about checking for directories or compression data. */ if (!isMapped(tif)) { if (!SeekOK(tif, pdir_offset)) { TIFFError(tif->tif_name, "Seek error accessing TIFF private subdirectory"); return (0); } if (!ReadOK(tif, &dircount, sizeof (uint16))) { TIFFError(tif->tif_name, "Can not read TIFF private subdirectory count"); return (0); } if (tif->tif_flags & TIFF_SWAB) TIFFSwabShort(&dircount); dir = (TIFFDirEntry *)CheckMalloc(tif, dircount * sizeof (TIFFDirEntry), "to read TIFF private subdirectory"); if (dir == NULL) return (0); if (!ReadOK(tif, dir, dircount*sizeof (TIFFDirEntry))) { TIFFError(tif->tif_name, "Can not read TIFF private subdirectory"); goto bad; } /* * Read offset to next directory for sequential scans. */ (void) ReadOK(tif, &nextdiroff, sizeof (uint32)); } else { toff_t off = pdir_offset; if (off + sizeof (short) > tif->tif_size) { TIFFError(tif->tif_name, "Can not read TIFF private subdirectory count"); return (0); } else _TIFFmemcpy(&dircount, tif->tif_base + off, sizeof (uint16)); off += sizeof (uint16); if (tif->tif_flags & TIFF_SWAB) TIFFSwabShort(&dircount); dir = (TIFFDirEntry *)CheckMalloc(tif, dircount * sizeof (TIFFDirEntry), "to read TIFF private subdirectory"); if (dir == NULL) return (0); if (off + dircount*sizeof (TIFFDirEntry) > tif->tif_size) { TIFFError(tif->tif_name, "Can not read TIFF private subdirectory"); goto bad; } else _TIFFmemcpy(dir, tif->tif_base + off, dircount*sizeof (TIFFDirEntry)); off += dircount* sizeof (TIFFDirEntry); if (off + sizeof (uint32) < tif->tif_size) _TIFFmemcpy(&nextdiroff, tif->tif_base+off, sizeof (uint32)); } if (tif->tif_flags & TIFF_SWAB) TIFFSwabLong(&nextdiroff); /* * Setup default value and then make a pass over * the fields to check type and tag information, * and to extract info required to size data * structures. A second pass is made afterwards * to read in everthing not taken in the first pass. */ td = &tif->tif_dir; for (fip = field_info, dp = dir, n = dircount; n > 0; n--, dp++) { if (tif->tif_flags & TIFF_SWAB) { TIFFSwabArrayOfShort(&dp->tdir_tag, 2); TIFFSwabArrayOfLong(&dp->tdir_count, 2); } /* * Find the field information entry for this tag. */ /* * Silicon Beach (at least) writes unordered * directory tags (violating the spec). Handle * it here, but be obnoxious (maybe they'll fix it?). */ if (dp->tdir_tag < fip->field_tag) { if (!diroutoforderwarning) { TIFFWarning(tif->tif_name, "invalid TIFF private subdirectory; tags are not sorted in ascending order"); diroutoforderwarning = 1; } fip = field_info; /* O(n^2) */ } while (fip->field_tag && fip->field_tag < dp->tdir_tag) fip++; if (!fip->field_tag || fip->field_tag != dp->tdir_tag) { TIFFWarning(tif->tif_name, "unknown field with tag %d (0x%x) in private subdirectory ignored", dp->tdir_tag, dp->tdir_tag); dp->tdir_tag = IGNORE; fip = field_info;/* restart search */ continue; } /* * Null out old tags that we ignore. */ /* Not implemented yet, since FIELD_IGNORE is specific to the main directories. Could pass this in too... */ if (0 /* && fip->field_bit == FIELD_IGNORE */) { ignore: dp->tdir_tag = IGNORE; continue; } /* * Check data type. */ while (dp->tdir_type != (u_short)fip->field_type) { if (fip->field_type == TIFF_ANY) /* wildcard */ break; fip++; if (!fip->field_tag || fip->field_tag != dp->tdir_tag) { TIFFWarning(tif->tif_name, "wrong data type %d for \"%s\"; tag ignored", dp->tdir_type, fip[-1].field_name); goto ignore; } } /* * Check count if known in advance. */ if (fip->field_readcount != TIFF_VARIABLE) { uint32 expected = (fip->field_readcount == TIFF_SPP) ? (uint32) td->td_samplesperpixel : (uint32) fip->field_readcount; if (!CheckDirCount(tif, dp, expected)) goto ignore; } /* Now read in and process data from field. */ if (!TIFFFetchNormalSubTag(tif, dp, fip, setFieldFn)) goto bad; } if (dir) _TIFFfree(dir); return (1);bad: if (dir) _TIFFfree(dir); return (0);}static voidEstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount){ register TIFFDirEntry *dp; register TIFFDirectory *td = &tif->tif_dir; uint16 i; if (td->td_stripbytecount) _TIFFfree(td->td_stripbytecount); td->td_stripbytecount = (uint32*) CheckMalloc(tif, td->td_nstrips * sizeof (uint32), "for \"StripByteCounts\" array"); if (td->td_compression != COMPRESSION_NONE) { uint32 space = (uint32)(sizeof (TIFFHeader) + sizeof (uint16) + (dircount * sizeof (TIFFDirEntry)) + sizeof (uint32)); toff_t filesize = TIFFGetFileSize(tif); uint16 n; /* calculate amount of space used by indirect values */ for (dp = dir, n = dircount; n > 0; n--, dp++) { uint32 cc = dp->tdir_count*TIFFDataWidth(dp->tdir_type); if (cc > sizeof (uint32)) space += cc; } space = (filesize - space) / td->td_samplesperpixel; for (i = 0; i < td->td_nstrips; i++) td->td_stripbytecount[i] = space; /* * This gross hack handles the case were the offset to * the last strip is past the place where we think the strip * should begin. Since a strip of data must be contiguous, * it's safe to assume that we've overestimated the amount * of data in the strip and trim this number back accordingly. */ i--; if (td->td_stripoffset[i] + td->td_stripbytecount[i] > filesize) td->td_stripbytecount[i] = filesize - td->td_stripoffset[i]; } else { uint32 rowbytes = TIFFScanlineSize(tif); uint32 rowsperstrip = td->td_imagelength / td->td_nstrips; for (i = 0; i < td->td_nstrips; i++) td->td_stripbytecount[i] = rowbytes*rowsperstrip; } TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS); if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP)) td->td_rowsperstrip = td->td_imagelength;}static voidMissingRequired(TIFF* tif, const char* tagname){ TIFFError(tif->tif_name, "TIFF directory is missing required \"%s\" field", tagname);}/* * Check the count field of a directory * entry against a known value. The caller * is expected to skip/ignore the tag if * there is a mismatch. */static intCheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count){ if (count != dir->tdir_count) { TIFFWarning(tif->tif_name, "incorrect count for field \"%s\" (%lu, expecting %lu); tag ignored", _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name, dir->tdir_count, count); return (0); } return (1);}/* * Fetch a contiguous directory item. */static tsize_tTIFFFetchData(TIFF* tif, TIFFDirEntry* dir, char* cp){ int w = TIFFDataWidth(dir->tdir_type); tsize_t cc = dir->tdir_count * w; if (!isMapped(tif)) { if (!SeekOK(tif, dir->tdir_offset)) goto bad;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -