This source file includes following definitions.
- TIFFWriteScanline
- TIFFWriteEncodedStrip
- TIFFWriteRawStrip
- TIFFWriteTile
- TIFFWriteEncodedTile
- TIFFWriteRawTile
- TIFFSetupStrips
- TIFFWriteCheck
- TIFFWriteBufferSetup
- TIFFGrowStrips
- TIFFAppendToStrip
- TIFFFlushData1
- TIFFSetWriteOffset
#include "tiffiop.h"
#include <stdio.h>
#define STRIPINCR 20
#define WRITECHECKSTRIPS(tif, module) \
(((tif)->tif_flags&TIFF_BEENWRITING) || TIFFWriteCheck((tif),0,module))
#define WRITECHECKTILES(tif, module) \
(((tif)->tif_flags&TIFF_BEENWRITING) || TIFFWriteCheck((tif),1,module))
#define BUFFERCHECK(tif) \
((((tif)->tif_flags & TIFF_BUFFERSETUP) && tif->tif_rawdata) || \
TIFFWriteBufferSetup((tif), NULL, (tmsize_t) -1))
static int TIFFGrowStrips(TIFF* tif, uint32 delta, const char* module);
static int TIFFAppendToStrip(TIFF* tif, uint32 strip, uint8* data, tmsize_t cc);
int
TIFFWriteScanline(TIFF* tif, void* buf, uint32 row, uint16 sample)
{
static const char module[] = "TIFFWriteScanline";
register TIFFDirectory *td;
int status, imagegrew = 0;
uint32 strip;
if (!WRITECHECKSTRIPS(tif, module))
return (-1);
if (!BUFFERCHECK(tif))
return (-1);
tif->tif_flags |= TIFF_BUF4WRITE;
td = &tif->tif_dir;
if (row >= td->td_imagelength) {
if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
TIFFErrorExt(tif->tif_clientdata, module,
"Can not change \"ImageLength\" when using separate planes");
return (-1);
}
td->td_imagelength = row+1;
imagegrew = 1;
}
if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
if (sample >= td->td_samplesperpixel) {
TIFFErrorExt(tif->tif_clientdata, module,
"%lu: Sample out of range, max %lu",
(unsigned long) sample, (unsigned long) td->td_samplesperpixel);
return (-1);
}
strip = sample*td->td_stripsperimage + row/td->td_rowsperstrip;
} else
strip = row / td->td_rowsperstrip;
if (strip >= td->td_nstrips && !TIFFGrowStrips(tif, 1, module))
return (-1);
if (strip != tif->tif_curstrip) {
if (!TIFFFlushData(tif))
return (-1);
tif->tif_curstrip = strip;
if (strip >= td->td_stripsperimage && imagegrew)
td->td_stripsperimage =
TIFFhowmany_32(td->td_imagelength,td->td_rowsperstrip);
tif->tif_row =
(strip % td->td_stripsperimage) * td->td_rowsperstrip;
if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
if (!(*tif->tif_setupencode)(tif))
return (-1);
tif->tif_flags |= TIFF_CODERSETUP;
}
tif->tif_rawcc = 0;
tif->tif_rawcp = tif->tif_rawdata;
if( td->td_stripbytecount[strip] > 0 )
{
td->td_stripbytecount[strip] = 0;
tif->tif_curoff = 0;
}
if (!(*tif->tif_preencode)(tif, sample))
return (-1);
tif->tif_flags |= TIFF_POSTENCODE;
}
if (row != tif->tif_row) {
if (row < tif->tif_row) {
tif->tif_row = (strip % td->td_stripsperimage) *
td->td_rowsperstrip;
tif->tif_rawcp = tif->tif_rawdata;
}
if (!(*tif->tif_seek)(tif, row - tif->tif_row))
return (-1);
tif->tif_row = row;
}
tif->tif_postdecode( tif, (uint8*) buf, tif->tif_scanlinesize );
status = (*tif->tif_encoderow)(tif, (uint8*) buf,
tif->tif_scanlinesize, sample);
tif->tif_row = row + 1;
return (status);
}
tmsize_t
TIFFWriteEncodedStrip(TIFF* tif, uint32 strip, void* data, tmsize_t cc)
{
static const char module[] = "TIFFWriteEncodedStrip";
TIFFDirectory *td = &tif->tif_dir;
uint16 sample;
if (!WRITECHECKSTRIPS(tif, module))
return ((tmsize_t) -1);
if (strip >= td->td_nstrips) {
if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
TIFFErrorExt(tif->tif_clientdata, module,
"Can not grow image by strips when using separate planes");
return ((tmsize_t) -1);
}
if (!TIFFGrowStrips(tif, 1, module))
return ((tmsize_t) -1);
td->td_stripsperimage =
TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip);
}
if (!BUFFERCHECK(tif))
return ((tmsize_t) -1);
tif->tif_flags |= TIFF_BUF4WRITE;
tif->tif_curstrip = strip;
tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
if (!(*tif->tif_setupencode)(tif))
return ((tmsize_t) -1);
tif->tif_flags |= TIFF_CODERSETUP;
}
tif->tif_rawcc = 0;
tif->tif_rawcp = tif->tif_rawdata;
if( td->td_stripbytecount[strip] > 0 )
{
tif->tif_curoff = 0;
}
tif->tif_flags &= ~TIFF_POSTENCODE;
sample = (uint16)(strip / td->td_stripsperimage);
if (!(*tif->tif_preencode)(tif, sample))
return ((tmsize_t) -1);
tif->tif_postdecode( tif, (uint8*) data, cc );
if (!(*tif->tif_encodestrip)(tif, (uint8*) data, cc, sample))
return (0);
if (!(*tif->tif_postencode)(tif))
return ((tmsize_t) -1);
if (!isFillOrder(tif, td->td_fillorder) &&
(tif->tif_flags & TIFF_NOBITREV) == 0)
TIFFReverseBits(tif->tif_rawdata, tif->tif_rawcc);
if (tif->tif_rawcc > 0 &&
!TIFFAppendToStrip(tif, strip, tif->tif_rawdata, tif->tif_rawcc))
return ((tmsize_t) -1);
tif->tif_rawcc = 0;
tif->tif_rawcp = tif->tif_rawdata;
return (cc);
}
tmsize_t
TIFFWriteRawStrip(TIFF* tif, uint32 strip, void* data, tmsize_t cc)
{
static const char module[] = "TIFFWriteRawStrip";
TIFFDirectory *td = &tif->tif_dir;
if (!WRITECHECKSTRIPS(tif, module))
return ((tmsize_t) -1);
if (strip >= td->td_nstrips) {
if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
TIFFErrorExt(tif->tif_clientdata, module,
"Can not grow image by strips when using separate planes");
return ((tmsize_t) -1);
}
if (strip >= td->td_stripsperimage)
td->td_stripsperimage =
TIFFhowmany_32(td->td_imagelength,td->td_rowsperstrip);
if (!TIFFGrowStrips(tif, 1, module))
return ((tmsize_t) -1);
}
tif->tif_curstrip = strip;
tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
return (TIFFAppendToStrip(tif, strip, (uint8*) data, cc) ?
cc : (tmsize_t) -1);
}
tmsize_t
TIFFWriteTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
{
if (!TIFFCheckTile(tif, x, y, z, s))
return ((tmsize_t)(-1));
return (TIFFWriteEncodedTile(tif,
TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));
}
tmsize_t
TIFFWriteEncodedTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc)
{
static const char module[] = "TIFFWriteEncodedTile";
TIFFDirectory *td;
uint16 sample;
if (!WRITECHECKTILES(tif, module))
return ((tmsize_t)(-1));
td = &tif->tif_dir;
if (tile >= td->td_nstrips) {
TIFFErrorExt(tif->tif_clientdata, module, "Tile %lu out of range, max %lu",
(unsigned long) tile, (unsigned long) td->td_nstrips);
return ((tmsize_t)(-1));
}
if (!BUFFERCHECK(tif))
return ((tmsize_t)(-1));
tif->tif_flags |= TIFF_BUF4WRITE;
tif->tif_curtile = tile;
tif->tif_rawcc = 0;
tif->tif_rawcp = tif->tif_rawdata;
if( td->td_stripbytecount[tile] > 0 )
{
tif->tif_curoff = 0;
}
tif->tif_row = (tile % TIFFhowmany_32(td->td_imagelength, td->td_tilelength))
* td->td_tilelength;
tif->tif_col = (tile % TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth))
* td->td_tilewidth;
if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
if (!(*tif->tif_setupencode)(tif))
return ((tmsize_t)(-1));
tif->tif_flags |= TIFF_CODERSETUP;
}
tif->tif_flags &= ~TIFF_POSTENCODE;
sample = (uint16)(tile/td->td_stripsperimage);
if (!(*tif->tif_preencode)(tif, sample))
return ((tmsize_t)(-1));
if ( cc < 1 || cc > tif->tif_tilesize)
cc = tif->tif_tilesize;
tif->tif_postdecode( tif, (uint8*) data, cc );
if (!(*tif->tif_encodetile)(tif, (uint8*) data, cc, sample))
return (0);
if (!(*tif->tif_postencode)(tif))
return ((tmsize_t)(-1));
if (!isFillOrder(tif, td->td_fillorder) &&
(tif->tif_flags & TIFF_NOBITREV) == 0)
TIFFReverseBits((uint8*)tif->tif_rawdata, tif->tif_rawcc);
if (tif->tif_rawcc > 0 && !TIFFAppendToStrip(tif, tile,
tif->tif_rawdata, tif->tif_rawcc))
return ((tmsize_t)(-1));
tif->tif_rawcc = 0;
tif->tif_rawcp = tif->tif_rawdata;
return (cc);
}
tmsize_t
TIFFWriteRawTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc)
{
static const char module[] = "TIFFWriteRawTile";
if (!WRITECHECKTILES(tif, module))
return ((tmsize_t)(-1));
if (tile >= tif->tif_dir.td_nstrips) {
TIFFErrorExt(tif->tif_clientdata, module, "Tile %lu out of range, max %lu",
(unsigned long) tile,
(unsigned long) tif->tif_dir.td_nstrips);
return ((tmsize_t)(-1));
}
return (TIFFAppendToStrip(tif, tile, (uint8*) data, cc) ?
cc : (tmsize_t)(-1));
}
#define isUnspecified(tif, f) \
(TIFFFieldSet(tif,f) && (tif)->tif_dir.td_imagelength == 0)
int
TIFFSetupStrips(TIFF* tif)
{
TIFFDirectory* td = &tif->tif_dir;
if (isTiled(tif))
td->td_stripsperimage =
isUnspecified(tif, FIELD_TILEDIMENSIONS) ?
td->td_samplesperpixel : TIFFNumberOfTiles(tif);
else
td->td_stripsperimage =
isUnspecified(tif, FIELD_ROWSPERSTRIP) ?
td->td_samplesperpixel : TIFFNumberOfStrips(tif);
td->td_nstrips = td->td_stripsperimage;
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
td->td_stripsperimage /= td->td_samplesperpixel;
td->td_stripoffset = (uint64 *)
_TIFFmalloc(td->td_nstrips * sizeof (uint64));
td->td_stripbytecount = (uint64 *)
_TIFFmalloc(td->td_nstrips * sizeof (uint64));
if (td->td_stripoffset == NULL || td->td_stripbytecount == NULL)
return (0);
_TIFFmemset(td->td_stripoffset, 0, td->td_nstrips*sizeof (uint64));
_TIFFmemset(td->td_stripbytecount, 0, td->td_nstrips*sizeof (uint64));
TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
return (1);
}
#undef isUnspecified
int
TIFFWriteCheck(TIFF* tif, int tiles, const char* module)
{
if (tif->tif_mode == O_RDONLY) {
TIFFErrorExt(tif->tif_clientdata, module, "File not open for writing");
return (0);
}
if (tiles ^ isTiled(tif)) {
TIFFErrorExt(tif->tif_clientdata, module, tiles ?
"Can not write tiles to a stripped image" :
"Can not write scanlines to a tiled image");
return (0);
}
_TIFFFillStriles( tif );
if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {
TIFFErrorExt(tif->tif_clientdata, module,
"Must set \"ImageWidth\" before writing data");
return (0);
}
if (tif->tif_dir.td_samplesperpixel == 1) {
if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG))
tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
} else {
if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {
TIFFErrorExt(tif->tif_clientdata, module,
"Must set \"PlanarConfiguration\" before writing data");
return (0);
}
}
if (tif->tif_dir.td_stripoffset == NULL && !TIFFSetupStrips(tif)) {
tif->tif_dir.td_nstrips = 0;
TIFFErrorExt(tif->tif_clientdata, module, "No space for %s arrays",
isTiled(tif) ? "tile" : "strip");
return (0);
}
if (isTiled(tif))
{
tif->tif_tilesize = TIFFTileSize(tif);
if (tif->tif_tilesize == 0)
return (0);
}
else
tif->tif_tilesize = (tmsize_t)(-1);
tif->tif_scanlinesize = TIFFScanlineSize(tif);
if (tif->tif_scanlinesize == 0)
return (0);
tif->tif_flags |= TIFF_BEENWRITING;
return (1);
}
int
TIFFWriteBufferSetup(TIFF* tif, void* bp, tmsize_t size)
{
static const char module[] = "TIFFWriteBufferSetup";
if (tif->tif_rawdata) {
if (tif->tif_flags & TIFF_MYBUFFER) {
_TIFFfree(tif->tif_rawdata);
tif->tif_flags &= ~TIFF_MYBUFFER;
}
tif->tif_rawdata = NULL;
}
if (size == (tmsize_t)(-1)) {
size = (isTiled(tif) ?
tif->tif_tilesize : TIFFStripSize(tif));
if (size < 8*1024)
size = 8*1024;
bp = NULL;
}
if (bp == NULL) {
bp = _TIFFmalloc(size);
if (bp == NULL) {
TIFFErrorExt(tif->tif_clientdata, module, "No space for output buffer");
return (0);
}
tif->tif_flags |= TIFF_MYBUFFER;
} else
tif->tif_flags &= ~TIFF_MYBUFFER;
tif->tif_rawdata = (uint8*) bp;
tif->tif_rawdatasize = size;
tif->tif_rawcc = 0;
tif->tif_rawcp = tif->tif_rawdata;
tif->tif_flags |= TIFF_BUFFERSETUP;
return (1);
}
static int
TIFFGrowStrips(TIFF* tif, uint32 delta, const char* module)
{
TIFFDirectory *td = &tif->tif_dir;
uint64* new_stripoffset;
uint64* new_stripbytecount;
assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
new_stripoffset = (uint64*)_TIFFrealloc(td->td_stripoffset,
(td->td_nstrips + delta) * sizeof (uint64));
new_stripbytecount = (uint64*)_TIFFrealloc(td->td_stripbytecount,
(td->td_nstrips + delta) * sizeof (uint64));
if (new_stripoffset == NULL || new_stripbytecount == NULL) {
if (new_stripoffset)
_TIFFfree(new_stripoffset);
if (new_stripbytecount)
_TIFFfree(new_stripbytecount);
td->td_nstrips = 0;
TIFFErrorExt(tif->tif_clientdata, module, "No space to expand strip arrays");
return (0);
}
td->td_stripoffset = new_stripoffset;
td->td_stripbytecount = new_stripbytecount;
_TIFFmemset(td->td_stripoffset + td->td_nstrips,
0, delta*sizeof (uint64));
_TIFFmemset(td->td_stripbytecount + td->td_nstrips,
0, delta*sizeof (uint64));
td->td_nstrips += delta;
tif->tif_flags |= TIFF_DIRTYDIRECT;
return (1);
}
static int
TIFFAppendToStrip(TIFF* tif, uint32 strip, uint8* data, tmsize_t cc)
{
static const char module[] = "TIFFAppendToStrip";
TIFFDirectory *td = &tif->tif_dir;
uint64 m;
int64 old_byte_count = -1;
if (td->td_stripoffset[strip] == 0 || tif->tif_curoff == 0) {
assert(td->td_nstrips > 0);
if( td->td_stripbytecount[strip] != 0
&& td->td_stripoffset[strip] != 0
&& td->td_stripbytecount[strip] >= (uint64) cc )
{
if (!SeekOK(tif, td->td_stripoffset[strip])) {
TIFFErrorExt(tif->tif_clientdata, module,
"Seek error at scanline %lu",
(unsigned long)tif->tif_row);
return (0);
}
}
else
{
td->td_stripoffset[strip] = TIFFSeekFile(tif, 0, SEEK_END);
tif->tif_flags |= TIFF_DIRTYSTRIP;
}
tif->tif_curoff = td->td_stripoffset[strip];
old_byte_count = td->td_stripbytecount[strip];
td->td_stripbytecount[strip] = 0;
}
m = tif->tif_curoff+cc;
if (!(tif->tif_flags&TIFF_BIGTIFF))
m = (uint32)m;
if ((m<tif->tif_curoff)||(m<(uint64)cc))
{
TIFFErrorExt(tif->tif_clientdata, module, "Maximum TIFF file size exceeded");
return (0);
}
if (!WriteOK(tif, data, cc)) {
TIFFErrorExt(tif->tif_clientdata, module, "Write error at scanline %lu",
(unsigned long) tif->tif_row);
return (0);
}
tif->tif_curoff = m;
td->td_stripbytecount[strip] += cc;
if( (int64) td->td_stripbytecount[strip] != old_byte_count )
tif->tif_flags |= TIFF_DIRTYSTRIP;
return (1);
}
int
TIFFFlushData1(TIFF* tif)
{
if (tif->tif_rawcc > 0 && tif->tif_flags & TIFF_BUF4WRITE ) {
if (!isFillOrder(tif, tif->tif_dir.td_fillorder) &&
(tif->tif_flags & TIFF_NOBITREV) == 0)
TIFFReverseBits((uint8*)tif->tif_rawdata,
tif->tif_rawcc);
if (!TIFFAppendToStrip(tif,
isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip,
tif->tif_rawdata, tif->tif_rawcc))
return (0);
tif->tif_rawcc = 0;
tif->tif_rawcp = tif->tif_rawdata;
}
return (1);
}
void
TIFFSetWriteOffset(TIFF* tif, toff_t off)
{
tif->tif_curoff = off;
}