loci.formats
Class TiffTools

java.lang.Object
  extended by loci.formats.TiffTools

public final class TiffTools
extends Object

A utility class for manipulating TIFF files.

Source code:
Trac, SVN

Author:
Curtis Rueden ctrueden at wisc.edu, Eric Kjellman egkjellman at wisc.edu, Melissa Linkert linkert at wisc.edu, Chris Allan callan at blackcat.ca

Field Summary
static int ARTIST
           
static int ASCII
           
static int BIG
           
static int BIG_TIFF
           
static int BIG_TIFF_BYTES_PER_ENTRY
          The number of bytes in each IFD entry of a BigTIFF file.
static int BIG_TIFF_MAGIC_NUMBER
           
static int BITS_PER_SAMPLE
           
static int BLACK_IS_ZERO
           
static int BYTE
           
static int[] BYTES_PER_ELEMENT
           
static int BYTES_PER_ENTRY
          The number of bytes in each IFD entry.
static int CCITT_1D
           
static int CELL_LENGTH
           
static int CELL_WIDTH
           
static int CFA_ARRAY
           
static int CIE_LAB
           
static int CMYK
           
static int COLOR_MAP
           
static int COMPRESSION
           
static int COPYRIGHT
           
static int DATE_TIME
           
static int DEFLATE
           
static int DOCUMENT_NAME
           
static int DOT_RANGE
           
static int DOUBLE
           
static int EXTRA_SAMPLES
           
static int FILL_ORDER
           
static int FLOAT
           
static int FREE_BYTE_COUNTS
           
static int FREE_OFFSETS
           
static int GRAY_RESPONSE_CURVE
           
static int GRAY_RESPONSE_UNIT
           
static int GROUP_3_FAX
           
static int GROUP_4_FAX
           
static int HALFTONE_HINTS
           
static int HOST_COMPUTER
           
static int IFD8
           
static int IMAGE_DESCRIPTION
           
static int IMAGE_LENGTH
           
static int IMAGE_WIDTH
           
static int INK_NAMES
           
static int INK_SET
           
static int JPEG
           
static int JPEG_AC_TABLES
           
static int JPEG_DC_TABLES
           
static int JPEG_INTERCHANGE_FORMAT
           
static int JPEG_INTERCHANGE_FORMAT_LENGTH
           
static int JPEG_LOSSLESS_PREDICTORS
           
static int JPEG_POINT_TRANSFORMS
           
static int JPEG_PROC
           
static int JPEG_Q_TABLES
           
static int JPEG_RESTART_INTERVAL
           
static int LITTLE
           
static int LITTLE_ENDIAN
           
static int LONG
           
static int LONG8
           
static int LURAWAVE
           
static int LZW
           
static int MAGIC_NUMBER
           
static int MAKE
           
static int MAX_SAMPLE_VALUE
           
static int MIN_SAMPLE_VALUE
           
static int MODEL
           
static int NEW_SUBFILE_TYPE
           
static int NIKON
           
static int NUMBER_OF_INKS
           
static int ORIENTATION
           
static int PACK_BITS
           
static int PAGE_NAME
           
static int PAGE_NUMBER
           
static int PHOTOMETRIC_INTERPRETATION
           
static int PLANAR_CONFIGURATION
           
static int PREDICTOR
           
static int PRIMARY_CHROMATICITIES
           
static int PROPRIETARY_DEFLATE
           
static int RATIONAL
           
static int REFERENCE_BLACK_WHITE
           
static int RESOLUTION_UNIT
           
static int RGB
           
static int RGB_PALETTE
           
static int ROWS_PER_STRIP
           
static int S_MAX_SAMPLE_VALUE
           
static int S_MIN_SAMPLE_VALUE
           
static int SAMPLE_FORMAT
           
static int SAMPLES_PER_PIXEL
           
static int SBYTE
           
static int SHORT
           
static int SLONG
           
static int SLONG8
           
static int SOFTWARE
           
static int SRATIONAL
           
static int SSHORT
           
static int STRIP_BYTE_COUNTS
           
static int STRIP_OFFSETS
           
static int SUBFILE_TYPE
           
static int T4_OPTIONS
           
static int T6_OPTIONS
           
static int TARGET_PRINTER
           
static int THRESHHOLDING
           
static int THUNDERSCAN
           
static int TILE_BYTE_COUNTS
           
static int TILE_LENGTH
           
static int TILE_OFFSETS
           
static int TILE_WIDTH
           
static int TRANSFER_FUNCTION
           
static int TRANSFER_RANGE
           
static int TRANSPARENCY_MASK
           
static int UNCOMPRESSED
           
static int UNDEFINED
           
static int WHITE_IS_ZERO
           
static int WHITE_POINT
           
static int X_POSITION
           
static int X_RESOLUTION
           
static int Y_CB_CR
           
