⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jpegeqview.cpp

📁 可以选择各种压缩方式对读入的图像进行压缩和解压
💻 CPP
字号:
// JPEGEQView.cpp : implementation of the CJPEGEQView class
//

#include "stdafx.h"
#include "JPEGEQ.h"

#include "JPEGEQDoc.h"
#include "JPEGEQView.h"
#include "BMPDisplay.h"   //added by lyf
#include "YasuoDlg.h"
#include "DecCompDlg.h"
#include "structures.h"
#include "jpeg.h"
#include "types.h"    


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CJPEGEQView

IMPLEMENT_DYNCREATE(CJPEGEQView, CView)

BEGIN_MESSAGE_MAP(CJPEGEQView, CView)
	//{{AFX_MSG_MAP(CJPEGEQView)
	ON_COMMAND(ID_DISPLAYBMP, OnDisplaybmp)
	//}}AFX_MSG_MAP
	// Standard printing commands
	//ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	//ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	//ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
	ON_COMMAND(ID_COMPRESS, &CJPEGEQView::OnCompress)
	ON_COMMAND(ID_DECOMP, &CJPEGEQView::OnDecomp)
	ON_COMMAND(ID_TEST, &CJPEGEQView::OnTest)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJPEGEQView construction/destruction

CJPEGEQView::CJPEGEQView()
{
	// TODO: add construction code here

}

CJPEGEQView::~CJPEGEQView()
{
}

BOOL CJPEGEQView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CJPEGEQView drawing

void CJPEGEQView::OnDraw(CDC* pDC)
{
	CJPEGEQDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	//pDC->TextOut(0,0,pDoc->mstring);
	/*CString str; 
	for(int i=1;i<=15;i++) 
	{ 
		str.Format("%d",i); 
		pDC->TextOut(20,20+20*i,str); 
	}*/

}

/////////////////////////////////////////////////////////////////////////////
// CJPEGEQView printing

//BOOL CJPEGEQView::OnPreparePrinting(CPrintInfo* pInfo)
//{
//	// default preparation
//	return DoPreparePrinting(pInfo);
//}
//
//void CJPEGEQView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
//{
//	// TODO: add extra initialization before printing
//}
//
//void CJPEGEQView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
//{
//	// TODO: add cleanup after printing
//}

/////////////////////////////////////////////////////////////////////////////
// CJPEGEQView diagnostics

#ifdef _DEBUG
void CJPEGEQView::AssertValid() const
{
	CView::AssertValid();
}

