?? tif_dirwrite.c
字號(hào):
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, TIFFDirEntry* dir, uint16* v){ if (dir->tdir_count <= 2) { if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) { dir->tdir_offset = (uint32) ((long) v[0] << 16); if (dir->tdir_count == 2) dir->tdir_offset |= v[1] & 0xffff; } else { dir->tdir_offset = v[0] & 0xffff; if (dir->tdir_count == 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, TIFFDirEntry* dir, uint32* v){ if (dir->tdir_count == 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, TIFFDirEntry* dir, float* v){ uint32 i; uint32* t; int status; t = (uint32*) _TIFFmalloc(2 * dir->tdir_count * sizeof (uint32)); if (t == NULL) { TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space to write RATIONAL array"); return (0); } for (i = 0; i < dir->tdir_count; i++) { float fv = v[i]; int sign = 1; uint32 den; if (fv < 0) { if (dir->tdir_type == TIFF_RATIONAL) { TIFFWarningExt(tif->tif_clientdata, tif->tif_name, "\"%s\": Information lost writing value (%g) as (unsigned) RATIONAL", _TIFFFieldWithTag(tif,dir->tdir_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);}static intTIFFWriteFloatArray(TIFF* tif, TIFFDirEntry* dir, float* v){ TIFFCvtNativeToIEEEFloat(tif, dir->tdir_count, v); if (dir->tdir_count == 1) { dir->tdir_offset = *(uint32*) &v[0]; return (1); } else return (TIFFWriteData(tif, dir, (char*) v));}static intTIFFWriteDoubleArray(TIFF* tif, TIFFDirEntry* dir, double* v){ TIFFCvtNativeToIEEEDouble(tif, dir->tdir_count, 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)); 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: return (TIFFWriteDoubleArray(tif, dir, v)); default: /* TIFF_NOTYPE */ /* TIFF_ASCII */ /* TIFF_UNDEFINED */ /* TIFF_RATIONAL */ /* TIFF_SRATIONAL */ goto out; } status = 1; out: if (w != buf) _TIFFfree(w); return (status);}static 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));}static intTIFFWriteInkNames(TIFF* tif, TIFFDirEntry* dir){ TIFFDirectory* td = &tif->tif_dir; dir->tdir_tag = TIFFTAG_INKNAMES; dir->tdir_type = (short) TIFF_ASCII; dir->tdir_count = td->td_inknameslen; return (TIFFWriteByteArray(tif, dir, td->td_inknames));}/* * 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((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);}/* * Similar to TIFFWriteDirectory(), but if the directory has already * been written once, it is relocated to the end of the file, in case it * has changed in size. Note that this will result in the loss of the * previously used directory space. */ int TIFFRewriteDirectory( TIFF *tif ){ static const char module[] = "TIFFRewriteDirectory"; /* We don't need to do anything special if it hasn't been written. */ if( tif->tif_diroff == 0 ) return TIFFWriteDirectory( tif ); /* ** Find and zero the pointer to this directory, so that TIFFLinkDirectory ** will cause it to be added after this directories current pre-link. */ /* Is it the first directory in the file? */ if (tif->tif_header.tiff_diroff == tif->tif_diroff) { tif->tif_header.tiff_diroff = 0; tif->tif_diroff = 0; TIFFSeekFile(tif, (toff_t)(TIFF_MAGIC_SIZE+TIFF_VERSION_SIZE), SEEK_SET); if (!WriteOK(tif, &(tif->tif_header.tiff_diroff), sizeof (tif->tif_diroff))) { TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Error updating TIFF header"); return (0); } } else { toff_t nextdir, off; nextdir = tif->tif_header.tiff_diroff; do { uint16 dircount; if (!SeekOK(tif, nextdir) || !ReadOK(tif, &dircount, sizeof (dircount))) { TIFFErrorExt(tif->tif_clientdata, 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))) { TIFFErrorExt(tif->tif_clientdata, module, "Error fetching directory link"); return (0); } if (tif->tif_flags & TIFF_SWAB) TIFFSwabLong(&nextdir); } while (nextdir != tif->tif_diroff && nextdir != 0); off = TIFFSeekFile(tif, 0, SEEK_CUR); /* get current offset */ (void) TIFFSeekFile(tif, off - (toff_t)sizeof(nextdir), SEEK_SET); tif->tif_diroff = 0; if (!WriteOK(tif, &(tif->tif_diroff), sizeof (nextdir))) { TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link"); return (0); } } /* ** Now use TIFFWriteDirectory() normally. */ return TIFFWriteDirectory( tif );}/* * Link the current directory into the directory chain for the file. */static intTIFFLinkDirectory(TIFF* tif){ static const char module[] = "TIFFLinkDirectory"; toff_t nextdir; toff_t diroff, off; tif->tif_diroff = (TIFFSeekFile(tif, (toff_t) 0, SEEK_END)+1) &~ 1; diroff = tif->tif_diroff; if (tif->tif_flags & TIFF_SWAB) TIFFSwabLong(&diroff); /* * Handle SubIFDs */ if (tif->tif_flags & TIFF_INSUBIFD) { (void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET); if (!WriteOK(tif, &diroff, sizeof (diroff))) { TIFFErrorExt(tif->tif_clientdata, 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); } if (tif->tif_header.tiff_diroff == 0) { /* * First directory, overwrite offset in header. */ tif->tif_header.tiff_diroff = tif->tif_diroff; (void) TIFFSeekFile(tif, (toff_t)(TIFF_MAGIC_SIZE+TIFF_VERSION_SIZE), SEEK_SET); if (!WriteOK(tif, &diroff, sizeof (diroff))) { TIFFErrorExt(tif->tif_clientdata, 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))) { TIFFErrorExt(tif->tif_clientdata, 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))) { TIFFErrorExt(tif->tif_clientdata, module, "Error fetching directory link"); return (0); } if (tif->tif_flags & TIFF_SWAB) TIFFSwabLong(&nextdir); } while (nextdir != 0); off = TIFFSeekFile(tif, 0, SEEK_CUR); /* get current offset */ (void) TIFFSeekFile(tif, off - (toff_t)sizeof(nextdir), SEEK_SET); if (!WriteOK(tif, &diroff, sizeof (diroff))) { TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link"); return (0); } return (1);}/* vim: set ts=8 sts=8 sw=8 noet: */
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -