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

📄 cdpcmdemoview.cpp

📁 采用DPCM压缩算法的工程文件
💻 CPP
字号:
// CDpcmDemoView.cpp : implementation of the CCDpcmDemoView class
//

#include "stdafx.h"
#include "CDpcmDemo.h"

#include "CDpcmDemoDoc.h"
#include "CDpcmDemoView.h"
#include ".\cdpcmdemoview.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCDpcmDemoView

IMPLEMENT_DYNCREATE(CCDpcmDemoView, CView)

BEGIN_MESSAGE_MAP(CCDpcmDemoView, CView)
	//{{AFX_MSG_MAP(CCDpcmDemoView)
	ON_COMMAND(ID_ARITHMETIC, OnArithmetic)
	//}}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_DARITHMETIC, OnDarithmetic)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCDpcmDemoView construction/destruction

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

}

CCDpcmDemoView::~CCDpcmDemoView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CCDpcmDemoView drawing

void CCDpcmDemoView::OnDraw(CDC* pDC)
{
	CCDpcmDemoDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CSize sizeDibDisplay; 
	if(!pDoc->m_pDibInit->IsEmpty()){	
		sizeDibDisplay = pDoc->m_pDibInit->GetDimensions();
		pDoc->m_pDibInit->Draw(pDC,CPoint(0,0),sizeDibDisplay);	
	}	
}

/////////////////////////////////////////////////////////////////////////////
// CCDpcmDemoView printing

BOOL CCDpcmDemoView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CCDpcmDemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CCDpcmDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CCDpcmDemoView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CCDpcmDemoView message handlers

void CCDpcmDemoView::OnArithmetic() 
{
	// TODO: Add your command handler code here
	j_compress_struct main_cinfo;
	int p; 
	jpeg_destination_mgr main_dest;
	unsigned char fileheader[4];            /*the compact image data include only width and height*/
	for(int i=0; i<4; i++)
	{
		fileheader[i] = 0x00;
	}

	j_compress_ptr cinfo=&main_cinfo;
	arith_entropy_encoder entropy;
	
	cinfo->dest=&main_dest;
	cinfo->buffer_count=0;
	cinfo->entropy=&entropy;
	main_cinfo.outbuffer=(JOCTET *)m_pDpcm.Alloc_One_Row(cinfo,(size_t) (OUTPUT_BUF_SIZE)); /*sizeof(JOCTET) is 1 */
	main_dest.next_output_byte =cinfo->outbuffer;
	main_dest.free_in_buffer = OUTPUT_BUF_SIZE;
	cinfo->arith_dc_L=1;
	cinfo->first_line=0;
	MEMZERO(cinfo->entropy->dc_stats,160);
	MEMZERO(cinfo->entropy->context_b,1000);
	MEMZERO(cinfo->entropy->val_b,1000);
	MEMZERO(cinfo->pre.B,4*365);
	MEMZERO(cinfo->pre.N,4*365); 
	MEMZERO(cinfo->pre.C,4*365);
	
	
	cinfo->pre.reset=64;
	cinfo->entropy->last_val=0;
	cinfo->entropy->context=0;
	cinfo->arith_dc_U=2;
	cinfo->entropy->c = 0;
	cinfo->entropy->a = 0x10000L;
	cinfo->entropy->sc = 0;
	cinfo->entropy->zc = 0;
	cinfo->entropy->ct = 11;
	cinfo->entropy->buffer = -1;            /* empty */ 
	
	CString strOpenFileType = "位图文件 (*.jpg)|*.jpg; *.jpg||";
	CFileDialog FileDlg(TRUE, "*.jpg", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strOpenFileType);
	
	if (FileDlg.DoModal() == IDOK)
	{ 
		CString strFilePath = FileDlg.GetPathName();
		if((cinfo->inputfile=fopen(strFilePath,"rb"))==NULL)
		{
			MessageBox("打开文件错误!", "错误信息", MB_OK|MB_ICONERROR);
			return;
		}
		
		if((cinfo->outputfile=fopen("d:\\Hello.img","wb+"))==NULL)
		{
			MessageBox("创建文件失败", "错误信息", MB_OK|MB_ICONERROR);
			return;
		}
		
		if (!ReadOK(cinfo->inputfile, fileheader, 4))
		{
			MessageBox("error when read the head of file!", "错误信息", MB_OK|MB_ICONERROR);
			return;
		}

		cinfo->image_width = (UINT16)GET_2B(fileheader,0);
		cinfo->image_height = (UINT16)GET_2B(fileheader,2);
		
		cinfo->inbuffer=m_pDpcm.Alloc_Sarray((JDIMENSION) (cinfo->image_width),
		1,cinfo);                
		/*the inbuffer is a row of image elements now for*
		 *one demintion prediction.
		*/
		
		/*create the quality and huff table*/
		/*create_huff (cinfo); It is keeped for later compareration*/ 		
		
		/*write the proper file header,for simplisity we only reserve the SOI and EOI symbole*/
		m_pDpcm.Write_File_Header(cinfo);
		
		/*prepare for the compresion*/
		/*jpeg_make_c_derived_tbl (cinfo, 1,
		*		      &(cinfo->d_dc_huff_tbl_ptr));
		*jpeg_make_c_derived_tbl (cinfo, 0,
		*		      &(cinfo->d_ac_huff_tbl_ptr));
		*/
		/*Enthopy coding and write to the JPEG file*/
		
		for (int i=0;i<(cinfo->image_height);i++)
		{			
			m_pDpcm.Input_Image_Data(cinfo);
			m_pDpcm.Compress_Data(cinfo,i);                   /*include DCT and entropy coding*/
			if (i==0)
				cinfo->first_line=0;
		} 
		
		/*kill all the buffer and output EOI marker*/
		m_pDpcm.Flush_Bits (cinfo);                      /*out put the patial image data*/
		m_pDpcm.Write_File_Trailer (cinfo); 
		JFWRITE(cinfo->outputfile, cinfo->outbuffer, 
			(((size_t) (OUTPUT_BUF_SIZE))-cinfo->dest->free_in_buffer));
		
		m_pDpcm.Free_Mem(cinfo);
		/*fseek(cinfo->inputfile,-2,2);*/
		p=0;
		while(getc(cinfo->inputfile)!=EOF)	
			p++; 
		fclose(cinfo->inputfile);
		fclose(cinfo->outputfile);		
	}
}

