Reading and Writing Channels

In the digital imaging channel is a grayscale image of the size of the original image and containing a specific primary color from each original pixel. The number of image channels depends on how many primary colors are in the image. Also the channels can include the alpha channel, which itself does not contain color information but specifies opacity for each pixel. For example, an RGB image contains 3 channels (red, green, and blue), an aRGB image contains 4 channels (red, green, blue, and alpha), and so on. Channels of extended images are 16 bpp grayscale format, channels of regular images are 8 bpp grayscale. This topic examines working with color channels in Graphics Mill. Here you will learn how to:

Note

Working with channels, as with using other Graphics Mill features, you can choose the API tier that suits you: the Bitmap class or pipelines. If you are not familiar with pipelines, read the Understanding Image Processing Approaches in Graphics Mill topic.

Reading Channels

Getting Channel

Using the Bitmap.Channels property you can get access to a single channel, either by its index or by the channel alias. (Note: channel order is described in the Accessing Pixel Data topic.) Since each channel is a bitmap itself, you can handle it as you would any other grayscale bitmap: draw on it, save it to a file, and so on. Channel alias can be specified through the Channel enumeration.

The following code gets the cyan and alpha channels from a CMYK with alpha image and saves them as grayscale TIFF images:

C#
using (var bitmap = new Bitmap(@"Images\cmyk.tif"))
{
    var cyanBitmap = bitmap.Channels[Channel.Cyan];
    cyanBitmap.Save(@"Images\Output\Cmyk_test_Channel_C.tif");

    var alphaBitmap = bitmap.Channels[Channel.Alpha];
    alphaBitmap.Save(@"Images\Output\Cmyk_test_Channel_A.tif");
}

Splitting Channels with Pipelines

In printing it is not uncommon to handle large images. One of the major stages in offset printing is pre-press production, which includes converting an image to the proper CMYK color model and separating the image by colors. Pipelines offer a memory-friendly way of processing large images, including handling channels. The result of channels separation is a number of grayscale images that are used to create printing plates. The number of images depends on the source image color space. Graphics Mill provides the following classes, which allow the separation of images by channels:

The pixel format of the resulting image depends on the source image pixel format. The resulting image is Format8bppGrayscale if the source bitmap is not extended, and Format16bppGrayscale otherwise. Each splitter class also provides an ability to get the alpha channel.

The following code separates a CMYK image by color channels:

C#
using (var reader = new TiffReader(@"Images\cmyk.tif"))
using (var splitter = new CmykChannelSplitter())
{
    splitter.C = new TiffWriter(@"Images\Output\Cmyk_test_Channel_C.tif");
    splitter.M = new TiffWriter(@"Images\Output\Cmyk_test_Channel_M.tif");
    splitter.Y = new TiffWriter(@"Images\Output\Cmyk_test_Channel_Y.tif");
    splitter.K = new TiffWriter(@"Images\Output\Cmyk_test_Channel_K.tif");
    Pipeline.Run(reader + splitter);
}

Writing Channels

Replacing Channel

Some image editing operations require changing only one image channel (for example, the alpha channel). After the changes are completed, the source image channel should be replaced with the new one. In Graphics Mill you can easily replace a channel with a grayscale image through the Bitmap.Channels property.

Important

A new channel must be of the same dimensions and resolution as the target image.

The following code replaces the cyan and alpha channels in a CMYK image with grayscale bitmaps:

C#
using (var bitmap = new Bitmap(@"Images\cmyk.tif"))
using (var cyanBitmap = new Bitmap(@"Images\Cmyk_test_Channel_C.tif"))
using (var alphaBitmap = new Bitmap(@"Images\Cmyk_test_Channel_A.tif"))
{
    bitmap.Channels[Channel.Cyan] = cyanBitmap;
    bitmap.Channels[Channel.Alpha] = alphaBitmap;
    bitmap.Save(@"Images\Output\Cmyk_test_cyan_alpha.tif");
}

Merging Channels with Pipelines

Merging channels into a single image allows the creating of color images by combining several grayscale images. It is useful when, for example, you have 4 grayscale bitmaps prepared for printing (cyan, magenta, yellow, and black) and you need to make a color preview of the resulting image. The easiest way to merge channels into one image is to use one of the following combiner classes:

Important

The merging channels must be of the same dimensions and resolution.

The following code creates a CMYK image by combining 4 grayscale bitmaps (one bitmap per color channel):

C#
using (var combiner = new CmykChannelCombiner())
using (var writer = new TiffWriter(@"Images\Output\Cmyk_test_combined.tif"))
{
    combiner.C = new TiffReader(@"Images\Cmyk_test_Channel_C.tif");
    combiner.M = new TiffReader(@"Images\Cmyk_test_Channel_M.tif");
    combiner.Y = new TiffReader(@"Images\Cmyk_test_Channel_Y.tif");
    combiner.K = new TiffReader(@"Images\Cmyk_test_Channel_K.tif");
    combiner.AutoDisposeSources = true;
    Pipeline.Run(combiner + writer);
}

See Also

Reference

Manual