📄 tif_pdsdirwrite.c
字号:
if (v > 0xffffL) { dir->tdir_type = (short) TIFF_LONG; dir->tdir_offset = v; } else { dir->tdir_type = (short) TIFF_SHORT; dir->tdir_offset = TIFFInsertData(tif, (int) TIFF_SHORT, v); }}#undef MakeShortDirent#ifndef TIFFWriteRational/* * Setup a RATIONAL directory entry and * write the associated indirect value. */static intTIFFWriteRational(TIFF* tif, TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, float v){ return (TIFFWriteRationalArray(tif, type, tag, dir, 1, &v));}#endif#define NITEMS(x) (sizeof (x) / sizeof (x[0]))/* * Setup a directory entry that references a * samples/pixel array of SHORT values and * (potentially) write the associated indirect * values. */static intTIFFWritePerSampleShorts(TIFF* tif, ttag_t tag, TIFFDirEntry* dir){ uint16 buf[10], v; uint16* w = buf; int i, status, samples = tif->tif_dir.td_samplesperpixel; if (samples > NITEMS(buf)) w = (uint16*) _TIFFmalloc(samples * sizeof (uint16)); TIFFGetField(tif, tag, &v); for (i = 0; i < samples; i++) w[i] = v; status = TIFFWriteShortArray(tif, TIFF_SHORT, tag, dir, samples, w); if (w != buf) _TIFFfree((char*) w); return (status);}/* * Setup a directory entry that references a samples/pixel array of ``type'' * values and (potentially) write the associated indirect values. The source * data from TIFFGetField() for the specified tag must be returned as double. */static intTIFFWritePerSampleAnys(TIFF* tif, TIFFDataType type, ttag_t tag, TIFFDirEntry* dir){ double buf[10], v; double* w = buf; int i, status; int samples = (int) tif->tif_dir.td_samplesperpixel; if (samples > NITEMS(buf)) w = (double*) _TIFFmalloc(samples * sizeof (double)); TIFFGetField(tif, tag, &v); for (i = 0; i < samples; i++) w[i] = v; status = TIFFWriteAnyArray(tif, type, tag, dir, samples, w); if (w != buf) _TIFFfree(w); return (status);}#undef NITEMS/* * Setup a pair of shorts that are returned by * value, rather than as a reference to an array. */static intTIFFSetupShortPair(TIFF* tif, ttag_t tag, TIFFDirEntry* dir){ uint16 v[2]; TIFFGetField(tif, tag, &v[0], &v[1]); return (TIFFWriteShortArray(tif, TIFF_SHORT, tag, dir, 2, v));}/* * Setup a directory entry for an NxM table of shorts, * where M is known to be 2**bitspersample, and write * the associated indirect data. */static intTIFFWriteShortTable(TIFF* tif, ttag_t tag, TIFFDirEntry* dir, uint32 n, uint16** table){ uint32 i, off; dir->tdir_tag = tag; dir->tdir_type = (short) TIFF_SHORT; /* XXX -- yech, fool TIFFWriteData */ dir->tdir_count = (uint32) (1L<<tif->tif_dir.td_bitspersample); off = tif->tif_dataoff; for (i = 0; i < n; i++) if (!TIFFWriteData(tif, dir, (char *)table[i])) return (0); dir->tdir_count *= n; dir->tdir_offset = off; return (1);}/* * Write/copy data associated with an ASCII or opaque tag value. */static intTIFFWriteByteArray(TIFF* tif, TIFFDirEntry* dir, char* cp){ if (dir->tdir_count > 4) { if (!TIFFWriteData(tif, dir, cp)) return (0); } else _TIFFmemcpy(&dir->tdir_offset, cp, dir->tdir_count); return (1);}/* * Setup a directory entry of an array of SHORT * or SSHORT and write the associated indirect values. */static intTIFFWriteShortArray(TIFF* tif, TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, uint16* v){ dir->tdir_tag = tag; dir->tdir_type = (short) type; dir->tdir_count = n; if (n <= 2) { if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) { dir->tdir_offset = (uint32) ((long) v[0] << 16); if (n == 2) dir->tdir_offset |= v[1] & 0xffff; } else { dir->tdir_offset = v[0] & 0xffff; if (n == 2) dir->tdir_offset |= (long) v[1] << 16; } return (1); } else return (TIFFWriteData(tif, dir, (char*) v));}/* * Setup a directory entry of an array of LONG * or SLONG and write the associated indirect values. */static intTIFFWriteLongArray(TIFF* tif, TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, uint32* v){ dir->tdir_tag = tag; dir->tdir_type = (short) type; dir->tdir_count = n; if (n == 1) { dir->tdir_offset = v[0]; return (1); } else return (TIFFWriteData(tif, dir, (char*) v));}/* * Setup a directory entry of an array of RATIONAL * or SRATIONAL and write the associated indirect values. */static intTIFFWriteRationalArray(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)); 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) { 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);}static intTIFFWriteFloatArray(TIFF* tif, TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, float* v){ dir->tdir_tag = tag; dir->tdir_type = (short) type; dir->tdir_count = n; TIFFCvtNativeToIEEEFloat(tif, n, v); if (n == 1) { dir->tdir_offset = *(uint32*) &v[0]; return (1); } else return (TIFFWriteData(tif, dir, (char*) v));}static intTIFFWriteDoubleArray(TIFF* tif, TIFFDataType type, ttag_t tag, TIFFDirEntry* dir, uint32 n, double* v){ dir->tdir_tag = tag; dir->tdir_type = (short) type; dir->tdir_count = n; TIFFCvtNativeToIEEEDouble(tif, n, v); return (TIFFWriteData(tif, dir, (char*) v));}/* * 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 intTIFFWriteAnyArray(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; 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; 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; 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; 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; 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; 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; 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);}#ifdef COLORIMETRY_SUPPORTstatic intTIFFWriteTransferFunction(TIFF* tif, TIFFDirEntry* dir){ TIFFDirectory* td = &tif->tif_dir; tsize_t n = (1L<<td->td_bitspersample) * sizeof (uint16); uint16** tf = td->td_transferfunction; int ncols; /* * Check if the table can be written as a single column, * or if it must be written as 3 columns. Note that we * write a 3-column tag if there are 2 samples/pixel and * a single column of data won't suffice--hmm. */ switch (td->td_samplesperpixel - td->td_extrasamples) { default: if (_TIFFmemcmp(tf[0], tf[2], n)) { ncols = 3; break; } case 2: if (_TIFFmemcmp(tf[0], tf[1], n)) { ncols = 3; break; } case 1: case 0: ncols = 1; } return (TIFFWriteShortTable(tif, TIFFTAG_TRANSFERFUNCTION, dir, ncols, tf));}#endif/* * Write a contiguous directory item. */static intTIFFWriteData(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(dir->tdir_type); if (SeekOK(tif, dir->tdir_offset) && WriteOK(tif, cp, cc)) { tif->tif_dataoff += (cc + 1) & ~1; return (1); } TIFFError(tif->tif_name, "Error writing data for field \"%s\"", _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name); return (0);}/* * Link the current directory into the * directory chain for the file. */static intTIFFLinkDirectory(TIFF* tif){ static const char module[] = "TIFFLinkDirectory"; uint32 nextdir; uint32 diroff; tif->tif_diroff = (TIFFSeekFile(tif, (toff_t) 0, SEEK_END)+1) &~ 1; diroff = (uint32) tif->tif_diroff; if (tif->tif_flags & TIFF_SWAB) TIFFSwabLong(&diroff);#if SUBIFD_SUPPORT if (tif->tif_flags & TIFF_INSUBIFD) { (void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET); if (!WriteOK(tif, &diroff, sizeof (diroff))) { TIFFError(module, "%s: Error writing SubIFD directory link", tif->tif_name); return (0); } /* * Advance to the next SubIFD or, if this is * the last one configured, revert back to the * normal directory linkage. */ if (--tif->tif_nsubifd) tif->tif_subifdoff += sizeof (diroff); else tif->tif_flags &= ~TIFF_INSUBIFD; return (1); }#endif if (tif->tif_header.tiff_diroff == 0) { /* * First directory, overwrite offset in header. */ tif->tif_header.tiff_diroff = (uint32) tif->tif_diroff;#define HDROFF(f) ((toff_t) &(((TIFFHeader*) 0)->f)) (void) TIFFSeekFile(tif, HDROFF(tiff_diroff), SEEK_SET); if (!WriteOK(tif, &diroff, sizeof (diroff))) { TIFFError(tif->tif_name, "Error writing TIFF header"); return (0); } return (1); } /* * Not the first directory, search to the last and append. */ nextdir = tif->tif_header.tiff_diroff; do { uint16 dircount; if (!SeekOK(tif, nextdir) || !ReadOK(tif, &dircount, sizeof (dircount))) { TIFFError(module, "Error fetching directory count"); return (0); } if (tif->tif_flags & TIFF_SWAB) TIFFSwabShort(&dircount); (void) TIFFSeekFile(tif, dircount * sizeof (TIFFDirEntry), SEEK_CUR); if (!ReadOK(tif, &nextdir, sizeof (nextdir))) { TIFFError(module, "Error fetching directory link"); return (0); } if (tif->tif_flags & TIFF_SWAB) TIFFSwabLong(&nextdir); } while (nextdir != 0); (void) TIFFSeekFile(tif, -(toff_t) sizeof (nextdir), SEEK_CUR); if (!WriteOK(tif, &diroff, sizeof (diroff))) { TIFFError(module, "Error writing directory link"); return (0); } return (1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -