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:
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.
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:
Create a business card template without a logo.
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.
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()
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:
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:
All these actions are demonstrated in the code sample below.
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()
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();
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.
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.
There are two ways to write TIFF image with opacity: