This documentation is for the old version. Go to the latest Graphics Mill docs

Working with TIFF Extra Channels

TIFF (Tagged Image File Format) is a file format for storing and interchanging raster images. It has a number of advantages, such as multiple pages support, metadata storage, etc. Find more detailed information on this image format and its features in the TIFF File Format topic. This article considers another, but not less important, feature of the TIFF image format: extra channels support.

Depending on a color space, an image contains one or more primary color channels. For example, Red, Green, and Blue channels for RGB color space or Cyan, Magenta, Yellow, and Black for CMYK. All other channels are extra and are intended for storing additional information, such as alpha or complementary color data. According to the TIFF specification the following types of extra channels are supported:

  • Unassociated alpha data (corresponds to Alpha Graphics Mill TIFF channel type) is transparency information that logically exists independently of an image. Unassociated alpha is ancillary information which can be removed or edited.
  • Associated alpha data (corresponds to PremultipliedAlpha Graphics Mill TIFF channel type) is opacity information. This channel type differs from the previous one: primary color channels of each pixel are premultiplied with value of alpha channel additionally. Storing data in premultiplied format allows compositing operations to be implemented more efficiently.
  • Unspecified data (corresponds to Undefined Graphics Mill TIFF channel type) is color information additional to primary colors (see example below) or some non-color information of an image.
Note

Including both unassociated and associated alpha is undefined because associated alpha specifies that color components are pre-multiplied by the alpha component, while unassociated alpha specifies the opposite.

Let us show how to work with TIFF extra channels in Graphics Mill for .NET using the following example. Suppose you have CMYK printer and you need to print gold letters on some printings. Unfortunately, by its very nature gold color cannot derived from cyan, magenta, yellow and black. To get rid of this problem the printer should support special gold ink. Suppose you have this special printer and you need to store business card template with gold letters in TIFF format. This article will show you how to load and save this template in TIFF format with additional gold color.

Adding an Extra Channel to the TIFF Image

In Graphics Mill for .NET TiffFrame class has ExtraChannels property. This property returns an instance of TiffExtraChannelCollection class which stores all extra channels wrapped in TiffExtraChannel instances. Extra channel is represented by Format8bppGrayScale or Format16bppGrayScale bitmap, pixel format of extra channel depends on pixel format of original bitmap. If a bitmap has extended pixel format (16 bits per channel), extra channel should be Format16bppGrayScale otherwise it should have Format8bppGrayScale format. Extra channel bitmap should be of the same dimensions as a source one.

The task of saving image along with additional color information can be easily solved. To perform it we just need to walk through the following steps:

  1. Create a business card template without a logo.

    Source Image
  2. Create a grayscale image with the desired logo which meets all extra channel requirements. Each value of this image indicates whether to print this pixel with gold ink on print output or not.

    Extra Channel
  3. Add the logo image to the source TIFF file as an extra channel. See the code sample below.
Visual Basic
Dim sourceImage As String = "c:\BusinessCard.tif"
Dim extraChannel As String = "c:\extra.jpg"
Dim resultImage As String = "c:\result.tif"

Dim sourceBitmap As New Aurigma.GraphicsMill.Bitmap(sourceImage)

' Load bitmap for the extra channel.
' Note: image for extra channel must be gray scale and have the same dimensions as the source one. 
Dim extraBitmap As New Aurigma.GraphicsMill.Bitmap(extraChannel)

' Create extra alpha channel using extraBitmap.
Dim tiffChannel As New Aurigma.GraphicsMill.Codecs.TiffExtraChannel(extraBitmap, Aurigma.GraphicsMill.Codecs.TiffChannelType.Undefined)

' Create tiff writer.
Dim writer As New Aurigma.GraphicsMill.Codecs.TiffWriter(resultImage)

' Create tiff frame.
Dim tiffFrame As New Aurigma.GraphicsMill.Codecs.TiffFrame()
' Set source image to the frame.
tiffFrame.SetBitmap(sourceBitmap)
' Add extra channel to the frame.
tiffFrame.ExtraChannels.Add(tiffChannel)

writer.AddFrame(tiffFrame)

writer.Dispose()
tiffFrame.Dispose()
extraBitmap.Dispose()
sourceBitmap.Dispose()
C#
string sourceImage = @"c:\BusinessCard.tif";
string extraChannel = @"c:\extra.jpg";
string resultImage = @"c:\result.tif";

Aurigma.GraphicsMill.Bitmap sourceBitmap = new Aurigma.GraphicsMill.Bitmap(sourceImage);

// Load bitmap for the extra channel.
// Note: image for extra channel must be gray scale and have the same dimensions as the source one. 
Aurigma.GraphicsMill.Bitmap extraBitmap = new Aurigma.GraphicsMill.Bitmap(extraChannel);

