When video frames are collected by VFW
1. if the camera uses the rgb24 output, the RGB three-part sorting order in the buffer is [B0, G0, R0, B1, G1, R1 .... BN, GN, Rn], you can directly use stretchdibits to draw a picture, but you must fill in the bitmapinfo information structure before drawing. The content in this struct can be obtained from the capcapturegetsetup function in the VFW function library.
2. If the camera uses the yuy2 output, because the stretchdibits function can only use the RGB color space, it must first be converted to RGB data. In this case, the data in the buffer obtained by the VFW callback is arranged as follows: [y0 U0 Y1 V0 Y2 U1 Y3 V1] The Conversion Function is as follows:
/*
Every four consecutive yuvs indicate that two pixels of UV are shared.
1. y0 U0 Y1 V0 ==> [y0 U0 V0] [Y1 U0 V0] represents two pixels
*/
Void yuy2_rgb2 (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 clip255 (long V)
{
If (v <0) V = 0;
Else if (V> 255) V = 255;
Return (byte) V;
}
After YUV Conversion, if you want to call stretchdibits for drawing, the RGB components in the input buffer must be arranged in BGR order. Otherwise, the color is reversed, therefore, the above conversion function also uses the BGR sort order to sort the order.
3. If the camera outputs the data of rgb16, we only need to set the bitmap attribute to be consistent with the output attribute when drawing the stretchdibits.
To use setpiexel to draw pixels, you must extend rgb16 (rgb555, rgb565) to rgb24 (rgb888)
The extension formula is as follows:
Here the source is rgb16 (rgb555)
Void CDIB: convertline16to24_555 (byte * target, byte * Source, int width_in_pixels)
{
Word * bits = (word *) source;
For (INT Cols = 0; Cols <width_in_pixels; Cols ++)
{
Target [0] = (BITS [Cols] & 0x7c00)> 10) * 0xff)/0x1f;
Target [1] = (BITS [Cols] & 0x03e0)> 5) * 0xff)/0x1f;
Target [2] = (BITS [Cols] & 0x001f)> 0) * 0xff)/0x1f;
Target + = 3;
}
}