roentgen b75cab
/* $Id: tif_write.c,v 1.37 2012-08-13 22:10:17 fwarmerdam Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1988-1997 Sam Leffler
roentgen b75cab
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
roentgen b75cab
 *
roentgen b75cab
 * Permission to use, copy, modify, distribute, and sell this software and
roentgen b75cab
 * its documentation for any purpose is hereby granted without fee, provided
roentgen b75cab
 * that (i) the above copyright notices and this permission notice appear in
roentgen b75cab
 * all copies of the software and related documentation, and (ii) the names of
roentgen b75cab
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
roentgen b75cab
 * publicity relating to the software without the specific, prior written
roentgen b75cab
 * permission of Sam Leffler and Silicon Graphics.
roentgen b75cab
 *
roentgen b75cab
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
roentgen b75cab
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
roentgen b75cab
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
roentgen b75cab
 *
roentgen b75cab
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
roentgen b75cab
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
roentgen b75cab
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
roentgen b75cab
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
roentgen b75cab
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
roentgen b75cab
 * OF THIS SOFTWARE.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * TIFF Library.
roentgen b75cab
 *
roentgen b75cab
 * Scanline-oriented Write Support
roentgen b75cab
 */
roentgen b75cab
#include "tiffiop.h"
roentgen b75cab
#include <stdio.h></stdio.h>
roentgen b75cab
roentgen b75cab
#define STRIPINCR	20		/* expansion factor on strip array */
roentgen b75cab
roentgen b75cab
#define WRITECHECKSTRIPS(tif, module)				\
roentgen b75cab
	(((tif)->tif_flags&TIFF_BEENWRITING) || TIFFWriteCheck((tif),0,module))
roentgen b75cab
#define WRITECHECKTILES(tif, module)				\
roentgen b75cab
	(((tif)->tif_flags&TIFF_BEENWRITING) || TIFFWriteCheck((tif),1,module))
roentgen b75cab
#define BUFFERCHECK(tif)					\
roentgen b75cab
	((((tif)->tif_flags & TIFF_BUFFERSETUP) && tif->tif_rawdata) ||	\
roentgen b75cab
	    TIFFWriteBufferSetup((tif), NULL, (tmsize_t) -1))
roentgen b75cab
roentgen b75cab
static int TIFFGrowStrips(TIFF* tif, uint32 delta, const char* module);
roentgen b75cab
static int TIFFAppendToStrip(TIFF* tif, uint32 strip, uint8* data, tmsize_t cc);
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TIFFWriteScanline(TIFF* tif, void* buf, uint32 row, uint16 sample)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteScanline";
roentgen b75cab
	register TIFFDirectory *td;
roentgen b75cab
	int status, imagegrew = 0;
roentgen b75cab
	uint32 strip;
roentgen b75cab
roentgen b75cab
	if (!WRITECHECKSTRIPS(tif, module))
roentgen b75cab
		return (-1);
roentgen b75cab
	/*
roentgen b75cab
	 * Handle delayed allocation of data buffer.  This
roentgen b75cab
	 * permits it to be sized more intelligently (using
roentgen b75cab
	 * directory information).
roentgen b75cab
	 */
roentgen b75cab
	if (!BUFFERCHECK(tif))
roentgen b75cab
		return (-1);
roentgen b75cab
        tif->tif_flags |= TIFF_BUF4WRITE; /* not strictly sure this is right*/
roentgen b75cab
roentgen b75cab
	td = &tif->tif_dir;
roentgen b75cab
	/*
roentgen b75cab
	 * Extend image length if needed
roentgen b75cab
	 * (but only for PlanarConfig=1).
roentgen b75cab
	 */
roentgen b75cab
	if (row >= td->td_imagelength) {	/* extend image */
roentgen b75cab
		if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			    "Can not change \"ImageLength\" when using separate planes");
roentgen b75cab
			return (-1);
roentgen b75cab
		}
roentgen b75cab
		td->td_imagelength = row+1;
roentgen b75cab
		imagegrew = 1;
roentgen b75cab
	}
roentgen b75cab
	/*
roentgen b75cab
	 * Calculate strip and check for crossings.
roentgen b75cab
	 */
roentgen b75cab
	if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
roentgen b75cab
		if (sample >= td->td_samplesperpixel) {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			    "%lu: Sample out of range, max %lu",
roentgen b75cab
			    (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
roentgen b75cab
			return (-1);
roentgen b75cab
		}
roentgen b75cab
		strip = sample*td->td_stripsperimage + row/td->td_rowsperstrip;
roentgen b75cab
	} else
roentgen b75cab
		strip = row / td->td_rowsperstrip;
