Bmp.c
上传用户:s6549606
上传日期:2015-11-11
资源大小:12002k
文件大小:7k
源码类别:

图形图像处理

开发平台:

Visual C++

  1. //////////////////////////////////////////////////////////////
  2. //Name:bmp.c
  3. //Purpose: To open a bitmap file and display it on screen.
  4. //Author: phoenix, CS, TshingHua, Beijing, P.R.C
  5. //Email: bjlufengjun@www.163.net or lufengjun@hotmail.com
  6. //Date:April 3, 1998
  7. //header file
  8. #include "bmp.h"
  9. //macro definition
  10. #define WIDTHBYTES(i)    ((i+31)/32*4)
  11. //function declaration
  12. int PASCAL WinMain (HANDLE, HANDLE, LPSTR, int);
  13. LRESULT CALLBACK MainWndProc(HWND , UINT,WPARAM, LPARAM);
  14. BOOL LoadBmpFile (HWND hWnd,char *BmpFileName);
  15. //global variable declaration
  16. BITMAPFILEHEADER   bf;
  17. BITMAPINFOHEADER   bi;
  18. HPALETTE hPalette=NULL;
  19. HBITMAP  hBitmap=NULL;
  20. HGLOBAL  hImgData=NULL;
  21. HWND     hWnd;
  22. ///////////////////////////////////////////////////////////
  23. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  24.     LPSTR lpszCmdLine, int nCmdShow)
  25. {
  26. MSG       msg;
  27. WNDCLASS  wndclass;
  28. if ( ! hPrevInstance ){
  29. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  30. wndclass.lpfnWndProc = MainWndProc;
  31. wndclass.cbClsExtra = 0;
  32. wndclass.cbWndExtra = 0;
  33. wndclass.hInstance = hInstance;
  34. wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  35. wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  36. wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  37. wndclass.lpszMenuName =  "BMPMENU";
  38. wndclass.lpszClassName = "phoenix ip system";
  39.     }
  40.     if ( ! RegisterClass (&wndclass) )
  41.     return FALSE;
  42.  
  43. hWnd = CreateWindow ("phoenix ip system","Open and display a bitmap",
  44.  WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
  45.  CW_USEDEFAULT, CW_USEDEFAULT, NULL,NULL,
  46.  hInstance, NULL);
  47. if (!hWnd)
  48. return FALSE;
  49. ShowWindow (hWnd, SW_SHOWMAXIMIZED);
  50. UpdateWindow (hWnd);
  51. while ( GetMessage (&msg, NULL, 0, 0) ){
  52. TranslateMessage (&msg);
  53. DispatchMessage (&msg);
  54.     }
  55. return msg.wParam;
  56. }
  57. ////////////////////////////////////////////////////////////////
  58. LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam)
  59. {
  60.     static  HDC    hDC,hMemDC;
  61.     PAINTSTRUCT    ps;
  62.     switch (message){
  63.     case WM_PAINT:
  64. {         
  65. hDC = BeginPaint(hWnd, &ps);
  66. if (hBitmap)
  67. hMemDC = CreateCompatibleDC(hDC);
  68. if (hPalette)
  69. {           
  70. SelectPalette (hDC, hPalette, FALSE); 
  71. SelectPalette (hMemDC, hPalette, FALSE);
  72. RealizePalette (hDC);
  73. }   
  74. SelectObject(hMemDC, hBitmap); 
  75. BitBlt(hDC, 0, 0, bi.biWidth, bi.biHeight, hMemDC, 0, 0, SRCCOPY);
  76. DeleteDC(hMemDC);
  77. }
  78. EndPaint(hWnd, &ps);
  79. break;
  80. }
  81. case WM_DESTROY: //注意释放内存和位图,调色板句柄
  82.         if(hBitmap!=NULL)
  83.         DeleteObject(hBitmap);
  84.     
  85. if(hPalette!=NULL)                     
  86.         DeleteObject(hPalette);
  87. if(hImgData!=NULL){
  88. GlobalUnlock(hImgData);
  89. GlobalFree(hImgData);
  90. }         
  91.     PostQuitMessage (0);
  92.     return 0;
  93. case WM_COMMAND:
  94.     switch (wParam){
  95.     case IDM_LOADBMP: 
  96. //注意重新分配内存和调色板,位图句柄时,先释放原来的
  97.         if(hBitmap!=NULL){     
  98. DeleteObject(hBitmap);
  99.             hBitmap=NULL;
  100.         }
  101.         if(hPalette!=NULL){                     
  102. DeleteObject(hPalette);
  103.             hPalette=NULL;
  104.         }
  105. if(hImgData!=NULL){
  106. GlobalUnlock(hImgData);
  107. GlobalFree(hImgData);  
  108. hImgData=NULL;
  109. }         
  110. if(LoadBmpFile(hWnd,"c:\test.bmp")) //成功,则重画窗口
  111.                 InvalidateRect(hWnd,NULL,TRUE);
  112.         break;
  113.     case IDM_EXIT:
  114.         SendMessage(hWnd,WM_DESTROY,0,0L);
  115.         break;
  116.     }
  117. break;                
  118.     }
  119.     return DefWindowProc (hWnd, message, wParam, lParam);
  120. }
  121. ////////////////////////////////////////////////////////////////
  122. BOOL LoadBmpFile (HWND hWnd,char *BmpFileName)
  123. {   
  124.     HFILE              hf;
  125.     LPBITMAPINFOHEADER lpImgData;
  126.     LOGPALETTE         *pPal;
  127.     LPRGBQUAD          lpRGB;
  128.     HPALETTE           hPrevPalette; 
  129.     HDC                hDc;
  130. HLOCAL             hPal;
  131. DWORD    LineBytes;
  132. DWORD         ImgSize;
  133. DWORD              NumColors;
  134. DWORD              i;
  135.     if((hf=_lopen(BmpFileName,OF_READ))==HFILE_ERROR){
  136.         MessageBox(hWnd,"File c:\test.bmp not found!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  137.         return FALSE;
  138. }
  139. _lread(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  140. _lread(hf,(LPSTR)&bi,sizeof(BITMAPINFOHEADER));
  141. LineBytes=(DWORD)WIDTHBYTES(bi.biWidth*bi.biBitCount);
  142. ImgSize=(DWORD)LineBytes*bi.biHeight;
  143.     if(bi.biClrUsed!=0)
  144. NumColors=(DWORD)bi.biClrUsed;
  145. else
  146.         switch(bi.biBitCount){
  147.         case 1:
  148.              NumColors=2;
  149.              break;
  150.          case 4:
  151.              NumColors=16;
  152.              break;
  153.          case 8:
  154.              NumColors=256;
  155.              break;
  156.          case 24:
  157.              NumColors=0;
  158.              break;
  159.               default:
  160.                   MessageBox(hWnd,"Invalid color numbers!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  161.                   _lclose(hf);
  162.                   return FALSE; 
  163.         }
  164. if(bf.bfOffBits!=(DWORD)(NumColors*sizeof(RGBQUAD)+sizeof(BITMAPFILEHEADER)
  165. +sizeof(BITMAPINFOHEADER)))
  166. {
  167.      MessageBox(hWnd,"Invalid color numbers!","Error Message" ,MB_OK|
  168.                MB_ICONEXCLAMATION);
  169. _lclose(hf);
  170. return FALSE; 
  171. }
  172. bf.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+ImgSize;
  173. if((hImgData=GlobalAlloc(GHND,(DWORD)(sizeof(BITMAPINFOHEADER)+
  174.      NumColors*sizeof(RGBQUAD)+ImgSize)))==NULL)
  175. {
  176.      MessageBox(hWnd,"Error alloc memory!","ErrorMessage",MB_OK|
  177.                    MB_ICONEXCLAMATION);
  178.     _lclose(hf);
  179. return FALSE;
  180. }
  181.   
  182. lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData); 
  183.     _llseek(hf,sizeof(BITMAPFILEHEADER),SEEK_SET);
  184. _hread(hf,(char *)lpImgData,(long)sizeof(BITMAPINFOHEADER)
  185.            +(long)NumColors*sizeof(RGBQUAD)+ImgSize);
  186. _lclose(hf);
  187.     if(NumColors!=0)
  188. {                    
  189.     hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
  190.     pPal =(LOGPALETTE *)LocalLock(hPal);
  191.     pPal->palNumEntries =(WORD) NumColors;
  192. pPal->palVersion    = 0x300;
  193.     lpRGB = (LPRGBQUAD)((LPSTR)lpImgData + (DWORD)sizeof(BITMAPINFOHEADER));
  194. for (i = 0; i < NumColors; i++) {
  195.       pPal->palPalEntry[i].peRed=lpRGB->rgbRed;
  196. pPal->palPalEntry[i].peGreen=lpRGB->rgbGreen;
  197. pPal->palPalEntry[i].peBlue=lpRGB->rgbBlue;
  198. pPal->palPalEntry[i].peFlags=(BYTE)0;
  199. lpRGB++;
  200. }
  201. hPalette=CreatePalette(pPal);
  202. LocalUnlock(hPal);
  203. LocalFree(hPal);
  204. }
  205. hDc=GetDC(hWnd);
  206. if(hPalette){
  207.         hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  208. RealizePalette(hDc);
  209. }
  210. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpImgData, (LONG)CBM_INIT,
  211. (LPSTR)lpImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  212.     (LPBITMAPINFO)lpImgData, DIB_RGB_COLORS);
  213. if(hPalette && hPrevPalette){
  214. SelectPalette(hDc,hPrevPalette,FALSE);
  215. RealizePalette(hDc);
  216. }
  217.  
  218. ReleaseDC(hWnd,hDc);
  219. GlobalUnlock(hImgData);
  220. return TRUE; 
  221. }