static int JBIGVSetField(TIFF* tif, ttag_t tag, va_list ap) { JBIGState* codec = GetJBIGState(tif); switch (tag) { case TIFFTAG_FAXRECVPARAMS: codec->recvparams = va_arg(ap, uint32); break; case TIFFTAG_FAXSUBADDRESS: _TIFFsetString(&codec->subaddress, va_arg(ap, char*)); break; case TIFFTAG_FAXRECVTIME: codec->recvtime = va_arg(ap, uint32); break; case TIFFTAG_FAXDCS: _TIFFsetString(&codec->faxdcs, va_arg(ap, char*)); break; default: return (*codec->vsetparent)(tif, tag, ap); } TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit); tif->tif_flags |= TIFF_DIRTYDIRECT; return 1; }
/* * Setup a directory entry of an array of RATIONAL * or SRATIONAL and write the associated indirect values. */ static int TIFFWriteRationalArray(TIFF* tif, TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, float* v) { uint32 i; uint32* t; int status; dir->tdir_tag = tag; dir->tdir_type = (short) type; dir->tdir_count = n; t = (uint32*) _TIFFmalloc(2*n * sizeof (uint32)); #if defined(__INTEL_COMPILER) && 1 /* VDM auto patch */ # pragma ivdep # pragma swp # pragma unroll # pragma prefetch # if 0 # pragma simd noassert # endif #endif /* VDM auto patch */ for (i = 0; i < n; i++) { float fv = v[i]; int sign = 1; uint32 den; if (fv < 0) { if (type == TIFF_RATIONAL) { TIFFWarning(tif->tif_name, "\"%s\": Information lost writing value (%g) as (unsigned) RATIONAL", _TIFFFieldWithTag(tif,tag)->field_name, v); fv = 0; } else fv = -fv, sign = -1; } den = 1L; if (fv > 0) { #if defined(__INTEL_COMPILER) && 1 /* VDM auto patch */ # pragma ivdep # pragma swp # pragma unroll # pragma prefetch # if 0 # pragma simd noassert # endif #endif /* VDM auto patch */ while (fv < 1L<<(31-3) && den < 1L<<(31-3)) fv *= 1<<3, den *= 1L<<3; } t[2*i+0] = sign * (fv + 0.5); t[2*i+1] = den; } status = TIFFWriteData(tif, dir, (char *)t); _TIFFfree((char*) t); return (status); }
/* * 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 int CheckDirCount(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); }
/* * Convert numerator+denominator to float. */ static int cvtRational(TIFF* tif, TIFFDirEntry* dir, uint32 num, uint32 denom, float* rv) { if (denom == 0) { TIFFError(tif->tif_name, "%s: Rational with zero denominator (num = %lu)", _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name, num); return (0); } else { if (dir->tdir_type == TIFF_RATIONAL) *rv = ((float)num / (float)denom); else *rv = ((float)(int32)num / (float)(int32)denom); return (1); } }
/* * Setup a directory entry of an array of RATIONAL * or SRATIONAL and write the associated indirect values. */ static int TIFFWriteRationalArray(TIFF* tif, TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, float* v) { uint32 i; uint32* t; int status; dir->tdir_tag = (uint16) tag; dir->tdir_type = (short) type; dir->tdir_count = n; t = (uint32*) _TIFFmalloc(2*n * sizeof (uint32)); if (t == NULL) { TIFFError(tif->tif_name, "No space to write RATIONAL array"); return (0); } for (i = 0; i < n; i++) { float fv = v[i]; int sign = 1; uint32 den; if (fv < 0) { if (type == TIFF_RATIONAL) { TIFFWarning(tif->tif_name, "\"%s\": Information lost writing value (%g) as (unsigned) RATIONAL", _TIFFFieldWithTag(tif,tag)->field_name, fv); fv = 0; } else fv = -fv, sign = -1; } den = 1L; if (fv > 0) { while (fv < 1L<<(31-3) && den < 1L<<(31-3)) fv *= 1<<3, den *= 1L<<3; } t[2*i+0] = (uint32) (sign * (fv + 0.5)); t[2*i+1] = den; } status = TIFFWriteData(tif, dir, (char *)t); _TIFFfree((char*) t); return (status); }
/* * Write a contiguous directory item. */ static int TIFFWriteData(TIFF* tif, TIFFDirEntry* dir, char* cp) { tsize_t cc; if (tif->tif_flags & TIFF_SWAB) { switch (dir->s.tdir_type) { case TIFF_SHORT: case TIFF_SSHORT: TIFFSwabArrayOfShort((uint16*) cp, TDIRGetEntryCount(tif,dir)); break; case TIFF_LONG: case TIFF_SLONG: case TIFF_IFD: case TIFF_FLOAT: TIFFSwabArrayOfLong((uint32*) cp, TDIRGetEntryCount(tif,dir)); break; case TIFF_LONG8: case TIFF_SLONG8: case TIFF_IFD8: TIFFSwabArrayOfLong8((uint64*) cp, TDIRGetEntryCount(tif,dir)); break; case TIFF_RATIONAL: case TIFF_SRATIONAL: TIFFSwabArrayOfLong((uint32*) cp, 2 * TDIRGetEntryCount(tif,dir)); break; case TIFF_DOUBLE: TIFFSwabArrayOfDouble((double*) cp, TDIRGetEntryCount(tif,dir)); break; } } TDIRSetEntryOff(tif,dir,tif->tif_dataoff); cc = TDIRGetEntryCount(tif,dir) * TIFFDataWidth((TIFFDataType) dir->s.tdir_type); if (SeekOK(tif, tif->tif_dataoff) && WriteOK(tif, cp, cc)) { tif->tif_dataoff += ((cc + 1) & ~1); return (1); } TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error writing data for field \"%s\" (%d)", _TIFFFieldWithTag(tif, dir->s.tdir_tag)->field_name, TIFFGetErrno(tif)); return (0); }
/* * Fetch a contiguous directory item. */ static tsize_t TIFFFetchData(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; if (!ReadOK(tif, cp, cc)) goto bad; } else { if (dir->tdir_offset + cc > tif->tif_size) goto bad; _TIFFmemcpy(cp, tif->tif_base + dir->tdir_offset, cc); } if (tif->tif_flags & TIFF_SWAB) { switch (dir->tdir_type) { case TIFF_SHORT: case TIFF_SSHORT: TIFFSwabArrayOfShort((uint16*) cp, dir->tdir_count); break; case TIFF_LONG: case TIFF_SLONG: case TIFF_FLOAT: TIFFSwabArrayOfLong((uint32*) cp, dir->tdir_count); break; case TIFF_RATIONAL: case TIFF_SRATIONAL: TIFFSwabArrayOfLong((uint32*) cp, 2*dir->tdir_count); break; case TIFF_DOUBLE: TIFFSwabArrayOfDouble((double*) cp, dir->tdir_count); break; } } return (cc); bad: TIFFError(tif->tif_name, "Error fetching data for field \"%s\"", _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name); return ((tsize_t) 0); }
/* * Write a contiguous directory item. */ static int TIFFWriteData(TIFF* tif, TIFFDirEntry* dir, char* cp) { tsize_t cc; if (tif->tif_flags & TIFF_SWAB) { switch (dir->tdir_type) { case TIFF_SHORT: case TIFF_SSHORT: TIFFSwabArrayOfShort((uint16*) cp, dir->tdir_count); break; case TIFF_LONG: case TIFF_SLONG: case TIFF_FLOAT: TIFFSwabArrayOfLong((uint32*) cp, dir->tdir_count); break; case TIFF_RATIONAL: case TIFF_SRATIONAL: TIFFSwabArrayOfLong((uint32*) cp, 2*dir->tdir_count); break; case TIFF_DOUBLE: TIFFSwabArrayOfDouble((double*) cp, dir->tdir_count); break; } } dir->tdir_offset = tif->tif_dataoff; cc = dir->tdir_count * TIFFDataWidth((TIFFDataType) dir->tdir_type); if (SeekOK(tif, dir->tdir_offset) && WriteOK(tif, cp, cc)) { tif->tif_dataoff += (cc + 1) & ~1; return (1); } TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error writing data for field \"%s\"", _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name); return (0); }
static int _XTIFFVSetField(TIFF* tif, ttag_t tag, va_list ap) { xtiff *xt = XTIFFDIR(tif); XTIFFDirectory* xd = &xt->xtif_dir; int status = 1; uint32 v32=0; int i=0, v=0; va_list ap1 = ap; /* va_start is called by the calling routine */ switch (tag) { /* * XXX put your extended tags here; replace the implemented * example tags with your own. */ case TIFFTAG_EXAMPLE_MULTI: /* multi-valued tags need to store the count as well */ xd->xd_num_multi = (uint16) va_arg(ap, int); _TIFFsetDoubleArray(&xd->xd_example_multi, va_arg(ap, double*), (long) xd->xd_num_multi); break; case TIFFTAG_EXAMPLE_SINGLE: xd->xd_example_single = va_arg(ap, uint32); break; case TIFFTAG_EXAMPLE_ASCII: _TIFFsetString(&xd->xd_example_ascii, va_arg(ap, char*)); break; default: /* call the inherited method */ return (PARENT(xt,vsetfield))(tif,tag,ap); break; } if (status) { /* we have to override the print method here, * after the compression tags have gotten to it. * This makes sense because the only time we would * need the extended print method is if an extended * tag is set by the reader. */ if (!(xt->xtif_flags & XTIFFP_PRINT)) { PARENT(xt,printdir) = TIFFMEMBER(tif,printdir); TIFFMEMBER(tif,printdir) = _XTIFFPrintDirectory; xt->xtif_flags |= XTIFFP_PRINT; } TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit); tif->tif_flags |= TIFF_DIRTYDIRECT; } va_end(ap); return (status); badvalue: TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "%d: Bad value for \"%s\"", v, _TIFFFieldWithTag(tif, tag)->field_name); va_end(ap); return (0); badvalue32: TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "%ld: Bad value for \"%s\"", v32, _TIFFFieldWithTag(tif, tag)->field_name); va_end(ap); return (0); }
static int _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap) { static const char module[] = "_TIFFVSetField"; TIFFDirectory* td = &tif->tif_dir; int status = 1; uint32 v32; int i, v; double d; char* s; switch (tag) { case TIFFTAG_SUBFILETYPE: td->td_subfiletype = va_arg(ap, uint32); break; case TIFFTAG_IMAGEWIDTH: td->td_imagewidth = va_arg(ap, uint32); break; case TIFFTAG_IMAGELENGTH: td->td_imagelength = va_arg(ap, uint32); break; case TIFFTAG_BITSPERSAMPLE: td->td_bitspersample = (uint16) va_arg(ap, int); /* * If the data require post-decoding processing * to byte-swap samples, set it up here. Note * that since tags are required to be ordered, * compression code can override this behaviour * in the setup method if it wants to roll the * post decoding work in with its normal work. */ if (tif->tif_flags & TIFF_SWAB) { if (td->td_bitspersample == 16) tif->tif_postdecode = _TIFFSwab16BitData; else if (td->td_bitspersample == 32) tif->tif_postdecode = _TIFFSwab32BitData; else if (td->td_bitspersample == 64) tif->tif_postdecode = _TIFFSwab64BitData; } break; case TIFFTAG_COMPRESSION: v = va_arg(ap, int) & 0xffff; /* * If we're changing the compression scheme, * the notify the previous module so that it * can cleanup any state it's setup. */ if (TIFFFieldSet(tif, FIELD_COMPRESSION)) { if (td->td_compression == v) break; (*tif->tif_cleanup)(tif); tif->tif_flags &= ~TIFF_CODERSETUP; } /* * Setup new compression routine state. */ if( (status = TIFFSetCompressionScheme(tif, v)) != 0 ) td->td_compression = (uint16) v; else status = 0; break; case TIFFTAG_PHOTOMETRIC: td->td_photometric = (uint16) va_arg(ap, int); break; case TIFFTAG_THRESHHOLDING: td->td_threshholding = (uint16) va_arg(ap, int); break; case TIFFTAG_FILLORDER: v = va_arg(ap, int); if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB) goto badvalue; td->td_fillorder = (uint16) v; break; case TIFFTAG_DOCUMENTNAME: _TIFFsetString(&td->td_documentname, va_arg(ap, char*)); break; case TIFFTAG_ARTIST: _TIFFsetString(&td->td_artist, va_arg(ap, char*)); break; case TIFFTAG_DATETIME: _TIFFsetString(&td->td_datetime, va_arg(ap, char*)); break; case TIFFTAG_HOSTCOMPUTER: _TIFFsetString(&td->td_hostcomputer, va_arg(ap, char*)); break; case TIFFTAG_IMAGEDESCRIPTION: _TIFFsetString(&td->td_imagedescription, va_arg(ap, char*)); break; case TIFFTAG_MAKE: _TIFFsetString(&td->td_make, va_arg(ap, char*)); break; case TIFFTAG_MODEL: _TIFFsetString(&td->td_model, va_arg(ap, char*)); break; case TIFFTAG_COPYRIGHT: _TIFFsetString(&td->td_copyright, va_arg(ap, char*)); break; case TIFFTAG_ORIENTATION: v = va_arg(ap, int); if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v) { TIFFWarning(tif->tif_name, "Bad value %ld for \"%s\" tag ignored", v, _TIFFFieldWithTag(tif, tag)->field_name); } else td->td_orientation = (uint16) v; break; case TIFFTAG_SAMPLESPERPIXEL: /* XXX should cross check -- e.g. if pallette, then 1 */ v = va_arg(ap, int); if (v == 0) goto badvalue; td->td_samplesperpixel = (uint16) v; break; case TIFFTAG_ROWSPERSTRIP: v32 = va_arg(ap, uint32); if (v32 == 0) goto badvalue32; td->td_rowsperstrip = v32; if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) { td->td_tilelength = v32; td->td_tilewidth = td->td_imagewidth; } break; case TIFFTAG_MINSAMPLEVALUE: td->td_minsamplevalue = (uint16) va_arg(ap, int); break; case TIFFTAG_MAXSAMPLEVALUE: td->td_maxsamplevalue = (uint16) va_arg(ap, int); break; case TIFFTAG_SMINSAMPLEVALUE: td->td_sminsamplevalue = (double) va_arg(ap, dblparam_t); break; case TIFFTAG_SMAXSAMPLEVALUE: td->td_smaxsamplevalue = (double) va_arg(ap, dblparam_t); break; case TIFFTAG_XRESOLUTION: td->td_xresolution = (float) va_arg(ap, dblparam_t); break; case TIFFTAG_YRESOLUTION: td->td_yresolution = (float) va_arg(ap, dblparam_t); break; case TIFFTAG_PLANARCONFIG: v = va_arg(ap, int); if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE) goto badvalue; td->td_planarconfig = (uint16) v; break; case TIFFTAG_PAGENAME: _TIFFsetString(&td->td_pagename, va_arg(ap, char*)); break; case TIFFTAG_XPOSITION: td->td_xposition = (float) va_arg(ap, dblparam_t); break; case TIFFTAG_YPOSITION: td->td_yposition = (float) va_arg(ap, dblparam_t); break; case TIFFTAG_RESOLUTIONUNIT: v = va_arg(ap, int); if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v) goto badvalue; td->td_resolutionunit = (uint16) v; break; case TIFFTAG_PAGENUMBER: td->td_pagenumber[0] = (uint16) va_arg(ap, int); td->td_pagenumber[1] = (uint16) va_arg(ap, int); break; case TIFFTAG_HALFTONEHINTS: td->td_halftonehints[0] = (uint16) va_arg(ap, int); td->td_halftonehints[1] = (uint16) va_arg(ap, int); break; case TIFFTAG_COLORMAP: v32 = (uint32)(1L<<td->td_bitspersample); _TIFFsetShortArray(&td->td_colormap[0], va_arg(ap, uint16*), v32); _TIFFsetShortArray(&td->td_colormap[1], va_arg(ap, uint16*), v32); _TIFFsetShortArray(&td->td_colormap[2], va_arg(ap, uint16*), v32); break; case TIFFTAG_EXTRASAMPLES: if (!setExtraSamples(td, ap, &v)) goto badvalue; break; case TIFFTAG_MATTEING: td->td_extrasamples = (uint16) (va_arg(ap, int) != 0); if (td->td_extrasamples) { uint16 sv = EXTRASAMPLE_ASSOCALPHA; _TIFFsetShortArray(&td->td_sampleinfo, &sv, 1); } break; case TIFFTAG_TILEWIDTH: v32 = va_arg(ap, uint32); if (v32 % 16) { if (tif->tif_mode != O_RDONLY) goto badvalue32; TIFFWarning(tif->tif_name, "Nonstandard tile width %d, convert file", v32); } td->td_tilewidth = v32; tif->tif_flags |= TIFF_ISTILED; break; case TIFFTAG_TILELENGTH: v32 = va_arg(ap, uint32); if (v32 % 16) { if (tif->tif_mode != O_RDONLY) goto badvalue32; TIFFWarning(tif->tif_name, "Nonstandard tile length %d, convert file", v32); } td->td_tilelength = v32; tif->tif_flags |= TIFF_ISTILED; break; case TIFFTAG_TILEDEPTH: v32 = va_arg(ap, uint32); if (v32 == 0) goto badvalue32; td->td_tiledepth = v32; break; case TIFFTAG_DATATYPE: v = va_arg(ap, int); switch (v) { case DATATYPE_VOID: v = SAMPLEFORMAT_VOID; break; case DATATYPE_INT: v = SAMPLEFORMAT_INT; break; case DATATYPE_UINT: v = SAMPLEFORMAT_UINT; break; case DATATYPE_IEEEFP: v = SAMPLEFORMAT_IEEEFP;break; default: goto badvalue; } td->td_sampleformat = (uint16) v; break; case TIFFTAG_SAMPLEFORMAT: v = va_arg(ap, int); if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v) goto badvalue; td->td_sampleformat = (uint16) v; /* Try to fix up the SWAB function for complex data. */ if( td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT && td->td_bitspersample == 32 && tif->tif_postdecode == _TIFFSwab32BitData ) tif->tif_postdecode = _TIFFSwab16BitData; else if( (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT || td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP) && td->td_bitspersample == 64 && tif->tif_postdecode == _TIFFSwab64BitData ) tif->tif_postdecode = _TIFFSwab32BitData; else if( td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP && td->td_bitspersample == 128 && tif->tif_postdecode == NULL ) tif->tif_postdecode = _TIFFSwab64BitData; break; case TIFFTAG_IMAGEDEPTH: td->td_imagedepth = va_arg(ap, uint32); break; case TIFFTAG_STONITS: d = va_arg(ap, dblparam_t); if (d <= 0.) goto badvaluedbl; td->td_stonits = d; break; /* Begin Pixar Tags */ case TIFFTAG_PIXAR_IMAGEFULLWIDTH: td->td_imagefullwidth = va_arg(ap, uint32); break; case TIFFTAG_PIXAR_IMAGEFULLLENGTH: td->td_imagefulllength = va_arg(ap, uint32); break; case TIFFTAG_PIXAR_TEXTUREFORMAT: _TIFFsetString(&td->td_textureformat, va_arg(ap, char*)); break; case TIFFTAG_PIXAR_WRAPMODES: _TIFFsetString(&td->td_wrapmodes, va_arg(ap, char*)); break; case TIFFTAG_PIXAR_FOVCOT: td->td_fovcot = (float) va_arg(ap, dblparam_t); break; case TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN: _TIFFsetFloatArray(&td->td_matrixWorldToScreen, va_arg(ap, float*), 16); break; case TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA: _TIFFsetFloatArray(&td->td_matrixWorldToCamera, va_arg(ap, float*), 16); break; /* End Pixar Tags */ case TIFFTAG_SUBIFD: if ((tif->tif_flags & TIFF_INSUBIFD) == 0) { td->td_nsubifd = (uint16) va_arg(ap, int); _TIFFsetLongArray(&td->td_subifd, va_arg(ap, uint32*), (long) td->td_nsubifd); } else {
/* * Read the next TIFF directory from a file * and convert it to the internal format. * We read directories sequentially. */ int TIFFReadDirectory(TIFF* tif) { register TIFFDirEntry* dp; register int n; register TIFFDirectory* td; TIFFDirEntry* dir; int iv; long v; double dv; const TIFFFieldInfo* fip; int fix; uint16 dircount; toff_t nextdiroff; char* cp; int diroutoforderwarning = 0; tif->tif_diroff = tif->tif_nextdiroff; if (tif->tif_diroff == 0) /* no more directories */ return (0); /* * Cleanup any previous compression state. */ (*tif->tif_cleanup)(tif); tif->tif_curdir++; nextdiroff = 0; if (!isMapped(tif)) { if (!SeekOK(tif, tif->tif_diroff)) { TIFFError(tif->tif_name, "Seek error accessing TIFF directory"); return (0); } if (!ReadOK(tif, &dircount, sizeof (uint16))) { TIFFError(tif->tif_name, "Can not read TIFF directory count"); return (0); } if (tif->tif_flags & TIFF_SWAB) TIFFSwabShort(&dircount); dir = (TIFFDirEntry *)CheckMalloc(tif, dircount * sizeof (TIFFDirEntry), "to read TIFF directory"); if (dir == NULL) return (0); if (!ReadOK(tif, dir, dircount*sizeof (TIFFDirEntry))) { TIFFError(tif->tif_name, "Can not read TIFF directory"); goto bad; } /* * Read offset to next directory for sequential scans. */ (void) ReadOK(tif, &nextdiroff, sizeof (uint32)); } else { toff_t off = tif->tif_diroff; if (off + sizeof (uint16) > tif->tif_size) { TIFFError(tif->tif_name, "Can not read TIFF directory 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 directory"); if (dir == NULL) return (0); if (off + dircount*sizeof (TIFFDirEntry) > tif->tif_size) { TIFFError(tif->tif_name, "Can not read TIFF directory"); 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); tif->tif_nextdiroff = nextdiroff; tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */ /* * 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; /* free any old stuff and reinit */ TIFFFreeDirectory(tif); TIFFDefaultDirectory(tif); /* * Electronic Arts writes gray-scale TIFF files * without a PlanarConfiguration directory entry. * Thus we setup a default value here, even though * the TIFF spec says there is no default value. */ TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); /* * Sigh, we must make a separate pass through the * directory for the following reason: * * We must process the Compression tag in the first pass * in order to merge in codec-private tag definitions (otherwise * we may get complaints about unknown tags). However, the * Compression tag may be dependent on the SamplesPerPixel * tag value because older TIFF specs permited Compression * to be written as a SamplesPerPixel-count tag entry. * Thus if we don't first figure out the correct SamplesPerPixel * tag value then we may end up ignoring the Compression tag * value because it has an incorrect count value (if the * true value of SamplesPerPixel is not 1). * * It sure would have been nice if Aldus had really thought * this stuff through carefully. */ for (dp = dir, n = dircount; n > 0; n--, dp++) { if (tif->tif_flags & TIFF_SWAB) { TIFFSwabArrayOfShort(&dp->tdir_tag, 2); TIFFSwabArrayOfLong(&dp->tdir_count, 2); } if (dp->tdir_tag == TIFFTAG_SAMPLESPERPIXEL) { if (!TIFFFetchNormalTag(tif, dp)) goto bad; dp->tdir_tag = IGNORE; } } /* * First real pass over the directory. */ fix = 0; for (dp = dir, n = dircount; n > 0; n--, dp++) { /* * Find the field information entry for this tag. * Added check for tags to ignore ... [BFC] */ if( TIFFReassignTagToIgnore(TIS_EXTRACT, dp->tdir_tag) ) dp->tdir_tag = IGNORE; if (dp->tdir_tag == IGNORE) continue; /* * 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 < tif->tif_fieldinfo[fix]->field_tag) { if (!diroutoforderwarning) { TIFFWarning(tif->tif_name, "invalid TIFF directory; tags are not sorted in ascending order"); diroutoforderwarning = 1; } fix = 0; /* O(n^2) */ } while (fix < tif->tif_nfields && tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag) fix++; if (fix == tif->tif_nfields || tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) { TIFFWarning(tif->tif_name, "unknown field with tag %d (0x%x) ignored", dp->tdir_tag, dp->tdir_tag); dp->tdir_tag = IGNORE; fix = 0; /* restart search */ continue; } /* * Null out old tags that we ignore. */ if (tif->tif_fieldinfo[fix]->field_bit == FIELD_IGNORE) { ignore: dp->tdir_tag = IGNORE; continue; } /* * Check data type. */ fip = tif->tif_fieldinfo[fix]; while (dp->tdir_type != (u_short) fip->field_type) { if (fip->field_type == TIFF_ANY) /* wildcard */ break; fip++, fix++; if (fix == tif->tif_nfields || 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; } switch (dp->tdir_tag) { case TIFFTAG_COMPRESSION: /* * The 5.0 spec says the Compression tag has * one value, while earlier specs say it has * one value per sample. Because of this, we * accept the tag if one value is supplied. */ if (dp->tdir_count == 1) { v = TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset); if (!TIFFSetField(tif, dp->tdir_tag, (int)v)) goto bad; break; } if (!TIFFFetchPerSampleShorts(tif, dp, &iv) || !TIFFSetField(tif, dp->tdir_tag, iv)) goto bad; dp->tdir_tag = IGNORE; break; case TIFFTAG_STRIPOFFSETS: case TIFFTAG_STRIPBYTECOUNTS: case TIFFTAG_TILEOFFSETS: case TIFFTAG_TILEBYTECOUNTS: TIFFSetFieldBit(tif, fip->field_bit); break; case TIFFTAG_IMAGEWIDTH: case TIFFTAG_IMAGELENGTH: case TIFFTAG_IMAGEDEPTH: case TIFFTAG_TILELENGTH: case TIFFTAG_TILEWIDTH: case TIFFTAG_TILEDEPTH: case TIFFTAG_PLANARCONFIG: case TIFFTAG_ROWSPERSTRIP: if (!TIFFFetchNormalTag(tif, dp)) goto bad; dp->tdir_tag = IGNORE; break; case TIFFTAG_EXTRASAMPLES: (void) TIFFFetchExtraSamples(tif, dp); dp->tdir_tag = IGNORE; break; } } /* * Allocate directory structure and setup defaults. */ if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) { MissingRequired(tif, "ImageLength"); goto bad; } if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) { MissingRequired(tif, "PlanarConfiguration"); goto bad; } /* * Setup appropriate structures (by strip or by tile) */ if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) { td->td_nstrips = TIFFNumberOfStrips(tif); td->td_tilewidth = td->td_imagewidth; td->td_tilelength = td->td_rowsperstrip; td->td_tiledepth = td->td_imagedepth; tif->tif_flags &= ~TIFF_ISTILED; } else { td->td_nstrips = TIFFNumberOfTiles(tif); tif->tif_flags |= TIFF_ISTILED; } td->td_stripsperimage = td->td_nstrips; if (td->td_planarconfig == PLANARCONFIG_SEPARATE) td->td_stripsperimage /= td->td_samplesperpixel; if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) { MissingRequired(tif, isTiled(tif) ? "TileOffsets" : "StripOffsets"); goto bad; } /* * Second pass: extract other information. */ for (dp = dir, n = dircount; n > 0; n--, dp++) { if (dp->tdir_tag == IGNORE) continue; switch (dp->tdir_tag) { case TIFFTAG_MINSAMPLEVALUE: case TIFFTAG_MAXSAMPLEVALUE: case TIFFTAG_BITSPERSAMPLE: /* * The 5.0 spec says the Compression tag has * one value, while earlier specs say it has * one value per sample. Because of this, we * accept the tag if one value is supplied. * * The MinSampleValue, MaxSampleValue and * BitsPerSample tags are supposed to be written * as one value/sample, but some vendors incorrectly * write one value only -- so we accept that * as well (yech). */ if (dp->tdir_count == 1) { v = TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset); if (!TIFFSetField(tif, dp->tdir_tag, (int)v)) goto bad; break; } /* fall thru... */ case TIFFTAG_DATATYPE: case TIFFTAG_SAMPLEFORMAT: if (!TIFFFetchPerSampleShorts(tif, dp, &iv) || !TIFFSetField(tif, dp->tdir_tag, iv)) goto bad; break; case TIFFTAG_SMINSAMPLEVALUE: case TIFFTAG_SMAXSAMPLEVALUE: if (!TIFFFetchPerSampleAnys(tif, dp, &dv) || !TIFFSetField(tif, dp->tdir_tag, dv)) goto bad; break; case TIFFTAG_STRIPOFFSETS: case TIFFTAG_TILEOFFSETS: if (!TIFFFetchStripThing(tif, dp, td->td_nstrips, &td->td_stripoffset)) goto bad; break; case TIFFTAG_STRIPBYTECOUNTS: case TIFFTAG_TILEBYTECOUNTS: if (!TIFFFetchStripThing(tif, dp, td->td_nstrips, &td->td_stripbytecount)) goto bad; break; case TIFFTAG_COLORMAP: case TIFFTAG_TRANSFERFUNCTION: /* * TransferFunction can have either 1x or 3x data * values; Colormap can have only 3x items. */ v = 1L<<td->td_bitspersample; if (dp->tdir_tag == TIFFTAG_COLORMAP || dp->tdir_count != (uint32) v) { if (!CheckDirCount(tif, dp, (uint32)(3*v))) break; } v *= sizeof (uint16); cp = CheckMalloc(tif, dp->tdir_count * sizeof (uint16), "to read \"TransferFunction\" tag"); if (cp != NULL) { if (TIFFFetchData(tif, dp, cp)) { /* * This deals with there being only * one array to apply to all samples. */ uint32 c = (uint32)1 << td->td_bitspersample; if (dp->tdir_count == c) v = 0; TIFFSetField(tif, dp->tdir_tag, cp, cp+v, cp+2*v); } _TIFFfree(cp); } break; case TIFFTAG_PAGENUMBER: case TIFFTAG_HALFTONEHINTS: case TIFFTAG_YCBCRSUBSAMPLING: case TIFFTAG_DOTRANGE: (void) TIFFFetchShortPair(tif, dp); break; #ifdef COLORIMETRY_SUPPORT case TIFFTAG_REFERENCEBLACKWHITE: (void) TIFFFetchRefBlackWhite(tif, dp); break; #endif /* BEGIN REV 4.0 COMPATIBILITY */ case TIFFTAG_OSUBFILETYPE: v = 0; switch (TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset)) { case OFILETYPE_REDUCEDIMAGE: v = FILETYPE_REDUCEDIMAGE; break; case OFILETYPE_PAGE: v = FILETYPE_PAGE; break; } if (v) (void) TIFFSetField(tif, TIFFTAG_SUBFILETYPE, (int)v); break; /* END REV 4.0 COMPATIBILITY */ default: (void) TIFFFetchNormalTag(tif, dp); break; } } /* * Verify Palette image has a Colormap. */ if (td->td_photometric == PHOTOMETRIC_PALETTE && !TIFFFieldSet(tif, FIELD_COLORMAP)) { MissingRequired(tif, "Colormap"); goto bad; } /* * Attempt to deal with a missing StripByteCounts tag. */ if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) { /* * Some manufacturers violate the spec by not giving * the size of the strips. In this case, assume there * is one uncompressed strip of data. */ if ((td->td_planarconfig == PLANARCONFIG_CONTIG && td->td_nstrips > 1) || (td->td_planarconfig == PLANARCONFIG_SEPARATE && td->td_nstrips != td->td_samplesperpixel)) { MissingRequired(tif, "StripByteCounts"); goto bad; } TIFFWarning(tif->tif_name, "TIFF directory is missing required \"%s\" field, calculating from imagelength", _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name); EstimateStripByteCounts(tif, dir, dircount); #define BYTECOUNTLOOKSBAD \ ((td->td_stripbytecount[0] == 0 && td->td_stripoffset[0] != 0) || \ (td->td_compression == COMPRESSION_NONE && \ td->td_stripbytecount[0] > TIFFGetFileSize(tif) - td->td_stripoffset[0])) } else if (td->td_nstrips == 1 && BYTECOUNTLOOKSBAD) { /* * Plexus (and others) sometimes give a value * of zero for a tag when they don't know what * the correct value is! Try and handle the * simple case of estimating the size of a one * strip image. */ TIFFWarning(tif->tif_name, "Bogus \"%s\" field, ignoring and calculating from imagelength", _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name); EstimateStripByteCounts(tif, dir, dircount); } if (dir) _TIFFfree((char *)dir); if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE)) td->td_maxsamplevalue = (uint16)((1L<<td->td_bitspersample)-1); /* * Setup default compression scheme. */ if (!TIFFFieldSet(tif, FIELD_COMPRESSION)) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); /* * Some manufacturers make life difficult by writing * large amounts of uncompressed data as a single strip. * This is contrary to the recommendations of the spec. * The following makes an attempt at breaking such images * into strips closer to the recommended 8k bytes. A * side effect, however, is that the RowsPerStrip tag * value may be changed. */ if (td->td_nstrips == 1 && td->td_compression == COMPRESSION_NONE && (tif->tif_flags & (TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP) ChopUpSingleUncompressedStrip(tif); /* * Reinitialize i/o since we are starting on a new directory. */ tif->tif_row = (uint32) -1; tif->tif_curstrip = (tstrip_t) -1; tif->tif_col = (uint32) -1; tif->tif_curtile = (ttile_t) -1; tif->tif_tilesize = TIFFTileSize(tif); tif->tif_scanlinesize = TIFFScanlineSize(tif); return (1); bad: if (dir) _TIFFfree(dir); return (0); }