public class TIFFCodec extends ImageCodec implements TIFFConstants
TIFFCodec codec = new TIFFCodec(); codec.setFile("image.tif", CodecMode.LOAD); codec.process(); PixelImage loadedImage = codec.getImage();Saving images is not supported by this codec.
TIFFDecoderUncompressed
.TIFFDecoderPackbits
.TIFFDecoderModifiedHuffman
.TIFFDecoderDeflated
.TIFFDecoderLogLuv
.
Note that you can write your own decoder (extending TIFFDecoder
) for any compression type
you want.
BilevelImage
.Gray8Image
.
Note that you can write your own decoder (extending TIFFDecoder
) for any compression type
you want.
Writing TIFFs is not supported. I don't know if or when it will be supported.
Later, the concept of tiles was added to the TIFF specs. Tiled TIFFs are separated into rectangles that not only had a defineable height but also a defineable width (tile width and tile height are also stored in corresponding tags).
Obviously, strips are just a special case of tiles, with the tile width being equal to image width. That is why JIU internally only deals with tiles. The only difference: No row padding takes place for strips. In a tiled image with a tile height of 10 and an image height of 14, the image is two tiles high.
ImageCodec.getImageIndex()
and skips to the
correct image.
ImageCodec.getDpiX()
and ImageCodec.getDpiY()
.
TIFF was created by Aldus and now belongs to Adobe, who offer a specification document: TIFF (Tagged Image File Format) 6.0 Specification (updated on Web September, 20 1995, document dated June, 3 1992) (PDF: 385 KB / 121 pages).
Other good references include the homepage of libtiff, a free C library to read and write TIFF files and The Unofficial TIFF homepage by Niles Ritter. Also see the TIFF section of the Open Directory.
TIFF is used for various specialized tasks. As an example, see GeoTIFF (geographical data) or EXIF (digital camera metadata; this is actually a TIFF directory embedded in a JPEG header).
Here's a list of features that make TIFF quite complex:
Modifier and Type | Field and Description |
---|---|
static int |
BYTE_ORDER_INTEL |
static int |
BYTE_ORDER_MOTOROLA |
private int |
byteOrder |
private static java.util.Hashtable |
decoders |
private static int |
MAGIC_INTEL |
private static int |
MAGIC_MOTOROLA |
private int |
nextIfdOffset |
COMPRESSION_CCITT_GROUP3_1D_MODIFIED_HUFFMAN, COMPRESSION_CCITT_T4, COMPRESSION_CCITT_T6, COMPRESSION_DEFLATED_INOFFICIAL, COMPRESSION_DEFLATED_OFFICIAL, COMPRESSION_JBIG, COMPRESSION_JBIG2, COMPRESSION_JPEG_6_0, COMPRESSION_JPEG_POST_6_0, COMPRESSION_LZW, COMPRESSION_NEXT, COMPRESSION_NONE, COMPRESSION_NONE_WORD_ALIGNED, COMPRESSION_PACKBITS, COMPRESSION_SGI_LOG_24_PACKED, COMPRESSION_SGI_LOG_RLE, COMPRESSION_THUNDERSCAN, PHOTOMETRIC_BLACK_IS_ZERO, PHOTOMETRIC_LOGL, PHOTOMETRIC_PALETTED, PHOTOMETRIC_TRUECOLOR_CMYK, PHOTOMETRIC_TRUECOLOR_LOGLUV, PHOTOMETRIC_TRUECOLOR_RGB, PHOTOMETRIC_WHITE_IS_ZERO, PLANAR_CONFIGURATION_CHUNKY, PLANAR_CONFIGURATION_PLANAR, TAG_ARTIST, TAG_BAD_FAX_LINES, TAG_BITS_PER_SAMPLE, TAG_CELL_LENGTH, TAG_CELL_WIDTH, TAG_CLEAN_FAX_DATA, TAG_COLOR_MAP, TAG_COMPRESSION, TAG_CONSECUTIVE_BAD_FAX_LINES, TAG_COPYRIGHT, TAG_DATE_TIME, TAG_DOCUMENT_NAME, TAG_EXTRA_SAMPLES, TAG_FILL_ORDER, TAG_FREE_BYTE_COUNTS, TAG_FREE_OFFSETS, TAG_GRAY_RESPONSE_CURVE, TAG_GRAY_RESPONSE_UNIT, TAG_HOST_COMPUTER, TAG_IMAGE_DESCRIPTION, TAG_IMAGE_LENGTH, TAG_IMAGE_WIDTH, TAG_LENGTH, TAG_MAKE, TAG_MAX_SAMPLE_VALUE, TAG_MIN_SAMPLE_VALUE, TAG_MODEL, TAG_NEW_SUBFILE_TYPE, TAG_ORIENTATION, TAG_PHOTOMETRIC_INTERPRETATION, TAG_PHOTOSHOP_IMAGE_RESOURCES, TAG_PLANAR_CONFIGURATION, TAG_PREDICTOR, TAG_RESOLUTION_UNIT, TAG_RESOLUTION_X, TAG_RESOLUTION_Y, TAG_ROWS_PER_STRIP, TAG_SAMPLES_PER_PIXEL, TAG_SOFTWARE, TAG_STRIP_BYTE_COUNTS, TAG_STRIP_OFFSETS, TAG_T4_OPTIONS, TAG_T6_OPTIONS, TAG_TILE_BYTE_COUNTS, TAG_TILE_HEIGHT, TAG_TILE_OFFSETS, TAG_TILE_WIDTH, TAG_TYPE_ASCII, TAG_TYPE_BYTE, TAG_TYPE_DOUBLE, TAG_TYPE_FLOAT, TAG_TYPE_LONG, TAG_TYPE_RATIONAL, TAG_TYPE_SBYTE, TAG_TYPE_SHORT, TAG_TYPE_SLONG, TAG_TYPE_SRATIONAL, TAG_TYPE_SSHORT, TAG_TYPE_UNDEFINED
Constructor and Description |
---|
TIFFCodec() |
Modifier and Type | Method and Description |
---|---|
private int |
adjustInt(int value,
int type)
If the current byte order is
BYTE_ORDER_MOTOROLA and the type
argument is TiffConstants.TAG_TYPE_BYTE or
TiffConstants.TAG_TYPE_SHORT , the value parameter must
be adjusted by some bitshifting. |
private static TIFFDecoder |
createDecoder(TIFFCodec codec,
TIFFImageFileDirectory ifd,
int tileIndex) |
int |
getByteOrder()
Returns the current byte order, either
BYTE_ORDER_INTEL or
BYTE_ORDER_MOTOROLA . |
java.lang.String |
getFormatName()
Returns the name of the file format supported by this codec.
|
java.lang.String[] |
getMimeTypes()
Return the MIME
(Multipurpose Internet Mail Extensions) type strings for this format, or
null
if none are available. |
static java.lang.String |
getTagName(int id)
Returns the name of a tag in English.
|
boolean |
isLoadingSupported()
Returns if this codec is able to load images in the file format supported by this codec.
|
boolean |
isSavingSupported()
Returns if this codec is able to save images in the file format supported by this codec.
|
private void |
load()
Attempts to load an image from a file in the TIFF format.
|
private void |
load(TIFFImageFileDirectory ifd) |
void |
process()
This method does the actual work of the operation.
|
private void |
readHeader()
Reads the first eight bytes from the input file, checks if this is a
valid TIFF file and stores byte order and offset of the first image
file directory.
|
private TIFFImageFileDirectory |
readImageFileDirectory()
Reads a complete TIFF image file directory including all data that is
pointed to using the offset components and returns it.
|
private int |
readInt()
Reads a 32 bit signed integer value, regarding the current byte order.
|
private short |
readShort()
Reads a 16 bit signed integer value, regarding the current byte order.
|
private java.lang.String |
readString(int length)
Loads a String of a given length from current position of input file.
|
private TIFFTag |
readTag()
Reads a TIFF tag and all data belonging to it and returns a
TIFFTag object.
|
static void |
registerDecoder(java.lang.Class decoderClass)
Register a
TIFFDecoder class. |
private void |
setByteOrder(int newByteOrder)
Sets the byte order to the argument.
|
void |
setFile(java.lang.String fileName,
CodecMode codecMode)
Gives a file name and codec mode to the codec which will then
try to create the corresponding I/O object.
|
private void |
skipImageFileDirectories(int numDirectories)
Skips a given number of image file directories in this TIFF files.
|
appendComment, checkBounds, checkImageResolution, close, getBoundsHeight, getBoundsWidth, getBoundsX1, getBoundsX2, getBoundsY1, getBoundsY2, getComment, getDataInput, getDataOutput, getDpiX, getDpiY, getFileExtensions, getImage, getImageIndex, getInputAsDataInput, getInputStream, getMode, getNumComments, getOutputAsDataOutput, getOutputStream, getRandomAccessFile, hasBounds, initModeFromIOObjects, isRowRequired, isTileRequired, removeAllComments, removeBounds, setBounds, setBoundsIfNecessary, setDataInput, setDataOutput, setDpi, setFile, setImage, setImageIndex, setInputStream, setOutputStream, setRandomAccessFile, suggestFileExtension
addProgressListener, addProgressListeners, getAbort, removeProgressListener, setAbort, setProgress, setProgress
public static final int BYTE_ORDER_MOTOROLA
public static final int BYTE_ORDER_INTEL
private static final int MAGIC_INTEL
private static final int MAGIC_MOTOROLA
private int byteOrder
private int nextIfdOffset
private static java.util.Hashtable decoders
private int adjustInt(int value, int type)
BYTE_ORDER_MOTOROLA
and the type
argument is TiffConstants.TAG_TYPE_BYTE
or
TiffConstants.TAG_TYPE_SHORT
, the value parameter must
be adjusted by some bitshifting.
If the above mentioned criteria are not met, the value argument is
returned without any modifications.
Why this is necessary remains a mystery to me. Marco
value
- the int value which may have to be adjustedprivate static TIFFDecoder createDecoder(TIFFCodec codec, TIFFImageFileDirectory ifd, int tileIndex) throws java.io.IOException, UnsupportedTypeException
java.io.IOException
UnsupportedTypeException
public int getByteOrder()
BYTE_ORDER_INTEL
or
BYTE_ORDER_MOTOROLA
.public java.lang.String getFormatName()
ImageCodec
ImageCodec
must override this method.
When overriding, leave out any words in a particular language so
that this format name can be understood by everyone.
Usually it is enough to return the format creator plus a typical
abbreviation, e.g. Microsoft BMP
or Portable Anymap (PNM)
.getFormatName
in class ImageCodec
public java.lang.String[] getMimeTypes()
ImageCodec
null
if none are available.getMimeTypes
in class ImageCodec
public static java.lang.String getTagName(int id)
id
- of the tag for which a name is to be returned?
public boolean isLoadingSupported()
ImageCodec
true
is returned this does not necessarily mean that all files in this
format can be read, but at least some.isLoadingSupported
in class ImageCodec
public boolean isSavingSupported()
ImageCodec
true
is returned this does not necessarily mean that all types files in this
format can be written, but at least some.isSavingSupported
in class ImageCodec
private void load() throws InvalidFileStructureException, java.io.IOException, UnsupportedTypeException, WrongFileFormatException, WrongParameterException
RandomAccessFile
using
#setInput(java.io.RandomAccessFile)
.ImageCodec.setImageIndex(int)
.
The argument is the index of the image, the first being 0
,
the second 1
and so on. The default is 0
.InvalidFileStructureException
- if the TIFF file was corrupt in some wayjava.io.IOException
- if there were errors reading from the input fileUnsupportedTypeException
- if the flavour of TIFF encountered in the input
file is not supported yetWrongFileFormatException
WrongParameterException
private void load(TIFFImageFileDirectory ifd) throws InvalidFileStructureException, java.io.IOException, UnsupportedTypeException, WrongFileFormatException, WrongParameterException
InvalidFileStructureException
java.io.IOException
UnsupportedTypeException
WrongFileFormatException
WrongParameterException
public void process() throws MissingParameterException, OperationFailedException
Operation
process
in class Operation
MissingParameterException
- if any mandatory parameter was not given to the operationWrongParameterException
- if at least one of the input parameters was
not initialized appropriately (values out of the valid interval, etc.)OperationFailedException
private void readHeader() throws java.io.IOException, WrongFileFormatException
java.io.IOException
- if there were reading errorsWrongFileFormatException
- if this is not a valid TIFF fileprivate TIFFImageFileDirectory readImageFileDirectory() throws InvalidFileStructureException, java.io.IOException
null
on failureInvalidFileStructureException
java.io.IOException
private int readInt() throws java.io.IOException
java.io.IOException
getByteOrder()
private short readShort() throws java.io.IOException
java.io.IOException
private java.lang.String readString(int length) throws java.io.IOException
length
- number of characters in a row to be loadedjava.io.IOException
- if there were reading errors or an unexpected
end of fileprivate TIFFTag readTag() throws InvalidFileStructureException, java.io.IOException
InvalidFileStructureException
java.io.IOException
public static void registerDecoder(java.lang.Class decoderClass)
TIFFDecoder
class.
TIFF knows many compression types, and JIU only supports some of them.
To register an external TIFFDecoder class with TIFFCodec, call this method
with the class field of your decoder.
As an example, for your TIFFDecoderMyCompression class,
call TIFFCodec.registerDecoder(TIFFDecoderMyCompression.class)
.
It will be checked if
decoderClass.newInstance() instanceof TIFFDecoder
is true and, if so, the class will be added to an internal list.
Whenever a TIFF file is to be decoded, the correct decoder is determined
(each decoder knows about the compression types it supports via the getCompressionTypes method)
and for each tile or strip such a decoder object will be created.private void setByteOrder(int newByteOrder)
BYTE_ORDER_INTEL
or
BYTE_ORDER_MOTOROLA
.newByteOrder
- the new byte order to be setjava.lang.IllegalArgumentException
- if the argument is not one of the above
mentioned constantspublic void setFile(java.lang.String fileName, CodecMode codecMode) throws java.io.IOException, UnsupportedCodecModeException
ImageCodec
setFile
in class ImageCodec
fileName
- name of the file to be used for loading or savingcodecMode
- defines whether file is to be used for loading or savingjava.io.IOException
UnsupportedCodecModeException
private void skipImageFileDirectories(int numDirectories) throws InvalidFileStructureException, java.io.IOException
numDirectories
- the number of directories to be skipped,
should be non-negativejava.lang.IllegalArgumentException
- if argument is negativeInvalidFileStructureException
- if there aren't enough image
file directoriesIOExceptions
- if there were errors reading or skipping datajava.io.IOException