Completely revealing the secrets of GDI + colormatrix

Source: Internet
Author: User
Tags cos

Regardless of the language used, anyone who has used Windows's GDI + is familiar with colormatrix, And I have mentioned many times in my blog articles, I also gave a detailed explanation of the functions of the colormatrix in the article "GDI + for VCL basics-Color adjustment matrix colormatrix". Although I think that I am quite familiar with colormatrix, however, I still know its principles, but I do not know why. Until a friend of mine called colormatrix a few days ago, the image de-countermeasure function was not normal and asked me (see http://topic.csdn.net/u/20080830/20/070c83de-d45b-441f-996e-3c68892855cd.html), which I thought was unlikely! As we all know, using the RGB main diagonal-1 matrix to reverse image search is one of the important functions of colormatrix. However, after many experiments, the image cannot be reversed normally. After I had doubts about what I think I was familiar with, I had the urge to not understand and never give up. Therefore, I thoroughly analyzed the principles of colormatrix, this not only clarified the cause of abnormal image de-inversion, but also fully implemented the colormatrix function with the code!

This article mainly discloses the principle of GDI + colormatrix adjustment and introduces the code (see "Delphi Image Processing-image color matrix adjustment").

Although I have two articles in my blog that reference the classic text about the colormatrix function, I will reference it again here as the beginning of the principle:

GDI + provides image and bitmap classes for storing and operating images. The image and bitmap objects store the color of each pixel in 32 bits: red, green, blue, and Alpha. The values of these four components are from 0 to 255. 0 indicates no brightness, and 255 indicates the maximum brightness. The Alpha component specifies the transparency of the color. 0 indicates full transparency, and 255 indicates full opacity.
The color vector is in the form of 4 tuples (red, green, blue, and alpha ). For example, a color vector (0,255, 0,255) indicates an opaque color without red or blue but green reaching the maximum brightness.
Another convention for color is to use number 1 to indicate that the brightness reaches the maximum. With this convention, the colors described in the previous section are represented by (0, 1, 0, 1. GDI + uses 1 to represent the maximum brightness during color conversion.
Linear transformation (rotation, scaling, etc.) can be applied to the color vector by multiplying the color vectors with a 4 × 4 matrix. However, you cannot use a 4x4 matrix for translation (non-linear ). If you add a virtual 5th coordinate (for example, number 1) to each color vector, you can use the 5-xx matrix to apply any combination form of linear transformation and shift. A transformation composed of linear transformations followed by translation is called an affine transformation.

To be honest, not only did I get started with colormatrix, but now I see this text, and I think it is a bit mysterious. The first three paragraphs are very understandable, especially the later sections, even more, those who first came to know GDI + thought colormatrix was profound: How does colormatrix achieve color scaling, rotation, cutting, and translation? What effects can images be achieved through these features? Or can a certain effect be implemented using colormatrix?

Next, let's unveil the mystery of colormatrix step by step. I believe that after reading this article, you will surely say: Oh, it's so easy! In fact, this is the so-called "a piece of paper in the rivers and lakes, not worth a penny "! A lot of things that seem to be highly technical are kept confidential, patent applications, and mysterious. In fact, once published, this is the case!

To reveal the colormatrix, We need to parse how its scaling, rotation, cutting, and translation functions are implemented. To facilitate the description, I drew a colormatrix matrix and pasted it on it, in the following statement, upper-case argb indicates the existing values of each color component, and lower-case argb indicates the new value after calculation.

1. color scaling: color scaling is very simple, that is, based on the given proportional value, a new component value is calculated based on the existing component values of A, R, G, and B in the image pixel. The proportional value is the other four values except M55 on the main diagonal of colormatrix. For example, the rgba values of a certain pixel are now 255, 128, 64, and 255, while the main diagonal M11-m44 values are 0.8, 0.5,-1, and 0.5, respectively, the new rgba value of this pixel should be:

R = r * M11 = 255*0.8 = 204;

G = g * m22 = 128*0.5 = 64;

B = B * M33 = 64 *-1 = 192;

A = A * m44 = 255*0.5 = 128;

Is it easy ?! Some beginners may say that the above B value is calculated incorrectly. 64 *-1 should be equal to-64. Yes, 64 is represented as 0xffffffc0 in 32 bits. The unsigned byte is saturated and the last 8 bits 0xc0 is equal to 192. Negative value calculation will be detailed later.

2. Color cut: In general, the R, G, and B components of the image increase or decrease the color weight in proportion to the other color weight. In fact, this expression is not complete, and the component of the pixel is also involved!

Taking the red component R as an example, if you want to cut by the green component G, then m21 is the shear ratio value, and m21 * g gets the shear volume of G to R. Similarly, m31 * B and M41 * A can obtain the shear volumes of B and A to R respectively. The total shear volumes are the total shear volumes of R. Expressed:

R = g * m21 + B * m31 + A * M41;

G = r * m12 + B * M32 + A * M42;

B = r * M13 + G * M23 + A * m43;

A = r * m14 + G * M24 + B * m34;

3. Color rotation: The description of color rotation is complex, that is, in the image pixel, two of them are used to calculate the result around the other one according to a certain angle, is the color rotation. Take the red component R and green component G as an example to rotate 60 degrees around the blue component G:

M11 = cos (60) = 0.5, M12 = sin (60) = 0.866, m21 =-sin (60) =-0.866, m22 = cos (60) = 0.5, then, R and G get the following rotation values:

R = r * M11 (0.5) + G * m21 (-0.866 );

G = r * M12 (0.866) + G * m22 (0.5 );

From the above formula, the so-called color rotation volume is actually the scaling volume of the two rotating components plus the shear volume of the other party! From an operational perspective, it has nothing to do with other components.

4. Color Translation: The above scale, cut, and rotation belong to the linear transformation of color (both the accumulation and multiplication), while the translation is the nonlinear transformation of color, it is just an addition to each color component: the translation of each component of the image pixel is represented by the so-called virtual bit, that is, the values of the 5th rows, the virtual row value of each component plus the column is the color translation, and the essence is to adjust the brightness value of the component non-linear. Use the formula to indicate the translation volume of each component:

R = R + m51 x 255;

G = G + m52 * 255;

B = B + m53 * 255;

A = a + m54x 255;

Based on the scaling, rotation, cutting, and translation formulas of the above colors, for each component of the color, R, G, B, and, the actual values R, G, B, And a obtained by using colormatrix are expressed:

R = r * M11 + G * m21 + B * m31 + A * M41 + m51 * 255;

G = r * m12 + G * m22 + B * M32 + A * M42 + m52 * 255;

B = r * M13 + G * M23 + B * M33 + A * m43 + m53 * 255;

A = r * m14 + G * M24 + B * m34 + A * m44 + M54 * 255;

Technically, this formula indicates the new value of each component of the color, equals to the product of the values of the first four rows of the corresponding column in colormatrix and the current values of R, G, A, and B plus the product of the values of the first 5th rows and the constant 255, the virtual column (5th columns) does not have any effect.

About the implementation principle of colormatrix, I will talk about the negative value calculation problem mentioned above: the negative value calculation of colormatrix is a little troublesome in terms of principle and pure technical implementation:

A) in the preceding scaling example, if only the primary diagonal has a value, the negative value of B is calculated as B = B * M33 = 64 *-1 = 192, this is correct, but if a cut or translation is added, this value changes. For example, adding a translation volume of 0.1 on the basis of scaling seems to be a very simple problem. Simply adding 0.1*255 is not enough! However, this is a simple problem, but there are two solutions: Add a translation volume of 192*0.1 equals 255 on the scaled 218? Or should we perform continuous operations: B = B * M33 = 64 *-1 + 255*0.1 =-38 (0 after saturation processing? If it is the former, it seems that the algorithm cannot be said; if it is the latter, it is hard to be accepted by the senses, B = 192 makes the color value "blue, I wanted to make it "blue" a little more, and add a translation (brightness), but even if the translation volume is quite small, for example, only translating 0.001, it changes the value of B to 0, not only does it not change to "blue", but the color loses its blue weight! Colormatrix uses solutions that make your senses unacceptable. In fact, it is no wonder that the designer, as long as they implement the Code with its own nature, will naturally become the result. This result is not only the translation volume, as long as the value other than the primary diagonal in the matrix is not 0, including the 5th columns that do not work, it will make the primary diagonal, the rgba component corresponding to a negative value changes accordingly. For details, refer to the code implementation in this article.

B) As mentioned above, some friends have used the-1 matrix to solve the problem that image inversion is not required. By calculating the scaling formula above, it is not difficult to find that the image obtained from the-1 matrix is just similar! Why? The so-called inversion is reversible. If we use 255 minus R (GB) to obtain the inverse, we can use 255 minus it to restore. We use 255 different and R (GB ), needless to say, it is reversible, while-1 * R (GB) is not completely reversible: 1 *-1 after saturation is set to 254, 2 *-1 is ...... and so on; in turn, the result is 255 *-1 = 1, 254 *-1 = 2 ...... wait a moment. The range from 1 to 255 is recoverable. Although this reduction in the original sense has an error of 1, this error is acceptable from the human senses, only the value "0" is irreversible. The value 0 *-1 is an integer or 0. Whether it is a reverse or a restoration, the value "0" is still "0 ", this damages the coordination of the reverse image and makes the human senses completely unacceptable. For example, the original pure red pixel should be blue after the reverse image, but it turns black here, can you accept it? So as long as any component of the image pixel RGB is 0, the reverse effect of this image will become a problem! Some people may say that this is what we do. Except for a few images, most of the reverse images still look normal. Do these normal images have no pixels whose RGB component is 0? In fact, the intermittent distribution of several pixels with RGB components of 0 in a graph is not so obvious after the inversion, so we think this is successful, only those parts in the "0" component set seriously damage the coordination of the graph.

This is the secret of colormatrix principles. Due to my limited level of cultural theory, I can only reveal the implementation principle of colormatrix from a technical point of view. Sorry!

Through the detailed technical anatomy of colormatrix, we have introduced its calculation formula. With this, it should be easy to use program code to implement the colormatrix function, in this article, we will introduce the specific implementation of the colormatrix function. I believe you will be better at it.

If you have different opinions and questions, you can write them in the comments. I will try my best to answer them. Thank you for your guidance and advice. Email: maozefa@hotmail.com.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.