Beispiel #1
0
        // Read a tile of data and decompress the specified
        // amount into the user-supplied buffer.
        public static int TIFFReadEncodedTile(TIFF tif, uint tile, byte[] buf, int size)
        {
            TIFFDirectory td       = tif.tif_dir;
            int           tilesize = tif.tif_tilesize;

            if (!TIFFCheckRead(tif, true))
            {
                return(-1);
            }

            if (tile >= td.td_nstrips)
            {
                TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "{0}: Tile out of range, max {1}", tile, td.td_nstrips);
                return(-1);
            }

            if (size == -1)
            {
                size = tilesize;
            }
            else if (size > tilesize)
            {
                size = tilesize;
            }

            if (TIFFFillTile(tif, tile) && tif.tif_decodetile(tif, buf, size, (ushort)(tile / td.td_stripsperimage)))
            {
                tif.tif_postdecode(tif, buf, 0, size);
                return(size);
            }

            return(-1);
        }
Beispiel #2
0
        public static int TIFFReadScanline(TIFF tif, byte[] buf, uint row, ushort sample)
        {
            if (!TIFFCheckRead(tif, false))
            {
                return(-1);
            }

            bool e = TIFFSeek(tif, buf, row, sample);

            if (e)
            {
                // Decompress desired row into user buffer.
                e = tif.tif_decoderow(tif, buf, (int)tif.tif_scanlinesize, sample);

                // we are now poised at the beginning of the next row
                tif.tif_row = row + 1;

                if (e)
                {
                    tif.tif_postdecode(tif, buf, 0, (int)tif.tif_scanlinesize);
                }
            }

            return(e?1:-1);
        }
Beispiel #3
0
        // Read a strip of data and decompress the specified
        // amount into the user-supplied buffer.
        public static int TIFFReadEncodedStrip(TIFF tif, int strip, byte[] buf, int size)
        {
            if (!TIFFCheckRead(tif, false))
            {
                return(-1);
            }

            TIFFDirectory td = tif.tif_dir;

            if (strip >= td.td_nstrips)
            {
                TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "{0}: Strip out of range, max {1}", strip, td.td_nstrips);
                return(-1);
            }

            // Calculate the strip size according to the number of
            // rows in the strip (check for truncated last strip on any
            // of the separations).
            uint strips_per_sep;

            if (td.td_rowsperstrip >= td.td_imagelength)
            {
                strips_per_sep = 1;
            }
            else
            {
                strips_per_sep = (td.td_imagelength + td.td_rowsperstrip - 1) / td.td_rowsperstrip;
            }

            uint sep_strip = (uint)strip % strips_per_sep;

            uint nrows = td.td_imagelength % td.td_rowsperstrip;

            if (sep_strip != strips_per_sep - 1 || nrows == 0)
            {
                nrows = td.td_rowsperstrip;
            }

            int stripsize = TIFFVStripSize(tif, nrows);

            if (size == -1)
            {
                size = stripsize;
            }
            else if (size > stripsize)
            {
                size = stripsize;
            }

            if (TIFFFillStrip(tif, (uint)strip) && tif.tif_decodestrip(tif, buf, size, (ushort)(strip / td.td_stripsperimage)))
            {
                tif.tif_postdecode(tif, buf, 0, size);
                return(size);
            }

            return(-1);
        }
Beispiel #4
0
        public static int TIFFReadScanline(TIFF tif, byte[] buf, uint row, ushort sample)
        {
            if(!TIFFCheckRead(tif, false)) return -1;

            bool e=TIFFSeek(tif, buf, row, sample);

            if(e)
            {
                // Decompress desired row into user buffer.
                e=tif.tif_decoderow(tif, buf, (int)tif.tif_scanlinesize, sample);

                // we are now poised at the beginning of the next row
                tif.tif_row=row+1;

                if(e) tif.tif_postdecode(tif, buf, 0, (int)tif.tif_scanlinesize);
            }

            return e?1:-1;
        }
