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

图形图像处理

开发平台:

Visual C++

  1. //////////////////////////////////////////////////////////////
  2. //Name:geotrans.c
  3. //Purpose: To perform geometric transform
  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. #include "memory.h"
  10. #include "math.h"
  11. //macro definition
  12. #define WIDTHBYTES(i)    ((i+31)/32*4)
  13. #define PI 3.1415926535
  14. #define RADIAN(angle) ((angle)*PI/180.0) //convert angle to radian
  15. //function declaration
  16. int PASCAL WinMain (HANDLE, HANDLE, LPSTR, int);
  17. LRESULT CALLBACK MainWndProc(HWND , UINT,WPARAM, LPARAM);
  18. BOOL LoadBmpFile (HWND hWnd,char *BmpFileName);
  19. BOOL CALLBACK InputBox( HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam );
  20. BOOL Translation(HWND hWnd);
  21. BOOL Rotation(HWND hWnd);
  22. BOOL Mirror(HWND hWnd,BOOL XDirection);
  23. BOOL Transpose(HWND hWnd);
  24. BOOL Zoom(HWND hWnd);
  25. //global variable declaration
  26. BITMAPFILEHEADER   bf;
  27. BITMAPINFOHEADER   bi;
  28. HPALETTE           hPalette=NULL;
  29. HBITMAP            hBitmap=NULL;
  30. HGLOBAL            hImgData=NULL;
  31. DWORD              NumColors;
  32. DWORD              LineBytes;
  33. HWND               hWnd;
  34. HINSTANCE          ghInst;
  35. int                xOffset=0,yOffset=0;
  36. float    RotateAngle=0.0f;
  37. float              ZoomRatio=0.25f;
  38. DWORD              ImgWidth=0 , ImgHeight=0;
  39. ///////////////////////////////////////////////////////////
  40. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  41.     LPSTR lpszCmdLine, int nCmdShow)
  42. {
  43. MSG       msg;
  44. WNDCLASS  wndclass;
  45. ghInst=hInstance;
  46. if ( ! hPrevInstance ){
  47. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  48. wndclass.lpfnWndProc = MainWndProc;
  49. wndclass.cbClsExtra = 0;
  50. wndclass.cbWndExtra = 0;
  51. wndclass.hInstance = hInstance;
  52. wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  53. wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  54. wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  55. wndclass.lpszMenuName =  "BMPMENU";
  56. wndclass.lpszClassName = "phoenix ip system";
  57.     }
  58.     if ( ! RegisterClass (&wndclass) )
  59.     return FALSE;
  60.  
  61. hWnd = CreateWindow ("phoenix ip system","geometric transform",
  62.  WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
  63.  CW_USEDEFAULT, CW_USEDEFAULT, NULL,NULL,
  64.  hInstance, NULL);
  65. if (!hWnd)
  66. return FALSE;
  67. ShowWindow (hWnd, SW_SHOWMAXIMIZED);
  68. UpdateWindow (hWnd);
  69. while ( GetMessage (&msg, NULL, 0, 0) ){
  70. TranslateMessage (&msg);
  71. DispatchMessage (&msg);
  72.     }
  73. return msg.wParam;
  74. }
  75. ////////////////////////////////////////////////////////////////
  76. LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam)
  77. {
  78.     static  HDC    hDC,hMemDC;
  79.     PAINTSTRUCT    ps;
  80.     switch (message){
  81.     case WM_PAINT:
  82. {         
  83. hDC = BeginPaint(hWnd, &ps);
  84. if (hBitmap)
  85. hMemDC = CreateCompatibleDC(hDC);
  86. if (hPalette)
  87. {           
  88. SelectPalette (hDC, hPalette, FALSE); 
  89. SelectPalette (hMemDC, hPalette, FALSE);
  90. RealizePalette (hDC);
  91. }   
  92. SelectObject(hMemDC, hBitmap); 
  93. BitBlt(hDC, 0, 0, ImgWidth,ImgHeight, hMemDC, 0, 0, SRCCOPY);
  94. DeleteDC(hMemDC);
  95. }
  96. EndPaint(hWnd, &ps);
  97. break;
  98. }
  99. case WM_DESTROY: //注意释放内存和位图,调色板句柄
  100.         if(hBitmap!=NULL)
  101.         DeleteObject(hBitmap);
  102.     
  103. if(hPalette!=NULL)                     
  104.         DeleteObject(hPalette);
  105. if(hImgData!=NULL){
  106. GlobalUnlock(hImgData);
  107. GlobalFree(hImgData);
  108. }         
  109.     PostQuitMessage (0);
  110.     return 0;
  111. case WM_COMMAND:
  112.     switch (wParam){
  113.     case IDM_LOADBMP: 
  114. //注意重新分配内存和调色板,位图句柄时,先释放原来的
  115.         if(hBitmap!=NULL){     
  116. DeleteObject(hBitmap);
  117.             hBitmap=NULL;
  118.         }
  119.         if(hPalette!=NULL){                     
  120. DeleteObject(hPalette);
  121.             hPalette=NULL;
  122.         }
  123. if(hImgData!=NULL){
  124. GlobalUnlock(hImgData);
  125. GlobalFree(hImgData);  
  126. hImgData=NULL;
  127. }         
  128. if(LoadBmpFile(hWnd,"c:\test.bmp")) //成功,则重画窗口
  129.                 InvalidateRect(hWnd,NULL,TRUE);
  130.         break;
  131. case IDM_TRANSLATION: 
  132. if(hImgData!=NULL){
  133. if(Translation(hWnd))
  134. InvalidateRect(hWnd,NULL,TRUE);
  135. }
  136. else
  137. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  138. break;
  139. case IDM_ROTATION: 
  140. if(hImgData!=NULL){
  141. if(Rotation(hWnd))
  142. InvalidateRect(hWnd,NULL,TRUE);
  143. }
  144. else
  145. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  146. break;
  147. case IDM_MIRRORX: 
  148. if(hImgData!=NULL){
  149. if(Mirror(hWnd,TRUE))
  150. InvalidateRect(hWnd,NULL,TRUE);
  151. }
  152. else
  153. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  154. break;
  155. case IDM_MIRRORY: 
  156. if(hImgData!=NULL){
  157. if(Mirror(hWnd,FALSE))
  158. InvalidateRect(hWnd,NULL,TRUE);
  159. }
  160. else
  161. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  162. break;
  163. case IDM_TRANSPOSE: 
  164. if(hImgData!=NULL){
  165. if(Transpose(hWnd))
  166. InvalidateRect(hWnd,NULL,TRUE);
  167. }
  168. else
  169. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  170. break;
  171. case IDM_ZOOM: 
  172. if(hImgData!=NULL){
  173. if(Zoom(hWnd))
  174. InvalidateRect(hWnd,NULL,TRUE);
  175. }
  176. else
  177. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  178. break;
  179. case IDM_EXIT:
  180.         SendMessage(hWnd,WM_DESTROY,0,0L);
  181.         break;
  182.     }
  183. break;                
  184.     }
  185.     return DefWindowProc (hWnd, message, wParam, lParam);
  186. }
  187. ////////////////////////////////////////////////////////////////
  188. BOOL LoadBmpFile (HWND hWnd,char *BmpFileName)
  189. {   
  190.     HFILE              hf;
  191.     LPBITMAPINFOHEADER lpImgData;
  192.     LOGPALETTE         *pPal;
  193.     LPRGBQUAD          lpRGB;
  194.     HPALETTE           hPrevPalette; 
  195.     HDC                hDc;
  196. HLOCAL             hPal;
  197. DWORD         ImgSize;
  198. DWORD              i;
  199.     if((hf=_lopen("test.bmp",OF_READ))==HFILE_ERROR){
  200.         MessageBox(hWnd,"File c:\test.bmp not found!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  201.         return FALSE;
  202. }
  203. _lread(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  204. _lread(hf,(LPSTR)&bi,sizeof(BITMAPINFOHEADER));
  205. ImgWidth=bi.biWidth;
  206. ImgHeight=bi.biHeight;
  207. LineBytes=(DWORD)WIDTHBYTES(bi.biWidth*bi.biBitCount);
  208. ImgSize=(DWORD)LineBytes*bi.biHeight;
  209.     if(bi.biClrUsed!=0)
  210. NumColors=(DWORD)bi.biClrUsed;
  211. else
  212.         switch(bi.biBitCount){
  213.         case 1:
  214.              NumColors=2;
  215.              break;
  216.          case 4:
  217.              NumColors=16;
  218.              break;
  219.          case 8:
  220.              NumColors=256;
  221.              break;
  222.          case 24:
  223.              NumColors=0;
  224.              break;
  225.               default:
  226.                   MessageBox(hWnd,"Invalid color numbers!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  227.                   _lclose(hf);
  228.                   return FALSE; 
  229.         }
  230. if(bf.bfOffBits!=(DWORD)(NumColors*sizeof(RGBQUAD)+sizeof(BITMAPFILEHEADER)
  231. +sizeof(BITMAPINFOHEADER)))
  232. {
  233.      MessageBox(hWnd,"Invalid color numbers!","Error Message" ,MB_OK|
  234.                MB_ICONEXCLAMATION);
  235. _lclose(hf);
  236. return FALSE; 
  237. }
  238.         bf.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+ImgSize;
  239. if((hImgData=GlobalAlloc(GHND,(DWORD)(sizeof(BITMAPINFOHEADER)+
  240.      NumColors*sizeof(RGBQUAD)+ImgSize)))==NULL)
  241. {
  242.      MessageBox(hWnd,"Error alloc memory!","ErrorMessage",MB_OK|
  243.                    MB_ICONEXCLAMATION);
  244.     _lclose(hf);
  245. return FALSE;
  246. }
  247.   
  248. lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData); 
  249.     _llseek(hf,sizeof(BITMAPFILEHEADER),SEEK_SET);
  250. _hread(hf,(char *)lpImgData,(long)sizeof(BITMAPINFOHEADER)
  251.            +(long)NumColors*sizeof(RGBQUAD)+ImgSize);
  252. _lclose(hf);
  253.     if(NumColors!=0)
  254. {                    
  255.     hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
  256.     pPal =(LOGPALETTE *)LocalLock(hPal);
  257.     pPal->palNumEntries =(WORD) NumColors;
  258. pPal->palVersion    = 0x300;
  259.     lpRGB = (LPRGBQUAD)((LPSTR)lpImgData + (DWORD)sizeof(BITMAPINFOHEADER));
  260. for (i = 0; i < NumColors; i++) {
  261.       pPal->palPalEntry[i].peRed=lpRGB->rgbRed;
  262. pPal->palPalEntry[i].peGreen=lpRGB->rgbGreen;
  263. pPal->palPalEntry[i].peBlue=lpRGB->rgbBlue;
  264. pPal->palPalEntry[i].peFlags=(BYTE)0;
  265. lpRGB++;
  266. }
  267. hPalette=CreatePalette(pPal);
  268. LocalUnlock(hPal);
  269. LocalFree(hPal);
  270. }
  271. hDc=GetDC(hWnd);
  272. if(hPalette){
  273.         hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  274. RealizePalette(hDc);
  275. }
  276. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpImgData, (LONG)CBM_INIT,
  277. (LPSTR)lpImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  278.     (LPBITMAPINFO)lpImgData, DIB_RGB_COLORS);
  279. if(hPalette && hPrevPalette){
  280. SelectPalette(hDc,hPrevPalette,FALSE);
  281. RealizePalette(hDc);
  282. }
  283.  
  284. ReleaseDC(hWnd,hDc);
  285. GlobalUnlock(hImgData);
  286. return TRUE; 
  287. }
  288. ////////////////////////////////////////////////////////////////
  289. BOOL CALLBACK InputBox( HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam )
  290. char str[80];
  291.     switch  ( message ){
  292.     case WM_INITDIALOG:    
  293.       SetDlgItemText(hDlg,ID_XOFFSET,"0");
  294.       SetDlgItemText(hDlg,ID_YOFFSET,"0");
  295.       SetDlgItemText(hDlg,ID_ANGLE,"0");
  296.       SetDlgItemText(hDlg,ID_ZOOMRATIO,"0.25");
  297.      return TRUE;
  298.     case WM_COMMAND:    
  299.      if ( wParam == IDOK || wParam == IDCANCEL )
  300.         {
  301.          GetDlgItemText(hDlg,ID_XOFFSET,str,80);
  302. xOffset=atoi(str);
  303.          GetDlgItemText(hDlg,ID_YOFFSET,str,80);
  304. yOffset=atoi(str);
  305.          GetDlgItemText(hDlg,ID_ANGLE,str,80);
  306. RotateAngle=(float)atof(str);
  307.          GetDlgItemText(hDlg,ID_ZOOMRATIO,str,80);
  308. ZoomRatio=(float)atof(str);
  309.             EndDialog ( hDlg, TRUE );
  310.             return  TRUE;
  311.         }
  312.         break;
  313.     }
  314. return FALSE;      
  315. }
  316. ////////////////////////////////////////////////////////////////
  317. BOOL Translation(HWND hWnd)
  318. {
  319. DLGPROC            dlgInputBox = NULL;
  320. DWORD              OffBits,BufSize;
  321.     LPBITMAPINFOHEADER lpImgData;
  322. LPSTR              lpPtr;
  323. HLOCAL             hTempImgData;
  324. LPBITMAPINFOHEADER lpTempImgData;
  325. LPSTR              lpTempPtr;
  326. int    SrcX0,SrcY0,SrcX1,SrcY1,DstX0,DstY0,DstX1,DstY1;
  327. int                RectWidth,RectHeight;
  328. BOOL               xVisible,yVisible;
  329. HDC                hDc;
  330. HFILE              hf;
  331. int                i;
  332. dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
  333. DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
  334. FreeProcInstance ( (FARPROC) dlgInputBox );
  335. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  336. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  337. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  338.     {
  339.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  340.         return FALSE;
  341.     }
  342.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  343. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  344. lpPtr=(char *)lpImgData;
  345. lpTempPtr=(char *)lpTempImgData;
  346. memset(lpTempPtr,(BYTE)255,BufSize);
  347. memcpy(lpTempPtr,lpPtr,OffBits);
  348. xVisible=TRUE;
  349. if( xOffset<= -bi.biWidth )
  350. xVisible=FALSE;
  351. else if( xOffset<=0){
  352. DstX0=0;
  353. DstX1=bi.biWidth+xOffset;
  354. }
  355. else if ( xOffset<bi.biWidth){
  356. DstX0=xOffset;
  357. DstX1=bi.biWidth;
  358. }
  359. else
  360. xVisible=FALSE;
  361. SrcX0=DstX0-xOffset;
  362. SrcX1=DstX1-xOffset;
  363. RectWidth=DstX1-DstX0;
  364. yVisible=TRUE;
  365. if( yOffset<= -bi.biHeight )
  366. yVisible=FALSE;
  367. else if( yOffset<=0){
  368. DstY0=0;
  369. DstY1=bi.biHeight+yOffset;
  370. }
  371. else if ( yOffset<bi.biHeight){
  372. DstY0=yOffset;
  373. DstY1=bi.biHeight;
  374. }
  375. else
  376. yVisible=FALSE;
  377. SrcY0=DstY0-yOffset;
  378. SrcY1=DstY1-yOffset;
  379. RectHeight=DstY1-DstY0;
  380. if( xVisible && yVisible){
  381. for(i=0;i<RectHeight;i++){
  382. lpPtr=(char *)lpImgData+(BufSize-LineBytes-(i+SrcY0)*LineBytes)+SrcX0;
  383. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-(i+DstY0)*LineBytes)+DstX0;
  384. memcpy(lpTempPtr,lpPtr,RectWidth);
  385. }
  386. }
  387. hDc=GetDC(hWnd);
  388.     if(hBitmap!=NULL)
  389.     DeleteObject(hBitmap);
  390. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  391. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  392.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  393.     hf=_lcreat("c:\translation.bmp",0);
  394. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  395. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  396. _lclose(hf);
  397.   ReleaseDC(hWnd,hDc);
  398. LocalUnlock(hTempImgData);
  399. LocalFree(hTempImgData);
  400. GlobalUnlock(hImgData);
  401. return TRUE;
  402. }
  403. ////////////////////////////////////////////////////////////////
  404. BOOL Rotation(HWND hWnd)
  405. {
  406. DLGPROC            dlgInputBox = NULL;
  407. DWORD              OffBits,SrcBufSize,DstBufSize,DstLineBytes;
  408.     LPBITMAPINFOHEADER lpImgData;
  409. LPSTR              lpPtr;
  410. HLOCAL             hTempImgData;
  411. LPBITMAPINFOHEADER lpTempImgData;
  412. LPSTR              lpTempPtr;
  413. float    SrcX1,SrcY1,SrcX2,SrcY2,SrcX3,SrcY3,SrcX4,SrcY4;
  414. float    DstX1,DstY1,DstX2,DstY2,DstX3,DstY3,DstX4,DstY4;
  415. DWORD              Wold,Hold,Wnew,Hnew;
  416. HDC                hDc;
  417. HFILE              hf;
  418. DWORD              x0,y0,x1,y1;
  419. float              cosa,sina; //cos(a),sin(a)
  420. float    num1,num2;
  421. BITMAPFILEHEADER   DstBf;
  422. BITMAPINFOHEADER   DstBi;
  423. dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
  424. DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
  425. FreeProcInstance ( (FARPROC) dlgInputBox );
  426. RotateAngle=(float)RADIAN(RotateAngle);
  427. cosa=(float)cos((double)RotateAngle);
  428. sina=(float)sin((double)RotateAngle);
  429. Wold=bi.biWidth;
  430. Hold=bi.biHeight;
  431. SrcX1=(float)(-0.5*Wold);
  432. SrcY1=(float)(0.5*Hold);
  433. SrcX2=(float)(0.5*Wold);
  434. SrcY2=(float)(0.5*Hold);
  435. SrcX3=(float)(-0.5*Wold);
  436. SrcY3=(float)(-0.5*Hold);
  437. SrcX4=(float)(0.5*Wold);
  438. SrcY4=(float)(-0.5*Hold);
  439. DstX1=cosa*SrcX1+sina*SrcY1;
  440. DstY1=-sina*SrcX1+cosa*SrcY1;
  441. DstX2=cosa*SrcX2+sina*SrcY2;
  442. DstY2=-sina*SrcX2+cosa*SrcY2;
  443. DstX3=cosa*SrcX3+sina*SrcY3;
  444. DstY3=-sina*SrcX3+cosa*SrcY3;
  445. DstX4=cosa*SrcX4+sina*SrcY4;
  446. DstY4=-sina*SrcX4+cosa*SrcY4;
  447. Wnew = (DWORD)(max(fabs(DstX4-DstX1), fabs(DstX3-DstX2))+0.5);
  448.     Hnew = (DWORD)(max(fabs(DstY4-DstY1), fabs(DstY3-DstY2))+0.5);
  449. num1=(float)( -0.5*Wnew*cosa-0.5*Hnew*sina+0.5*Wold);
  450. num2=(float)(0.5*Wnew*sina-0.5*Hnew*cosa+0.5*Hold);
  451. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  452. SrcBufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  453. ImgWidth=Wnew;
  454. ImgHeight=Hnew;
  455. DstLineBytes=(DWORD)WIDTHBYTES(Wnew*bi.biBitCount);
  456. DstBufSize=(DWORD)(sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+(DWORD)DstLineBytes*Hnew);
  457. if((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
  458.     {
  459.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  460.         return FALSE;
  461.     }
  462.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  463. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  464. lpPtr=(char *)lpImgData;
  465. lpTempPtr=(char *)lpTempImgData;
  466. memset(lpTempPtr,(BYTE)255,DstBufSize);
  467. memcpy(lpTempPtr,lpPtr,OffBits);
  468. //get new bitmapfileheader and bitmapinfoheader
  469. memcpy((char *)&DstBf,(char *)&bf,sizeof(BITMAPFILEHEADER));
  470. memcpy((char *)&DstBi,(char *)&bi,sizeof(BITMAPINFOHEADER));
  471. DstBf.bfSize=DstBufSize+sizeof(BITMAPFILEHEADER);
  472. DstBi.biWidth=Wnew;
  473. DstBi.biHeight=Hnew;
  474. //overwrite bitmapinfoheader with the new one
  475. memcpy(lpTempPtr,(char *)&DstBi,sizeof(BITMAPINFOHEADER));
  476. for(y1=0;y1<Hnew;y1++)
  477. for(x1=0;x1<Wnew;x1++){
  478. x0= (DWORD)(x1*cosa+y1*sina+num1);
  479. y0= (DWORD)(-1.0f*x1*sina+y1*cosa+num2);
  480. if( (x0>=0) && (x0<Wold) && (y0>=0) && (y0<Hold))
  481. {
  482. lpPtr=(char *)lpImgData+(SrcBufSize-LineBytes-y0*LineBytes)+x0;
  483. lpTempPtr=(char *)lpTempImgData+(DstBufSize-DstLineBytes-y1*DstLineBytes)+x1;
  484. *lpTempPtr=*lpPtr;
  485. }
  486. }
  487. hDc=GetDC(hWnd);
  488.     if(hBitmap!=NULL)
  489.     DeleteObject(hBitmap);
  490. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  491. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  492.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  493.     hf=_lcreat("c:\rotation.bmp",0);
  494. _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER)); 
  495. _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
  496. _lclose(hf);
  497.   ReleaseDC(hWnd,hDc);
  498. LocalUnlock(hTempImgData);
  499. LocalFree(hTempImgData);
  500. GlobalUnlock(hImgData);
  501. return TRUE;
  502. }
  503. ////////////////////////////////////////////////////////////////
  504. BOOL Mirror(HWND hWnd,BOOL XDirection)
  505. {
  506. DWORD              OffBits,BufSize;
  507.     LPBITMAPINFOHEADER lpImgData;
  508. LPSTR              lpPtr;
  509. HLOCAL             hTempImgData;
  510. LPBITMAPINFOHEADER lpTempImgData;
  511. LPSTR              lpTempPtr;
  512. HDC                hDc;
  513. HFILE              hf;
  514. LONG               x0,y0,x1,y1;
  515. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  516. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  517. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  518.     {
  519.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  520.         return FALSE;
  521.     }
  522.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  523. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  524. lpPtr=(char *)lpImgData;
  525. lpTempPtr=(char *)lpTempImgData;
  526. memset(lpTempPtr,(BYTE)255,BufSize);
  527. memcpy(lpTempPtr,lpPtr,OffBits);
  528. if( XDirection){
  529. for(y1=0;y1<bi.biHeight;y1++)
  530. for(x1=0;x1<bi.biWidth;x1++){
  531. x0=bi.biWidth-1-x1;
  532. y0=y1;
  533. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y0*LineBytes)+x0;
  534. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y1*LineBytes)+x1;
  535. *lpTempPtr=*lpPtr;
  536. }
  537. }
  538. else{
  539. for(y1=0;y1<bi.biHeight;y1++)
  540. for(x1=0;x1<bi.biWidth;x1++){
  541. x0=x1;
  542. y0=bi.biHeight-1-y1;
  543. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y0*LineBytes)+x0;
  544. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y1*LineBytes)+x1;
  545. *lpTempPtr=*lpPtr;
  546. }
  547. }
  548. hDc=GetDC(hWnd);
  549.     if(hBitmap!=NULL)
  550.     DeleteObject(hBitmap);
  551. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  552. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  553.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  554. if( XDirection)
  555. hf=_lcreat("c:\mirrorx.bmp",0);
  556. else
  557. hf=_lcreat("c:\mirrory.bmp",0);
  558. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  559. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  560. _lclose(hf);
  561.   ReleaseDC(hWnd,hDc);
  562. LocalUnlock(hTempImgData);
  563. LocalFree(hTempImgData);
  564. GlobalUnlock(hImgData);
  565. return TRUE;
  566. }
  567. ////////////////////////////////////////////////////////////////
  568. BOOL Transpose(HWND hWnd)
  569. {
  570. DWORD              OffBits,SrcBufSize,DstBufSize,DstLineBytes;
  571.     LPBITMAPINFOHEADER lpImgData;
  572. LPSTR              lpPtr;
  573. HLOCAL             hTempImgData;
  574. LPBITMAPINFOHEADER lpTempImgData;
  575. LPSTR              lpTempPtr;
  576. DWORD              Wnew,Hnew;
  577. HDC                hDc;
  578. HFILE              hf;
  579. DWORD              x0,y0,x1,y1;
  580. BITMAPFILEHEADER   DstBf;
  581. BITMAPINFOHEADER   DstBi;
  582. Wnew = (DWORD)bi.biHeight;
  583.     Hnew = (DWORD)bi.biWidth;
  584. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  585. SrcBufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  586. ImgWidth=Wnew;
  587. ImgHeight=Hnew;
  588. DstLineBytes=(DWORD)WIDTHBYTES(Wnew*bi.biBitCount);
  589. DstBufSize=(DWORD)(sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+(DWORD)DstLineBytes*Hnew);
  590. if((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
  591.     {
  592.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  593.         return FALSE;
  594.     }
  595.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  596. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  597. lpPtr=(char *)lpImgData;
  598. lpTempPtr=(char *)lpTempImgData;
  599. memset(lpTempPtr,(BYTE)255,DstBufSize);
  600. memcpy(lpTempPtr,lpPtr,OffBits);
  601. //get new bitmapfileheader and bitmapinfoheader
  602. memcpy((char *)&DstBf,(char *)&bf,sizeof(BITMAPFILEHEADER));
  603. memcpy((char *)&DstBi,(char *)&bi,sizeof(BITMAPINFOHEADER));
  604. DstBf.bfSize=DstBufSize+sizeof(BITMAPFILEHEADER);
  605. DstBi.biWidth=Wnew;
  606. DstBi.biHeight=Hnew;
  607. //overwrite bitmapinfoheader with the new one
  608. memcpy(lpTempPtr,(char *)&DstBi,sizeof(BITMAPINFOHEADER));
  609. for(y1=0;y1<Hnew;y1++)
  610. for(x1=0;x1<Wnew;x1++){
  611. x0= y1;
  612. y0= x1;
  613. lpPtr=(char *)lpImgData+(SrcBufSize-LineBytes-y0*LineBytes)+x0;
  614. lpTempPtr=(char *)lpTempImgData+(DstBufSize-DstLineBytes-y1*DstLineBytes)+x1;
  615. *lpTempPtr=*lpPtr;
  616. }
  617. hDc=GetDC(hWnd);
  618.     if(hBitmap!=NULL)
  619.     DeleteObject(hBitmap);
  620. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  621. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  622.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  623.     hf=_lcreat("c:\transpose.bmp",0);
  624. _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER)); 
  625. _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
  626. _lclose(hf);
  627.   ReleaseDC(hWnd,hDc);
  628. LocalUnlock(hTempImgData);
  629. LocalFree(hTempImgData);
  630. GlobalUnlock(hImgData);
  631. return TRUE;
  632. }
  633. ////////////////////////////////////////////////////////////////
  634. BOOL Zoom(HWND hWnd)
  635. {
  636. DLGPROC            dlgInputBox = NULL;
  637. DWORD              OffBits,SrcBufSize,DstBufSize,DstLineBytes;
  638.     LPBITMAPINFOHEADER lpImgData;
  639. LPSTR              lpPtr;
  640. HLOCAL             hTempImgData;
  641. LPBITMAPINFOHEADER lpTempImgData;
  642. LPSTR              lpTempPtr;
  643. DWORD              Wold,Hold,Wnew,Hnew;
  644. HDC                hDc;
  645. HFILE              hf;
  646. DWORD              x0,y0,x1,y1;
  647. float    num1;
  648. BITMAPFILEHEADER   DstBf;
  649. BITMAPINFOHEADER   DstBi;
  650. dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
  651. DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
  652. FreeProcInstance ( (FARPROC) dlgInputBox );
  653. num1=(float)(1.0/ZoomRatio);
  654. Wold=bi.biWidth;
  655. Hold=bi.biHeight;
  656. Wnew = (DWORD)(Wold*ZoomRatio+0.5);
  657.     Hnew = (DWORD)(Hold*ZoomRatio+0.5);
  658. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  659. SrcBufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  660. ImgWidth=Wnew;
  661. ImgHeight=Hnew;
  662. DstLineBytes=(DWORD)WIDTHBYTES(Wnew*bi.biBitCount);
  663. DstBufSize=(DWORD)(sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+(DWORD)DstLineBytes*Hnew);
  664. if((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
  665.     {
  666.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  667.         return FALSE;
  668.     }
  669.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  670. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  671. lpPtr=(char *)lpImgData;
  672. lpTempPtr=(char *)lpTempImgData;
  673. memset(lpTempPtr,(BYTE)255,DstBufSize);
  674. memcpy(lpTempPtr,lpPtr,OffBits);
  675. //get new bitmapfileheader and bitmapinfoheader
  676. memcpy((char *)&DstBf,(char *)&bf,sizeof(BITMAPFILEHEADER));
  677. memcpy((char *)&DstBi,(char *)&bi,sizeof(BITMAPINFOHEADER));
  678. DstBf.bfSize=DstBufSize+sizeof(BITMAPFILEHEADER);
  679. DstBi.biWidth=Wnew;
  680. DstBi.biHeight=Hnew;
  681. //overwrite bitmapinfoheader with the new one
  682. memcpy(lpTempPtr,(char *)&DstBi,sizeof(BITMAPINFOHEADER));
  683. for(y1=0;y1<Hnew;y1++)
  684. for(x1=0;x1<Wnew;x1++){
  685. x0= (DWORD)(x1*num1);
  686. y0= (DWORD)(y1*num1);
  687. if( (x0>=0) && (x0<Wold) && (y0>=0) && (y0<Hold))
  688. {
  689. lpPtr=(char *)lpImgData+(SrcBufSize-LineBytes-y0*LineBytes)+x0;
  690. lpTempPtr=(char *)lpTempImgData+(DstBufSize-DstLineBytes-y1*DstLineBytes)+x1;
  691. *lpTempPtr=*lpPtr;
  692. }
  693. }
  694. hDc=GetDC(hWnd);
  695.     if(hBitmap!=NULL)
  696.     DeleteObject(hBitmap);
  697. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  698. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  699.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  700.     hf=_lcreat("c:\zoom.bmp",0);
  701. _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER)); 
  702. _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
  703. _lclose(hf);
  704.   ReleaseDC(hWnd,hDc);
  705. LocalUnlock(hTempImgData);
  706. LocalFree(hTempImgData);
  707. GlobalUnlock(hImgData);
  708. return TRUE;
  709. }