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

📄 ycrcb2rgbdlg.cpp

📁 我自己寫的RGB與YCbCr(YUV)色彩格式轉換,可以RGB轉YCbCr或YCbCr轉RGB,並支持顯示轉換後的色彩功能.
💻 CPP
字号:
// YCrCb2RGBDlg.cpp : implementation file
//

#include "stdafx.h"
#include "YCrCb2RGB.h"
#include "YCrCb2RGBDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CYCrCb2RGBDlg dialog

CYCrCb2RGBDlg::CYCrCb2RGBDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CYCrCb2RGBDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CYCrCb2RGBDlg)
	m_E1 = 0;
	m_E2 = 0;
	m_E3 = 0;
	m_E4 = 0;
	m_E5 = 0;
	m_E6 = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CYCrCb2RGBDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CYCrCb2RGBDlg)
	DDX_Text(pDX, IDC_EDIT1, m_E1);
	DDV_MinMaxByte(pDX, m_E1, 0, 255);
	DDX_Text(pDX, IDC_EDIT2, m_E2);
	DDV_MinMaxByte(pDX, m_E2, 0, 255);
	DDX_Text(pDX, IDC_EDIT3, m_E3);
	DDV_MinMaxByte(pDX, m_E3, 0, 255);
	DDX_Text(pDX, IDC_EDIT4, m_E4);
	DDV_MinMaxByte(pDX, m_E4, 0, 255);
	DDX_Text(pDX, IDC_EDIT5, m_E5);
	DDV_MinMaxByte(pDX, m_E5, 0, 255);
	DDX_Text(pDX, IDC_EDIT6, m_E6);
	DDV_MinMaxByte(pDX, m_E6, 0, 255);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CYCrCb2RGBDlg, CDialog)
	//{{AFX_MSG_MAP(CYCrCb2RGBDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
	ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
	ON_EN_CHANGE(IDC_EDIT3, OnChangeEdit3)
	ON_EN_CHANGE(IDC_EDIT4, OnChangeEdit4)
	ON_EN_CHANGE(IDC_EDIT5, OnChangeEdit5)
	ON_EN_CHANGE(IDC_EDIT6, OnChangeEdit6)
	ON_EN_ERRSPACE(IDC_EDIT1, OnErrspaceEdit1)
	ON_EN_HSCROLL(IDC_EDIT1, OnHscrollEdit1)
	ON_BN_DOUBLECLICKED(IDC_BUTTON1, OnDoubleclickedButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CYCrCb2RGBDlg message handlers

BOOL CYCrCb2RGBDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	//IDC_EDIT1.m_E1=(1);
	//OnChangeEdit1.m_E1=setWndText();
	//OnChangeEdit1.m_E1=setWindowText(1);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CYCrCb2RGBDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CYCrCb2RGBDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CYCrCb2RGBDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CYCrCb2RGBDlg::OnButton1()  // YCbCr ===> RGB 
{
	// TODO: Add your control notification handler code here
	CClientDC DC(this);
	TEXTMETRIC tm;
	
	char m_str[40];
	char m_Y[6];
	char m_Cb[6];
	char m_Cr[6];
	char m_R[6];
	char m_G[6];
	char m_B[6];
	BYTE Y,Cb,Cr,r,g,b;
	double R,G,B;

	Y=GetDlgItemInt(IDC_EDIT1);
	Cb=GetDlgItemInt(IDC_EDIT2);
	Cr=GetDlgItemInt(IDC_EDIT3);	
	DC.SetTextColor(RGB(0,0,0));
	DC.SetBkColor(RGB(190,190,190));
	wsprintf(m_Y,"0x%X  ",Y);
	DC.TextOut(27,30,m_Y,strlen(m_Y));
	wsprintf(m_Cb,"0x%X  ",Cb);
	DC.TextOut(27,55,m_Cb,strlen(m_Cb));
	wsprintf(m_Cr,"0x%X  ",Cr);
	DC.TextOut(27,79,m_Cr,strlen(m_Cr));

	R=Y+(1.402*(Cr-128));
	G=Y-(0.71414*(Cr-128))-(0.34414*(Cb-128));
	B=Y+(1.772*(Cb-128));
	if (R>256){R=255;};
	if (R<0){R=0;};
	if (G>256){G=255;};
	if (G<0){G=0;};
	if (B>256){B=255;};
	if (B<0){B=0;};

	SetDlgItemInt(IDC_EDIT4,R);
	SetDlgItemInt(IDC_EDIT5,G);
	SetDlgItemInt(IDC_EDIT6,B);
	
	r=R;
	g=G;
	b=B;
	DC.SetTextColor(RGB(0,0,0));
	DC.SetBkColor(RGB(190,190,190));
	wsprintf(m_R,"0x%X  ",r);
	DC.TextOut(290,30,m_R,strlen(m_R));
	wsprintf(m_G,"0x%X  ",g);
	DC.TextOut(290,55,m_G,strlen(m_G));
	wsprintf(m_B,"0x%X  ",b);
	DC.TextOut(290,79,m_B,strlen(m_B));

	DC.SetTextColor(RGB(R,G,B));
	DC.SetBkColor(RGB(R,G,B));
	DC.GetTextMetrics(&tm);
	wsprintf(m_str,"                                                         ",tm.tmHeight);
	DC.TextOut(7,105,m_str,strlen(m_str));
}

void CYCrCb2RGBDlg::OnButton2()  // RGB ===> YCbCr
{
	// TODO: Add your control notification handler code here
	CClientDC DC(this);
	TEXTMETRIC tm;
	
	char m_str[40];
	char m_R[6];
	char m_G[6];
	char m_B[6];
	char m_Y[6];
	char m_Cb[6];
	char m_Cr[6];

	BYTE R,G,B,y,cb,cr;
	double Y,Cb,Cr;

	R=GetDlgItemInt(IDC_EDIT4);
	G=GetDlgItemInt(IDC_EDIT5);
	B=GetDlgItemInt(IDC_EDIT6);
	DC.SetTextColor(RGB(0,0,0));
	DC.SetBkColor(RGB(190,190,190));
	wsprintf(m_R,"0x%X  ",R);
	DC.TextOut(290,30,m_R,strlen(m_R));
	wsprintf(m_G,"0x%X  ",G);
	DC.TextOut(290,55,m_G,strlen(m_G));
	wsprintf(m_B,"0x%X  ",B);
	DC.TextOut(290,79,m_B,strlen(m_B));

	Y=(0.299*R)+(0.587*G)+(0.114*B);
	Cb=(-0.1687*R)-(0.3313*G)+(0.5*B)+128;
	Cr=(0.5*R)-(0.4187*G)-(0.0813*B)+128;
	if (Y>256){Y=255;};
	if (Y<0){Y=0;};
	if (Cb>256){Cb=255;};
	if (Cb<0){Cb=0;};
	if (Cr>256){Cr=255;};
	if (Cr<0){Cr=0;};

	SetDlgItemInt(IDC_EDIT1,Y);
	SetDlgItemInt(IDC_EDIT2,Cb);
	SetDlgItemInt(IDC_EDIT3,Cr);

	y=Y;
	cb=Cb;
	cr=Cr;
	DC.SetTextColor(RGB(0,0,0));
	DC.SetBkColor(RGB(190,190,190));
	wsprintf(m_Y,"0x%X  ",y);
	DC.TextOut(27,30,m_Y,strlen(m_Y));
	wsprintf(m_Cb,"0x%X  ",cb);
	DC.TextOut(27,55,m_Cb,strlen(m_Cb));
	wsprintf(m_Cr,"0x%X  ",cr);
	DC.TextOut(27,79,m_Cr,strlen(m_Cr));

	DC.SetTextColor(RGB(R,G,B));
	DC.SetBkColor(RGB(R,G,B));
	DC.GetTextMetrics(&tm);
	wsprintf(m_str,"                                                         ",tm.tmHeight);
	DC.TextOut(7,105,m_str,strlen(m_str));

}

void CYCrCb2RGBDlg::OnChangeEdit1() //Y
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	CClientDC DC(this);
	TEXTMETRIC tm;
	char m_Y[6];
	BYTE Y;
	Y=GetDlgItemInt(IDC_EDIT1);
	DC.SetTextColor(RGB(0,0,0));
	DC.SetBkColor(RGB(190,190,190));
	wsprintf(m_Y,"0x%X  ",Y);
	DC.TextOut(27,30,m_Y,strlen(m_Y));
}

