Colormatrix (color matrix) is a matrix used in GDI + to adjust the color of an image.
What is a matrix? To put it bluntly, it is a two-dimensional array in C.
So what is the principle of Color adjustment in this matrix? How does it adjust the color? This should begin with the multiplication of matrices in linear algebra.
Readers who have learned linear algebra in the following sections can skip this section. Here I will use my own understanding to describe the algorithm and result of matrix multiplication.
In linear algebra, two matrices are multiplied in this way:
If matrix A is multiplied by matrix B, a new matrix C is generated. The element of column M in row n of column C is equal to the sum of the elements multiplied by one of the N rows of A and column M of Column B. The number of new matrix rows equals to the number of rows of A, and the number of columns equals to the number of columns of B.
In addition, a and B must meet the requirements. The number of columns of A is equal to the number of rows of B. This is to ensure that each element in each row of A can be multiplied by each element in each column of B.
That is, a [M, N] x B [N, P] = C [m, p]
In an intuitive description, there is a matrix A [2, 3]
{
1, 2, 3
4, 5, 6
}
And matrix B [3, 2]
{
7, 8
9, 10
11, 12
}
Then, after multiplication, the new matrix C [2, 2] is generated.
{
1*7 + 2*9 + 3*11, 1*8 + 2*10 + 3*12
4*7 + 5*9 + 6*11, 4*8 + 5*10 + 6*12
}
Now, the concept of the matrix is described here. The color matrix in GDI + is described below.
In a computer, an image can be regarded as a collection of points. Although images have the concept of width and height, they seem to be two-dimensional. In fact, we can regard width and height as the attributes of images and have nothing to do with vertices, consider all vertices of an image as a one-dimensional array.
The vertex itself is a set of red, green, and blue. Now, an Alpha value (indicating transparency) is added to the computing image, which is the set of four attributes. In this way, an image becomes a two-dimensional array, that is, the standard matrix.
Then, the image with a pair of four points is described as a matrix of P [4, 4].
{
R1, G1, B1, A1
R2, G2, B2, A2
R3, G3, B3, A3
R4, G4, B4, A4
}
When we combine this matrix with another 4x4 matrix m
{
RR, GR, BR, ar
RG, GG, BG, AG
RB, GB, BB, AB
Ra, Ga, Ba, AA
}
When a matrix is multiplied, a new matrix is generated. The number of elements in the new matrix is the same as that in the original matrix. (If you do not believe this, use the [5, 4], [6, 4] matrix to multiply it ), in addition, every element of the new matrix has undergone interesting changes. Let's take a look at the results. The new matrix NP:
{
R1 * RR + G1 * RG + B1 * RB + A1 * ra, R1 * GR + G1 * gg + B1 * GB + A1 * GA, r1 * Br + G1 * RG + B1 * BB + A1 * Ba, R1 * Ar + G1 * Ag + B1 * AB + A1 * AA
......
......
......
}
We can see that R, G, B, A is equal to the new value of rgba at the first vertex of the original image. That is to say, by multiplying the matrix, you can change the value of each component of the rgba in the new image based on the original image.
At the same time, I can see why I named the elements in the M matrix: RR indicates the ratio of the original R component in the newly generated R component, and GR indicates the ratio of the original R component in the new G component, BR indicates the ratio of the original R component to the new B component. And so on.
In addition, colormatrix in GDI + is a 5x5 matrix, rather than a 4x4 matrix. Why is one row and one column more.
Let's look at np. His first element is R1 * RR + G1 * RG + B1 * RB + A1 * ra. Do you see anything? That is, only three elements can be added, but not negative values. That is, if I want to perform the inverse color operation of R1 (minus the original value with 255), I will not be able to do it, therefore, based on the original one-dimensional and virtual one-dimensional W, the GDI + becomes R, G, B, A, W. This extra W does not exist at ordinary times. It is only used for color matrix calculation. The default value is 255.
We can see the result of the first NP element after adding a one-dimensional W.
R1 * RR + G1 * RG + B1 * RB + A1 * RA + W1 * RW
In this way, if RW is set to 1, RR is set to-1, and other values are 0, the result is 255-r1. How can we complete the Reversed Operation.
Technorati labels: colormatrix, GDI +, matrix, multiplication