/* * Write the contents of the current directory * to the specified file. This routine doesn't * handle overwriting a directory with auxiliary * storage that's been changed. */ int TIFFWriteDirectory(TIFF* tif) { uint16 dircount; toff_t diroff; ttag_t tag; uint32 nfields; tsize_t dirsize; char* data; TIFFDirEntry* dir; TIFFDirectory* td; u_long b, fields[FIELD_SETLONGS]; int fi, nfi; if (tif->tif_mode == O_RDONLY) return (1); /* * Clear write state so that subsequent images with * different characteristics get the right buffers * setup for them. */ if (tif->tif_flags & TIFF_POSTENCODE) { tif->tif_flags &= ~TIFF_POSTENCODE; if (!(*tif->tif_postencode)(tif)) { TIFFError(tif->tif_name, "Error post-encoding before directory write"); return (0); } } (*tif->tif_close)(tif); /* shutdown encoder */ /* * Flush any data that might have been written * by the compression close+cleanup routines. */ if (tif->tif_rawcc > 0 && !TIFFFlushData1(tif)) { TIFFError(tif->tif_name, "Error flushing data before directory write"); return (0); } if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) { _TIFFfree(tif->tif_rawdata); tif->tif_rawdata = NULL; tif->tif_rawcc = 0; tif->tif_rawdatasize = 0; } tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP); td = &tif->tif_dir; /* * Size the directory so that we can calculate * offsets for the data items that aren't kept * in-place in each field. */ nfields = 0; for (b = 0; b <= FIELD_LAST; b++) if (TIFFFieldSet(tif, b)) nfields += (b < FIELD_SUBFILETYPE ? 2 : 1); dirsize = nfields * sizeof (TIFFDirEntry); data = (char*) _TIFFmalloc(dirsize); if (data == NULL) { TIFFError(tif->tif_name, "Cannot write directory, out of space"); return (0); } /* * Directory hasn't been placed yet, put * it at the end of the file and link it * into the existing directory structure. */ if (tif->tif_diroff == 0 && !TIFFLinkDirectory(tif)) goto bad; tif->tif_dataoff = (toff_t)( tif->tif_diroff + sizeof (uint16) + dirsize + sizeof (toff_t)); if (tif->tif_dataoff & 1) tif->tif_dataoff++; (void) TIFFSeekFile(tif, tif->tif_dataoff, SEEK_SET); tif->tif_curdir++; dir = (TIFFDirEntry*) data; /* * Setup external form of directory * entries and write data items. */ _TIFFmemcpy(fields, td->td_fieldsset, sizeof (fields)); /* * Write out ExtraSamples tag only if * extra samples are present in the data. */ if (FieldSet(fields, FIELD_EXTRASAMPLES) && !td->td_extrasamples) { ResetFieldBit(fields, FIELD_EXTRASAMPLES); nfields--; dirsize -= sizeof (TIFFDirEntry); } /*XXX*/ for (fi = 0, nfi = tif->tif_nfields; nfi > 0; nfi--, fi++) { const TIFFFieldInfo* fip = tif->tif_fieldinfo[fi]; if (!FieldSet(fields, fip->field_bit)) continue; switch (fip->field_bit) { case FIELD_STRIPOFFSETS: /* * We use one field bit for both strip and tile * offsets, and so must be careful in selecting * the appropriate field descriptor (so that tags * are written in sorted order). */ tag = isTiled(tif) ? TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS; if (tag != fip->field_tag) continue; if (!TIFFWriteLongArray(tif, TIFF_LONG, tag, dir, (uint32) td->td_nstrips, td->td_stripoffset)) goto bad; break; case FIELD_STRIPBYTECOUNTS: /* * We use one field bit for both strip and tile * byte counts, and so must be careful in selecting * the appropriate field descriptor (so that tags * are written in sorted order). */ tag = isTiled(tif) ? TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS; if (tag != fip->field_tag) continue; if (!TIFFWriteLongArray(tif, TIFF_LONG, tag, dir, (uint32) td->td_nstrips, td->td_stripbytecount)) goto bad; break; case FIELD_ROWSPERSTRIP: TIFFSetupShortLong(tif, TIFFTAG_ROWSPERSTRIP, dir, td->td_rowsperstrip); break; case FIELD_COLORMAP: if (!TIFFWriteShortTable(tif, TIFFTAG_COLORMAP, dir, 3, td->td_colormap)) goto bad; break; case FIELD_IMAGEDIMENSIONS: TIFFSetupShortLong(tif, TIFFTAG_IMAGEWIDTH, dir++, td->td_imagewidth); TIFFSetupShortLong(tif, TIFFTAG_IMAGELENGTH, dir, td->td_imagelength); break; case FIELD_TILEDIMENSIONS: TIFFSetupShortLong(tif, TIFFTAG_TILEWIDTH, dir++, td->td_tilewidth); TIFFSetupShortLong(tif, TIFFTAG_TILELENGTH, dir, td->td_tilelength); break; case FIELD_POSITION: WriteRationalPair(TIFF_RATIONAL, TIFFTAG_XPOSITION, td->td_xposition, TIFFTAG_YPOSITION, td->td_yposition); break; case FIELD_RESOLUTION: WriteRationalPair(TIFF_RATIONAL, TIFFTAG_XRESOLUTION, td->td_xresolution, TIFFTAG_YRESOLUTION, td->td_yresolution); break; case FIELD_BITSPERSAMPLE: case FIELD_MINSAMPLEVALUE: case FIELD_MAXSAMPLEVALUE: case FIELD_SAMPLEFORMAT: if (!TIFFWritePerSampleShorts(tif, fip->field_tag, dir)) goto bad; break; case FIELD_SMINSAMPLEVALUE: case FIELD_SMAXSAMPLEVALUE: if (!TIFFWritePerSampleAnys(tif, _TIFFSampleToTagType(tif), fip->field_tag, dir)) goto bad; break; case FIELD_PAGENUMBER: case FIELD_HALFTONEHINTS: #ifdef YCBCR_SUPPORT case FIELD_YCBCRSUBSAMPLING: #endif #ifdef CMYK_SUPPORT case FIELD_DOTRANGE: #endif if (!TIFFSetupShortPair(tif, fip->field_tag, dir)) goto bad; break; #ifdef CMYK_SUPPORT case FIELD_INKNAMES: if (!TIFFWriteInkNames(tif, dir)) goto bad; break; #endif #ifdef COLORIMETRY_SUPPORT case FIELD_TRANSFERFUNCTION: if (!TIFFWriteTransferFunction(tif, dir)) goto bad; break; #endif #if SUBIFD_SUPPORT case FIELD_SUBIFD: if (!TIFFWriteNormalTag(tif, dir, fip)) goto bad; /* * Total hack: if this directory includes a SubIFD * tag then force the next <n> directories to be * written as ``sub directories'' of this one. This * is used to write things like thumbnails and * image masks that one wants to keep out of the * normal directory linkage access mechanism. */ if (dir->tdir_count > 0) { tif->tif_flags |= TIFF_INSUBIFD; tif->tif_nsubifd = (uint16) dir->tdir_count; if (dir->tdir_count > 1) tif->tif_subifdoff = dir->tdir_offset; else tif->tif_subifdoff = (uint32)( tif->tif_diroff + sizeof (uint16) + ((char*)&dir->tdir_offset-data)); } break; #endif default: if (!TIFFWriteNormalTag(tif, dir, fip)) goto bad; break; } dir++; ResetFieldBit(fields, fip->field_bit); } /* * Write directory. */ dircount = (uint16) nfields; diroff = (uint32) tif->tif_nextdiroff; if (tif->tif_flags & TIFF_SWAB) { /* * The file's byte order is opposite to the * native machine architecture. We overwrite * the directory information with impunity * because it'll be released below after we * write it to the file. Note that all the * other tag construction routines assume that * we do this byte-swapping; i.e. they only * byte-swap indirect data. */ for (dir = (TIFFDirEntry*) data; dircount; dir++, dircount--) { TIFFSwabArrayOfShort(&dir->tdir_tag, 2); TIFFSwabArrayOfLong(&dir->tdir_count, 2); } dircount = (uint16) nfields; TIFFSwabShort(&dircount); TIFFSwabLong(&diroff); } (void) TIFFSeekFile(tif, tif->tif_diroff, SEEK_SET); if (!WriteOK(tif, &dircount, sizeof (dircount))) { TIFFError(tif->tif_name, "Error writing directory count"); goto bad; } if (!WriteOK(tif, data, dirsize)) { TIFFError(tif->tif_name, "Error writing directory contents"); goto bad; } if (!WriteOK(tif, &diroff, sizeof (diroff))) { TIFFError(tif->tif_name, "Error writing directory link"); goto bad; } TIFFFreeDirectory(tif); _TIFFfree(data); tif->tif_flags &= ~TIFF_DIRTYDIRECT; (*tif->tif_cleanup)(tif); /* * Reset directory-related state for subsequent * directories. */ TIFFCreateDirectory(tif); return (1); bad: _TIFFfree(data); return (0); }
/* * Write an array of ``type'' values for a specified tag (i.e. this is a tag * which is allowed to have different types, e.g. SMaxSampleType). * Internally the data values are represented as double since a double can * hold any of the TIFF tag types (yes, this should really be an abstract * type tany_t for portability). The data is converted into the specified * type in a temporary buffer and then handed off to the appropriate array * writer. */ static int TIFFWriteAnyArray(TIFF* tif, TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, double* v) { char buf[10 * sizeof(double)]; char* w = buf; int i, status = 0; if (n * tiffDataWidth[type] > sizeof buf) w = (char*) _TIFFmalloc(n * tiffDataWidth[type]); switch (type) { case TIFF_BYTE: { uint8* bp = (uint8*) w; for (i = 0; i < (int) n; i++) bp[i] = (uint8) v[i]; dir->tdir_tag = (uint16) tag; dir->tdir_type = (short) type; dir->tdir_count = n; if (!TIFFWriteByteArray(tif, dir, (char*) bp)) goto out; } break; case TIFF_SBYTE: { int8* bp = (int8*) w; for (i = 0; i < (int) n; i++) bp[i] = (int8) v[i]; dir->tdir_tag = (uint16) tag; dir->tdir_type = (short) type; dir->tdir_count = n; if (!TIFFWriteByteArray(tif, dir, (char*) bp)) goto out; } break; case TIFF_SHORT: { uint16* bp = (uint16*) w; for (i = 0; i < (int) n; i++) bp[i] = (uint16) v[i]; if (!TIFFWriteShortArray(tif, type, tag, dir, n, (uint16*)bp)) goto out; } break; case TIFF_SSHORT: { int16* bp = (int16*) w; for (i = 0; i < (int) n; i++) bp[i] = (int16) v[i]; if (!TIFFWriteShortArray(tif, type, tag, dir, n, (uint16*)bp)) goto out; } break; case TIFF_LONG: { uint32* bp = (uint32*) w; for (i = 0; i < (int) n; i++) bp[i] = (uint32) v[i]; if (!TIFFWriteLongArray(tif, type, tag, dir, n, bp)) goto out; } break; case TIFF_SLONG: { int32* bp = (int32*) w; for (i = 0; i < (int) n; i++) bp[i] = (int32) v[i]; if (!TIFFWriteLongArray(tif, type, tag, dir, n, (uint32*) bp)) goto out; } break; case TIFF_FLOAT: { float* bp = (float*) w; for (i = 0; i < (int) n; i++) bp[i] = (float) v[i]; if (!TIFFWriteFloatArray(tif, type, tag, dir, n, bp)) goto out; } break; case TIFF_DOUBLE: return (TIFFWriteDoubleArray(tif, type, tag, dir, n, v)); default: /* TIFF_NOTYPE */ /* TIFF_ASCII */ /* TIFF_UNDEFINED */ /* TIFF_RATIONAL */ /* TIFF_SRATIONAL */ goto out; } status = 1; out: if (w != buf) _TIFFfree(w); return (status); }
/* * Process tags that are not special cased. */ static int TIFFWriteNormalTag(TIFF* tif, TIFFDirEntry* dir, const TIFFFieldInfo* fip) { uint16 wc = (uint16) fip->field_writecount; uint32 wc2; dir->tdir_tag = (uint16) fip->field_tag; dir->tdir_type = (uint16) fip->field_type; dir->tdir_count = wc; switch (fip->field_type) { case TIFF_SHORT: case TIFF_SSHORT: if (fip->field_passcount) { uint16* wp; if (wc == (uint16) TIFF_VARIABLE2) { TIFFGetField(tif, fip->field_tag, &wc2, &wp); dir->tdir_count = wc2; } else { /* Assume TIFF_VARIABLE */ TIFFGetField(tif, fip->field_tag, &wc, &wp); dir->tdir_count = wc; } if (!TIFFWriteShortArray(tif, dir, wp)) return 0; } else { if (wc == 1) { uint16 sv; TIFFGetField(tif, fip->field_tag, &sv); dir->tdir_offset = TIFFInsertData(tif, dir->tdir_type, sv); } else { uint16* wp; TIFFGetField(tif, fip->field_tag, &wp); if (!TIFFWriteShortArray(tif, dir, wp)) return 0; } } break; case TIFF_LONG: case TIFF_SLONG: case TIFF_IFD: if (fip->field_passcount) { uint32* lp; if (wc == (uint16) TIFF_VARIABLE2) { TIFFGetField(tif, fip->field_tag, &wc2, &lp); dir->tdir_count = wc2; } else { /* Assume TIFF_VARIABLE */ TIFFGetField(tif, fip->field_tag, &wc, &lp); dir->tdir_count = wc; } if (!TIFFWriteLongArray(tif, dir, lp)) return 0; } else { if (wc == 1) { /* XXX handle LONG->SHORT conversion */ TIFFGetField(tif, fip->field_tag, &dir->tdir_offset); } else { uint32* lp; TIFFGetField(tif, fip->field_tag, &lp); if (!TIFFWriteLongArray(tif, dir, lp)) return 0; } } break; case TIFF_RATIONAL: case TIFF_SRATIONAL: if (fip->field_passcount) { float* fp; if (wc == (uint16) TIFF_VARIABLE2) { TIFFGetField(tif, fip->field_tag, &wc2, &fp); dir->tdir_count = wc2; } else { /* Assume TIFF_VARIABLE */ TIFFGetField(tif, fip->field_tag, &wc, &fp); dir->tdir_count = wc; } if (!TIFFWriteRationalArray(tif, dir, fp)) return 0; } else { if (wc == 1) { float fv; TIFFGetField(tif, fip->field_tag, &fv); if (!TIFFWriteRationalArray(tif, dir, &fv)) return 0; } else { float* fp; TIFFGetField(tif, fip->field_tag, &fp); if (!TIFFWriteRationalArray(tif, dir, fp)) return 0; } } break; case TIFF_FLOAT: if (fip->field_passcount) { float* fp; if (wc == (uint16) TIFF_VARIABLE2) { TIFFGetField(tif, fip->field_tag, &wc2, &fp); dir->tdir_count = wc2; } else { /* Assume TIFF_VARIABLE */ TIFFGetField(tif, fip->field_tag, &wc, &fp); dir->tdir_count = wc; } if (!TIFFWriteFloatArray(tif, dir, fp)) return 0; } else { if (wc == 1) { float fv; TIFFGetField(tif, fip->field_tag, &fv); if (!TIFFWriteFloatArray(tif, dir, &fv)) return 0; } else { float* fp; TIFFGetField(tif, fip->field_tag, &fp); if (!TIFFWriteFloatArray(tif, dir, fp)) return 0; } } break; case TIFF_DOUBLE: if (fip->field_passcount) { double* dp; if (wc == (uint16) TIFF_VARIABLE2) { TIFFGetField(tif, fip->field_tag, &wc2, &dp); dir->tdir_count = wc2; } else { /* Assume TIFF_VARIABLE */ TIFFGetField(tif, fip->field_tag, &wc, &dp); dir->tdir_count = wc; } if (!TIFFWriteDoubleArray(tif, dir, dp)) return 0; } else { if (wc == 1) { double dv; TIFFGetField(tif, fip->field_tag, &dv); if (!TIFFWriteDoubleArray(tif, dir, &dv)) return 0; } else { double* dp; TIFFGetField(tif, fip->field_tag, &dp); if (!TIFFWriteDoubleArray(tif, dir, dp)) return 0; } } break; case TIFF_ASCII: { char* cp; if (fip->field_passcount) { if( wc == (uint16) TIFF_VARIABLE2 ) TIFFGetField(tif, fip->field_tag, &wc2, &cp); else TIFFGetField(tif, fip->field_tag, &wc, &cp); } else TIFFGetField(tif, fip->field_tag, &cp); dir->tdir_count = (uint32) (strlen(cp) + 1); if (!TIFFWriteByteArray(tif, dir, cp)) return (0); } break; case TIFF_BYTE: case TIFF_SBYTE: if (fip->field_passcount) { char* cp; if (wc == (uint16) TIFF_VARIABLE2) { TIFFGetField(tif, fip->field_tag, &wc2, &cp); dir->tdir_count = wc2; } else { /* Assume TIFF_VARIABLE */ TIFFGetField(tif, fip->field_tag, &wc, &cp); dir->tdir_count = wc; } if (!TIFFWriteByteArray(tif, dir, cp)) return 0; } else { if (wc == 1) { char cv; TIFFGetField(tif, fip->field_tag, &cv); if (!TIFFWriteByteArray(tif, dir, &cv)) return 0; } else { char* cp; TIFFGetField(tif, fip->field_tag, &cp); if (!TIFFWriteByteArray(tif, dir, cp)) return 0; } } break; case TIFF_UNDEFINED: { char* cp; if (wc == (unsigned short) TIFF_VARIABLE) { TIFFGetField(tif, fip->field_tag, &wc, &cp); dir->tdir_count = wc; } else if (wc == (unsigned short) TIFF_VARIABLE2) { TIFFGetField(tif, fip->field_tag, &wc2, &cp); dir->tdir_count = wc2; } else TIFFGetField(tif, fip->field_tag, &cp); if (!TIFFWriteByteArray(tif, dir, cp)) return (0); } break; case TIFF_NOTYPE: break; } return (1); }
/* * Write an array of ``type'' values for a specified tag (i.e. this is a tag * which is allowed to have different types, e.g. SMaxSampleType). * Internally the data values are represented as double since a double can * hold any of the TIFF tag types (yes, this should really be an abstract * type tany_t for portability). The data is converted into the specified * type in a temporary buffer and then handed off to the appropriate array * writer. */ static int TIFFWriteAnyArray(TIFF* tif, TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, double* v) { char buf[10 * sizeof(double)]; char* w = buf; int i, status = 0; if (n * TIFFDataWidth(type) > sizeof buf) { w = (char*) _TIFFmalloc(n * TIFFDataWidth(type)); if (w == NULL) { TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space to write array"); return (0); } } dir->tdir_tag = (uint16) tag; dir->tdir_type = (uint16) type; dir->tdir_count = n; switch (type) { case TIFF_BYTE: { uint8* bp = (uint8*) w; for (i = 0; i < (int) n; i++) bp[i] = (uint8) v[i]; if (!TIFFWriteByteArray(tif, dir, (char*) bp)) goto out; } break; case TIFF_SBYTE: { int8* bp = (int8*) w; for (i = 0; i < (int) n; i++) bp[i] = (int8) v[i]; if (!TIFFWriteByteArray(tif, dir, (char*) bp)) goto out; } break; case TIFF_SHORT: { uint16* bp = (uint16*) w; for (i = 0; i < (int) n; i++) bp[i] = (uint16) v[i]; if (!TIFFWriteShortArray(tif, dir, (uint16*)bp)) goto out; } break; case TIFF_SSHORT: { int16* bp = (int16*) w; for (i = 0; i < (int) n; i++) bp[i] = (int16) v[i]; if (!TIFFWriteShortArray(tif, dir, (uint16*)bp)) goto out; } break; case TIFF_LONG: { uint32* bp = (uint32*) w; for (i = 0; i < (int) n; i++) bp[i] = (uint32) v[i]; if (!TIFFWriteLongArray(tif, dir, bp)) goto out; } break; case TIFF_SLONG: { int32* bp = (int32*) w; for (i = 0; i < (int) n; i++) bp[i] = (int32) v[i]; if (!TIFFWriteLongArray(tif, dir, (uint32*) bp)) goto out; } break; case TIFF_FLOAT: { float* bp = (float*) w; for (i = 0; i < (int) n; i++) bp[i] = (float) v[i]; if (!TIFFWriteFloatArray(tif, dir, bp)) goto out; } break; case TIFF_DOUBLE: { if( !TIFFWriteDoubleArray(tif, dir, v)) goto out; } default: /* TIFF_NOTYPE */ /* TIFF_ASCII */ /* TIFF_UNDEFINED */ /* TIFF_RATIONAL */ /* TIFF_SRATIONAL */ goto out; } status = 1; out: if (w != buf) _TIFFfree(w); return (status); }
/* * Write an array of ``type'' values for a specified tag (i.e. this is a tag * which is allowed to have different types, e.g. SMaxSampleType). * Internally the data values are represented as double since a double can * hold any of the TIFF tag types (yes, this should really be an abstract * type tany_t for portability). The data is converted into the specified * type in a temporary buffer and then handed off to the appropriate array * writer. */ static int TIFFWriteAnyArray(TIFF* tif, TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, double* v) { char buf[10 * sizeof(double)]; char* w = buf; int i, status = 0; if (n * TIFFDataWidth(type) > sizeof buf) w = (char*) _TIFFmalloc(n * TIFFDataWidth(type)); switch (type) { case TIFF_BYTE: { unsigned char* bp = (unsigned char*) w; #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++) bp[i] = (unsigned char) v[i]; dir->tdir_tag = tag; dir->tdir_type = (short) type; dir->tdir_count = n; if (!TIFFWriteByteArray(tif, dir, (char*) bp)) goto out; } break; case TIFF_SBYTE: { signed char* bp = (signed char*) w; #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++) bp[i] = (signed char) v[i]; dir->tdir_tag = tag; dir->tdir_type = (short) type; dir->tdir_count = n; if (!TIFFWriteByteArray(tif, dir, (char*) bp)) goto out; } break; case TIFF_SHORT: { uint16* bp = (uint16*) w; #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++) bp[i] = (uint16) v[i]; if (!TIFFWriteShortArray(tif, type, tag, dir, n, (uint16*)bp)) goto out; } break; case TIFF_SSHORT: { int16* bp = (int16*) w; #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++) bp[i] = (int16) v[i]; if (!TIFFWriteShortArray(tif, type, tag, dir, n, (uint16*)bp)) goto out; } break; case TIFF_LONG: { uint32* bp = (uint32*) w; #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++) bp[i] = (uint32) v[i]; if (!TIFFWriteLongArray(tif, type, tag, dir, n, bp)) goto out; } break; case TIFF_SLONG: { int32* bp = (int32*) w; #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++) bp[i] = (int32) v[i]; if (!TIFFWriteLongArray(tif, type, tag, dir, n, (uint32*) bp)) goto out; } break; case TIFF_FLOAT: { float* bp = (float*) w; #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++) bp[i] = (float) v[i]; if (!TIFFWriteFloatArray(tif, type, tag, dir, n, bp)) goto out; } break; case TIFF_DOUBLE: return (TIFFWriteDoubleArray(tif, type, tag, dir, n, v)); default: /* TIFF_NOTYPE */ /* TIFF_ASCII */ /* TIFF_UNDEFINED */ /* TIFF_RATIONAL */ /* TIFF_SRATIONAL */ goto out; } status = 1; out: if (w != buf) _TIFFfree(w); return (status); }
/* * Write the contents of the current directory * to the specified file. This routine doesn't * handle overwriting a directory with auxiliary * storage that's been changed. */ static int _TIFFWriteDirectory(TIFF* tif, int done) { uint64 dircount; /* 16-bit or 64-bit directory entry count */ toff_t diroff; /* 32-bit or 64-bit directory offset */ ttag_t tag; uint32 nfields; tsize_t dirsize; char* data; TIFFDirEntry* dir; TIFFDirectory* td; unsigned long b, fields[FIELD_SETLONGS]; int fi, nfi; if (tif->tif_mode == O_RDONLY) return (1); /* * Clear write state so that subsequent images with * different characteristics get the right buffers * setup for them. */ if (done) { if (tif->tif_flags & TIFF_POSTENCODE) { tif->tif_flags &= ~TIFF_POSTENCODE; if (!(*tif->tif_postencode)(tif)) { TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error post-encoding before directory write"); return (0); } } (*tif->tif_close)(tif); /* shutdown encoder */ /* * Flush any data that might have been written * by the compression close+cleanup routines. */ if (tif->tif_rawcc > 0 && !TIFFFlushData1(tif)) { TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error flushing data before directory write"); return (0); } if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) { _TIFFfree(tif->tif_rawdata); tif->tif_rawdata = NULL; tif->tif_rawcc = 0; tif->tif_rawdatasize = 0; } tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP); } /* * Loop to allow retry in case transition to BigTIFF required */ while (1) { /* * Directory hasn't been placed yet, put * it at the end of the file and link it * into the existing directory structure. */ if (tif->tif_diroff == 0 && !TIFFLinkDirectory(tif)) goto bad2; td = &tif->tif_dir; /* * Size the directory so that we can calculate * offsets for the data items that aren't kept * in-place in each field. */ nfields = 0; for (b = 0; b <= FIELD_LAST; b++) if (TIFFFieldSet(tif, b) && b != FIELD_CUSTOM) nfields += (b < FIELD_SUBFILETYPE ? 2 : 1); nfields += td->td_customValueCount; dirsize = nfields * TDIREntryLen(tif); data = (char*) _TIFFmalloc(dirsize); if (data == NULL) { TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Cannot write directory, out of space"); goto bad2; } _TIFFmemset(data, 0, dirsize); /* * Setup external form of directory * entries and write data items. */ _TIFFmemcpy(fields, td->td_fieldsset, sizeof (fields)); /* * Write out ExtraSamples tag only if * extra samples are present in the data. */ if (FieldSet(fields, FIELD_EXTRASAMPLES) && !td->td_extrasamples) { ResetFieldBit(fields, FIELD_EXTRASAMPLES); nfields--; dirsize -= TDIREntryLen(tif); } /*XXX*/ tif->tif_dataoff = tif->tif_diroff + TIFFDirCntLen(tif) + dirsize + TIFFDirOffLen(tif); (void) TIFFSeekFile(tif, tif->tif_dataoff, SEEK_SET); dir = (TIFFDirEntry*) data; for (fi = 0, nfi = tif->tif_nfields; nfi > 0; nfi--, fi++) { const TIFFFieldInfo* fip = tif->tif_fieldinfo[fi]; /* ** For custom fields, we test to see if the custom field ** is set or not. For normal fields, we just use the ** FieldSet test. */ if( fip->field_bit == FIELD_CUSTOM ) { int ci, is_set = FALSE; for( ci = 0; ci < td->td_customValueCount; ci++ ) is_set |= (td->td_customValues[ci].info == fip); if( !is_set ) continue; } else if (!FieldSet(fields, fip->field_bit)) continue; /* ** Handle other fields. */ switch (fip->field_bit) { case FIELD_STRIPOFFSETS: /* * We use one field bit for both strip and tile * offsets, and so must be careful in selecting * the appropriate field descriptor (so that tags * are written in sorted order). */ tag = (isTiled(tif) ? TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS); if (tag != fip->field_tag) continue; dir->s.tdir_tag = (uint16) tag; TDIRSetEntryCount(tif,dir, (uint32) td->td_nstrips); /* * If file is BigTIFF strip/tile offsets are written as 64-bit, * else convert to array of 32-bit offsets. */ if (isBigTIFF(tif)) { dir->s.tdir_type = (uint16) TIFF_LONG8; if (!TIFFWriteLong8Array(tif, dir, td->td_stripoffset)) goto bad; } else { uint32 tdi; int status; uint32 *soff = _TIFFCheckMalloc(tif, td->td_nstrips, sizeof(uint32), "32-bit offset array"); dir->s.tdir_type = (uint16) TIFF_LONG; for (tdi = 0; tdi < td->td_nstrips; tdi++) soff[tdi] = (uint32) td->td_stripoffset[tdi]; status = TIFFWriteLongArray(tif, dir, soff); _TIFFfree(soff); if (!status) goto bad; } break; case FIELD_STRIPBYTECOUNTS: /* * We use one field bit for both strip and tile * byte counts, and so must be careful in selecting * the appropriate field descriptor (so that tags * are written in sorted order). */ tag = (isTiled(tif) ? TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS); if (tag != fip->field_tag) continue; dir->s.tdir_tag = (uint16) tag; dir->s.tdir_type = (uint16) TIFF_LONG; TDIRSetEntryCount(tif,dir, (uint32) td->td_nstrips); if (!TIFFWriteLongArray(tif, dir, td->td_stripbytecount)) goto bad; break; case FIELD_ROWSPERSTRIP: TIFFSetupShortLong(tif, TIFFTAG_ROWSPERSTRIP, dir, td->td_rowsperstrip); break; case FIELD_COLORMAP: if (!TIFFWriteShortTable(tif, TIFFTAG_COLORMAP, dir, 3, td->td_colormap)) goto bad; break; case FIELD_IMAGEDIMENSIONS: TIFFSetupShortLong(tif, TIFFTAG_IMAGEWIDTH, dir, td->td_imagewidth); TDIREntryNext(tif,dir); TIFFSetupShortLong(tif, TIFFTAG_IMAGELENGTH, dir, td->td_imagelength); break; case FIELD_TILEDIMENSIONS: TIFFSetupShortLong(tif, TIFFTAG_TILEWIDTH, dir, td->td_tilewidth); TDIREntryNext(tif,dir); TIFFSetupShortLong(tif, TIFFTAG_TILELENGTH, dir, td->td_tilelength); break; case FIELD_COMPRESSION: TIFFSetupShort(tif, TIFFTAG_COMPRESSION, dir, td->td_compression); break; case FIELD_PHOTOMETRIC: TIFFSetupShort(tif, TIFFTAG_PHOTOMETRIC, dir, td->td_photometric); break; case FIELD_POSITION: WriteRationalPair(TIFF_RATIONAL, TIFFTAG_XPOSITION, td->td_xposition, TIFFTAG_YPOSITION, td->td_yposition); break; case FIELD_RESOLUTION: WriteRationalPair(TIFF_RATIONAL, TIFFTAG_XRESOLUTION, td->td_xresolution, TIFFTAG_YRESOLUTION, td->td_yresolution); break; case FIELD_BITSPERSAMPLE: case FIELD_MINSAMPLEVALUE: case FIELD_MAXSAMPLEVALUE: case FIELD_SAMPLEFORMAT: if (!TIFFWritePerSampleShorts(tif, fip->field_tag, dir)) goto bad; break; case FIELD_SMINSAMPLEVALUE: case FIELD_SMAXSAMPLEVALUE: if (!TIFFWritePerSampleAnys(tif, _TIFFSampleToTagType(tif), fip->field_tag, dir)) goto bad; break; case FIELD_PAGENUMBER: case FIELD_HALFTONEHINTS: case FIELD_YCBCRSUBSAMPLING: if (!TIFFSetupShortPair(tif, fip->field_tag, dir)) goto bad; break; case FIELD_INKNAMES: if (!TIFFWriteInkNames(tif, dir)) goto bad; break; case FIELD_TRANSFERFUNCTION: if (!TIFFWriteTransferFunction(tif, dir)) goto bad; break; case FIELD_SUBIFD: /* * Total hack: if this directory includes a SubIFD * tag then force the next <n> directories to be * written as ``sub directories'' of this one. This * is used to write things like thumbnails and * image masks that one wants to keep out of the * normal directory linkage access mechanism. * * If file is BigTIFF subdirectory offsets are written as 64-bit, * else as array of 32-bit offsets. At this point the actual * offset values are unknown (will be set in TIFFLinkDirectory). */ dir->s.tdir_tag = (uint16) fip->field_tag; TDIRSetEntryCount(tif,dir, (uint32) td->td_nsubifd); TDIRSetEntryOff(tif,dir, 0); if (td->td_nsubifd > 0) { tif->tif_flags |= TIFF_INSUBIFD; if (isBigTIFF(tif)) { toff_t *doff = _TIFFCheckMalloc(tif, td->td_nsubifd, sizeof(toff_t), "Allocate SubIFD offset array"); if (!doff) goto bad; _TIFFmemset(doff, 0, td->td_nsubifd * sizeof(toff_t)); dir->b.tdir_type = TIFF_IFD8; if (td->td_nsubifd > TIFFDirOffLen(tif) / sizeof(toff_t)) tif->tif_subifdoff = tif->tif_dataoff; else tif->tif_subifdoff = tif->tif_diroff + TIFFDirCntLen(tif) + (char*) &dir->b.tdir_offset - data; if (!TIFFWriteLong8Array(tif, dir, (toff_t*) doff)) goto bad; _TIFFfree(doff); } else { uint32 *doff = _TIFFCheckMalloc(tif, td->td_nsubifd, sizeof(uint32), "Allocate SubIFD offset array"); if (!doff) goto bad; _TIFFmemset(doff, 0, td->td_nsubifd * sizeof(uint32)); dir->b.tdir_type = TIFF_LONG; if (td->td_nsubifd > TIFFDirOffLen(tif) / sizeof(uint32)) tif->tif_subifdoff = tif->tif_dataoff; else tif->tif_subifdoff = tif->tif_diroff + TIFFDirCntLen(tif) + (char*) &dir->s.tdir_offset - data; if (!TIFFWriteLongArray(tif, dir, doff)) goto bad; _TIFFfree(doff); } } break; default: /* XXX: Should be fixed and removed. */ if (fip->field_tag == TIFFTAG_DOTRANGE) { if (!TIFFSetupShortPair(tif, fip->field_tag, dir)) goto bad; } else if (!TIFFWriteNormalTag(tif, dir, fip)) goto bad; break; } TDIREntryNext(tif,dir); if( fip->field_bit != FIELD_CUSTOM ) ResetFieldBit(fields, fip->field_bit); } /* * Check if BigTIFF now required based on data location. If so reset * data offset to overwrite already-written data for directory, then * convert file to BigTIFF and loop to recreate directory. */ if (isBigTIFF(tif) || !isBigOff(tif->tif_dataoff + sizeof(uint32))) break; _TIFFfree(data); tif->tif_dataoff = tif->tif_diroff; if (!TIFFMakeBigTIFF(tif)) goto bad2; tif->tif_diroff = 0; } /* bottom of BigTIFF retry loop */ /* * Write directory. */ tif->tif_curdir++; TIFFSetDirCnt(tif,dircount, (uint32) nfields); TIFFSetDirOff(tif,diroff,tif->tif_nextdiroff); if (tif->tif_flags & TIFF_SWAB) { /* * The file's byte order is opposite to the * native machine architecture. We overwrite * the directory information with impunity * because it'll be released below after we * write it to the file. Note that all the * other tag construction routines assume that * we do this byte-swapping; i.e. they only * byte-swap indirect data. */ uint32 di; for (dir = (TIFFDirEntry*) data, di = 0; di < nfields; TDIREntryNext(tif,dir), di++) { TIFFSwabShort(&dir->s.tdir_tag); TIFFSwabShort(&dir->s.tdir_type); TDIRSwabEntryCount(tif,dir); TDIRSwabEntryOff(tif,dir); } TIFFSwabDirCnt(tif,&dircount); TIFFSwabDirOff(tif,&diroff); } (void) TIFFSeekFile(tif, tif->tif_diroff, SEEK_SET); if (!WriteOK(tif, &dircount, TIFFDirCntLen(tif))) { TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error writing directory count (%d)", TIFFGetErrno(tif)); goto bad; } if (!WriteOK(tif, data, dirsize)) { TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error writing directory contents (%d)", TIFFGetErrno(tif)); goto bad; } if (!WriteOK(tif, &diroff, TIFFDirOffLen(tif))) { TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error writing directory link (%d)", TIFFGetErrno(tif)); goto bad; } if (done) { TIFFFreeDirectory(tif); tif->tif_flags &= ~TIFF_DIRTYDIRECT; (*tif->tif_cleanup)(tif); /* * Reset directory-related state for subsequent * directories. */ TIFFCreateDirectory(tif); } _TIFFfree(data); return (1); bad: _TIFFfree(data); bad2: return (0); }