loci.formats.tiff

## Class TiffParser

- java.lang.Object
- - loci.formats.tiff.TiffParser

- All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

      public class TiffParser
      extends java.lang.Object
      implements java.io.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 and Description
    --- | ---
    `private boolean` | `bigTiff` Whether or not the TIFF file contains BigTIFF data.
    `private byte[]` | `cachedTileBuffer` Cached tile buffer to avoid re-allocations when reading tiles.
    `private boolean` | `canClose`
    `private CodecOptions` | `codecOptions` Codec options to be used when decoding compressed pixel data.
    `private boolean` | `doCaching`
    `private boolean` | `equalStrips`
    `private boolean` | `fakeBigTiff` Whether or not 64-bit offsets are used for non-BigTIFF files.
    `private IFD` | `firstIFD` Cached first IFD in the current file.
    `private IFDList` | `ifdList` Cached list of IFDs in the current file.
    `protected loci.common.RandomAccessInputStream` | `in` Input source from which to parse TIFF data.
    `private static org.slf4j.Logger` | `LOGGER`
    `private java.lang.String` | `tagEncoding`
    `private boolean` | `ycbcrCorrection`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `TiffParser(loci.common.RandomAccessInputStream in)` Constructs a new TIFF parser from the given input source.
    `TiffParser(java.lang.String filename)` Constructs a new TIFF parser from the given file name.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `java.lang.Boolean` | `checkHeader()` Checks the TIFF header.
    `void` | `close()` Close the underlying stream, if this TiffParser was constructed with a file path.
    `void` | `fillInIFD(IFD ifd)` Fill in IFD entries that are stored at an arbitrary offset.
    `CodecOptions` | `getCodecOptions()` Retrieves the current set of codec options being used to decompress pixel data.
    `int[]` | `getColorMap(IFD ifd)` Retrieve the color map associated with the given IFD.
    `java.lang.String` | `getComment()` Convenience method for obtaining a stream's first ImageDescription.
    `IFDList` | `getExifIFDs()` Returns EXIF IFDs.
    `IFD` | `getFirstIFD()` Gets the first IFD within the TIFF file, or null if the input source is not a valid TIFF file.
    `TiffIFDEntry` | `getFirstIFDEntry(int tag)` Retrieve a given entry from the first IFD in the stream.
    `long` | `getFirstOffset()` Gets offset to the first IFD, or -1 if stream is not TIFF.
    `IFD` | `getIFD(long offset)` Gets the IFD stored at the given offset.
    `long[]` | `getIFDOffsets()` Gets the offsets to every IFD in the file.
    `IFDList` | `getIFDs()` Deprecated. Use `getMainIFDs()` and `getSubIFDs(IFD)` instead.
    `java.lang.Object` | `getIFDValue(TiffIFDEntry entry)` Retrieve the value corresponding to the given TiffIFDEntry.
    `IFDList` | `getMainIFDs()` Returns the main list of IFDs in the file.
    `(package private) long` | `getNextOffset(long previous)` Read a file offset.
    `IFDList` | `getNonThumbnailIFDs()` Returns non-thumbnail IFDs.
    `byte[]` | `getSamples(IFD ifd, byte[] buf)`
    `byte[]` | `getSamples(IFD ifd, byte[] buf, int x, int y, long width, long height)`
    `byte[]` | `getSamples(IFD ifd, byte[] buf, int x, int y, long width, long height, int overlapX, int overlapY)`
    `loci.common.RandomAccessInputStream` | `getStream()` Gets the stream from which TIFF data is being parsed.
    `IFDList` | `getSubIFDs(IFD ifd)` Returns the SUBIFDS belonging to a given IFD.
    `IFDList` | `getThumbnailIFDs()` Returns thumbnail IFDs.
    `byte[]` | `getTile(IFD ifd, byte[] buf, int row, int col)`
    `boolean` | `isBigTiff()` Returns whether or not the current TIFF file contains BigTIFF data.
    `boolean` | `isValidHeader()` Tests this stream to see if it represents a TIFF file.
    `(package private) TiffIFDEntry` | `readTiffIFDEntry()`
    `void` | `setAssumeEqualStrips(boolean equalStrips)` Sets whether or not to assume that strips are of equal size.
    `void` | `setCodecOptions(CodecOptions codecOptions)` Sets the codec options to be used when decompressing pixel data.
    `void` | `setDoCaching(boolean doCaching)` Sets whether or not IFD entries should be cached.
    `void` | `setEncoding(java.lang.String encoding)` Sets the encoding to use for tags with ASCII values.
    `void` | `setUse64BitOffsets(boolean use64Bit)` Sets whether or not 64-bit offsets are used for non-BigTIFF files.
    `void` | `setYCbCrCorrection(boolean correctionAllowed)` Sets whether or not YCbCr color correction is allowed.
    `static void` | `unpackBytes(byte[] samples, int startIndex, byte[] bytes, IFD ifd)` Extracts pixel information from the given byte array according to the bits per sample, photometric interpretation and color map IFD directory entry values, and the specified byte ordering.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### LOGGER

          private static final org.slf4j.Logger LOGGER

    - #### in

          protected transient loci.common.RandomAccessInputStream in

      Input source from which to parse TIFF data.

    - #### cachedTileBuffer

          private byte[] cachedTileBuffer

      Cached tile buffer to avoid re-allocations when reading tiles.

    - #### bigTiff

          private boolean bigTiff

      Whether or not the TIFF file contains BigTIFF data.

    - #### fakeBigTiff

          private boolean fakeBigTiff

      Whether or not 64-bit offsets are used for non-BigTIFF files.

    - #### ycbcrCorrection

          private boolean ycbcrCorrection

    - #### equalStrips

          private boolean equalStrips

    - #### doCaching

          private boolean doCaching

    - #### ifdList

          private IFDList ifdList

      Cached list of IFDs in the current file.

    - #### firstIFD

          private IFD firstIFD

      Cached first IFD in the current file.

    - #### codecOptions

          private CodecOptions codecOptions

      Codec options to be used when decoding compressed pixel data.

    - #### canClose

          private boolean canClose

    - #### tagEncoding

          private java.lang.String tagEncoding

  - ### Constructor Detail

    - #### TiffParser

          public TiffParser(java.lang.String filename)
                     throws java.io.IOException

      Constructs a new TIFF parser from the given file name.
      Throws:
        `java.io.IOException`

    - #### TiffParser

          public TiffParser(loci.common.RandomAccessInputStream in)

      Constructs a new TIFF parser from the given input source.

  - ### Method Detail

    - #### close

          public void close()
                     throws java.io.IOException

      Close the underlying stream, if this TiffParser was constructed with a file path. Closing a stream that was pre-initialized is potentially dangerous.
      Specified by:
        `close` in interface `java.io.Closeable`
      Specified by:
        `close` in interface `java.lang.AutoCloseable`
      Throws:
        `java.io.IOException`

    - #### setAssumeEqualStrips

          public void setAssumeEqualStrips(boolean equalStrips)

      Sets whether or not to assume that strips are of equal size.
      Parameters:
        `equalStrips` - Whether or not the strips are of equal size.

    - #### setCodecOptions

          public void setCodecOptions(CodecOptions codecOptions)

      Sets the codec options to be used when decompressing pixel data.
      Parameters:
        `codecOptions` - Codec options to use.

    - #### getCodecOptions

          public CodecOptions getCodecOptions()

      Retrieves the current set of codec options being used to decompress pixel data.
      Returns:
        See above.

    - #### setDoCaching

          public void setDoCaching(boolean doCaching)

      Sets whether or not IFD entries should be cached.

    - #### setUse64BitOffsets

          public void setUse64BitOffsets(boolean use64Bit)

      Sets whether or not 64-bit offsets are used for non-BigTIFF files.

    - #### setYCbCrCorrection

          public void setYCbCrCorrection(boolean correctionAllowed)

      Sets whether or not YCbCr color correction is allowed.

    - #### setEncoding

          public void setEncoding(java.lang.String encoding)

      Sets the encoding to use for tags with ASCII values. Defaults to UTF-8 if unset.

    - #### getStream

          public loci.common.RandomAccessInputStream getStream()

      Gets the stream from which TIFF data is being parsed.

    - #### isValidHeader

          public boolean isValidHeader()

      Tests this stream to see if it represents a TIFF file.

    - #### checkHeader

          public java.lang.Boolean checkHeader()
                                        throws java.io.IOException

      Checks the TIFF header.
      Returns:
        true if little-endian, false if big-endian, or null if not a TIFF.
      Throws:
        `java.io.IOException`

    - #### isBigTiff

          public boolean isBigTiff()

      Returns whether or not the current TIFF file contains BigTIFF data.

    - #### getMainIFDs

          public IFDList getMainIFDs()
                              throws java.io.IOException

      Returns the main list of IFDs in the file. This does not include SUBIFDS.
      Throws:
        `java.io.IOException`

    - #### getSubIFDs

          public IFDList getSubIFDs(IFD ifd)
                             throws java.io.IOException

      Returns the SUBIFDS belonging to a given IFD.
      Throws:
        `java.io.IOException`

    - #### getIFDs

          @Deprecated
          public IFDList getIFDs()
                                      throws java.io.IOException

      Deprecated. Use `getMainIFDs()` and `getSubIFDs(IFD)` instead.
      Returns all IFDs in the file, including SUBIFDS.
      Throws:
        `java.io.IOException`

    - #### getThumbnailIFDs

          public IFDList getThumbnailIFDs()
                                   throws java.io.IOException

      Returns thumbnail IFDs.
      Throws:
        `java.io.IOException`

    - #### getNonThumbnailIFDs

          public IFDList getNonThumbnailIFDs()
                                      throws java.io.IOException

      Returns non-thumbnail IFDs.
      Throws:
        `java.io.IOException`

    - #### getExifIFDs

          public IFDList getExifIFDs()
                              throws FormatException,
                                     java.io.IOException

      Returns EXIF IFDs.
      Throws:
        `FormatException`
        `java.io.IOException`

    - #### getIFDOffsets

          public long[] getIFDOffsets()
                               throws java.io.IOException

      Gets the offsets to every IFD in the file.
      Throws:
        `java.io.IOException`

    - #### getFirstIFD

          public IFD getFirstIFD()
                          throws java.io.IOException

      Gets the first IFD within the TIFF file, or null if the input source is not a valid TIFF file.
      Throws:
        `java.io.IOException`

    - #### getFirstIFDEntry

          public TiffIFDEntry getFirstIFDEntry(int tag)
                                        throws java.io.IOException

      Retrieve a given entry from the first IFD in the stream.
      Parameters:
        `tag` - the tag of the entry to be retrieved.
      Returns:
        an object representing the entry's fields.
      Throws:
        `java.io.IOException` - when there is an error accessing the stream.
        `java.lang.IllegalArgumentException` - when the tag number is unknown.

    - #### getFirstOffset

          public long getFirstOffset()
                              throws java.io.IOException

      Gets offset to the first IFD, or -1 if stream is not TIFF.
      Throws:
        `java.io.IOException`

    - #### getIFD

          public IFD getIFD(long offset)
                     throws java.io.IOException

      Gets the IFD stored at the given offset.
      Throws:
        `java.io.IOException`

    - #### fillInIFD

          public void fillInIFD(IFD ifd)
                         throws java.io.IOException

      Fill in IFD entries that are stored at an arbitrary offset.
      Throws:
        `java.io.IOException`

    - #### getIFDValue

          public java.lang.Object getIFDValue(TiffIFDEntry entry)
                                       throws java.io.IOException

      Retrieve the value corresponding to the given TiffIFDEntry.
      Throws:
        `java.io.IOException`

    - #### getComment

          public java.lang.String getComment()
                                      throws java.io.IOException

      Convenience method for obtaining a stream's first ImageDescription.
      Throws:
        `java.io.IOException`

    - #### getColorMap

          public int[] getColorMap(IFD ifd)
                            throws java.io.IOException

      Retrieve the color map associated with the given IFD.
      Throws:
        `java.io.IOException`

    - #### getTile

          public byte[] getTile(IFD ifd,
                                byte[] buf,
                                int row,
                                int col)
                         throws FormatException,
                                java.io.IOException

      Throws:
        `FormatException`
        `java.io.IOException`

    - #### getSamples

          public byte[] getSamples(IFD ifd,
                                   byte[] buf)
                            throws FormatException,
                                   java.io.IOException

      Throws:
        `FormatException`
        `java.io.IOException`

    - #### getSamples

          public byte[] getSamples(IFD ifd,
                                   byte[] buf,
                                   int x,
                                   int y,
                                   long width,
                                   long height)
                            throws FormatException,
                                   java.io.IOException

      Throws:
        `FormatException`
        `java.io.IOException`

    - #### getSamples

          public byte[] getSamples(IFD ifd,
                                   byte[] buf,
                                   int x,
                                   int y,
                                   long width,
                                   long height,
                                   int overlapX,
                                   int overlapY)
                            throws FormatException,
                                   java.io.IOException

      Throws:
        `FormatException`
        `java.io.IOException`

    - #### unpackBytes

          public static void unpackBytes(byte[] samples,
                                         int startIndex,
                                         byte[] bytes,
                                         IFD ifd)
                                  throws FormatException

      Extracts pixel information from the given byte array according to the bits per sample, photometric interpretation and color map IFD directory entry values, and the specified byte ordering. No error checking is performed.
      Throws:
        `FormatException`

    - #### getNextOffset

          long getNextOffset(long previous)
                      throws java.io.IOException

      Read a file offset. For bigTiff, a 64-bit number is read. For other Tiffs, a 32-bit number is read and possibly adjusted for a possible carry-over from the previous offset.
      Throws:
        `java.io.IOException`

    - #### readTiffIFDEntry

          TiffIFDEntry readTiffIFDEntry()
                                 throws java.io.IOException

      Throws:
        `java.io.IOException`

Copyright © 2026 Open Microscopy Environment