roentgen b75cab
	/*
roentgen b75cab
	 * Check strip array to make sure there's space. We don't support
roentgen b75cab
	 * dynamically growing files that have data organized in separate
roentgen b75cab
	 * bitplanes because it's too painful.  In that case we require that
roentgen b75cab
	 * the imagelength be set properly before the first write (so that the
roentgen b75cab
	 * strips array will be fully allocated above).
roentgen b75cab
	 */
roentgen b75cab
	if (strip >= td->td_nstrips && !TIFFGrowStrips(tif, 1, module))
roentgen b75cab
		return (-1);
roentgen b75cab
	if (strip != tif->tif_curstrip) {
roentgen b75cab
		/*
roentgen b75cab
		 * Changing strips -- flush any data present.
roentgen b75cab
		 */
roentgen b75cab
		if (!TIFFFlushData(tif))
roentgen b75cab
			return (-1);
roentgen b75cab
		tif->tif_curstrip = strip;
roentgen b75cab
		/*
roentgen b75cab
		 * Watch out for a growing image.  The value of strips/image
roentgen b75cab
		 * will initially be 1 (since it can't be deduced until the
roentgen b75cab
		 * imagelength is known).
roentgen b75cab
		 */
roentgen b75cab
		if (strip >= td->td_stripsperimage && imagegrew)
roentgen b75cab
			td->td_stripsperimage =
roentgen b75cab
			    TIFFhowmany_32(td->td_imagelength,td->td_rowsperstrip);
roentgen b75cab
		tif->tif_row =
roentgen b75cab
		    (strip % td->td_stripsperimage) * td->td_rowsperstrip;
roentgen b75cab
		if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
roentgen b75cab
			if (!(*tif->tif_setupencode)(tif))
roentgen b75cab
				return (-1);
roentgen b75cab
			tif->tif_flags |= TIFF_CODERSETUP;
roentgen b75cab
		}
roentgen b75cab
        
roentgen b75cab
		tif->tif_rawcc = 0;
roentgen b75cab
		tif->tif_rawcp = tif->tif_rawdata;
roentgen b75cab
roentgen b75cab
		if( td->td_stripbytecount[strip] > 0 )
roentgen b75cab
		{
roentgen b75cab
			/* if we are writing over existing tiles, zero length */
roentgen b75cab
			td->td_stripbytecount[strip] = 0;
roentgen b75cab
roentgen b75cab
			/* this forces TIFFAppendToStrip() to do a seek */
roentgen b75cab
			tif->tif_curoff = 0;
roentgen b75cab
		}
roentgen b75cab
roentgen b75cab
		if (!(*tif->tif_preencode)(tif, sample))
roentgen b75cab
			return (-1);
roentgen b75cab
		tif->tif_flags |= TIFF_POSTENCODE;
roentgen b75cab
	}
roentgen b75cab
	/*
roentgen b75cab
	 * Ensure the write is either sequential or at the
roentgen b75cab
	 * beginning of a strip (or that we can randomly
roentgen b75cab
	 * access the data -- i.e. no encoding).
roentgen b75cab
	 */
roentgen b75cab
	if (row != tif->tif_row) {
roentgen b75cab
		if (row < tif->tif_row) {
roentgen b75cab
			/*
roentgen b75cab
			 * Moving backwards within the same strip:
roentgen b75cab
			 * backup to the start and then decode
roentgen b75cab
			 * forward (below).
roentgen b75cab
			 */
roentgen b75cab
			tif->tif_row = (strip % td->td_stripsperimage) *
roentgen b75cab
			    td->td_rowsperstrip;
roentgen b75cab
			tif->tif_rawcp = tif->tif_rawdata;
roentgen b75cab
		}
roentgen b75cab
		/*
roentgen b75cab
		 * Seek forward to the desired row.
roentgen b75cab
		 */
roentgen b75cab
		if (!(*tif->tif_seek)(tif, row - tif->tif_row))
roentgen b75cab
			return (-1);
roentgen b75cab
		tif->tif_row = row;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	/* swab if needed - note that source buffer will be altered */
roentgen b75cab
	tif->tif_postdecode( tif, (uint8*) buf, tif->tif_scanlinesize );
roentgen b75cab
roentgen b75cab
	status = (*tif->tif_encoderow)(tif, (uint8*) buf,
roentgen b75cab
	    tif->tif_scanlinesize, sample);
roentgen b75cab
roentgen b75cab
        /* we are now poised at the beginning of the next row */
roentgen b75cab
	tif->tif_row = row + 1;
roentgen b75cab
	return (status);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Encode the supplied data and write it to the
roentgen b75cab
 * specified strip.
roentgen b75cab
 *
roentgen b75cab
 * NB: Image length must be setup before writing.
roentgen b75cab
 */
roentgen b75cab
tmsize_t
roentgen b75cab
TIFFWriteEncodedStrip(TIFF* tif, uint32 strip, void* data, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteEncodedStrip";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	uint16 sample;
roentgen b75cab
roentgen b75cab
	if (!WRITECHECKSTRIPS(tif, module))
roentgen b75cab
		return ((tmsize_t) -1);
roentgen b75cab
	/*
roentgen b75cab
	 * Check strip array to make sure there's space.
roentgen b75cab
	 * We don't support dynamically growing files that
roentgen b75cab
	 * have data organized in separate bitplanes because
roentgen b75cab
	 * it's too painful.  In that case we require that
roentgen b75cab
	 * the imagelength be set properly before the first
roentgen b75cab
	 * write (so that the strips array will be fully
roentgen b75cab
	 * allocated above).
roentgen b75cab
	 */
roentgen b75cab
	if (strip >= td->td_nstrips) {
roentgen b75cab
		if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			    "Can not grow image by strips when using separate planes");
roentgen b75cab
			return ((tmsize_t) -1);
roentgen b75cab
		}
roentgen b75cab
		if (!TIFFGrowStrips(tif, 1, module))
roentgen b75cab
			return ((tmsize_t) -1);
roentgen b75cab
		td->td_stripsperimage =
roentgen b75cab
		    TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip);  
roentgen b75cab
	}
roentgen b75cab
	/*
roentgen b75cab
	 * Handle delayed allocation of data buffer.  This
roentgen b75cab
	 * permits it to be sized according to the directory
roentgen b75cab
	 * info.
roentgen b75cab
	 */
roentgen b75cab
	if (!BUFFERCHECK(tif))
roentgen b75cab
		return ((tmsize_t) -1);
roentgen b75cab
roentgen b75cab
        tif->tif_flags |= TIFF_BUF4WRITE;
roentgen b75cab
	tif->tif_curstrip = strip;
roentgen b75cab
roentgen b75cab
	tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
roentgen b75cab
	if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
roentgen b75cab
		if (!(*tif->tif_setupencode)(tif))
roentgen b75cab
			return ((tmsize_t) -1);
roentgen b75cab
		tif->tif_flags |= TIFF_CODERSETUP;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	if( td->td_stripbytecount[strip] > 0 )
roentgen b75cab
        {
roentgen b75cab
            /* Make sure that at the first attempt of rewriting the tile, we will have */
roentgen b75cab
            /* more bytes available in the output buffer than the previous byte count, */
roentgen b75cab
            /* so that TIFFAppendToStrip() will detect the overflow when it is called the first */
roentgen b75cab
            /* time if the new compressed tile is bigger than the older one. (GDAL #4771) */
roentgen b75cab
            if( tif->tif_rawdatasize <= td->td_stripbytecount[strip] )
roentgen b75cab
            {
roentgen b75cab
                if( !(TIFFWriteBufferSetup(tif, NULL,
roentgen b75cab
                    (tmsize_t)TIFFroundup_64((uint64)(td->td_stripbytecount[strip] + 1), 1024))) )
roentgen b75cab
                    return ((tmsize_t)(-1));
roentgen b75cab
            }
roentgen b75cab
roentgen b75cab
	    /* Force TIFFAppendToStrip() to consider placing data at end
roentgen b75cab
               of file. */
roentgen b75cab
            tif->tif_curoff = 0;
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
    tif->tif_rawcc = 0;
roentgen b75cab
    tif->tif_rawcp = tif->tif_rawdata;
roentgen b75cab
roentgen b75cab
	tif->tif_flags &= ~TIFF_POSTENCODE;
roentgen b75cab
	sample = (uint16)(strip / td->td_stripsperimage);
roentgen b75cab
	if (!(*tif->tif_preencode)(tif, sample))
roentgen b75cab
		return ((tmsize_t) -1);
roentgen b75cab
roentgen b75cab
        /* swab if needed - note that source buffer will be altered */
roentgen b75cab
	tif->tif_postdecode( tif, (uint8*) data, cc );
roentgen b75cab
roentgen b75cab
	if (!(*tif->tif_encodestrip)(tif, (uint8*) data, cc, sample))
roentgen b75cab
		return (0);
roentgen b75cab
	if (!(*tif->tif_postencode)(tif))
roentgen b75cab
		return ((tmsize_t) -1);
roentgen b75cab
	if (!isFillOrder(tif, td->td_fillorder) &&
roentgen b75cab
	    (tif->tif_flags & TIFF_NOBITREV) == 0)
roentgen b75cab
		TIFFReverseBits(tif->tif_rawdata, tif->tif_rawcc);
roentgen b75cab
	if (tif->tif_rawcc > 0 &&
roentgen b75cab
	    !TIFFAppendToStrip(tif, strip, tif->tif_rawdata, tif->tif_rawcc))
roentgen b75cab
		return ((tmsize_t) -1);
roentgen b75cab
	tif->tif_rawcc = 0;
roentgen b75cab
	tif->tif_rawcp = tif->tif_rawdata;
roentgen b75cab
	return (cc);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Write the supplied data to the specified strip.
roentgen b75cab
 *
roentgen b75cab
 * NB: Image length must be setup before writing.
roentgen b75cab
 */
roentgen b75cab
tmsize_t
roentgen b75cab
TIFFWriteRawStrip(TIFF* tif, uint32 strip, void* data, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteRawStrip";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
	if (!WRITECHECKSTRIPS(tif, module))
roentgen b75cab
		return ((tmsize_t) -1);
roentgen b75cab
	/*
roentgen b75cab
	 * Check strip array to make sure there's space.
roentgen b75cab
	 * We don't support dynamically growing files that
roentgen b75cab
	 * have data organized in separate bitplanes because
roentgen b75cab
	 * it's too painful.  In that case we require that
roentgen b75cab
	 * the imagelength be set properly before the first
roentgen b75cab
	 * write (so that the strips array will be fully
roentgen b75cab
	 * allocated above).
roentgen b75cab
	 */
roentgen b75cab
	if (strip >= td->td_nstrips) {
roentgen b75cab
		if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			    "Can not grow image by strips when using separate planes");
roentgen b75cab
			return ((tmsize_t) -1);
roentgen b75cab
		}
roentgen b75cab
		/*
roentgen b75cab
		 * Watch out for a growing image.  The value of
roentgen b75cab
		 * strips/image will initially be 1 (since it
roentgen b75cab
		 * can't be deduced until the imagelength is known).
roentgen b75cab
		 */
roentgen b75cab
		if (strip >= td->td_stripsperimage)
roentgen b75cab
			td->td_stripsperimage =
roentgen b75cab
			    TIFFhowmany_32(td->td_imagelength,td->td_rowsperstrip);
roentgen b75cab
		if (!TIFFGrowStrips(tif, 1, module))
roentgen b75cab
			return ((tmsize_t) -1);
roentgen b75cab
	}
