Video DemoView.cpp
上传用户:smdfuse
上传日期:2015-11-06
资源大小:98k
文件大小:6k
源码类别:

图形图象

开发平台:

Visual C++

  1. // Video DemoView.cpp : implementation of the CVideoDemoView class
  2. //
  3. #include "stdafx.h"
  4. #include "Video Demo.h"
  5. #include "Video DemoDoc.h"
  6. #include "Video DemoView.h"
  7. #include "colortrans.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CVideoDemoView
  15. IMPLEMENT_DYNCREATE(CVideoDemoView, CView)
  16. BEGIN_MESSAGE_MAP(CVideoDemoView, CView)
  17. //{{AFX_MSG_MAP(CVideoDemoView)
  18. // NOTE - the ClassWizard will add and remove mapping macros here.
  19. //    DO NOT EDIT what you see in these blocks of generated code!
  20. //}}AFX_MSG_MAP
  21. // Standard printing commands
  22. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  23. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  24. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CVideoDemoView construction/destruction
  28. CVideoDemoView::CVideoDemoView()
  29. {
  30. // TODO: add construction code here
  31. this->data = NULL;
  32. this->outputAvailable = false;
  33. }
  34. CVideoDemoView::~CVideoDemoView()
  35. {
  36. if(this->data != NULL)
  37. delete[] data;  // 释放输出缓冲
  38. }
  39. BOOL CVideoDemoView::PreCreateWindow(CREATESTRUCT& cs)
  40. {
  41. // TODO: Modify the Window class or styles here by modifying
  42. //  the CREATESTRUCT cs
  43. return CView::PreCreateWindow(cs);
  44. }
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CVideoDemoView drawing
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CVideoDemoView printing
  49. BOOL CVideoDemoView::OnPreparePrinting(CPrintInfo* pInfo)
  50. {
  51. // default preparation
  52. return DoPreparePrinting(pInfo);
  53. }
  54. void CVideoDemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  55. {
  56. // TODO: add extra initialization before printing
  57. }
  58. void CVideoDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  59. {
  60. // TODO: add cleanup after printing
  61. }
  62. /////////////////////////////////////////////////////////////////////////////
  63. // CVideoDemoView diagnostics
  64. #ifdef _DEBUG
  65. void CVideoDemoView::AssertValid() const
  66. {
  67. CView::AssertValid();
  68. }
  69. void CVideoDemoView::Dump(CDumpContext& dc) const
  70. {
  71. CView::Dump(dc);
  72. }
  73. CVideoDemoDoc* CVideoDemoView::GetDocument() // non-debug version is inline
  74. {
  75. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CVideoDemoDoc)));
  76. return (CVideoDemoDoc*)m_pDocument;
  77. }
  78. #endif //_DEBUG
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CVideoDemoView message handlers
  81. /************************************************************************/
  82. /* 绘图函数                                                             */
  83. /************************************************************************/
  84. void CVideoDemoView::OnDraw(CDC* pDC)
  85. {
  86. if(outputAvailable)   // 允许输出图像数据
  87. {
  88. pDC->SetStretchBltMode(STRETCH_DELETESCANS);
  89. StretchDIBits(pDC->m_hDC,0,0,imgWidth,imgHeight,
  90. 0,0,imgWidth,imgHeight,
  91. data, &bitmapInfo, DIB_RGB_COLORS,SRCCOPY);
  92. }
  93. }
  94. /************************************************************************/
  95. /* OnActiveView                                                         */
  96. /* 当视图切换时改变当前数据                                             */
  97. /************************************************************************/
  98. void  CVideoDemoView::OnActivateView(BOOL bActivate,CView* pActivateView,CView* pDeactiveView)
  99. {
  100. if(bActivate == TRUE)
  101. {
  102. CVideoDemoDoc *doc = (CVideoDemoDoc*)GetDocument();
  103. theApp.lastImageData = theApp.curImageData;  // 设置上一次的数据位置
  104. theApp.curImageData = doc->id;   // 设置当前的doc
  105. if(doc->id != NULL)
  106. TRACE("Change View Index : %dn",doc->id->index);
  107. else
  108. TRACE("Change View Index NULL IDn");
  109. }
  110. }
  111. /************************************************************************/
  112. /* 允许输出数据                                                         */
  113. /************************************************************************/
  114. void CVideoDemoView::RefreshData()
  115. {
  116. // 自动获取图像数据源
  117. CVideoDemoDoc* pDoc = GetDocument();
  118. //ASSERT_VALID(pDoc);
  119. // 显示图像
  120. ImageData *id = pDoc->id;           // 图像数据
  121. if(data != NULL)
  122. delete[] data;
  123. data = new unsigned char[id->imgWidth * id->imgHeight * 3];
  124. ColorTrans ct;
  125. switch(id->format)
  126. {
  127. case IMAGE_FORMAT_GRAY8:
  128. // 8位灰度图像
  129. ct.gray2RGB2(id->data,data,id->imgWidth,id->imgHeight);
  130. this->imgWidth = id->imgWidth;
  131. this->imgHeight = id->imgHeight;
  132. memset(&bitmapInfo,0,sizeof(BITMAPINFO));
  133. bitmapInfo.bmiHeader.biHeight = (LONG)imgHeight;
  134. bitmapInfo.bmiHeader.biWidth = (LONG)imgWidth;
  135. bitmapInfo.bmiHeader.biSizeImage = (LONG)imgWidth * (LONG)imgHeight * 3;
  136. bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  137. bitmapInfo.bmiHeader.biBitCount = 24;
  138. bitmapInfo.bmiHeader.biCompression = 0;
  139. bitmapInfo.bmiHeader.biPlanes = 1;
  140. outputAvailable = true;        //输出允许
  141. break;
  142. case IMAGE_FORMAT_RGB24:
  143. memcpy(this->data,id->data,id->imgWidth * id->imgHeight * sizeof(unsigned char) * 3); 
  144. this->imgWidth = id->imgWidth;
  145. this->imgHeight = id->imgHeight;
  146. bitmapInfo.bmiHeader.biHeight = (LONG)imgHeight;
  147. bitmapInfo.bmiHeader.biWidth = (LONG)imgWidth;
  148. bitmapInfo.bmiHeader.biSizeImage = (LONG)imgWidth * (LONG)imgHeight * 3;
  149. bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  150. bitmapInfo.bmiHeader.biBitCount = 24;
  151. bitmapInfo.bmiHeader.biCompression = 0;
  152. bitmapInfo.bmiHeader.biPlanes = 1;
  153. this->outputAvailable = true;
  154. break;
  155. default:
  156. this->outputAvailable = false; // 输出禁止
  157. }
  158. this->Invalidate(false);
  159. }