- About Graphics Mill
- What's New?
- Breaking Changes in Graphics Mill 12
- Breaking Changes in Graphics Mill 11
- Features
- Supported File Formats
- System Requirements
- Getting Started
- Working with Image Formats
- Transforming Images (Resize, Rotate, Crop)
- Working with Metadata
- Color Management and Color Reduction
- Drawing Text and Graphics
- Color Keying (Green Screen Removal)
- Channels and Pixel Data
- Color and Tone Correction
- Working with Photoshop (PSD) Files
- Image Filters
- Processing Large Images
- Basics of Image Processing
- Troubleshooting
- Class Library Reference
- Licensing
- End User License Agreement
- Copyright Notices
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:
- extract a channel from an image
- replace a channel with a bitmap
- split an image into channels
- create an image by merging several channels
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:
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:
- RgbChannelSplitter is specified to work with RGB images
- CmykChannelSplitter is specified to work with CMYK images
- GrayscaleChannelSplitter is specified to work with grayscale images containing the alpha channel
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:
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.
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:
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:
- RgbChannelCombiner is specified to work with RGB images
- CmykChannelCombiner is specified to work with CMYK images
- GrayscaleChannelCombiner is specified to work with grayscale images containing the alpha channel
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):
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
- Bitmap Class
- RgbChannelSplitter Class
- CmykChannelSplitter Class
- GrayscaleChannelSplitter Class
- RgbChannelCombiner Class
- CmykChannelCombiner Class
- GrayscaleChannelCombiner Class