Documentation

Tiff class

MATLAB Gateway to LibTIFF library routines

Description

TheTiffclass 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 theTiffobject to call routines in the LibTIFF library. While you can use theimreadandimwritefunctions to read and write TIFF files, theTiffclass 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 theTiffmethod is similar to the syntax of the corresponding LibTIFF library function. To get the most out of theTiffobject, you must be familiar with the LibTIFF API, as well as the TIFF specification and technical notes. View this documentation atLibTIFF - TIFF Library and Utilities.

MATLAB®supports LibTIFF version 4.0.0.

For copyright information, see thelibtiffcopyright.txtfile.

Construction

obj = Tiff(filename,mode)creates aTiffobject associated with the TIFF filefilename.modespecifies 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 thecurrentIFD.Tiffmethods operate on the current IFD. You can useTiffobject methods to navigate among the IFDs and the subIFDs in a TIFF file.

When you open a TIFF file for writing or appending, theTiffobject 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 includeImageWidth,ImageHeight,BitsPerSample,SamplesPerPixel,Compression,PlanarConfiguration, andPhotometric. If the image data has a stripped layout, the IFD contains theRowsPerStriptag. If the image data has a tiled layout, the IFD contains theTileWidthandTileHeighttags. Use thesetTagmethod to define values for these tags.

Input Arguments

filename

Character vector specifying name of file.

mode

Type of access to the TIFF file specified as one of the following.

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 thesetTagmethod. For more clarification, see the example.

Supported Values

Example: Set theCompressiontag to the valueJPEG. 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 thesetTagmethod. For more clarification, see the example.

这个字段是必需的,如果有额外的通道in addition to the usual colormetric channels.

Supported Values

Example: Set theExtraSamplestag to the valueAssociatedAlpha. Note how you use the property to specify the value.

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

See AlsoSpecify 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 thesetTagmethod. For more clarification, see the example.

This property is also referred to asFax3andT4Options. 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 thesetTagmethod. 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

指定控制YCbCr / RGB conversion

只有当光度interpr使用这些值etation 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 thereadRGBAImage,readRGBAStrip, andreadRGBATilemethods.

Supported Values

Example:

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

See alsoCreate 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 thesetTagmethod. 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 thesetTagmethod. 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 thesetTagmethod. For more clarification, see the example.

Supported Values

Example:

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

ResolutionUnit

Specify unit of measure used to interpret theXResolutionandYResolutiontags.

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 thesetTagmethod. For more clarification, see the example.

Supported Values

Example: SetResolutionUnittag to the valueInch. Then, settingXResolutiontag 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 thesetTagmethod. 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 thesetTagmethod. For more clarification, see the example.

These enumerated values should only be used when the photometric interpretation value isLogLorLogLUV. TheBitsPerSample,SamplesPerPixel, andSampleFormattags should not be set if the image type isLogLorLogLuv. The choice ofSGILogDataFmtwill set these tags automatically. TheFloatandBits8settings imply aSamplesPerPixelvalue of 3 forLogLUVimages, but only 1 forLogLimages.

Supported Values

This tag can be set only once per instance of aLogL/LogLuvTiff 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 thesetTagmethod. For more clarification, see the example.

SubFileTypeis 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 thesetTagmethod. For example,Tiff.TagID.ImageWidthreturns the ID of theImageWidthtag. To get a list of the names of supported tags, use thegetTagNamesmethod.

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 thesetTagmethod. 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 thesetTagmethod. For more clarification, see the example.

This property identifies all supported values for theYCbCrPositioningtag.

Supported Values

Example:

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

Methods

close Close Tiff object
computeStrip Index number of strip containing specified coordinate
computeTile Index number of tile containing specified coordinates
currentDirectory Index of current IFD
getTag Value of specified tag
getTagNames List of recognized TIFF tags
getVersion LibTIFF library version
isTiled Determine if tiled image
lastDirectory Determine if current IFD is last in file
nextDirectory Make next IFD current IFD
numberOfStrips Total number of strips in image
numberOfTiles Total number of tiles in image
read Read entire image
readEncodedStrip Read data from specified strip
readEncodedTile Read data from specified tile
readRGBAImage Read image using RGBA interface
readRGBAStrip Read strip data using RGBA interface
readRGBATile Read tile data using RGBA interface
rewriteDirectory Write modified metadata to existing IFD
setDirectory Make specified IFD current IFD
setSubDirectory Make subIFD specified by byte offset current IFD
setTag Set value of tag
write Write entire image
writeDirectory Create new IFD and make it current IFD
writeEncodedStrip Write data to specified strip
writeEncodedTile Write data to specified tile

Examples

expand all

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

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

Close the Tiff object.

t.close();

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 aTiffobject,t, and set the object properties. Set the value of theExtraSamplestag 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 theTiffobject.

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

Get RGB data.

rgb = imread('example.tif');

Create aTiffobject,t, and set the object properties. Specify RGB input data using theJPEGColorModeproperty.

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 theTiffobject.

t.write(rgb); t.close();
Was this topic helpful?