DirectX -- overlay the characters on yuv420p, and yuv420p

Source: Internet
Author: User

DirectX -- overlay the characters on yuv420p, and yuv420p


Unsigned char * pTemp;
BYTE OsdY = 0;
BYTE OsdU = 0;
BYTE OsdV = 0;

Void OSDSetTextColor (BYTE OsdR, BYTE OsdG, BYTE OsdB)
{
OsdY = 0.25 * OsdR + 0.504 * OsdG + 0.098 * OsdB + 16;
OsdU =-0.148 * OsdR-0.291 * OsdG + 0.439 * OsdB + 128;
OsdV = 0.439 * OsdR-0.368 * OsdG-0.071 * OsdB + 128;
}
Void CTest0Dlg: setOSD ()
{
FILE * fp = fopen ("d: \ temp \ OSD.bmp", "rb ");
If (fp = 0)
Return;
HDC hDC =: GetDC (GetSafeHwnd ());
HDC hMemDC = CreateCompatibleDC (hDC );
Fseek (fp, sizeof (BITMAPFILEHEADER), 0 );
BITMAPINFOHEADER head;
Fread (& head, sizeof (BITMAPINFOHEADER), 1, fp );
Int bmpHeight = head. biHeight;
Int BMP width = head. biWidth;
Int biBitCount = head. biBitCount;
Int lineByte = (BMP width * biBitCount/8 + 3)/4*4;
RGBQUAD * pColorTable;
Unsigned char * pBmpBuf = new unsigned char [lineByte * bmpHeight];
Fread (pBmpBuf, 1, lineByte * bmpHeight, fp );
Memset (pBmpBuf, 0, lineByte * bmpHeight );

CDC * m_pMemDC;
M_pMemDC = new CDC ();
CDC * pDC;
PDC = GetDC ();
M_pMemDC-> CreateCompatibleDC (pDC );
CBitmap * m_pOldBmp = NULL;
CBitmap * m_pMemBmp = new CBitmap (); // create a compatible Bitmap Based on the image size
M_pMemBmp-> CreateCompatibleBitmap (pDC, BMP width, bmpHeight );
M_pOldBmp = m_pMemDC-> SelectObject (m_pMemBmp );

// Draw the image data to a compatible bitmap

SetDIBits (m_pMemDC-> GetSafeHdc (), (HBITMAP) m_pMemBmp-> m_hObject,
0, bmpHeight, (LPVOID) pBmpBuf, (BITMAPINFO *) & head, DIB_RGB_COLORS );
M_pMemDC-> SetBkMode (TRANSPARENT );
M_pMemDC-> SetTextColor (RGB (255, 0, 0); // RGB flag color
OSDSetTextColor (0, 0, 0); // OSD display color

// Set the font
CFont font;
Font. CreateFont (20, 15, 0, 10, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_SWISS, "");
SelectObject (hMemDC, font );
///////////////////////////
SYSTEMTIME st;
CString strDate, strTime;
GetLocalTime (& st );
StrDate. Format ("time % 04d-% 02d-% 02d", st. wYear, st. wMonth, st. wDay );
StrTime. Format ("% 02d: % 02d: % 02d", st. wHour, st. wMinute, st. wSecond );
// Add text to a specified position
M_pMemDC-> TextOut (20, 40, strDate + strTime );

PTemp = new unsigned char [lineByte * bmpHeight];
GetDIBits (m_pMemDC-> GetSafeHdc (), (HBITMAP) m_pMemBmp-> m_hObject, 0, bmpHeight,
(LPVOID) pTemp, (BITMAPINFO *) & head, DIB_RGB_COLORS );
Fclose (fp );

Delete m_pMemBmp;
Delete m_pMemDC;
Delete [] pBmpBuf;


// MessageBox ("VOER! ");
}

Call part

//////////////////////////////////////// //////////////////////////////////////// //////
SetOSD ();
For (int I = 0; I <80; I ++)
{
For (int k = 0; k <640; k ++)
{
// Dc. setPixel (k, 79-i, RGB (pTemp [(I * 640 + k) * 3 + 2], pTemp [(I * 640 + k) * 3 + 1], pTemp [(I * 640 + k) * 3 + 0]);
If (pTemp [(I * 640 + k) * 3 + 2]! = 0) // You Need to overlay the filter by marking the color.
{
PFrameYUV-> data [0] [(79-i) * 720 + k] = OsdY;
If (79-i) % 2 = 0)
{
PFrameYUV-> data [1] [(79-i) x 720/4 + k/2] = OsdU;
PFrameYUV-> data [2] [(79-i) x 720/4 + k/2] = OsdV;
}

}
}
}
//////////////////////////////////////// //////////////////////////////////////// /////////
Memcpy (image-> pixels [0], pFrameYUV-> data [0], 720*576); // copy the data yuv420, or change the pointer in advance
Memcpy (image-> pixels [2], pFrameYUV-> data [1], 720*576/4 );
Memcpy (image-> pixels [1], pFrameYUV-> data [2], 720*576/4 );

.........................

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.