256 color palette with alpha mix

xiaoxiao2021-03-06  51

This paper is taught in the TC in the 256-color tone panel mode of VGA, how to make Alpha hybrids. This is written in my VGA13H Graphics Lib for TC 2.0, basically achieves Alpha hybrid. The disadvantage is that the speed is too slow, and the division operations are used in both ARGB and GETRGB, and the speed is impossible to get up. And also made an error, which is meaningless in the 256-color mode of the alpha mix, and it is impossible to implement, and only 4 levels can be reached. In short, there are still many errors, recently repeatedly study Alpha mixed and palette, and new gains, new algorithms are sufficient to use real-time Alpha hybrids, and then write in the art. The 256 color video mode is due to the palette, the pixel value stored in the memory is actually the index number of the palette, and the 256-color BMP file is similar, and the data field is stored in the data field. The index number, this situation gives our alpha mixers to bring great inconvenience.

In actual applications, a fixed palette is typically selected, and then drawing operations under this palette. This is because a pair image has different display effects under different palettes, and two bitmaps using different palettes are in general, and they cannot be displayed on the same screen. So the usual situation is to select a universal palette, which is the problem of using this universal palette to use this generic palette to use this generic palette. In this way, how to choose a universal palette is the key.

Everyone knows that VGA / SVGA's palette register is usually 6 bits. Each register stores a color component, and a RGB color vector requires three registers to store, so you can represent 2 ^ 6 * 2 ^ 6 * 2 ^ 6 = 256K color, this color range is very large. The VGA / SVGA has only 256 sets of tones registers, so in such a vector space in (0, 0, 0) - (63, 63, 63), 256 color vectors are selected. This is similar to the base (very unrelated group) of the vector space in the linear algebra. Our task is similar to finding a base in this color space to be (0, 0, 0) - (63, 63, 63). Simply put to find 256 color vectors, form a color palette, and make this 256 colors evenly distributed in (0, 0, 0) - (63, 63, 63) this space, and also To ensure its independence. Detailed approach is no longer discussed, but only gives a relatively universal palette. R (i) = (i / 32% 8) * 9; g (i) = (i / 4% 8) * 9; B (i) = (i% 4) * 21; where i is the register group number, R (i), g (i), b (i) is the RGB color component value of the register, which is a transform type from I to R (I), G (I), B (I). Thereby, its inverse transform: i = r / 9 * 32 g / 9 * 4 b / 21;

Do it, you can do a multiplication and division operation, you can get the following: R (i) = (((i >> 5)% 8) << 3) ((i >> 5)% 8); g (i) = (((i >> 2) 4% 8) << 3) ((I >> 2) 4% 8); B (i) = ((i% 4) << 4) ( (i% 4) << 2) (i% 4);

I = ((R / 9) << 5) ((g / 9) << 2) B / 21;

According to these two transformations, we can write two macros for obtaining color number I, and (R, g, b) values ​​corresponding to color number I, and color number I. #define RGB (((((((((((((((((((((((g) / 9) << 2) (b) / 21) #define argb (r, g , b) RGB (R 4, G 4, B 10) #define getRGB (I, PR, PG, PB) {* (PR) = (((i >> 5)% 8) << 3) ((i >> 5)% 8); * (pg) = (((i >> 2) 4% 8) << 3) ((i >> 2) 4% 8); (* PB) = ((i% 4) << 4) ((i% 4) << 2) (I% 4);} where the Argb (Adjusted RGB) macro is corrected to the RGB macro because the RGB macro is in error.

In this way, we have established the correspondence between I and (R, G, B), which I have paved the road for our alpha.

Now talk about Alpha mixing. Alpha hybrid refers to a given two points P1, P2, and its RGB color component is (R1, G1, B1), (R2, G2, B2), respectively, assuming that the P1 is located behind P2, the transparency of P2 is A (0 %

Optimize results: R3 = R2 A * (R1-R2); G3 = G2 A * (G1-G2); B3 = B2 A * (B1-B2); a multiplication operation is performed less. However, since A is a floating point number, it is still very slow, so it is generally not used in the above formula, and the integer level Alpha is mixed, as follows: R2 = R2 N * (R1-R2) / 256; G2 = G2 N * (G1-G2) / 256; B2 = B2 n * (B1-B2) / 256; above 256 Alpha hybrid formula, since the VGA / SVGA palette register is 6bits, so 256 colors of Alpha hybrid significance Not big.

The 64-level Alpha hybrid formula is used: R2 = R2 N * (R1-R2) / 64; G2 = G2 N * (G1-G2) / 64; B2 = B2 N * (B1-B2) / 64;

Further optimization is L: R2 = R2 (N * (R1-R2) >> 6); G2 = G2 (N * (G1-G2) >> 6); B2 = B2 (N * (B1-B2) >> 6);

Only one multiplication is made, so the program should be able to run fast.

The alpha algorithm for mixing a point is given: int Alpha (int P1, int P2, int N) {Int C1 [3]; int C2 [3]; int C3 [3];

GETRGB (P1, C1, C1 1, C1 2); GETRGB (P2, C2, C2 1, C2 2);

C3 [0] = C2 [0] (N * (C1 [0] -C2 [0]) >> 6); C3 [1] = C2 [1] (N * (C1 [1] -C2 [ 1]) >> 6); C3 [2] = C2 [2] (N * (C1 [2] -C2 [2]) >> 6); RETURN Argb (C3 [0], C3 [1], C3 [2]);

For translucent mixing, there can be a faster formula: R2 = R2 ((R1-R2) >> 1); G2 = G2 ((G1-G2) >> 1); b2 = b2 ((B1-B2) >> 1); this formula does not have multiplication and division, and translucent is also widely used in the game.

The following is a translucent alpha mix: int Alpha (int P1, int P2, int N) {Int C1 [3]; int C2 [3]; int C3 [3];

GETRGB (P1, C1, C1 1, C1 2); GETRGB (P2, C2, C2 1, C2 2);

C3 [0] = C2 [0] ((C1 [0] -C2 [0]) >> 1); C3 [1] = C2 [1] ((C1 [1] -C2 [1])> > 1); C3 [2] = C2 [2] ((C1 [2] -C2 [2]) >> 1);

Return Argb (C3 [0], C3 [1], C3 [2]);

For the multiplication of the N-class Alpha hybrid, we also have a way to further optimize, and can use the technology of shift multiplication to achieve fast multiplication, but the performance is not large, and interesting friends can check the relevant information, this is no longer detailed .

(Note: The above implementation is in my function library called VGA13h Graphics Lib inside an active program, you can download on my website, address: http://rockcarry.126.com)

Author: Chen Kai 2004.10.10 All Rights Reserved


New Post(0)