CSBitmap.cpp
上传用户:fjzzwyy
上传日期:2007-01-14
资源大小:244k
文件大小:8k
源码类别:

绘图程序

开发平台:

Visual C++

  1. #include "stdafx.h" 
  2. //#include "CMyBit.h" 
  3. #include "CSBitmap.h" 
  4. #include "math.h" 
  5.   
  6. #ifdef _DEBUG 
  7. #undef THIS_FILE 
  8. static char THIS_FILE[]=__FILE__; 
  9. #define new DEBUG_NEW 
  10. #endif 
  11.   
  12. ////////////////////////////////////////////////////////////////////// 
  13. // Construction/Destruction 
  14. ////////////////////////////////////////////////////////////////////// 
  15.   
  16. CSBitmap::CSBitmap() 
  17.         m_bHavePal=false; 
  18.         m_hDIB=NULL; 
  19.   
  20. CSBitmap::~CSBitmap() 
  21.   
  22. BOOL CSBitmap::SaveBMP(CDC* pDC,int x,int y,int cx,int cy,LPCTSTR sBMPFile)
  23. {
  24.     BITMAPINFO* bmpInfo = (BITMAPINFO *) new BYTE[sizeof(BITMAPINFOHEADER)];     
  25. int size = m_Width*m_Height*3;    
  26.     BITMAPFILEHEADER hdr;
  27.     CFile file(sBMPFile,CFile::modeWrite|CFile::modeCreate);
  28.     hdr.bfType = ((WORD)'M'<<8) + 'B';
  29. hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +size+sizeof(BITMAPINFO));
  30.     hdr.bfReserved1 = 0; 
  31.     hdr.bfReserved2 = 0; 
  32.     // Compute the offset to the array of color indices. 
  33.     hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFO);                    
  34.     //写入文件头
  35. file.Write((LPVOID) &hdr, sizeof(BITMAPFILEHEADER));
  36. //写入信息头
  37.     file.Write((LPVOID) bmpInfo, sizeof(BITMAPINFOHEADER)); 
  38.     // Copy the array of color indices into the .BMP file. 
  39. //写入数据
  40. int nTotal = size; 
  41.     file.WriteHuge((LPSTR)m_hDIB,nTotal);
  42. //关闭文件
  43. file.Close();
  44. delete bmpInfo;
  45. return true;
  46. }
  47.   
  48.   
  49. BOOL CSBitmap::LoadBMP(LPCTSTR sBMPFile) 
  50.         //, HGLOBAL *phDIB, CPalette *pPal 
  51.         CFile file; 
  52.         if( !file.Open( sBMPFile, CFile::modeRead)) 
  53.                 return FALSE; 
  54.   
  55.         BITMAPFILEHEADER bmfHeader; 
  56.         long nFileLen; 
  57.   
  58.         nFileLen = file.GetLength();   
  59.   
  60.         // Read file header 
  61.         if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))   
  62.                 return FALSE; 
  63.   
  64.         // File type should be 'BM' 
  65.         if (bmfHeader.bfType != ((WORD) ('M' << 8) | 'B')) 
  66.                 return FALSE; 
  67.   
  68.         HGLOBAL hDIB = ::GlobalAlloc(GMEM_FIXED, nFileLen); 
  69.         if (hDIB == 0) 
  70.                 return FALSE; 
  71.   
  72.         m_DataSize = nFileLen - sizeof(BITMAPFILEHEADER); 
  73.         // Read the remainder of the bitmap file. 
  74.         if (file.ReadHuge((LPSTR)hDIB, nFileLen - sizeof(BITMAPFILEHEADER)) != 
  75.                 nFileLen - sizeof(BITMAPFILEHEADER) ) 
  76.         { 
  77.                 ::GlobalFree(hDIB); 
  78.                 return FALSE; 
  79.         } 
  80.   
  81.         BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ; 
  82. m_Height=bmInfo.bmiHeader.biHeight;
  83. m_Width=bmInfo.bmiHeader.biWidth;
  84.   
  85.         int nColors = bmInfo.bmiHeader.biClrUsed ? bmInfo.bmiHeader.biClrUsed : 
  86.                                                 1 << bmInfo.bmiHeader.biBitCount; 
  87.   
  88.         // Create the palette 
  89.         if(m_bHavePal==true) 
  90.         { 
  91.                 m_Pal.DeleteObject(); 
  92.         }; 
  93. /*  
  94.         if( nColors <= 256 ) 
  95.         { 
  96.                 m_bHavePal=true; 
  97.                 UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY)*nColors); 
  98.                 LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize]; 
  99.   
  100.                 pLP->palVersion = 0x300; 
  101.                 pLP->palNumEntries = nColors; 
  102.   
  103.                 for( int i=0; i < nColors; i++) 
  104.                 { 
  105.                         pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed; 
  106.                         pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen; 
  107.                         pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
  108.   
  109.                         pLP->palPalEntry[i].peFlags = 0; 
  110.                 } 
  111.   
  112.                 m_Pal.CreatePalette( pLP ); 
  113.   
  114.                 delete[] pLP; 
  115.         } */
  116.   
  117.         m_hDIB = hDIB; 
  118.         return TRUE; 
  119.   
  120.   
  121.   
  122.   
  123.   
  124. void CSBitmap::DrawImg(CDC *pDC, int x, int y, int cx, int cy) 
  125.         CPalette* pPal; 
  126.         // TODO: add draw code for native data here 
  127.         BITMAPINFO& bi=*(LPBITMAPINFO)m_hDIB; 
  128.         LPVOID  lpBits; 
  129.         //double Magnify=0; 
  130.         //Magnify=(double(cx)/bi.bmiHeader.biWidth-double(cy)/bi.bmiHeader.biHeight>0)? 
  131.         //      double(cy)/bi.bmiHeader.biHeight:double(cx)/bi.bmiHeader.biWidth 
  132.   
  133.   
  134.         //lpBits=(LPVOID)((BYTE*)m_hglobal+sizeof(BITMAPINFO)+256*4); 
  135. //+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256); 
  136.         if(m_bHavePal==true) 
  137.         { 
  138.                 pPal=pDC->SelectPalette(&m_Pal,FALSE); 
  139.                 pDC->RealizePalette(); 
  140.   
  141.                 if(bi.bmiHeader.biClrUsed!=0) 
  142.                         lpBits=(LPVOID)((BYTE*)m_hDIB+sizeof(BITMAPINFOHEADER)+bi.bmiHeader. biClrUsed*4);//+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256); 
  143.                 else 
  144.                         lpBits=(LPVOID)((BYTE*)m_hDIB+sizeof(BITMAPINFOHEADER)+(int)pow(2,bi.bmiHeader.biBitCount)*4); 
  145.         } 
  146.         else 
  147.         { 
  148.                 lpBits=(LPVOID)((BYTE*)m_hDIB+sizeof(BITMAPINFOHEADER)); 
  149.         }; 
  150.         SetStretchBltMode(pDC->GetSafeHdc(),HALFTONE); 
  151.         StretchDIBits(pDC->GetSafeHdc(), 
  152.                 x,y,cx,cy, 
  153.         //      x,y,int(bi.bmiHeader.biWidth*Magnify),int(bi.bmiHeader.biHeight*agnify), 
  154.                 0,0,bi.bmiHeader.biWidth,bi.bmiHeader.biHeight, 
  155.                 lpBits,&bi,DIB_RGB_COLORS,SRCCOPY); 
  156.   
  157.         if(m_bHavePal==true) 
  158.                 pDC->SelectPalette(pPal,FALSE); 
  159.   
  160. void CSBitmap::ReleaseMem() 
  161.         if(m_hDIB!=NULL) 
  162.                 ::GlobalFree(m_hDIB); 
  163.         m_hDIB=NULL; 
  164.         m_bHavePal=false; 
  165.         m_Pal.DeleteObject(); 
  166.   
  167.   
  168. // function     :       GetRotatedBitmap        - Create a new bitmap with rotatd image 
  169. // input        :       Returns         - Returns new bitmap with rotated image 
  170. //                              hDIB            - Device-independent bitmap to rtate 
  171. //                              radians         - Angle of rotation in radians 
  172. //                              clrBack         - Color of pixels in the resulti g bitmap that do 
  173. //                                                      not get covered by sourc  pixels 
  174.   
  175. //Add by CQX: 
  176. BOOL CSBitmap::LoadBMP( BYTE * bmpdata,long size) 
  177.         HGLOBAL hDIB = (HGLOBAL)bmpdata; 
  178.         if (hDIB == 0) 
  179.                 return FALSE; 
  180.   
  181.         // Read the remainder of the bitmap file. 
  182.   
  183.         BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ; 
  184.   
  185.         int nColors = bmInfo.bmiHeader.biClrUsed ? bmInfo.bmiHeader.biClrUsed : 
  186.                                                 1 << bmInfo.bmiHeader.biBitCount;
  187.   
  188.   
  189.         // Create the palette 
  190.         if(m_bHavePal==true) 
  191.         { 
  192.                 m_Pal.DeleteObject(); 
  193.         }; 
  194.   
  195.         if( nColors <= 256 ) 
  196.         { 
  197.                 m_bHavePal=true; 
  198.                 UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors); 
  199.                 LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize]; 
  200.   
  201.                 pLP->palVersion = 0x300; 
  202.                 pLP->palNumEntries = nColors; 
  203.   
  204.                 for( int i=0; i < nColors; i++) 
  205.                 { 
  206.                         pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed; 
  207.                         pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen; 
  208.                         pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;  
  209.                         pLP->palPalEntry[i].peFlags = 0; 
  210.                 } 
  211.             m_Pal.CreatePalette( pLP );
  212.                 delete[] pLP;
  213.         }
  214.         m_hDIB = hDIB;
  215.         return TRUE;
  216. }                              
  217.