Beispiel #5
0
        // Read a tile of data and decompress the specified
        // amount into the user-supplied buffer.
        public static int TIFFReadEncodedTile(TIFF tif, uint tile, byte[] buf, int size)
        {
            TIFFDirectory td=tif.tif_dir;
            int tilesize=tif.tif_tilesize;

            if(!TIFFCheckRead(tif, true)) return -1;

            if(tile>=td.td_nstrips)
            {
                TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "{0}: Tile out of range, max {1}", tile, td.td_nstrips);
                return -1;
            }

            if(size==-1) size=tilesize;
            else if(size>tilesize) size=tilesize;

            if(TIFFFillTile(tif, tile)&&tif.tif_decodetile(tif, buf, size, (ushort)(tile/td.td_stripsperimage)))
            {
                tif.tif_postdecode(tif, buf, 0, size);
                return size;
            }

            return -1;
        }
Beispiel #6
0
        // Read a strip of data and decompress the specified
        // amount into the user-supplied buffer.
        public static int TIFFReadEncodedStrip(TIFF tif, int strip, byte[] buf, int size)
        {
            if(!TIFFCheckRead(tif, false)) return -1;

            TIFFDirectory td=tif.tif_dir;

            if(strip>=td.td_nstrips)
            {
                TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "{0}: Strip out of range, max {1}", strip, td.td_nstrips);
                return -1;
            }

            // Calculate the strip size according to the number of
            // rows in the strip (check for truncated last strip on any
            // of the separations).
            uint strips_per_sep;
            if(td.td_rowsperstrip>=td.td_imagelength) strips_per_sep=1;
            else strips_per_sep=(td.td_imagelength+td.td_rowsperstrip-1)/td.td_rowsperstrip;

            uint sep_strip=(uint)strip%strips_per_sep;

            uint nrows=td.td_imagelength%td.td_rowsperstrip;

            if(sep_strip!=strips_per_sep-1||nrows==0) nrows=td.td_rowsperstrip;

            int stripsize=TIFFVStripSize(tif, nrows);
            if(size==-1) size=stripsize;
            else if(size>stripsize) size=stripsize;

            if(TIFFFillStrip(tif, (uint)strip)&&tif.tif_decodestrip(tif, buf, size, (ushort)(strip/td.td_stripsperimage)))
            {
                tif.tif_postdecode(tif, buf, 0, size);
                return (size);
            }

            return -1;
        }
