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

📄 feifeiview.cpp

📁 C++数字图像源代码
💻 CPP
字号:
// feifeiView.cpp : implementation of the CFeifeiView class
//

#include "stdafx.h"
#include "feifei.h"

#include "feifeiDoc.h"
#include "feifeiView.h"
#include "Jpgstd.h"
#include "bmpfile.h"
#include "jpeglib.h"

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

UINT gColor;
UINT gScale;
UINT gDct;
UINT step;
CString fileName = "";
BOOL gAdjust = FALSE;
int  drawnumber1=0;
int  drawnumber2=100;
int  num1,num2;
/////////////////////////////////////////////////////////////////////////////
// CFeifeiView

IMPLEMENT_DYNCREATE(CFeifeiView, CView)

BEGIN_MESSAGE_MAP(CFeifeiView, CView)
	//{{AFX_MSG_MAP(CFeifeiView)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_SIZEHALF, OnSizehalf)
	ON_COMMAND(ID_QUARTER, OnQuarter)
	ON_COMMAND(ID_HALFCUBIC, OnHalfcubic)
	ON_COMMAND(ID_YCBCR, OnYcbcr)
	ON_COMMAND(ID_RGB, OnRgb)
	ON_COMMAND(ID_GRAY, OnGray)
	ON_COMMAND(ID_DCTSLOW, OnDctslow)
	ON_COMMAND(ID_DCTFAST, OnDctfast)
	ON_COMMAND(ID_DCTFLOAT, OnDctfloat)
	ON_COMMAND(ID_STEP1, OnStep1)
	ON_COMMAND(ID_STEP2, OnStep2)
	ON_COMMAND(ID_STEP3, OnStep3)
	//}}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)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFeifeiView construction/destruction

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

	
}

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

	return CView::PreCreateWindow(cs);
}

void CFeifeiView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	
	
}
/////////////////////////////////////////////////////////////////////////////
// CFeifeiView drawing

void CFeifeiView::OnDraw(CDC* pDC)
{
	CFeifeiDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

}

/////////////////////////////////////////////////////////////////////////////
// CFeifeiView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CFeifeiView diagnostics

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

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

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

void CFeifeiView::DrawBMP()
{
    CFeifeiDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
   if (pDoc->m_buf==NULL) return;

	CDC *theDC = GetDC();

	if (theDC!=NULL) {

		CRect clientRect;
		GetClientRect(clientRect);

		// Center It
		int left = max(clientRect.left, ((clientRect.Width() - (int)pDoc->m_width) / 2));
		int top = max(clientRect.top, ((clientRect.Height() - (int)pDoc->m_height) / 2));

		// a 24-bit DIB is DWORD-aligned, vertically flipped and 
		// has Red and Blue bytes swapped. we already did the 
		// RGB->BGR and the flip when we read the images, now do
		// the DWORD-align

		BYTE *tmp;
		// DWORD-align for display
		tmp = Jpgstd::MakeDwordAlignedBuf(pDoc->m_buf,
										 pDoc->m_width,
										 pDoc->m_height,
										 &pDoc->m_widthDW);

		// set up a DIB 
		BITMAPINFOHEADER bmiHeader;
		bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
		bmiHeader.biWidth = pDoc->m_width;
		bmiHeader.biHeight = pDoc->m_height;
		bmiHeader.biPlanes = 1;
		bmiHeader.biBitCount = 24;
		bmiHeader.biCompression = BI_RGB;
		bmiHeader.biSizeImage = 0;
		bmiHeader.biXPelsPerMeter = 0;
		bmiHeader.biYPelsPerMeter = 0;
		bmiHeader.biClrUsed = 0;
		bmiHeader.biClrImportant = 0;


		// now blast it to the CDC passed in.
		// lines returns the number of lines actually displayed
		int lines = StretchDIBits(theDC->m_hDC,
									left-260+drawnumber1, top-drawnumber2,
									bmiHeader.biWidth,
									bmiHeader.biHeight,
									0,0,
									bmiHeader.biWidth,
									bmiHeader.biHeight,
									tmp,
									(LPBITMAPINFO)&bmiHeader,
									DIB_RGB_COLORS,
									SRCCOPY);

		delete [] tmp;

		ReleaseDC(theDC);
	}
	gScale=1;
	drawnumber2=100;
}
void CFeifeiView::Text(int x,int y)
{
    CFeifeiDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
   if (pDoc->m_buf==NULL) return;
	CDC *theDC = GetDC();

	if (theDC!=NULL) {
        CString info;
		info.Format("[%dx%d]", pDoc->m_width, pDoc->m_height);
		theDC->SetBkMode(TRANSPARENT);
		theDC->SetTextColor(RGB(0,0,0));
		theDC->TextOut(x,y, info);  
        ReleaseDC(theDC);
	}

}
/////////////////////////////////////////////////////////////////////////////
// CFeifeiView message handlers
void CFeifeiView::compress()
{ 
    CFeifeiDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
   if (pDoc->m_buf==NULL) return;

	LoadJPG(fileName);


	BOOL color=TRUE;
	CString file=fileName;
	// we vertical flip for display. undo that.

	Jpgstd::VertFlipBuf(pDoc->m_buf, pDoc->m_width * 3, pDoc->m_height);
	Jpgstd::BGRFromRGB(pDoc->m_buf, pDoc->m_width, pDoc->m_height);

	BOOL ok=Jpgstd::RGBToJpgstd(file, 
		                            pDoc->m_buf,
									pDoc->m_width,
									pDoc->m_height,
									color, 
									step);			// quality 

	LoadJPG(file);
	


}
void CFeifeiView::LoadJPG(CString fileName)
{

	// m_buf is the global buffer
    CFeifeiDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (pDoc->m_buf!=NULL) {
		delete [] pDoc->m_buf;
		pDoc->m_buf=NULL;
	}
   
	// read to buffer tmp
	pDoc->m_buf=Jpgstd::JpgstdToRGB(fileName, &pDoc->m_width, &pDoc->m_height);

	//////////////////////
	// set up for display

	// do this before DWORD-alignment!!!
	// this works on packed (not DWORD-aligned) buffers
	// swap red and blue for display
	Jpgstd::BGRFromRGB(pDoc->m_buf, pDoc->m_width, pDoc->m_height);

	// vertical flip for display
	Jpgstd::VertFlipBuf(pDoc->m_buf, pDoc->m_width * 3, pDoc->m_height);

}
void CFeifeiView::LoadBMP(CString fileName)
{   
	CFeifeiDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (pDoc->m_buf!=NULL) {
		delete [] pDoc->m_buf;
		pDoc->m_buf=NULL;
	}
   
	BMPFile theBmpFile;

	pDoc->m_buf=theBmpFile.LoadBMP(fileName, &pDoc->m_width, &pDoc->m_height);

	if ((pDoc->m_buf==NULL) || (theBmpFile.m_errorText!="OK")) 
	{
		AfxMessageBox(theBmpFile.m_errorText);
		pDoc->m_buf=NULL;
		return;
	}

	//////////////////////
	// set up for display

	// do this before DWORD-alignment!!!
	// this works on packed (not DWORD-aligned) buffers
	// swap red and blue for display
	Jpgstd::BGRFromRGB(pDoc->m_buf, pDoc->m_width, pDoc->m_height);

	// vertical flip for display
	Jpgstd::VertFlipBuf(pDoc->m_buf, pDoc->m_width * 3, pDoc->m_height);
	
}


void CFeifeiView::ReadFile() 
{    
    
	CString filt="JPG File (*.JPG)|*.JPG|BMP (*.BMP)|*.BMP|All files (*.*)|*.*||";
    
    // OPENFILENAME - so i can get to its Help page easily
	CFileDialog fileDlg(TRUE,"*.JPG","*.JPG",NULL,filt,this);

	fileDlg.m_ofn.Flags|=OFN_FILEMUSTEXIST;
	fileDlg.m_ofn.lpstrTitle="File to load";

	if (fileDlg.DoModal()==IDOK) {

		AfxGetApp()->DoWaitCursor(1);

		fileName=fileDlg.GetPathName();
		CString ext=fileName.Right(4);

		if (!ext.CompareNoCase(".JPG"))
			LoadJPG(fileName);
        if (!ext.CompareNoCase(".BMP"))
			LoadBMP(fileName);

		
		AfxGetApp()->DoWaitCursor(-1);

	}            
}

void CFeifeiView::OnFileOpen() 
{
	// TODO: Add your command handler code here
	drawnumber1=0;
	gColor=2;
	ReadFile();
	DrawBMP();
	num1=240;
	num2=200;
	Text(num1,num2);

}


void CFeifeiView::OnSizehalf() 
{
	// TODO: Add your command handler code here
	gScale=2;
	gAdjust=TRUE;
	drawnumber1=200; 
    //Invalidate(false);
    LoadJPG(fileName);
	DrawBMP();
    num1=355;
	num2=200;
	Text(num1,num2);
}

void CFeifeiView::OnQuarter() 
{
	// TODO: Add your command handler code here
	gScale=4;
	gAdjust=TRUE;
	drawnumber1=310; 
    //Invalidate(false);
	LoadJPG(fileName);
	DrawBMP();
    num1=440;
	num2=200;
	Text(num1,num2);
}

void CFeifeiView::OnHalfcubic() 
{
	// TODO: Add your command handler code here
	gScale=8;
	gAdjust=TRUE;
	drawnumber1=390; 
    //Invalidate(false);  
    LoadJPG(fileName);
	DrawBMP();
    num1=530;
	num2=200;
	Text(num1,num2);
}

void CFeifeiView::OnYcbcr() 
{
	// TODO: Add your command handler code here
	gAdjust=TRUE;
	drawnumber1=230;
	gColor=3;
    LoadJPG(fileName);
	DrawBMP();
   
}

void CFeifeiView::OnRgb() 
{
	// TODO: Add your command handler code here
	gAdjust=TRUE;
	drawnumber1=460;
	gColor=2;
    LoadJPG(fileName);
	DrawBMP();
  
}

void CFeifeiView::OnGray() 
{
	// TODO: Add your command handler code here
	gAdjust=TRUE;
	drawnumber1=460;
	gColor=1;
    LoadJPG(fileName);
	DrawBMP();
   
}

void CFeifeiView::OnDctslow() 
{
	// TODO: Add your command handler code here
	gAdjust=TRUE;
	drawnumber1=0;
	drawnumber2=-100;
	gDct=1;
    LoadJPG(fileName);
	DrawBMP();
    num1=240;
	num2=400;
	Text(num1,num2);
}

void CFeifeiView::OnDctfast() 
{
	// TODO: Add your command handler code here
	gAdjust=TRUE;
	drawnumber1=230;
	drawnumber2=-100;
	gDct=2;
    LoadJPG(fileName);
	DrawBMP();
    num1=420;
	num2=400;
	Text(num1,num2);
}

void CFeifeiView::OnDctfloat() 
{
	// TODO: Add your command handler code here
	gAdjust=TRUE;
	drawnumber1=460;
	drawnumber2=-100;
	gDct=3;
    LoadJPG(fileName);
	DrawBMP();
    num1=620;
	num2=400;
	Text(num1,num2);
}

void CFeifeiView::OnStep1() 
{
	// TODO: Add your command handler code here
	step=5;
	gAdjust=TRUE;
	drawnumber1=0;
	drawnumber2=-100;
	compress();
	DrawBMP();
}

void CFeifeiView::OnStep2() 
{
	// TODO: Add your command handler code here
	step=25;
	gAdjust=TRUE;
	drawnumber1=0;
	drawnumber2=-100;
	compress();
	DrawBMP();
}

void CFeifeiView::OnStep3() 
{
	// TODO: Add your command handler code here
	step=50;
	gAdjust=TRUE;
	drawnumber1=0;
	drawnumber2=-100;
	compress();
	DrawBMP();
}

⌨️ 快捷键说明

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