bitmap.cpp
Upload User: aya088
Upload Date: 2021-10-23
Package Size: 42k
Code Size: 9k
Category:

DirextX

Development Platform:

Visual C++

  1. //------------------------------------------------------------------------------
  2. // File: Bitmap.cpp
  3. //
  4. // Desc: DirectShow sample code - Bitmap manipulation routines for 
  5. //       VMR alpha-blended bitmap
  6. //
  7. // Copyright (c) Microsoft Corporation.  All rights reserved.
  8. //------------------------------------------------------------------------------
  9. #include <dshow.h>
  10. #include <commctrl.h>
  11. #include <commdlg.h>
  12. #include <stdio.h>
  13. #include <tchar.h>
  14. #include <atlbase.h>
  15. #include "text.h"
  16. #include "bitmap.h"
  17. //
  18. // Constants
  19. //
  20. const float X_EDGE_BUFFER=0.05f; // Pixel buffer between bitmap and window edge
  21.                                  // (represented in composition space [0 - 1.0f])
  22. const float Y_EDGE_BUFFER=0.05f;
  23. const int UPDATE_TIMER   = 2000;
  24. const int UPDATE_TIMEOUT = 2000; // 2 seconds between ticker movements
  25. //
  26. // Global data
  27. //
  28. IVMRMixerBitmap9 *pBMP = NULL;
  29. TCHAR g_szAppText[DYNAMIC_TEXT_SIZE]={0};
  30. int gnTimer=0;
  31. float g_fBitmapCompWidth=0;  // Width of bitmap in composition space units
  32. int g_nImageWidth=0;         // Width of text bitmap
  33. // Text font information
  34. HFONT g_hFont=0;
  35. LONG g_lFontPointSize   = DEFAULT_FONT_SIZE;
  36. COLORREF g_rgbColors    = DEFAULT_FONT_COLOR;
  37. TCHAR g_szFontName[100] = {DEFAULT_FONT_NAME};
  38. TCHAR g_szFontStyle[32] = {DEFAULT_FONT_STYLE};
  39. // Destination rectangle used for alpha-blended text
  40. VMR9NormalizedRect  g_rDest={0};
  41. HRESULT BlendText(HWND hwndApp, TCHAR *szNewText)
  42. {
  43.     LONG cx, cy;
  44.     HRESULT hr;
  45.     // Read the default video size
  46.     hr = pWC->GetNativeVideoSize(&cx, &cy, NULL, NULL);
  47.     if (FAILED(hr))
  48.     {
  49.         Msg(TEXT("GetNativeVideoSize FAILED!  hr=0x%xrn"), hr);
  50.         return hr;
  51.     }
  52.     // Create a device context compatible with the current window
  53.     HDC hdc = GetDC(hwndApp);
  54.     HDC hdcBmp = CreateCompatibleDC(hdc);
  55.     // Write with a known font by selecting it into our HDC
  56.     HFONT hOldFont = (HFONT) SelectObject(hdcBmp, g_hFont);
  57.     // Determine the length of the string, then determine the
  58.     // dimensions (in pixels) of the character string using the
  59.     // currently selected font.  These dimensions are used to create
  60.     // a bitmap below.
  61.     int nLength, nTextBmpWidth, nTextBmpHeight;
  62.     SIZE sz={0};
  63.     nLength = (int) _tcslen(szNewText);
  64.     GetTextExtentPoint32(hdcBmp, szNewText, nLength, &sz);
  65.     nTextBmpHeight = sz.cy;
  66.     nTextBmpWidth  = sz.cx;
  67.     // Create a new bitmap that is compatible with the current window
  68.     HBITMAP hbm = CreateCompatibleBitmap(hdc, nTextBmpWidth, nTextBmpHeight);
  69.     ReleaseDC(hwndApp, hdc);
  70.     // Select our bitmap into the device context and save the old one
  71.     BITMAP bm;
  72.     HBITMAP hbmOld;
  73.     GetObject(hbm, sizeof(bm), &bm);
  74.     hbmOld = (HBITMAP)SelectObject(hdcBmp, hbm);
  75.     // Set initial bitmap settings
  76.     RECT rcText;
  77.     SetRect(&rcText, 0, 0, nTextBmpWidth, nTextBmpHeight);
  78.     SetBkColor(hdcBmp, RGB(255, 255, 255)); // Pure white background
  79.     SetTextColor(hdcBmp, g_rgbColors);      // Write text with requested color
  80.     // Draw the requested text string onto the bitmap
  81.     TextOut(hdcBmp, 0, 0, szNewText, nLength);
  82.     // Configure the VMR's bitmap structure
  83.     VMR9AlphaBitmap bmpInfo;
  84.     ZeroMemory(&bmpInfo, sizeof(bmpInfo) );
  85.     bmpInfo.dwFlags = VMRBITMAP_HDC;
  86.     bmpInfo.hdc = hdcBmp;  // DC which has selected our bitmap
  87.     // Remember the width of this new bitmap
  88.     g_nImageWidth = bm.bmWidth;
  89.     // Save the ratio of the bitmap's width to the width of the video file.
  90.     // This value is used to reposition the bitmap in composition space.
  91.     g_fBitmapCompWidth = (float)g_nImageWidth / (float)cx;
  92.     // Display the bitmap in the bottom right corner.
  93.     // rSrc specifies the source rectangle in the GDI device context 
  94.     // rDest specifies the destination rectangle in composition space (0.0f to 1.0f)
  95.     bmpInfo.rDest.left  = 0.0f + X_EDGE_BUFFER;
  96.     bmpInfo.rDest.right = 1.0f - X_EDGE_BUFFER;
  97.     bmpInfo.rDest.top = (float)(cy - bm.bmHeight) / (float)cy - Y_EDGE_BUFFER;
  98.     bmpInfo.rDest.bottom = 1.0f - Y_EDGE_BUFFER;
  99.     bmpInfo.rSrc = rcText;
  100.     // Transparency value 1.0 is opaque, 0.0 is transparent.
  101.     bmpInfo.fAlpha = TRANSPARENCY_VALUE;
  102.     // Set the COLORREF so that the bitmap outline will be transparent
  103.     SetColorRef(bmpInfo);
  104.     // Give the bitmap to the VMR for display
  105.     hr = pBMP->SetAlphaBitmap(&bmpInfo);
  106.     if (FAILED(hr))
  107.         Msg(TEXT("SetAlphaBitmap FAILED!  hr=0x%xrnrn%s"), hr,
  108.             STR_VMR_DISPLAY_WARNING);
  109.     // Select the initial objects back into our device context
  110.     DeleteObject(SelectObject(hdcBmp, hbmOld));
  111.     SelectObject(hdc, hOldFont);
  112.     // Clean up resources
  113.     DeleteObject(hbm);
  114.     DeleteDC(hdcBmp);
  115.     return hr;
  116. }
  117. void UpdateText(void)
  118. {
  119.     static int nCurrentTextLine=0;
  120.     const int MAX_TEXT_LINES=5;
  121.     static TCHAR szText[MAX_TEXT_LINES][80]  = {
  122.         TEXT("Introducing the Video Mixing Renderer 9,"),
  123.         TEXT("only available with Microsoft DirectX 9!"),
  124.         TEXT("The VMR9 supports multiple video streams"),
  125.         TEXT("and a static bitmap, all of which"),
  126.         TEXT("can be mixed with alpha blending.")
  127.     };
  128.     // Update the text string
  129.     BlendText(ghApp, szText[nCurrentTextLine]);
  130.     // Advance to the next string in the list
  131.     nCurrentTextLine++;
  132.     // Wrap around to beginning of list
  133.     if (nCurrentTextLine >= MAX_TEXT_LINES)
  134.         nCurrentTextLine=0;
  135. }
  136. void SetColorRef(VMR9AlphaBitmap& bmpInfo)
  137. {
  138.     // Set the COLORREF so that the bitmap outline will be transparent
  139.     bmpInfo.clrSrcKey = RGB(255, 255, 255);  // Pure white
  140.     bmpInfo.dwFlags |= VMRBITMAP_SRCCOLORKEY;
  141. }
  142. HFONT UserSelectFont( void ) 
  143. {
  144.     // Allow the user to specify the text font to use with
  145.     // dynamic text.  Display the Windows ChooseFont() dialog.
  146.     return (SetTextFont(TRUE));
  147. }
  148. HFONT SetTextFont(BOOL bShowDialog) 
  149.     CHOOSEFONT cf={0}; 
  150.     LOGFONT lf={0}; 
  151.     HFONT hfont; 
  152.     HDC hdc;
  153.     LONG lHeight;
  154.     // Convert requested font point size to logical units
  155.     hdc = GetDC( ghApp );
  156.     lHeight = -MulDiv( g_lFontPointSize, GetDeviceCaps(hdc, LOGPIXELSY), 72 );
  157.     ReleaseDC( ghApp, hdc );
  158.     // Initialize members of the LOGFONT structure. 
  159.     lstrcpyn(lf.lfFaceName, g_szFontName, 32);
  160.     lf.lfHeight = lHeight;      // Logical units
  161.     // Prevent font smoothing, which could distort the text and leave
  162.     // white pixels on the edges.  Disabling antialiasing leads to 
  163.     // smoother text in this context.
  164.     lf.lfQuality = NONANTIALIASED_QUALITY;
  165.     // Initialize members of the CHOOSEFONT structure. 
  166.     cf.lStructSize = sizeof(CHOOSEFONT); 
  167.     cf.hwndOwner   = ghApp; 
  168.     cf.hDC         = (HDC)NULL; 
  169.     cf.lpLogFont   = &lf; 
  170.     cf.iPointSize  = g_lFontPointSize * 10; 
  171.     cf.rgbColors   = g_rgbColors; 
  172.     cf.lCustData   = 0L; 
  173.     cf.lpfnHook    = (LPCFHOOKPROC)NULL; 
  174.     cf.hInstance   = (HINSTANCE) NULL; 
  175.     cf.lpszStyle   = g_szFontStyle; 
  176.     cf.nFontType   = SCREEN_FONTTYPE; 
  177.     cf.nSizeMin    = 0; 
  178.     cf.lpTemplateName = NULL; 
  179.     cf.Flags = CF_SCREENFONTS | CF_SCALABLEONLY | CF_INITTOLOGFONTSTRUCT | 
  180.                CF_EFFECTS     | CF_USESTYLE     | CF_LIMITSIZE; 
  181.     // Limit font size to prevent bitmap from becoming too wide
  182.     cf.nSizeMax = MAX_FONT_SIZE; 
  183.  
  184.     // If we previously changed a pure white font to 'almost white'
  185.     // to support writing white text over a white colorkey, then
  186.     // configure the font dialog for pure white text.
  187.     if (cf.rgbColors == ALMOST_WHITE)
  188.         cf.rgbColors = PURE_WHITE;
  189.     // Display the CHOOSEFONT common-dialog box.  When it closes,
  190.     // the CHOOSEFONT structure members will be updated.
  191.     if (bShowDialog)
  192.         ChooseFont(&cf); 
  193.     // Save the user's selections for configuring the dialog box next time.
  194.     // The style is automatically saved in g_szFontStyle (cf.lpszStyle)
  195.     lstrcpyn(g_szFontName, lf.lfFaceName, NUMELMS(g_szFontName));
  196.     g_lFontPointSize = cf.iPointSize / 10;  // Specified in 1/10 point units
  197.     g_rgbColors = cf.rgbColors;
  198.     // Because we use a white colorkey to introduce transparency behind
  199.     // our text, drawing white text will cause it to be transparent.
  200.     // Therefore, filter out pure white (RGB(255,255,255)).
  201.     if (g_rgbColors == PURE_WHITE)
  202.         g_rgbColors = ALMOST_WHITE;
  203.     // Create a logical font based on the user's selection and 
  204.     // return a handle identifying that font.  
  205.     hfont = CreateFontIndirect(cf.lpLogFont); 
  206.     return (hfont); 
  207. void StartTimer(void)
  208. {
  209.     gnTimer = (int) SetTimer(NULL, UPDATE_TIMER, UPDATE_TIMEOUT, TimerProc);
  210. }
  211. void StopTimer(void)
  212. {
  213.     if (gnTimer)
  214.     {
  215.         KillTimer(NULL, gnTimer);
  216.         gnTimer = 0;
  217.     }
  218. }
  219. VOID CALLBACK TimerProc(
  220.   HWND hwnd,         // handle to window
  221.   UINT uMsg,         // WM_TIMER message
  222.   UINT_PTR idEvent,  // timer identifier
  223.   DWORD dwTime       // current system time
  224. )
  225. {
  226.     // Draw a new line of text over the video in the main window
  227.     UpdateText();
  228. }