static int Y_CB_CR_COEFFICIENTS
           
static int Y_CB_CR_POSITIONING
           
static int Y_CB_CR_SUB_SAMPLING
           
static int Y_POSITION
           
static int Y_RESOLUTION
           
 
Method Summary
static Boolean checkHeader(byte[] block)
          Checks the TIFF header.
static Boolean checkHeader(RandomAccessStream in)
          Checks the TIFF header.
static byte[] compress(byte[] input, int compression)
          Encodes a strip of data with the given compression scheme.
static void debug(String message)
          Prints a debugging message with current time.
static void difference(byte[] input, int[] bitsPerSample, long width, int planarConfig, int predictor)
          Performs in-place differencing according to the given predictor value.
static int[] getBitsPerSample(Hashtable ifd)
          Retrieves the image's bits per sample (TIFF tag BitsPerSample) from a given TIFF IFD.
static String getComment(String id)
          Convenience method for obtaining a file's first ImageDescription.
static int getCompression(Hashtable ifd)
          Retrieves the image's compression type (TIFF tag Compression) from a given TIFF IFD.
static String getFieldName(int value)
          This method uses reflection to scan the values of this class's static fields, returning the first matching field's name.
static Hashtable getFirstIFD(RandomAccessStream in)
          Gets the first IFD within the given TIFF file, or null if the given file is not a valid TIFF file.
static TiffIFDEntry getFirstIFDEntry(RandomAccessStream in, int tag)
          Retrieve a given entry from the first IFD in a stream.
static long getFirstOffset(RandomAccessStream in)
          Gets offset to the first IFD, or -1 if stream is not TIFF.
static long getFirstOffset(RandomAccessStream in, boolean bigTiff)
          Gets offset to the first IFD, or -1 if stream is not TIFF.
static Hashtable getIFD(RandomAccessStream in, long ifdNum, long offset)
          Gets the IFD stored at the given offset.
static Hashtable getIFD(RandomAccessStream in, long ifdNum, long offset, boolean bigTiff)
          Gets the IFD stored at the given offset.
static int[] getIFDIntArray(Hashtable ifd, int tag, boolean checkNull)
          Gets the given directory entry values in int format from the specified IFD, performing some error checking.
static int getIFDIntValue(Hashtable ifd, int tag)
          Gets the given directory entry value in int format from the specified IFD, or -1 if the given directory does not exist.
static int getIFDIntValue(Hashtable ifd, int tag, boolean checkNull, int defaultValue)
          Gets the given directory entry value in int format from the specified IFD, performing some error checking.
static long[] getIFDLongArray(Hashtable ifd, int tag, boolean checkNull)
          Gets the given directory entry values in long format from the specified IFD, performing some error checking.
static long getIFDLongValue(Hashtable ifd, int tag, boolean checkNull, long defaultValue)
          Gets the given directory entry value in long format from the specified IFD, performing some error checking.
static TiffRational getIFDRationalValue(Hashtable ifd, int tag, boolean checkNull)
          Gets the given directory entry value in rational format from the specified IFD, performing some error checking.
static Hashtable[] getIFDs(RandomAccessStream in)
          Gets all IFDs within the given TIFF file, or null if the given file is not a valid TIFF file.
static short[] getIFDShortArray(Hashtable ifd, int tag, boolean checkNull)
          Gets the given directory entry values in short format from the specified IFD, performing some error checking.
static String getIFDTagName(int tag)
          Gets the name of the IFD tag encoded by the given number.
static String getIFDTypeName(int type)
          Gets the name of the IFD type encoded by the given number.
static Object getIFDValue(Hashtable ifd, int tag)
          Gets the given directory entry value from the specified IFD.
static Object getIFDValue(Hashtable ifd, int tag, boolean checkNull, Class checkClass)
          Gets the given directory entry value from the specified IFD, performing some error checking.
static BufferedImage getImage(Hashtable ifd, RandomAccessStream in)
          Reads the image defined in the given IFD from the specified file.
static long getImageLength(Hashtable ifd)
          Retrieves the image's length (TIFF tag ImageLength) from a given TIFF IFD.
static long getImageWidth(Hashtable ifd)
          Retrieves the image's width (TIFF tag ImageWidth) from a given TIFF IFD.
static int getPhotometricInterpretation(Hashtable ifd)
          Retrieves the image's photometric interpretation (TIFF tag PhotometricInterpretation) from a given TIFF IFD.
static long[] getRowsPerStrip(Hashtable ifd)
          Retrieves the number of rows per strip for image (TIFF tag RowsPerStrip) from a given TIFF IFD.
static byte[][] getSamples(Hashtable ifd, RandomAccessStream in)
          Reads the image defined in the given IFD from the specified file.
static byte[] getSamples(Hashtable ifd, RandomAccessStream in, byte[] buf)
           
static int getSamplesPerPixel(Hashtable ifd)
          Retrieves the number of samples per pixel for the image (TIFF tag SamplesPerPixel) from a given TIFF IFD.
static long[] getStripByteCounts(Hashtable ifd)
          Retrieves strip byte counts for the image (TIFF tag StripByteCounts) from a given TIFF IFD.
static long[] getStripOffsets(Hashtable ifd)
          Retrieves the strip offsets for the image (TIFF tag StripOffsets) from a given TIFF IFD.
static boolean isBigTiff(Hashtable ifd)
          Gets whether this is a BigTIFF IFD.
static boolean isLittleEndian(Hashtable ifd)
          Gets whether the TIFF information in the given IFD is little-endian.
static boolean isValidHeader(byte[] block)
          Tests the given data block to see if it represents the first few bytes of a TIFF file.
static void overwriteComment(String id, Object value)
          Convenience method for overwriting a file's first ImageDescription.
static void overwriteIFDValue(RandomAccessFile raf, int ifd, int tag, Object value)
          Surgically overwrites an existing IFD value with the given one.
static void planarUnpack(short[][] samples, int startIndex, byte[] bytes, int[] bitsPerSample, int photoInterp, boolean littleEndian, int strip, int numStrips)
          Extracts pixel information from the given byte array according to the bits per sample, photometric interpretation, and the specified byte ordering.
static void putIFDValue(Hashtable ifd, int tag, int value)
          Adds a directory entry of type SHORT to an IFD.
static void putIFDValue(Hashtable ifd, int tag, long value)
          Adds a directory entry of type LONG to an IFD.
static void putIFDValue(Hashtable ifd, int tag, Object value)
          Adds a directory entry to an IFD.
static void putIFDValue(Hashtable ifd, int tag, short value)
          Adds a directory entry of type BYTE to an IFD.
static byte[] uncompress(byte[] input, int compression)
          Decodes a strip of data compressed with the given compression scheme.
static void undifference(byte[] input, int[] bitsPerSample, long width, int planarConfig, int predictor)
          Undoes in-place differencing according to the given predictor value.
static void unpackBytes(short[][] samples, int startIndex, byte[] bytes, int[] bitsPerSample, int photoInterp, int[] colorMap, boolean littleEndian, long maxValue, int planar, int strip, int numStrips, long imageWidth)
          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.
static void writeIFDValue(DataOutput ifdOut, ByteArrayOutputStream extraBuf, DataOutputStream extraOut, int offset, int tag, Object value)
          Writes the given IFD value to the given output object.
static long writeImage(BufferedImage img, Hashtable ifd, OutputStream out, int offset, boolean last)
          Writes the given field to the specified output stream using the given byte offset and IFD, in big-endian format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BYTES_PER_ENTRY

public static final int BYTES_PER_ENTRY
The number of bytes in each IFD entry.

See Also:
Constant Field Values

BIG_TIFF_BYTES_PER_ENTRY

public static final int BIG_TIFF_BYTES_PER_ENTRY
The number of bytes in each IFD entry of a BigTIFF file.

See Also:
Constant Field Values

LITTLE_ENDIAN

public static final int LITTLE_ENDIAN
See Also:
Constant Field Values

BIG_TIFF

public static final int BIG_TIFF
See Also:
Constant Field Values

BYTE

public static final int BYTE
See Also:
Constant Field Values

ASCII

public static final int ASCII
See Also:
Constant Field Values

SHORT

public static final int SHORT
See Also:
Constant Field Values

LONG

public static final int LONG
See Also:
Constant Field Values

RATIONAL

public static final int RATIONAL
See Also:
Constant Field Values

SBYTE

public static final int SBYTE
See Also:
Constant Field Values

UNDEFINED

public static final int UNDEFINED
See Also:
Constant Field Values

SSHORT

public static final int SSHORT
See Also:
Constant Field Values

SLONG

public static final int SLONG
See Also:
Constant Field Values

SRATIONAL

public static final int SRATIONAL
See Also:
Constant Field Values

FLOAT

public static final int FLOAT
See Also:
Constant Field Values

DOUBLE

public static final int DOUBLE
See Also:
Constant Field Values

LONG8

public static final int LONG8
See Also:
Constant Field Values

SLONG8

public static final int SLONG8
See Also:
Constant Field Values

IFD8

public static final int IFD8
See Also:
Constant Field Values

BYTES_PER_ELEMENT

public static final int[] BYTES_PER_ELEMENT

NEW_SUBFILE_TYPE

public static final int NEW_SUBFILE_TYPE
See Also:
Constant Field Values

SUBFILE_TYPE

public static final int SUBFILE_TYPE
See Also:
Constant Field Values

IMAGE_WIDTH

public static final int IMAGE_WIDTH
See Also:
Constant Field Values

IMAGE_LENGTH