roentgen b75cab
	tif->tif_curstrip = strip;
roentgen b75cab
	tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
roentgen b75cab
	return (TIFFAppendToStrip(tif, strip, (uint8*) data, cc) ?
roentgen b75cab
	    cc : (tmsize_t) -1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Write and compress a tile of data.  The
roentgen b75cab
 * tile is selected by the (x,y,z,s) coordinates.
roentgen b75cab
 */
roentgen b75cab
tmsize_t
roentgen b75cab
TIFFWriteTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
roentgen b75cab
{
roentgen b75cab
	if (!TIFFCheckTile(tif, x, y, z, s))
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	/*
roentgen b75cab
	 * NB: A tile size of -1 is used instead of tif_tilesize knowing
roentgen b75cab
	 *     that TIFFWriteEncodedTile will clamp this to the tile size.
roentgen b75cab
	 *     This is done because the tile size may not be defined until
roentgen b75cab
	 *     after the output buffer is setup in TIFFWriteBufferSetup.
roentgen b75cab
	 */
roentgen b75cab
	return (TIFFWriteEncodedTile(tif,
roentgen b75cab
	    TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Encode the supplied data and write it to the
roentgen b75cab
 * specified tile.  There must be space for the
roentgen b75cab
 * data.  The function clamps individual writes
roentgen b75cab
 * to a tile to the tile size, but does not (and
roentgen b75cab
 * can not) check that multiple writes to the same
roentgen b75cab
 * tile do not write more than tile size data.
roentgen b75cab
 *
roentgen b75cab
 * NB: Image length must be setup before writing; this
roentgen b75cab
 *     interface does not support automatically growing
roentgen b75cab
 *     the image on each write (as TIFFWriteScanline does).
roentgen b75cab
 */
roentgen b75cab
tmsize_t
roentgen b75cab
TIFFWriteEncodedTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteEncodedTile";
roentgen b75cab
	TIFFDirectory *td;
roentgen b75cab
	uint16 sample;
roentgen b75cab
roentgen b75cab
	if (!WRITECHECKTILES(tif, module))
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	td = &tif->tif_dir;
roentgen b75cab
	if (tile >= td->td_nstrips) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "Tile %lu out of range, max %lu",
roentgen b75cab
		    (unsigned long) tile, (unsigned long) td->td_nstrips);
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	}
roentgen b75cab
	/*
roentgen b75cab
	 * Handle delayed allocation of data buffer.  This
roentgen b75cab
	 * permits it to be sized more intelligently (using
roentgen b75cab
	 * directory information).
roentgen b75cab
	 */
roentgen b75cab
	if (!BUFFERCHECK(tif))
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
roentgen b75cab
        tif->tif_flags |= TIFF_BUF4WRITE;
roentgen b75cab
	tif->tif_curtile = tile;
roentgen b75cab
roentgen b75cab
	if( td->td_stripbytecount[tile] > 0 )
roentgen b75cab
        {
roentgen b75cab
            /* Make sure that at the first attempt of rewriting the tile, we will have */
roentgen b75cab
            /* more bytes available in the output buffer than the previous byte count, */
roentgen b75cab
            /* so that TIFFAppendToStrip() will detect the overflow when it is called the first */
roentgen b75cab
            /* time if the new compressed tile is bigger than the older one. (GDAL #4771) */
roentgen b75cab
            if( tif->tif_rawdatasize <= td->td_stripbytecount[tile] )
roentgen b75cab
            {
roentgen b75cab
                if( !(TIFFWriteBufferSetup(tif, NULL,
roentgen b75cab
                    (tmsize_t)TIFFroundup_64((uint64)(td->td_stripbytecount[tile] + 1), 1024))) )
roentgen b75cab
                    return ((tmsize_t)(-1));
roentgen b75cab
            }
roentgen b75cab
roentgen b75cab
	    /* Force TIFFAppendToStrip() to consider placing data at end
roentgen b75cab
               of file. */
roentgen b75cab
            tif->tif_curoff = 0;
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
    tif->tif_rawcc = 0;
roentgen b75cab
    tif->tif_rawcp = tif->tif_rawdata;
roentgen b75cab
roentgen b75cab
	/* 
roentgen b75cab
	 * Compute tiles per row & per column to compute
roentgen b75cab
	 * current row and column
roentgen b75cab
	 */
roentgen b75cab
	tif->tif_row = (tile % TIFFhowmany_32(td->td_imagelength, td->td_tilelength))
roentgen b75cab
		* td->td_tilelength;
roentgen b75cab
	tif->tif_col = (tile % TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth))
roentgen b75cab
		* td->td_tilewidth;
roentgen b75cab
roentgen b75cab
	if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
roentgen b75cab
		if (!(*tif->tif_setupencode)(tif))
roentgen b75cab
			return ((tmsize_t)(-1));
roentgen b75cab
		tif->tif_flags |= TIFF_CODERSETUP;
roentgen b75cab
	}
roentgen b75cab
	tif->tif_flags &= ~TIFF_POSTENCODE;
roentgen b75cab
	sample = (uint16)(tile/td->td_stripsperimage);
roentgen b75cab
	if (!(*tif->tif_preencode)(tif, sample))
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	/*
roentgen b75cab
	 * Clamp write amount to the tile size.  This is mostly
roentgen b75cab
	 * done so that callers can pass in some large number
roentgen b75cab
	 * (e.g. -1) and have the tile size used instead.
roentgen b75cab
	 */
roentgen b75cab
	if ( cc < 1 || cc > tif->tif_tilesize)
roentgen b75cab
		cc = tif->tif_tilesize;
roentgen b75cab
roentgen b75cab
        /* swab if needed - note that source buffer will be altered */
roentgen b75cab
	tif->tif_postdecode( tif, (uint8*) data, cc );
roentgen b75cab
roentgen b75cab
	if (!(*tif->tif_encodetile)(tif, (uint8*) data, cc, sample))
roentgen b75cab
		return (0);
roentgen b75cab
	if (!(*tif->tif_postencode)(tif))
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	if (!isFillOrder(tif, td->td_fillorder) &&
roentgen b75cab
	    (tif->tif_flags & TIFF_NOBITREV) == 0)
roentgen b75cab
		TIFFReverseBits((uint8*)tif->tif_rawdata, tif->tif_rawcc);
roentgen b75cab
	if (tif->tif_rawcc > 0 && !TIFFAppendToStrip(tif, tile,
roentgen b75cab
	    tif->tif_rawdata, tif->tif_rawcc))
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	tif->tif_rawcc = 0;
roentgen b75cab
	tif->tif_rawcp = tif->tif_rawdata;
roentgen b75cab
	return (cc);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Write the supplied data to the specified strip.
roentgen b75cab
 * There must be space for the data; we don't check
roentgen b75cab
 * if strips overlap!
roentgen b75cab
 *
roentgen b75cab
 * NB: Image length must be setup before writing; this
roentgen b75cab
 *     interface does not support automatically growing
roentgen b75cab
 *     the image on each write (as TIFFWriteScanline does).
roentgen b75cab
 */
roentgen b75cab
tmsize_t
roentgen b75cab
TIFFWriteRawTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteRawTile";
roentgen b75cab
roentgen b75cab
	if (!WRITECHECKTILES(tif, module))
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	if (tile >= tif->tif_dir.td_nstrips) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "Tile %lu out of range, max %lu",
roentgen b75cab
		    (unsigned long) tile,
roentgen b75cab
		    (unsigned long) tif->tif_dir.td_nstrips);
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	}
roentgen b75cab
	return (TIFFAppendToStrip(tif, tile, (uint8*) data, cc) ?
roentgen b75cab
	    cc : (tmsize_t)(-1));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#define	isUnspecified(tif, f) \
