Performing Color Transformation Operations in C# GDI+ - Shearing Color Operation
(Page 4 of 4 )
Shearing increases or decreases a color component by an amount proportional to another color component. For example, consider the transformation where the red component is increased by one half the value of the blue component. Under such a transformation, the color (0.2, 0.5, 1) would become (0.7, 0.5, 1). The new red component is 0.2 + (1/2)(1) = 0.7. Herein, we say that the red component is a map to the blue component; or in another way, that the blue component is sheared to the red one. Figure 10 shows the color relations in the shearing color operation.
Figure 10-the color components in gray join in the shearing transformation
_html_26748bc4.png)
Now, let's continue to take a look at the related sample. The following example constructs an Image object from the file Colorinput.bmp. Then the code applies the shearing transformation described in the preceding paragraph to each pixel in the image.
Graphics graphics=this.CreateGraphics();
graphics.Clear(Color.White);
//load the image
Bitmap image=new Bitmap("Colorinput.bmp");
ImageAttributes imageAttributes=new ImageAttributes();
int width = image.Width;
int height = image.Height;
//define the color transformation matrix
float[][] colorMatrixElements=
{
new float[]{1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[]{0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[]{0.5f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.0f, 0.0f, 1.0f, 0.0f},
new float[]{ 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix colorMatrix=new ColorMatrix(colorMatrixElements);
//enable the color transformation matrix
imageAttributes.SetColorMatrix(
colorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
//render the source image
graphics.DrawImage(image, 0, 0);
//use the color transformation matrix defined above to render new image
graphics.TranslateTransform(width+10,0);
graphics.DrawImage(image, new Rectangle(0, 0, width, height),
0, 0,width, height,GraphicsUnit.Pixel,imageAttributes);
In the code, the element at column 1 of row 3 of the color transformation matrix is set to 0.5, which means that during the shearing color transformation, all the increments of the red component are half of the blue component. The following illustration (Figure 11) shows the original image on the left and the newly-sheared one on the right.
Figure 11-the running-time snapshot for the shearing transformation
_html_743b9ce6.png)
As is apparently shown in Figure 11, after the shearing operation the red components in the target image increase.
Summary
In this installment, we've thoroughly examined the four kinds of color operations(translating, scaling, rotating, and shearing) in GDI+ and come to realize that the four transformations are the fundamental operations of nearly all coloring support. In the third installment of this series, we will explore the color remapping support in GDI+, as well as try to achieve the effect of Photoshop-like color channel isolating via the color transformation matrices.
-DOWNLOAD SOURCE-
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |