Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
PicItemView.cpp
Package: PicItem.rar [view]
Upload User: zhaisir71
Upload Date: 2022-07-10
Package Size: 3208k
Code Size: 12k
Category:
Graph program
Development Platform:
Visual C++
- // PicItemView.cpp : implementation of the CPicItemView class
- //
- #include "stdafx.h"
- #include "PicItem.h"
- #include "PicItemDoc.h"
- #include "PicItemView.h"
- #include "testDlg.h"
- #include "GramDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CPicItemView
- IMPLEMENT_DYNCREATE(CPicItemView, CView)
- BEGIN_MESSAGE_MAP(CPicItemView, CView)
- //{{AFX_MSG_MAP(CPicItemView)
- ON_COMMAND(ID_PIC_PROC, OnPicProc)
- ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
- ON_COMMAND(ID_FILE_SAVE, OnFileSave)
- ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
- ON_COMMAND(ID_PIC_ADA, OnPicAda)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_MESSAGE (WM_USER_REDRAW,OnReDraw)
- ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CPicItemView construction/destruction
- CPicItemView::CPicItemView()
- {
- // TODO: add construction code here
- m_pDIBData=NULL;
- m_pBMPInfo=NULL;
- }
- CPicItemView::~CPicItemView()
- {
- }
- BOOL CPicItemView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CPicItemView drawing
- void CPicItemView::OnDraw(CDC* pDC)
- {
- CPicItemDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- if(0==ExtName.Compare(_T("bmp")))
- {
- if(m_pBMPInfo==NULL||m_pDIBData==NULL)
- return;
- HRESULT rval=0;
- CPoint point;
- COLORREF Color=0;
- long bmp_Width=m_pBMPInfo->bmiHeader.biWidth;
- long bmp_Height=m_pBMPInfo->bmiHeader.biHeight;
- USHORT bmp_bits=m_pBMPInfo->bmiHeader.biBitCount;
- pDC->SetStretchBltMode(COLORONCOLOR);
- StretchDIBits(pDC->GetSafeHdc(),0,0,bmp_Width,bmp_Height,
- 0,0,bmp_Width,bmp_Height,m_pDIBData,m_pBMPInfo,DIB_RGB_COLORS,SRCCOPY);
- }
- else{
- if(0==ExtName.Compare(_T("jpg")))
- {
- ShowJpgGif(pDC,FileName,1,1);
- //AfxMessageBox("string");
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CPicItemView printing
- BOOL CPicItemView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CPicItemView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CPicItemView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CPicItemView diagnostics
- #ifdef _DEBUG
- void CPicItemView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CPicItemView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CPicItemDoc* CPicItemView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPicItemDoc)));
- return (CPicItemDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CPicItemView message handlers
- void CPicItemView::OnPicProc()
- {
- // TODO: Add your command handler code here
- CtestDlg dlg;
- dlg.DoModal();
- }
- void CPicItemView::OnFileOpen()
- {
- // TODO: Add your command handler code here
- HRESULT rval;
- CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,("位图文件(*.BMP)|*.BMP|jpg文件(*.jpg)|*.jpg||"));
- rval=dlg.DoModal();
- if(rval==IDCANCEL)
- return;
- FileName=dlg.GetPathName();
- ExtName=dlg.GetFileExt();
- CFile file;
- BITMAPFILEHEADER bfh;
- rval=file.Open(FileName.GetBuffer(0),CFile::modeRead|CFile::typeBinary,NULL);
- if(!rval)
- return;
- file.Read(&bfh,sizeof(bfh));
- /*if(bfh.bfType!=0x4d42)
- {
- file.Close();
- return;
- }*/
- rval=OpenBMPFile(&file);
- if(rval==FALSE)
- {
- file.Close();
- return;
- }
- file.Close();
- /*SIZE size;
- RECT rect;
- size.cx=m_pBMPInfo->bmiHeader.biWidth;
- size.cy=m_pBMPInfo->bmiHeader.biHeight;
- GetClientRect(&rect);
- if((rect.left-rect.right)<size.cx||(rect.bottom-rect.top)<size.cy)
- {
- this->SetScrollSizes(MM_TEXT,size,sizeDefault,sizeDefault);
- }*/
- Invalidate(FALSE);
- return;
- }
- BOOL CPicItemView::OpenBMPFile(CFile *pfile)
- {
- BITMAPFILEHEADER bfh;
- BITMAPINFOHEADER bih;
- BITMAPINFO* pBMPInfo=NULL;
- RGBQUAD* pColorTab=NULL;
- BYTE* pData=NULL;
- DWORD dwSize=0;
- pfile->SeekToBegin();
- pfile->Read((void*)&bfh,sizeof(BITMAPFILEHEADER));
- pfile->Read((void*)&bih,sizeof(BITMAPINFOHEADER));
- if(bih.biBitCount==8)
- {
- dwSize=bfh.bfOffBits-sizeof(BITMAPFILEHEADER);
- pBMPInfo=(BITMAPINFO*)new char[dwSize];
- pfile->Seek(sizeof(BITMAPFILEHEADER),CFile::begin);
- pfile->Read(pBMPInfo,dwSize);
- /* if(m_pOriginBMPInfo!=NULL)
- {
- delete m_pOriginBMPInfo;
- m_pOriginBMPInfo=NULL;
- }*/
- m_pOriginBMPInfo=(BITMAPINFO*)new char[dwSize];
- memcpy(m_pOriginBMPInfo,pBMPInfo,dwSize);
- //读取颜色表
- BYTE* point=(BYTE*)pBMPInfo;
- point+=sizeof(BITMAPINFOHEADER);
- pColorTab=(RGBQUAD*)point;
- point=(BYTE*)m_pOriginBMPInfo;
- point+=sizeof(BITMAPINFOHEADER);
- m_pOriginColorTab=(RGBQUAD*)point;
- //读取图像数据
- dwSize=bfh.bfSize-bfh.bfOffBits;
- pData=(BYTE*)new char[dwSize];
- if(pData==NULL)
- return FALSE;
- pfile->Seek(bfh.bfOffBits,CFile::begin);
- pfile->ReadHuge((void*)pData,dwSize);
- /*if(m_pOriginDIBData!=NULL)
- {
- delete m_pOriginDIBData;
- m_pOriginDIBData=NULL;
- }*/
- m_pOriginDIBData=(BYTE*)new char[dwSize];
- memcpy(m_pOriginDIBData,pData,dwSize);
- //记录bmp文件数据
- /*if(m_pBMPInfo!=NULL)
- {
- delete m_pBMPInfo;
- m_pBMPInfo=NULL;
- }*/
- m_pBMPInfo=pBMPInfo;
- m_pColorTab=pColorTab;
- /*if(m_pDIBData!=NULL)
- {
- delete m_pDIBData;
- m_pDIBData=NULL;
- }*/
- m_pDIBData=pData;
- //}
- return TRUE;
- }
- }
- LRESULT CPicItemView::OnReDraw(WPARAM wParam,LPARAM lParam)
- {
- int i,j;
- long b_Width=m_pBMPInfo->bmiHeader.biWidth;
- long b_Height=m_pBMPInfo->bmiHeader.biHeight;
- USHORT b_bits=m_pBMPInfo->bmiHeader.biBitCount;
- int w=(b_Width*b_bits+31)/32*4;
- for(i=0;i<b_Height;i++)
- for(j=0;j<b_Width;j++)
- {
- if(*(m_pDIBData+i*w+j)<=thethre)
- *(m_pDIBData+i*w+j)=255;
- else
- *(m_pDIBData+i*w+j)=0;
- }
- Invalidate(FALSE);
- return 0;
- }
- BOOL CPicItemView::ShowJpgGif(CDC *pDC, CString strPath, int x, int y)
- {
- IStream *pStm;
- CFileStatus fstatus;
- CFile file;
- LONG cb;
- //打开文件并检测文件的有效性
- if (file.Open(strPath,CFile::modeRead)&&
- file.GetStatus(strPath,fstatus)&&
- ((cb = fstatus.m_size) != -1))
- {
- HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
- LPVOID pvData = NULL;
- if (hGlobal != NULL)
- {
- pvData = GlobalLock(hGlobal);
- if (pvData != NULL)
- {
- //file.ReadHuge(pvData, cb); //6.0中可能是用这个函数
- file.Read(pvData, cb); //VC2005.NET中用这个函数
- GlobalUnlock(hGlobal);
- CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
- }
- }
- }
- else
- {
- return false;
- } //打开文件结束
- IPicture *pPic;
- if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)))
- {
- OLE_XSIZE_HIMETRIC hmWidth;
- OLE_YSIZE_HIMETRIC hmHeight;
- pPic->get_Width(&hmWidth);
- pPic->get_Height(&hmHeight);
- double fX,fY;
- //get image height and width
- //fX=MulDiv(hmWidth, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSX), pDC->GetDeviceCaps(HORZSIZE)*10.0);
- // fY=MulDiv(hmWidth, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), pDC->GetDeviceCaps(VERTSIZE)*10.0);
- fX = (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);
- fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);
- //use render function display image
- if(FAILED(pPic->Render(*pDC,x,y,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL)))
- {
- pPic->Release();
- return false;
- }
- pPic->Release();
- }
- else
- {
- return false;
- }
- return true;
- }
- void CPicItemView::OnFileSave()
- {
- // TODO: Add your command handler code here
- CFile fp(FileName,CFile::modeReadWrite|CFile::typeBinary);
- DoWrite(fp);
- fp.Close();
- }
- bool CPicItemView::DoWrite(CFile &file)
- {
- long size;
- int PaletteSize=256;
- BITMAPFILEHEADER bfh;
- if(!m_pBMPInfo)
- return false;
- bfh.bfType=0x4d42;
- long b_Width=m_pBMPInfo->bmiHeader.biWidth;
- long b_Height=m_pBMPInfo->bmiHeader.biHeight;
- USHORT b_bits=m_pBMPInfo->bmiHeader.biBitCount;
- int w=(b_Width*b_bits+31)/32*4;
- size=w*b_Height;
- bfh.bfReserved1=0;
- bfh.bfReserved2=0;
- bfh.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+
- PaletteSize*sizeof(RGBQUAD);
- bfh.bfSize=bfh.bfOffBits+size;
- file.Write(&bfh,sizeof(BITMAPFILEHEADER));
- file.Write(m_pBMPInfo,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*PaletteSize);
- file.Write(m_pDIBData,size);
- return true;
- }
- void CPicItemView::OnFileSaveAs()
- {
- // TODO: Add your command handler code here
- CFileDialog dlg(FALSE,NULL,NULL,NULL,NULL,NULL);
- int rval=dlg.DoModal();
- if(IDCANCEL==rval)
- return;
- CString filename;
- filename=dlg.GetPathName();
- CFile fp(filename,CFile::modeCreate |CFile::modeReadWrite|CFile::typeBinary);
- DoWrite(fp);
- fp.Close();
- }
- void CPicItemView::OnPicAda()
- {
- // TODO: Add your command handler code here
- //CGramDlg dlg;
- //dlg.DoModal();
- int i,j;
- long dwWidth=m_pBMPInfo->bmiHeader.biWidth;
- long dwHeight=m_pBMPInfo->bmiHeader.biHeight;
- UINT bitcount=m_pBMPInfo->bmiHeader.biBitCount;
- int Wi=(dwWidth*bitcount+31)/32*4;
- int maxb,hist[300];
- DWORD* h;
- long uu[300],w[300],b[300],ave,max;
- double p[300];
- int n=0;
- char sPos[10];
- if (dwWidth*dwHeight>2500000)
- {
- MessageBox("图片太大,请选择较小的图片进行处理","警告",MB_ICONWARNING+MB_OK+MB_DEFBUTTON1);
- }
- else
- {
- h=new DWORD[dwWidth*dwHeight];
- /*for(i=0;i<dwWidth;i++)
- for(j=0;j<dwHeight;j++)
- {
- CPoint point;
- point.x=i;
- point.y=j;
- GetColor_TrueColor_Origin(&point,&n);
- n=*(m_pDIBData+i*Wi+j);
- }*/
- //获得各等级像素个数
- for(n=0;n<256;n++)
- hist[n]=0;
- for(i=0;i<dwHeight;i++)
- for(j=0;j<dwWidth;j++)
- {
- n=*(m_pDIBData+i*Wi+j);
- hist[n]+=1;
- }
- //计算灰度级
- for(i=0;i<256;i++)
- p[i]=(double)hist[i]/dwWidth*dwHeight;
- //计算灰度均值
- for(i=0;i<256;i++)
- ave=ave+(long)((i-1)*p[i]);
- //计算灰度类均值,类直方图的和以及类分离指标
- uu[0]=p[0];w[0]=0;w[1]=p[1];
- for(i=0;i<256;i++)
- {
- uu[i]=uu[i-1]+(i+1)*p[i];
- w[i]=w[i-1]+p[i];
- if(w[i]*(1-w[i])!=0)
- b[i]=((ave*w[i]-uu[i])*(ave*w[i]-uu[i]))/w[i]*(1-w[i]);
- }
- //求类分离指标最大的i值
- max=b[2];
- for(i=1;i<256;i++)
- if(b[i]>=max) max=b[i];
- for(i=1;i<256;i++)
- if(b[i]==max) maxb=i;
- itoa(maxb-1,sPos,10);
- MessageBox(sPos);
- //输出阈值
- /*if(m_pYuzhiDlg==NULL)
- {
- m_pYuzhiDlg=new CYuzhiDialog;
- m_pYuzhiDlg->create(IDD_YUZHI_DLG,this);
- }
- m_pYuzhiDlg->ShowWindow(SW_SHOW);
- itoa(maxb-1,sPos,10);
- m_pYuzhiDlg->m_Yuzhi.SetSel(0,-1);
- m_pYuzhiDlg->m_Yuzhi.ReplaceSel(sPos);
- UpdateData(FALSE);*/
- //根据阈值进行灰度图像的二值化
- for(i=0;i<dwHeight;i++)
- for(j=0;j<dwWidth;j++)
- {
- if(*(m_pDIBData+i*Wi+j)<=maxb)
- *(m_pDIBData+i*Wi+j)=255;
- else
- *(m_pDIBData+i*Wi+j)=0;
- }
- Invalidate(FALSE);
- delete []h;
- }
- }