roentgen b75cab
    (TIFFFieldSet(tif,f) && (tif)->tif_dir.td_imagelength == 0)
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TIFFSetupStrips(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	TIFFDirectory* td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
	if (isTiled(tif))
roentgen b75cab
		td->td_stripsperimage =
roentgen b75cab
		    isUnspecified(tif, FIELD_TILEDIMENSIONS) ?
roentgen b75cab
			td->td_samplesperpixel : TIFFNumberOfTiles(tif);
roentgen b75cab
	else
roentgen b75cab
		td->td_stripsperimage =
roentgen b75cab
		    isUnspecified(tif, FIELD_ROWSPERSTRIP) ?
roentgen b75cab
			td->td_samplesperpixel : TIFFNumberOfStrips(tif);
roentgen b75cab
	td->td_nstrips = td->td_stripsperimage;
roentgen b75cab
	if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
roentgen b75cab
		td->td_stripsperimage /= td->td_samplesperpixel;
roentgen b75cab
	td->td_stripoffset = (uint64 *)
roentgen b75cab
	    _TIFFmalloc(td->td_nstrips * sizeof (uint64));
roentgen b75cab
	td->td_stripbytecount = (uint64 *)
roentgen b75cab
	    _TIFFmalloc(td->td_nstrips * sizeof (uint64));
roentgen b75cab
	if (td->td_stripoffset == NULL || td->td_stripbytecount == NULL)
roentgen b75cab
		return (0);
roentgen b75cab
	/*
roentgen b75cab
	 * Place data at the end-of-file
roentgen b75cab
	 * (by setting offsets to zero).
roentgen b75cab
	 */
roentgen b75cab
	_TIFFmemset(td->td_stripoffset, 0, td->td_nstrips*sizeof (uint64));
roentgen b75cab
	_TIFFmemset(td->td_stripbytecount, 0, td->td_nstrips*sizeof (uint64));
roentgen b75cab
	TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
roentgen b75cab
	TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
#undef isUnspecified
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Verify file is writable and that the directory
roentgen b75cab
 * information is setup properly.  In doing the latter
roentgen b75cab
 * we also "freeze" the state of the directory so
roentgen b75cab
 * that important information is not changed.
roentgen b75cab
 */
roentgen b75cab
int
roentgen b75cab
TIFFWriteCheck(TIFF* tif, int tiles, const char* module)
roentgen b75cab
{
roentgen b75cab
	if (tif->tif_mode == O_RDONLY) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "File not open for writing");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	if (tiles ^ isTiled(tif)) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, tiles ?
roentgen b75cab
		    "Can not write tiles to a stripped image" :
roentgen b75cab
		    "Can not write scanlines to a tiled image");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
        _TIFFFillStriles( tif );