Beispiel #7
0
        public static int TIFFWriteScanline(TIFF tif, byte[] buf, uint row, ushort sample)
        {
            string module="TIFFWriteScanline";

            if(!((tif.tif_flags&TIF_FLAGS.TIFF_BEENWRITING)!=0||TIFFWriteCheck(tif, false, module))) return -1;

            // Handle delayed allocation of data buffer. This
            // permits it to be sized more intelligently (using
            // directory information).
            if(!BUFFERCHECK(tif)) return -1;
            TIFFDirectory td=tif.tif_dir;
            bool imagegrew=false;

            // Extend image length if needed
            // (but only for PlanarConfig=1).
            if(row>=td.td_imagelength)
            {	// extend image
                if(td.td_planarconfig==PLANARCONFIG.SEPARATE)
                {
                    TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "Can not change \"ImageLength\" when using separate planes");
                    return -1;
                }

                td.td_imagelength=row+1;
                imagegrew=true;
            }

            // Calculate strip and check for crossings.
            uint strip;
            if(td.td_planarconfig==PLANARCONFIG.SEPARATE)
            {
                if(sample>=td.td_samplesperpixel)
                {
                    TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "{0}: Sample out of range, max {1}", sample, td.td_samplesperpixel);
                    return -1;
                }
                strip=sample*td.td_stripsperimage+row/td.td_rowsperstrip;
            }
            else strip=row/td.td_rowsperstrip;

            // Check strip array to make sure there's space. We don't support
            // dynamically growing files that have data organized in separate
            // bitplanes because it's too painful. In that case we require that
            // the imagelength be set properly before the first write (so that the
            // strips array will be fully allocated above).
            if(strip>=td.td_nstrips&&!TIFFGrowStrips(tif, 1, module)) return -1;

            if(strip!=tif.tif_curstrip)
            {
                // Changing strips -- flush any data present.
                if(!TIFFFlushData(tif)) return -1;

                tif.tif_curstrip=strip;

                // Watch out for a growing image. The value of strips/image
                // will initially be 1 (since it can't be deduced until the
                // imagelength is known).
                if(strip>=td.td_stripsperimage&&imagegrew) td.td_stripsperimage=TIFFhowmany(td.td_imagelength, td.td_rowsperstrip);

                tif.tif_row=(strip%td.td_stripsperimage)*td.td_rowsperstrip;
                if((tif.tif_flags&TIF_FLAGS.TIFF_CODERSETUP)==0)
                {
                    if(!tif.tif_setupencode(tif)) return -1;
                    tif.tif_flags|=TIF_FLAGS.TIFF_CODERSETUP;
                }

                tif.tif_rawcc=0;
                tif.tif_rawcp=0;

                if(td.td_stripbytecount[strip]>0)
                {
                    // Force TIFFAppendToStrip() to consider placing data at end of file.
                    tif.tif_curoff=0;
                }

                if(!tif.tif_preencode(tif, sample)) return -1;
                tif.tif_flags|=TIF_FLAGS.TIFF_POSTENCODE;
            }

            // Ensure the write is either sequential or at the
            // beginning of a strip (or that we can randomly
            // access the data -- i.e. no encoding).
            if(row!=tif.tif_row)
            {
                if(row<tif.tif_row)
                {
                    // Moving backwards within the same strip:
                    // backup to the start and then decode
                    // forward (below).
                    tif.tif_row=(strip%td.td_stripsperimage)*td.td_rowsperstrip;
                    tif.tif_rawcp=0;
                }

                // Seek forward to the desired row.
                if(!tif.tif_seek(tif, row-tif.tif_row)) return -1;
                tif.tif_row=row;
            }

            // swab if needed - note that source buffer will be altered
            tif.tif_postdecode(tif, buf, 0, (int)tif.tif_scanlinesize);

            bool status=tif.tif_encoderow(tif, buf, (int)tif.tif_scanlinesize, sample);

            // we are now poised at the beginning of the next row
            tif.tif_row=row+1;

            return status?1:0;
        }
Beispiel #8
0
        // Encode the supplied data and write it to the
        // specified tile. There must be space for the
        // data. The function clamps individual writes
        // to a tile to the tile size, but does not (and
        // can not) check that multiple writes to the same
        // tile do not write more than tile size data.
        //
        // NB:	Image length must be setup before writing; this
        //        interface does not support automatically growing
        //        the image on each write (as TIFFWriteScanline does).
        public static int TIFFWriteEncodedTile(TIFF tif, uint tile, byte[] data, int cc)
        {
            string module="TIFFWriteEncodedTile";
            ushort sample;

            if(!((tif.tif_flags&TIF_FLAGS.TIFF_BEENWRITING)!=0||TIFFWriteCheck(tif, true, module))) return -1;
            TIFFDirectory td=tif.tif_dir;
            if(tile>=td.td_nstrips)
            {
                TIFFErrorExt(tif.tif_clientdata, module, "{0}: Tile {1} out of range, max {2}", tif.tif_name, tile, td.td_nstrips);
                return -1;
            }

            // Handle delayed allocation of data buffer. This
            // permits it to be sized more intelligently (using
            // directory information).
            if(!BUFFERCHECK(tif)) return -1;

            tif.tif_curtile=tile;

            tif.tif_rawcc=0;
            tif.tif_rawcp=0;;

            if(td.td_stripbytecount[tile]>0)
            {
                // if we are writing over existing tiles, zero length.
                td.td_stripbytecount[tile]=0;

                // this forces TIFFAppendToStrip() to do a seek.
                tif.tif_curoff=0;
            }

            // Compute tiles per row & per column to compute
            // current row and column
            tif.tif_row=(tile%TIFFhowmany(td.td_imagelength, td.td_tilelength))*td.td_tilelength;
            tif.tif_col=(tile%TIFFhowmany(td.td_imagewidth, td.td_tilewidth))*td.td_tilewidth;

            if((tif.tif_flags&TIF_FLAGS.TIFF_CODERSETUP)==0)
            {
                if(!tif.tif_setupencode(tif)) return -1;
                tif.tif_flags|=TIF_FLAGS.TIFF_CODERSETUP;
            }

            tif.tif_flags&=~TIF_FLAGS.TIFF_POSTENCODE;
            sample=(ushort)(tile/td.td_stripsperimage);
            if(!tif.tif_preencode(tif, sample)) return -1;

            // Clamp write amount to the tile size. This is mostly
            // done so that callers can pass in some large number
            // (e.g. -1) and have the tile size used instead.
            if(cc<1||cc>tif.tif_tilesize) cc=tif.tif_tilesize;

            // swab if needed - note that source buffer will be altered
            tif.tif_postdecode(tif, data, 0, cc);

            if(!tif.tif_encodetile(tif, data, cc, sample)) return 0;
            if(!tif.tif_postencode(tif)) return -1;

            if(!isFillOrder(tif, td.td_fillorder)&&(tif.tif_flags&TIF_FLAGS.TIFF_NOBITREV)==0) TIFFReverseBits(tif.tif_rawdata, tif.tif_rawcc);

            if(tif.tif_rawcc>0&&!TIFFAppendToStrip(tif, tile, tif.tif_rawdata, tif.tif_rawcc)) return -1;

            tif.tif_rawcc=0;
            tif.tif_rawcp=0;

            return cc;
        }
