How to convert RGB color information and YUV color information

Source: Internet
Author: User


GUID Format Description
MEDIASUBTYPE_RGB1 2 colors, each pixel is represented by 1 bits, need palette
MEDIASUBTYPE_RGB4 16 colors, each pixel is represented by 4 bits, need palette
MEDIASUBTYPE_RGB8 256 colors, each pixel is represented by 8 bits, need palette
mediasubtype_rgb565 each pixel is represented by 16 bits, the RGB component uses 5-bit, 6-bit, 5-bit
mediasubtype_rgb555 each pixel is represented by 16 bits, and the RGB component uses 5 bits (the remaining 1 bits are not used)
Mediasubtype_rgb24 each pixel is represented by 24 bits, and the RGB component uses 8 bits each
Mediasubtype_rgb32 each pixel is represented by 32 bits, the RGB component uses 8 bits (the remaining 8 bits are not used)
Mediasubtype_argb32 each pixel is represented by 32 bits, the RGB components each use 8 bits (the remaining 8 bits are used to represent the alpha channel value)
MEDIASUBTYPE_YUY2 YUY2 format, packaged in 4:2:2 manner
MEDIASUBTYPE_YUYV yuyv Format (the actual format is the same as YUY2)
Mediasubtype_yvyu yvyu format, packaged in 4:2:2 manner
Mediasubtype_uyvy UYVY format, packaged in 4:2:2 manner
MEDIASUBTYPE_AYUV 4:4:4 YUV format with alpha channel
MEDIASUBTYPE_Y41P y41p format, packaged in 4:1:1 manner
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

The following is a description of each RGB format.

¨RGB1, RGB4, RGB8 are both color palette-type RGB formats, and when describing the format details of these media types, a palette (defining a series of colors) is typically followed by the Bitmapinfoheader data structure. Their image data is not a true color value, but rather the index of the current pixel color value in the palette. Take RGB1 (2-color bitmap) as an example, for example, the two color values defined in its palette are 0x000000 (black) and 0xFFFFFF (white), then the image data 001101010111 ... (1 bits per pixel) indicates the color of the corresponding pixels: black black and white black white

The ¨rgb565 uses a 16-bit representation of a pixel, 5 bits in 16 bits for R, 6 bits for g, and 5 bits for B. The program typically uses a single word (word, a word equal to two bytes) to manipulate a pixel. When a pixel is read, the meanings of each bit of the word are as follows:
High byte low byte
R-R r r R G G G G G G B b b B. b b
You can use the mask and shift operations together to get the values of the RGB components:

#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; Value Range 0-31

¨RGB555 is another 16-bit RGB format, and the RGB components are represented by 5 bits (the remaining 1 bits are not used). After reading a single pixel with one word, the meanings of each bit of the word are as follows:
High byte low byte
X r r r R G G G G G B b b b b (x indicates no, can be omitted)
You can use the mask and shift operations together to get the values of the RGB components:

#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 bits to represent a pixel, and the RGB component is represented by 8 bits, with a value range of 0-255. Note in memory that the RGB components are arranged in the order of: BGR BGR BGR .... You can usually use the Rgbtriple data structure to manipulate a pixel, which is defined as:

typedef struct TAGRGBTRIPLE {
BYTE Rgbtblue; Blue Component
BYTE Rgbtgreen; Green component
BYTE rgbtred; Red component
} rgbtriple;

The ¨RGB32 uses 32 bits to represent a pixel, the RGB components are 8 bits each, and the remaining 8 bits are used as alpha channels or not. (ARGB32 is a RGB32 with an alpha channel.) Note that in-memory RGB components are arranged in the order of: BGRA BGRA BGRA .... You can usually use the Rgbquad data structure to manipulate a pixel, which is defined as:

typedef struct TAGRGBQUAD {
BYTE Rgbblue; Blue Component
BYTE Rgbgreen; Green component
BYTE rgbred; Red component
BYTE rgbreserved; Reserved bytes (used as alpha channel or ignored)
} Rgbquad;

Various YUV formats are described below. The YUV format typically has two main classes: the packaged (packed) format and the planar format. The former stores YUV components in the same array, usually several adjacent pixels to form a macro pixel (Macro-pixel), and the latter uses three arrays to store the YUV three components separately, as if it were a three-dimensional plane. The YUY2 to Y211 in table 2.3 are packaged formats, and IF09 to YVU9 are flat formats. (Note: In the introduction of various specific formats, the YUV components will have subscript, such as Y0, U0, V0 represents the YUV component of the first pixel, Y1, U1, V1 represents the YUV component of the second pixel, and so on. )

The ¨yuy2 (and YUYV) format retains the Y component for each pixel, while the UV component is sampled every two pixels in the horizontal direction. A macro pixel is 4 bytes, which actually represents 2 pixels. (4:2:2 means that there are 4 y components, 2 U-components, and 2 v components in a macro pixel.) The YUV components in the image data are arranged in the following order:
Y0 U0 Y1 V0 Y2 U2 Y3 V2 ...

The ¨yvyu format is similar to YUY2, except that the order of YUV components in the image data is different:
Y0 V0 Y1 U0 Y2 V2 Y3 U2 ...

The ¨uyvy format is similar to YUY2, except that the order of YUV components in the image data is different:
U0 Y0 V0 Y1 U2 Y2 V2 Y3 ...

The ¨AYUV format has an alpha channel, and the YUV component is extracted for each pixel, and the image Data format is as follows:
A0 Y0 U0 V0 A1 Y1 U1 V1 ...

The ¨y41p (and Y411) format retains the Y component for each pixel, while the UV component is sampled every 4 pixels in the horizontal direction. A macro pixel is 12 bytes, which actually represents 8 pixels. The YUV components in the image data are arranged in the following order:
U0 Y0 V0 Y1 U4 Y2 V4 Y3 Y4 Y5 Y6 Y8 ...

The ¨y211 format samples the Y component in a horizontal direction every 2 pixels, and the UV component is sampled every 4 pixels. A macro pixel is 4 bytes, which actually represents 4 pixels. The YUV components in the image data are arranged in the following order:
Y0 U0 Y2 V0 Y4 U4 Y6 V4 ...

The ¨YVU9 format extracts the y component for each pixel, and when the UV component is extracted, the image is first divided into several 4 x 4 macro blocks, and then each macro block extracts a U component and a V component. When the image data is stored, the first is the Y-component array of the entire image, followed by the U-component array, and the V-component array. The IF09 format is similar to YVU9.

The ¨IYUV format extracts the y component for each pixel, and when the UV component is extracted, the image is first divided into several 2 x 2 macro blocks, and then each macro block extracts a U component and a V component. The YV12 format is similar to IYUV.

¨yuv411, YUV420 format is found in DV data, the former is used in NTSC system, the latter is used in PAL system. The YUV411 extracts the Y component for each pixel, and the UV component is sampled every 4 pixels in a horizontal direction. The YUV420 is not a V-component sample of 0, but increases the chromatic aberration sampling frequency in the horizontal direction compared to the YUV411, reducing half of the chromatic aberration in the vertical direction by the u/v interval.

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.