roentgen b75cab
        
roentgen b75cab
	/*
roentgen b75cab
	 * On the first write verify all the required information
roentgen b75cab
	 * has been setup and initialize any data structures that
roentgen b75cab
	 * had to wait until directory information was set.
roentgen b75cab
	 * Note that a lot of our work is assumed to remain valid
roentgen b75cab
	 * because we disallow any of the important parameters
roentgen b75cab
	 * from changing after we start writing (i.e. once
roentgen b75cab
	 * TIFF_BEENWRITING is set, TIFFSetField will only allow
roentgen b75cab
	 * the image's length to be changed).
roentgen b75cab
	 */
roentgen b75cab
	if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		    "Must set \"ImageWidth\" before writing data");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	if (tif->tif_dir.td_samplesperpixel == 1) {
roentgen b75cab
		/* 
roentgen b75cab
		 * Planarconfiguration is irrelevant in case of single band
roentgen b75cab
		 * images and need not be included. We will set it anyway,
roentgen b75cab
		 * because this field is used in other parts of library even
roentgen b75cab
		 * in the single band case.
roentgen b75cab
		 */
roentgen b75cab
		if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG))
roentgen b75cab
                    tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
roentgen b75cab
	} else {
roentgen b75cab
		if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			    "Must set \"PlanarConfiguration\" before writing data");
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	if (tif->tif_dir.td_stripoffset == NULL && !TIFFSetupStrips(tif)) {
roentgen b75cab
		tif->tif_dir.td_nstrips = 0;
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "No space for %s arrays",
roentgen b75cab
		    isTiled(tif) ? "tile" : "strip");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	if (isTiled(tif))
roentgen b75cab
	{
roentgen b75cab
		tif->tif_tilesize = TIFFTileSize(tif);
roentgen b75cab
		if (tif->tif_tilesize == 0)
roentgen b75cab
			return (0);
roentgen b75cab
	}