Beispiel #9
0
        // Encode the supplied data and write it to the
        // specified strip.
        //
        // NB: Image length must be setup before writing.
        public static int TIFFWriteEncodedStrip(TIFF tif, uint strip, byte[] data, int cc)
        {
            string module="TIFFWriteEncodedStrip";
            TIFFDirectory td=tif.tif_dir;
            ushort sample;

            if(!((tif.tif_flags&TIF_FLAGS.TIFF_BEENWRITING)!=0||TIFFWriteCheck(tif, false, module))) return -1;

            // Check strip array to make sure there's space.
            // We don't support dynamically growing files that
            // have data organized in separate bitplanes because
            // it's too painful. In that case we require that
            // the imagelength be set properly before the first
            // write (so that the strips array will be fully
            // allocated above).
            ///
            if(strip>=td.td_nstrips)
            {
                if(td.td_planarconfig==PLANARCONFIG.SEPARATE)
                {
                    TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "Can not grow image by strips when using separate planes");
                    return -1;
                }

                if(!TIFFGrowStrips(tif, 1, module)) return -1;
                td.td_stripsperimage=TIFFhowmany(td.td_imagelength, td.td_rowsperstrip);
            }

            // Handle delayed allocation of data buffer. This
            // permits it to be sized according to the directory
            // info.
            if(!BUFFERCHECK(tif)) return -1;

            tif.tif_curstrip=strip;
            tif.tif_row=(strip%td.td_stripsperimage)*td.td_rowsperstrip;
            if((tif.tif_flags&TIF_FLAGS.TIFF_CODERSETUP)==0)
            {
                if(!tif.tif_setupencode(tif)) return -1;
                tif.tif_flags|=TIF_FLAGS.TIFF_CODERSETUP;
            }

            tif.tif_rawcc=0;
            tif.tif_rawcp=0;

            if(td.td_stripbytecount[strip]>0)
            {
                // Force TIFFAppendToStrip() to consider placing data at end of file.
                tif.tif_curoff=0;
            }

            tif.tif_flags&=~TIF_FLAGS.TIFF_POSTENCODE;
            sample=(ushort)(strip/td.td_stripsperimage);
            if(!tif.tif_preencode(tif, sample)) return -1;

            // swab if needed - note that source buffer will be altered
            tif.tif_postdecode(tif, data, 0, cc);

            if(!tif.tif_encodestrip(tif, data, cc, sample)) return 0;
            if(!tif.tif_postencode(tif)) return -1;
            if(!isFillOrder(tif, td.td_fillorder)&&(tif.tif_flags&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 -1;

            tif.tif_rawcc=0;
            tif.tif_rawcp=0;

            return cc;
        }
Beispiel #10
0
        public static int TIFFWriteScanline(TIFF tif, byte[] buf, uint row, ushort sample)
        {
            string module = "TIFFWriteScanline";

            if (!((tif.tif_flags & TIF_FLAGS.TIFF_BEENWRITING) != 0 || TIFFWriteCheck(tif, false, module)))
            {
                return(-1);
            }

            // Handle delayed allocation of data buffer. This
            // permits it to be sized more intelligently (using
            // directory information).
            if (!BUFFERCHECK(tif))
            {
                return(-1);
            }
            TIFFDirectory td        = tif.tif_dir;
            bool          imagegrew = false;

            // Extend image length if needed
            // (but only for PlanarConfig=1).
            if (row >= td.td_imagelength)
            {                   // extend image
                if (td.td_planarconfig == PLANARCONFIG.SEPARATE)
                {
                    TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "Can not change \"ImageLength\" when using separate planes");
                    return(-1);
                }

                td.td_imagelength = row + 1;
                imagegrew         = true;
            }

            // Calculate strip and check for crossings.
            uint strip;

            if (td.td_planarconfig == PLANARCONFIG.SEPARATE)
            {
                if (sample >= td.td_samplesperpixel)
                {
                    TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "{0}: Sample out of range, max {1}", sample, td.td_samplesperpixel);
                    return(-1);
                }
                strip = sample * td.td_stripsperimage + row / td.td_rowsperstrip;
            }
            else
            {
                strip = row / td.td_rowsperstrip;
            }

            // Check strip array to make sure there's space. We don't support
            // dynamically growing files that have data organized in separate
            // bitplanes because it's too painful. In that case we require that
            // the imagelength be set properly before the first write (so that the
            // strips array will be fully allocated above).
            if (strip >= td.td_nstrips && !TIFFGrowStrips(tif, 1, module))
            {
                return(-1);
            }

            if (strip != tif.tif_curstrip)
            {
                // Changing strips -- flush any data present.
                if (!TIFFFlushData(tif))
                {
                    return(-1);
                }

                tif.tif_curstrip = strip;

                // Watch out for a growing image. The value of strips/image
                // will initially be 1 (since it can't be deduced until the
                // imagelength is known).
                if (strip >= td.td_stripsperimage && imagegrew)
                {
                    td.td_stripsperimage = TIFFhowmany(td.td_imagelength, td.td_rowsperstrip);
                }

                tif.tif_row = (strip % td.td_stripsperimage) * td.td_rowsperstrip;
                if ((tif.tif_flags & TIF_FLAGS.TIFF_CODERSETUP) == 0)
                {
                    if (!tif.tif_setupencode(tif))
                    {
                        return(-1);
                    }
                    tif.tif_flags |= TIF_FLAGS.TIFF_CODERSETUP;
                }

                tif.tif_rawcc = 0;
                tif.tif_rawcp = 0;

                if (td.td_stripbytecount[strip] > 0)
                {
                    // Force TIFFAppendToStrip() to consider placing data at end of file.
                    tif.tif_curoff = 0;
                }

                if (!tif.tif_preencode(tif, sample))
                {
                    return(-1);
                }
                tif.tif_flags |= TIF_FLAGS.TIFF_POSTENCODE;
            }

            // Ensure the write is either sequential or at the
            // beginning of a strip (or that we can randomly
            // access the data -- i.e. no encoding).
            if (row != tif.tif_row)
            {
                if (row < tif.tif_row)
                {
                    // Moving backwards within the same strip:
                    // backup to the start and then decode
                    // forward (below).
                    tif.tif_row   = (strip % td.td_stripsperimage) * td.td_rowsperstrip;
                    tif.tif_rawcp = 0;
                }

                // Seek forward to the desired row.
                if (!tif.tif_seek(tif, row - tif.tif_row))
                {
                    return(-1);
                }
                tif.tif_row = row;
            }

            // swab if needed - note that source buffer will be altered
            tif.tif_postdecode(tif, buf, 0, (int)tif.tif_scanlinesize);

            bool status = tif.tif_encoderow(tif, buf, (int)tif.tif_scanlinesize, sample);

            // we are now poised at the beginning of the next row
            tif.tif_row = row + 1;

            return(status?1:0);
        }
