Working with TIFF Extra Channels

TIFF (Tagged Image File Format) is a file format for storing and interchanging raster images. You can find the format description and a list of format-specific Graphics Mill features in the Supported File Formats topic. This article describes how to work with TIFF extra channels.

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

  • Unassociated alpha data is transparency information that logically exists independently of an image. Unassociated alpha is additional information, which can be removed or edited. This type corresponds to the Graphics Mill TiffChannelType.Alpha channel type.
  • Associated alpha data is opacity information. The color information of each pixel is additionally premultiplied with the value of alpha channel. If this channel data is removed or edited, the image changes. Storing data in premultiplied format allows compositing operations to be implemented more efficiently. This type corresponds to the Graphics Mill TiffChannelType.PremultipliedAlpha channel type.
  • Unspecified data can contain color information in addition to primary colors or non-color information. This type corresponds to the Graphics Mill TiffChannelType.Undefined channel type.
Note

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

The example used throughout this article assumes that you have a CMYK printer and you need to print a gold logo on a business card. By its very nature the color gold cannot be derived from cyan, magenta, yellow, and black. So, the printer supports special gold ink. The article examines how to load and save the business card template in TIFF format with additional gold color.

Adding Extra Channels to TIFF Image

The method for creating a TIFF image with an extra channel is to create a base image and an extra channel image, and combine them using Graphics Mill. Both images should have the same resolution and size (in pixels). An extra channel image should be grayscale and its pixel format depends on the pixel format of the base image. If the base image has an extended pixel format (16 bits per channel), then the extra channel image should be Format16bppGrayscale; otherwise, it should be Format8bppGrayscale.

Graphics Mill provides the TiffFrame.ExtraChannels property to manipulate with the extra channels of a frame. Using the TiffExtraChannelEntry class you can configure an extra channel before adding it to a TIFF image.

The following code adds a JPEG image to a TIFF image as an unassociated alpha extra channel, and saves the resulting TIFF image.

C#
using (var reader = new Aurigma.GraphicsMill.Codecs.TiffReader(@"Images\BusinessCard.tif"))
using (var writer = new Aurigma.GraphicsMill.Codecs.TiffWriter(@"Images\Output\result.tif"))
{
    // Load bitmap for the extra channel.
    // Note: image for extra channel must be gray scale and have the same dimensions and DPI as the source one. 
    using (var extraBitmap = new Aurigma.GraphicsMill.Bitmap(@"Images\extraChannel.jpg"))
    {
        // Create extra channel options based on extraBitmap.
        var extraChannel = new Aurigma.GraphicsMill.Codecs.TiffExtraChannelEntry(
            extraBitmap, Aurigma.GraphicsMill.Codecs.TiffChannelType.Alpha);

        writer.ExtraChannels.Add(extraChannel);
        Aurigma.GraphicsMill.Pipeline.Run(reader + writer);
    }
}

The code snippet above uses the following images:

  1. A business card template without a logo.

    Source Image
  2. An extra channel grayscale image. Each value of this image indicates whether to print this pixel with gold ink or not.

    Extra Channel

If you print the resulting image using the printer with gold ink described above, you will get a business card like the following one:

Result Image

Extracting Extra Channel from TIFF Image

Graphics Mill allows the extracting of channels from a TIFF image and working with an extracted channel like with any bitmap. The TiffFrame class, inherited from the Frame class, provides the ExtraChannels property to access all extra channels associated with a frame. Each frame allows getting its bitmap using the Frame.GetBitmap() method.

So, to extract an extra channel from a TIFF file you need to perform the following steps:

  1. Create a TiffReader instance, and pass a filename as a constructor parameter.
  2. Get a desired frame from the Frames collection of the previously created reader (for a single page TIFF it is a zero frame).
  3. Get an extra channel from the TiffFrame.ExtraChannels collection by specifying the channel number. For images containing an alpha channel, the first extra channel is the alpha channel. Channel numbering is zero-based.
  4. Extract a bitmap from the TiffExtraChannel using the GetBitmap() method.

The following code reads a TIFF image, extracts an extra channel from it, and saves the channel's bitmap as a JPEG image.

C#
using (var reader = new Aurigma.GraphicsMill.Codecs.TiffReader(@"Images\result.tif"))
{
    reader.Frames[0].ExtraChannels[1].GetBitmap().Save(@"Images\Output\extra.jpg");
}

Handling Opacity in Extra Channels

According to the TIFF specification, alpha channels in TIFF images are stored as extra channels. If you need to handle alpha information as a part of pixel data, Graphics Mill provides this capability. This section describes how to load and save bitmaps containing opacity information in pixel data from/to TIFF images.

Loading Images with Opacity from TIFF Images

TiffReader provides the TreatFirstExtraChannelAsAlpha property to specify whether to interpret the first extra channel as an alpha channel in the pixel data. Suppose you read an image containing three color channels (RGB) and two extra channels. If the TreatFirstExtraChannelAsAlpha property is true, then the Frame.GetBitmap() method interprets the image as ARGB (RGB + alpha) independently of the first extra channel type. Otherwise, if the TreatFirstExtraChannelAsAlpha property is false, the image is interpreted as ARGB only if the first extra channel type is alpha, otherwise the image is interpreted as RGB.

Saving Images with Opacity to TIFF Images

There are two ways to write TIFF image with opacity:

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

See Also

Reference

Manual