void CJPEGEQView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CJPEGEQDoc* CJPEGEQView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CJPEGEQDoc)));
	return (CJPEGEQDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CJPEGEQView message handlers

void CJPEGEQView::OnDisplaybmp() 
{
	// TODO: Add your command handler code here
    BMPDisplay dlg;
	if(dlg.DoModal()==IDOK)
	{
		bmpsrcfile=dlg.m_bmpsrcfile;
		ptype **P=NULL; //picture
		char *temp=bmpsrcfile.GetBuffer(0);
		if (loadBMP(temp,bmpwidth,bmpheight,&P)) 
		{
			MessageBox("错误1:无法打开BMP文件","错误",MB_OK);
			return;
		}
		//if(bmpwidth>400||bmpheight>320)
		//{
		//	MessageBox("错误2:指定图像尺寸不能超过400x320","错误",MB_OK);
		//	return;
		//}
		for(unsigned short i=0;i<bmpheight;i++)
			for(unsigned short j=0;j<bmpwidth;j++)
			{
				WritePixel(j,i,int(P[i][j]));
			}
		deleteP(P,bmpheight);
	}
}

void CJPEGEQView::WritePixel(int x, int y, int c)
{
	COLORREF color;

    color=0xff & c;
    color=(color<<16) | (color<<8) | color;	 
    CClientDC dc(this);
    dc.SetPixel(x,y,color);
}

void CJPEGEQView::ShowMsg(int mode, clock_t t1,clock_t t2, long double pnsr, double cr) {
		CClientDC pDC(this);
		CString str="峰值信噪比为:",str1;
		str1.Format("%f",pnsr);
		str+=str1;
		pDC.TextOut(0,0,str); 

		str="压缩比为: ";
		str1.Format("%f",cr);
		str+=str1;
		pDC.TextOut(0,20,str);

		str="采取模式";
		str1.Format("%d(",mode);
		str+=str1;
		switch (mode) {
			case 1: str+="等步长";break;
			case 2: str+="线形渐变步长";break;
			case 3: str+="直方图";break;
			//case 4: str+="";break;
			default: str+="JPEG标准";
		}
		str+="量化方法)";
		pDC.TextOut(0,40,str);

		str="耗时: ";
		str1.Format("%d",long(1000.0*double(t2-t1)/double(CLOCKS_PER_SEC)));
		str=str+str1+" ms";
		pDC.TextOut(0,60,str);
}

void CJPEGEQView::OnCompress()
{
	// TODO: Add your command handler code here
	char* loadfile,*savefile;
	int quantimode;
	BOOL adapornot=FALSE;
    YasuoDlg dlg;
    if(dlg.DoModal()==IDOK)
	{
		loadfile=dlg.srcfilename.GetBuffer(0);
		savefile=dlg.dstfilename.GetBuffer(0);
		quantimode=dlg.QuantiMode;
        adapornot=dlg.Adapornot;
	
		ptype **P=NULL; //picture
		unsigned width,height;
		if (loadBMP(loadfile,width,height,&P)) 
		{
			MessageBox("错误1:无法打开制定BMP文件","错误",MB_OK);
			return;
		}
		btype (*Q)[8][8]=NULL; //blocks;
		blstype n=0; //number of blocks;
		btype QT[8][8];
		clock_t t1,t2;
		int level;
		if (quantimode<0||quantimode>3) MessageBox("错误3:未指定量化操作","错误",MB_OK);

		//mode2table(quantimode,adapornot,P,QT);
		if (4==quantimode) {
			level=6;
		}
		else 
			if (3==quantimode || 1==quantimode) {
				level=65;
			}

		t1=clock();
		n=dctqt(P,width,height,Q,quantimode,level, adapornot,QT); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

		//n=Quantization(P,width,height,&Q,QT);

		//switch(quantimode)
		//{
		//	case 0:n=Quantization(0,P,width,height,&Q,QT);break;
		//	case 1:n=Quantization(1,P,width,height,&Q,QT);break;
		//	case 2:n=Quantization(2,P,width,height,&Q,QT);break;
		//	case 3:n=Quantization(3,P,width,height,&Q,QT);break;
		//	default: MessageBox("错误3:未指定量化操作","错误",MB_OK);
		//}
		List dclist;
		dcCodeList *testdc;
		cltype mdc;
		short *diff=NULL;
		DCProcess(Q,&diff,n,dclist);
		testdc=List2dcCode(dclist,mdc);

		symbolStreamStruct *teststream=NULL;
		codeListStruct *testlist;
		cltype mac;
		unsigned long acStreamLength=0;
		testlist=symbolStream(Q,n,&teststream,mac,acStreamLength);

		enCodeDC(testdc, mdc);
		enCodeAC(testlist,mac);
		t2=clock();

		btype (*Q2)[8][8]=NULL;
		reconstruct(n, teststream, &Q2);
		diff2dc(Q2,diff,n);

		ptype **P2;
		idctqt(Q2,P2,height,width,quantimode,QT);

		//IQuantization(&P2,Q2,height,width,QT);

		
		long double pnsr=imgpnsr(P,P2,height,width);
		unsigned long compressedsize=
			TransAndSave(quantimode,savefile,testlist,teststream,testdc,diff,n,acStreamLength,mac,mdc,width,height,QT);
		double compressratio=double(width*height*sizeof(ptype))/double(compressedsize);

		ShowMsg(quantimode,t1,t2,pnsr,compressratio);
		deleteP(P,height);
		deleteP(P2,height);
		deleteQ(Q);
		deleteQ(Q2);
		delete []diff;
		deleteACCList(testlist);
		deleteDCCList(testdc);
		deleteStream(teststream);
	}
}

void CJPEGEQView::OnDecomp()
{
	// TODO: Add your command handler code here
	char * dstfilename;
    DecCompDlg dlg;
	if(dlg.DoModal()==IDOK)
	{
		dstfilename=dlg.dstfilepath.GetBuffer(0);
	
		unsigned int width,height;
		short *outdiff=NULL;
		symbolStreamStruct *outstream=NULL;
		blstype nn;
		int mode;
		btype QT[8][8];
		nn=ReadFile(dstfilename,mode,width,height,QT,outstream,outdiff);

		btype (*Q2)[8][8]=NULL;
		reconstruct(nn, outstream, &Q2);
		diff2dc(Q2,outdiff,nn);

		ptype **P2;
		idctqt(Q2,P2,height,width,mode,QT);
	    
		/*clock_t t1=clock();
		while (double(clock()-t1)<2*CLOCKS_PER_SEC);*/

		for(unsigned short i=0;i<height;i++)
			for(unsigned short j=0;j<width;j++)
			{
				WritePixel(j,i,int(P2[i][j]));
			}

		deleteP(P2,height);
		deleteQ(Q2);
		deleteStream(outstream);
		delete []outdiff;
	}

}

void CJPEGEQView::OnTest()
{
	// TODO: Add your command handler code here

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -