• DirectX--yuv420p上实现的字符叠加


     1 unsigned char *pTemp;    
     2 BYTE OsdY = 0;
     3 BYTE OsdU = 0;
     4 BYTE OsdV = 0;
     5 
     6 void OSDSetTextColor(BYTE OsdR, BYTE OsdG, BYTE OsdB)
     7 {
     8     OsdY = 0.25*OsdR+0.504*OsdG+0.098*OsdB+16;
     9     OsdU = -0.148*OsdR-0.291*OsdG+0.439*OsdB+128;
    10     OsdV = 0.439*OsdR-0.368*OsdG-0.071*OsdB+128;
    11 }
    12 
    13 void CTest0Dlg::setOSD()
    14 {
    15     FILE *fp=fopen("d:\temp\OSD.bmp", "rb");
    16     if (fp == 0)
    17         return;
    18 
    19     HDC hDC = ::GetDC(GetSafeHwnd());
    20     HDC hMemDC = CreateCompatibleDC(hDC);
    21     fseek(fp, sizeof(BITMAPFILEHEADER), 0);
    22     BITMAPINFOHEADER head;
    23     fread(&head, sizeof(BITMAPINFOHEADER), 1, fp);
    24     int bmpHeight = head.biHeight;
    25     int bmpWidth = head.biWidth;
    26     int biBitCount = head.biBitCount;
    27     int lineByte = (bmpWidth*biBitCount/8+3)/4*4;
    28     RGBQUAD *pColorTable;
    29     unsigned char *pBmpBuf = new unsigned char[lineByte*bmpHeight];
    30     fread(pBmpBuf, 1, lineByte*bmpHeight, fp);
    31     memset(pBmpBuf,0, lineByte*bmpHeight);
    32     
    33     CDC *m_pMemDC;
    34     m_pMemDC = new CDC();
    35     CDC *pDC;
    36     pDC = GetDC();
    37     m_pMemDC->CreateCompatibleDC(pDC);
    38     CBitmap *m_pOldBmp = NULL;
    39     CBitmap *m_pMemBmp = new CBitmap();//根据图片的大小创建一个兼容位图
    40     m_pMemBmp->CreateCompatibleBitmap(pDC, bmpWidth, bmpHeight);
    41     m_pOldBmp = m_pMemDC->SelectObject(m_pMemBmp);
    42     
    43     // 把图像的数据绘制到兼容位图上
    44     SetDIBits(m_pMemDC->GetSafeHdc(), (HBITMAP)m_pMemBmp->m_hObject, 0, bmpHeight, (LPVOID)pBmpBuf, (BITMAPINFO*)&head, DIB_RGB_COLORS);
    45     m_pMemDC->SetBkMode(TRANSPARENT);
    46     m_pMemDC->SetTextColor(RGB(255, 0, 0));//RGB标记色
    47     OSDSetTextColor(0, 0, 0);//OSD显示色
    48     
    49     //设置字体
    50     CFont font;
    51     font.CreateFont(20,15,0,0,10,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_DEFAULT_PRECIS,OUT_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_SWISS,"宋体");
    52     SelectObject(hMemDC, font); 
    53     ///////////////////////////
    54     SYSTEMTIME st;
    55     CString strDate,strTime;
    56     GetLocalTime(&st);
    57     strDate.Format("时间 %04d-%02d-%02d ",st.wYear,st.wMonth,st.wDay);
    58     strTime.Format("%02d:%02d:%02d",st.wHour,st.wMinute,st.wSecond);
    59     // 添加文字到指定位置
    60     m_pMemDC->TextOut(20, 40, strDate+strTime);
    61     
    62     pTemp = new unsigned char[lineByte*bmpHeight];
    63     GetDIBits(m_pMemDC->GetSafeHdc(), (HBITMAP)m_pMemBmp->m_hObject, 0, bmpHeight, (LPVOID)pTemp, (BITMAPINFO*)&head, DIB_RGB_COLORS);
    64     fclose(fp);
    65 
    66     delete m_pMemBmp;
    67     delete m_pMemDC;
    68     delete[] pBmpBuf;
    69 
    70 
    71     //MessageBox("VOER!");
    72 }
    73 
    74 调用部分
    75 //////////////////////////////////////////////////////////////////////////////////////
    76     setOSD();
    77     for (int i=0;i<80;i++)
    78     {
    79         for (int k=0;k<640;k++)
    80         {
    81             //dc.SetPixel(k,79-i,RGB(pTemp[(i*640+k)*3+2],pTemp[(i*640+k)*3+1],pTemp[(i*640+k)*3+0]));
    82             if (pTemp[(i*640+k)*3+2]!=0)//需要叠加  通过标记色过滤
    83             {
    84                 pFrameYUV->data[0][(79-i)*720+k]=OsdY;
    85                 if ((79-i)%2==0)
    86                 {
    87                     pFrameYUV->data[1][(79-i)*720/4+k/2]=OsdU;
    88                     pFrameYUV->data[2][(79-i)*720/4+k/2]=OsdV;
    89                 }            
    90             }
    91         }
    92     }
    93 /////////////////////////////////////////////////////////////////////////////////////////
    94 
    95     //拷贝数据yuv420,也可预先改变指针
    96     memcpy(image->pixels[0],    pFrameYUV->data[0],720*576);
    97     memcpy(image->pixels[2],    pFrameYUV->data[1],720*576/4);
    98     memcpy(image->pixels[1],    pFrameYUV->data[2],720*576/4);

    http://blog.csdn.net/mao0514/article/details/41979245

  • 相关阅读:
    LightOJ 1239
    LightOJ 1166 Old Sorting 置换群 或 贪心 水题
    vijos 1243 生产产品 DP + 单调队列优化
    POJ 3348 Cows 凸包 求面积
    POJ 1389 Area of Simple Polygons 扫描线+线段树面积并
    POJ 1151 Atlantis 扫描线+线段树
    POJ 1228 Grandpa's Estate 凸包 唯一性
    bzoj 3622 DP + 容斥
    Uva5211/POJ1873 The Fortified Forest 凸包
    POJ 2007 Scrambled Polygon 极角序 水
  • 原文地址:https://www.cnblogs.com/eustoma/p/6662061.html
Copyright © 2020-2023  润新知