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

图形图像处理

开发平台:

Visual C++

  1. //////////////////////////////////////////////////////////////
  2. //Name:colorope.c
  3. //Purpose: histogram and color operation
  4. //Author: phoenix, CS, TshingHua, Beijing, P.R.C.
  5. //Email: bjlufengjun@www.163.net or lufengjun@hotmail.com
  6. //Date:April 3, 1998
  7. //header file
  8. #include "bmp.h"
  9. #include "memory.h"
  10. #include "math.h"
  11. #include "stdio.h"
  12. //macro definition
  13. #define WIDTHBYTES(i)    ((i+31)/32*4)
  14. //function declaration
  15. int PASCAL WinMain (HANDLE, HANDLE, LPSTR, int);
  16. LRESULT CALLBACK MainWndProc(HWND , UINT,WPARAM, LPARAM);
  17. BOOL LoadBmpFile (HWND hWnd,char *BmpFileName);
  18. BOOL CALLBACK InputBox( HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam );
  19. BOOL Histogram(HWND hWnd);
  20. long FAR PASCAL PopupWndProc (HWND,UINT,WPARAM,LPARAM);
  21. BOOL ColortoGrayScale(HWND hWnd);
  22. BOOL Trueto256(HWND hWnd);
  23. BOOL Invert(HWND hWnd);
  24. BOOL ContrastStretch(HWND hWnd);
  25. BOOL Cliping(HWND hWnd);
  26. BOOL Thresholding(HWND hWnd);
  27. BOOL Slice(HWND hWnd,BOOL WithBack);
  28. BOOL HistogramEqua(HWND hWnd);
  29. //global variable declaration
  30. BITMAPFILEHEADER   bf;
  31. BITMAPINFOHEADER   bi;
  32. HPALETTE           hPalette=NULL;
  33. HBITMAP            hBitmap=NULL;
  34. HGLOBAL            hImgData=NULL;
  35. DWORD              NumColors;
  36. DWORD              LineBytes;
  37. HINSTANCE          ghInst;
  38. DWORD              ImgWidth=0 , ImgHeight=0;
  39. int GrayTable[256];
  40. int MaxGrayNum;
  41. int MinGrayNum;
  42. int FirstPoint=0, SecondPoint=255;
  43. float StretchRatio=2.0f;
  44. int EquaScale;
  45. ///////////////////////////////////////////////////////////
  46. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  47.     LPSTR lpszCmdLine, int nCmdShow)
  48. {
  49. MSG       msg;
  50. WNDCLASS  wndclass;
  51. HWND      hWnd;
  52. ghInst=hInstance;
  53. if ( ! hPrevInstance ){
  54. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  55. wndclass.lpfnWndProc = MainWndProc;
  56. wndclass.cbClsExtra = 0;
  57. wndclass.cbWndExtra = 0;
  58. wndclass.hInstance = hInstance;
  59. wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  60. wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  61. wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  62. wndclass.lpszMenuName =  "BMPMENU";
  63. wndclass.lpszClassName = "phoenix ip system";
  64.     }
  65.     if ( ! RegisterClass (&wndclass) )
  66.     return FALSE;
  67. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  68.     wndclass.lpfnWndProc = PopupWndProc;
  69.     wndclass.cbClsExtra = 0;
  70.     wndclass.cbWndExtra = 0;
  71.     wndclass.hInstance = ghInst;
  72.     wndclass.hIcon = NULL;
  73.     wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  74.     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  75.     wndclass.lpszMenuName =  NULL;
  76.     wndclass.lpszClassName = "PopupWindowClass";
  77.     if ( ! RegisterClass (&wndclass) )
  78. return FALSE;
  79.  
  80. hWnd = CreateWindow ("phoenix ip system","Color Operation",
  81.  WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
  82.  CW_USEDEFAULT, CW_USEDEFAULT, NULL,NULL,
  83.  hInstance, NULL);
  84. if (!hWnd)
  85. return FALSE;
  86. ShowWindow (hWnd, SW_SHOWMAXIMIZED);
  87. UpdateWindow (hWnd);
  88. while ( GetMessage (&msg, NULL, 0, 0) ){
  89. TranslateMessage (&msg);
  90. DispatchMessage (&msg);
  91.     }
  92. return msg.wParam;
  93. }
  94. ////////////////////////////////////////////////////////////////
  95. LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam)
  96. {
  97.     static  HDC    hDC,hMemDC;
  98.     PAINTSTRUCT    ps;
  99.     switch (message){
  100.     case WM_PAINT:
  101. {         
  102. hDC = BeginPaint(hWnd, &ps);
  103. if (hBitmap)
  104. hMemDC = CreateCompatibleDC(hDC);
  105. if (hPalette)
  106. {           
  107. SelectPalette (hDC, hPalette, FALSE); 
  108. SelectPalette (hMemDC, hPalette, FALSE);
  109. RealizePalette (hDC);
  110. }   
  111. SelectObject(hMemDC, hBitmap); 
  112. BitBlt(hDC, 0, 0, ImgWidth,ImgHeight, hMemDC, 0, 0, SRCCOPY);
  113. DeleteDC(hMemDC);
  114. }
  115. EndPaint(hWnd, &ps);
  116. break;
  117. }
  118. case WM_DESTROY: //注意释放内存和位图,调色板句柄
  119.         if(hBitmap!=NULL)
  120.         DeleteObject(hBitmap);
  121.     
  122. if(hPalette!=NULL)                     
  123.         DeleteObject(hPalette);
  124. if(hImgData!=NULL){
  125. GlobalUnlock(hImgData);
  126. GlobalFree(hImgData);
  127. }         
  128.     PostQuitMessage (0);
  129.     return 0;
  130. case WM_COMMAND:
  131.     switch (wParam){
  132.     case IDM_LOADBMP: 
  133. //注意重新分配内存和调色板,位图句柄时,先释放原来的
  134.         if(hBitmap!=NULL){     
  135. DeleteObject(hBitmap);
  136.             hBitmap=NULL;
  137.         }
  138.         if(hPalette!=NULL){                     
  139. DeleteObject(hPalette);
  140.             hPalette=NULL;
  141.         }
  142. if(hImgData!=NULL){
  143. GlobalUnlock(hImgData);
  144. GlobalFree(hImgData);  
  145. hImgData=NULL;
  146. }         
  147. if(LoadBmpFile(hWnd,"c:\test.bmp")) //成功,则重画窗口
  148.                 InvalidateRect(hWnd,NULL,TRUE);
  149.         break;
  150. case IDM_HISTOGRAM:
  151. if(hImgData!=NULL){
  152. if(Histogram(hWnd))
  153. InvalidateRect(hWnd,NULL,TRUE);
  154. }
  155. else
  156. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  157. break;
  158. case IDM_TRUE256:
  159. if(hImgData!=NULL){
  160. if(Trueto256(hWnd))
  161. InvalidateRect(hWnd,NULL,TRUE);
  162. }
  163. else
  164. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  165. break;
  166. case IDM_CLRGRAY:
  167. if(hImgData!=NULL){
  168. if(ColortoGrayScale(hWnd))
  169. InvalidateRect(hWnd,NULL,TRUE);
  170. }
  171. else
  172. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  173. break;
  174. case IDM_INVERT:
  175. if(hImgData!=NULL){
  176. if(Invert(hWnd))
  177. InvalidateRect(hWnd,NULL,TRUE);
  178. }
  179. else
  180. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  181. break;
  182. case IDM_CONTSTRETCH:
  183. if(hImgData!=NULL){
  184. if (ContrastStretch(hWnd))
  185. InvalidateRect(hWnd,NULL,TRUE);
  186. }
  187. else
  188. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  189. break;
  190. case IDM_CLIPING:
  191. if(hImgData!=NULL){
  192. if(Cliping(hWnd))
  193. InvalidateRect(hWnd,NULL,TRUE);
  194. }
  195. else
  196. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  197. break;
  198. case IDM_THRESHOLD:
  199. if(hImgData!=NULL){
  200. if(Thresholding(hWnd))
  201. InvalidateRect(hWnd,NULL,TRUE);
  202. }
  203. else
  204. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  205. break;
  206. case IDM_SLICEWITHOUTBACK:
  207. if(hImgData!=NULL){
  208. if(Slice(hWnd,FALSE))
  209. InvalidateRect(hWnd,NULL,TRUE);
  210. }
  211. else
  212. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  213. break;
  214. case IDM_SLICEWITHBACK:
  215. if(hImgData!=NULL){
  216. if(Slice(hWnd,TRUE))
  217. InvalidateRect(hWnd,NULL,TRUE);
  218. }
  219. else
  220. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  221. break;
  222. case IDM_HISTOGRAMEQUA:
  223. if(hImgData!=NULL){
  224. if(HistogramEqua(hWnd))
  225. InvalidateRect(hWnd,NULL,TRUE);
  226. }
  227. else
  228. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  229. break;
  230. case IDM_EXIT:
  231.         SendMessage(hWnd,WM_DESTROY,0,0L);
  232.         break;
  233.     }
  234. break;                
  235.     }
  236.     return DefWindowProc (hWnd, message, wParam, lParam);
  237. }
  238. ////////////////////////////////////////////////////////////////
  239. BOOL LoadBmpFile (HWND hWnd,char *BmpFileName)
  240. {   
  241.     HFILE              hf;
  242.     LPBITMAPINFOHEADER lpImgData;
  243.     LOGPALETTE         *pPal;
  244.     LPRGBQUAD          lpRGB;
  245.     HPALETTE           hPrevPalette; 
  246.     HDC                hDc;
  247. HLOCAL             hPal;
  248. DWORD         ImgSize;
  249. DWORD              i;
  250.     if((hf=_lopen(BmpFileName,OF_READ))==HFILE_ERROR){
  251.         MessageBox(hWnd,"File c:\test.bmp not found!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  252.         return FALSE;
  253. }
  254. _lread(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  255. _lread(hf,(LPSTR)&bi,sizeof(BITMAPINFOHEADER));
  256. ImgWidth=bi.biWidth;
  257. ImgHeight=bi.biHeight;
  258. LineBytes=(DWORD)WIDTHBYTES(bi.biWidth*bi.biBitCount);
  259. ImgSize=(DWORD)LineBytes*bi.biHeight;
  260.     if(bi.biClrUsed!=0)
  261. NumColors=(DWORD)bi.biClrUsed;
  262. else
  263.         switch(bi.biBitCount){
  264.         case 1:
  265.              NumColors=2;
  266.              break;
  267.          case 4:
  268.              NumColors=16;
  269.              break;
  270.          case 8:
  271.              NumColors=256;
  272.              break;
  273.          case 24:
  274.              NumColors=0;
  275.              break;
  276.               default:
  277.                   MessageBox(hWnd,"Invalid color numbers!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  278.                   _lclose(hf);
  279.                   return FALSE; 
  280.         }
  281. if(bf.bfOffBits!=(DWORD)(NumColors*sizeof(RGBQUAD)+sizeof(BITMAPFILEHEADER)
  282. +sizeof(BITMAPINFOHEADER)))
  283. {
  284.      MessageBox(hWnd,"Invalid color numbers!","Error Message" ,MB_OK|
  285.                MB_ICONEXCLAMATION);
  286. _lclose(hf);
  287. return FALSE; 
  288. }
  289. bf.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+ImgSize;
  290. if((hImgData=GlobalAlloc(GHND,(DWORD)(sizeof(BITMAPINFOHEADER)+
  291.      NumColors*sizeof(RGBQUAD)+ImgSize)))==NULL)
  292. {
  293.      MessageBox(hWnd,"Error alloc memory!","ErrorMessage",MB_OK|
  294.                    MB_ICONEXCLAMATION);
  295.     _lclose(hf);
  296. return FALSE;
  297. }
  298.   
  299. lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData); 
  300.     _llseek(hf,sizeof(BITMAPFILEHEADER),FILE_BEGIN);
  301. _hread(hf,(char *)lpImgData,(long)sizeof(BITMAPINFOHEADER)
  302.            +(long)NumColors*sizeof(RGBQUAD)+ImgSize);
  303. _lclose(hf);
  304.     if(NumColors!=0)
  305. {                    
  306.     hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
  307.     pPal =(LOGPALETTE *)LocalLock(hPal);
  308.     pPal->palNumEntries =(WORD) NumColors;
  309. pPal->palVersion    = 0x300;
  310.     lpRGB = (LPRGBQUAD)((LPSTR)lpImgData + (DWORD)sizeof(BITMAPINFOHEADER));
  311. for (i = 0; i < NumColors; i++) {
  312.       pPal->palPalEntry[i].peRed=lpRGB->rgbRed;
  313. pPal->palPalEntry[i].peGreen=lpRGB->rgbGreen;
  314. pPal->palPalEntry[i].peBlue=lpRGB->rgbBlue;
  315. pPal->palPalEntry[i].peFlags=(BYTE)0;
  316. lpRGB++;
  317. }
  318. hPalette=CreatePalette(pPal);
  319. LocalUnlock(hPal);
  320. LocalFree(hPal);
  321. }
  322. hDc=GetDC(hWnd);
  323. if(hPalette){
  324.         hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  325. RealizePalette(hDc);
  326. }
  327. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpImgData, (LONG)CBM_INIT,
  328. (LPSTR)lpImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  329.     (LPBITMAPINFO)lpImgData, DIB_RGB_COLORS);
  330. if(hPalette && hPrevPalette){
  331. SelectPalette(hDc,hPrevPalette,FALSE);
  332. RealizePalette(hDc);
  333. }
  334.  
  335. ReleaseDC(hWnd,hDc);
  336. GlobalUnlock(hImgData);
  337. return TRUE; 
  338. }
  339. ////////////////////////////////////////////////////////////////
  340. BOOL CALLBACK InputBox( HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam )
  341. char str[80];
  342.     switch  ( message ){
  343.     case WM_INITDIALOG:    
  344.       SetDlgItemText(hDlg,ID_FIRSTPOINT,"0");
  345.       SetDlgItemText(hDlg,ID_SECONDPOINT,"0");
  346.       SetDlgItemText(hDlg,ID_STRETCHRATIO,"2.0");
  347.       SetDlgItemText(hDlg,ID_EQUASCALE,"128");
  348.      return TRUE;
  349.     case WM_COMMAND:    
  350.      if ( wParam == IDOK || wParam == IDCANCEL )
  351.         {
  352.          GetDlgItemText(hDlg,ID_FIRSTPOINT,str,80);
  353. FirstPoint=atoi(str);
  354.          GetDlgItemText(hDlg,ID_SECONDPOINT,str,80);
  355. SecondPoint=atoi(str);
  356.          GetDlgItemText(hDlg,ID_STRETCHRATIO,str,80);
  357. StretchRatio=(float)atof(str);
  358.          GetDlgItemText(hDlg,ID_EQUASCALE,str,80);
  359. EquaScale=atoi(str);
  360.             EndDialog ( hDlg, TRUE );
  361.             return  TRUE;
  362.         }
  363.         break;
  364.     }
  365. return FALSE;      
  366. }
  367. ////////////////////////////////////////////////////////////////
  368. BOOL ColortoGrayScale(HWND hWnd)
  369. {
  370. DWORD              SrcBufSize,DstBufSize,DstLineBytes;
  371.     LPBITMAPINFOHEADER lpImgData;
  372. LPSTR              lpPtr;
  373. HLOCAL             hTempImgData;
  374. LPBITMAPINFOHEADER lpTempImgData;
  375. LPSTR              lpTempPtr;
  376. HDC                hDc;
  377. HFILE              hf;
  378. LONG               x,y;
  379. BITMAPFILEHEADER   DstBf;
  380. BITMAPINFOHEADER   DstBi;
  381.     LOGPALETTE         *pPal;
  382.     HPALETTE           hPrevPalette; 
  383. HLOCAL             hPal;
  384. DWORD    NewNumColors;
  385. WORD    NewBitCount;
  386. float              Y;
  387. DWORD              i;
  388. unsigned char      Red,Green,Blue,Gray;
  389. NewNumColors=NumColors;
  390. NewBitCount=bi.biBitCount;
  391. if(NumColors==0) //true color
  392. {
  393. NewNumColors=256;
  394. NewBitCount=8;
  395. }
  396. DstLineBytes=(DWORD)WIDTHBYTES(bi.biWidth*NewBitCount);
  397. DstBufSize=(DWORD)(sizeof(BITMAPINFOHEADER)+NewNumColors*sizeof(RGBQUAD)+(DWORD)DstLineBytes*bi.biHeight);
  398. memcpy((char *)&DstBf,(char *)&bf,sizeof(BITMAPFILEHEADER));
  399. memcpy((char *)&DstBi,(char *)&bi,sizeof(BITMAPINFOHEADER));
  400. DstBf.bfSize=DstBufSize+sizeof(BITMAPFILEHEADER);
  401. DstBf.bfOffBits=(DWORD)(NewNumColors*sizeof(RGBQUAD)+sizeof(BITMAPFILEHEADER)
  402. +sizeof(BITMAPINFOHEADER));
  403. DstBi.biClrUsed=0;
  404. DstBi.biBitCount=NewBitCount;
  405. SrcBufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  406. if((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
  407.     {
  408.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  409.         return FALSE;
  410.     }
  411.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  412. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  413. //copy image data
  414. memcpy(lpTempImgData,lpImgData,DstBufSize);
  415. //overwrite bitmapinfoheader with the new one
  416. memcpy(lpTempImgData,(char *)&DstBi,sizeof(BITMAPINFOHEADER));
  417. lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
  418. lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
  419.     hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NewNumColors* sizeof(PALETTEENTRY));
  420.     pPal =(LOGPALETTE *)LocalLock(hPal);
  421.     pPal->palNumEntries =(WORD) NewNumColors;
  422. pPal->palVersion    = 0x300;
  423. if(NumColors==0) //true color
  424. for (i = 0; i < 256; i++) {
  425.       pPal->palPalEntry[i].peRed=(BYTE)i;
  426. pPal->palPalEntry[i].peGreen=(BYTE)i;
  427. pPal->palPalEntry[i].peBlue=(BYTE)i;
  428. pPal->palPalEntry[i].peFlags=(BYTE)0;
  429. *(lpTempPtr++)=(unsigned char)i;
  430. *(lpTempPtr++)=(unsigned char)i;
  431. *(lpTempPtr++)=(unsigned char)i;
  432. *(lpTempPtr++)=0;
  433. }
  434. else 
  435. for (i = 0; i < NewNumColors; i++) {
  436. Blue=(unsigned char )(*lpPtr++);
  437. Green=(unsigned char )(*lpPtr++);
  438. Red=(unsigned char )(*lpPtr++);
  439. Y=(float)(Red*0.299+Green*0.587+Blue*0.114);
  440. Gray=(BYTE)Y;
  441. lpPtr++;
  442.       pPal->palPalEntry[i].peRed=Gray;
  443. pPal->palPalEntry[i].peGreen=Gray;
  444. pPal->palPalEntry[i].peBlue=Gray;
  445. pPal->palPalEntry[i].peFlags=0;
  446. *(lpTempPtr++)=(unsigned char)Gray;
  447. *(lpTempPtr++)=(unsigned char)Gray;
  448. *(lpTempPtr++)=(unsigned char)Gray;
  449. *(lpTempPtr++)=0;
  450. }
  451. if(hPalette!=NULL)                     
  452.         DeleteObject(hPalette);
  453. hPalette=CreatePalette(pPal);
  454. LocalUnlock(hPal);
  455. LocalFree(hPal);
  456. hDc=GetDC(hWnd);
  457. if(hPalette){
  458.         hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  459. RealizePalette(hDc);
  460. }
  461. if(NumColors==0)
  462. for(y=0;y<bi.biHeight;y++){
  463. lpPtr=(char *)lpImgData+(SrcBufSize-LineBytes-y*LineBytes);
  464. lpTempPtr=(char *)lpTempImgData+(DstBufSize-DstLineBytes-y*DstLineBytes);
  465. for(x=0;x<bi.biWidth;x++){
  466. Blue=(unsigned char )(*lpPtr++);
  467. Green=(unsigned char )(*lpPtr++);
  468. Red=(unsigned char )(*lpPtr++);
  469. Y=(float)(Red*0.299+Green*0.587+Blue*0.114);
  470. Gray=(BYTE)Y;
  471. *(lpTempPtr++)=(unsigned char)Gray;
  472. }
  473. }
  474.     if(hBitmap!=NULL)
  475.     DeleteObject(hBitmap);
  476. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  477. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NewNumColors*sizeof(RGBQUAD),
  478.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  479. if(hPalette && hPrevPalette){
  480. SelectPalette(hDc,hPrevPalette,FALSE);
  481. RealizePalette(hDc);
  482. }
  483.     hf=_lcreat("c:\gray.bmp",0);
  484. _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER)); 
  485. _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
  486. _lclose(hf);
  487.   ReleaseDC(hWnd,hDc);
  488. LocalUnlock(hTempImgData);
  489. LocalFree(hTempImgData);
  490. GlobalUnlock(hImgData);
  491. return TRUE;
  492. }
  493. ////////////////////////////////////////////////////////////////
  494. BOOL Invert(HWND hWnd)
  495. {
  496. DWORD              BufSize;
  497.     LPBITMAPINFOHEADER lpImgData;
  498. LPSTR              lpPtr;
  499. HLOCAL             hTempImgData;
  500. LPBITMAPINFOHEADER lpTempImgData;
  501. LPSTR              lpTempPtr;
  502. HDC                hDc;
  503. HFILE              hf;
  504. LONG               x,y;
  505.     LOGPALETTE         *pPal;
  506.     HPALETTE           hPrevPalette=NULL; 
  507. HLOCAL             hPal;
  508. DWORD              i;
  509. unsigned char      Red,Green,Blue;
  510. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  511. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  512.     {
  513.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  514.         return FALSE;
  515.     }
  516.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  517. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  518. //copy image data
  519. memcpy(lpTempImgData,lpImgData,BufSize);
  520. hDc=GetDC(hWnd);
  521. if(NumColors!=0){
  522. lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
  523. lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
  524.     hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
  525. pPal =(LOGPALETTE *)LocalLock(hPal);
  526. pPal->palNumEntries =(WORD) NumColors;
  527. pPal->palVersion    = 0x300;
  528. for (i = 0; i < NumColors; i++) {
  529. Blue=(unsigned char )(*lpPtr++);
  530. Green=(unsigned char )(*lpPtr++);
  531. Red=(unsigned char )(*lpPtr++);
  532. lpPtr++;
  533.       pPal->palPalEntry[i].peRed=(BYTE)(255-Red);
  534. pPal->palPalEntry[i].peGreen=(BYTE)(255-Green);
  535. pPal->palPalEntry[i].peBlue=(BYTE)(255-Blue);
  536. pPal->palPalEntry[i].peFlags=0;
  537. *(lpTempPtr++)=(unsigned char)(255-Blue);
  538. *(lpTempPtr++)=(unsigned char)(255-Green);
  539. *(lpTempPtr++)=(unsigned char)(255-Red);
  540. *(lpTempPtr++)=0;
  541. }
  542. if(hPalette!=NULL)                     
  543. DeleteObject(hPalette);
  544. hPalette=CreatePalette(pPal);
  545. LocalUnlock(hPal);
  546. LocalFree(hPal);
  547. if(hPalette){
  548. hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  549. RealizePalette(hDc);
  550. }
  551. }
  552. else{
  553. for(y=0;y<bi.biHeight;y++){
  554. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
  555. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
  556. for(x=0;x<bi.biWidth;x++){
  557. Blue=(unsigned char )(*lpPtr++);
  558. Green=(unsigned char )(*lpPtr++);
  559. Red=(unsigned char )(*lpPtr++);
  560. *(lpTempPtr++)=(unsigned char)(255-Blue);
  561. *(lpTempPtr++)=(unsigned char)(255-Green);
  562. *(lpTempPtr++)=(unsigned char)(255-Red);
  563. }
  564. }
  565. }
  566.     if(hBitmap!=NULL)
  567.     DeleteObject(hBitmap);
  568. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  569. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  570.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  571. if(hPalette && hPrevPalette){
  572. SelectPalette(hDc,hPrevPalette,FALSE);
  573. RealizePalette(hDc);
  574. }
  575.     hf=_lcreat("c:\invert.bmp",0);
  576. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  577. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  578. _lclose(hf);
  579.   ReleaseDC(hWnd,hDc);
  580. LocalUnlock(hTempImgData);
  581. LocalFree(hTempImgData);
  582. GlobalUnlock(hImgData);
  583. return TRUE;
  584. }
  585. ////////////////////////////////////////////////////////////////
  586. BOOL Histogram(HWND hWnd)
  587. {
  588. DWORD              OffBits,BufSize;
  589.     LPBITMAPINFOHEADER lpImgData;
  590. LPSTR              lpPtr;
  591. int    x,y;
  592. int                grayindex;
  593. HWND    hPopupWnd;
  594. int                temp;
  595. for(grayindex=0;grayindex<256;grayindex++)
  596. GrayTable[grayindex]=0;
  597. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  598. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  599.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
  600. for(y=0;y<bi.biHeight;y++){
  601. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
  602. for(x=0;x<bi.biWidth;x++){
  603. grayindex=(unsigned char)*(lpPtr++);
  604. GrayTable[grayindex]++;
  605. }
  606. }
  607. MaxGrayNum=0;
  608. MinGrayNum=65535;
  609. for(grayindex=0;grayindex<256;grayindex++){
  610. temp=GrayTable[grayindex];
  611. if(temp>MaxGrayNum)
  612. MaxGrayNum=temp;
  613. if( (temp<MinGrayNum) && (temp>0) )
  614. MinGrayNum=temp;
  615. }
  616. GlobalUnlock(hImgData);
  617.     hPopupWnd = CreateWindow (
  618.   "PopupWindowClass",
  619.   "Histogram Statistic Window",
  620.        WS_OVERLAPPEDWINDOW,
  621.        50,80,550,350,
  622.        hWnd,NULL,ghInst,NULL
  623.        );
  624. if (hPopupWnd){
  625. ShowWindow (hPopupWnd, SW_SHOW);
  626.     UpdateWindow (hPopupWnd);
  627.     }
  628. return TRUE;
  629. }
  630. ////////////////////////////////////////////////////////////////
  631. long FAR PASCAL PopupWndProc (HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
  632. {
  633. HDC  hdc;
  634. PAINTSTRUCT  ps;   
  635. DWORD   i;
  636. int             xstart;   
  637. static LOGPEN   blp={PS_SOLID,1,1,RGB(0,0,255)};
  638. HPEN            bhp;
  639. float           a,b,temp;
  640. char            str[10];
  641. a=(float)(265.0 /( MaxGrayNum - MinGrayNum) );
  642. b=(float) (270.0-a* MaxGrayNum);
  643.     switch (message)
  644.      {
  645.     case WM_PAINT:
  646.         hdc = BeginPaint(hWnd, &ps);
  647.     bhp = CreatePenIndirect(&blp);
  648. SelectObject(hdc,bhp);
  649.         MoveToEx(hdc,2,270,NULL);
  650.         LineTo(hdc,518,270);
  651.         xstart=2;
  652.         for(i=0;i<256;i++){
  653.          MoveToEx(hdc,xstart,270,NULL);
  654. if (GrayTable[i]!=0) 
  655. temp=(float)(a*GrayTable[i]+b);
  656. else temp=0.0f;
  657.          LineTo(hdc,xstart,270-(int)temp);
  658. if (i%16 ==0){
  659.          MoveToEx(hdc,xstart,270,NULL);
  660.          LineTo(hdc,xstart,280);
  661. _itoa(i,str,10);
  662. TextOut(hdc,xstart,285,str,strlen(str));
  663. }
  664.          xstart+=2;
  665.         }
  666.     MoveToEx(hdc,xstart,270,NULL);
  667.     LineTo(hdc,xstart,280);
  668. TextOut(hdc,xstart,285,"256",strlen("256"));
  669.         EndPaint(hWnd,&ps);
  670.         DeleteObject(bhp);
  671.         break;
  672.     default:
  673.      break;
  674.      }
  675.     return DefWindowProc (hWnd, message, wParam, lParam);
  676. }
  677. ////////////////////////////////////////////////////////////////
  678. BOOL Trueto256(HWND hWnd)
  679. {
  680. DWORD              SrcBufSize,OffBits,DstBufSize,DstLineBytes;
  681.     LPBITMAPINFOHEADER lpImgData;
  682. LPSTR              lpPtr;
  683. HLOCAL             hTempImgData;
  684. LPBITMAPINFOHEADER lpTempImgData;
  685. LPSTR              lpTempPtr;
  686. HDC                hDc;
  687. HFILE              hf;
  688. LONG               x,y;
  689. BITMAPFILEHEADER   DstBf;
  690. BITMAPINFOHEADER   DstBi;
  691.     LOGPALETTE         *pPal;
  692.     HPALETTE           hPrevPalette; 
  693. HLOCAL             hPal;
  694. WORD               i,j;
  695. int    Red,Green,Blue,ClrIndex;
  696. DWORD              ColorHits[4096];
  697. WORD               ColorIndex[4096];
  698. DWORD              PalCounts,temp;
  699. long    ColorError1,ColorError2;
  700. if(NumColors!=0){
  701.      MessageBox(hWnd,"Must be a true color bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  702.         return FALSE;
  703.     }
  704. DstLineBytes=(DWORD)WIDTHBYTES(bi.biWidth*8);
  705. DstBufSize=(DWORD)(sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)+(DWORD)DstLineBytes*bi.biHeight);
  706. memcpy((char *)&DstBf,(char *)&bf,sizeof(BITMAPFILEHEADER));
  707. memcpy((char *)&DstBi,(char *)&bi,sizeof(BITMAPINFOHEADER));
  708. DstBf.bfSize=DstBufSize+sizeof(BITMAPFILEHEADER);
  709. DstBf.bfOffBits=(DWORD)(256*sizeof(RGBQUAD)+sizeof(BITMAPFILEHEADER)
  710. +sizeof(BITMAPINFOHEADER));
  711. DstBi.biClrUsed=0;
  712. DstBi.biBitCount=8;
  713. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  714. SrcBufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  715. if((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
  716.     {
  717.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  718.         return FALSE;
  719.     }
  720.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  721. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  722. //copy image data
  723. memcpy(lpTempImgData,lpImgData,OffBits);
  724. //overwrite bitmapinfoheader with the new one
  725. memcpy(lpTempImgData,(char *)&DstBi,sizeof(BITMAPINFOHEADER));
  726. memset(ColorHits,0,4096*sizeof(DWORD));
  727. memset(ColorIndex,0,4096*sizeof(WORD));
  728. for(y=0;y<bi.biHeight;y++){
  729. lpPtr=(unsigned char *)lpImgData+(SrcBufSize-LineBytes-y*LineBytes);
  730. for(x=0;x<bi.biWidth;x++){
  731. Blue=(int)(*(lpPtr++) & 0xf0);
  732. Green=(int)(*(lpPtr++) & 0xf0);
  733. Red=(int)(*(lpPtr++) & 0xf0);
  734. ClrIndex=(Blue<<4) + Green +(Red >>4);
  735. ColorHits[ClrIndex]++;
  736. }
  737. }
  738. PalCounts=0;
  739. //pack the color table
  740.     for (ClrIndex = 0; ClrIndex < 4096; ClrIndex++)
  741.     {
  742. if(ColorHits[ClrIndex]!=0){
  743. ColorHits[PalCounts]=ColorHits[ClrIndex];
  744. ColorIndex[PalCounts]=ClrIndex;
  745. PalCounts++;
  746. }
  747. }
  748.     //sort the color table downsize
  749.     for (i = 0; i < PalCounts-1; i++)
  750. for (j = i + 1; j < PalCounts; j++){
  751. if (ColorHits[j] > ColorHits[i]){
  752. temp = ColorHits[i];
  753. ColorHits[i] = ColorHits[j];
  754. ColorHits[j] = temp;  
  755.             temp = ColorIndex[i];
  756.         ColorIndex[i] = ColorIndex[j];
  757.     ColorIndex[j] = (WORD)temp;
  758. }
  759. }
  760.     hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + 256* sizeof(PALETTEENTRY));
  761.     pPal =(LOGPALETTE *)LocalLock(hPal);
  762.     pPal->palNumEntries =(WORD) 256;
  763. pPal->palVersion    = 0x300;
  764. lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
  765. for (i = 0; i < 256; i++) {
  766.     pPal->palPalEntry[i].peRed=(BYTE)((ColorIndex[i] & 0x00f) << 4);
  767. pPal->palPalEntry[i].peGreen=(BYTE)((ColorIndex[i] & 0x0f0));
  768. pPal->palPalEntry[i].peBlue=(BYTE)((ColorIndex[i] & 0xf00) >> 4);
  769. pPal->palPalEntry[i].peFlags=(BYTE)0;
  770. *(lpTempPtr++)=(unsigned char)((ColorIndex[i] & 0xf00) >> 4);
  771. *(lpTempPtr++)=(unsigned char)((ColorIndex[i] & 0x0f0));
  772. *(lpTempPtr++)=(unsigned char)((ColorIndex[i] & 0x00f) << 4);
  773. *(lpTempPtr++)=0;
  774. ColorHits[i]=i;
  775. }
  776. //其余的颜色依据最小平方差近似为前256中最接近的一种
  777.     if (PalCounts > 256){
  778. for (i = 256; i < PalCounts; i++){
  779. ColorError1=1000000000;
  780. Blue = (long)((ColorIndex[i] & 0xf00) >> 4);
  781. Green = (long)((ColorIndex[i] & 0x0f0));
  782. Red = (long)((ColorIndex[i] & 0x00f) << 4);
  783. ClrIndex = 0;
  784. for (j = 0; j < 256; j++){
  785. ColorError2=(long)(Blue-pPal->palPalEntry[j].peBlue)*(Blue-pPal->palPalEntry[j].peBlue)+ 
  786.                         (long)(Green-pPal->palPalEntry[j].peGreen)*(Green-pPal->palPalEntry[j].peGreen)+
  787.                         (long)(Red-pPal->palPalEntry[j].peRed)*(Red-pPal->palPalEntry[j].peRed); 
  788.                 if (ColorError2 < ColorError1){
  789. ColorError1 = ColorError2;
  790. ClrIndex = j;
  791.                 }
  792. }                        
  793. ColorHits[i] = ClrIndex;
  794. }
  795. }                
  796. if(hPalette!=NULL)                     
  797.         DeleteObject(hPalette);
  798. //create new logic palette
  799. hPalette=CreatePalette(pPal);
  800. LocalUnlock(hPal);
  801. LocalFree(hPal);
  802. hDc=GetDC(hWnd);
  803. if(hPalette){
  804.         hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  805. RealizePalette(hDc);
  806. }
  807. for(y=0;y<bi.biHeight;y++){
  808. lpPtr=(char *)lpImgData+(SrcBufSize-LineBytes-y*LineBytes);
  809. lpTempPtr=(char *)lpTempImgData+(DstBufSize-DstLineBytes-y*DstLineBytes);
  810. for(x=0;x<bi.biWidth;x++){
  811. Blue=(int)(*(lpPtr++) & 0xf0);
  812. Green=(int)(*(lpPtr++) & 0xf0);
  813. Red=(int)(*(lpPtr++) & 0xf0);
  814. ClrIndex=(Blue<<4) + Green +(Red >>4);
  815.             for (i = 0; i < PalCounts;i++)
  816. if (ClrIndex == ColorIndex[i]){
  817. *(lpTempPtr++)=(unsigned char)ColorHits[i];
  818.                     break;
  819. }
  820. }
  821. }
  822.     if(hBitmap!=NULL)
  823.     DeleteObject(hBitmap);
  824. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  825. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +256*sizeof(RGBQUAD),
  826.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  827. if(hPalette && hPrevPalette){
  828. SelectPalette(hDc,hPrevPalette,FALSE);
  829. RealizePalette(hDc);
  830. }
  831.     hf=_lcreat("c:\256.bmp",0);
  832. _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER)); 
  833. _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
  834. _lclose(hf);
  835.   ReleaseDC(hWnd,hDc);
  836. LocalUnlock(hTempImgData);
  837. LocalFree(hTempImgData);
  838. GlobalUnlock(hImgData);
  839. return TRUE;
  840. }
  841. ////////////////////////////////////////////////////////////////
  842. BOOL ContrastStretch(HWND hWnd)
  843. {
  844. DLGPROC            dlgInputBox = NULL;
  845. DWORD              BufSize;
  846.     LPBITMAPINFOHEADER lpImgData;
  847. LPSTR              lpPtr;
  848. HLOCAL             hTempImgData;
  849. LPBITMAPINFOHEADER lpTempImgData;
  850. LPSTR              lpTempPtr;
  851. HDC                hDc;
  852. HFILE              hf;
  853.     LOGPALETTE         *pPal;
  854.     HPALETTE           hPrevPalette=NULL; 
  855. HLOCAL             hPal;
  856. DWORD              i;
  857. unsigned char      Gray;
  858. float              a,g1,g2,g;
  859. if( NumColors!=256){
  860.      MessageBox(hWnd,"Must be a 256 grayscale bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  861.         return FALSE;
  862.     }
  863. dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
  864. DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
  865. FreeProcInstance ( (FARPROC) dlgInputBox );
  866. if( StretchRatio*(SecondPoint-FirstPoint) > 255.0){
  867.      MessageBox(hWnd,"StretchRatio*(SecondPoint-FirstPoint)  can not be larger than 255!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  868.         return FALSE;
  869.     }
  870. if( (SecondPoint-FirstPoint) >=255){
  871.      MessageBox(hWnd,"The area you selected can not be the whole scale!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  872.         return FALSE;
  873.     }
  874. a=(float)((255.0-StretchRatio*(SecondPoint-FirstPoint))/(255.0-(SecondPoint-FirstPoint))); 
  875. g1=a*FirstPoint;
  876. g2=StretchRatio*(SecondPoint-FirstPoint)+g1;
  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. //copy image data
  886. memcpy(lpTempImgData,lpImgData,BufSize);
  887. hDc=GetDC(hWnd);
  888. lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
  889. lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
  890. hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
  891. pPal =(LOGPALETTE *)LocalLock(hPal);
  892. pPal->palNumEntries =(WORD) NumColors;
  893. pPal->palVersion    = 0x300;
  894. for (i = 0; i < 256; i++) {
  895. Gray=(unsigned char )*lpPtr;
  896. lpPtr+=4;
  897. if(Gray<FirstPoint) g=(float)(a*Gray);
  898. else if (Gray<SecondPoint) g=g1+StretchRatio*(Gray-FirstPoint);
  899. else g=g2+a*(Gray-SecondPoint);
  900.     pPal->palPalEntry[i].peRed=(BYTE)g;
  901. pPal->palPalEntry[i].peGreen=(BYTE)g;
  902. pPal->palPalEntry[i].peBlue=(BYTE)g;
  903. pPal->palPalEntry[i].peFlags=0;
  904. *(lpTempPtr++)=(unsigned char)g;
  905. *(lpTempPtr++)=(unsigned char)g;
  906. *(lpTempPtr++)=(unsigned char)g;
  907. *(lpTempPtr++)=0;
  908. }
  909. if(hPalette!=NULL)                     
  910. DeleteObject(hPalette);
  911. hPalette=CreatePalette(pPal);
  912. LocalUnlock(hPal);
  913. LocalFree(hPal);
  914. if(hPalette){
  915. hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  916. RealizePalette(hDc);
  917. }
  918.     if(hBitmap!=NULL)
  919.     DeleteObject(hBitmap);
  920. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  921. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  922.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  923. if(hPalette && hPrevPalette){
  924. SelectPalette(hDc,hPrevPalette,FALSE);
  925. RealizePalette(hDc);
  926. }
  927.     hf=_lcreat("c:\stretch.bmp",0);
  928. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  929. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  930. _lclose(hf);
  931.   ReleaseDC(hWnd,hDc);
  932. LocalUnlock(hTempImgData);
  933. LocalFree(hTempImgData);
  934. GlobalUnlock(hImgData);
  935. return TRUE;
  936. }
  937. ////////////////////////////////////////////////////////////////
  938. BOOL Cliping(HWND hWnd)
  939. {
  940. DLGPROC            dlgInputBox = NULL;
  941. DWORD              BufSize;
  942.     LPBITMAPINFOHEADER lpImgData;
  943. LPSTR              lpPtr;
  944. HLOCAL             hTempImgData;
  945. LPBITMAPINFOHEADER lpTempImgData;
  946. LPSTR              lpTempPtr;
  947. HDC                hDc;
  948. HFILE              hf;
  949.     LOGPALETTE         *pPal;
  950.     HPALETTE           hPrevPalette=NULL; 
  951. HLOCAL             hPal;
  952. DWORD              i;
  953. unsigned char      Gray;
  954. float              a,g;
  955. if( NumColors!=256){
  956.      MessageBox(hWnd,"Must be a 256 grayscale bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  957.         return FALSE;
  958.     }
  959. dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
  960. DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
  961. FreeProcInstance ( (FARPROC) dlgInputBox );
  962. if( (SecondPoint-FirstPoint) >255){
  963.      MessageBox(hWnd,"The area you selected must be within [0-255]!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  964.         return FALSE;
  965.     }
  966. a=(float)(255.0/(SecondPoint-FirstPoint)); 
  967. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  968. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  969.     {
  970.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  971.         return FALSE;
  972.     }
  973.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  974. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  975. //copy image data
  976. memcpy(lpTempImgData,lpImgData,BufSize);
  977. hDc=GetDC(hWnd);
  978. lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
  979. lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
  980. hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
  981. pPal =(LOGPALETTE *)LocalLock(hPal);
  982. pPal->palNumEntries =(WORD) NumColors;
  983. pPal->palVersion    = 0x300;
  984. for (i = 0; i < 256; i++) {
  985. Gray=(unsigned char )*lpPtr;
  986. lpPtr+=4;
  987. if(Gray<FirstPoint) g=0.0f;
  988. else if (Gray<SecondPoint) g=a*(Gray-FirstPoint);
  989. else g=255.0f;
  990.     pPal->palPalEntry[i].peRed=(BYTE)g;
  991. pPal->palPalEntry[i].peGreen=(BYTE)g;
  992. pPal->palPalEntry[i].peBlue=(BYTE)g;
  993. pPal->palPalEntry[i].peFlags=0;
  994. *(lpTempPtr++)=(unsigned char)g;
  995. *(lpTempPtr++)=(unsigned char)g;
  996. *(lpTempPtr++)=(unsigned char)g;
  997. *(lpTempPtr++)=0;
  998. }
  999. if(hPalette!=NULL)                     
  1000. DeleteObject(hPalette);
  1001. hPalette=CreatePalette(pPal);
  1002. LocalUnlock(hPal);
  1003. LocalFree(hPal);
  1004. if(hPalette){
  1005. hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  1006. RealizePalette(hDc);
  1007. }
  1008.     if(hBitmap!=NULL)
  1009.     DeleteObject(hBitmap);
  1010. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  1011. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  1012.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  1013. if(hPalette && hPrevPalette){
  1014. SelectPalette(hDc,hPrevPalette,FALSE);
  1015. RealizePalette(hDc);
  1016. }
  1017.     hf=_lcreat("c:\cliping.bmp",0);
  1018. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  1019. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  1020. _lclose(hf);
  1021.   ReleaseDC(hWnd,hDc);
  1022. LocalUnlock(hTempImgData);
  1023. LocalFree(hTempImgData);
  1024. GlobalUnlock(hImgData);
  1025. return TRUE;
  1026. }
  1027. ////////////////////////////////////////////////////////////////
  1028. BOOL Thresholding(HWND hWnd)
  1029. {
  1030. DLGPROC            dlgInputBox = NULL;
  1031. DWORD              BufSize;
  1032.     LPBITMAPINFOHEADER lpImgData;
  1033. LPSTR              lpPtr;
  1034. HLOCAL             hTempImgData;
  1035. LPBITMAPINFOHEADER lpTempImgData;
  1036. LPSTR              lpTempPtr;
  1037. HDC                hDc;
  1038. HFILE              hf;
  1039.     LOGPALETTE         *pPal;
  1040.     HPALETTE           hPrevPalette=NULL; 
  1041. HLOCAL             hPal;
  1042. DWORD              i;
  1043. unsigned char      Gray;
  1044. if( NumColors!=256){
  1045.      MessageBox(hWnd,"Must be a 256 grayscale bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1046.         return FALSE;
  1047.     }
  1048. dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
  1049. DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
  1050. FreeProcInstance ( (FARPROC) dlgInputBox );
  1051. if( FirstPoint >255){
  1052.      MessageBox(hWnd,"The point you selected must be within [0-255]","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1053.         return FALSE;
  1054.     }
  1055. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  1056. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  1057.     {
  1058.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1059.         return FALSE;
  1060.     }
  1061.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  1062. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  1063. //copy image data
  1064. memcpy(lpTempImgData,lpImgData,BufSize);
  1065. hDc=GetDC(hWnd);
  1066. lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
  1067. lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
  1068. hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
  1069. pPal =(LOGPALETTE *)LocalLock(hPal);
  1070. pPal->palNumEntries =(WORD) NumColors;
  1071. pPal->palVersion    = 0x300;
  1072. for (i = 0; i < 256; i++) {
  1073. Gray=(unsigned char )*lpPtr;
  1074. lpPtr+=4;
  1075. if(Gray<FirstPoint) Gray=0;
  1076. else Gray=255;
  1077.     pPal->palPalEntry[i].peRed=Gray;
  1078. pPal->palPalEntry[i].peGreen=Gray;
  1079. pPal->palPalEntry[i].peBlue=Gray;
  1080. pPal->palPalEntry[i].peFlags=0;
  1081. *(lpTempPtr++)=(unsigned char)Gray;
  1082. *(lpTempPtr++)=(unsigned char)Gray;
  1083. *(lpTempPtr++)=(unsigned char)Gray;
  1084. *(lpTempPtr++)=0;
  1085. }
  1086. if(hPalette!=NULL)                     
  1087. DeleteObject(hPalette);
  1088. hPalette=CreatePalette(pPal);
  1089. LocalUnlock(hPal);
  1090. LocalFree(hPal);
  1091. if(hPalette){
  1092. hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  1093. RealizePalette(hDc);
  1094. }
  1095.     if(hBitmap!=NULL)
  1096.     DeleteObject(hBitmap);
  1097. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  1098. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  1099.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  1100. if(hPalette && hPrevPalette){
  1101. SelectPalette(hDc,hPrevPalette,FALSE);
  1102. RealizePalette(hDc);
  1103. }
  1104.     hf=_lcreat("c:\threshold.bmp",0);
  1105. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  1106. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  1107. _lclose(hf);
  1108.   ReleaseDC(hWnd,hDc);
  1109. LocalUnlock(hTempImgData);
  1110. LocalFree(hTempImgData);
  1111. GlobalUnlock(hImgData);
  1112. return TRUE;
  1113. }
  1114. ////////////////////////////////////////////////////////////////
  1115. BOOL Slice(HWND hWnd,BOOL WithBack)
  1116. {
  1117. DLGPROC            dlgInputBox = NULL;
  1118. DWORD              BufSize;
  1119.     LPBITMAPINFOHEADER lpImgData;
  1120. LPSTR              lpPtr;
  1121. HLOCAL             hTempImgData;
  1122. LPBITMAPINFOHEADER lpTempImgData;
  1123. LPSTR              lpTempPtr;
  1124. HDC                hDc;
  1125. HFILE              hf;
  1126.     LOGPALETTE         *pPal;
  1127.     HPALETTE           hPrevPalette=NULL; 
  1128. HLOCAL             hPal;
  1129. DWORD              i;
  1130. unsigned char      Gray;
  1131. if( NumColors!=256){
  1132.      MessageBox(hWnd,"Must be a 256 grayscale bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1133.         return FALSE;
  1134.     }
  1135. dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
  1136. DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
  1137. FreeProcInstance ( (FARPROC) dlgInputBox );
  1138. if( (SecondPoint-FirstPoint) >255){
  1139.      MessageBox(hWnd,"The area you selected must be within [0-255]!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1140.         return FALSE;
  1141.     }
  1142. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  1143. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  1144.     {
  1145.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1146.         return FALSE;
  1147.     }
  1148.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  1149. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  1150. //copy image data
  1151. memcpy(lpTempImgData,lpImgData,BufSize);
  1152. hDc=GetDC(hWnd);
  1153. lpPtr=(char *)lpImgData+sizeof(BITMAPINFOHEADER);
  1154. lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
  1155. hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
  1156. pPal =(LOGPALETTE *)LocalLock(hPal);
  1157. pPal->palNumEntries =(WORD) NumColors;
  1158. pPal->palVersion    = 0x300;
  1159. for (i = 0; i < 256; i++) {
  1160. Gray=(unsigned char )*lpPtr;
  1161. lpPtr+=4;
  1162. if(WithBack){
  1163. if( (Gray>=FirstPoint) && (Gray<=SecondPoint))
  1164. Gray=255;
  1165. }
  1166. else{
  1167. if(Gray<FirstPoint) Gray=0;
  1168. else if (Gray<SecondPoint) Gray=255;
  1169. else Gray=0;
  1170. }
  1171.     pPal->palPalEntry[i].peRed=Gray;
  1172. pPal->palPalEntry[i].peGreen=Gray;
  1173. pPal->palPalEntry[i].peBlue=Gray;
  1174. pPal->palPalEntry[i].peFlags=0;
  1175. *(lpTempPtr++)=(unsigned char)Gray;
  1176. *(lpTempPtr++)=(unsigned char)Gray;
  1177. *(lpTempPtr++)=(unsigned char)Gray;
  1178. *(lpTempPtr++)=0;
  1179. }
  1180. if(hPalette!=NULL)                     
  1181. DeleteObject(hPalette);
  1182. hPalette=CreatePalette(pPal);
  1183. LocalUnlock(hPal);
  1184. LocalFree(hPal);
  1185. if(hPalette){
  1186. hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  1187. RealizePalette(hDc);
  1188. }
  1189.     if(hBitmap!=NULL)
  1190.     DeleteObject(hBitmap);
  1191. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  1192. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  1193.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  1194. if(hPalette && hPrevPalette){
  1195. SelectPalette(hDc,hPrevPalette,FALSE);
  1196. RealizePalette(hDc);
  1197. }
  1198. if(WithBack)
  1199. hf=_lcreat("c:\sliceb.bmp",0);
  1200. else
  1201. hf=_lcreat("c:\slice.bmp",0);
  1202. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  1203. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  1204. _lclose(hf);
  1205.   ReleaseDC(hWnd,hDc);
  1206. LocalUnlock(hTempImgData);
  1207. LocalFree(hTempImgData);
  1208. GlobalUnlock(hImgData);
  1209. return TRUE;
  1210. }
  1211. ////////////////////////////////////////////////////////////////
  1212. BOOL HistogramEqua(HWND hWnd)
  1213. {
  1214. DLGPROC            dlgInputBox = NULL;
  1215. DWORD              BufSize,OffBits;
  1216.     LPBITMAPINFOHEADER lpImgData;
  1217. LPSTR              lpPtr;
  1218. HLOCAL             hTempImgData;
  1219. LPBITMAPINFOHEADER lpTempImgData;
  1220. LPSTR              lpTempPtr;
  1221. HDC                hDc;
  1222. HFILE              hf;
  1223. LONG               x,y;
  1224.     LOGPALETTE         *pPal;
  1225.     HPALETTE           hPrevPalette; 
  1226. HLOCAL             hPal;
  1227. WORD               i;
  1228. int      Gray;
  1229. DWORD              GrayHits[256];
  1230. int    GrayIndex[256];
  1231. float              s[256];
  1232. if( NumColors!=256){
  1233.      MessageBox(hWnd,"Must be a 256 grayscale bitmap!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1234.         return FALSE;
  1235.     }
  1236. dlgInputBox = (DLGPROC) MakeProcInstance ( (FARPROC)InputBox, ghInst );
  1237. DialogBox (ghInst, "INPUTBOX", hWnd, dlgInputBox);
  1238. FreeProcInstance ( (FARPROC) dlgInputBox );
  1239. if( EquaScale >=255){
  1240.      MessageBox(hWnd,"The new scale can not be larger than 255","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1241.         return FALSE;
  1242.     }
  1243. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  1244. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  1245. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  1246.     {
  1247.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  1248.         return FALSE;
  1249.     }
  1250.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  1251. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  1252. //copy image data
  1253. memcpy(lpTempImgData,lpImgData,OffBits);
  1254. memset(GrayHits,0,256*sizeof(DWORD));
  1255. memset(GrayIndex,0,256*sizeof(WORD));
  1256. for(y=0;y<bi.biHeight;y++){
  1257. lpPtr=(unsigned char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
  1258. for(x=0;x<bi.biWidth;x++){
  1259. Gray=(unsigned char )*(lpPtr++);
  1260. GrayHits[Gray]++;
  1261. }
  1262. }
  1263. for(i=0;i<256;i++)
  1264. s[i]=(float)GrayHits[i]/((float)bi.biWidth*(float)bi.biHeight);
  1265. for(i=1;i<256;i++)
  1266. s[i]+=s[i-1];
  1267. for(i=0;i<256;i++)
  1268. GrayIndex[i]=(int)(s[i]*(EquaScale-1));
  1269.     hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + 256* sizeof(PALETTEENTRY));
  1270.     pPal =(LOGPALETTE *)LocalLock(hPal);
  1271. memset(pPal,0,sizeof(LOGPALETTE) + 256* sizeof(PALETTEENTRY));
  1272.     pPal->palNumEntries =(WORD) 256;
  1273. pPal->palVersion    = 0x300;
  1274. lpTempPtr=(char *)lpTempImgData+sizeof(BITMAPINFOHEADER);
  1275. for (i = 0; i < EquaScale; i++) {
  1276. Gray=(int)(i*255.0/(EquaScale-1));
  1277.     pPal->palPalEntry[i].peRed=(BYTE)Gray;
  1278. pPal->palPalEntry[i].peGreen=(BYTE)Gray;
  1279. pPal->palPalEntry[i].peBlue=(BYTE)Gray;
  1280. pPal->palPalEntry[i].peFlags=(BYTE)0;
  1281. *(lpTempPtr++)=(unsigned char)Gray;
  1282. *(lpTempPtr++)=(unsigned char)Gray;
  1283. *(lpTempPtr++)=(unsigned char)Gray;
  1284. *(lpTempPtr++)=0;
  1285. }
  1286. if(hPalette!=NULL)                     
  1287.         DeleteObject(hPalette);
  1288. //create new logic palette
  1289. hPalette=CreatePalette(pPal);
  1290. LocalUnlock(hPal);
  1291. LocalFree(hPal);
  1292. hDc=GetDC(hWnd);
  1293. if(hPalette){
  1294.         hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  1295. RealizePalette(hDc);
  1296. }
  1297. for(y=0;y<bi.biHeight;y++){
  1298. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
  1299. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
  1300. for(x=0;x<bi.biWidth;x++){
  1301. Gray=(unsigned char )*(lpPtr++);
  1302. Gray=GrayIndex[Gray];
  1303. *(lpTempPtr++)=(unsigned char)Gray;
  1304. }
  1305. }
  1306.     if(hBitmap!=NULL)
  1307.     DeleteObject(hBitmap);
  1308. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  1309. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +256*sizeof(RGBQUAD),
  1310.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  1311. if(hPalette && hPrevPalette){
  1312. SelectPalette(hDc,hPrevPalette,FALSE);
  1313. RealizePalette(hDc);
  1314. }
  1315.     hf=_lcreat("c:\equa.bmp",0);
  1316. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  1317. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  1318. _lclose(hf);
  1319.   ReleaseDC(hWnd,hDc);
  1320. LocalUnlock(hTempImgData);
  1321. LocalFree(hTempImgData);
  1322. GlobalUnlock(hImgData);
  1323. return TRUE;
  1324. }