tgawnd.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. // TGAWnd.cpp : implementation file
  5. //
  6. #include "stdafx.h"
  7. #include "TGAWnd.h"
  8. #include "TGAInfo.h"
  9. /////////////////////////////////////////////////////////////////////////////
  10. // TGAWnd
  11. TGAWnd::TGAWnd()
  12. {
  13. bThisIsInitialized = false;
  14. m_bTGAChanged = false;
  15. m_pImage = NULL;
  16. m_pBmi = (BITMAPINFO*)malloc( sizeof( BITMAPINFOHEADER ) );
  17. memset( m_pBmi, 0, sizeof( BITMAPINFOHEADER ) );
  18. m_pBmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  19. m_pBmi->bmiHeader.biPlanes = 1;
  20. m_pBmi->bmiHeader.biBitCount = 32;
  21. m_pBmi->bmiHeader.biCompression = 0 ;
  22. m_pBmi->bmiHeader.biXPelsPerMeter = 0;
  23. m_pBmi->bmiHeader.biYPelsPerMeter = 0;
  24. m_pBmi->bmiHeader.biClrUsed = 0;
  25. m_pBmi->bmiHeader.biClrImportant = 0;
  26. m_pMemDC = NULL;
  27. m_hBitmap = NULL;
  28. m_hSplashBitMap = NULL;
  29. bThisIsInitialized = true;/*it may be premature to flag it as initialized here*/
  30. }
  31. TGAWnd::~TGAWnd()
  32. {
  33. bThisIsInitialized = false;
  34. free( m_pBmi );
  35. if ( m_hBitmap )
  36. DeleteObject( m_hBitmap );
  37. if ( m_pImage )
  38. {
  39. free( m_pImage );
  40. m_pImage = NULL;
  41. }
  42. if ( m_pMemDC )
  43. delete m_pMemDC;
  44. if ( m_hSplashBitMap )
  45. DeleteObject( m_hSplashBitMap );
  46. }
  47. void TGAWnd::SetTGAFileName( const CString& str )
  48. {
  49. m_FileName = str;
  50. m_bTGAChanged = true;
  51. if ( m_pImage )
  52. {
  53. free( m_pImage );
  54. m_pImage = NULL;
  55. }
  56. CFile File;
  57. if ( !File.Open( str, CFile::modeRead | CFile::shareDenyNone ) )
  58. {
  59. m_pBmi->bmiHeader.biHeight = 0;
  60. m_pBmi->bmiHeader.biWidth = 0;
  61. return;
  62. }
  63. if ( m_hBitmap )
  64. {
  65. DeleteObject( m_hBitmap );
  66. m_hBitmap = NULL;
  67. }
  68. TGAFileHeader header;
  69. File.Read( &header, sizeof( TGAFileHeader ) );
  70. if ( header.image_type == UNC_TRUE )
  71. {
  72. m_pImage = (char *)malloc( header.width * header.height * 4 );
  73. File.Read( m_pImage, header.width * header.height * 4);
  74. m_pBmi->bmiHeader.biHeight = -header.height;
  75. m_pBmi->bmiHeader.biWidth = header.width;
  76. }
  77. }
  78. void TGAWnd::SetTGAFileData( BYTE* data, int size )
  79. {
  80. m_FileName = "";
  81. m_bTGAChanged = true;
  82. TGAFileHeader* header;
  83. header = (TGAFileHeader*)data;
  84. if ( m_pImage && (m_pBmi->bmiHeader.biWidth != - header->width || m_pBmi->bmiHeader.biHeight != header->height) )
  85. {
  86. free( m_pImage );
  87. m_pImage = NULL;
  88. }
  89. if ( !m_pImage )
  90. m_pImage = (char *) malloc( header->width * header->height * 4 );
  91. if ( header->image_type == UNC_TRUE )
  92. {
  93. memcpy( m_pImage, data + sizeof( TGAFileHeader ), header->width * header->height * 4 );
  94. m_pBmi->bmiHeader.biHeight = -header->height;
  95. m_pBmi->bmiHeader.biWidth = header->width;
  96. }
  97. }
  98. BEGIN_MESSAGE_MAP(TGAWnd, CStatic)
  99. //{{AFX_MSG_MAP(TGAWnd)
  100. ON_WM_PAINT()
  101. //}}AFX_MSG_MAP
  102. END_MESSAGE_MAP()
  103. /////////////////////////////////////////////////////////////////////////////
  104. // TGAWnd message handlers
  105. void TGAWnd::OnPaint()
  106. {
  107. if (!bThisIsInitialized) { return; }
  108. CPaintDC dc(this); // device context for painting
  109. if ( m_bTGAChanged )
  110. {
  111. // Fill in the BITMAPINFOHEADER
  112. if ( !m_pMemDC )
  113. {
  114. m_pMemDC = new CDC;
  115. m_pMemDC->CreateCompatibleDC(&dc);
  116. }
  117. if ( !m_hBitmap )
  118. m_hBitmap = CreateDIBSection( dc.m_hDC, m_pBmi, DIB_RGB_COLORS, (void **)&m_pBits, NULL, 0 );
  119. memcpy( m_pBits, m_pImage, m_pBmi->bmiHeader.biWidth * -m_pBmi->bmiHeader.biHeight * 4 );
  120. m_bTGAChanged = false;
  121. }
  122. if ( m_pMemDC )
  123. {
  124. HGDIOBJ hOldObj = m_pMemDC->SelectObject( m_hBitmap );
  125. CRect rect;
  126. GetWindowRect( rect );
  127. dc.SetStretchBltMode( STRETCH_DELETESCANS );
  128. dc.StretchBlt( 0, 0, rect.Width(), rect.Height(), m_pMemDC, 0, 0, m_pBmi->bmiHeader.biWidth,
  129. -m_pBmi->bmiHeader.biHeight, SRCCOPY );
  130. m_pMemDC->SelectObject( hOldObj );
  131. }
  132. else
  133. {
  134. if (!m_hSplashBitMap) {
  135. m_hSplashBitMap = (HBITMAP)LoadImage(NULL, "tacsplash.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  136. }
  137. if (m_hSplashBitMap) {
  138. BITMAP bm_struct;
  139. {
  140. CBitmap *pSplashBitmap = CBitmap::FromHandle(m_hSplashBitMap);
  141. pSplashBitmap->GetBitmap(&bm_struct);
  142. }
  143. CDC l_MemDC;
  144. l_MemDC.CreateCompatibleDC(&dc);
  145. HGDIOBJ hOldObj = l_MemDC.SelectObject( m_hSplashBitMap );
  146. CRect rect;
  147. GetWindowRect( rect );
  148. dc.SetStretchBltMode( STRETCH_DELETESCANS );
  149. dc.StretchBlt( 0, 0, rect.Width(), rect.Height(), &l_MemDC, 0, 0, bm_struct.bmWidth,
  150. bm_struct.bmHeight, SRCCOPY );
  151. l_MemDC.SelectObject( hOldObj );
  152. }
  153. }
  154. // Do not call CStatic::OnPaint() for painting messages
  155. }