void CYCrCb2RGBDlg::OnChangeEdit2() //Cb
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	CClientDC DC(this);
	TEXTMETRIC tm;
	char m_Cb[6];
	BYTE Cb;
	Cb=GetDlgItemInt(IDC_EDIT2);
	DC.SetTextColor(RGB(0,0,0));
	DC.SetBkColor(RGB(190,190,190));
	wsprintf(m_Cb,"0x%X  ",Cb);
	DC.TextOut(27,55,m_Cb,strlen(m_Cb));
}

void CYCrCb2RGBDlg::OnChangeEdit3() //Cr
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	CClientDC DC(this);
	TEXTMETRIC tm;
	char m_Cr[6];
	BYTE Cr;
	Cr=GetDlgItemInt(IDC_EDIT3);	
	DC.SetTextColor(RGB(0,0,0));
	DC.SetBkColor(RGB(190,190,190));
	wsprintf(m_Cr,"0x%X  ",Cr);
	DC.TextOut(27,79,m_Cr,strlen(m_Cr));
}

void CYCrCb2RGBDlg::OnChangeEdit4() //R
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	CClientDC DC(this);
	TEXTMETRIC tm;
	char m_R[6];
	BYTE R;
	R=GetDlgItemInt(IDC_EDIT4);
	DC.SetTextColor(RGB(0,0,0));
	DC.SetBkColor(RGB(190,190,190));
	wsprintf(m_R,"0x%X  ",R);
	DC.TextOut(290,30,m_R,strlen(m_R));
}

void CYCrCb2RGBDlg::OnChangeEdit5() //G
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	CClientDC DC(this);
	TEXTMETRIC tm;
	char m_G[6];
	BYTE G;
	G=GetDlgItemInt(IDC_EDIT5);
	DC.SetTextColor(RGB(0,0,0));
	DC.SetBkColor(RGB(190,190,190));
	wsprintf(m_G,"0x%X  ",G);
	DC.TextOut(290,55,m_G,strlen(m_G));
}

void CYCrCb2RGBDlg::OnChangeEdit6() //B
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	CClientDC DC(this);
	TEXTMETRIC tm;
	char m_B[6];
	BYTE B;
	B=GetDlgItemInt(IDC_EDIT6);
	DC.SetTextColor(RGB(0,0,0));
	DC.SetBkColor(RGB(190,190,190));
	wsprintf(m_B,"0x%X  ",B);
	DC.TextOut(290,79,m_B,strlen(m_B));
}

void CYCrCb2RGBDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

void CYCrCb2RGBDlg::OnErrspaceEdit1() 
{
	// TODO: Add your control notification handler code here
	
}

void CYCrCb2RGBDlg::OnHscrollEdit1() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_SCROLL flag ORed into the lParam mask.
	
	// TODO: Add your control notification handler code here
	
}

void CYCrCb2RGBDlg::OnDoubleclickedButton1() 
{
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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