Convert yv12 to rgb24

Source: Internet
Author: User

Note: The yv12 type of data collected from the hikvision camera must be converted to rgb24 for data processing. The data collected from other cameras is generally rgb24, which can be processed directly.

Bool yv12_to_rgb24 (unsigned char * pyv12, unsigned char * prgb24, int iwidth, int iheight)

{
If (! Pyv12 |! Prgb24)
Return false;
Const long nylen = long (iheight * iwidth );
Const int nhfwidth = (iwidth> 1 );
If (nylen <1 | nhfwidth <1)
Return false;
// Yv12 data format, where the Y component length is width * height, and the u and V component lengths are both width * Height/4
// | Width |
// Y... y --------
// Y... y height
// Y... y
// Y... y --------
// V. v
// V. v
// U .. u
// U .. u
Unsigned char * ydata = pyv12; // The first address of Y
Unsigned char * vdata = & ydata [nylen]; // The first address of the U.
Unsigned char * udata = & vdata [nylen> 2]; // The first address of V
If (! Udata |! Vdata)
Return false;
// Convert yv12 to rgb24
Int RGB [3];
Int I, j, m, n, x, y;
M =-iwidth;
N =-nhfwidth;
For (y = 0; y <iheight; y ++)
{
M + = iwidth; // index of an element in the first row
If (! (Y % 2 ))
N + = nhfwidth;
For (x = 0; x <iwidth; X ++)
{
I = m + X;
J = N + (x> 1 );
RGB [2] = int (ydata [I] + 1.370705 * (vdata [J]-128); // R component Value
RGB [1] = int (ydata [I]-0.698001 * (udata [J]-128)-0.703125 * (vdata [J]-128); // G component Value
RGB [0] = int (ydata [I] + 1.732446 * (udata [J]-128); // B component Value
J = nylen-iwidth-M + X;
I = (j <1) + J;
For (Int J = 0; j <3; j ++)
{
If (RGB [J]> = 0 & RGB [J] <= 255)
Prgb24 [I + J] = RGB [J];
Else
Prgb24 [I + J] = (RGB [J] <0 )? 0: 255;
}
}
}
Return true;

}

Note:

Frame_info * pframeinfo;

Iwidth is pframeinfo-> nwidth

Iheight is pframeinfo-> nheight

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.