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

图形图像处理

开发平台:

Visual C++

  1. //////////////////////////////////////////////////////////////
  2. //Name:dither.c
  3. //Purpose: Pattern and Dithering
  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 LimbPatternM3(HWND hWnd);
  19. BOOL LimbPatternM4(HWND hWnd);
  20. BOOL Steinberg(HWND hWnd);
  21. BOOL Bmp2Txt(HWND hWnd);
  22. //global variable declaration
  23. BITMAPFILEHEADER   bf;
  24. BITMAPINFOHEADER   bi;
  25. HPALETTE           hPalette=NULL;
  26. HBITMAP            hBitmap=NULL;
  27. HGLOBAL            hImgData=NULL;
  28. DWORD              NumColors;
  29. DWORD              LineBytes;
  30. HINSTANCE          ghInst;
  31. DWORD              ImgWidth=0 , ImgHeight=0;
  32. //template array
  33. BYTE BayerPattern[8][8]={0,32,8,40,2,34,10,42,
  34.  48,16,56,24,50,18,58,26,
  35.  12,44,4,36,14,46,6,38,
  36.  60,28,52,20,62,30,54,22,
  37.  3,35,11,43,1,33,9,41,
  38.  51,19,59,27,49,17,57,25,
  39.  15,47,7,39,13,45,5,37,
  40.  63,31,55,23,61,29,53,21};
  41. static char ch[95]= {
  42. ' ',
  43. '`','1','2','3','4','5','6','7','8','9','0','-','=','\',
  44. 'q','w','e','r','t','y','u','i','o','p','[',']',
  45. 'a','s','d','f','g','h','j','k','l',';',''',
  46. 'z','x','c','v','b','n','m',',','.','/',
  47. '~','!','@','#','$','%','^','&','*','(',')','_','+','|',
  48. 'Q','W','E','R','T','Y','U','I','O','P','{','}',
  49. 'A','S','D','F','G','H','J','K','L',':','"',
  50. 'Z','X','C','V','B','N','M','<','>','?'
  51. };
  52. static int  gr[95]= {
  53.  0,
  54.  7,22,28,31,31,27,32,22,38,32,40, 6,12,20,38,32,26,20,24,40,
  55.     29,24,28,38,32,32,26,22,34,24,44,33,32,32,24,16, 6,22,26,22,
  56.  26,34,29,35,10, 6,20,14,22,47,42,34,40,10,35,21,22,22,16,14,
  57.  26,40,39,29,38,22,28,36,22,36,30,22,22,36,26,36,25,34,38,24,
  58.  36,22,12,12,26,30,30,34,39,42,41,18,18,22
  59.  };
  60. ///////////////////////////////////////////////////////////
  61. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  62.     LPSTR lpszCmdLine, int nCmdShow)
  63. {
  64. MSG       msg;
  65. WNDCLASS  wndclass;
  66. HWND      hWnd;
  67. ghInst=hInstance;
  68. if ( ! hPrevInstance ){
  69. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  70. wndclass.lpfnWndProc = MainWndProc;
  71. wndclass.cbClsExtra = 0;
  72. wndclass.cbWndExtra = 0;
  73. wndclass.hInstance = hInstance;
  74. wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  75. wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  76. wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  77. wndclass.lpszMenuName =  "BMPMENU";
  78. wndclass.lpszClassName = "phoenix ip system";
  79.     }
  80.     if ( ! RegisterClass (&wndclass) )
  81.     return FALSE;
  82. hWnd = CreateWindow ("phoenix ip system","Pattern and Dithering",
  83.  WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
  84.  CW_USEDEFAULT, CW_USEDEFAULT, NULL,NULL,
  85.  hInstance, NULL);
  86. if (!hWnd)
  87. return FALSE;
  88. ShowWindow (hWnd, SW_SHOWMAXIMIZED);
  89. UpdateWindow (hWnd);
  90. while ( GetMessage (&msg, NULL, 0, 0) ){
  91. TranslateMessage (&msg);
  92. DispatchMessage (&msg);
  93.     }
  94. return msg.wParam;
  95. }
  96. ////////////////////////////////////////////////////////////////
  97. LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam)
  98. {
  99.     static  HDC    hDC,hMemDC;
  100.     PAINTSTRUCT    ps;
  101.     switch (message){
  102.     case WM_PAINT:
  103. {         
  104. hDC = BeginPaint(hWnd, &ps);
  105. if (hBitmap)
  106. hMemDC = CreateCompatibleDC(hDC);
  107. if (hPalette)
  108. {           
  109. SelectPalette (hDC, hPalette, FALSE); 
  110. SelectPalette (hMemDC, hPalette, FALSE);
  111. RealizePalette (hDC);
  112. }   
  113. SelectObject(hMemDC, hBitmap); 
  114. BitBlt(hDC, 0, 0, ImgWidth,ImgHeight, hMemDC, 0, 0, SRCCOPY);
  115. DeleteDC(hMemDC);
  116. }
  117. EndPaint(hWnd, &ps);
  118. break;
  119. }
  120. case WM_DESTROY: //注意释放内存和位图,调色板句柄
  121.         if(hBitmap!=NULL)
  122.         DeleteObject(hBitmap);
  123.     
  124. if(hPalette!=NULL)                     
  125.         DeleteObject(hPalette);
  126. if(hImgData!=NULL){
  127. GlobalUnlock(hImgData);
  128. GlobalFree(hImgData);
  129. }         
  130.     PostQuitMessage (0);
  131.     return 0;
  132. case WM_COMMAND:
  133.     switch (wParam){
  134.     case IDM_LOADBMP: 
  135. //注意重新分配内存和调色板,位图句柄时,先释放原来的
  136.         if(hBitmap!=NULL){     
  137. DeleteObject(hBitmap);
  138.             hBitmap=NULL;
  139.         }
  140.         if(hPalette!=NULL){                     
  141. DeleteObject(hPalette);
  142.             hPalette=NULL;
  143.         }
  144. if(hImgData!=NULL){
  145. GlobalUnlock(hImgData);
  146. GlobalFree(hImgData);  
  147. hImgData=NULL;
  148. }         
  149. if(LoadBmpFile(hWnd,"c:\test.bmp")) //成功,则重画窗口
  150.                 InvalidateRect(hWnd,NULL,TRUE);
  151.         break;
  152. case IDM_LIMBPATTERNM3:
  153. if(hImgData!=NULL){
  154. if(LimbPatternM3(hWnd))
  155. InvalidateRect(hWnd,NULL,TRUE);
  156. }
  157. else
  158. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  159. break;
  160. case IDM_LIMBPATTERNM4:
  161. if(hImgData!=NULL){
  162. if(LimbPatternM4(hWnd))
  163. InvalidateRect(hWnd,NULL,TRUE);
  164. }
  165. else
  166. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  167. break;
  168. case IDM_STEINBERG:
  169. if(hImgData!=NULL){
  170. if(Steinberg(hWnd));
  171. InvalidateRect(hWnd,NULL,TRUE);
  172. }
  173. else
  174. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  175. break;
  176. case IDM_BMP2TXT:
  177. if(hImgData!=NULL){
  178. if(Bmp2Txt(hWnd))
  179.      MessageBox(hWnd,"Finished!","Success Message",MB_OK);
  180. }
  181. else
  182. MessageBox(hWnd,"File not loaded yet!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  183. break;
  184. case IDM_EXIT:
  185.         SendMessage(hWnd,WM_DESTROY,0,0L);
  186.         break;
  187.     }
  188. break;                
  189.     }
  190.     return DefWindowProc (hWnd, message, wParam, lParam);
  191. }
  192. ////////////////////////////////////////////////////////////////
  193. BOOL LoadBmpFile (HWND hWnd,char *BmpFileName)
  194. {   
  195.     HFILE              hf;
  196.     LPBITMAPINFOHEADER lpImgData;
  197.     LOGPALETTE         *pPal;
  198.     LPRGBQUAD          lpRGB;
  199.     HPALETTE           hPrevPalette; 
  200.     HDC                hDc;
  201. HLOCAL             hPal;
  202. DWORD         ImgSize;
  203. DWORD              i;
  204.     if((hf=_lopen(BmpFileName,OF_READ))==HFILE_ERROR){
  205.         MessageBox(hWnd,"File c:\test.bmp not found!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  206.         return FALSE;
  207. }
  208. _lread(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  209. _lread(hf,(LPSTR)&bi,sizeof(BITMAPINFOHEADER));
  210. ImgWidth=bi.biWidth;
  211. ImgHeight=bi.biHeight;
  212. LineBytes=(DWORD)WIDTHBYTES(bi.biWidth*bi.biBitCount);
  213. ImgSize=(DWORD)LineBytes*bi.biHeight;
  214.     if(bi.biClrUsed!=0)
  215. NumColors=(DWORD)bi.biClrUsed;
  216. else
  217.         switch(bi.biBitCount){
  218.         case 1:
  219.              NumColors=2;
  220.              break;
  221.          case 4:
  222.              NumColors=16;
  223.              break;
  224.          case 8:
  225.              NumColors=256;
  226.              break;
  227.          case 24:
  228.              NumColors=0;
  229.              break;
  230.               default:
  231.                   MessageBox(hWnd,"Invalid color numbers!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  232.                   _lclose(hf);
  233.                   return FALSE; 
  234.         }
  235. if(bf.bfOffBits!=(DWORD)(NumColors*sizeof(RGBQUAD)+sizeof(BITMAPFILEHEADER)
  236. +sizeof(BITMAPINFOHEADER)))
  237. {
  238.      MessageBox(hWnd,"Invalid color numbers!","Error Message" ,MB_OK|
  239.                MB_ICONEXCLAMATION);
  240. _lclose(hf);
  241. return FALSE; 
  242. }
  243. bf.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+ImgSize;
  244. if((hImgData=GlobalAlloc(GHND,(DWORD)(sizeof(BITMAPINFOHEADER)+
  245.      NumColors*sizeof(RGBQUAD)+ImgSize)))==NULL)
  246. {
  247.      MessageBox(hWnd,"Error alloc memory!","ErrorMessage",MB_OK|
  248.                    MB_ICONEXCLAMATION);
  249.     _lclose(hf);
  250. return FALSE;
  251. }
  252.   
  253. lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData); 
  254.     _llseek(hf,sizeof(BITMAPFILEHEADER),FILE_BEGIN);
  255. _hread(hf,(char *)lpImgData,(long)sizeof(BITMAPINFOHEADER)
  256.            +(long)NumColors*sizeof(RGBQUAD)+ImgSize);
  257. _lclose(hf);
  258.     if(NumColors!=0)
  259. {                    
  260.     hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
  261.     pPal =(LOGPALETTE *)LocalLock(hPal);
  262.     pPal->palNumEntries =(WORD) NumColors;
  263. pPal->palVersion    = 0x300;
  264.     lpRGB = (LPRGBQUAD)((LPSTR)lpImgData + (DWORD)sizeof(BITMAPINFOHEADER));
  265. for (i = 0; i < NumColors; i++) {
  266.       pPal->palPalEntry[i].peRed=lpRGB->rgbRed;
  267. pPal->palPalEntry[i].peGreen=lpRGB->rgbGreen;
  268. pPal->palPalEntry[i].peBlue=lpRGB->rgbBlue;
  269. pPal->palPalEntry[i].peFlags=(BYTE)0;
  270. lpRGB++;
  271. }
  272. hPalette=CreatePalette(pPal);
  273. LocalUnlock(hPal);
  274. LocalFree(hPal);
  275. }
  276. hDc=GetDC(hWnd);
  277. if(hPalette){
  278.         hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
  279. RealizePalette(hDc);
  280. }
  281. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpImgData, (LONG)CBM_INIT,
  282. (LPSTR)lpImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  283.     (LPBITMAPINFO)lpImgData, DIB_RGB_COLORS);
  284. if(hPalette && hPrevPalette){
  285. SelectPalette(hDc,hPrevPalette,FALSE);
  286. RealizePalette(hDc);
  287. }
  288.  
  289. ReleaseDC(hWnd,hDc);
  290. GlobalUnlock(hImgData);
  291. return TRUE; 
  292. }
  293. ////////////////////////////////////////////////////////////////
  294. BOOL LimbPatternM3(HWND hWnd)
  295. {
  296. DWORD              BufSize;
  297.     LPBITMAPINFOHEADER lpImgData;
  298. LPSTR              lpPtr;
  299. HLOCAL             hTempImgData;
  300. LPBITMAPINFOHEADER lpTempImgData;
  301. LPSTR              lpTempPtr;
  302. HDC                hDc;
  303. HFILE              hf;
  304. LONG               x,y;
  305. unsigned char      num;
  306. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  307. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  308.     {
  309.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  310.         return FALSE;
  311.     }
  312.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  313. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  314. //copy image data
  315. memcpy(lpTempImgData,lpImgData,BufSize);
  316. for(y=0;y<bi.biHeight;y++){
  317. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
  318. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
  319. for(x=0;x<bi.biWidth;x++){
  320. num=(unsigned char)*lpPtr++;
  321. if ( (num>>2) > BayerPattern[y&7][x&7])
  322. *(lpTempPtr++)=(unsigned char)255;
  323. else *(lpTempPtr++)=(unsigned char)0;
  324. }
  325. }
  326.     if(hBitmap!=NULL)
  327.     DeleteObject(hBitmap);
  328. hDc=GetDC(hWnd);
  329. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  330. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  331.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  332.     hf=_lcreat("c:\limbm3.bmp",0);
  333. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  334. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  335. _lclose(hf);
  336.   ReleaseDC(hWnd,hDc);
  337. LocalUnlock(hTempImgData);
  338. LocalFree(hTempImgData);
  339. GlobalUnlock(hImgData);
  340. return TRUE;
  341. }
  342. ////////////////////////////////////////////////////////////////
  343. BOOL LimbPatternM4(HWND hWnd)
  344. {
  345. DWORD              BufSize;
  346.     LPBITMAPINFOHEADER lpImgData;
  347. LPSTR              lpPtr;
  348. HLOCAL             hTempImgData;
  349. LPBITMAPINFOHEADER lpTempImgData;
  350. LPSTR              lpTempPtr;
  351. HDC                hDc;
  352. HFILE              hf;
  353. LONG               x,y;
  354. DWORD              i,j;
  355. unsigned char      num;
  356. BYTE    M4Pattern[16][16];
  357. for(i=0;i<8;i++)
  358. for(j=0;j<8;j++)
  359. M4Pattern[i][j]=4*BayerPattern[i][j];
  360. for(i=8;i<16;i++)
  361. for(j=0;j<8;j++)
  362. M4Pattern[i][j]=4*BayerPattern[i-8][j]+2;
  363. for(i=0;i<8;i++)
  364. for(j=8;j<16;j++)
  365. M4Pattern[i][j]=4*BayerPattern[i][j-8]+3;
  366. for(i=8;i<16;i++)
  367. for(j=8;j<16;j++)
  368. M4Pattern[i][j]=4*BayerPattern[i-8][j-8]+1;
  369. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  370. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  371.     {
  372.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  373.         return FALSE;
  374.     }
  375.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  376. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  377. //copy image data
  378. memcpy(lpTempImgData,lpImgData,BufSize);
  379. for(y=0;y<bi.biHeight;y++){
  380. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
  381. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
  382. for(x=0;x<bi.biWidth;x++){
  383. num=(unsigned char)*lpPtr++;
  384. if ( num > M4Pattern[y&15][x&15])
  385. *(lpTempPtr++)=(unsigned char)255;
  386. else *(lpTempPtr++)=(unsigned char)0;
  387. }
  388. }
  389.     if(hBitmap!=NULL)
  390.     DeleteObject(hBitmap);
  391. hDc=GetDC(hWnd);
  392. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  393. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  394.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  395.     hf=_lcreat("c:\limbm4.bmp",0);
  396. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  397. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  398. _lclose(hf);
  399.   ReleaseDC(hWnd,hDc);
  400. LocalUnlock(hTempImgData);
  401. LocalFree(hTempImgData);
  402. GlobalUnlock(hImgData);
  403. return TRUE;
  404. }
  405. ////////////////////////////////////////////////////////////////
  406. BOOL Steinberg(HWND hWnd)
  407. {
  408. DWORD              OffBits,BufSize,IntBufSize;
  409.     LPBITMAPINFOHEADER lpImgData;
  410. HLOCAL             hTempImgData;
  411. LPBITMAPINFOHEADER lpTempImgData;
  412. LPSTR              lpPtr;
  413. LPSTR              lpTempPtr;
  414. HDC                hDc;
  415. HFILE              hf;
  416. LONG               x,y;
  417. unsigned char      num;
  418. float              e,f;
  419. HLOCAL             hIntBuf;
  420. int                *lpIntBuf,*lpIntPtr;
  421. int                tempnum;
  422. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  423. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  424. if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
  425.     {
  426.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  427.         return FALSE;
  428.     }
  429. IntBufSize=(DWORD)bi.biHeight*LineBytes*sizeof(int);
  430. if((hIntBuf=LocalAlloc(LHND,IntBufSize))==NULL)
  431.     {
  432.      MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
  433. LocalFree(hTempImgData);
  434.         return FALSE;
  435.     }
  436.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);    
  437. lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
  438. lpIntBuf=(int *)LocalLock(hIntBuf);
  439. //copy header data
  440. memcpy(lpTempImgData,lpImgData,OffBits);
  441. //copy image data to int buffer
  442. for(y=0;y<bi.biHeight;y++){
  443. lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
  444. lpIntPtr=(int *)lpIntBuf+(bi.biHeight-1-y)*LineBytes;
  445. for(x=0;x<bi.biWidth;x++)
  446. *(lpIntPtr++)=(unsigned char)*(lpPtr++);
  447. }
  448. for(y=0;y<bi.biHeight;y++){  
  449. for(x=0;x<bi.biWidth;x++){
  450. lpIntPtr=(int *)lpIntBuf+(bi.biHeight-1-y)*LineBytes+x;
  451. num=(unsigned char)*lpIntPtr;
  452. if ( num > 128 ){
  453. *lpIntPtr=255;
  454. e=(float)(num-255.0);
  455. }
  456. else{
  457. *lpIntPtr=0;
  458. e=(float)num;
  459. }
  460. if(x<bi.biWidth-1){
  461. f=(float)*(lpIntPtr+1);
  462. f+=(float)( (3.0/8.0) * e);
  463. *(lpIntPtr+1)=(int)f;
  464. }
  465. if(y<bi.biHeight-1){
  466. f=(float)*(lpIntPtr-LineBytes);
  467. f+=(float)( (3.0/8.0) * e);
  468. *(lpIntPtr-LineBytes)=(int)f;
  469. f=(float)*(lpIntPtr-LineBytes+1);
  470. f+=(float)( (1.0/4.0) * e);
  471. *(lpIntPtr-LineBytes+1)=(int)f;
  472. }
  473. }
  474. }
  475. //copy new image data from int buffer
  476. for(y=0;y<bi.biHeight;y++){
  477. lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
  478. lpIntPtr=(int *)lpIntBuf+(bi.biHeight-1-y)*LineBytes;
  479. for(x=0;x<bi.biWidth;x++){
  480. tempnum=*(lpIntPtr++);
  481. if(tempnum>255) tempnum=255;
  482. else if (tempnum<0) tempnum=0;
  483. *(lpTempPtr++)=(unsigned char)tempnum;
  484. }
  485. }
  486.     if(hBitmap!=NULL)
  487.     DeleteObject(hBitmap);
  488. hDc=GetDC(hWnd);
  489. hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
  490. (LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
  491.     (LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
  492.     hf=_lcreat("c:\steinberg.bmp",0);
  493. _lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER)); 
  494. _lwrite(hf,(LPSTR)lpTempImgData,BufSize);
  495. _lclose(hf);
  496.   ReleaseDC(hWnd,hDc);
  497. GlobalUnlock(hImgData);
  498. LocalUnlock(hTempImgData);
  499. LocalFree(hTempImgData);
  500. LocalUnlock(hIntBuf);
  501. LocalFree(hIntBuf);
  502. return TRUE;
  503. }
  504. ////////////////////////////////////////////////////////////////
  505. BOOL Bmp2Txt(HWND hWnd)
  506. {
  507. DWORD              OffBits,BufSize;
  508.     LPBITMAPINFOHEADER lpImgData;
  509. LPSTR              lpPtr;
  510. HFILE              hf;
  511. int    i, j, k,h,tint,grayindex;
  512. char               tchar;
  513. int                TransHeight, TransWidth;
  514. for(i=0;i<94;i++)
  515. for(j=i+1;j<95;j++){
  516. if(gr[i]>gr[j]){
  517. tchar=ch[i],tint=gr[i];
  518. ch[i]=ch[j],gr[i]=gr[j];
  519. ch[j]=tchar,gr[j]=tint;
  520. }
  521. }
  522. OffBits=bf.bfOffBits-sizeof(BITMAPFILEHEADER);
  523. BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
  524.     lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
  525. TransWidth = bi.biWidth/8;
  526. TransHeight = bi.biHeight/16;
  527.     hf=_lcreat("c:\bmp2txt.txt",0);
  528. for(i=0;i<TransHeight;i++){
  529. for(j=0;j<TransWidth;j++){
  530. grayindex=0;
  531. for(k=0;k<16;k++)
  532. for(h=0;h<8;h++){
  533. lpPtr=(char *)lpImgData+BufSize-LineBytes-(i*16+k)*LineBytes+j*8+h;
  534. grayindex+=(unsigned char)*lpPtr;
  535. }
  536. grayindex/=16*8;
  537. grayindex=gr[94]*grayindex/255;
  538. k=0;
  539. while(gr[k+1]<grayindex)
  540. k++;
  541. _lwrite(hf,(char *)&ch[k],sizeof(char)); 
  542. }
  543. tchar=(char)13;
  544. _lwrite(hf,(char *)&tchar,sizeof(char)); 
  545. tchar=(char)10;
  546. _lwrite(hf,(char *)&tchar,sizeof(char));
  547. }
  548. _lclose(hf);
  549. GlobalUnlock(hImgData);
  550. return TRUE;
  551. }