// Create extra alpha channel using extraBitmap.
Aurigma.GraphicsMill.Codecs.TiffExtraChannel tiffChannel =
    new Aurigma.GraphicsMill.Codecs.TiffExtraChannel(extraBitmap, Aurigma.GraphicsMill.Codecs.TiffChannelType.Undefined);

// Create tiff writer.
Aurigma.GraphicsMill.Codecs.TiffWriter writer = new Aurigma.GraphicsMill.Codecs.TiffWriter(resultImage);

// Create tiff frame.
Aurigma.GraphicsMill.Codecs.TiffFrame tiffFrame = new Aurigma.GraphicsMill.Codecs.TiffFrame();
// Set source image to the frame.
tiffFrame.SetBitmap(sourceBitmap);
// Add extra channel to the frame.
tiffFrame.ExtraChannels.Add(tiffChannel);

writer.AddFrame(tiffFrame);

writer.Dispose();
tiffFrame.Dispose();
extraBitmap.Dispose();
sourceBitmap.Dispose();

If you print the result image using the printer with the gold ink plane associated with the extra channel you will get the similar business card:

Result Image

Getting the Extra Channel from the TIFF Image

Suppose you load TIFF frame as TiffFrame instance, it contains all associated extra channels. To extract them you need to get TiffExtraChannelCollection instance using TiffFrame.ExtraChannels property. This returned collection stores all extra channels wrapped as TiffExtraChannel instances. So all you need is to call TiffExtraChannel.GetBitmap(Bitmap) method and you get extra channel as bitmap information. This bitmap meets all the requirements of the extra channel bitmap described above.

For example, to extract the extra channel from the TIFF file written in the previous example we need to perform the following steps:

  1. Create the TiffReader for the file we need to get extra channel from;
  2. Load a zero frame from this reader (because of TIFF has a single page);
  3. Load a zero extra channel from its ExtraChannels collection;
  4. Extract a bitmap from this TiffExtraChannel using the GetBitmap(Bitmap) method.

All these actions are demonstrated in the code sample below.

Visual Basic
Dim sourceImage As String = "c:\result.tif"
Dim extraChannel As String = "c:\extraChannel.jpg"

' Create tiff reader.
Dim reader As New Aurigma.GraphicsMill.Codecs.TiffReader(sourceImage)

' Load zero frame.
Dim tiffFrame As New Aurigma.GraphicsMill.Codecs.TiffFrame()
tiffFrame = CType(reader.LoadFrame(0), Aurigma.GraphicsMill.Codecs.TiffFrame)

' Create the result bitmap.
Dim result As New Aurigma.GraphicsMill.Bitmap()

' Load zero extra channel to the result bitmap.
tiffFrame.ExtraChannels.Item(0).GetBitmap(result)

result.Save(extraChannel)

result.Dispose()
tiffFrame.Dispose()
reader.Dispose()
C#
string sourceImage = @"c:\result.tif";
string extraChannel = @"c:\extraChannel.jpg";

// Create tiff reader.
Aurigma.GraphicsMill.Codecs.TiffReader reader = new Aurigma.GraphicsMill.Codecs.TiffReader(sourceImage);

// Load zero frame.
Aurigma.GraphicsMill.Codecs.TiffFrame frame = (Aurigma.GraphicsMill.Codecs.TiffFrame)reader.LoadFrame(0);

// Create the result bitmap.
Aurigma.GraphicsMill.Bitmap result = new Aurigma.GraphicsMill.Bitmap();

// Load zero extra channel to the result bitmap.
frame.ExtraChannels[0].GetBitmap(result);

result.Save(extraChannel);

result.Dispose();
frame.Dispose();
reader.Dispose();

Handling Opacity in Extra Channels

According to the TIFF specification alpha channels are stored as extra channels. If you need to handle alpha information as part of pixel data (load it to some pixel format supporting alpha channel, for example, Format32bppArgb), Graphics Mill for .NET provides you with this possibility. This section describes how to read and write bitmaps from/to TIFF image format with opacity information in pixel data.

Reading TIFF Images with Opacity

The TiffReader provides the FirstExtraChannelIsAlpha property to specify whether to interpret the first extra channel as alpha channel in pixel data. For example, assume you read an image contained three color channels (RGB) and two extra ones. If this property is true, the image loaded from frame using Frame.GetBitmap(Bitmap) method will be interpreted as ARGB (RGB + alpha). TiffFrame.ExtraChannels collection will contain one extra channel in this case. Otherwise, if the FirstExtraChannelIsAlpha property is false, it will be interpreted as RGB with two extra channels.

Writing TIFF Images with Opacity

There are two ways to write TIFF image with opacity:

  1. Create the bitmap you want to write to TIFF file with alpha channel, e.g. 32 bits per pixel ARGB format. Put this bitmap into a frame without extra channel and add this frame to the writer.
  2. Put a bitmap without alpha channel to a frame and store opacity data in the first extra channel with Alpha type.

See Also

Reference

Manual