PicItemView.cpp
Upload User: zhaisir71
Upload Date: 2022-07-10
Package Size: 3208k
Code Size: 12k
Category:

Graph program

Development Platform:

Visual C++

  1. // PicItemView.cpp : implementation of the CPicItemView class
  2. //
  3. #include "stdafx.h"
  4. #include "PicItem.h"
  5. #include "PicItemDoc.h"
  6. #include "PicItemView.h"
  7. #include "testDlg.h"
  8. #include "GramDlg.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CPicItemView
  16. IMPLEMENT_DYNCREATE(CPicItemView, CView)
  17. BEGIN_MESSAGE_MAP(CPicItemView, CView)
  18. //{{AFX_MSG_MAP(CPicItemView)
  19. ON_COMMAND(ID_PIC_PROC, OnPicProc)
  20. ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  21. ON_COMMAND(ID_FILE_SAVE, OnFileSave)
  22. ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
  23. ON_COMMAND(ID_PIC_ADA, OnPicAda)
  24. //}}AFX_MSG_MAP
  25. // Standard printing commands
  26. ON_MESSAGE (WM_USER_REDRAW,OnReDraw)
  27. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  28. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  29. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  30. END_MESSAGE_MAP()
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CPicItemView construction/destruction
  33. CPicItemView::CPicItemView()
  34. {
  35. // TODO: add construction code here
  36. m_pDIBData=NULL;
  37. m_pBMPInfo=NULL;
  38. }
  39. CPicItemView::~CPicItemView()
  40. {
  41. }
  42. BOOL CPicItemView::PreCreateWindow(CREATESTRUCT& cs)
  43. {
  44. // TODO: Modify the Window class or styles here by modifying
  45. //  the CREATESTRUCT cs
  46. return CView::PreCreateWindow(cs);
  47. }
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CPicItemView drawing
  50. void CPicItemView::OnDraw(CDC* pDC)
  51. {
  52. CPicItemDoc* pDoc = GetDocument();
  53. ASSERT_VALID(pDoc);
  54. // TODO: add draw code for native data here
  55. if(0==ExtName.Compare(_T("bmp")))
  56. {
  57. if(m_pBMPInfo==NULL||m_pDIBData==NULL)
  58. return;
  59. HRESULT rval=0;
  60. CPoint point;
  61. COLORREF Color=0;
  62. long bmp_Width=m_pBMPInfo->bmiHeader.biWidth;
  63. long bmp_Height=m_pBMPInfo->bmiHeader.biHeight;
  64. USHORT bmp_bits=m_pBMPInfo->bmiHeader.biBitCount;
  65. pDC->SetStretchBltMode(COLORONCOLOR);
  66. StretchDIBits(pDC->GetSafeHdc(),0,0,bmp_Width,bmp_Height,
  67. 0,0,bmp_Width,bmp_Height,m_pDIBData,m_pBMPInfo,DIB_RGB_COLORS,SRCCOPY);
  68. }
  69. else{
  70. if(0==ExtName.Compare(_T("jpg")))
  71. {
  72. ShowJpgGif(pDC,FileName,1,1);
  73.       //AfxMessageBox("string");
  74. }
  75. }
  76. }
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CPicItemView printing
  79. BOOL CPicItemView::OnPreparePrinting(CPrintInfo* pInfo)
  80. {
  81. // default preparation
  82. return DoPreparePrinting(pInfo);
  83. }
  84. void CPicItemView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  85. {
  86. // TODO: add extra initialization before printing
  87. }
  88. void CPicItemView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  89. {
  90. // TODO: add cleanup after printing
  91. }
  92. /////////////////////////////////////////////////////////////////////////////
  93. // CPicItemView diagnostics
  94. #ifdef _DEBUG
  95. void CPicItemView::AssertValid() const
  96. {
  97. CView::AssertValid();
  98. }
  99. void CPicItemView::Dump(CDumpContext& dc) const
  100. {
  101. CView::Dump(dc);
  102. }
  103. CPicItemDoc* CPicItemView::GetDocument() // non-debug version is inline
  104. {
  105. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPicItemDoc)));
  106. return (CPicItemDoc*)m_pDocument;
  107. }
  108. #endif //_DEBUG
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CPicItemView message handlers
  111. void CPicItemView::OnPicProc() 
  112. {
  113. // TODO: Add your command handler code here
  114. CtestDlg dlg;
  115. dlg.DoModal();
  116. }
  117. void CPicItemView::OnFileOpen() 
  118. {
  119. // TODO: Add your command handler code here
  120. HRESULT rval;
  121. CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,("位图文件(*.BMP)|*.BMP|jpg文件(*.jpg)|*.jpg||")); 
  122. rval=dlg.DoModal();
  123. if(rval==IDCANCEL)
  124. return;
  125. FileName=dlg.GetPathName();
  126. ExtName=dlg.GetFileExt();
  127. CFile file;
  128. BITMAPFILEHEADER bfh;
  129. rval=file.Open(FileName.GetBuffer(0),CFile::modeRead|CFile::typeBinary,NULL);
  130. if(!rval)
  131. return;
  132. file.Read(&bfh,sizeof(bfh));
  133. /*if(bfh.bfType!=0x4d42)
  134. {
  135. file.Close();
  136. return;
  137. }*/
  138. rval=OpenBMPFile(&file);
  139. if(rval==FALSE)
  140. {
  141. file.Close();
  142. return;
  143. }
  144. file.Close();
  145. /*SIZE size;
  146. RECT rect;
  147. size.cx=m_pBMPInfo->bmiHeader.biWidth;
  148. size.cy=m_pBMPInfo->bmiHeader.biHeight;
  149. GetClientRect(&rect);
  150. if((rect.left-rect.right)<size.cx||(rect.bottom-rect.top)<size.cy)
  151. {
  152. this->SetScrollSizes(MM_TEXT,size,sizeDefault,sizeDefault);
  153. }*/
  154.     Invalidate(FALSE);
  155. return;
  156. }
  157. BOOL CPicItemView::OpenBMPFile(CFile *pfile)
  158. {
  159. BITMAPFILEHEADER bfh;
  160. BITMAPINFOHEADER bih;
  161. BITMAPINFO* pBMPInfo=NULL;
  162. RGBQUAD* pColorTab=NULL;
  163. BYTE* pData=NULL;
  164. DWORD dwSize=0;
  165. pfile->SeekToBegin();
  166. pfile->Read((void*)&bfh,sizeof(BITMAPFILEHEADER));
  167. pfile->Read((void*)&bih,sizeof(BITMAPINFOHEADER));
  168. if(bih.biBitCount==8)
  169. {
  170. dwSize=bfh.bfOffBits-sizeof(BITMAPFILEHEADER);
  171. pBMPInfo=(BITMAPINFO*)new char[dwSize];
  172. pfile->Seek(sizeof(BITMAPFILEHEADER),CFile::begin);
  173. pfile->Read(pBMPInfo,dwSize);
  174. /* if(m_pOriginBMPInfo!=NULL)
  175. {
  176. delete m_pOriginBMPInfo;
  177. m_pOriginBMPInfo=NULL;
  178. }*/
  179. m_pOriginBMPInfo=(BITMAPINFO*)new char[dwSize];
  180. memcpy(m_pOriginBMPInfo,pBMPInfo,dwSize);
  181. //读取颜色表
  182. BYTE* point=(BYTE*)pBMPInfo;
  183. point+=sizeof(BITMAPINFOHEADER);
  184. pColorTab=(RGBQUAD*)point;
  185.     point=(BYTE*)m_pOriginBMPInfo;
  186. point+=sizeof(BITMAPINFOHEADER);
  187. m_pOriginColorTab=(RGBQUAD*)point;
  188. //读取图像数据
  189. dwSize=bfh.bfSize-bfh.bfOffBits;
  190. pData=(BYTE*)new char[dwSize];
  191. if(pData==NULL)
  192. return FALSE;
  193. pfile->Seek(bfh.bfOffBits,CFile::begin);
  194. pfile->ReadHuge((void*)pData,dwSize);
  195.     /*if(m_pOriginDIBData!=NULL)
  196. {
  197. delete m_pOriginDIBData;
  198. m_pOriginDIBData=NULL;
  199. }*/
  200. m_pOriginDIBData=(BYTE*)new char[dwSize];
  201. memcpy(m_pOriginDIBData,pData,dwSize);
  202. //记录bmp文件数据
  203. /*if(m_pBMPInfo!=NULL)
  204. {
  205. delete m_pBMPInfo;
  206. m_pBMPInfo=NULL;
  207. }*/
  208. m_pBMPInfo=pBMPInfo;
  209. m_pColorTab=pColorTab;
  210. /*if(m_pDIBData!=NULL)
  211. {
  212. delete m_pDIBData;
  213. m_pDIBData=NULL;
  214. }*/
  215. m_pDIBData=pData;
  216. //}
  217. return TRUE;
  218. }
  219. }
  220. LRESULT CPicItemView::OnReDraw(WPARAM wParam,LPARAM lParam)
  221. {
  222. int i,j;
  223. long b_Width=m_pBMPInfo->bmiHeader.biWidth;
  224. long b_Height=m_pBMPInfo->bmiHeader.biHeight;
  225. USHORT b_bits=m_pBMPInfo->bmiHeader.biBitCount;
  226. int w=(b_Width*b_bits+31)/32*4;
  227. for(i=0;i<b_Height;i++)
  228. for(j=0;j<b_Width;j++)
  229. {
  230. if(*(m_pDIBData+i*w+j)<=thethre)
  231. *(m_pDIBData+i*w+j)=255;
  232. else
  233. *(m_pDIBData+i*w+j)=0;
  234. }
  235. Invalidate(FALSE);
  236. return 0;
  237. }
  238. BOOL CPicItemView::ShowJpgGif(CDC *pDC, CString strPath, int x, int y)
  239. {
  240. IStream *pStm;  
  241. CFileStatus fstatus;  
  242. CFile file;  
  243. LONG cb;  
  244.     //打开文件并检测文件的有效性
  245.     if (file.Open(strPath,CFile::modeRead)&&
  246. file.GetStatus(strPath,fstatus)&& 
  247. ((cb = fstatus.m_size) != -1))  
  248. {  
  249. HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);  
  250. LPVOID pvData = NULL;  
  251.         if (hGlobal != NULL)  
  252. {  
  253. pvData = GlobalLock(hGlobal);
  254.             if (pvData != NULL)  
  255. {  
  256.                 //file.ReadHuge(pvData, cb);  //6.0中可能是用这个函数
  257. file.Read(pvData, cb);  //VC2005.NET中用这个函数
  258. GlobalUnlock(hGlobal);  
  259. CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);  
  260. }
  261.     else
  262. {
  263.         return false;
  264. } //打开文件结束
  265.     
  266. IPicture *pPic; 
  267.     if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic))) 
  268. OLE_XSIZE_HIMETRIC hmWidth;  
  269. OLE_YSIZE_HIMETRIC hmHeight;  
  270. pPic->get_Width(&hmWidth);  
  271. pPic->get_Height(&hmHeight);  
  272.         double fX,fY;  
  273.         //get image height and width
  274. //fX=MulDiv(hmWidth, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSX), pDC->GetDeviceCaps(HORZSIZE)*10.0);
  275. // fY=MulDiv(hmWidth, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), pDC->GetDeviceCaps(VERTSIZE)*10.0);
  276. fX = (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);  
  277. fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);  
  278.         //use render function display image
  279.         if(FAILED(pPic->Render(*pDC,x,y,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL)))  
  280. {
  281. pPic->Release();
  282.             return false;
  283. }
  284. pPic->Release();  
  285. }  
  286.     else  
  287. {
  288.         return false;  
  289. }
  290.     return true;
  291. }
  292. void CPicItemView::OnFileSave() 
  293. {
  294. // TODO: Add your command handler code here
  295. CFile fp(FileName,CFile::modeReadWrite|CFile::typeBinary);
  296. DoWrite(fp);
  297. fp.Close();
  298. }
  299. bool CPicItemView::DoWrite(CFile &file)
  300. {
  301. long size;
  302. int PaletteSize=256;
  303. BITMAPFILEHEADER bfh;
  304. if(!m_pBMPInfo)
  305. return false;
  306. bfh.bfType=0x4d42;
  307. long b_Width=m_pBMPInfo->bmiHeader.biWidth;
  308. long b_Height=m_pBMPInfo->bmiHeader.biHeight;
  309. USHORT b_bits=m_pBMPInfo->bmiHeader.biBitCount;
  310. int w=(b_Width*b_bits+31)/32*4;
  311. size=w*b_Height;
  312. bfh.bfReserved1=0;
  313. bfh.bfReserved2=0;
  314. bfh.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+
  315. PaletteSize*sizeof(RGBQUAD);
  316. bfh.bfSize=bfh.bfOffBits+size;
  317. file.Write(&bfh,sizeof(BITMAPFILEHEADER));
  318. file.Write(m_pBMPInfo,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*PaletteSize);
  319. file.Write(m_pDIBData,size);
  320. return true;
  321. }
  322. void CPicItemView::OnFileSaveAs() 
  323. {
  324. // TODO: Add your command handler code here
  325. CFileDialog dlg(FALSE,NULL,NULL,NULL,NULL,NULL);
  326. int rval=dlg.DoModal();
  327. if(IDCANCEL==rval)
  328. return;
  329.    CString filename;
  330. filename=dlg.GetPathName();
  331. CFile fp(filename,CFile::modeCreate |CFile::modeReadWrite|CFile::typeBinary);
  332. DoWrite(fp);
  333. fp.Close();
  334. }
  335. void CPicItemView::OnPicAda() 
  336. {
  337. // TODO: Add your command handler code here
  338. //CGramDlg dlg;
  339. //dlg.DoModal();
  340. int i,j;
  341.     long dwWidth=m_pBMPInfo->bmiHeader.biWidth;
  342. long dwHeight=m_pBMPInfo->bmiHeader.biHeight;
  343. UINT bitcount=m_pBMPInfo->bmiHeader.biBitCount;
  344. int Wi=(dwWidth*bitcount+31)/32*4;
  345. int maxb,hist[300];
  346. DWORD* h;
  347. long uu[300],w[300],b[300],ave,max;
  348. double p[300];
  349. int n=0;
  350. char sPos[10];
  351. if (dwWidth*dwHeight>2500000)
  352. {
  353. MessageBox("图片太大,请选择较小的图片进行处理","警告",MB_ICONWARNING+MB_OK+MB_DEFBUTTON1);
  354. else
  355. {
  356. h=new DWORD[dwWidth*dwHeight];
  357. /*for(i=0;i<dwWidth;i++)
  358. for(j=0;j<dwHeight;j++)
  359. {
  360. CPoint point;
  361. point.x=i;
  362. point.y=j;
  363. GetColor_TrueColor_Origin(&point,&n);
  364. n=*(m_pDIBData+i*Wi+j);
  365. }*/
  366. //获得各等级像素个数
  367. for(n=0;n<256;n++)
  368. hist[n]=0;
  369. for(i=0;i<dwHeight;i++)
  370. for(j=0;j<dwWidth;j++)
  371. {
  372. n=*(m_pDIBData+i*Wi+j);
  373. hist[n]+=1;
  374. }
  375. //计算灰度级
  376. for(i=0;i<256;i++)
  377. p[i]=(double)hist[i]/dwWidth*dwHeight;
  378. //计算灰度均值
  379. for(i=0;i<256;i++)
  380. ave=ave+(long)((i-1)*p[i]);
  381. //计算灰度类均值,类直方图的和以及类分离指标
  382. uu[0]=p[0];w[0]=0;w[1]=p[1];
  383. for(i=0;i<256;i++)
  384. {
  385. uu[i]=uu[i-1]+(i+1)*p[i];
  386. w[i]=w[i-1]+p[i];
  387. if(w[i]*(1-w[i])!=0)
  388. b[i]=((ave*w[i]-uu[i])*(ave*w[i]-uu[i]))/w[i]*(1-w[i]);
  389. }
  390. //求类分离指标最大的i值
  391. max=b[2];
  392. for(i=1;i<256;i++)
  393. if(b[i]>=max) max=b[i];
  394. for(i=1;i<256;i++)
  395. if(b[i]==max) maxb=i;
  396. itoa(maxb-1,sPos,10);
  397.     MessageBox(sPos);
  398. //输出阈值
  399. /*if(m_pYuzhiDlg==NULL)
  400. {
  401. m_pYuzhiDlg=new CYuzhiDialog;
  402. m_pYuzhiDlg->create(IDD_YUZHI_DLG,this);
  403. }
  404. m_pYuzhiDlg->ShowWindow(SW_SHOW);
  405. itoa(maxb-1,sPos,10);
  406. m_pYuzhiDlg->m_Yuzhi.SetSel(0,-1);
  407. m_pYuzhiDlg->m_Yuzhi.ReplaceSel(sPos);
  408. UpdateData(FALSE);*/
  409. //根据阈值进行灰度图像的二值化
  410. for(i=0;i<dwHeight;i++)
  411. for(j=0;j<dwWidth;j++)
  412. {
  413. if(*(m_pDIBData+i*Wi+j)<=maxb)
  414. *(m_pDIBData+i*Wi+j)=255;
  415. else
  416. *(m_pDIBData+i*Wi+j)=0;
  417. }
  418. Invalidate(FALSE);
  419. delete []h;
  420. }
  421. }