roentgen b75cab
	else
roentgen b75cab
		tif->tif_tilesize = (tmsize_t)(-1);
roentgen b75cab
	tif->tif_scanlinesize = TIFFScanlineSize(tif);
roentgen b75cab
	if (tif->tif_scanlinesize == 0)
roentgen b75cab
		return (0);
roentgen b75cab
	tif->tif_flags |= TIFF_BEENWRITING;
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Setup the raw data buffer used for encoding.
roentgen b75cab
 */
roentgen b75cab
int
roentgen b75cab
TIFFWriteBufferSetup(TIFF* tif, void* bp, tmsize_t size)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteBufferSetup";
roentgen b75cab
roentgen b75cab
	if (tif->tif_rawdata) {
roentgen b75cab
		if (tif->tif_flags & TIFF_MYBUFFER) {
roentgen b75cab
			_TIFFfree(tif->tif_rawdata);
roentgen b75cab
			tif->tif_flags &= ~TIFF_MYBUFFER;
roentgen b75cab
		}
roentgen b75cab
		tif->tif_rawdata = NULL;
roentgen b75cab
	}
roentgen b75cab
	if (size == (tmsize_t)(-1)) {
roentgen b75cab
		size = (isTiled(tif) ?
roentgen b75cab
		    tif->tif_tilesize : TIFFStripSize(tif));
roentgen b75cab
		/*
roentgen b75cab
		 * Make raw data buffer at least 8K
roentgen b75cab
		 */
roentgen b75cab
		if (size < 8*1024)
roentgen b75cab
			size = 8*1024;
roentgen b75cab
		bp = NULL;			/* NB: force malloc */
roentgen b75cab
	}
roentgen b75cab
	if (bp == NULL) {
roentgen b75cab
		bp = _TIFFmalloc(size);
roentgen b75cab
		if (bp == NULL) {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module, "No space for output buffer");
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
		tif->tif_flags |= TIFF_MYBUFFER;
roentgen b75cab
	} else
roentgen b75cab
		tif->tif_flags &= ~TIFF_MYBUFFER;
roentgen b75cab
	tif->tif_rawdata = (uint8*) bp;
roentgen b75cab
	tif->tif_rawdatasize = size;
roentgen b75cab
	tif->tif_rawcc = 0;
roentgen b75cab
	tif->tif_rawcp = tif->tif_rawdata;
