Dib.cpp
上传用户:yeung_1189
上传日期:2010-02-10
资源大小:3536k
文件大小:4k
源码类别:

图形/文字识别

开发平台:

Visual C++

  1. // Dib.cpp: implementation of the CDib class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "StdAfx.h"
  5. #include "Dib.h"
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static char THIS_FILE[]=__FILE__;
  9. #define new DEBUG_NEW
  10. #endif
  11. //////////////////////////////////////////////////////////////////////
  12. // Construction/Destruction
  13. //////////////////////////////////////////////////////////////////////
  14. CDib::CDib()
  15. {
  16. m_hDrawDib=NULL;
  17. m_pDib=NULL;
  18. }
  19. CDib::~CDib()
  20. {
  21. Close();
  22. }
  23. void CDib::Draw(CDC *pDC,int nWidth, int nHeight)
  24. {
  25. if(m_pDib!=NULL)
  26. {
  27. ASSERT(IsValid());
  28. DrawDibRealize(m_hDrawDib,pDC->GetSafeHdc(),TRUE);
  29. DrawDibDraw(m_hDrawDib,pDC->GetSafeHdc(),
  30. 0,  //desktop left
  31. 0,  //desktop top
  32. nWidth,
  33. nHeight,
  34. (BITMAPINFOHEADER *)m_pDib,
  35. (LPVOID) GetBits(),
  36. 0,  //source left
  37. 0,  //source top
  38. ((BITMAPINFOHEADER *)m_pDib)->biWidth,
  39. ((BITMAPINFOHEADER *)m_pDib)->biHeight,
  40. DDF_BACKGROUNDPAL);
  41. }
  42. }
  43. CSize CDib::GetSize()
  44. {
  45. return CSize(((BITMAPINFOHEADER *)m_pDib)->biWidth,
  46.  ((BITMAPINFOHEADER *)m_pDib)->biHeight);
  47. }
  48. LONG CDib::GetWidth()
  49. {
  50. return ((BITMAPINFOHEADER *)m_pDib)->biWidth;
  51. }
  52. LONG CDib::GetHeight()
  53. {
  54. return ((BITMAPINFOHEADER *)m_pDib)->biHeight;
  55. }
  56. void CDib::Close()
  57. {
  58. if(m_hDrawDib!=NULL)
  59. {
  60. DrawDibClose(m_hDrawDib);
  61. m_hDrawDib=NULL;
  62. }
  63. if(m_pDib!=NULL)
  64. {
  65. delete m_pDib;
  66. m_pDib=NULL;
  67. }
  68. }
  69. BOOL CDib::Open(const char * pzFileName)
  70. {
  71. // BITMAPFILEHEADER bmpFileHeader;
  72. CFile file;
  73. int nBmpFileHeaderSize;
  74. Close();
  75. //drawdibopen initialize the diradib library and 
  76. //returns a handle for all drawdib operations
  77. if(!(m_hDrawDib=DrawDibOpen()))
  78. goto exit;
  79.   //open and read the DIB file header
  80. nBmpFileHeaderSize=sizeof(BITMAPFILEHEADER);
  81. if(!file.Open(pzFileName,CFile::modeRead | CFile::typeBinary))
  82. goto exit;
  83. if(file.Read((void *)&bmpFileHeader,nBmpFileHeaderSize)!=(UINT)nBmpFileHeaderSize)
  84. goto failure;
  85. //validate the DIB file header by checking the first
  86. //two characters for the signature "BM"
  87. if(bmpFileHeader.bfType!=*((WORD *)"BM"))
  88. goto failure;
  89. //allocate a big chuck of global memory to store the DIB
  90. m_pDib=(BYTE *)new char [bmpFileHeader.bfSize-nBmpFileHeaderSize];
  91. //allocate memory fail
  92. if(!m_pDib)
  93. goto failure;
  94. //read the dib into the buffer at a time using ReadHuge
  95. file.ReadHuge(m_pDib,bmpFileHeader.bfSize-nBmpFileHeaderSize);
  96. if(((BITMAPINFOHEADER *)m_pDib)->biSizeImage==0)
  97. {
  98. //the application that create this bitmap didn't fill
  99. //in the biSizeImage field. Let's fill it
  100. //in even though the DrawDib * functions don't need it.
  101. BITMAPINFOHEADER *pDib=(BITMAPINFOHEADER *)m_pDib;
  102. //scan lines must be DWord aligned, hence the strange bit stuff
  103. pDib->biSizeImage=((((pDib->biWidth*pDib->biBitCount)+31)&~31)>>3)*pDib->biHeight;
  104. }
  105. m_pDibBits=GetBits();
  106. file.Close();
  107. return TRUE;
  108. failure:
  109. file.Close();
  110. exit:
  111. Close();
  112. return FALSE;
  113. }
  114. BOOL CDib::Save(const char * pzFileName)
  115. {
  116. // BITMAPFILEHEADER bmpFileHeader;
  117. CFile file;
  118. int nBmpFileHeaderSize;
  119.   //open and read the DIB file header
  120. nBmpFileHeaderSize=sizeof(BITMAPFILEHEADER);
  121. if(!file.Open(pzFileName,CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
  122. goto exit;
  123. file.Write(&bmpFileHeader,nBmpFileHeaderSize); 
  124. //allocate memory fail
  125. if(!m_pDib)
  126. goto failure;
  127. //read the dib into the buffer at a time using ReadHuge
  128. file.WriteHuge(m_pDib,bmpFileHeader.bfSize-nBmpFileHeaderSize);
  129. file.Close();
  130. return TRUE;
  131. failure:
  132. file.Close();
  133. exit:
  134. return FALSE;
  135. }
  136. BYTE * CDib::GetBits()
  137. {
  138. //the size of the color map is determined by the number
  139. //of RGBQUAD structures presend.
  140. //it also depends on the bit_depth of the Dib
  141. DWORD dwNumColors,dwColorTableSize;
  142. BITMAPINFOHEADER *lpDib=(BITMAPINFOHEADER *)m_pDib;
  143. WORD wBitCount=lpDib->biBitCount;
  144. if(lpDib->biSize>=36)
  145. dwNumColors=lpDib->biClrUsed;
  146. else
  147. dwNumColors=0;
  148. if(dwNumColors==0)
  149. {
  150. if(wBitCount!=24)
  151. dwNumColors=1L<<wBitCount;
  152. else 
  153. dwNumColors=0;
  154. }
  155. dwColorTableSize=dwNumColors*sizeof(RGBQUAD);
  156. return m_pDib+lpDib->biSize+dwColorTableSize;
  157. }
  158. int CDib::GetBiBitCount()
  159. {
  160. if(m_pDib!=NULL)
  161. return ((BITMAPINFOHEADER *)m_pDib)->biBitCount; 
  162. return 0;
  163. }