Beispiel #11
0
        // Encode the supplied data and write it to the
        // specified tile. There must be space for the
        // data. The function clamps individual writes
        // to a tile to the tile size, but does not (and
        // can not) check that multiple writes to the same
        // tile do not write more than tile size data.
        //
        // NB:	Image length must be setup before writing; this
        //		interface does not support automatically growing
        //		the image on each write (as TIFFWriteScanline does).
        public static int TIFFWriteEncodedTile(TIFF tif, uint tile, byte[] data, int cc)
        {
            string module = "TIFFWriteEncodedTile";
            ushort sample;

            if (!((tif.tif_flags & TIF_FLAGS.TIFF_BEENWRITING) != 0 || TIFFWriteCheck(tif, true, module)))
            {
                return(-1);
            }
            TIFFDirectory td = tif.tif_dir;

            if (tile >= td.td_nstrips)
            {
                TIFFErrorExt(tif.tif_clientdata, module, "{0}: Tile {1} out of range, max {2}", tif.tif_name, tile, td.td_nstrips);
                return(-1);
            }

            // Handle delayed allocation of data buffer. This
            // permits it to be sized more intelligently (using
            // directory information).
            if (!BUFFERCHECK(tif))
            {
                return(-1);
            }

            tif.tif_curtile = tile;

            tif.tif_rawcc = 0;
            tif.tif_rawcp = 0;;

            if (td.td_stripbytecount[tile] > 0)
            {
                // if we are writing over existing tiles, zero length.
                td.td_stripbytecount[tile] = 0;

                // this forces TIFFAppendToStrip() to do a seek.
                tif.tif_curoff = 0;
            }

            // Compute tiles per row & per column to compute
            // current row and column
            tif.tif_row = (tile % TIFFhowmany(td.td_imagelength, td.td_tilelength)) * td.td_tilelength;
            tif.tif_col = (tile % TIFFhowmany(td.td_imagewidth, td.td_tilewidth)) * td.td_tilewidth;

            if ((tif.tif_flags & TIF_FLAGS.TIFF_CODERSETUP) == 0)
            {
                if (!tif.tif_setupencode(tif))
                {
                    return(-1);
                }
                tif.tif_flags |= TIF_FLAGS.TIFF_CODERSETUP;
            }

            tif.tif_flags &= ~TIF_FLAGS.TIFF_POSTENCODE;
            sample         = (ushort)(tile / td.td_stripsperimage);
            if (!tif.tif_preencode(tif, sample))
            {
                return(-1);
            }

            // Clamp write amount to the tile size. This is mostly
            // done so that callers can pass in some large number
            // (e.g. -1) and have the tile size used instead.
            if (cc < 1 || cc > tif.tif_tilesize)
            {
                cc = tif.tif_tilesize;
            }

            // swab if needed - note that source buffer will be altered
            tif.tif_postdecode(tif, data, 0, cc);

            if (!tif.tif_encodetile(tif, data, cc, sample))
            {
                return(0);
            }
            if (!tif.tif_postencode(tif))
            {
                return(-1);
            }

            if (!isFillOrder(tif, td.td_fillorder) && (tif.tif_flags & TIF_FLAGS.TIFF_NOBITREV) == 0)
            {
                TIFFReverseBits(tif.tif_rawdata, tif.tif_rawcc);
            }

            if (tif.tif_rawcc > 0 && !TIFFAppendToStrip(tif, tile, tif.tif_rawdata, tif.tif_rawcc))
            {
                return(-1);
            }

            tif.tif_rawcc = 0;
            tif.tif_rawcp = 0;

            return(cc);
        }