void CCDpcmDemoView::OnDarithmetic()
{
	// TODO: 在此添加命令处理程序代码
	j_compress_struct main_cinfo;
	int i,p; 
	jpeg_destination_mgr main_dest;
	unsigned char fileheader[8];            /*the compact image data include only width and height*/

	j_compress_ptr cinfo=&main_cinfo;
	arith_entropy_encoder entropy;
	jpeg_source_mgr src;

	cinfo->src=&src;
	cinfo->dest=&main_dest;
	cinfo->buffer_count=0;
	cinfo->entropy=&entropy;
	main_cinfo.outbuffer=(JOCTET *)m_pDpcm.Alloc_One_Row(cinfo,(size_t) (OUTPUT_BUF_SIZE)); /*sizeof(JOCTET) is 1 */
	main_dest.next_output_byte =cinfo->outbuffer;
	main_dest.free_in_buffer = OUTPUT_BUF_SIZE;
	cinfo->arith_dc_L=1;
	cinfo->first_line=0;
	cinfo->unread_marker=0;
	MEMZERO(cinfo->entropy->dc_stats,160);
	MEMZERO(cinfo->entropy->context_b,1000);
	MEMZERO(cinfo->entropy->val_b,1000);
	MEMZERO(cinfo->pre.N,365*4);
	MEMZERO(cinfo->pre.B,365*4);
	MEMZERO(cinfo->pre.C,365*4);
	cinfo->pre.reset=64;    

	cinfo->arith_dc_U=2;
	cinfo->entropy->c = 0;
	cinfo->entropy->a = 0x10000;
	cinfo->entropy->sc = 0;
	cinfo->entropy->zc = 0;
	cinfo->entropy->ct = 0;
	cinfo->entropy->last_val=0;
	cinfo->entropy->context=0;
	cinfo->entropy->buffer = -1;            /* empty */ 

	CString strOpenFileType = "DPCM压缩文件 (*.img)|*.img; *.img||";
	CFileDialog FileDlg(TRUE, "*.img", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strOpenFileType);

	if (FileDlg.DoModal() == IDOK)
	{ 
		if((cinfo->inputfile=fopen(FileDlg.GetPathName(),"rb"))==NULL)
		{
			MessageBox("打开文件错误!", "错误信息", MB_OK|MB_ICONERROR);
			return;
		}

		if((cinfo->outputfile=fopen("d:\\Hello.jpg","wb+"))==NULL)
		{
			MessageBox("创建文件失败", "错误信息", MB_OK|MB_ICONERROR);
			return;
		}

		if (! ReadOK(cinfo->inputfile, fileheader, 6))
		{
			MessageBox("error when read the head of file!", "错误信息", MB_OK|MB_ICONERROR);
			return;
		}

		cinfo->image_width = (UINT16) GET_2B(fileheader,2);
		cinfo->image_height = (UINT16) GET_2B(fileheader,4);

		cinfo->inbuffer=m_pDpcm.Alloc_Sarray((JDIMENSION) (4096),
			1,cinfo);                /*the inbuffer is a row of image elements now for*
									 *one demintion prediction.
									 */

		src.next_input_byte=cinfo->inbuffer[0];
		src.bytes_in_buffer=0;
		/*create the quality and huff table*/
		/*create_huff (cinfo); It is keeped for later compareration*/

		/*write the proper file header,for simplisity we only reserve the SOI and EOI symbole*/
		m_pDpcm.Write_File_Header(cinfo);

		/*prepare for the compresion*/
		/*jpeg_make_c_derived_tbl (cinfo, 1,
		*		      &(cinfo->d_dc_huff_tbl_ptr));
		*jpeg_make_c_derived_tbl (cinfo, 0,
		*		      &(cinfo->d_ac_huff_tbl_ptr));
		*/
		/*Enthopy coding and write to the JPEG file*/
		m_pDpcm.Initial_Decoder(cinfo);
		for (i=0;i<(cinfo->image_height);i++)
		{
			m_pDpcm.Decode_Row(cinfo,i);
		} 

		/*kill all the buffer and output EOI marker*/
		/*out put the patial image data*/
		m_pDpcm.Write_File_Trailer(cinfo); 
		JFWRITE(cinfo->outputfile, cinfo->outbuffer, 
			(((size_t) (OUTPUT_BUF_SIZE))-cinfo->dest->free_in_buffer));

		m_pDpcm.Free_Mem(cinfo);
		/*fseek(cinfo->inputfile,-2,2);*/
		p=0;
		while(getc(cinfo->inputfile)!=EOF)	
			p++; 
		fclose(cinfo->inputfile);
		fclose(cinfo->outputfile);
	}
}

⌨️ 快捷键说明

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