public static final int IMAGE_LENGTH
See Also:
Constant Field Values

BITS_PER_SAMPLE

public static final int BITS_PER_SAMPLE
See Also:
Constant Field Values

COMPRESSION

public static final int COMPRESSION
See Also:
Constant Field Values

PHOTOMETRIC_INTERPRETATION

public static final int PHOTOMETRIC_INTERPRETATION
See Also:
Constant Field Values

THRESHHOLDING

public static final int THRESHHOLDING
See Also:
Constant Field Values

CELL_WIDTH

public static final int CELL_WIDTH
See Also:
Constant Field Values

CELL_LENGTH

public static final int CELL_LENGTH
See Also:
Constant Field Values

FILL_ORDER

public static final int FILL_ORDER
See Also:
Constant Field Values

DOCUMENT_NAME

public static final int DOCUMENT_NAME
See Also:
Constant Field Values

IMAGE_DESCRIPTION

public static final int IMAGE_DESCRIPTION
See Also:
Constant Field Values

MAKE

public static final int MAKE
See Also:
Constant Field Values

MODEL

public static final int MODEL
See Also:
Constant Field Values

STRIP_OFFSETS

public static final int STRIP_OFFSETS
See Also:
Constant Field Values

ORIENTATION

public static final int ORIENTATION
See Also:
Constant Field Values

SAMPLES_PER_PIXEL

public static final int SAMPLES_PER_PIXEL
See Also:
Constant Field Values

ROWS_PER_STRIP

public static final int ROWS_PER_STRIP
See Also:
Constant Field Values

STRIP_BYTE_COUNTS

public static final int STRIP_BYTE_COUNTS
See Also:
Constant Field Values

MIN_SAMPLE_VALUE

public static final int MIN_SAMPLE_VALUE
See Also:
Constant Field Values

MAX_SAMPLE_VALUE

public static final int MAX_SAMPLE_VALUE
See Also:
Constant Field Values

X_RESOLUTION

public static final int X_RESOLUTION
See Also:
Constant Field Values

Y_RESOLUTION

public static final int Y_RESOLUTION
See Also:
Constant Field Values

PLANAR_CONFIGURATION

public static final int PLANAR_CONFIGURATION
See Also:
Constant Field Values

PAGE_NAME

public static final int PAGE_NAME
See Also:
Constant Field Values

X_POSITION

public static final int X_POSITION
See Also:
Constant Field Values

Y_POSITION

public static final int Y_POSITION
See Also:
Constant Field Values

FREE_OFFSETS

public static final int FREE_OFFSETS
See Also:
Constant Field Values

FREE_BYTE_COUNTS

public static final int FREE_BYTE_COUNTS
See Also:
Constant Field Values

GRAY_RESPONSE_UNIT

public static final int GRAY_RESPONSE_UNIT
See Also:
Constant Field Values

GRAY_RESPONSE_CURVE

public static final int GRAY_RESPONSE_CURVE
See Also:
Constant Field Values

T4_OPTIONS

public static final int T4_OPTIONS
See Also:
Constant Field Values

T6_OPTIONS

public static final int T6_OPTIONS
See Also:
Constant Field Values

RESOLUTION_UNIT

public static final int RESOLUTION_UNIT
See Also:
Constant Field Values

PAGE_NUMBER

public static final int PAGE_NUMBER
See Also:
Constant Field Values

TRANSFER_FUNCTION

public static final int TRANSFER_FUNCTION
See Also:
Constant Field Values

SOFTWARE

public static final int SOFTWARE
See Also:
Constant Field Values

DATE_TIME

public static final int DATE_TIME
See Also:
Constant Field Values

ARTIST

public static final int ARTIST
See Also:
Constant Field Values

HOST_COMPUTER

public static final int HOST_COMPUTER
See Also:
Constant Field Values

PREDICTOR

public static final int PREDICTOR
See Also:
Constant Field Values

WHITE_POINT

public static final int WHITE_POINT
See Also:
Constant Field Values

PRIMARY_CHROMATICITIES

public static final int PRIMARY_CHROMATICITIES
See Also:
Constant Field Values

COLOR_MAP

public static final int COLOR_MAP
See Also:
Constant Field Values

HALFTONE_HINTS

public static final int HALFTONE_HINTS
See Also:
Constant Field Values

TILE_WIDTH

public static final int TILE_WIDTH
See Also:
Constant Field Values

TILE_LENGTH

public static final int TILE_LENGTH
See Also:
Constant Field Values

TILE_OFFSETS

public static final int TILE_OFFSETS
See Also:
Constant Field Values

TILE_BYTE_COUNTS

public static final int TILE_BYTE_COUNTS
See Also:
Constant Field Values

INK_SET

public static final int INK_SET
See Also:
Constant Field Values

INK_NAMES

public static final int INK_NAMES
See Also:
Constant Field Values

NUMBER_OF_INKS

public static final int NUMBER_OF_INKS
See Also:
Constant Field Values

DOT_RANGE

public static final int DOT_RANGE
See Also:
Constant Field Values

TARGET_PRINTER

public static final int TARGET_PRINTER
See Also:
Constant Field Values

EXTRA_SAMPLES

public static final int EXTRA_SAMPLES
See Also:
Constant Field Values

SAMPLE_FORMAT

public static final int SAMPLE_FORMAT
See Also:
Constant Field Values

S_MIN_SAMPLE_VALUE

public static final int S_MIN_SAMPLE_VALUE
See Also:
Constant Field Values

S_MAX_SAMPLE_VALUE

public static final int S_MAX_SAMPLE_VALUE
See Also:
Constant Field Values

TRANSFER_RANGE

public static final int TRANSFER_RANGE
See Also:
Constant Field Values

JPEG_PROC

public static final int JPEG_PROC
See Also:
Constant Field Values

JPEG_INTERCHANGE_FORMAT

public static final int JPEG_INTERCHANGE_FORMAT
See Also:
Constant Field Values

JPEG_INTERCHANGE_FORMAT_LENGTH

public static final int JPEG_INTERCHANGE_FORMAT_LENGTH
See Also:
Constant Field Values

JPEG_RESTART_INTERVAL

public static final int JPEG_RESTART_INTERVAL
See Also:
Constant Field Values

JPEG_LOSSLESS_PREDICTORS

public static final int JPEG_LOSSLESS_PREDICTORS
See Also:
Constant Field Values

JPEG_POINT_TRANSFORMS

public static final int JPEG_POINT_TRANSFORMS
See Also:
Constant Field Values

JPEG_Q_TABLES

public static final int JPEG_Q_TABLES
See Also:
Constant Field Values

JPEG_DC_TABLES

public static final int JPEG_DC_TABLES
See Also:
Constant Field Values

JPEG_AC_TABLES

public static final int JPEG_AC_TABLES
See Also:
Constant Field Values

Y_CB_CR_COEFFICIENTS

public static final int Y_CB_CR_COEFFICIENTS
See Also:
Constant Field Values

Y_CB_CR_SUB_SAMPLING

public static final int Y_CB_CR_SUB_SAMPLING
See Also:
Constant Field Values

Y_CB_CR_POSITIONING

public static final int Y_CB_CR_POSITIONING
See Also:
Constant Field Values

REFERENCE_BLACK_WHITE

public static final int REFERENCE_BLACK_WHITE
See Also:
Constant Field Values

COPYRIGHT

public static final int COPYRIGHT
See Also:
Constant Field Values

UNCOMPRESSED

public static final int UNCOMPRESSED
See Also:
Constant Field Values

CCITT_1D

public static final int CCITT_1D
See Also:
Constant Field Values

GROUP_3_FAX

public static final int GROUP_3_FAX
See Also:
Constant Field Values

GROUP_4_FAX

public static final int GROUP_4_FAX
See Also:
Constant Field Values

LZW

public static final int LZW
See Also:
Constant Field Values

JPEG

public static final int JPEG
See Also:
Constant Field Values

PACK_BITS

public static final int PACK_BITS
See Also:
Constant Field Values

PROPRIETARY_DEFLATE

public static final int PROPRIETARY_DEFLATE
See Also:
Constant Field Values

DEFLATE

public static final int DEFLATE
See Also:
Constant Field Values

THUNDERSCAN

public static final int THUNDERSCAN
See Also:
Constant Field Values

NIKON

public static final int NIKON
See Also:
Constant Field Values

LURAWAVE

public static final int LURAWAVE
See Also:
Constant Field Values

WHITE_IS_ZERO

public static final int WHITE_IS_ZERO
See Also:
Constant Field Values

BLACK_IS_ZERO

public static final int BLACK_IS_ZERO
See Also:
Constant Field Values

RGB

public static final int RGB
See Also:
Constant Field Values

RGB_PALETTE

public static final int RGB_PALETTE
See Also:
Constant Field Values

TRANSPARENCY_MASK

public static final int TRANSPARENCY_MASK
See Also:
Constant Field Values

CMYK

public static final int CMYK
See Also:
Constant Field Values

Y_CB_CR

public static final int Y_CB_CR
See Also:
Constant Field Values

CIE_LAB

public static final int CIE_LAB
See Also:
Constant Field Values

CFA_ARRAY

public static final int CFA_ARRAY
See Also:
Constant Field Values

MAGIC_NUMBER

public static final int MAGIC_NUMBER
See Also:
Constant Field Values

BIG_TIFF_MAGIC_NUMBER

public static final int BIG_TIFF_MAGIC_NUMBER
See Also:
Constant Field Values

LITTLE

public static final int LITTLE
See Also:
Constant Field Values

BIG

public static final int BIG
See Also:
Constant Field Values
Method Detail

isValidHeader

public static boolean isValidHeader(byte[] block)
Tests the given data block to see if it represents the first few bytes of a TIFF file.


checkHeader

public static Boolean checkHeader(byte[] block)
Checks the TIFF header.

Returns:
true if little-endian, false if big-endian, or null if not a TIFF.

isBigTiff

public static boolean isBigTiff(Hashtable ifd)
                         throws FormatException
Gets whether this is a BigTIFF IFD.

Throws:
FormatException

isLittleEndian

public static boolean isLittleEndian(Hashtable ifd)
                              throws FormatException
Gets whether the TIFF information in the given IFD is little-endian.

Throws:
FormatException

getIFDs

public static Hashtable[] getIFDs(RandomAccessStream in)
                           throws IOException
Gets all IFDs within the given TIFF file, or null if the given file is not a valid TIFF file.

Throws:
IOException

getFirstIFD

public static Hashtable getFirstIFD(RandomAccessStream in)
                             throws IOException
Gets the first IFD within the given TIFF file, or null if the given file is not a valid TIFF file.

Throws:
IOException

getFirstIFDEntry

public static TiffIFDEntry getFirstIFDEntry(RandomAccessStream in,
                                            int tag)
                                     throws IOException
Retrieve a given entry from the first IFD in a stream.

Parameters:
in - the stream to retrieve the entry from.
tag - the tag of the entry to be retrieved.
Returns:
an object representing the entry's fields.
Throws:
IOException - when there is an error accessing the stream in.

checkHeader

public static Boolean checkHeader(RandomAccessStream in)
                           throws IOException
Checks the TIFF header.

Returns:
true if little-endian, false if big-endian, or null if not a TIFF.
Throws:
IOException

getFirstOffset

public static long getFirstOffset(RandomAccessStream in)
                           throws IOException
Gets offset to the first IFD, or -1 if stream is not TIFF. Assumes the stream is positioned properly (checkHeader just called).

Throws:
IOException

getFirstOffset

public static long getFirstOffset(RandomAccessStream in,
                                  boolean bigTiff)
                           throws IOException
Gets offset to the first IFD, or -1 if stream is not TIFF. Assumes the stream is positioned properly (checkHeader just called).

Parameters:
bigTiff - true if this is a BigTIFF file (8 byte pointers).
Throws:
IOException

getIFD

public static Hashtable getIFD(RandomAccessStream in,
                               long ifdNum,
                               long offset)
                        throws IOException
Gets the IFD stored at the given offset.

Throws:
IOException

getIFD

public static Hashtable getIFD(RandomAccessStream in,
                               long ifdNum,
                               long offset,
                               boolean bigTiff)
                        throws IOException
Gets the IFD stored at the given offset.

Throws:
IOException

getIFDTagName

public static String getIFDTagName(int tag)
Gets the name of the IFD tag encoded by the given number.


getIFDTypeName

public static String getIFDTypeName(int type)
Gets the name of the IFD type encoded by the given number.


getFieldName

public static String getFieldName(int value)
This method uses reflection to scan the values of this class's static fields, returning the first matching field's name. It is probably not very efficient, and is mainly intended for debugging.


getIFDValue

public static Object getIFDValue(Hashtable ifd,
                                 int tag)
Gets the given directory entry value from the specified IFD.


getIFDValue

public static Object getIFDValue(Hashtable ifd,
                                 int tag,
                                 boolean checkNull,
                                 Class checkClass)
                          throws FormatException
Gets the given directory entry value from the specified IFD, performing some error checking.

Throws:
FormatException

getIFDLongValue

public static long getIFDLongValue(Hashtable ifd,
                                   int tag,
                                   boolean checkNull,
                                   long defaultValue)
                            throws FormatException
Gets the given directory entry value in long format from the specified IFD, performing some error checking.

Throws:
FormatException

getIFDIntValue

public static int getIFDIntValue(Hashtable ifd,
                                 int tag)
Gets the given directory entry value in int format from the specified IFD, or -1 if the given directory does not exist.


getIFDIntValue

public static int getIFDIntValue(Hashtable ifd,
                                 int tag,
                                 boolean checkNull,
                                 int defaultValue)
                          throws FormatException
Gets the given directory entry value in int format from the specified IFD, performing some error checking.

Throws:
FormatException

getIFDRationalValue

public static TiffRational getIFDRationalValue(Hashtable ifd,
                                               int tag,
                                               boolean checkNull)
                                        throws FormatException
Gets the given directory entry value in rational format from the specified IFD, performing some error checking.

Throws:
FormatException

getIFDLongArray

public static long[] getIFDLongArray(Hashtable ifd,
                                     int tag,
                                     boolean checkNull)
                              throws FormatException
Gets the given directory entry values in long format from the specified IFD, performing some error checking.

Throws:
FormatException

getIFDIntArray

public static int[] getIFDIntArray(Hashtable ifd,
                                   int tag,
                                   boolean checkNull)
                            throws FormatException
Gets the given directory entry values in int format from the specified IFD, performing some error checking.

Throws:
FormatException

getIFDShortArray

public static short[] getIFDShortArray(Hashtable ifd,
                                       int tag,
                                       boolean checkNull)
                                throws FormatException
Gets the given directory entry values in short format from the specified IFD, performing some error checking.

Throws:
FormatException

getComment

public static String getComment(String id)
                         throws FormatException,
                                IOException
Convenience method for obtaining a file's first ImageDescription.

Throws:
FormatException
IOException

getSamples

public static byte[][] getSamples(Hashtable ifd,
                                  RandomAccessStream in)
                           throws FormatException,
                                  IOException
Reads the image defined in the given IFD from the specified file.

Throws:
FormatException
IOException

getSamples

public static byte[] getSamples(Hashtable ifd,
                                RandomAccessStream in,
                                byte[] buf)
                         throws FormatException,
                                IOException
Throws:
FormatException
IOException

getImage

public static BufferedImage getImage(Hashtable ifd,
                                     RandomAccessStream in)
                              throws FormatException,
                                     IOException
Reads the image defined in the given IFD from the specified file.

Throws:
FormatException
IOException

planarUnpack

public static void planarUnpack(short[][] samples,
                                int startIndex,
                                byte[] bytes,
                                int[] bitsPerSample,
                                int photoInterp,
                                boolean littleEndian,
                                int strip,
                                int numStrips)
                         throws FormatException
Extracts pixel information from the given byte array according to the bits per sample, photometric interpretation, and the specified byte ordering. No error checking is performed. This method is tailored specifically for planar (separated) images.

Throws:
FormatException

unpackBytes

public static void unpackBytes(short[][] samples,
                               int startIndex,
                               byte[] bytes,
                               int[] bitsPerSample,
                               int photoInterp,
                               int[] colorMap,
                               boolean littleEndian,
                               long maxValue,
                               int planar,
                               int strip,
                               int numStrips,
                               long imageWidth)
                        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

uncompress

public static byte[] uncompress(byte[] input,
                                int compression)
                         throws FormatException,
                                IOException
Decodes a strip of data compressed with the given compression scheme.

Throws:
FormatException
IOException

undifference

public static void undifference(byte[] input,
                                int[] bitsPerSample,
                                long width,
                                int planarConfig,
                                int predictor)
                         throws FormatException
Undoes in-place differencing according to the given predictor value.

Throws:
FormatException

putIFDValue

public static void putIFDValue(Hashtable ifd,
                               int tag,
                               Object value)
Adds a directory entry to an IFD.


putIFDValue

public static void putIFDValue(Hashtable ifd,
                               int tag,
                               short value)
Adds a directory entry of type BYTE to an IFD.


putIFDValue

public static void putIFDValue(Hashtable ifd,
                               int tag,
                               int value)
Adds a directory entry of type SHORT to an IFD.


putIFDValue

public static void putIFDValue(Hashtable ifd,
                               int tag,
                               long value)
Adds a directory entry of type LONG to an IFD.


writeIFDValue

public static void writeIFDValue(DataOutput ifdOut,
                                 ByteArrayOutputStream extraBuf,
                                 DataOutputStream extraOut,
                                 int offset,
                                 int tag,
                                 Object value)
                          throws FormatException,
                                 IOException
Writes the given IFD value to the given output object.

Parameters:
ifdOut - output object for writing IFD stream
extraBuf - buffer to which "extra" IFD information should be written
extraOut - data output wrapper for extraBuf (passed for efficiency)
offset - global offset to use for IFD offset values
tag - IFD tag to write
value - IFD value to write
Throws:
FormatException
IOException

overwriteIFDValue

