Tiff class

MATLAB Gateway to LibTIFF library routines

Description

The Tiff class represents a connection to a Tagged Image File Format (TIFF) file and provides access to many of the capabilities of the LibTIFF library. Use the methods of the Tiff object to call routines in the LibTIFF library. While you can use the imread and imwrite functions to read and write TIFF files, the Tiff class offers capabilities that these functions don't provide, such as reading subimages, writing tiles and strips of image data, and modifying individual TIFF tags.

In most cases, the syntax of the Tiff method is similar to the syntax of the corresponding LibTIFF library function. To get the most out of the Tiff object, you must be familiar with the LibTIFF API, as well as the TIFF specification and technical notes. View this documentation at LibTIFF - TIFF Library and Utilities.

MATLAB® supports LibTIFF version 4.0.0.

For copyright information, see the libtiffcopyright.txt file.

Construction

obj = Tiff(filename,mode) creates a Tiff object associated with the TIFF file filename. mode specifies the type of access to the file.

A TIFF file is made up of one or more image file directories (IFDs). An IFD contains image data and associated metadata. IFDs can also contain subIFDs which also contain image data and metadata. When you open a TIFF file for reading, the Tiff object makes the first IFD in the file the current IFD. Tiff methods operate on the current IFD. You can use Tiff object methods to navigate among the IFDs and the subIFDs in a TIFF file.

When you open a TIFF file for writing or appending, the Tiff object automatically creates a IFD in the file for writing subsequent data. This IFD has all the default values specified in TIFF Revision 6.0.

When creating a new TIFF file, before writing any image to the file, you must create certain required fields (tags) in the file. These tags include ImageWidth, ImageHeight, BitsPerSample, SamplesPerPixel, Compression, PlanarConfiguration, and Photometric. If the image data has a stripped layout, the IFD contains the RowsPerStrip tag. If the image data has a tiled layout, the IFD contains the TileWidth and TileHeight tags. Use the setTag method to define values for these tags.

Input Arguments

filename

Text string specifying name of file.

mode

One of the following text strings specifying the type of access to the TIFF file.

 Supported Values

Properties

Compression

Specify scheme used to compress image data

This property defines all the supported values for this tag. You use this property to specify the value you want to assign to the tag, using the setTag method. For more clarification, see the example.

 Supported Values

Example: Set the Compression tag to the value JPEG. Note how you use the property to specify the value.

tiffobj.setTag('Compression', Tiff.Compression.JPEG);

ExtraSamples

Describe extra components

This property defines all the supported values for this tag. You use this property to specify the value you want to assign to the tag, using the setTag method. For more clarification, see the example.

This field is required if there are extra channels in addition to the usual colormetric channels.

 Supported Values

Example: Set the ExtraSamples tag to the value AssociatedAlpha. Note how you use the property to specify the value.

tiffobj.setTag('ExtraSamples', Tiff.ExtraSamples.AssociatedAlpha);

See Also Specify Tiff object properties and describe alpha channel

Group3Options

Options for Group 3 Fax Compression

This property defines all the supported values for this tag. You use this property to specify the value you want to assign to the tag, using the setTag method. For more clarification, see the example.

This property is also referred to as Fax3 and T4Options. This value is a bit mask controlled by the first three bits.

 Supported Values

Example:

mask = bitor(Tiff.Group3Options.Encoding2D,Tiff.Group3Options.Uncompressed)
tiffobj.setTag('Group3Options',mask);

InkSet

Specify set of inks used in separated image

This property defines all the supported values for this tag. You use this property to specify the value you want to assign to the tag, using the setTag method. For more clarification, see the example.

In this context, separated refers to photometric interpretation, not the planar configuration.

 Supported Values

Example:

tiffobj.setTag('InkSet', Tiff.InkSet.CMYK);

JPEGColorMode

Specify control of YCbCr/RGB conversion

Use these values only when the photometric interpretation is YCbCr.