Beispiel #12
0
        // Encode the supplied data and write it to the
        // specified strip.
        //
        // NB: Image length must be setup before writing.
        public static int TIFFWriteEncodedStrip(TIFF tif, uint strip, byte[] data, int cc)
        {
            string        module = "TIFFWriteEncodedStrip";
            TIFFDirectory td     = tif.tif_dir;
            ushort        sample;

            if (!((tif.tif_flags & TIF_FLAGS.TIFF_BEENWRITING) != 0 || TIFFWriteCheck(tif, false, module)))
            {
                return(-1);
            }

            // Check strip array to make sure there's space.
            // We don't support dynamically growing files that
            // have data organized in separate bitplanes because
            // it's too painful. In that case we require that
            // the imagelength be set properly before the first
            // write (so that the strips array will be fully
            // allocated above).
            ///
            if (strip >= td.td_nstrips)
            {
                if (td.td_planarconfig == PLANARCONFIG.SEPARATE)
                {
                    TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "Can not grow image by strips when using separate planes");
                    return(-1);
                }

                if (!TIFFGrowStrips(tif, 1, module))
                {
                    return(-1);
                }
                td.td_stripsperimage = TIFFhowmany(td.td_imagelength, td.td_rowsperstrip);
            }

            // Handle delayed allocation of data buffer. This
            // permits it to be sized according to the directory
            // info.
            if (!BUFFERCHECK(tif))
            {
                return(-1);
            }

            tif.tif_curstrip = strip;
            tif.tif_row      = (strip % td.td_stripsperimage) * td.td_rowsperstrip;
            if ((tif.tif_flags & TIF_FLAGS.TIFF_CODERSETUP) == 0)
            {
                if (!tif.tif_setupencode(tif))
                {
                    return(-1);
                }
                tif.tif_flags |= TIF_FLAGS.TIFF_CODERSETUP;
            }

            tif.tif_rawcc = 0;
            tif.tif_rawcp = 0;

            if (td.td_stripbytecount[strip] > 0)
            {
                // Force TIFFAppendToStrip() to consider placing data at end of file.
                tif.tif_curoff = 0;
            }

            tif.tif_flags &= ~TIF_FLAGS.TIFF_POSTENCODE;
            sample         = (ushort)(strip / td.td_stripsperimage);
            if (!tif.tif_preencode(tif, sample))
            {
                return(-1);
            }

            // swab if needed - note that source buffer will be altered
            tif.tif_postdecode(tif, data, 0, cc);

            if (!tif.tif_encodestrip(tif, data, cc, sample))
            {
                return(0);
            }
            if (!tif.tif_postencode(tif))
            {
                return(-1);
            }
            if (!isFillOrder(tif, td.td_fillorder) && (tif.tif_flags & 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(-1);
            }

            tif.tif_rawcc = 0;
            tif.tif_rawcp = 0;

            return(cc);
        }