public static void overwriteIFDValue(RandomAccessFile 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.

Throws:
FormatException
IOException

overwriteComment

public static void overwriteComment(String id,
                                    Object value)
                             throws FormatException,
                                    IOException
Convenience method for overwriting a file's first ImageDescription.

Throws:
FormatException
IOException

writeImage

public static long writeImage(BufferedImage img,
                              Hashtable ifd,
                              OutputStream out,
                              int offset,
                              boolean last)
                       throws FormatException,
                              IOException
Writes the given field to the specified output stream using the given byte offset and IFD, in big-endian format.

Parameters:
img - The field to write
ifd - Hashtable representing the TIFF IFD; can be null
out - The output stream to which the TIFF data should be written
offset - The value to use for specifying byte offsets
last - Whether this image is the final IFD entry of the TIFF data
Returns:
total number of bytes written
Throws:
FormatException
IOException

getImageWidth

public static long getImageWidth(Hashtable ifd)
                          throws FormatException
Retrieves the image's width (TIFF tag ImageWidth) from a given TIFF IFD.

Parameters:
ifd - a TIFF IFD hashtable.
Returns:
the image's width.
Throws:
FormatException - if there is a problem parsing the IFD metadata.

getImageLength

public static long getImageLength(Hashtable ifd)
                           throws FormatException
Retrieves the image's length (TIFF tag ImageLength) from a given TIFF IFD.

Parameters:
ifd - a TIFF IFD hashtable.
Returns:
the image's length.
Throws:
FormatException - if there is a problem parsing the IFD metadata.

getBitsPerSample

public static int[] getBitsPerSample(Hashtable ifd)
                              throws FormatException
Retrieves the image's bits per sample (TIFF tag BitsPerSample) from a given TIFF IFD.

Parameters:
ifd - a TIFF IFD hashtable.
Returns:
the image's bits per sample. The length of the array is equal to the number of samples per pixel.
Throws:
FormatException - if there is a problem parsing the IFD metadata.
See Also:
getSamplesPerPixel(Hashtable)

getSamplesPerPixel

public static int getSamplesPerPixel(Hashtable ifd)
                              throws FormatException
Retrieves the number of samples per pixel for the image (TIFF tag SamplesPerPixel) from a given TIFF IFD.

Parameters:
ifd - a TIFF IFD hashtable.
Returns:
the number of samples per pixel.
Throws:
FormatException - if there is a problem parsing the IFD metadata.

getCompression

public static int getCompression(Hashtable ifd)
                          throws FormatException
Retrieves the image's compression type (TIFF tag Compression) from a given TIFF IFD.

Parameters:
ifd - a TIFF IFD hashtable.
Returns:
the image's compression type. As of TIFF 6.0 this is one of:
  • Uncompressed (1)
  • CCITT 1D (2)
  • Group 3 Fax (3)
  • Group 4 Fax (4)
  • LZW (5)
  • JPEG (6)
  • PackBits (32773)
Throws:
FormatException - if there is a problem parsing the IFD metadata.

getPhotometricInterpretation

public static int getPhotometricInterpretation(Hashtable ifd)
                                        throws FormatException
Retrieves the image's photometric interpretation (TIFF tag PhotometricInterpretation) from a given TIFF IFD.

Parameters:
ifd - a TIFF IFD hashtable.
Returns:
the image's photometric interpretation. As of TIFF 6.0 this is one of:
  • WhiteIsZero (0)
  • BlackIsZero (1)
  • RGB (2)
  • RGB Palette (3)
  • Transparency mask (4)
  • CMYK (5)
  • YbCbCr (6)
  • CIELab (8)
Throws:
FormatException - if there is a problem parsing the IFD metadata.

getStripOffsets

public static long[] getStripOffsets(Hashtable ifd)
                              throws FormatException
Retrieves the strip offsets for the image (TIFF tag StripOffsets) from a given TIFF IFD.

Parameters:
ifd - a TIFF IFD hashtable.
Returns:
the strip offsets for the image. The lenght of the array is equal to the number of strips per image. StripsPerImage = floor ((ImageLength + RowsPerStrip - 1) / RowsPerStrip).
Throws:
FormatException - if there is a problem parsing the IFD metadata.
See Also:
getStripByteCounts(Hashtable), getRowsPerStrip(Hashtable)

getStripByteCounts

public static long[] getStripByteCounts(Hashtable ifd)
                                 throws FormatException
Retrieves strip byte counts for the image (TIFF tag StripByteCounts) from a given TIFF IFD.

Parameters:
ifd - a TIFF IFD hashtable.
Returns:
the byte counts for each strip. The length of the array is equal to the number of strips per image. StripsPerImage = floor((ImageLength + RowsPerStrip - 1) / RowsPerStrip).
Throws:
FormatException - if there is a problem parsing the IFD metadata.
See Also:
getStripOffsets(Hashtable)

getRowsPerStrip

public static long[] getRowsPerStrip(Hashtable ifd)
                              throws FormatException
Retrieves the number of rows per strip for image (TIFF tag RowsPerStrip) from a given TIFF IFD.

Parameters:
ifd - a TIFF IFD hashtable.
Returns:
the number of rows per strip.
Throws:
FormatException - if there is a problem parsing the IFD metadata.

compress

public static byte[] compress(byte[] input,
                              int compression)
                       throws FormatException,
                              IOException
Encodes a strip of data with the given compression scheme.

Throws:
FormatException
IOException

difference

public static void difference(byte[] input,
                              int[] bitsPerSample,
                              long width,
                              int planarConfig,
                              int predictor)
                       throws FormatException
Performs in-place differencing according to the given predictor value.

Throws:
FormatException

debug

public static void debug(String message)
Prints a debugging message with current time.