roentgen b75cab
	tif->tif_flags |= TIFF_BUFFERSETUP;
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Grow the strip data structures by delta strips.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
TIFFGrowStrips(TIFF* tif, uint32 delta, const char* module)
roentgen b75cab
{
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	uint64* new_stripoffset;
roentgen b75cab
	uint64* new_stripbytecount;
roentgen b75cab
roentgen b75cab
	assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
roentgen b75cab
	new_stripoffset = (uint64*)_TIFFrealloc(td->td_stripoffset,
roentgen b75cab
		(td->td_nstrips + delta) * sizeof (uint64));
roentgen b75cab
	new_stripbytecount = (uint64*)_TIFFrealloc(td->td_stripbytecount,
roentgen b75cab
		(td->td_nstrips + delta) * sizeof (uint64));
roentgen b75cab
	if (new_stripoffset == NULL || new_stripbytecount == NULL) {
roentgen b75cab
		if (new_stripoffset)
roentgen b75cab
			_TIFFfree(new_stripoffset);
roentgen b75cab
		if (new_stripbytecount)
roentgen b75cab
			_TIFFfree(new_stripbytecount);
roentgen b75cab
		td->td_nstrips = 0;
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "No space to expand strip arrays");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	td->td_stripoffset = new_stripoffset;
roentgen b75cab
	td->td_stripbytecount = new_stripbytecount;
roentgen b75cab
	_TIFFmemset(td->td_stripoffset + td->td_nstrips,
roentgen b75cab
		    0, delta*sizeof (uint64));
roentgen b75cab
	_TIFFmemset(td->td_stripbytecount + td->td_nstrips,
roentgen b75cab
		    0, delta*sizeof (uint64));
roentgen b75cab
	td->td_nstrips += delta;
roentgen b75cab
        tif->tif_flags |= TIFF_DIRTYDIRECT;
roentgen b75cab
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Append the data to the specified strip.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
TIFFAppendToStrip(TIFF* tif, uint32 strip, uint8* data, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFAppendToStrip";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	uint64 m;
roentgen b75cab
        int64 old_byte_count = -1;
roentgen b75cab
roentgen b75cab
	if (td->td_stripoffset[strip] == 0 || tif->tif_curoff == 0) {
roentgen b75cab
            assert(td->td_nstrips > 0);
roentgen b75cab
roentgen b75cab
            if( td->td_stripbytecount[strip] != 0 
roentgen b75cab
                && td->td_stripoffset[strip] != 0 
roentgen b75cab
                && td->td_stripbytecount[strip] >= (uint64) cc )
roentgen b75cab
            {
roentgen b75cab
                /* 
roentgen b75cab
                 * There is already tile data on disk, and the new tile
roentgen b75cab
                 * data we have will fit in the same space.  The only 
roentgen b75cab
                 * aspect of this that is risky is that there could be
roentgen b75cab
                 * more data to append to this strip before we are done
roentgen b75cab
                 * depending on how we are getting called.
roentgen b75cab
                 */
roentgen b75cab
                if (!SeekOK(tif, td->td_stripoffset[strip])) {
roentgen b75cab
                    TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                                 "Seek error at scanline %lu",
roentgen b75cab
                                 (unsigned long)tif->tif_row);
roentgen b75cab
                    return (0);
roentgen b75cab
                }
roentgen b75cab
            }
roentgen b75cab
            else
roentgen b75cab
            {
roentgen b75cab
                /* 
roentgen b75cab
                 * Seek to end of file, and set that as our location to 
roentgen b75cab
                 * write this strip.
roentgen b75cab
                 */
roentgen b75cab
                td->td_stripoffset[strip] = TIFFSeekFile(tif, 0, SEEK_END);
roentgen b75cab
                tif->tif_flags |= TIFF_DIRTYSTRIP;
roentgen b75cab
            }
roentgen b75cab
roentgen b75cab
            tif->tif_curoff = td->td_stripoffset[strip];
roentgen b75cab
roentgen b75cab
            /*
roentgen b75cab
             * We are starting a fresh strip/tile, so set the size to zero.
roentgen b75cab
             */
roentgen b75cab
            old_byte_count = td->td_stripbytecount[strip];
roentgen b75cab
            td->td_stripbytecount[strip] = 0;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	m = tif->tif_curoff+cc;
roentgen b75cab
	if (!(tif->tif_flags&TIFF_BIGTIFF))
roentgen b75cab
		m = (uint32)m;
roentgen b75cab
	if ((m<tif->tif_curoff)||(m<(uint64)cc))</tif->
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "Maximum TIFF file size exceeded");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	if (!WriteOK(tif, data, cc)) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "Write error at scanline %lu",
roentgen b75cab
		    (unsigned long) tif->tif_row);
roentgen b75cab
		    return (0);
roentgen b75cab
	}
roentgen b75cab
	tif->tif_curoff = m;
roentgen b75cab
	td->td_stripbytecount[strip] += cc;
roentgen b75cab
roentgen b75cab
        if( (int64) td->td_stripbytecount[strip] != old_byte_count )
roentgen b75cab
            tif->tif_flags |= TIFF_DIRTYSTRIP;
roentgen b75cab
            
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Internal version of TIFFFlushData that can be
roentgen b75cab
 * called by ``encodestrip routines'' w/o concern
roentgen b75cab
 * for infinite recursion.
roentgen b75cab
 */
roentgen b75cab
int
roentgen b75cab
TIFFFlushData1(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	if (tif->tif_rawcc > 0 && tif->tif_flags & TIFF_BUF4WRITE ) {
roentgen b75cab
		if (!isFillOrder(tif, tif->tif_dir.td_fillorder) &&
roentgen b75cab
		    (tif->tif_flags & TIFF_NOBITREV) == 0)
roentgen b75cab
			TIFFReverseBits((uint8*)tif->tif_rawdata,
roentgen b75cab
			    tif->tif_rawcc);
roentgen b75cab
		if (!TIFFAppendToStrip(tif,
roentgen b75cab
		    isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip,
roentgen b75cab
		    tif->tif_rawdata, tif->tif_rawcc))
roentgen b75cab
			return (0);
roentgen b75cab
		tif->tif_rawcc = 0;
roentgen b75cab
		tif->tif_rawcp = tif->tif_rawdata;
roentgen b75cab
	}
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Set the current write offset.  This should only be
roentgen b75cab
 * used to set the offset to a known previous location
roentgen b75cab
 * (very carefully), or to 0 so that the next write gets
roentgen b75cab
 * appended to the end of the file.
roentgen b75cab
 */
roentgen b75cab
void
roentgen b75cab
TIFFSetWriteOffset(TIFF* tif, toff_t off)
roentgen b75cab
{
roentgen b75cab
	tif->tif_curoff = off;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/* vim: set ts=8 sts=8 sw=8 noet: */
roentgen b75cab
/*
roentgen b75cab
 * Local Variables:
roentgen b75cab
 * mode: c
roentgen b75cab
 * c-basic-offset: 8
roentgen b75cab
 * fill-column: 78
roentgen b75cab
 * End:
roentgen b75cab
 */