Class TiffSaver

java.lang.Object
loci.formats.tiff.TiffSaver
All Implemented Interfaces:
Closeable, AutoCloseable

public class TiffSaver extends Object implements Closeable
Parses TIFF data from an input source.
Author:
Curtis Rueden ctrueden at wisc.edu, Eric Kjellman egkjellman at wisc.edu, Melissa Linkert melissa at glencoesoftware.com, Chris Allan callan at blackcat.ca
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private boolean
    Whether or not to write BigTIFF data.
    protected loci.common.ByteArrayHandle
    Output bytes.
    protected String
    Output filename.
    private loci.common.ByteArrayHandle
     
    private static final org.slf4j.Logger
     
    private CodecOptions
    The codec options if set.
    protected loci.common.RandomAccessOutputStream
    Output stream to use when saving TIFF data.
    private long[]
     
    private Long
     
    private long[]
    Store tile offsets and original file pointer when writing sequentially.
    private boolean
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    TiffSaver(String filename)
    Constructs a new TIFF saver from the given filename.
    TiffSaver(loci.common.RandomAccessOutputStream out, String filename)
    Constructs a new TIFF saver from the given output source.
    TiffSaver(loci.common.RandomAccessOutputStream out, loci.common.ByteArrayHandle bytes)
    Constructs a new TIFF saver from the given output source.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the output stream if not null.
    loci.common.RandomAccessOutputStream
    Gets the stream from which TIFF data is being saved.
    boolean
    Returns whether or not we are writing BigTIFF data.
    boolean
    Returns whether or not we are writing little-endian data.
    void
    makeValidIFD(IFD ifd, int pixelType, int nChannels)
    Makes a valid IFD.
    void
    overwriteComment(loci.common.RandomAccessInputStream in, Object value)
    Convenience method for overwriting a file's first ImageDescription.
    void
    overwriteIFDOffset(loci.common.RandomAccessInputStream raf, long offset, long nextPointer)
     
    void
    overwriteIFDValue(loci.common.RandomAccessInputStream raf, int ifd, int tag, Object value)
    Surgically overwrites an existing IFD value with the given one.
    void
    overwriteIFDValue(loci.common.RandomAccessInputStream raf, long ifdOffset, int tag, Object value)
    Surgically overwrites an existing IFD value with the given one.
    void
    overwriteIFDValue(loci.common.RandomAccessInputStream raf, long ifdOffset, int tag, Object value, boolean skipHeaderCheck)
    Surgically overwrites an existing IFD value with the given one.
    void
    overwriteLastIFDOffset(loci.common.RandomAccessInputStream raf)
     
    void
    setBigTiff(boolean bigTiff)
    Sets whether or not BigTIFF data should be written.
    void
    Sets the codec options.
    void
    setLittleEndian(boolean littleEndian)
    Sets whether or not little-endian data should be written.
    void
    setWritingSequentially(boolean sequential)
    Sets whether or not we know that the planes will be written sequentially.
    void
    Writes the TIFF file header.
    void
    writeIFD(IFD ifd, long nextOffset)
    Write the given IFD to the open stream.
    void
    writeIFD(IFD ifd, long nextOffset, boolean calculateOffset)
    Write the given IFD to the open stream.
    private void
    writeIFDStrips(IFD ifd, int no, byte[][] strips, int nChannels, boolean last, int x, int y, long defaultByteCount)
     
    void
    writeIFDValue(loci.common.RandomAccessOutputStream extraOut, long offset, int tag, Object value)
    Writes the given IFD value to the given output object.
    void
    writeImage(byte[][] buf, IFDList ifds, int pixelType)
     
    void
    writeImage(byte[] buf, IFD ifd, int no, int pixelType, boolean last)
     
    void
    writeImage(byte[] buf, IFD ifd, int no, int pixelType, int x, int y, int w, int h, boolean last)
    Writes to any rectangle from the passed block.
    void
    writeImage(byte[] buf, IFD ifd, int no, int pixelType, int x, int y, int w, int h, boolean last, Integer nChannels, boolean copyDirectly)
     
    void
    writeImageIFD(IFD ifd, int no, byte[][] strips, int nChannels, boolean last, int x, int y)
    Performs the actual work of dealing with IFD data and writing it to the TIFF for a given image or sub-image.
    private void
    writeIntValue(loci.common.RandomAccessOutputStream out, long offset)
    Write the given value to the given RandomAccessOutputStream.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LOGGER

      private static final org.slf4j.Logger LOGGER
    • out

      protected loci.common.RandomAccessOutputStream out
      Output stream to use when saving TIFF data.
    • filename

      protected String filename
      Output filename.
    • bytes

      protected loci.common.ByteArrayHandle bytes
      Output bytes.
    • bigTiff

      private boolean bigTiff
      Whether or not to write BigTIFF data.
    • sequentialWrite

      private boolean sequentialWrite
    • sequentialTileOffsets

      private long[] sequentialTileOffsets
      Store tile offsets and original file pointer when writing sequentially.
    • sequentialTileByteCounts

      private long[] sequentialTileByteCounts
    • sequentialTileFilePointer

      private Long sequentialTileFilePointer
    • options

      private CodecOptions options
      The codec options if set.
    • ifdBuffer

      private loci.common.ByteArrayHandle ifdBuffer
  • Constructor Details

    • TiffSaver

      public TiffSaver(String filename) throws IOException
      Constructs a new TIFF saver from the given filename.
      Parameters:
      filename - Filename of the output stream that we may use to create extra input or output streams as required.
      Throws:
      IOException
    • TiffSaver

      public TiffSaver(loci.common.RandomAccessOutputStream out, String filename)
      Constructs a new TIFF saver from the given output source.
      Parameters:
      out - Output stream to save TIFF data to.
      filename - Filename of the output stream that we may use to create extra input or output streams as required.
    • TiffSaver

      public TiffSaver(loci.common.RandomAccessOutputStream out, loci.common.ByteArrayHandle bytes)
      Constructs a new TIFF saver from the given output source.
      Parameters:
      out - Output stream to save TIFF data to.
      bytes - In memory byte array handle that we may use to create extra input or output streams as required.
  • Method Details

    • close

      public void close() throws IOException
      Closes the output stream if not null.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - Thrown if an error occurred while closing.
    • setWritingSequentially

      public void setWritingSequentially(boolean sequential)
      Sets whether or not we know that the planes will be written sequentially. If we are writing planes sequentially and set this flag, then performance is slightly improved.
    • getStream

      public loci.common.RandomAccessOutputStream getStream()
      Gets the stream from which TIFF data is being saved.
    • setLittleEndian

      public void setLittleEndian(boolean littleEndian)
      Sets whether or not little-endian data should be written.
    • setBigTiff

      public void setBigTiff(boolean bigTiff)
      Sets whether or not BigTIFF data should be written.
    • isLittleEndian

      public boolean isLittleEndian()
      Returns whether or not we are writing little-endian data.
    • isBigTiff

      public boolean isBigTiff()
      Returns whether or not we are writing BigTIFF data.
    • setCodecOptions

      public void setCodecOptions(CodecOptions options)
      Sets the codec options.
      Parameters:
      options - The value to set.
    • writeHeader

      public void writeHeader() throws IOException
      Writes the TIFF file header.
      Throws:
      IOException
    • writeImage

      public void writeImage(byte[][] buf, IFDList ifds, int pixelType) throws FormatException, IOException
      Throws:
      FormatException
      IOException
    • writeImage

      public void writeImage(byte[] buf, IFD ifd, int no, int pixelType, boolean last) throws FormatException, IOException
      Throws:
      FormatException
      IOException
    • writeImage

      public void writeImage(byte[] buf, IFD ifd, int no, int pixelType, int x, int y, int w, int h, boolean last) throws FormatException, IOException
      Writes to any rectangle from the passed block.
      Parameters:
      buf - The block that is to be written.
      ifd - The Image File Directories. Mustn't be null.
      no - the plane index within the current series.
      pixelType - The type of pixels.
      x - The X-coordinate of the top-left corner.
      y - The Y-coordinate of the top-left corner.
      w - The width of the rectangle.
      h - The height of the rectangle.
      last - Pass true if it is the last image, false otherwise.
      Throws:
      FormatException
      IOException
    • writeImage

      public void writeImage(byte[] buf, IFD ifd, int no, int pixelType, int x, int y, int w, int h, boolean last, Integer nChannels, boolean copyDirectly) throws FormatException, IOException
      Throws:
      FormatException
      IOException
    • writeImageIFD

      public void writeImageIFD(IFD ifd, int no, byte[][] strips, int nChannels, boolean last, int x, int y) throws FormatException, IOException
      Performs the actual work of dealing with IFD data and writing it to the TIFF for a given image or sub-image.
      Parameters:
      ifd - The Image File Directories. Mustn't be null.
      no - the plane index within the series.
      strips - The strips to write to the file.
      last - Pass true if it is the last image, false otherwise.
      x - The initial X offset of the strips/tiles to write.
      y - The initial Y offset of the strips/tiles to write.
      Throws:
      FormatException
      IOException
    • writeIFD

      public void writeIFD(IFD ifd, long nextOffset) throws FormatException, IOException
      Write the given IFD to the open stream. The provided offset will be used as the offset to the next IFD, and can be a placeholder value.
      Parameters:
      ifd - the complete IFD to be written
      nextOffset - the offset of the next IFD to be written
      Throws:
      FormatException
      IOException
    • writeIFD

      public void writeIFD(IFD ifd, long nextOffset, boolean calculateOffset) throws FormatException, IOException
      Write the given IFD to the open stream. The provided offset will be used as the offset to the next IFD, unless offset calculation is requested. If offset calculation is requested, the next IFD's offset will be set so that it immediately follows the current IFD. Don't enable offset calculation when writing the last IFD, unless the offset will later be overwritten with 0.
      Parameters:
      ifd - the complete IFD to be written
      nextOffset - the offset of the next IFD to be written (if known)
      calculateOffset - true if nextOffset should be ignored, and the next IFD should be assumed to immediately follow this one
      Throws:
      FormatException
      IOException
    • writeIFDValue

      public void writeIFDValue(loci.common.RandomAccessOutputStream extraOut, long offset, int tag, Object value) throws FormatException, IOException
      Writes the given IFD value to the given output object.
      Parameters:
      extraOut - buffer to which "extra" IFD information should be written
      offset - global offset to use for IFD offset values
      tag - IFD tag to write
      value - IFD value to write
      Throws:
      FormatException
      IOException
    • overwriteLastIFDOffset

      public void overwriteLastIFDOffset(loci.common.RandomAccessInputStream raf) throws FormatException, IOException
      Throws:
      FormatException
      IOException
    • overwriteIFDOffset

      public void overwriteIFDOffset(loci.common.RandomAccessInputStream raf, long offset, long nextPointer) throws FormatException, IOException
      Throws:
      FormatException
      IOException
    • overwriteIFDValue

      public void overwriteIFDValue(loci.common.RandomAccessInputStream raf, int ifd, int tag, Object value) throws FormatException, IOException
      Surgically overwrites an existing IFD value with the given one. This method requires that the IFD directory entry already exist. It intelligently updates the count field of the entry to match the new length. If the new length is longer than the old length, it appends the new data to the end of the file and updates the offset field; if not, or if the old data is already at the end of the file, it overwrites the old data in place.
      Parameters:
      raf - the input stream representing the file to be edited
      ifd - the index into the list of IFDs, see TiffParser.getIFDOffsets()
      tag - the tag code
      value - the new value for the tag
      Throws:
      FormatException
      IOException
    • overwriteIFDValue

      public void overwriteIFDValue(loci.common.RandomAccessInputStream raf, long ifdOffset, int tag, Object value) throws FormatException, IOException
      Surgically overwrites an existing IFD value with the given one. This method requires that the IFD directory entry already exist. It intelligently updates the count field of the entry to match the new length. If the new length is longer than the old length, it appends the new data to the end of the file and updates the offset field; if not, or if the old data is already at the end of the file, it overwrites the old data in place.
      Parameters:
      raf - the input stream representing the file to be edited
      ifdOffset - the offset to the IFD
      tag - the tag code
      value - the new value for the tag
      Throws:
      FormatException
      IOException
    • overwriteIFDValue

      public void overwriteIFDValue(loci.common.RandomAccessInputStream raf, long ifdOffset, int tag, Object value, boolean skipHeaderCheck) throws FormatException, IOException
      Surgically overwrites an existing IFD value with the given one. This method requires that the IFD directory entry already exist. It intelligently updates the count field of the entry to match the new length. If the new length is longer than the old length, it appends the new data to the end of the file and updates the offset field; if not, or if the old data is already at the end of the file, it overwrites the old data in place.
      Parameters:
      raf - the input stream representing the file to be edited
      ifdOffset - the offset to the IFD
      tag - the tag code
      value - the new value for the tag
      skipHeaderCheck - true if the TIFF header does not need to be checked
      Throws:
      FormatException
      IOException
    • overwriteComment

      public void overwriteComment(loci.common.RandomAccessInputStream in, Object value) throws FormatException, IOException
      Convenience method for overwriting a file's first ImageDescription.
      Throws:
      FormatException
      IOException
    • writeIntValue

      private void writeIntValue(loci.common.RandomAccessOutputStream out, long offset) throws IOException
      Write the given value to the given RandomAccessOutputStream. If the 'bigTiff' flag is set, then the value will be written as an 8 byte long; otherwise, it will be written as a 4 byte integer.
      Throws:
      IOException
    • makeValidIFD

      public void makeValidIFD(IFD ifd, int pixelType, int nChannels)
      Makes a valid IFD.
      Parameters:
      ifd - The IFD to handle.
      pixelType - The pixel type.
      nChannels - The number of channels.
    • writeIFDStrips

      private void writeIFDStrips(IFD ifd, int no, byte[][] strips, int nChannels, boolean last, int x, int y, long defaultByteCount) throws FormatException, IOException
      Throws:
      FormatException
      IOException