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

图形图像处理

开发平台:

Visual C++

  1. //////////////////////////////////////////////////////////////
  2. //Name:bmp.c
  3. //Purpose: phoenix's image processing system
  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 "jpeg.h"
  10. #include "memory.h"
  11. #include "math.h"
  12. #include "stdio.h"
  13. //define return value of function
  14. #define FUNC_OK 0
  15. #define FUNC_MEMORY_ERROR 1
  16. #define FUNC_FILE_ERROR 2
  17. #define FUNC_FORMAT_ERROR 3
  18. //owner defined stack
  19. typedef struct{
  20.   HGLOBAL hMem;
  21.      POINT *lpMyStack;
  22.   LONG  ElementsNum;
  23.   LONG  ptr;
  24.   }MYSTACK;
  25. //macro definition
  26. #define WIDTHBYTES(i)    ((i+31)/32*4)
  27. #define PI 3.1415926535
  28. #define RADIAN(angle) ((angle)*PI/180.0) //convert angle to radian
  29. //function declaration
  30. int PASCAL WinMain (HANDLE, HANDLE, LPSTR, int);
  31. LRESULT CALLBACK MainWndProc(HWND , UINT,WPARAM, LPARAM);
  32. BOOL LoadBmpFile (HWND hWnd,char *BmpFileName);
  33. BOOL CALLBACK InputBox( HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam );
  34. BOOL Translation(HWND hWnd);
  35. BOOL Rotation(HWND hWnd);
  36. BOOL Mirror(HWND hWnd,BOOL XDirection);
  37. BOOL Transpose(HWND hWnd);
  38. BOOL Zoom(HWND hWnd);
  39. BOOL LimbPatternM3(HWND hWnd);
  40. BOOL LimbPatternM4(HWND hWnd);
  41. BOOL Steinberg(HWND hWnd);
  42. BOOL Bmp2Txt(HWND hWnd);
  43. BOOL Histogram(HWND hWnd);
  44. long FAR PASCAL PopupWndProc (HWND,UINT,WPARAM,LPARAM);
  45. BOOL ColortoGrayScale(HWND hWnd);
  46. BOOL Trueto256(HWND hWnd);
  47. BOOL Invert(HWND hWnd);
  48. BOOL TemplateOperation(HWND hWnd, int TemplateType);
  49. BOOL ContrastStretch(HWND hWnd);
  50. BOOL Cliping(HWND hWnd);
  51. BOOL Thresholding(HWND hWnd);
  52. BOOL Slice(HWND hWnd,BOOL WithBack);
  53. BOOL HistogramEqua(HWND hWnd);
  54. BOOL Dilation(HWND hWnd,BOOL Hori);
  55. BOOL Erosion(HWND hWnd,BOOL Hori);
  56. BOOL MorphOpen(HWND hWnd,BOOL Hori);
  57. BOOL MorphClose(HWND hWnd,BOOL Hori);
  58. BOOL Thinning(HWND hWnd);
  59. BOOL Outline(HWND hWnd);
  60. BOOL Hough(HWND hWnd);
  61. BOOL MedianFilter(HWND hWnd,BOOL Hori);
  62. BOOL LapOfGauss(HWND hWnd);
  63. BOOL Contour(HWND hWnd);
  64. BOOL IsContourP(LONG x,LONG y, char *lpPtr);
  65. BOOL SeedFill(HWND hWnd);
  66. BOOL InitStack(HWND hWnd,LONG StackLen);
  67. void DeInitStack();
  68. BOOL MyPush(POINT p);
  69. POINT MyPop();
  70. BOOL IsStackEmpty();
  71. BOOL Projection(HWND hWnd,BOOL Hori);
  72. BOOL Subtraction(HWND hWnd);
  73. BOOL LoadPcxFile(HWND hWnd,char *BmpFileName);
  74. void ReadPcxLine(unsigned char *p,FILE *fp);
  75. //////////////////////////////////////////////////
  76. //Jpeg functions
  77. BOOL LoadJpegFile(HWND hWnd,char *BmpFileName);
  78. void showerror(int funcret);
  79. int  InitTag();
  80. void InitTable();
  81. int  Decode();
  82. int  DecodeMCUBlock();
  83. int  HufBlock(BYTE dchufindex,BYTE achufindex);
  84. int  DecodeElement();
  85. void IQtIZzMCUComponent(short flag);
  86. void IQtIZzBlock(short  *s ,int * d,short flag);
  87. void GetYUV(short flag);
  88. void StoreBuffer();
  89. BYTE ReadByte();
  90. void Initialize_Fast_IDCT();
  91. void Fast_IDCT(int * block);
  92. void idctrow(int * blk);
  93. void idctcol(int * blk);
  94. //global variable declaration
  95. BITMAPFILEHEADER   bf;
  96. BITMAPINFOHEADER   bi;
  97. HPALETTE           hPalette=NULL;
  98. HBITMAP            hBitmap=NULL;
  99. HGLOBAL            hImgData=NULL;
  100. DWORD              NumColors;
  101. DWORD              LineBytes;
  102. HINSTANCE          ghInst;
  103. int                xOffset=0,yOffset=0;
  104. float    RotateAngle=0.0f;
  105. float              ZoomRatio=0.25f;
  106. DWORD              ImgWidth=0 , ImgHeight=0;
  107. //template array
  108. float Template_Smooth_Box[9]={1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f};
  109. float Template_Smooth_Gauss[9]={1.0f,2.0f,1.0f,2.0f,4.0f,2.0f,1.0f,2.0f,1.0f};
  110. float Template_Sharpen_Laplacian[9]={-1.0f,-1.0f,-1.0f,-1.0f,9.0f,-1.0f,-1.0f,-1.0f,-1.0f};
  111. float Template_HSobel[9]={-1.0f,0.0f,1.0f,-2.0f,0.0f,2.0f,-1.0f,0.0f,1.0f};
  112. float Template_VSobel[9]={-1.0f,-2.0f,-1.0f,0.0f,0.0f,0.0f,1.0f,2.0f,1.0f};
  113. float Template_HIsoSobel[9]={-1.0f,0.0f,1.0f,-1.4142f,0.0f,1.4142f,-1.0f,0.0f,1.0f};
  114. float Template_VIsoSobel[9]={-1.0f,-1.4142f,-1.0f,0.0f,0.0f,0.0f,1.0f,1.4142f,1.0f};
  115. float Template_Log[25]={-2.0f,-4.0f,-4.0f,-4.0f,-2.0f,
  116.         -4.0f,0.0f,8.0f,0.0f,-4.0f,
  117. -4.0f,8.0f,24.0f,8.0f,-4.0f,
  118. -4.0f,0.0f,8.0f,0.0f,-4.0f,
  119. -2.0f,-4.0f,-4.0f,-4.0f,-2.0f};
  120. BYTE BayerPattern[8][8]={0,32,8,40,2,34,10,42,
  121.  48,16,56,24,50,18,58,26,
  122.  12,44,4,36,14,46,6,38,
  123.  60,28,52,20,62,30,54,22,
  124.  3,35,11,43,1,33,9,41,
  125.  51,19,59,27,49,17,57,25,
  126.  15,47,7,39,13,45,5,37,
  127.  63,31,55,23,61,29,53,21};
  128. static char ch[95]= {
  129. ' ',
  130. '`','1','2','3','4','5','6','7','8','9','0','-','=','\',
  131. 'q','w','e','r','t','y','u','i','o','p','[',']',
  132. 'a','s','d','f','g','h','j','k','l',';',''',
  133. 'z','x','c','v','b','n','m',',','.','/',
  134. '~','!','@','#','$','%','^','&','*','(',')','_','+','|',
  135. 'Q','W','E','R','T','Y','U','I','O','P','{','}',
  136. 'A','S','D','F','G','H','J','K','L',':','"',
  137. 'Z','X','C','V','B','N','M','<','>','?'
  138. };
  139. static int  gr[95]= {
  140.  0,
  141.  7,22,28,31,31,27,32,22,38,32,40, 6,12,20,38,32,26,20,24,40,
  142.     29,24,28,38,32,32,26,22,34,24,44,33,32,32,24,16, 6,22,26,22,
  143.  26,34,29,35,10, 6,20,14,22,47,42,34,40,10,35,21,22,22,16,14,
  144.  26,40,39,29,38,22,28,36,22,36,30,22,22,36,26,36,25,34,38,24,
  145.  36,22,12,12,26,30,30,34,39,42,41,18,18,22
  146.  };
  147. int GrayTable[256];
  148. int MaxGrayNum;
  149. int MinGrayNum;
  150. int FirstPoint=0, SecondPoint=255;
  151. float StretchRatio=2.0f;
  152. int EquaScale;
  153. MYSTACK SeedFillStack;
  154. POINT SeedPoint;
  155. BOOL IsSelecting;
  156. unsigned int  PcxBytesPerLine;
  157. LPSTR              lpPtr;
  158. //////////////////////////////////////////////////
  159. //variables used in jpeg function
  160. short SampRate_Y_H,SampRate_Y_V;
  161. short SampRate_U_H,SampRate_U_V;
  162. short SampRate_V_H,SampRate_V_V;
  163. short H_YtoU,V_YtoU,H_YtoV,V_YtoV;
  164. short Y_in_MCU,U_in_MCU,V_in_MCU;
  165. unsigned char   *lpJpegBuf;
  166. unsigned char   *lp;
  167. short qt_table[3][64];
  168. short comp_num;
  169. BYTE comp_index[3];
  170. BYTE     YDcIndex,YAcIndex,UVDcIndex,UVAcIndex;
  171. BYTE HufTabIndex;
  172. short     *YQtTable,*UQtTable,*VQtTable;
  173. BYTE And[9]={0,1,3,7,0xf,0x1f,0x3f,0x7f,0xff};
  174. short     code_pos_table[4][16],code_len_table[4][16];
  175. unsigned short code_value_table[4][256];
  176. unsigned short huf_max_value[4][16],huf_min_value[4][16];
  177. short BitPos,CurByte;
  178. short rrun,vvalue;
  179. short MCUBuffer[10*64];
  180. int QtZzMCUBuffer[10*64];
  181. short BlockBuffer[64];
  182. short ycoef,ucoef,vcoef;
  183. BOOL IntervalFlag;
  184. short interval=0;
  185. int Y[4*64],U[4*64],V[4*64];
  186. DWORD     sizei,sizej;
  187. short  restart;
  188. static  long iclip[1024];
  189. static  long *iclp;
  190. ///////////////////////////////////////////////////////////
  191. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  192.     LPSTR lpszCmdLine, int nCmdShow)
  193. {
  194. MSG       msg;
  195. WNDCLASS  wndclass;
  196. HWND      hWnd;
  197. ghInst=hInstance;
  198. if ( ! hPrevInstance ){
  199. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  200. wndclass.lpfnWndProc = MainWndProc;
  201. wndclass.cbClsExtra = 0;
  202. wndclass.cbWndExtra = 0;
  203. wndclass.hInstance = hInstance;
  204. wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  205. wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  206. wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  207. wndclass.lpszMenuName =  "BMPMENU";
  208. wndclass.lpszClassName = "phoenix ip system";
  209.     }
  210.     if ( ! RegisterClass (&wndclass) )
  211.     return FALSE;
  212. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  213.     wndclass.lpfnWndProc = PopupWndProc;
  214.     wndclass.cbClsExtra = 0;
  215.     wndclass.cbWndExtra = 0;
  216.     wndclass.hInstance = ghInst;
  217.     wndclass.hIcon = NULL;
  218.     wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  219.     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  220.     wndclass.lpszMenuName =  NULL;
  221.     wndclass.lpszClassName = "PopupWindowClass";
  222.     if ( ! RegisterClass (&wndclass) )
  223. return FALSE;
  224.  
  225. hWnd = CreateWindow ("phoenix ip system","Image Processing System",
  226.  WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
  227.  CW_USEDEFAULT, CW_USEDEFAULT, NULL,NULL,
  228.  hInstance, NULL);
  229. if (!hWnd)
  230. return FALSE;
  231. ShowWindow (hWnd, SW_SHOWMAXIMIZED);
  232. UpdateWindow (hWnd);
  233. while ( GetMessage (&msg, NULL, 0, 0) ){
  234. TranslateMessage (&msg);
  235. DispatchMessage (&msg);
  236.     }
  237. IsSelecting=FALSE;
  238. return msg.wParam;
  239. }
  240. ////////////////////////////////////////////////////////////////
  241. LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam)
  242. {
  243.     static  HDC    hDC,hMemDC;
  244.     PAINTSTRUCT    ps;
  245.     switch (message){
  246.     case WM_PAINT:
  247. {         
  248. hDC = BeginPaint(hWnd, &ps);
  249. if (hBitmap)
  250. hMemDC = CreateCompatibleDC(hDC);
  251. if (hPalette)
  252. {           
  253. SelectPalette (hDC, hPalette, FALSE); 
  254. SelectPalette (hMemDC, hPalette, FALSE);
  255. RealizePalette (hDC);
  256. }   
  257. SelectObject(hMemDC, hBitmap); 
  258. BitBlt(hDC, 0, 0, ImgWidth,ImgHeight, hMemDC, 0, 0, SRCCOPY);
  259. DeleteDC(hMemDC);
  260. }
  261. EndPaint(hWnd, &ps);
  262. break;
  263. }
  264. case WM_DESTROY: //注意释放内存和位图,调色板句柄
  265.         if(hBitmap!=NULL)
  266.         DeleteObject(hBitmap);
  267.     
  268. if(hPalette!=NULL)                     
  269.         DeleteObject(hPalette);
  270. if(hImgData!=NULL){
  271. GlobalUnlock(hImgData);
  272. GlobalFree(hImgData);
  273. }         
  274.     PostQuitMessage (0);
  275.     return 0;
  276. case WM_LBUTTONDOWN:
  277. if(IsSelecting){
  278. SeedPoint.x = LOWORD(lParam);
  279. SeedPoint.y = HIWORD(lParam);
  280. IsSelecting=FALSE;
  281. if(SeedFill(hWnd))
  282. InvalidateRect(hWnd,NULL,TRUE);
  283. }
  284.   break;
  285. case WM_COMMAND:
  286.     switch (wParam){
  287.     case IDM_LOADBMP: 
  288. //注意重新分配内存和调色板,位图句柄时,先释放原来的
  289.         if(hBitmap!=NULL){     
  290. DeleteObject(hBitmap);
  291.             hBitmap=NULL;
  292.         }
  293.         if(hPalette!=NULL){                     
  294. DeleteObject(hPalette);
  295.             hPalette=NULL;
  296.         }
  297. if(hImgData!=NULL){
  298. GlobalUnlock(hImgData);
  299. GlobalFree(hImgData);  
  300. hImgData=NULL;
  301. }         
  302. if(LoadBmpFile(hWnd,"c:\test.bmp")) //成功,则重画窗口
  303.                 InvalidateRect(hWnd,NULL,TRUE);
  304.         break;
  305. case IDM_TRANSLATION: 
  306. if(hImgData!=NULL){
  307. if(Translation(hWnd))
  308. InvalidateRect(hWnd,NULL,TRUE);
  309. }
  310. else
  311. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  312. break;
  313. case IDM_ROTATION: 
  314. if(hImgData!=NULL){
  315. if(Rotation(hWnd))
  316. InvalidateRect(hWnd,NULL,TRUE);
  317. }
  318. else
  319. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  320. break;
  321. case IDM_MIRRORX: 
  322. if(hImgData!=NULL){
  323. if(Mirror(hWnd,TRUE))
  324. InvalidateRect(hWnd,NULL,TRUE);
  325. }
  326. else
  327. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  328. break;
  329. case IDM_MIRRORY: 
  330. if(hImgData!=NULL){
  331. if(Mirror(hWnd,FALSE))
  332. InvalidateRect(hWnd,NULL,TRUE);
  333. }
  334. else
  335. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  336. break;
  337. case IDM_TRANSPOSE: 
  338. if(hImgData!=NULL){
  339. if(Transpose(hWnd))
  340. InvalidateRect(hWnd,NULL,TRUE);
  341. }
  342. else
  343. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  344. break;
  345. case IDM_ZOOM: 
  346. if(hImgData!=NULL){
  347. if(Zoom(hWnd))
  348. InvalidateRect(hWnd,NULL,TRUE);
  349. }
  350. else
  351. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  352. break;
  353. case IDM_SMOOTHING_BOX:
  354. if(hImgData!=NULL){
  355. if(TemplateOperation(hWnd,TEMPLATE_SMOOTH_BOX))
  356. InvalidateRect(hWnd,NULL,TRUE);
  357. }
  358. else
  359. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  360. break;
  361. case IDM_SMOOTHING_GAUSS:
  362. if(hImgData!=NULL){
  363. if(TemplateOperation(hWnd,TEMPLATE_SMOOTH_GAUSS))
  364. InvalidateRect(hWnd,NULL,TRUE);
  365. }
  366. else
  367. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  368. break;
  369. case IDM_SHARPENING_LAPLACIAN:
  370. if(hImgData!=NULL){
  371. if(TemplateOperation(hWnd,TEMPLATE_SHARPEN_LAPLACIAN))
  372. InvalidateRect(hWnd,NULL,TRUE);
  373. }
  374. else
  375. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  376. break;
  377. case IDM_LIMBPATTERNM3:
  378. if(hImgData!=NULL){
  379. if(LimbPatternM3(hWnd))
  380. InvalidateRect(hWnd,NULL,TRUE);
  381. }
  382. else
  383. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  384. break;
  385. case IDM_LIMBPATTERNM4:
  386. if(hImgData!=NULL){
  387. if(LimbPatternM4(hWnd))
  388. InvalidateRect(hWnd,NULL,TRUE);
  389. }
  390. else
  391. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  392. break;
  393. case IDM_STEINBERG:
  394. if(hImgData!=NULL){
  395. if(Steinberg(hWnd));
  396. InvalidateRect(hWnd,NULL,TRUE);
  397. }
  398. else
  399. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  400. break;
  401. case IDM_BMP2TXT:
  402. if(hImgData!=NULL){
  403. if(Bmp2Txt(hWnd))
  404.      MessageBox(hWnd,"Finished!","Success Message",MB_OK);
  405. }
  406. else
  407. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  408. break;
  409. case IDM_HISTOGRAM:
  410. if(hImgData!=NULL){
  411. if(Histogram(hWnd))
  412. InvalidateRect(hWnd,NULL,TRUE);
  413. }
  414. else
  415. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  416. break;
  417. case IDM_TRUE256:
  418. if(hImgData!=NULL){
  419. if(Trueto256(hWnd))
  420. InvalidateRect(hWnd,NULL,TRUE);
  421. }
  422. else
  423. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  424. break;
  425. case IDM_CLRGRAY:
  426. if(hImgData!=NULL){
  427. if(ColortoGrayScale(hWnd))
  428. InvalidateRect(hWnd,NULL,TRUE);
  429. }
  430. else
  431. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  432. break;
  433. case IDM_INVERT:
  434. if(hImgData!=NULL){
  435. if(Invert(hWnd))
  436. InvalidateRect(hWnd,NULL,TRUE);
  437. }
  438. else
  439. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  440. break;
  441. case IDM_CONTSTRETCH:
  442. if(hImgData!=NULL){
  443. if (ContrastStretch(hWnd))
  444. InvalidateRect(hWnd,NULL,TRUE);
  445. }
  446. else
  447. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  448. break;
  449. case IDM_CLIPING:
  450. if(hImgData!=NULL){
  451. if(Cliping(hWnd))
  452. InvalidateRect(hWnd,NULL,TRUE);
  453. }
  454. else
  455. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  456. break;
  457. case IDM_THRESHOLD:
  458. if(hImgData!=NULL){
  459. if(Thresholding(hWnd))
  460. InvalidateRect(hWnd,NULL,TRUE);
  461. }
  462. else
  463. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  464. break;
  465. case IDM_SLICEWITHOUTBACK:
  466. if(hImgData!=NULL){
  467. if(Slice(hWnd,FALSE))
  468. InvalidateRect(hWnd,NULL,TRUE);
  469. }
  470. else
  471. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  472. break;
  473. case IDM_SLICEWITHBACK:
  474. if(hImgData!=NULL){
  475. if(Slice(hWnd,TRUE))
  476. InvalidateRect(hWnd,NULL,TRUE);
  477. }
  478. else
  479. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  480. break;
  481. case IDM_HISTOGRAMEQUA:
  482. if(hImgData!=NULL){
  483. if(HistogramEqua(hWnd))
  484. InvalidateRect(hWnd,NULL,TRUE);
  485. }
  486. else
  487. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  488. break;
  489. case IDM_HDILATION:
  490. if(hImgData!=NULL){
  491. if(Dilation(hWnd,TRUE))
  492. InvalidateRect(hWnd,NULL,TRUE);
  493. }
  494. else
  495. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  496. break;
  497. case IDM_VDILATION:
  498. if(hImgData!=NULL){
  499. if(Dilation(hWnd,FALSE))
  500. InvalidateRect(hWnd,NULL,TRUE);
  501. }
  502. else
  503. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  504. break;
  505. case IDM_HEROSION:
  506. if(hImgData!=NULL){
  507. if(Erosion(hWnd,TRUE))
  508. InvalidateRect(hWnd,NULL,TRUE);
  509. }
  510. else
  511. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  512. break;
  513. case IDM_VEROSION:
  514. if(hImgData!=NULL){
  515. if(Erosion(hWnd,FALSE))
  516. InvalidateRect(hWnd,NULL,TRUE);
  517. }
  518. else
  519. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  520. break;
  521. case IDM_HOPEN:
  522. if(hImgData!=NULL){
  523. if(MorphOpen(hWnd,TRUE))
  524. InvalidateRect(hWnd,NULL,TRUE);
  525. }
  526. else
  527. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  528. break;
  529. case IDM_VOPEN:
  530. if(hImgData!=NULL){
  531. if(MorphOpen(hWnd,FALSE))
  532. InvalidateRect(hWnd,NULL,TRUE);
  533. }
  534. else
  535. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  536. break;
  537. case IDM_HCLOSE:
  538. if(hImgData!=NULL){
  539. if(MorphClose(hWnd,TRUE))
  540. InvalidateRect(hWnd,NULL,TRUE);
  541. }
  542. else
  543. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  544. break;
  545. case IDM_VCLOSE:
  546. if(hImgData!=NULL){
  547. if(MorphClose(hWnd,FALSE))
  548. InvalidateRect(hWnd,NULL,TRUE);
  549. }
  550. else
  551. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  552. break;
  553. case IDM_THINNING:
  554. if(hImgData!=NULL){
  555. if(Thinning(hWnd))
  556. InvalidateRect(hWnd,NULL,TRUE);
  557. }
  558. else
  559. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  560. break;
  561. case IDM_OUTLINE:
  562. if(hImgData!=NULL){
  563. if(Outline(hWnd))
  564. InvalidateRect(hWnd,NULL,TRUE);
  565. }
  566. else
  567. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  568. break;
  569. case IDM_HSOBEL:
  570. if(hImgData!=NULL){
  571. if(TemplateOperation(hWnd,TEMPLATE_SOBEL_H))
  572. InvalidateRect(hWnd,NULL,TRUE);
  573. }
  574. else
  575. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  576. break;
  577. case IDM_VSOBEL:
  578. if(hImgData!=NULL){
  579. if(TemplateOperation(hWnd,TEMPLATE_SOBEL_V))
  580. InvalidateRect(hWnd,NULL,TRUE);
  581. }
  582. else
  583. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  584. break;
  585. case IDM_HISOSOBEL:
  586. if(hImgData!=NULL){
  587. if(TemplateOperation(hWnd,TEMPLATE_ISOSOBEL_H))
  588. InvalidateRect(hWnd,NULL,TRUE);
  589. }
  590. else
  591. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  592. break;
  593. case IDM_VISOSOBEL:
  594. if(hImgData!=NULL){
  595. if(TemplateOperation(hWnd,TEMPLATE_ISOSOBEL_V))
  596. InvalidateRect(hWnd,NULL,TRUE);
  597. }
  598. else
  599. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  600. break;
  601. case IDM_HOUGH:
  602. if(hImgData!=NULL) 
  603. Hough(hWnd);
  604. else
  605. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  606. break;
  607. case IDM_HMEDIAN:
  608. if(hImgData!=NULL){
  609. if(MedianFilter(hWnd,TRUE))
  610. InvalidateRect(hWnd,NULL,TRUE);
  611. }
  612. else
  613. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  614. break;
  615. case IDM_VMEDIAN:
  616. if(hImgData!=NULL){
  617. if(MedianFilter(hWnd,FALSE))
  618. InvalidateRect(hWnd,NULL,TRUE);
  619. }
  620. else
  621. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  622. break;
  623. case IDM_LOG:
  624. if(hImgData!=NULL){
  625. if(LapOfGauss(hWnd))
  626. InvalidateRect(hWnd,NULL,TRUE);
  627. }
  628. else
  629. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  630. break;
  631. case IDM_CONTOUR:
  632. if(hImgData!=NULL){
  633. if(Contour(hWnd))
  634. InvalidateRect(hWnd,NULL,TRUE);
  635. }
  636. else
  637. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  638. break;
  639. case IDM_SEEDFILL:
  640. if(hImgData!=NULL){
  641. IsSelecting=TRUE;
  642. MessageBox(hWnd,"Please click the area to be filled!","Message",MB_OK|MB_ICONEXCLAMATION);
  643. }
  644. else
  645. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  646. break;
  647. case IDM_HPROJECTION:
  648. if(hImgData!=NULL){
  649. if(Projection(hWnd,TRUE))
  650. InvalidateRect(hWnd,NULL,TRUE);
  651. }
  652. else
  653. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  654. break;
  655. case IDM_VPROJECTION:
  656. if(hImgData!=NULL){
  657. if(Projection(hWnd,FALSE))
  658. InvalidateRect(hWnd,NULL,TRUE);
  659. }
  660. else
  661. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  662. break;
  663. case IDM_SUBTRACTION:
  664. if(hImgData!=NULL){
  665. MessageBox(hWnd,"Be sure that c:\test.bmp and c:\backgnd.bmp are both 256 grayscale bitmaps ! They must be same size and have same palette!","Important",MB_OK);
  666. if(Subtraction(hWnd))
  667. InvalidateRect(hWnd,NULL,TRUE);
  668. }
  669. else
  670. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  671. break;
  672. case IDM_RUNLENGTH:
  673. //注意重新分配内存和调色板,位图句柄时,先释放原来的
  674.         if(hBitmap!=NULL){     
  675. DeleteObject(hBitmap);
  676.             hBitmap=NULL;
  677.         }
  678.         if(hPalette!=NULL){                     
  679. DeleteObject(hPalette);
  680.             hPalette=NULL;
  681.         }
  682. if(hImgData!=NULL){
  683. GlobalUnlock(hImgData);
  684. GlobalFree(hImgData);  
  685. hImgData=NULL;
  686. }         
  687. if(LoadPcxFile(hWnd,"c:\test.pcx")) //成功,则重画窗口
  688.                 InvalidateRect(hWnd,NULL,TRUE);
  689.         break;
  690. case IDM_JPEG:
  691. //注意重新分配内存和调色板,位图句柄时,先释放原来的
  692.         if(hBitmap!=NULL){     
  693. DeleteObject(hBitmap);
  694.             hBitmap=NULL;
  695.         }
  696.         if(hPalette!=NULL){                     
  697. DeleteObject(hPalette);
  698.             hPalette=NULL;
  699.         }
  700. if(hImgData!=NULL){
  701. GlobalUnlock(hImgData);
  702. GlobalFree(hImgData);  
  703. hImgData=NULL;
  704. }         
  705. if(LoadJpegFile(hWnd,"c:\test.jpg")) //成功,则重画窗口
  706.                 InvalidateRect(hWnd,NULL,TRUE);
  707.         break;
  708. case IDM_EXIT:
  709.         SendMessage(hWnd,WM_DESTROY,0,0L);
  710.         break;
  711.     }
  712. break;                
  713.     }
  714.     return DefWindowProc (hWnd, message, wParam, lParam);
  715.  }
  716. ////////////////////////////////////////////////////////////////
  717. BOOL LoadBmpFile (HWND hWnd,char *BmpFileName)
  718. {   
  719.     HFILE              hf;
  720.     LPBITMAPINFOHEADER lpImgData;
  721.     LOGPALETTE         *pPal;
  722.     LPRGBQUAD          lpRGB;
  723.     HPALETTE           hPrevPalette; 
  724.     HDC                hDc;
  725. HLOCAL             hPal;
  726. DWORD         ImgSize;
  727. DWORD              i;
  728.     if((hf=_lopen(BmpFileName,OF_READ))==HFILE_ERROR){
  729.         MessageBox(hWnd,"File c:\test.bmp not found!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  730.         return FALSE;
  731. }
  732. _lread(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  733. _lread(hf,(LPSTR)&bi,sizeof(BITMAPINFOHEADER));
  734. ImgWidth=bi.biWidth;
  735. ImgHeight=bi.biHeight;
  736. LineBytes=(DWORD)WIDTHBYTES(bi.biWidth*bi.biBitCount);
  737. ImgSize=(DWORD)LineBytes*bi.biHeight;
  738.     if(bi.biClrUsed!=0)
  739. NumColors=(DWORD)bi.biClrUsed;
  740. else
  741.         switch(bi.biBitCount){
  742.         case 1:
  743.              NumColors=2;
  744.              break;
  745.          case 4:
  746.              NumColors=16;
  747.              break;
  748.          case 8:
  749.              NumColors=256;
  750.              break;
  751.          case 24:
  752.              NumColors=0;
  753.              break;
  754.               default:
  755.                   MessageBox(hWnd,"Invalid color numbers!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  756.                   _lclose(hf);
  757.                   return FALSE; 
  758.         }
  759. if(bf.bfOffBits!=(DWORD)(NumColors*sizeof(RGBQUAD)+sizeof(BITMAPFILEHEADER)
  760. +sizeof(BITMAPINFOHEADER)))
  761. {
  762.      MessageBox(hWnd,"Invalid color numbers!","Error Message" ,MB_OK|
  763.                MB_ICONEXCLAMATION);
  764. _lclose(hf);
  765. return FALSE; 
  766. }
  767. bf.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+ImgSize;
  768. if((hImgData=GlobalAlloc(GHND,(DWORD)(sizeof(BITMAPINFOHEADER)+
  769.      NumColors*sizeof(RGBQUAD)+ImgSize)))==NULL)
  770. {
  771.      MessageBox(hWnd,"Error alloc memory!","ErrorMessage",MB_OK|
  772.                    MB_ICONEXCLAMATION);
  773.     _lclose(hf);
  774. return FALSE;
  775. }
  776.   
  777. lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData); 
  778.     _llseek(hf,sizeof(BITMAPFILEHEADER),FILE_BEGIN);
  779. _hread(hf,(char *)lpImgData,(long)sizeof(BITMAPINFOHEADER)
  780.            +(long)NumColors*sizeof(RGBQUAD)+ImgSize);
  781. _lclose(hf);
  782.     if(NumColors!=0)
  783. {                    
  784.     hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
  785.     pPal =(LOGPALETTE *)LocalLock(hPal);
  786.     pPal->palNumEntries =(WORD) NumColors;
  787. pPal->palVersion    = 0x300;
  788.     lpRGB = (LPRGBQUAD)((LPSTR)lpImgData + (DWORD)sizeof(BITMAPINFOHEADER));
  789. for (i = 0; i < NumColors; i++) {
  790.       pPal->palPalEntry[i].peRed=lpRGB->rgbRed;
  791. pPal->palPalEntry[i].peGreen=lpRGB->rgbGreen;
  792. pPal->palPalEntry[i].peBlue=lpRGB->rgbBlue;
  793. pPal->palPalEntry[i].peFlags=(BYTE)0;
  794. lpRGB++;
  795. }
  796. hPalette=CreatePalette(pPal);
  797. LocalUnlock(hPal);
  798. LocalFree(hPal);
  799. }
  800. hDc=GetDC(hWnd);
  801. if(hPalette){
  802.         hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  803. RealizePalette(hDc);
  804. }
  805. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpImgData, (LONG)CBM_INIT,
  806. (LPSTR)lpImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  807.     (LPBITMAPINFO)lpImgData, DIB_RGB_COLORS);
  808. if(hPalette && hPrevPalette){
  809. SelectPalette(hDc,hPrevPalette,FALSE);
  810. RealizePalette(hDc);
  811. }
  812.  
  813. ReleaseDC(hWnd,hDc);
  814. GlobalUnlock(hImgData);
  815. return TRUE; 
  816. }
  817. ////////////////////////////////////////////////////////////////
  818. BOOL CALLBACK InputBox( HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam )
  819. char str[80];
  820.     switch  ( message ){
  821.     case WM_INITDIALOG:    
  822.       SetDlgItemText(hDlg,ID_XOFFSET,"0");
  823.       SetDlgItemText(hDlg,ID_YOFFSET,"0");
  824.       SetDlgItemText(hDlg,ID_ANGLE,"0");
  825.       SetDlgItemText(hDlg,ID_ZOOMRATIO,"0.25");
  826.       SetDlgItemText(hDlg,ID_FIRSTPOINT,"0");
  827.       SetDlgItemText(hDlg,ID_SECONDPOINT,"0");
  828.       SetDlgItemText(hDlg,ID_STRETCHRATIO,"2.0");
  829.       SetDlgItemText(hDlg,ID_EQUASCALE,"128");
  830.      return TRUE;
  831.     case WM_COMMAND:    
  832.      if ( wParam == IDOK || wParam == IDCANCEL )
  833.         {
  834.          GetDlgItemText(hDlg,ID_XOFFSET,str,80);
  835. xOffset=atoi(str);
  836.          GetDlgItemText(hDlg,ID_YOFFSET,str,80);
  837. yOffset=atoi(str);
  838.          GetDlgItemText(hDlg,ID_ANGLE,str,80);
  839. RotateAngle=(float)atof(str);
  840.          GetDlgItemText(hDlg,ID_ZOOMRATIO,str,80);
  841. ZoomRatio=(float)atof(str);
  842.          GetDlgItemText(hDlg,ID_FIRSTPOINT,str,80);
  843. FirstPoint=atoi(str);
  844.          GetDlgItemText(hDlg,ID_SECONDPOINT,str,80);
  845. SecondPoint=atoi(str);
  846.          GetDlgItemText(hDlg,ID_STRETCHRATIO,str,80);
  847. StretchRatio=(float)atof(str);
  848.          GetDlgItemText(hDlg,ID_EQUASCALE,str,80);
  849. EquaScale=atoi(str);
  850.             EndDialog ( hDlg, TRUE );
  851.             return  TRUE;
  852.         }
  853.         break;
  854.     }
  855. return FALSE;      
  856. }
  857. ////////////////////////////////////////////////////////////////
  858. BOOL Translation(HWND hWnd)
  859. {
  860. DLGPROC            dlgInputBox = NULL;
  861. DWORD              OffBits,BufSize;
  862.     LPBITMAPINFOHEADER lpImgData;
  863. LPSTR              lpPtr;
  864. HLOCAL             hTempImgData;
  865. LPBITMAPINFOHEADER lpTempImgData;
  866. LPSTR              lpTempPtr;
  867. int    SrcX0,SrcY0,SrcX1,SrcY1,DstX0,DstY0,DstX1,DstY1;
  868. int                RectWidth,RectHeight;
  869. BOOL               xVisible,yVisible;
  870. HDC                hDc;
  871. HFILE              hf;
  872. int                i;
  873. dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
  874. DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
  875. FreeProcInstance ( (FARPROC) dlgInputBox );
  876. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  877. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  878. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  879.     {
  880.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  881.         return FALSE;
  882.     }
  883.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  884. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  885. lpPtr=(char *)lpImgData;
  886. lpTempPtr=(char *)lpTempImgData;
  887. memset(lpTempPtr,(BYTE)255,BufSize);
  888. memcpy(lpTempPtr,lpPtr,OffBits);
  889. xVisible=TRUE;
  890. if( xOffset<= -bi.biWidth )
  891. xVisible=FALSE;
  892. else if( xOffset<=0){
  893. DstX0=0;
  894. DstX1=bi.biWidth+xOffset;
  895. }
  896. else if ( xOffset<bi.biWidth){
  897. DstX0=xOffset;
  898. DstX1=bi.biWidth;
  899. }
  900. else
  901. xVisible=FALSE;
  902. SrcX0=DstX0-xOffset;
  903. SrcX1=DstX1-xOffset;
  904. RectWidth=DstX1-DstX0;
  905. yVisible=TRUE;
  906. if( yOffset<= -bi.biHeight )
  907. yVisible=FALSE;
  908. else if( yOffset<=0){
  909. DstY0=0;
  910. DstY1=bi.biHeight+yOffset;
  911. }
  912. else if ( yOffset<bi.biHeight){
  913. DstY0=yOffset;
  914. DstY1=bi.biHeight;
  915. }
  916. else
  917. yVisible=FALSE;
  918. SrcY0=DstY0-yOffset;
  919. SrcY1=DstY1-yOffset;
  920. RectHeight=DstY1-DstY0;
  921. if( xVisible && yVisible){
  922. for(i=0;i<RectHeight;i++){
  923. lpPtr=(char *)lpImgData+(BufSize-LineBytes-(i+SrcY0)*LineBytes)+SrcX0;
  924. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-(i+DstY0)*LineBytes)+DstX0;
  925. memcpy(lpTempPtr,lpPtr,RectWidth);
  926. }
  927. }
  928. hDc=GetDC(hWnd);
  929.     if(hBitmap!=NULL)
  930.     DeleteObject(hBitmap);
  931. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  932. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  933.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  934.     hf=_lcreat("c:\translation.bmp",0);
  935. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  936. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  937. _lclose(hf);
  938.   ReleaseDC(hWnd,hDc);
  939. LocalUnlock(hTempImgData);
  940. LocalFree(hTempImgData);
  941. GlobalUnlock(hImgData);
  942. return TRUE;
  943. }
  944. ////////////////////////////////////////////////////////////////
  945. BOOL Rotation(HWND hWnd)
  946. {
  947. DLGPROC            dlgInputBox = NULL;
  948. DWORD              OffBits,SrcBufSize,DstBufSize,DstLineBytes;
  949.     LPBITMAPINFOHEADER lpImgData;
  950. LPSTR              lpPtr;
  951. HLOCAL             hTempImgData;
  952. LPBITMAPINFOHEADER lpTempImgData;
  953. LPSTR              lpTempPtr;
  954. float    SrcX1,SrcY1,SrcX2,SrcY2,SrcX3,SrcY3,SrcX4,SrcY4;
  955. float    DstX1,DstY1,DstX2,DstY2,DstX3,DstY3,DstX4,DstY4;
  956. DWORD              Wold,Hold,Wnew,Hnew;
  957. HDC                hDc;
  958. HFILE              hf;
  959. DWORD              x0,y0,x1,y1;
  960. float              cosa,sina; //cos(a),sin(a)
  961. float    num1,num2;
  962. BITMAPFILEHEADER   DstBf;
  963. BITMAPINFOHEADER   DstBi;
  964. dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
  965. DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
  966. FreeProcInstance ( (FARPROC) dlgInputBox );
  967. RotateAngle=(float)RADIAN(RotateAngle);
  968. cosa=(float)cos((double)RotateAngle);
  969. sina=(float)sin((double)RotateAngle);
  970. Wold=bi.biWidth;
  971. Hold=bi.biHeight;
  972. SrcX1=(float)(-0.5*Wold);
  973. SrcY1=(float)(0.5*Hold);
  974. SrcX2=(float)(0.5*Wold);
  975. SrcY2=(float)(0.5*Hold);
  976. SrcX3=(float)(-0.5*Wold);
  977. SrcY3=(float)(-0.5*Hold);
  978. SrcX4=(float)(0.5*Wold);
  979. SrcY4=(float)(-0.5*Hold);
  980. DstX1=cosa*SrcX1+sina*SrcY1;
  981. DstY1=-sina*SrcX1+cosa*SrcY1;
  982. DstX2=cosa*SrcX2+sina*SrcY2;
  983. DstY2=-sina*SrcX2+cosa*SrcY2;
  984. DstX3=cosa*SrcX3+sina*SrcY3;
  985. DstY3=-sina*SrcX3+cosa*SrcY3;
  986. DstX4=cosa*SrcX4+sina*SrcY4;
  987. DstY4=-sina*SrcX4+cosa*SrcY4;
  988. Wnew = (DWORD)(max(fabs(DstX4-DstX1), fabs(DstX3-DstX2))+0.5);
  989.     Hnew = (DWORD)(max(fabs(DstY4-DstY1), fabs(DstY3-DstY2))+0.5);
  990. num1=(float)( -0.5*Wnew*cosa-0.5*Hnew*sina+0.5*Wold);
  991. num2=(float)(0.5*Wnew*sina-0.5*Hnew*cosa+0.5*Hold);
  992. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  993. SrcBufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  994. ImgWidth=Wnew;
  995. ImgHeight=Hnew;
  996. DstLineBytes=(DWORD)WIDTHBYTES(Wnew*bi.biBitCount);
  997. DstBufSize=(DWORD)(sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+(DWORD)DstLineBytes*Hnew);
  998. if((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
  999.     {
  1000.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1001.         return FALSE;
  1002.     }
  1003.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  1004. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  1005. lpPtr=(char *)lpImgData;
  1006. lpTempPtr=(char *)lpTempImgData;
  1007. memset(lpTempPtr,(BYTE)255,DstBufSize);
  1008. memcpy(lpTempPtr,lpPtr,OffBits);
  1009. //get new bitmapfileheader and bitmapinfoheader
  1010. memcpy((char *)&DstBf,(char *)&bf,sizeof(BITMAPFILEHEADER));
  1011. memcpy((char *)&DstBi,(char *)&bi,sizeof(BITMAPINFOHEADER));
  1012. DstBf.bfSize=DstBufSize+sizeof(BITMAPFILEHEADER);
  1013. DstBi.biWidth=Wnew;
  1014. DstBi.biHeight=Hnew;
  1015. //overwrite bitmapinfoheader with the new one
  1016. memcpy(lpTempPtr,(char *)&DstBi,sizeof(BITMAPINFOHEADER));
  1017. for(y1=0;y1<Hnew;y1++)
  1018. for(x1=0;x1<Wnew;x1++){
  1019. x0= (DWORD)(x1*cosa+y1*sina+num1);
  1020. y0= (DWORD)(-1.0f*x1*sina+y1*cosa+num2);
  1021. if( (x0>=0) && (x0<Wold) && (y0>=0) && (y0<Hold))
  1022. {
  1023. lpPtr=(char *)lpImgData+(SrcBufSize-LineBytes-y0*LineBytes)+x0;
  1024. lpTempPtr=(char *)lpTempImgData+(DstBufSize-DstLineBytes-y1*DstLineBytes)+x1;
  1025. *lpTempPtr=*lpPtr;
  1026. }
  1027. }
  1028. hDc=GetDC(hWnd);
  1029.     if(hBitmap!=NULL)
  1030.     DeleteObject(hBitmap);
  1031. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  1032. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  1033.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  1034.     hf=_lcreat("c:\rotation.bmp",0);
  1035. _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER)); 
  1036. _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
  1037. _lclose(hf);
  1038.   ReleaseDC(hWnd,hDc);
  1039. LocalUnlock(hTempImgData);
  1040. LocalFree(hTempImgData);
  1041. GlobalUnlock(hImgData);
  1042. return TRUE;
  1043. }
  1044. ////////////////////////////////////////////////////////////////
  1045. BOOL Mirror(HWND hWnd,BOOL XDirection)
  1046. {
  1047. DWORD              OffBits,BufSize;
  1048.     LPBITMAPINFOHEADER lpImgData;
  1049. LPSTR              lpPtr;
  1050. HLOCAL             hTempImgData;
  1051. LPBITMAPINFOHEADER lpTempImgData;
  1052. LPSTR              lpTempPtr;
  1053. HDC                hDc;
  1054. HFILE              hf;
  1055. LONG               x0,y0,x1,y1;
  1056. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  1057. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  1058. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  1059.     {
  1060.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1061.         return FALSE;
  1062.     }
  1063.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  1064. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  1065. lpPtr=(char *)lpImgData;
  1066. lpTempPtr=(char *)lpTempImgData;
  1067. memset(lpTempPtr,(BYTE)255,BufSize);
  1068. memcpy(lpTempPtr,lpPtr,OffBits);
  1069. if( XDirection){
  1070. for(y1=0;y1<bi.biHeight;y1++)
  1071. for(x1=0;x1<bi.biWidth;x1++){
  1072. x0=bi.biWidth-1-x1;
  1073. y0=y1;
  1074. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y0*LineBytes)+x0;
  1075. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y1*LineBytes)+x1;
  1076. *lpTempPtr=*lpPtr;
  1077. }
  1078. }
  1079. else{
  1080. for(y1=0;y1<bi.biHeight;y1++)
  1081. for(x1=0;x1<bi.biWidth;x1++){
  1082. x0=x1;
  1083. y0=bi.biHeight-1-y1;
  1084. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y0*LineBytes)+x0;
  1085. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y1*LineBytes)+x1;
  1086. *lpTempPtr=*lpPtr;
  1087. }
  1088. }
  1089. hDc=GetDC(hWnd);
  1090.     if(hBitmap!=NULL)
  1091.     DeleteObject(hBitmap);
  1092. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  1093. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  1094.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  1095. if( XDirection)
  1096. hf=_lcreat("c:\mirrorx.bmp",0);
  1097. else
  1098. hf=_lcreat("c:\mirrory.bmp",0);
  1099. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  1100. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  1101. _lclose(hf);
  1102.   ReleaseDC(hWnd,hDc);
  1103. LocalUnlock(hTempImgData);
  1104. LocalFree(hTempImgData);
  1105. GlobalUnlock(hImgData);
  1106. return TRUE;
  1107. }
  1108. ////////////////////////////////////////////////////////////////
  1109. BOOL Transpose(HWND hWnd)
  1110. {
  1111. DWORD              OffBits,SrcBufSize,DstBufSize,DstLineBytes;
  1112.     LPBITMAPINFOHEADER lpImgData;
  1113. LPSTR              lpPtr;
  1114. HLOCAL             hTempImgData;
  1115. LPBITMAPINFOHEADER lpTempImgData;
  1116. LPSTR              lpTempPtr;
  1117. DWORD              Wnew,Hnew;
  1118. HDC                hDc;
  1119. HFILE              hf;
  1120. DWORD              x0,y0,x1,y1;
  1121. BITMAPFILEHEADER   DstBf;
  1122. BITMAPINFOHEADER   DstBi;
  1123. Wnew = (DWORD)bi.biHeight;
  1124.     Hnew = (DWORD)bi.biWidth;
  1125. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  1126. SrcBufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  1127. ImgWidth=Wnew;
  1128. ImgHeight=Hnew;
  1129. DstLineBytes=(DWORD)WIDTHBYTES(Wnew*bi.biBitCount);
  1130. DstBufSize=(DWORD)(sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+(DWORD)DstLineBytes*Hnew);
  1131. if((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
  1132.     {
  1133.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1134.         return FALSE;
  1135.     }
  1136.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  1137. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  1138. lpPtr=(char *)lpImgData;
  1139. lpTempPtr=(char *)lpTempImgData;
  1140. memset(lpTempPtr,(BYTE)255,DstBufSize);
  1141. memcpy(lpTempPtr,lpPtr,OffBits);
  1142. //get new bitmapfileheader and bitmapinfoheader
  1143. memcpy((char *)&DstBf,(char *)&bf,sizeof(BITMAPFILEHEADER));
  1144. memcpy((char *)&DstBi,(char *)&bi,sizeof(BITMAPINFOHEADER));
  1145. DstBf.bfSize=DstBufSize+sizeof(BITMAPFILEHEADER);
  1146. DstBi.biWidth=Wnew;
  1147. DstBi.biHeight=Hnew;
  1148. //overwrite bitmapinfoheader with the new one
  1149. memcpy(lpTempPtr,(char *)&DstBi,sizeof(BITMAPINFOHEADER));
  1150. for(y1=0;y1<Hnew;y1++)
  1151. for(x1=0;x1<Wnew;x1++){
  1152. x0= y1;
  1153. y0= x1;
  1154. lpPtr=(char *)lpImgData+(SrcBufSize-LineBytes-y0*LineBytes)+x0;
  1155. lpTempPtr=(char *)lpTempImgData+(DstBufSize-DstLineBytes-y1*DstLineBytes)+x1;
  1156. *lpTempPtr=*lpPtr;
  1157. }
  1158. hDc=GetDC(hWnd);
  1159.     if(hBitmap!=NULL)
  1160.     DeleteObject(hBitmap);
  1161. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  1162. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  1163.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  1164.     hf=_lcreat("c:\transpose.bmp",0);
  1165. _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER)); 
  1166. _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
  1167. _lclose(hf);
  1168.   ReleaseDC(hWnd,hDc);
  1169. LocalUnlock(hTempImgData);
  1170. LocalFree(hTempImgData);
  1171. GlobalUnlock(hImgData);
  1172. return TRUE;
  1173. }
  1174. ////////////////////////////////////////////////////////////////
  1175. BOOL Zoom(HWND hWnd)
  1176. {
  1177. DLGPROC            dlgInputBox = NULL;
  1178. DWORD              OffBits,SrcBufSize,DstBufSize,DstLineBytes;
  1179.     LPBITMAPINFOHEADER lpImgData;
  1180. LPSTR              lpPtr;
  1181. HLOCAL             hTempImgData;
  1182. LPBITMAPINFOHEADER lpTempImgData;
  1183. LPSTR              lpTempPtr;
  1184. DWORD              Wold,Hold,Wnew,Hnew;
  1185. HDC                hDc;
  1186. HFILE              hf;
  1187. DWORD              x0,y0,x1,y1;
  1188. float    num1;
  1189. BITMAPFILEHEADER   DstBf;
  1190. BITMAPINFOHEADER   DstBi;
  1191. dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
  1192. DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
  1193. FreeProcInstance ( (FARPROC) dlgInputBox );
  1194. num1=(float)(1.0/ZoomRatio);
  1195. Wold=bi.biWidth;
  1196. Hold=bi.biHeight;
  1197. Wnew = (DWORD)(Wold*ZoomRatio+0.5);
  1198.     Hnew = (DWORD)(Hold*ZoomRatio+0.5);
  1199. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  1200. SrcBufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  1201. ImgWidth=Wnew;
  1202. ImgHeight=Hnew;
  1203. DstLineBytes=(DWORD)WIDTHBYTES(Wnew*bi.biBitCount);
  1204. DstBufSize=(DWORD)(sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+(DWORD)DstLineBytes*Hnew);
  1205. if((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
  1206.     {
  1207.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1208.         return FALSE;
  1209.     }
  1210.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  1211. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  1212. lpPtr=(char *)lpImgData;
  1213. lpTempPtr=(char *)lpTempImgData;
  1214. memset(lpTempPtr,(BYTE)255,DstBufSize);
  1215. memcpy(lpTempPtr,lpPtr,OffBits);
  1216. //get new bitmapfileheader and bitmapinfoheader
  1217. memcpy((char *)&DstBf,(char *)&bf,sizeof(BITMAPFILEHEADER));
  1218. memcpy((char *)&DstBi,(char *)&bi,sizeof(BITMAPINFOHEADER));
  1219. DstBf.bfSize=DstBufSize+sizeof(BITMAPFILEHEADER);
  1220. DstBi.biWidth=Wnew;
  1221. DstBi.biHeight=Hnew;
  1222. //overwrite bitmapinfoheader with the new one
  1223. memcpy(lpTempPtr,(char *)&DstBi,sizeof(BITMAPINFOHEADER));
  1224. for(y1=0;y1<Hnew;y1++)
  1225. for(x1=0;x1<Wnew;x1++){
  1226. x0= (DWORD)(x1*num1);
  1227. y0= (DWORD)(y1*num1);
  1228. if( (x0>=0) && (x0<Wold) && (y0>=0) && (y0<Hold))
  1229. {
  1230. lpPtr=(char *)lpImgData+(SrcBufSize-LineBytes-y0*LineBytes)+x0;
  1231. lpTempPtr=(char *)lpTempImgData+(DstBufSize-DstLineBytes-y1*DstLineBytes)+x1;
  1232. *lpTempPtr=*lpPtr;
  1233. }
  1234. }
  1235. hDc=GetDC(hWnd);
  1236.     if(hBitmap!=NULL)
  1237.     DeleteObject(hBitmap);
  1238. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  1239. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  1240.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  1241.     hf=_lcreat("c:\zoom.bmp",0);
  1242. _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER)); 
  1243. _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
  1244. _lclose(hf);
  1245.   ReleaseDC(hWnd,hDc);
  1246. LocalUnlock(hTempImgData);
  1247. LocalFree(hTempImgData);
  1248. GlobalUnlock(hImgData);
  1249. return TRUE;
  1250. }
  1251. ////////////////////////////////////////////////////////////////
  1252. BOOL TemplateOperation(HWND hWnd, int TemplateType)
  1253. {
  1254. DWORD              OffBits,BufSize;
  1255.     LPBITMAPINFOHEADER lpImgData;
  1256. LPSTR              lpPtr;
  1257. HLOCAL             hTempImgData;
  1258. LPBITMAPINFOHEADER lpTempImgData;
  1259. LPSTR              lpTempPtr;
  1260. HDC                hDc;
  1261. HFILE              hf;
  1262. LONG               x,y;
  1263. float              coef;
  1264. float              CoefArray[9];
  1265. float              TempNum;
  1266. char               filename[80];
  1267. switch(TemplateType){
  1268. case TEMPLATE_SMOOTH_BOX:
  1269. coef=(float)(1.0/9.0);
  1270. memcpy(CoefArray,Template_Smooth_Box,9*sizeof(float));
  1271. strcpy(filename,"c:\smbox.bmp");
  1272. break;
  1273. case TEMPLATE_SMOOTH_GAUSS:
  1274. coef=(float)(1.0/16.0);
  1275. memcpy(CoefArray,Template_Smooth_Gauss,9*sizeof(float));
  1276. strcpy(filename,"c:\smgauss.bmp");
  1277. break;
  1278. case TEMPLATE_SHARPEN_LAPLACIAN:
  1279. coef=(float)1.0;
  1280. memcpy(CoefArray,Template_Sharpen_Laplacian,9*sizeof(float));
  1281. strcpy(filename,"c:\shlaplac.bmp");
  1282. break;
  1283. case TEMPLATE_SOBEL_H:
  1284. coef=(float)1.0;
  1285. memcpy(CoefArray,Template_HSobel,9*sizeof(float));
  1286. strcpy(filename,"c:\hsobel.bmp");
  1287. break;
  1288. case TEMPLATE_SOBEL_V:
  1289. coef=(float)1.0;
  1290. memcpy(CoefArray,Template_VSobel,9*sizeof(float));
  1291. strcpy(filename,"c:\vsobel.bmp");
  1292. break;
  1293. case TEMPLATE_ISOSOBEL_H:
  1294. coef=(float)1.0;
  1295. memcpy(CoefArray,Template_HIsoSobel,9*sizeof(float));
  1296. strcpy(filename,"c:\hisobel.bmp");
  1297. break;
  1298. case TEMPLATE_ISOSOBEL_V:
  1299. coef=(float)1.0;
  1300. memcpy(CoefArray,Template_VIsoSobel,9*sizeof(float));
  1301. strcpy(filename,"c:\visobel.bmp");
  1302. break;
  1303. }
  1304. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  1305. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  1306. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  1307.     {
  1308.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1309.         return FALSE;
  1310.     }
  1311.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  1312. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  1313. lpPtr=(char *)lpImgData;
  1314. lpTempPtr=(char *)lpTempImgData;
  1315. memcpy(lpTempPtr,lpPtr,BufSize);
  1316. for(y=1;y<bi.biHeight-1;y++)
  1317. for(x=1;x<bi.biWidth-1;x++){
  1318. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes)+x;
  1319. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes)+x;
  1320. TempNum=(float)((unsigned char)*(lpPtr+LineBytes-1))*CoefArray[0];
  1321. TempNum+=(float)((unsigned char)*(lpPtr+LineBytes))*CoefArray[1];
  1322. TempNum+=(float)((unsigned char)*(lpPtr+LineBytes+1))*CoefArray[2];
  1323. TempNum+=(float)((unsigned char)*(lpPtr-1))*CoefArray[3];
  1324. TempNum+=(float)((unsigned char)*lpPtr)*CoefArray[4];
  1325. TempNum+=(float)((unsigned char)*(lpPtr+1))*CoefArray[5];
  1326. TempNum+=(float)((unsigned char)*(lpPtr-LineBytes-1))*CoefArray[6];
  1327. TempNum+=(float)((unsigned char)*(lpPtr-LineBytes))*CoefArray[7];
  1328. TempNum+=(float)((unsigned char)*(lpPtr-LineBytes+1))*CoefArray[8];
  1329. TempNum*=coef;
  1330. if(TempNum>255.0) *lpTempPtr=(BYTE)255;
  1331. else if(TempNum<0.0) 
  1332. *lpTempPtr=(unsigned char)fabs(TempNum);
  1333. else *lpTempPtr=(BYTE)TempNum;
  1334. }
  1335. hDc=GetDC(hWnd);
  1336.     if(hBitmap!=NULL)
  1337.     DeleteObject(hBitmap);
  1338. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  1339. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  1340.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  1341. hf=_lcreat(filename,0);
  1342. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  1343. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  1344. _lclose(hf);
  1345.   ReleaseDC(hWnd,hDc);
  1346. LocalUnlock(hTempImgData);
  1347. LocalFree(hTempImgData);
  1348. GlobalUnlock(hImgData);
  1349. return TRUE;
  1350. }
  1351. ////////////////////////////////////////////////////////////////
  1352. BOOL ColortoGrayScale(HWND hWnd)
  1353. {
  1354. DWORD              SrcBufSize,DstBufSize,DstLineBytes;
  1355.     LPBITMAPINFOHEADER lpImgData;
  1356. LPSTR              lpPtr;
  1357. HLOCAL             hTempImgData;
  1358. LPBITMAPINFOHEADER lpTempImgData;
  1359. LPSTR              lpTempPtr;
  1360. HDC                hDc;
  1361. HFILE              hf;
  1362. LONG               x,y;
  1363. BITMAPFILEHEADER   DstBf;
  1364. BITMAPINFOHEADER   DstBi;
  1365.     LOGPALETTE         *pPal;
  1366.     HPALETTE           hPrevPalette; 
  1367. HLOCAL             hPal;
  1368. DWORD    NewNumColors;
  1369. WORD    NewBitCount;
  1370. float              Y;
  1371. DWORD              i;
  1372. unsigned char      Red,Green,Blue,Gray;
  1373. NewNumColors=NumColors;
  1374. NewBitCount=bi.biBitCount;
  1375. if(NumColors==0) //true color
  1376. {
  1377. NewNumColors=256;
  1378. NewBitCount=8;
  1379. }
  1380. DstLineBytes=(DWORD)WIDTHBYTES(bi.biWidth*NewBitCount);
  1381. DstBufSize=(DWORD)(sizeof(BITMAPINFOHEADER)+NewNumColors*sizeof(RGBQUAD)+(DWORD)DstLineBytes*bi.biHeight);
  1382. memcpy((char *)&DstBf,(char *)&bf,sizeof(BITMAPFILEHEADER));
  1383. memcpy((char *)&DstBi,(char *)&bi,sizeof(BITMAPINFOHEADER));
  1384. DstBf.bfSize=DstBufSize+sizeof(BITMAPFILEHEADER);
  1385. DstBf.bfOffBits=(DWORD)(NewNumColors*sizeof(RGBQUAD)+sizeof(BITMAPFILEHEADER)
  1386. +sizeof(BITMAPINFOHEADER));
  1387. DstBi.biClrUsed=0;
  1388. DstBi.biBitCount=NewBitCount;
  1389. SrcBufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  1390. if((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
  1391.     {
  1392.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1393.         return FALSE;
  1394.     }
  1395.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  1396. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  1397. //copy image data
  1398. memcpy(lpTempImgData,lpImgData,DstBufSize);
  1399. //overwrite bitmapinfoheader with the new one
  1400. memcpy(lpTempImgData,(char *)&DstBi,sizeof(BITMAPINFOHEADER));
  1401. lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
  1402. lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
  1403.     hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NewNumColors* sizeof(PALETTEENTRY));
  1404.     pPal =(LOGPALETTE *)LocalLock(hPal);
  1405.     pPal->palNumEntries =(WORD) NewNumColors;
  1406. pPal->palVersion    = 0x300;
  1407. if(NumColors==0) //true color
  1408. for (i = 0; i < 256; i++) {
  1409.       pPal->palPalEntry[i].peRed=(BYTE)i;
  1410. pPal->palPalEntry[i].peGreen=(BYTE)i;
  1411. pPal->palPalEntry[i].peBlue=(BYTE)i;
  1412. pPal->palPalEntry[i].peFlags=(BYTE)0;
  1413. *(lpTempPtr++)=(unsigned char)i;
  1414. *(lpTempPtr++)=(unsigned char)i;
  1415. *(lpTempPtr++)=(unsigned char)i;
  1416. *(lpTempPtr++)=0;
  1417. }
  1418. else 
  1419. for (i = 0; i < NewNumColors; i++) {
  1420. Blue=(unsigned char )(*lpPtr++);
  1421. Green=(unsigned char )(*lpPtr++);
  1422. Red=(unsigned char )(*lpPtr++);
  1423. Y=(float)(Red*0.299+Green*0.587+Blue*0.114);
  1424. Gray=(BYTE)Y;
  1425. lpPtr++;
  1426.       pPal->palPalEntry[i].peRed=Gray;
  1427. pPal->palPalEntry[i].peGreen=Gray;
  1428. pPal->palPalEntry[i].peBlue=Gray;
  1429. pPal->palPalEntry[i].peFlags=0;
  1430. *(lpTempPtr++)=(unsigned char)Gray;
  1431. *(lpTempPtr++)=(unsigned char)Gray;
  1432. *(lpTempPtr++)=(unsigned char)Gray;
  1433. *(lpTempPtr++)=0;
  1434. }
  1435. if(hPalette!=NULL)                     
  1436.         DeleteObject(hPalette);
  1437. hPalette=CreatePalette(pPal);
  1438. LocalUnlock(hPal);
  1439. LocalFree(hPal);
  1440. hDc=GetDC(hWnd);
  1441. if(hPalette){
  1442.         hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  1443. RealizePalette(hDc);
  1444. }
  1445. if(NumColors==0)
  1446. for(y=0;y<bi.biHeight;y++){
  1447. lpPtr=(char *)lpImgData+(SrcBufSize-LineBytes-y*LineBytes);
  1448. lpTempPtr=(char *)lpTempImgData+(DstBufSize-DstLineBytes-y*DstLineBytes);
  1449. for(x=0;x<bi.biWidth;x++){
  1450. Blue=(unsigned char )(*lpPtr++);
  1451. Green=(unsigned char )(*lpPtr++);
  1452. Red=(unsigned char )(*lpPtr++);
  1453. Y=(float)(Red*0.299+Green*0.587+Blue*0.114);
  1454. Gray=(BYTE)Y;
  1455. *(lpTempPtr++)=(unsigned char)Gray;
  1456. }
  1457. }
  1458.     if(hBitmap!=NULL)
  1459.     DeleteObject(hBitmap);
  1460. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  1461. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NewNumColors*sizeof(RGBQUAD),
  1462.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  1463. if(hPalette && hPrevPalette){
  1464. SelectPalette(hDc,hPrevPalette,FALSE);
  1465. RealizePalette(hDc);
  1466. }
  1467.     hf=_lcreat("c:\gray.bmp",0);
  1468. _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER)); 
  1469. _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
  1470. _lclose(hf);
  1471.   ReleaseDC(hWnd,hDc);
  1472. LocalUnlock(hTempImgData);
  1473. LocalFree(hTempImgData);
  1474. GlobalUnlock(hImgData);
  1475. return TRUE;
  1476. }
  1477. ////////////////////////////////////////////////////////////////
  1478. BOOL Invert(HWND hWnd)
  1479. {
  1480. DWORD              BufSize;
  1481.     LPBITMAPINFOHEADER lpImgData;
  1482. LPSTR              lpPtr;
  1483. HLOCAL             hTempImgData;
  1484. LPBITMAPINFOHEADER lpTempImgData;
  1485. LPSTR              lpTempPtr;
  1486. HDC                hDc;
  1487. HFILE              hf;
  1488. LONG               x,y;
  1489.     LOGPALETTE         *pPal;
  1490.     HPALETTE           hPrevPalette=NULL; 
  1491. HLOCAL             hPal;
  1492. DWORD              i;
  1493. unsigned char      Red,Green,Blue;
  1494. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  1495. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  1496.     {
  1497.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1498.         return FALSE;
  1499.     }
  1500.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  1501. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  1502. //copy image data
  1503. memcpy(lpTempImgData,lpImgData,BufSize);
  1504. hDc=GetDC(hWnd);
  1505. if(NumColors!=0){
  1506. lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
  1507. lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
  1508.     hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
  1509. pPal =(LOGPALETTE *)LocalLock(hPal);
  1510. pPal->palNumEntries =(WORD) NumColors;
  1511. pPal->palVersion    = 0x300;
  1512. for (i = 0; i < NumColors; i++) {
  1513. Blue=(unsigned char )(*lpPtr++);
  1514. Green=(unsigned char )(*lpPtr++);
  1515. Red=(unsigned char )(*lpPtr++);
  1516. lpPtr++;
  1517.       pPal->palPalEntry[i].peRed=(BYTE)(255-Red);
  1518. pPal->palPalEntry[i].peGreen=(BYTE)(255-Green);
  1519. pPal->palPalEntry[i].peBlue=(BYTE)(255-Blue);
  1520. pPal->palPalEntry[i].peFlags=0;
  1521. *(lpTempPtr++)=(unsigned char)(255-Blue);
  1522. *(lpTempPtr++)=(unsigned char)(255-Green);
  1523. *(lpTempPtr++)=(unsigned char)(255-Red);
  1524. *(lpTempPtr++)=0;
  1525. }
  1526. if(hPalette!=NULL)                     
  1527. DeleteObject(hPalette);
  1528. hPalette=CreatePalette(pPal);
  1529. LocalUnlock(hPal);
  1530. LocalFree(hPal);
  1531. if(hPalette){
  1532. hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  1533. RealizePalette(hDc);
  1534. }
  1535. }
  1536. else{
  1537. for(y=0;y<bi.biHeight;y++){
  1538. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
  1539. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
  1540. for(x=0;x<bi.biWidth;x++){
  1541. Blue=(unsigned char )(*lpPtr++);
  1542. Green=(unsigned char )(*lpPtr++);
  1543. Red=(unsigned char )(*lpPtr++);
  1544. *(lpTempPtr++)=(unsigned char)(255-Blue);
  1545. *(lpTempPtr++)=(unsigned char)(255-Green);
  1546. *(lpTempPtr++)=(unsigned char)(255-Red);
  1547. }
  1548. }
  1549. }
  1550.     if(hBitmap!=NULL)
  1551.     DeleteObject(hBitmap);
  1552. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  1553. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  1554.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  1555. if(hPalette && hPrevPalette){
  1556. SelectPalette(hDc,hPrevPalette,FALSE);
  1557. RealizePalette(hDc);
  1558. }
  1559.     hf=_lcreat("c:\invert.bmp",0);
  1560. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  1561. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  1562. _lclose(hf);
  1563.   ReleaseDC(hWnd,hDc);
  1564. LocalUnlock(hTempImgData);
  1565. LocalFree(hTempImgData);
  1566. GlobalUnlock(hImgData);
  1567. return TRUE;
  1568. }
  1569. ////////////////////////////////////////////////////////////////
  1570. BOOL LimbPatternM3(HWND hWnd)
  1571. {
  1572. DWORD              BufSize;
  1573.     LPBITMAPINFOHEADER lpImgData;
  1574. LPSTR              lpPtr;
  1575. HLOCAL             hTempImgData;
  1576. LPBITMAPINFOHEADER lpTempImgData;
  1577. LPSTR              lpTempPtr;
  1578. HDC                hDc;
  1579. HFILE              hf;
  1580. LONG               x,y;
  1581. unsigned char      num;
  1582. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  1583. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  1584.     {
  1585.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1586.         return FALSE;
  1587.     }
  1588.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  1589. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  1590. //copy image data
  1591. memcpy(lpTempImgData,lpImgData,BufSize);
  1592. for(y=0;y<bi.biHeight;y++){
  1593. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
  1594. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
  1595. for(x=0;x<bi.biWidth;x++){
  1596. num=(unsigned char)*lpPtr++;
  1597. if ( (num>>2) > BayerPattern[y&7][x&7])
  1598. *(lpTempPtr++)=(unsigned char)255;
  1599. else *(lpTempPtr++)=(unsigned char)0;
  1600. }
  1601. }
  1602.     if(hBitmap!=NULL)
  1603.     DeleteObject(hBitmap);
  1604. hDc=GetDC(hWnd);
  1605. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  1606. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  1607.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  1608.     hf=_lcreat("c:\limbm3.bmp",0);
  1609. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  1610. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  1611. _lclose(hf);
  1612.   ReleaseDC(hWnd,hDc);
  1613. LocalUnlock(hTempImgData);
  1614. LocalFree(hTempImgData);
  1615. GlobalUnlock(hImgData);
  1616. return TRUE;
  1617. }
  1618. ////////////////////////////////////////////////////////////////
  1619. BOOL LimbPatternM4(HWND hWnd)
  1620. {
  1621. DWORD              BufSize;
  1622.     LPBITMAPINFOHEADER lpImgData;
  1623. LPSTR              lpPtr;
  1624. HLOCAL             hTempImgData;
  1625. LPBITMAPINFOHEADER lpTempImgData;
  1626. LPSTR              lpTempPtr;
  1627. HDC                hDc;
  1628. HFILE              hf;
  1629. LONG               x,y;
  1630. DWORD              i,j;
  1631. unsigned char      num;
  1632. BYTE    M4Pattern[16][16];
  1633. for(i=0;i<8;i++)
  1634. for(j=0;j<8;j++)
  1635. M4Pattern[i][j]=4*BayerPattern[i][j];
  1636. for(i=8;i<16;i++)
  1637. for(j=0;j<8;j++)
  1638. M4Pattern[i][j]=4*BayerPattern[i-8][j]+2;
  1639. for(i=0;i<8;i++)
  1640. for(j=8;j<16;j++)
  1641. M4Pattern[i][j]=4*BayerPattern[i][j-8]+3;
  1642. for(i=8;i<16;i++)
  1643. for(j=8;j<16;j++)
  1644. M4Pattern[i][j]=4*BayerPattern[i-8][j-8]+1;
  1645. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  1646. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  1647.     {
  1648.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1649.         return FALSE;
  1650.     }
  1651.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  1652. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  1653. //copy image data
  1654. memcpy(lpTempImgData,lpImgData,BufSize);
  1655. for(y=0;y<bi.biHeight;y++){
  1656. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
  1657. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
  1658. for(x=0;x<bi.biWidth;x++){
  1659. num=(unsigned char)*lpPtr++;
  1660. if ( num > M4Pattern[y&15][x&15])
  1661. *(lpTempPtr++)=(unsigned char)255;
  1662. else *(lpTempPtr++)=(unsigned char)0;
  1663. }
  1664. }
  1665.     if(hBitmap!=NULL)
  1666.     DeleteObject(hBitmap);
  1667. hDc=GetDC(hWnd);
  1668. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  1669. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  1670.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  1671.     hf=_lcreat("c:\limbm4.bmp",0);
  1672. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  1673. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  1674. _lclose(hf);
  1675.   ReleaseDC(hWnd,hDc);
  1676. LocalUnlock(hTempImgData);
  1677. LocalFree(hTempImgData);
  1678. GlobalUnlock(hImgData);
  1679. return TRUE;
  1680. }
  1681. ////////////////////////////////////////////////////////////////
  1682. BOOL Steinberg(HWND hWnd)
  1683. {
  1684. DWORD              OffBits,BufSize,IntBufSize;
  1685.     LPBITMAPINFOHEADER lpImgData;
  1686. HLOCAL             hTempImgData;
  1687. LPBITMAPINFOHEADER lpTempImgData;
  1688. LPSTR              lpPtr;
  1689. LPSTR              lpTempPtr;
  1690. HDC                hDc;
  1691. HFILE              hf;
  1692. LONG               x,y;
  1693. unsigned char      num;
  1694. float              e,f;
  1695. HLOCAL             hIntBuf;
  1696. int                *lpIntBuf,*lpIntPtr;
  1697. int                tempnum;
  1698. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  1699. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  1700. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  1701.     {
  1702.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1703.         return FALSE;
  1704.     }
  1705. IntBufSize=(DWORD)bi.biHeight*LineBytes*sizeof(int);
  1706. if((hIntBuf=LocalAlloc(LHND,IntBufSize))==NULL)
  1707.     {
  1708.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1709. LocalFree(hTempImgData);
  1710.         return FALSE;
  1711.     }
  1712.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  1713. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  1714. lpIntBuf=(int *)LocalLock(hIntBuf);
  1715. //copy header data
  1716. memcpy(lpTempImgData,lpImgData,OffBits);
  1717. //copy image data to int buffer
  1718. for(y=0;y<bi.biHeight;y++){
  1719. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
  1720. lpIntPtr=(int *)lpIntBuf+(bi.biHeight-1-y)*LineBytes;
  1721. for(x=0;x<bi.biWidth;x++)
  1722. *(lpIntPtr++)=(unsigned char)*(lpPtr++);
  1723. }
  1724. for(y=0;y<bi.biHeight;y++){  
  1725. for(x=0;x<bi.biWidth;x++){
  1726. lpIntPtr=(int *)lpIntBuf+(bi.biHeight-1-y)*LineBytes+x;
  1727. num=(unsigned char)*lpIntPtr;
  1728. if ( num > 128 ){
  1729. *lpIntPtr=255;
  1730. e=(float)(num-255.0);
  1731. }
  1732. else{
  1733. *lpIntPtr=0;
  1734. e=(float)num;
  1735. }
  1736. if(x<bi.biWidth-1){
  1737. f=(float)*(lpIntPtr+1);
  1738. f+=(float)( (3.0/8.0) * e);
  1739. *(lpIntPtr+1)=(int)f;
  1740. }
  1741. if(y<bi.biHeight-1){
  1742. f=(float)*(lpIntPtr-LineBytes);
  1743. f+=(float)( (3.0/8.0) * e);
  1744. *(lpIntPtr-LineBytes)=(int)f;
  1745. f=(float)*(lpIntPtr-LineBytes+1);
  1746. f+=(float)( (1.0/4.0) * e);
  1747. *(lpIntPtr-LineBytes+1)=(int)f;
  1748. }
  1749. }
  1750. }
  1751. //copy new image data from int buffer
  1752. for(y=0;y<bi.biHeight;y++){
  1753. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
  1754. lpIntPtr=(int *)lpIntBuf+(bi.biHeight-1-y)*LineBytes;
  1755. for(x=0;x<bi.biWidth;x++){
  1756. tempnum=*(lpIntPtr++);
  1757. if(tempnum>255) tempnum=255;
  1758. else if (tempnum<0) tempnum=0;
  1759. *(lpTempPtr++)=(unsigned char)tempnum;
  1760. }
  1761. }
  1762.     if(hBitmap!=NULL)
  1763.     DeleteObject(hBitmap);
  1764. hDc=GetDC(hWnd);
  1765. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  1766. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  1767.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  1768.     hf=_lcreat("c:\steinberg.bmp",0);
  1769. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  1770. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  1771. _lclose(hf);
  1772.   ReleaseDC(hWnd,hDc);
  1773. GlobalUnlock(hImgData);
  1774. LocalUnlock(hTempImgData);
  1775. LocalFree(hTempImgData);
  1776. LocalUnlock(hIntBuf);
  1777. LocalFree(hIntBuf);
  1778. return TRUE;
  1779. }
  1780. ////////////////////////////////////////////////////////////////
  1781. BOOL Bmp2Txt(HWND hWnd)
  1782. {
  1783. DWORD              OffBits,BufSize;
  1784.     LPBITMAPINFOHEADER lpImgData;
  1785. LPSTR              lpPtr;
  1786. HFILE              hf;
  1787. int    i, j, k,h,tint,grayindex;
  1788. char               tchar;
  1789. int                TransHeight, TransWidth;
  1790. for(i=0;i<94;i++)
  1791. for(j=i+1;j<95;j++){
  1792. if(gr[i]>gr[j]){
  1793. tchar=ch[i],tint=gr[i];
  1794. ch[i]=ch[j],gr[i]=gr[j];
  1795. ch[j]=tchar,gr[j]=tint;
  1796. }
  1797. }
  1798. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  1799. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  1800.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
  1801. TransWidth = bi.biWidth/8;
  1802. TransHeight = bi.biHeight/16;
  1803.     hf=_lcreat("c:\bmp2txt.txt",0);
  1804. for(i=0;i<TransHeight;i++){
  1805. for(j=0;j<TransWidth;j++){
  1806. grayindex=0;
  1807. for(k=0;k<16;k++)
  1808. for(h=0;h<8;h++){
  1809. lpPtr=(char *)lpImgData+BufSize-LineBytes-(i*16+k)*LineBytes+j*8+h;
  1810. grayindex+=(unsigned char)*lpPtr;
  1811. }
  1812. grayindex/=16*8;
  1813. grayindex=gr[94]*grayindex/255;
  1814. k=0;
  1815. while(gr[k+1]<grayindex)
  1816. k++;
  1817. _lwrite(hf,(char *)&ch[k],sizeof(char)); 
  1818. }
  1819. tchar=(char)13;
  1820. _lwrite(hf,(char *)&tchar,sizeof(char)); 
  1821. tchar=(char)10;
  1822. _lwrite(hf,(char *)&tchar,sizeof(char));
  1823. }
  1824. _lclose(hf);
  1825. GlobalUnlock(hImgData);
  1826. return TRUE;
  1827. }
  1828. ////////////////////////////////////////////////////////////////
  1829. BOOL Histogram(HWND hWnd)
  1830. {
  1831. DWORD              OffBits,BufSize;
  1832.     LPBITMAPINFOHEADER lpImgData;
  1833. LPSTR              lpPtr;
  1834. int    x,y;
  1835. int                grayindex;
  1836. HWND    hPopupWnd;
  1837. int                temp;
  1838. for(grayindex=0;grayindex<256;grayindex++)
  1839. GrayTable[grayindex]=0;
  1840. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  1841. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  1842.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
  1843. for(y=0;y<bi.biHeight;y++){
  1844. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
  1845. for(x=0;x<bi.biWidth;x++){
  1846. grayindex=(unsigned char)*(lpPtr++);
  1847. GrayTable[grayindex]++;
  1848. }
  1849. }
  1850. MaxGrayNum=0;
  1851. MinGrayNum=65535;
  1852. for(grayindex=0;grayindex<256;grayindex++){
  1853. temp=GrayTable[grayindex];
  1854. if(temp>MaxGrayNum)
  1855. MaxGrayNum=temp;
  1856. if( (temp<MinGrayNum) && (temp>0) )
  1857. MinGrayNum=temp;
  1858. }
  1859. GlobalUnlock(hImgData);
  1860.     hPopupWnd = CreateWindow (
  1861.   "PopupWindowClass",
  1862.   "Histogram Statistic Window",
  1863.        WS_OVERLAPPEDWINDOW,
  1864.        50,80,550,350,
  1865.        hWnd,NULL,ghInst,NULL
  1866.        );
  1867. if (hPopupWnd){
  1868. ShowWindow (hPopupWnd, SW_SHOW);
  1869.     UpdateWindow (hPopupWnd);
  1870.     }
  1871. return TRUE;
  1872. }
  1873. ////////////////////////////////////////////////////////////////
  1874. long FAR PASCAL PopupWndProc (HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
  1875. {
  1876. HDC  hdc;
  1877. PAINTSTRUCT  ps;   
  1878. DWORD   i;
  1879. int             xstart;   
  1880. static LOGPEN   blp={PS_SOLID,1,1,RGB(0,0,255)};
  1881. HPEN            bhp;
  1882. float           a,b,temp;
  1883. char            str[10];
  1884. a=(float)(265.0 /( MaxGrayNum - MinGrayNum) );
  1885. b=(float) (270.0-a* MaxGrayNum);
  1886.     switch (message)
  1887.      {
  1888.     case WM_PAINT:
  1889.         hdc = BeginPaint(hWnd, &ps);
  1890.     bhp = CreatePenIndirect(&blp);
  1891. SelectObject(hdc,bhp);
  1892.         MoveToEx(hdc,2,270,NULL);
  1893.         LineTo(hdc,518,270);
  1894.         xstart=2;
  1895.         for(i=0;i<256;i++){
  1896.          MoveToEx(hdc,xstart,270,NULL);
  1897. if (GrayTable[i]!=0) 
  1898. temp=(float)(a*GrayTable[i]+b);
  1899. else temp=0.0f;
  1900.          LineTo(hdc,xstart,270-(int)temp);
  1901. if (i%16 ==0){
  1902.          MoveToEx(hdc,xstart,270,NULL);
  1903.          LineTo(hdc,xstart,280);
  1904. _itoa(i,str,10);
  1905. TextOut(hdc,xstart,285,str,strlen(str));
  1906. }
  1907.          xstart+=2;
  1908.         }
  1909.     MoveToEx(hdc,xstart,270,NULL);
  1910.     LineTo(hdc,xstart,280);
  1911. TextOut(hdc,xstart,285,"256",strlen("256"));
  1912.         EndPaint(hWnd,&ps);
  1913.         DeleteObject(bhp);
  1914.         break;
  1915.     default:
  1916.      break;
  1917.      }
  1918.     return DefWindowProc (hWnd, message, wParam, lParam);
  1919. }
  1920. ////////////////////////////////////////////////////////////////
  1921. BOOL Trueto256(HWND hWnd)
  1922. {
  1923. DWORD              SrcBufSize,OffBits,DstBufSize,DstLineBytes;
  1924.     LPBITMAPINFOHEADER lpImgData;
  1925. LPSTR              lpPtr;
  1926. HLOCAL             hTempImgData;
  1927. LPBITMAPINFOHEADER lpTempImgData;
  1928. LPSTR              lpTempPtr;
  1929. HDC                hDc;
  1930. HFILE              hf;
  1931. LONG               x,y;
  1932. BITMAPFILEHEADER   DstBf;
  1933. BITMAPINFOHEADER   DstBi;
  1934.     LOGPALETTE         *pPal;
  1935.     HPALETTE           hPrevPalette; 
  1936. HLOCAL             hPal;
  1937. WORD               i,j;
  1938. int    Red,Green,Blue,ClrIndex;
  1939. DWORD              ColorHits[4096];
  1940. WORD               ColorIndex[4096];
  1941. DWORD              PalCounts,temp;
  1942. long    ColorError1,ColorError2;
  1943. if(NumColors!=0){
  1944.      MessageBox(hWnd,"Must be a true color bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1945.         return FALSE;
  1946.     }
  1947. DstLineBytes=(DWORD)WIDTHBYTES(bi.biWidth*8);
  1948. DstBufSize=(DWORD)(sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)+(DWORD)DstLineBytes*bi.biHeight);
  1949. memcpy((char *)&DstBf,(char *)&bf,sizeof(BITMAPFILEHEADER));
  1950. memcpy((char *)&DstBi,(char *)&bi,sizeof(BITMAPINFOHEADER));
  1951. DstBf.bfSize=DstBufSize+sizeof(BITMAPFILEHEADER);
  1952. DstBf.bfOffBits=(DWORD)(256*sizeof(RGBQUAD)+sizeof(BITMAPFILEHEADER)
  1953. +sizeof(BITMAPINFOHEADER));
  1954. DstBi.biClrUsed=0;
  1955. DstBi.biBitCount=8;
  1956. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  1957. SrcBufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  1958. if((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
  1959.     {
  1960.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1961.         return FALSE;
  1962.     }
  1963.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  1964. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  1965. //copy image data
  1966. memcpy(lpTempImgData,lpImgData,OffBits);
  1967. //overwrite bitmapinfoheader with the new one
  1968. memcpy(lpTempImgData,(char *)&DstBi,sizeof(BITMAPINFOHEADER));
  1969. memset(ColorHits,0,4096*sizeof(DWORD));
  1970. memset(ColorIndex,0,4096*sizeof(WORD));
  1971. for(y=0;y<bi.biHeight;y++){
  1972. lpPtr=(unsigned char *)lpImgData+(SrcBufSize-LineBytes-y*LineBytes);
  1973. for(x=0;x<bi.biWidth;x++){
  1974. Blue=(int)(*(lpPtr++) & 0xf0);
  1975. Green=(int)(*(lpPtr++) & 0xf0);
  1976. Red=(int)(*(lpPtr++) & 0xf0);
  1977. ClrIndex=(Blue<<4) + Green +(Red >>4);
  1978. ColorHits[ClrIndex]++;
  1979. }
  1980. }
  1981. PalCounts=0;
  1982. //pack the color table
  1983.     for (ClrIndex = 0; ClrIndex < 4096; ClrIndex++)
  1984.     {
  1985. if(ColorHits[ClrIndex]!=0){
  1986. ColorHits[PalCounts]=ColorHits[ClrIndex];
  1987. ColorIndex[PalCounts]=ClrIndex;
  1988. PalCounts++;
  1989. }
  1990. }
  1991.     //sort the color table downsize
  1992.     for (i = 0; i < PalCounts-1; i++)
  1993. for (j = i + 1; j < PalCounts; j++){
  1994. if (ColorHits[j] > ColorHits[i]){
  1995. temp = ColorHits[i];
  1996. ColorHits[i] = ColorHits[j];
  1997. ColorHits[j] = temp;  
  1998.             temp = ColorIndex[i];
  1999.         ColorIndex[i] = ColorIndex[j];
  2000.     ColorIndex[j] = (WORD)temp;
  2001. }
  2002. }
  2003.     hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + 256* sizeof(PALETTEENTRY));
  2004.     pPal =(LOGPALETTE *)LocalLock(hPal);
  2005.     pPal->palNumEntries =(WORD) 256;
  2006. pPal->palVersion    = 0x300;
  2007. lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
  2008. for (i = 0; i < 256; i++) {
  2009.     pPal->palPalEntry[i].peRed=(BYTE)((ColorIndex[i] & 0x00f) << 4);
  2010. pPal->palPalEntry[i].peGreen=(BYTE)((ColorIndex[i] & 0x0f0));
  2011. pPal->palPalEntry[i].peBlue=(BYTE)((ColorIndex[i] & 0xf00) >> 4);
  2012. pPal->palPalEntry[i].peFlags=(BYTE)0;
  2013. *(lpTempPtr++)=(unsigned char)((ColorIndex[i] & 0xf00) >> 4);
  2014. *(lpTempPtr++)=(unsigned char)((ColorIndex[i] & 0x0f0));
  2015. *(lpTempPtr++)=(unsigned char)((ColorIndex[i] & 0x00f) << 4);
  2016. *(lpTempPtr++)=0;
  2017. ColorHits[i]=i;
  2018. }
  2019. //其余的颜色依据最小平方差近似为前256中最接近的一种
  2020.     if (PalCounts > 256){
  2021. for (i = 256; i < PalCounts; i++){
  2022. ColorError1=1000000000;
  2023. Blue = (long)((ColorIndex[i] & 0xf00) >> 4);
  2024. Green = (long)((ColorIndex[i] & 0x0f0));
  2025. Red = (long)((ColorIndex[i] & 0x00f) << 4);
  2026. ClrIndex = 0;
  2027. for (j = 0; j < 256; j++){
  2028. ColorError2=(long)(Blue-pPal->palPalEntry[j].peBlue)*(Blue-pPal->palPalEntry[j].peBlue)+ 
  2029.                         (long)(Green-pPal->palPalEntry[j].peGreen)*(Green-pPal->palPalEntry[j].peGreen)+
  2030.                         (long)(Red-pPal->palPalEntry[j].peRed)*(Red-pPal->palPalEntry[j].peRed); 
  2031.                 if (ColorError2 < ColorError1){
  2032. ColorError1 = ColorError2;
  2033. ClrIndex = j;
  2034.                 }
  2035. }                        
  2036. ColorHits[i] = ClrIndex;
  2037. }
  2038. }                
  2039. if(hPalette!=NULL)                     
  2040.         DeleteObject(hPalette);
  2041. //create new logic palette
  2042. hPalette=CreatePalette(pPal);
  2043. LocalUnlock(hPal);
  2044. LocalFree(hPal);
  2045. hDc=GetDC(hWnd);
  2046. if(hPalette){
  2047.         hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  2048. RealizePalette(hDc);
  2049. }
  2050. for(y=0;y<bi.biHeight;y++){
  2051. lpPtr=(char *)lpImgData+(SrcBufSize-LineBytes-y*LineBytes);
  2052. lpTempPtr=(char *)lpTempImgData+(DstBufSize-DstLineBytes-y*DstLineBytes);
  2053. for(x=0;x<bi.biWidth;x++){
  2054. Blue=(int)(*(lpPtr++) & 0xf0);
  2055. Green=(int)(*(lpPtr++) & 0xf0);
  2056. Red=(int)(*(lpPtr++) & 0xf0);
  2057. ClrIndex=(Blue<<4) + Green +(Red >>4);
  2058.             for (i = 0; i < PalCounts;i++)
  2059. if (ClrIndex == ColorIndex[i]){
  2060. *(lpTempPtr++)=(unsigned char)ColorHits[i];
  2061.                     break;
  2062. }
  2063. }
  2064. }
  2065.     if(hBitmap!=NULL)
  2066.     DeleteObject(hBitmap);
  2067. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  2068. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +256*sizeof(RGBQUAD),
  2069.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  2070. if(hPalette && hPrevPalette){
  2071. SelectPalette(hDc,hPrevPalette,FALSE);
  2072. RealizePalette(hDc);
  2073. }
  2074.     hf=_lcreat("c:\256.bmp",0);
  2075. _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER)); 
  2076. _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
  2077. _lclose(hf);
  2078.   ReleaseDC(hWnd,hDc);
  2079. LocalUnlock(hTempImgData);
  2080. LocalFree(hTempImgData);
  2081. GlobalUnlock(hImgData);
  2082. return TRUE;
  2083. }
  2084. ////////////////////////////////////////////////////////////////
  2085. BOOL ContrastStretch(HWND hWnd)
  2086. {
  2087. DLGPROC            dlgInputBox = NULL;
  2088. DWORD              BufSize;
  2089.     LPBITMAPINFOHEADER lpImgData;
  2090. LPSTR              lpPtr;
  2091. HLOCAL             hTempImgData;
  2092. LPBITMAPINFOHEADER lpTempImgData;
  2093. LPSTR              lpTempPtr;
  2094. HDC                hDc;
  2095. HFILE              hf;
  2096.     LOGPALETTE         *pPal;
  2097.     HPALETTE           hPrevPalette=NULL; 
  2098. HLOCAL             hPal;
  2099. DWORD              i;
  2100. unsigned char      Gray;
  2101. float              a,g1,g2,g;
  2102. if( NumColors!=256){
  2103.      MessageBox(hWnd,"Must be a 256 grayscale bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  2104.         return FALSE;
  2105.     }
  2106. dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
  2107. DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
  2108. FreeProcInstance ( (FARPROC) dlgInputBox );
  2109. if( StretchRatio*(SecondPoint-FirstPoint) > 255.0){
  2110.      MessageBox(hWnd,"StretchRatio*(SecondPoint-FirstPoint)  can not be larger than 255!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  2111.         return FALSE;
  2112.     }
  2113. if( (SecondPoint-FirstPoint) >=255){
  2114.      MessageBox(hWnd,"The area you selected can not be the whole scale!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  2115.         return FALSE;
  2116.     }
  2117. a=(float)((255.0-StretchRatio*(SecondPoint-FirstPoint))/(255.0-(SecondPoint-FirstPoint))); 
  2118. g1=a*FirstPoint;
  2119. g2=StretchRatio*(SecondPoint-FirstPoint)+g1;
  2120. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  2121. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  2122.     {
  2123.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  2124.         return FALSE;
  2125.     }
  2126.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  2127. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  2128. //copy image data
  2129. memcpy(lpTempImgData,lpImgData,BufSize);
  2130. hDc=GetDC(hWnd);
  2131. lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
  2132. lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
  2133. hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
  2134. pPal =(LOGPALETTE *)LocalLock(hPal);
  2135. pPal->palNumEntries =(WORD) NumColors;
  2136. pPal->palVersion    = 0x300;
  2137. for (i = 0; i < 256; i++) {
  2138. Gray=(unsigned char )*lpPtr;
  2139. lpPtr+=4;
  2140. if(Gray<FirstPoint) g=(float)(a*Gray);
  2141. else if (Gray<SecondPoint) g=g1+StretchRatio*(Gray-FirstPoint);
  2142. else g=g2+a*(Gray-SecondPoint);
  2143.     pPal->palPalEntry[i].peRed=(BYTE)g;
  2144. pPal->palPalEntry[i].peGreen=(BYTE)g;
  2145. pPal->palPalEntry[i].peBlue=(BYTE)g;
  2146. pPal->palPalEntry[i].peFlags=0;
  2147. *(lpTempPtr++)=(unsigned char)g;
  2148. *(lpTempPtr++)=(unsigned char)g;
  2149. *(lpTempPtr++)=(unsigned char)g;
  2150. *(lpTempPtr++)=0;
  2151. }
  2152. if(hPalette!=NULL)                     
  2153. DeleteObject(hPalette);
  2154. hPalette=CreatePalette(pPal);
  2155. LocalUnlock(hPal);
  2156. LocalFree(hPal);
  2157. if(hPalette){
  2158. hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  2159. RealizePalette(hDc);
  2160. }
  2161.     if(hBitmap!=NULL)
  2162.     DeleteObject(hBitmap);
  2163. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  2164. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  2165.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  2166. if(hPalette && hPrevPalette){
  2167. SelectPalette(hDc,hPrevPalette,FALSE);
  2168. RealizePalette(hDc);
  2169. }
  2170.     hf=_lcreat("c:\stretch.bmp",0);
  2171. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  2172. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  2173. _lclose(hf);
  2174.   ReleaseDC(hWnd,hDc);
  2175. LocalUnlock(hTempImgData);
  2176. LocalFree(hTempImgData);
  2177. GlobalUnlock(hImgData);
  2178. return TRUE;
  2179. }
  2180. ////////////////////////////////////////////////////////////////
  2181. BOOL Cliping(HWND hWnd)
  2182. {
  2183. DLGPROC            dlgInputBox = NULL;
  2184. DWORD              BufSize;
  2185.     LPBITMAPINFOHEADER lpImgData;
  2186. LPSTR              lpPtr;
  2187. HLOCAL             hTempImgData;
  2188. LPBITMAPINFOHEADER lpTempImgData;
  2189. LPSTR              lpTempPtr;
  2190. HDC                hDc;
  2191. HFILE              hf;
  2192.     LOGPALETTE         *pPal;
  2193.     HPALETTE           hPrevPalette=NULL; 
  2194. HLOCAL             hPal;
  2195. DWORD              i;
  2196. unsigned char      Gray;
  2197. float              a,g;
  2198. if( NumColors!=256){
  2199.      MessageBox(hWnd,"Must be a 256 grayscale bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  2200.         return FALSE;
  2201.     }
  2202. dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
  2203. DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
  2204. FreeProcInstance ( (FARPROC) dlgInputBox );
  2205. if( (SecondPoint-FirstPoint) >255){
  2206.      MessageBox(hWnd,"The area you selected must be within [0-255]!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  2207.         return FALSE;
  2208.     }
  2209. a=(float)(255.0/(SecondPoint-FirstPoint)); 
  2210. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  2211. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  2212.     {
  2213.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  2214.         return FALSE;
  2215.     }
  2216.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  2217. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  2218. //copy image data
  2219. memcpy(lpTempImgData,lpImgData,BufSize);
  2220. hDc=GetDC(hWnd);
  2221. lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
  2222. lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
  2223. hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
  2224. pPal =(LOGPALETTE *)LocalLock(hPal);
  2225. pPal->palNumEntries =(WORD) NumColors;
  2226. pPal->palVersion    = 0x300;
  2227. for (i = 0; i < 256; i++) {
  2228. Gray=(unsigned char )*lpPtr;
  2229. lpPtr+=4;
  2230. if(Gray<FirstPoint) g=0.0f;
  2231. else if (Gray<SecondPoint) g=a*(Gray-FirstPoint);
  2232. else g=255.0f;
  2233.     pPal->palPalEntry[i].peRed=(BYTE)g;
  2234. pPal->palPalEntry[i].peGreen=(BYTE)g;
  2235. pPal->palPalEntry[i].peBlue=(BYTE)g;
  2236. pPal->palPalEntry[i].peFlags=0;
  2237. *(lpTempPtr++)=(unsigned char)g;
  2238. *(lpTempPtr++)=(unsigned char)g;
  2239. *(lpTempPtr++)=(unsigned char)g;
  2240. *(lpTempPtr++)=0;
  2241. }
  2242. if(hPalette!=NULL)                     
  2243. DeleteObject(hPalette);
  2244. hPalette=CreatePalette(pPal);
  2245. LocalUnlock(hPal);
  2246. LocalFree(hPal);
  2247. if(hPalette){
  2248. hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  2249. RealizePalette(hDc);
  2250. }
  2251.     if(hBitmap!=NULL)
  2252.     DeleteObject(hBitmap);
  2253. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  2254. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  2255.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  2256. if(hPalette && hPrevPalette){
  2257. SelectPalette(hDc,hPrevPalette,FALSE);
  2258. RealizePalette(hDc);
  2259. }
  2260.     hf=_lcreat("c:\cliping.bmp",0);
  2261. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  2262. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  2263. _lclose(hf);
  2264.   ReleaseDC(hWnd,hDc);
  2265. LocalUnlock(hTempImgData);
  2266. LocalFree(hTempImgData);
  2267. GlobalUnlock(hImgData);
  2268. return TRUE;
  2269. }
  2270. ////////////////////////////////////////////////////////////////
  2271. BOOL Thresholding(HWND hWnd)
  2272. {
  2273. DLGPROC            dlgInputBox = NULL;
  2274. DWORD              BufSize;
  2275.     LPBITMAPINFOHEADER lpImgData;
  2276. LPSTR              lpPtr;
  2277. HLOCAL             hTempImgData;
  2278. LPBITMAPINFOHEADER lpTempImgData;
  2279. LPSTR              lpTempPtr;
  2280. HDC                hDc;
  2281. HFILE              hf;
  2282.     LOGPALETTE         *pPal;
  2283.     HPALETTE           hPrevPalette=NULL; 
  2284. HLOCAL             hPal;
  2285. DWORD              i;
  2286. unsigned char      Gray;
  2287. if( NumColors!=256){
  2288.      MessageBox(hWnd,"Must be a 256 grayscale bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  2289.         return FALSE;
  2290.     }
  2291. dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
  2292. DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
  2293. FreeProcInstance ( (FARPROC) dlgInputBox );
  2294. if( FirstPoint >255){
  2295.      MessageBox(hWnd,"The point you selected must be within [0-255]","Error Message",MB_OK|MB_ICONEXCLAMATION);
  2296.         return FALSE;
  2297.     }
  2298. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  2299. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  2300.     {
  2301.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  2302.         return FALSE;
  2303.     }
  2304.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  2305. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  2306. //copy image data
  2307. memcpy(lpTempImgData,lpImgData,BufSize);
  2308. hDc=GetDC(hWnd);
  2309. lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
  2310. lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
  2311. hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
  2312. pPal =(LOGPALETTE *)LocalLock(hPal);
  2313. pPal->palNumEntries =(WORD) NumColors;
  2314. pPal->palVersion    = 0x300;
  2315. for (i = 0; i < 256; i++) {
  2316. Gray=(unsigned char )*lpPtr;
  2317. lpPtr+=4;
  2318. if(Gray<FirstPoint) Gray=0;
  2319. else Gray=255;
  2320.     pPal->palPalEntry[i].peRed=Gray;
  2321. pPal->palPalEntry[i].peGreen=Gray;
  2322. pPal->palPalEntry[i].peBlue=Gray;
  2323. pPal->palPalEntry[i].peFlags=0;
  2324. *(lpTempPtr++)=(unsigned char)Gray;
  2325. *(lpTempPtr++)=(unsigned char)Gray;
  2326. *(lpTempPtr++)=(unsigned char)Gray;
  2327. *(lpTempPtr++)=0;
  2328. }
  2329. if(hPalette!=NULL)                     
  2330. DeleteObject(hPalette);
  2331. hPalette=CreatePalette(pPal);
  2332. LocalUnlock(hPal);
  2333. LocalFree(hPal);
  2334. if(hPalette){
  2335. hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  2336. RealizePalette(hDc);
  2337. }
  2338.     if(hBitmap!=NULL)
  2339.     DeleteObject(hBitmap);
  2340. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  2341. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  2342.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  2343. if(hPalette && hPrevPalette){
  2344. SelectPalette(hDc,hPrevPalette,FALSE);
  2345. RealizePalette(hDc);
  2346. }
  2347.     hf=_lcreat("c:\threshold.bmp",0);
  2348. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  2349. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  2350. _lclose(hf);
  2351.   ReleaseDC(hWnd,hDc);
  2352. LocalUnlock(hTempImgData);
  2353. LocalFree(hTempImgData);
  2354. GlobalUnlock(hImgData);
  2355. return TRUE;
  2356. }
  2357. ////////////////////////////////////////////////////////////////
  2358. BOOL Slice(HWND hWnd,BOOL WithBack)
  2359. {
  2360. DLGPROC            dlgInputBox = NULL;
  2361. DWORD              BufSize;
  2362.     LPBITMAPINFOHEADER lpImgData;
  2363. LPSTR              lpPtr;
  2364. HLOCAL             hTempImgData;
  2365. LPBITMAPINFOHEADER lpTempImgData;
  2366. LPSTR              lpTempPtr;
  2367. HDC                hDc;
  2368. HFILE              hf;
  2369.     LOGPALETTE         *pPal;
  2370.     HPALETTE           hPrevPalette=NULL; 
  2371. HLOCAL             hPal;
  2372. DWORD              i;
  2373. unsigned char      Gray;
  2374. if( NumColors!=256){
  2375.      MessageBox(hWnd,"Must be a 256 grayscale bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  2376.         return FALSE;
  2377.     }
  2378. dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
  2379. DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
  2380. FreeProcInstance ( (FARPROC) dlgInputBox );
  2381. if( (SecondPoint-FirstPoint) >255){
  2382.      MessageBox(hWnd,"The area you selected must be within [0-255]!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  2383.         return FALSE;
  2384.     }
  2385. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  2386. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  2387.     {
  2388.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  2389.         return FALSE;
  2390.     }
  2391.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  2392. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  2393. //copy image data
  2394. memcpy(lpTempImgData,lpImgData,BufSize);
  2395. hDc=GetDC(hWnd);
  2396. lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
  2397. lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
  2398. hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
  2399. pPal =(LOGPALETTE *)LocalLock(hPal);
  2400. pPal->palNumEntries =(WORD) NumColors;
  2401. pPal->palVersion    = 0x300;
  2402. for (i = 0; i < 256; i++) {
  2403. Gray=(unsigned char )*lpPtr;
  2404. lpPtr+=4;
  2405. if(WithBack){
  2406. if( (Gray>=FirstPoint) && (Gray<=SecondPoint))
  2407. Gray=255;
  2408. }
  2409. else{
  2410. if(Gray<FirstPoint) Gray=0;