DrawPreView.cpp
上传用户:yatsl7111
上传日期:2007-01-08
资源大小:1433k
文件大小:11k
源码类别:

图形图象

开发平台:

Visual C++

  1. #include "StdAfx.h"
  2. #include "DrawPreView.h"
  3. #include "System.h"
  4. #include "draw.h"
  5. #include "Contrast.h"
  6. #include "EdgeEnhance.h"
  7. #include "Filter.h"
  8. #include "Resource.h"
  9. static int prev_width,prev_height,mem_size,Img_size;
  10. static RECT rt;
  11. // 清理内存函数,这里借用了临风的格式
  12. void ClearPreView()
  13. {
  14. EndDraw();
  15. Del(lptemp);
  16. Del(lpPreView);
  17. Del(lpPreBack);
  18. lptemp=NULL;
  19. lpPreView=NULL;
  20. lpPreBack=NULL;
  21. }
  22. // 预览窗口预览绘制函数
  23. void _fnSPE_DrawPreView(LPDRAWITEMSTRUCT lpInfo,int x,int y,int nStype)
  24. {
  25. WORD skip=lpProcInfo->sImageInfo.bitperpix/8;
  26. //求预览图片尺寸
  27. int rcw=lpInfo->rcItem.right-lpInfo->rcItem.left;
  28. int rch=lpInfo->rcItem.bottom-lpInfo->rcItem.top;
  29. if(lpPreView==NULL)
  30. {
  31. lpPreView=(LPBYTE)lpProcInfo->_psbdata;
  32. if(lpPreView==NULL)
  33. {
  34.     lpProcInfo->result=PR_MEMORYERR;
  35.     return;
  36. }
  37. if(lpProcInfo->sImageInfo.width < rcw || lpProcInfo->sImageInfo.height < rch)
  38. {
  39. if(lpProcInfo->sImageInfo.width >= rcw )
  40. {
  41.        prev_width=PREV_WIDTH;
  42. prev_height=lpProcInfo->sImageInfo.height;
  43. rt.left=0;
  44. rt.top=(rch-prev_height)/2;
  45. rt.right=rt.left+prev_width;
  46. rt.bottom=rt.top+prev_height;
  47. }
  48. else if(lpProcInfo->sImageInfo.height >= rch)
  49. {
  50.        prev_width=lpProcInfo->sImageInfo.width;
  51. prev_height=PREV_HEIGHT;
  52. rt.left=(rcw-prev_width)/2;
  53. rt.top=0;
  54. rt.right=rt.left+prev_width;
  55. rt.bottom=rt.top+prev_height;
  56. }
  57. else
  58. {
  59. prev_width=lpProcInfo->sImageInfo.width;
  60. prev_height=lpProcInfo->sImageInfo.height;
  61. rt.left=(rcw-prev_width)/2;
  62. rt.top=(rch-prev_height)/2;
  63. rt.right=rt.left+prev_width;
  64. rt.bottom=rt.top+prev_height;
  65. }
  66. //创建预览内存图像
  67. mem_size=prev_height*prev_width*skip;
  68. lptemp=(LPBYTE)New(mem_size);
  69. if(lptemp==NULL)
  70. {
  71.     lpProcInfo->result=PR_MEMORYERR;
  72.     return;
  73. }
  74. lpPreBack=(LPBYTE)New(mem_size);
  75. if(lpPreBack==NULL)
  76. {
  77.     lpProcInfo->result=PR_MEMORYERR;
  78.     return;
  79. }
  80. if(lpProcInfo->sImageInfo.width >= rcw )
  81. {
  82. for(int i=0;i<prev_height;i++)
  83. {
  84. CopyMemory(lptemp+i*(PREV_WIDTH)*skip,lpProcInfo->_psbdata+i*lpProcInfo->sImageInfo.byteperline+x*skip,PREV_WIDTH*skip);
  85. }
  86. }
  87. else if(lpProcInfo->sImageInfo.height >= rch)
  88. {
  89. for(int j=0,i=y;i<PREV_HEIGHT+y;i++,j++)
  90. {
  91. CopyMemory(lptemp+j*(prev_width)*skip,lpProcInfo->_psbdata+i*lpProcInfo->sImageInfo.byteperline,prev_width*skip);
  92. }
  93. }
  94. else
  95. {
  96. //生成预览图像
  97. CopyMemory(lptemp,lpProcInfo->_psbdata ,mem_size);
  98. }
  99. }
  100. else
  101. {
  102.       prev_height=PREV_HEIGHT;
  103. prev_width=PREV_WIDTH;
  104.     rt.left=0;
  105.        rt.top=(prev_width-prev_height)/2;
  106. rt.right=prev_width;
  107. rt.bottom=rt.top+prev_height;
  108. //创建预览内存图像
  109. mem_size=prev_height*prev_width*skip;
  110. lptemp=(LPBYTE)New(mem_size);
  111. if(lptemp==NULL)
  112. {
  113.     lpProcInfo->result=PR_MEMORYERR;
  114.     return;
  115. }
  116. lpPreBack=(LPBYTE)New(mem_size);
  117. if(lpPreBack==NULL)
  118. {
  119.     lpProcInfo->result=PR_MEMORYERR;
  120.     return;
  121. }
  122. //生成预览图像
  123. for(int j=0,i=y;i<PREV_HEIGHT+y;i++,j++)
  124. {
  125. CopyMemory(lptemp+j*(PREV_WIDTH)*skip,lpProcInfo->_psbdata+i*lpProcInfo->sImageInfo.byteperline+x*skip ,(PREV_WIDTH)*skip);
  126. }
  127. }
  128. lpPreView=lptemp;
  129. CopyMemory(lpPreBack,lpPreView,mem_size);
  130. }
  131. DoDlgDraw(lpInfo->hDC,&rt,prev_width,prev_height,lpPreView);
  132. }
  133. // 预览窗口(缩比)绘制函数,这里借用了临风的格式
  134. void _fnSPE_DrawResizePreView(LPDRAWITEMSTRUCT lpInfo)
  135. {
  136. float x,y;
  137. if(lpPreView==NULL)
  138. {
  139.     lpPreView=(LPBYTE)lpProcInfo->_psbdata;
  140. if(lpPreView==NULL)
  141.        return;
  142. //求预览图片尺寸
  143. int rcw=lpInfo->rcItem.right-lpInfo->rcItem.left;
  144. int rch=lpInfo->rcItem.bottom-lpInfo->rcItem.top;
  145. x=(float)lpProcInfo->sImageInfo.width;
  146. y=(float)lpProcInfo->sImageInfo.height;
  147. if(lpProcInfo->sImageInfo.width > rcw || lpProcInfo->sImageInfo.height > rch)
  148. {
  149.        if(lpProcInfo->sImageInfo.width >= lpProcInfo->sImageInfo.height)
  150. {
  151.         prev_width=rcw;
  152.        x=y/x;
  153.           x=x*prev_width;
  154. if(x<8)
  155.              x=8;
  156. prev_height=(int)x;
  157.     rt.left=0;
  158.        rt.top=(prev_width-prev_height)/2;
  159.           rt.right=prev_width;
  160. rt.bottom=rt.top+prev_height;
  161.     }
  162.        else
  163. {
  164.     prev_height=rch;
  165.        x=x/y;
  166.       x=x*prev_height;
  167.     if(x<8)
  168. x=8;
  169. prev_width=(int)x;
  170.     rt.left=(prev_height-prev_width)/2;
  171.        rt.top=0;
  172.           rt.right=rt.left+prev_width;
  173.         rt.bottom=prev_height;
  174.     }
  175. }
  176. else
  177. {
  178.        prev_width=lpProcInfo->sImageInfo.width;
  179. prev_height=lpProcInfo->sImageInfo.height;
  180. rt.left=(rcw-prev_width)/2;
  181. rt.top=(rch-prev_height)/2;
  182. rt.right=rt.left+prev_width;
  183. rt.bottom=rt.top+prev_height;
  184. }
  185.    //创建prev_width*prev_height内存图像
  186. mem_size=prev_width*prev_height*(lpProcInfo->sImageInfo.bitperpix/8);
  187. lpPreBack=(LPBYTE)New(mem_size);
  188. if(lpPreBack==NULL)
  189. {
  190.     lpProcInfo->result=PR_MEMORYERR;
  191. // SendMessage(NULL ,WM_CLOSE,0,0);
  192.     return;
  193. }
  194. lptemp=(LPBYTE)New(mem_size);
  195. if(lptemp==NULL)
  196. {
  197.     lpProcInfo->result=PR_MEMORYERR;
  198. // SendMessage(NULL,WM_CLOSE,0,0);
  199.     return;
  200. }
  201. //生成预览图像
  202. _fnSPE_Output_Resize(prev_width,prev_height,(LPBYTE)lptemp);
  203. lpPreView=lptemp;
  204. CopyMemory(lpPreBack,lpPreView,mem_size);
  205.    }
  206. DoDlgDraw(lpInfo->hDC,&rt,prev_width,prev_height,lpPreView);
  207. }
  208. // 重定义尺寸,这里借用了临风的格式
  209. int _fnSPE_Output_Resize(int width,int height,LPBYTE lpDestData)
  210. {
  211. LPBYTE lpData=(LPBYTE)lpProcInfo->_psbdata,lpBak,lpBako;
  212. LPBYTE lpdData=lpDestData;
  213. if(lpDestData==NULL )
  214.     return PROCERR_FALSE;
  215. int skip = lpProcInfo->sImageInfo.bitperpix/8;
  216.     float x=(float)lpProcInfo->sImageInfo.width/(float)width;
  217.     float y=(float)lpProcInfo->sImageInfo.height/(float)height;
  218. float x_cnt,y_cnt=0.00;
  219.     lpBak=lpBako=lpData;
  220.     for(int j=0;j<height;j++)
  221.     {
  222. x_cnt=0.00;
  223.        for(int i=0;i<width;i++)
  224.         {
  225. for(int k=0;k<skip;k++)
  226.             {
  227. *lpdData=*lpData;
  228. lpdData++;
  229. lpData++;
  230. }
  231.             x_cnt+=x;
  232.         lpData=lpBak+skip*(int)x_cnt;
  233. }
  234. y_cnt+=y;
  235.       lpBak=lpBako+lpProcInfo->sImageInfo.width*skip*(int)y_cnt;
  236. lpData=lpBak;
  237. }
  238. return TRUE;
  239. }
  240. // 绘制预览图像(缩比),这里借用了临风的格式
  241. void _fnCOM_DrawResizePrView(int nCtlID,LPDRAWITEMSTRUCT lpInfo)
  242. {
  243. switch(nCtlID)
  244.     {
  245. case IDC_PRVIEW_CONTRAST:
  246. _fnSPE_DrawResizePreView(lpInfo);
  247.             return;
  248. case IDC_PRVIEW_EDGE:
  249. _fnSPE_DrawResizePreView(lpInfo);
  250.             return;
  251. case IDC_PRVIEW_FILTER:
  252. _fnSPE_DrawResizePreView(lpInfo);
  253. return;
  254. }
  255. }
  256. void _fnCOM_SetPos_xy(int x,int y)
  257. {
  258. nPos_x=x;
  259. nPos_y=y;
  260. }
  261. int _fnCOM_GetPosx()
  262. {
  263. return nPos_x;
  264. }
  265. int _fnCOM_GetPosy()
  266. {
  267. return nPos_y;
  268. }
  269. // 绘制预览图像,这里借用了临风的格式
  270. void _fnCOM_DrawPrView(int nCtlID,LPDRAWITEMSTRUCT lpInfo)
  271. {
  272. switch(nCtlID)
  273.     {
  274. case IDC_PRVIEW_CONTRAST:
  275. _fnSPE_DrawPreView(lpInfo,nPos_x,nPos_y,nStype);
  276.             return;
  277. case IDC_PRVIEW_EDGE:
  278. _fnSPE_DrawPreView(lpInfo,nPos_x,nPos_y,nStype);
  279.             return;
  280. case IDC_PRVIEW_FILTER:
  281. _fnSPE_DrawPreView(lpInfo,nPos_x,nPos_y,nStype);
  282.             return;
  283. }
  284. }
  285. // 预览图像的亮度,对比度调节处理函数
  286. void _fnCOM_AdjustContrastPreView(int Brightness,int Contrast)
  287. {
  288. CopyMemory(lpPreView,lpPreBack,mem_size);
  289. LPBYTE pRed=(LPBYTE)lpPreView;
  290. LPBYTE pGrn=pRed+1, pBlu=pRed+2;
  291. int skip=mem_size/prev_width/prev_height;
  292. for(UINT i=0;i<(UINT)prev_width;i++)
  293. {
  294. for(UINT j=0;j<(UINT)prev_height;j++)
  295. {
  296. _fnSPE_ChangeBrightness(Brightness,pRed,pGrn,pBlu);
  297. _fnSPE_ChangeContrast(Contrast,pRed,pGrn,pBlu);
  298.        pRed+=skip;
  299.        pGrn+=skip;
  300.        pBlu+=skip;
  301. }
  302. }
  303. }
  304. // 预览图像的亮度,对比度调节处理函数
  305. void _fnCOM_AdjustConvolutePreView(int nKernel,int nStrength,KERNEL* kernel)
  306. {
  307. CopyMemory(lpPreView,lpPreBack,mem_size);
  308. WORD skip=mem_size/prev_height;
  309. UINT i,j;
  310. for(i=1;i<(UINT)prev_height-1;i++)
  311. {
  312. for(j=1;j<(UINT)prev_width-1;j++)
  313. {
  314. int red=0,green=0,blue=0;
  315. for(int k=0;k<nKernel;++k)
  316. {
  317. int r=0,g=0,b=0;
  318. _fnSPE_Convolute(&r,&g,&b,i,j,skip,lpPreBack,kernel+k);
  319. if(r>red)
  320. red=r;
  321. if(g>green)
  322. green=g;
  323. if(b>blue)
  324. blue=b;
  325. }
  326. LONG lOffset=(LONG)(i*skip)+(LONG)(j*4);
  327. if(nStrength!=100)
  328. {
  329. BYTE Oldb=*(lpPreBack+lOffset++);
  330. BYTE Oldg=*(lpPreBack+lOffset++);
  331. BYTE Oldr=*(lpPreBack+lOffset);
  332. red=Oldr+(((red-Oldr)*nStrength)/100);
  333. green=Oldg+(((green-Oldg)*nStrength)/100);
  334. blue=Oldb+(((blue-Oldb)*nStrength)/100);
  335. lOffset=(LONG)(i*skip)+(LONG)(j*4);
  336. }
  337. *(lpPreView+lOffset++)=(BYTE)((blue)<(0)?(0):((blue)>(255)?(255):(blue)));
  338. *(lpPreView+lOffset++)=(BYTE)((green)<(0)?(0):((green)>(255)?(255):(green)));
  339. *(lpPreView+lOffset)=(BYTE)((red)<(0)?(0):((red)>(255)?(255):(red)));
  340. }
  341. }
  342. }
  343. // 预览图像的亮度,对比度调节处理函数
  344. void _fnCOM_AdjustEdgePreView(int nStrength,BOOL bAlgorith)
  345. {
  346. CopyMemory(lpPreView,lpPreBack,mem_size);
  347. WORD skip=mem_size/prev_height;
  348. UINT i,j;
  349. for(i=1;i<(UINT)prev_height-1;i++)
  350. {
  351. for(j=1;j<(UINT)prev_width-1;j++)
  352. {
  353. int red=0,green=0,blue=0;
  354. _fnSPE_Edge(&red,&green,&blue,i,j,skip,lpPreBack,nStrength,bAlgorith);
  355. LONG lOffset=(LONG)(i*skip)+(LONG)(j*4);
  356. *(lpPreView+lOffset++)=(BYTE)((blue)<(0)?(0):((blue)>(255)?(255):(blue)));
  357. *(lpPreView+lOffset++)=(BYTE)((green)<(0)?(0):((green)>(255)?(255):(green)));
  358. *(lpPreView+lOffset)=(BYTE)((red)<(0)?(0):((red)>(255)?(255):(red)));
  359. }
  360. }
  361. }
  362. void _fnCOM_MedianFilterPreView(int nPels,int nStype)
  363. {
  364. CopyMemory(lpPreView,lpPreBack,mem_size);
  365. WORD skip=mem_size/prev_height;
  366. UINT i,j;
  367. for(i=1;i<(UINT)prev_height-1-1;i++)
  368. {
  369. for(j=1;j<(UINT)prev_width-1;j++)
  370. {
  371. int red=0,green=0,blue=0;
  372. _fnSPE_MedianFilter(&red,&green,&blue,i,j,1,skip,lpPreBack,nStype);
  373. LONG lOffset=(LONG)(i*skip)+(LONG)(j*4);
  374. *(lpPreView+lOffset++)=(BYTE)((blue)<(0)?(0):((blue)>(255)?(255):(blue)));
  375. *(lpPreView+lOffset++)=(BYTE)((green)<(0)?(0):((green)>(255)?(255):(green)));
  376. *(lpPreView+lOffset)=(BYTE)((red)<(0)?(0):((red)>(255)?(255):(red)));
  377. }
  378. }
  379. }
  380. void _fnCOM_HighFilterPreView(int nStrength,int nStype)
  381. {
  382. CopyMemory(lpPreView,lpPreBack,mem_size);
  383. WORD skip=mem_size/prev_height;
  384. static DWORD dwFact[9];
  385. switch(nStype)
  386. {
  387. case 0:
  388. dwFact[0]=10;
  389. dwFact[1]=10;
  390. dwFact[2]=10;
  391. dwFact[3]=10;
  392. dwFact[4]=160+nStrength;
  393. dwFact[5]=10;
  394. dwFact[6]=10;
  395. dwFact[7]=10;
  396. dwFact[8]=10;
  397. break;
  398. case 1:
  399. dwFact[0]=10;
  400. dwFact[1]=10;
  401. dwFact[2]=10;
  402. dwFact[3]=10;
  403. dwFact[4]=10;
  404. dwFact[5]=10;
  405. dwFact[6]=10;
  406. dwFact[7]=10;
  407. dwFact[8]=10;
  408. break;
  409. }
  410. UINT i,j;
  411. for(i=1;i<(UINT)prev_height-1-1;i++)
  412. {
  413. for(j=1;j<(UINT)prev_width-1;j++)
  414. {
  415. int red=0,green=0,blue=0;
  416. _fnSPE_Filter(&red,&green,&blue,i,j,skip,lpPreBack,dwFact);
  417. LONG lOffset=(LONG)(i*skip)+(LONG)(j*4);
  418. *(lpPreView+lOffset++)=(BYTE)((blue)<(0)?(0):((blue)>(255)?(255):(blue)));
  419. *(lpPreView+lOffset++)=(BYTE)((green)<(0)?(0):((green)>(255)?(255):(green)));
  420. *(lpPreView+lOffset)=(BYTE)((red)<(0)?(0):((red)>(255)?(255):(red)));
  421. }
  422. }
  423. }