Tips: RGB and YUV ---- excerpt from "DirectShow Practice Featured in" Author: Lu Qiming
The principle of the computer color display shows the color TV, which is the principle of R (RED), G (Green), B (Blue) plus color coloration: the inside of the screen by emitting three different intensity electron beams. The covered red, green, and blue phosphor material produces color. This color representation is referred to as RGB color space representation (it is also a color space representation method for multimedia computer technology).
According to the principle of tricho, any color light F can be mixed with different components of R, g, and b.
F = r [r] g [g] b [b]
Among them, R, g, and B are three basins involved in the coefficient of mixing. When the tricho component is 0 (the most weaker), it is mixed as black light; and when the tricho component is a K (strongest), it is mixed into white light. Adjust the value of the three coefficients of R, G, and B, which can be mixed with a variety of color light between black light and white light.
So what is YUV coming? In a modern color TV system, three-tube color camera or color CCD camera is usually used for imaging, and then the color image signal taken is divided, and the correction is enlarged to obtain the RGB, and then the matrix conversion circuit is obtained by the matrix conversion circuit to obtain the brightness signal Y and two. The color difference signals r-y (ie U), B-Y (ie, V), the last transmitting end encodes the brightness and color difference, respectively, and transmits it with the same channel. This color representation is the so-called YUV color space representation.
The importance of using YUV color space is its brightness signal Y and chroma signal U, V is separated. If only the Y signal component is not u, V component, then the image represented is a black and white grayscale image. Color TV uses YUV space to solve the compatibility of color TVs and black and white TVs with brightness signal Y, so that black and white televisions can also receive color TV signals.
The formula of YUV and RGB mutual conversion is as follows (RGB range value range 0-255):
Y = 0.299r 0.587g 0.114b
U = -0.147r - 0.289g 0.436B
V = 0.615R - 0.515G - 0.100B
R = Y 1.14V
G = y - 0.39u - 0.58V
B = Y 2.03U
In DirectShow, common RGB formats have RGB1, RGB4, RGB8, RGB565, RGB555, RGB24, RGB32, Argb32, etc .; Common YUV formats are yuy2, yuyv, yvyu, uyvy, AYUV, Y41P, Y411, Y211, IF09, IYUV , YV12, YVU9, YUV411, YUV420, etc. As a secondary type of video media type, the corresponding GUID is shown in Table 2.3.
Table 2.3 Common RGB and YUV format
GUID
Format description
MediaSubType_RGB1
2 colors, each pixel is used for 1 point, requires a palette
MediaSubtype_RGB4
16 colors, each pixel is represented by 4 digits, which requires a palette
MediaSubtype_RGB8
256 colors, each pixel is indicated by 8 bits, which requires a palette
MediaSubType_RGB565
Each pixel uses 16 bits, and the RGB components use 5, 6, 5, respectively.
MediaSubType_RGB555
Each pixel uses 16 points, RGB components use 5 bits (the remaining 1 bit is not available)
MediaSubType_RGB24 Each pixel is used 24 bits, and the RGB component uses 8 digits.
MediaSubtype_RGB32
Each pixel uses 32 points, and the RGB component uses 8 bits (the remaining 8 bits are not available)
MediaSubtype_argb32
Each pixel uses 32 bits, and the RGB component uses 8 bits (the remaining 8 bits are used to represent the Alpha channel value)
MediaSubType_yuy2
Yuy2 format, packaged in 4: 2: 2
MediaSubtype_yuyv
Yuyv format (actual format is the same as yuy2)
MediaSubtype_yvyu
Yvyu format, packaged in 4: 2: 2
MediaSubtype_uyvy
UYVY format, packaged in 4: 2: 2
MediaSubType_ayuv
4: 4: 4: 4: 4: 4 yUV format with Alpha channel
MediaSubType_Y41P
Y41P format, packaged in 4: 1: 1
MediaSubtype_Y411
Y411 format (the actual format is the same as Y41P)
MediaSubtype_y211
Y211 format
MediaSubtype_if09
IF09 format
MediaSubtype_iyuv
Iyuv format
MediaSubtype_yv12
YV12 format
MediaSubType_YVU9
YVU9 format
There are various RGB formats below.
¨ RGB1, RGB4, RGB8 are all RGB formats of palette types, which are usually in the format details describing these media types.
The BitMapInfoHeader data structure follows a palette (defined a series of colors). Their image data is not true
The color value, but the index of the current pixel color value in the palette. Take RGB1 (2 color space map) as an example, such as its palette
The two color values defined are 0x000000 (black) and 0xFFFFFF (White), then image data 00110101111 ...
(1 point of each pixel) indicates that the color of each pixel is: black black and white black and white black and white.
¨ RGB565 uses 16 bits to represent a pixel, 5 bits in this 16-bit for R, 6 bits for G, 5 bits for B. Usually used in the program
One word (Word, one word is equal to two bytes) to operate a pixel. When reading a pixel, the meaning of this word is like
under:
High byte low byte
R R r R r R g g g g g g g b b b b
You can use the shielding word and shift operation to get the value of each component of the RGB:
#define rgb565_mask_red 0xF800
#define rgb565_mask_green 0x07e0
#define rgb565_mask_blue 0x001F
R = (wpixel & rgb565_mask_red >> 11; // Value range 0-31
G = (WPIXEL & RGB565_MASK_GREEN) >> 5; // Value range 0-63
B = WPIXEL & RGB565_MASK_BLUE; // Range range 0-31
¨ RGB555 is another 16-bit RGB format, and RGB components are represented by 5 digits (the remaining 1 bits are not available). Read one by word
After the pixel, the sense of this word is as follows:
High byte low byte
X R r R r r r r r r R g g g g g g B B B B B (x is not used, it can be ignored) can combine the value of each component of the RGB using the shielding word and shift operation:
#DEFINE RGB555_MASK_RED 0X7C00
#define rgb555_mask_green 0x03e0
#DEFINE RGB555_MASK_BLUE 0x001F
R = (WPIXEL & RGB555_MASK_RED) >> 10; // Value range 0-31
G = (wpixel & rgb555_mask_green) >> 5; // Value range 0-31
B = WPIXEL & RGB555_MASK_BLUE; // Value range 0-31
¨ RGB24 uses 24 points to represent a pixel, and the RGB component is represented by 8 bits, and the value range is 0-255. Note that RGB in memory
The order of arrangement is: BGR BGR BGR .... You can usually use the RGBTRIPLE data structure to operate a pixel, its definition
for:
TypedEf struct tagrgbtriple {
Byte rgbtblue; // blue component
Byte rgbtgreen; // green component
Byte rgbtred; // red component
} RGBTRIPLE;
¨ RGB32 uses 32 points to represent a pixel, and the RGB component is used to 8 bits, and the remaining 8 bits are used as Alpha channels or not. (Argb32
It is RGB32 with Alpha channel. Note The order in which the RGB components in the memory is: BGRA BGRA BGRA .... Usually
Use the RGBQUAD data structure to operate a pixel, it is defined as:
Typedef struct tagrgbquad {
Byte rgbblue; // blue component
Byte rgbgreen; // green component
BYTE RGBRED; / / Red component
Byte rgbreserved; // Reserved byte (used as Alpha channel or ignore)
} RGBQUAD;
Here is a variety of YUV formats. The YUV format usually has two major categories: Packed format and plane (Planar) format. former
Store the YUV component in the same array, usually several adjacent pixels constitute a macro-pixel; and the latter
The three components stored in three arrays were used, just like a three-dimensional plane. The yuy2 to Y211 in Table 2.3 is all packaged formats.
And IF09 to YVU9 are flat formats. (Note: When introducing various specific formats, all YUV components will have subscripts, such as Y0, U0, V0.
The YUV component, Y1, U1, V1 representing the first pixel represents the YUV component of the second pixel, and so on. )
¨ Yuy2 (and yuyv) format The Y component is reserved for each pixel, while the UV component samples every two pixels in the horizontal direction. One
The macro pixel is 4 bytes, actually represents 2 pixels. (4: 2: 2 means 4 Y components in a macro pixel, 2 u components and 2
V component. The order of the YUV component in the image data is as follows:
Y0 U0 Y1 V0 Y2 U2 Y3 V2 ...
¨ YVYU format is similar to yuy2, but the order of the YUV components in image data is different:
Y0 V0 Y1 U0 Y2 V2 Y3 U2 ...
¨ UYVY format is similar to yuy2, but the order of the YUV component is different in image data: U0 Y0 V0 Y1 U2 Y2 V2 Y3 ...
¨ Ayuv format with an Alpha channel, and extracts the YUV component for each pixel, the image data format is as follows:
A0 Y0 U0 V0 A1 Y1 U1 V1 ...
¨ Y41P (and y411) format The Y component is retained for each pixel, and the UV component is sampled once every 4 pixels in the horizontal direction. One
The macro pixel is 12 bytes, actually represents 8 pixels. The order of YUV components in image data is as follows:
U0 Y0 V0 Y1 U4 Y2 V4 Y3 Y4 Y5 Y6 Y8 ...
¨ Y211 format Sampling every 2 pixels in the horizontal direction, and the UV component is sampled once every 4 pixels. A macro pixel
4 bytes, actually represent 4 pixels. The order of YUV components in image data is as follows:
Y0 U0 Y2 V0 Y4 U4 Y6 V4 ...
¨ YVU9 format The Y component is extracted for each pixel, and when the UV component is extracted, first divide the image into a macroblock of several 4 x 4,
Each macroblock is then extracted with a U component and a V component. When the image data is stored, the first is the Y component array of the entire image, then
Just follow the u component array, and the V component array. The IF09 format is similar to YVU9.
¨ IYUV Format Each pixel is extracted, and when the UV component is extracted, the image is first divided into a macroblock of several 2 x 2,
Each macroblock is then extracted with a U component and a V component. YV12 format is similar to Iyuv.
¨ YUV411, YUV420 format is more than DV data, for the former for NTSC system, the latter for PAL system. YUV411 is all pixels
The Y component is extracted, and the UV component is sampled once every 4 pixels in the horizontal direction. YUV420 is not a V component sampled 0, but with YUV411
Compared to the horizontal direction, a double color difference is reduced in a U / V interval in the vertical direction, and
As shown in Figure 2.12.
Figure 2.12 YUV411 and YUV420 sampling format