YUV and RGB conversion in various formats

Source: Internet
Author: User

This article from http://blog.csdn.net/hardVB/archive/2007/10/18/1831810.aspx

 

/Yuy2 video conversion RGB format by hard 2007-10-17
// Http://www.fourcc.org/fccyvrgb.php
Inline void yuy2_rgb (byte * yuy2buff, byte * rgbbuff, DWORD dwsize)
{

// B = 1.164 (Y-16) + 2.018 (u-128)
// G = 1.164 (Y-16)-0.813 (V-128)-0.391 (u-128)
// R = 1.164 (Y-16) + 1.596 (V-128)
Byte * orgrgbbuff = rgbbuff;
For (dword count = 0; count <dwsize; count + = 4)
{
// Y0 U0 Y1 V0
Float Y0 = * yuy2buff;
Float u = * (++ yuy2buff );
Float Y1 = * (++ yuy2buff );
Float v = * (++ yuy2buff );
++ Yuy2buff;

* (Rgbbuff) = (byte) (y0 + (1.370705*(V-128 )));
* (++ Rgbbuff) = (byte) (y0-(0.698001*(V-128)-(0.337633*(U-128 )));
* (++ Rgbbuff) = (byte) (y0 + (1.732446*(U-128 )));

* (++ Rgbbuff) = (byte) (Y1 + (1.370705*(V-128 )));
* (++ Rgbbuff) = (byte) (Y1-(0.698001*(V-128)-(0.337633*(U-128 )));
* (++ Rgbbuff) = (byte) (Y1 + (1.732446*(U-128 )));
++ Rgbbuff;
}
}
Inline void yuy2_rgb2 (byte * yuy2buff, byte * rgbbuff, DWORD dwsize)
{

/// R = Y + 1.14 V
// G = Y-0.39u-0.58 V
// B = Y + 2.03u

// R = Y + 1.403v'
// G = Y-0.344u '-0.714v'
// B = Y + 1.770u'
Byte * orgrgbbuff = rgbbuff;
For (dword count = 0; count <dwsize; count + = 4)
{
// Y0 U0 Y1 V0
Float Y0 = * yuy2buff;
Float u = * (++ yuy2buff );
Float Y1 = * (++ yuy2buff );
Float v = * (++ yuy2buff );
++ Yuy2buff;

Float R, G, B;

R = (y0 + 1.14f * V );
G = (y0-0.39f * U-0.58f * V );
B = (y0 + 2.03f * U );
If (r <0) r = 0;
If (r> 255) r = 255;
If (G <0) G = 0;
If (G> 255) G = 255;
If (B <0) B = 0;
If (B> 255) B = 255;

* (Rgbbuff) = (byte) B;
* (++ Rgbbuff) = (byte) g;
* (++ Rgbbuff) = (byte) R;

R = (Y1 + 1.14f * V );
G = (Y1-0.39f * U-0.58f * V );
B = (Y1 + 2.03f * U );
If (r <0) r = 0;
If (r> 255) r = 255;
If (G <0) G = 0;
If (G> 255) G = 255;
If (B <0) B = 0;
If (B> 255) B = 255;

* (++ Rgbbuff) = (byte) B;
* (++ Rgbbuff) = (byte) g;
* (++ Rgbbuff) = (byte) R;
++ Rgbbuff;
}
}

Inline void yuy2_rgb4 (byte * yuy2buff, byte * rgbbuff, DWORD dwsize)
{

// R = Y + 1.403v'
// G = Y-0.344u '-0.714v'
// B = Y + 1.770u'
Byte * orgrgbbuff = rgbbuff;
For (dword count = 0; count <dwsize; count + = 4)
{
// Y0 U0 Y1 V0
Float Y0 = * yuy2buff;
Float u = * (++ yuy2buff );
Float Y1 = * (++ yuy2buff );
Float v = * (++ yuy2buff );
++ Yuy2buff;

Float R, G, B;

R = (y0 + 1.403f * V );
G = (y0-0.344f * U-0.714f * V );
B = (y0 + 1.77f * U );
If (r <0) r = 0;
If (r> 255) r = 255;
If (G <0) G = 0;
If (G> 255) G = 255;
If (B <0) B = 0;
If (B> 255) B = 255;

* (Rgbbuff) = (byte) R;
* (++ Rgbbuff) = (byte) g;
* (++ Rgbbuff) = (byte) B;
R = (Y1 + 1.403f * V );
G = (Y1-0.344f * U-0.714f * V );
B = (Y1 + 1.77f * U );
If (r <0) r = 0;
If (r> 255) r = 255;
If (G <0) G = 0;
If (G> 255) G = 255;
If (B <0) B = 0;
If (B> 255) B = 255;

* (++ Rgbbuff) = (byte) R;
* (++ Rgbbuff) = (byte) g;
* (++ Rgbbuff) = (byte) B;
++ Rgbbuff;
}
}

Inline void yuy2_rgb3 (byte * yuy2buff, byte * rgbbuff, DWORD dwsize)
{

// B = 1.164 (Y-16) + 2.018 (u-128)
// G = 1.164 (Y-16)-0.813 (V-128)-0.391 (u-128)
// R = 1.164 (Y-16) + 1.596 (V-128)
Byte * orgrgbbuff = rgbbuff;
For (dword count = 0; count <dwsize; count + = 4)
{
// Y0 U0 Y1 V0
Float Y0 = * yuy2buff;
Float u = * (++ yuy2buff );
Float Y1 = * (++ yuy2buff );
Float v = * (++ yuy2buff );
++ Yuy2buff;

* (Rgbbuff) = (byte) (1.164f * (y0-16) + 2.018f * (u- 128 ));
* (++ Rgbbuff) = (byte) (1.164f * (y0-16)-0.813f * (V-128)-0.391f * (u-128 ));
* (++ Rgbbuff) = (byte) (1.164f * (y0-16) + 1.596f * (V-128 ));

* (++ Rgbbuff) = (byte) (1.164f * (y0-16) + 2.018f * (u-128 ));
* (++ Rgbbuff) = (byte) (1.164f * (y0-16)-0.813f * (V-128)-0.391f * (u-128 ));
* (++ Rgbbuff) = (byte) (1.164f * (y0-16) + 1.596f * (V-128 ));
++ Rgbbuff;

}
}
// Http://msdn2.microsoft.com/en-us/library/ms893078.aspx

Void vc4000graber: yuy2_rgb (byte * yuy2buff, byte * rgbbuff, DWORD dwsize)
{
//
// C = Y-16
// D = u-128.
/// E = V-128
// R = clip (298 * C + 409 * E + 128)> 8)
// G = clip (298 * C-100 * D-208 * E + 128)> 8)
// B = clip (298 * C + 516 * D + 128)> 8)
Byte * orgrgbbuff = rgbbuff;
For (dword count = 0; count <dwsize; count + = 4)
{
// Y0 U0 Y1 V0
Byte Y0 = * yuy2buff;
Byte u = * (++ yuy2buff );
Byte Y1 = * (++ yuy2buff );
Byte v = * (++ yuy2buff );
++ Yuy2buff;

Long y, C, D, E;
Byte R, G, B;

Y = y0;
C = Y-16;
D = U-128;
E = V-128;
R = clip255 (298 * C + 409 * E + 128)> 8 );
G = clip255 (298 * C-100 * D-208 * E + 128)> 8 );
B = clip255 (298 * C + 516 * D + 128)> 8 );

* (Rgbbuff) = B;
* (++ Rgbbuff) = g;
* (++ Rgbbuff) = R;

Y = Y1;
C = Y-16;
D = U-128;
E = V-128;
R = clip255 (298 * C + 409 * E + 128)> 8 );
G = clip255 (298 * C-100 * D-208 * E + 128)> 8 );
B = clip255 (298 * C + 516 * D + 128)> 8 );
* (++ Rgbbuff) = B;
* (++ Rgbbuff) = g;
* (++ Rgbbuff) = R;
++ Rgbbuff;

}
}

Byte vc4000graber: clip255 (long V)
{
If (v <0) V = 0;
Else if (V> 255) V = 255;
Return (byte) V;
}

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.