This property should not be used for the purpose of reading YCbCr imagery as RGB. In this case, use the RGBA interface provided by the readRGBAImage, readRGBAStrip, and readRGBATile methods.

 Supported Values

Example:

tiffobj.setTag('JPEGColorMode',Tiff/JPEGColorMode.RGB);

See also Create YCbCr/JPEG image from RGB data

Orientation

Specify visual orientation of the image data.

This property defines all the supported values for this tag. You use this property to specify the value you want to assign to the tag, using the setTag method. For more clarification, see the example.

The first row represents the top of the image, and the first column represents the left side. Support for this tag is for informational purposes only, and it does not affect how MATLAB reads or writes the image data.

 Supported Values

Example:

tiffobj.setTag('Orientation', Tiff.Orientation.TopRight);

Photometric

Specify color space of image data

This property defines all the supported values for this tag. You use this property to specify the value you want to assign to the tag, using the setTag method. For more clarification, see the example.

 Supported Values

Example:

tiffobj.setTag('Photometric', Tiff.Photometric.RGB);

PlanarConfiguration

Specifies how image data components are stored on disk

This property defines all the supported values for this tag. You use this property to specify the value you want to assign to the tag, using the setTag method. For more clarification, see the example.

 Supported Values

Example:

tiffobj.setTag('PlanarConfiguration', Tiff.PlanarConfiguration.Chunky);

ResolutionUnit

Specify unit of measure used to interpret the XResolution and YResolution tags.

This property defines all the supported values for this tag. You use this property to specify the value you want to assign to the tag, using the setTag method. For more clarification, see the example.

 Supported Values

Example: Set ResolutionUnit tag to the value Inch. Then, setting XResolution tag to 300 means pixels per inch.

tiffObj.setTag('ResolutionUnit', Tiff.ResolutionUnit.Inch);
tiffObj.setTag('XResolution', 300);
tiffObj.setTag('YResolution', 300);

SampleFormat

Specify how to interpret each pixel sample

This property defines all the supported values for this tag. You use this property to specify the value you want to assign to the tag, using the setTag method. For more clarification, see the examples.

 Supported Values

Example:

tiffobj.setTag('SampleFormat', Tiff.SampleFormat.IEEEFP);

SGILogDataFmt

Specify control of client data for SGILog codec

This property defines all the supported values for this tag. You use this property to specify the value you want to assign to the tag, using the setTag method. For more clarification, see the example.

These enumerated values should only be used when the photometric interpretation value is LogL or LogLUV. The BitsPerSample, SamplesPerPixel, and SampleFormat tags should not be set if the image type is LogL or LogLuv. The choice of SGILogDataFmt will set these tags automatically. The Float and Bits8 settings imply a SamplesPerPixel value of 3 for LogLUV images, but only 1 for LogL images.

 Supported Values

This tag can be set only once per instance of a LogL/LogLuv Tiff image object instance.

Example:

tiffobj = Tiff('example.tif','r');
tiffobj.setDirectory(3); % image three is a LogLuv image
tiffobj.setTag('SGILogDataFmt', Tiff.SGILogDataFmt.Float);
imdata = tiffobj.read();

SubFileType

Specify type of image

This property defines all the supported values for this tag. You use this property to specify the value you want to assign to the tag, using the setTag method. For more clarification, see the example.

SubFileType is a bitmask that indicates the type of the image.

 Supported Values

Example:

tiffobj.setTag('SubFileType', Tiff.SubFileType.Mask);

TagID

List of recognized TIFF tag names with their ID numbers

This property identifies all the supported TIFF tags with their ID numbers. Use this property to specify a tag when using the setTag method. For example, Tiff.TagID.ImageWidth returns the ID of the ImageWidth tag. To get a list of the names of supported tags, use the getTagNames method.

Example:

tiffobj.setTag(Tiff.TagID.ImageWidth, 300);

Thresholding

Specifies technique used to convert from gray to black and white pixels.

This property defines all the supported values for this tag. You use this property to specify the value you want to assign to the tag, using the setTag method. For more clarification, see the example.

 Supported Values

Example:

tiffobj.setTag('Thresholding', Tiff.Thresholding.HalfTone);

YCbCrPositioning

Specify relative positioning of chrominance samples

This property defines all the supported values for this tag. You use this property to specify the value you want to assign to the tag, using the setTag method. For more clarification, see the example.

This property identifies all supported values for the YCbCrPositioning tag.

 Supported Values

Example:

tiffobj.setTag('YCbCrPositioning', Tiff.YCbCrPositioning.Centered);

Methods

closeClose Tiff object
computeStripIndex number of strip containing specified coordinate
computeTileIndex number of tile containing specified coordinates
currentDirectoryIndex of current IFD
getTagValue of specified tag
getTagNamesList of recognized TIFF tags
getVersionLibTIFF library version
isTiledDetermine if tiled image
lastDirectoryDetermine if current IFD is last in file
nextDirectoryMake next IFD current IFD
numberOfStripsTotal number of strips in image
numberOfTilesTotal number of tiles in image
readRead entire image
readEncodedStripRead data from specified strip
readEncodedTile Read data from specified tile
readRGBAImageRead image using RGBA interface
readRGBAStripRead strip data using RGBA interface
readRGBATileRead tile data using RGBA interface
rewriteDirectoryWrite modified metadata to existing IFD
setDirectoryMake specified IFD current IFD
setSubDirectoryMake subIFD specified by byte offset current IFD
setTagSet value of tag
write Write entire image
writeDirectoryCreate new IFD and make it current IFD
writeEncodedStripWrite data to specified strip
writeEncodedTileWrite data to specified tile

Examples

expand all

Create New TIFF File Using Tiff object

Create a new file called myfile.tif. To run this example, your directory must be writable.

t = Tiff('myfile.tif', 'w');

Close the Tiff object.

t.close();

Specify Tiff object properties and describe alpha channel

Create an array of data, data, that contains colormetric channels and an alpha channel.

rgb = imread('example.tif');
numrows = size(rgb,1);
numcols = size(rgb,2);
alpha = 255*ones([numrows numcols], 'uint8');
data = cat(3,rgb,alpha);

Create a Tiff object, t, and set the object properties. Set the value of the ExtraSamples tag because the data contains the alpha channel in addition to the colormetric channels.

t = Tiff('myfile.tif','w');
t.setTag('Photometric',Tiff.Photometric.RGB);
t.setTag('Compression',Tiff.Compression.None);
t.setTag('BitsPerSample',8);
t.setTag('SamplesPerPixel',4);
t.setTag('SampleFormat',Tiff.SampleFormat.UInt);
t.setTag('ExtraSamples',Tiff.ExtraSamples.Unspecified);
t.setTag('ImageLength',numrows);
t.setTag('ImageWidth',numcols);
t.setTag('TileLength',32);
t.setTag('TileWidth',32);
t.setTag('PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);

Write the data to the Tiff object.

t.write(data);
t.close();

Create YCbCr/JPEG image from RGB data

Get RGB data.

rgb = imread('example.tif');

Create a Tiff object, t, and set the object properties. Specify RGB input data using the JPEGColorMode property.

t = Tiff('myfile.tif','w');
t.setTag('Photometric',Tiff.Photometric.YCbCr);
t.setTag('Compression',Tiff.Compression.JPEG);
t.setTag('YCbCrSubSampling',[2 2]);
t.setTag('BitsPerSample',8);
t.setTag('SamplesPerPixel',3);
t.setTag('SampleFormat',Tiff.SampleFormat.UInt);
t.setTag('ImageLength',size(rgb,1));
t.setTag('ImageWidth',size(rgb,2));
t.setTag('TileLength',32);
t.setTag('TileWidth',32);
t.setTag('PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
t.setTag('JPEGColorMode',Tiff.JPEGColorMode.RGB);
t.setTag('JPEGQuality',75);

Write the data to the Tiff object.

t.write(rgb);
t.close();

See Also

| |

Was this topic helpful?