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:
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 should support spot colors for printing with special gold ink. The article examines how you can load and save the business card template in TIFF format with additional gold color.
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.
using (var reader = new TiffReader(@"Images\BusinessCard.tif")) using (var writer = new 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 Bitmap(@"Images\extraChannel.jpg")) { // Create extra channel options based on extraBitmap. var extraChannel = new TiffExtraChannelEntry(extraBitmap, TiffChannelType.Alpha); writer.ExtraChannels.Add(extraChannel); Pipeline.Run(reader + writer); } }
This code snippet uses the following images:
A business card template without a logo.
An extra channel grayscale image. Each value of this image indicates whether to print this pixel with the spot color or not.
As a result, if you print the image using the gold ink, you get a business card like the following one:
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:
The following code reads a TIFF image, extracts an extra channel from it, and saves the channel's bitmap as a JPEG image.
using (var reader = new TiffReader(@"Images\result.tif")) { reader.Frames[0].ExtraChannels[1].GetBitmap().Save(@"Images\Output\extra.jpg"); }
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.
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.
There are two ways to write TIFF image with opacity: