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

📄 mediaview.cpp

📁 绘制LCD的RGB和喷墨打印机的 CMYK到CIELab的三维彩色视图
💻 CPP
字号:
// MediaView.cpp : implementation of the CMediaView class
//

#include "stdafx.h"
#include "Media.h"

#include "MediaDoc.h"
#include "MediaView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMediaView

IMPLEMENT_DYNCREATE(CMediaView, CView)

BEGIN_MESSAGE_MAP(CMediaView, CView)
	//{{AFX_MSG_MAP(CMediaView)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CMediaView construction/destruction

CMediaView::CMediaView()
{
	// TODO: add construction code here
	 m_x = 0;
    m_y = 0;
    m_z = 0;
    IsRotate = FALSE;
	
	
}

CMediaView::~CMediaView()
{
}

BOOL CMediaView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	cs.style |=WS_CLIPCHILDREN | WS_CLIPSIBLINGS;  //   设置窗口类型
	
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMediaView drawing

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

	Drawscene();
	pDC->SetTextColor(RGB(0,0,255));
	CString str = "喷墨打印机的CMYK->Lab三维彩色视图";
	pDC->TextOut(15,15,str);
/*	CBrush *pOldBrush;
	CBrush brush1(RGB(255, 0, 0));
	pOldBrush = pDC -> SelectObject(&brush1);*/
	//	设置当前绘图设备为OpenGL的设备情景对象

}

/////////////////////////////////////////////////////////////////////////////
// CMediaView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMediaView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMediaView message handlers

int CMediaView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
    //初始化OpenGL

	    m_pDC = new CClientDC(this);
        ASSERT(m_pDC!=NULL);
	
    static PIXELFORMATDESCRIPTOR pfd=
    {
        sizeof(PIXELFORMATDESCRIPTOR),        // pfd结构的大小 
	    1,                                    // 版本号 
	    PFD_DRAW_TO_WINDOW|                   // 支持在窗口中绘图
	    PFD_SUPPORT_OPENGL|					  // 支持 OpenGL 
	    PFD_DOUBLEBUFFER,                     // 双缓存模式 
	    PFD_TYPE_RGBA,                        // RGBA 颜色模式 
	    24,                                   // 24 位颜色深度 
	    0,0,0,0,0,0,                          // 忽略颜色位 
	    0,                                    // 没有非透明度缓存 
	    0,                                    // 忽略移位位 
	    0,                                    // 无累加缓存 
	    0,0,0,0,                              // 忽略累加位 
 	    32,                                   // 32 位深度缓存     
	    0,                                    // 无模板缓存 
	    0,                                    // 无辅助缓存 
	    PFD_MAIN_PLANE,                       // 主层 
	    0,                                    // 保留 
	    0,0,0                                 // 忽略层,可见性和损毁掩模 

    };

    int pixelformat;
	int n;

	if ((pixelformat=ChoosePixelFormat(m_pDC->GetSafeHdc(),&pfd))==0)
    {
      MessageBox("ChoosePixelformat failed");    //选择像素格式失败
      return FALSE;
    }

    //设置像素格式
    if (SetPixelFormat(m_pDC->GetSafeHdc(),pixelformat,&pfd)==FALSE)
    {
      MessageBox("SetPixelFormat failed");   //设置像素格式失败
      return FALSE;
    }

	n=::GetPixelFormat(m_pDC->GetSafeHdc());
	::DescribePixelFormat(m_pDC->GetSafeHdc(),n,sizeof(pfd),&pfd);

	m_hRC = wglCreateContext(m_pDC->GetSafeHdc());
	wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);

    GetmyLAB();
	SetPointColor();
//	GetmyLAB1();
//	SetPointColor1();

	return 0;
}

void CMediaView::OnDestroy() 
{
	CView::OnDestroy();
	
	// TODO: Add your message handler code here
	// 释放内存资源
	if(wglGetCurrentContext() != NULL)
	{
		//make the rendering context not current 
		//wglMakeCurrent(NULL, NULL)来消除当前RC。
		wglMakeCurrent(NULL,NULL);
	}
	if(m_hRC != NULL)
	{
		//通过wglDeleteContext来删除RC
		wglDeleteContext(m_hRC);
		m_hRC = NULL;
	}

	if ( m_pDC )				//清空删除当前渲染绘制环境
	{
		delete m_pDC;
	}
	
}

void CMediaView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	if(m_hRC)
		wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
	else
		return;

	glViewport(0,0,cx,cy);
	//	设置变换模式为投影变换
	glMatrixMode(GL_PROJECTION);
	//	初始化投影变换矩阵
	glLoadIdentity();
	//	根据窗口大小设置调整正射投影矩阵
	if(cx<=cy)
		glOrtho(-4.0,4.0,-4.0*(GLfloat)cy/(GLfloat)cx,
			4.0*(GLfloat)cy/(GLfloat)cx,-2.0,2.0);
	else
		glOrtho(-4.0*(GLfloat)cx/(GLfloat)cy,
			4.0*(GLfloat)cx/(GLfloat)cy,-4.0,4.0,-2.0,2.0);
	//	设置变换模式为模型变换
	glMatrixMode(GL_MODELVIEW);
	//	初始化模型变换矩阵
	glLoadIdentity();
}

void CMediaView::GetmyLAB()
{
	int i, j;

	FILE *fp;

	fp=fopen("lab.txt", "rb");

	if(!fp)
	{
		AfxMessageBox("读取LAB数据错误 !");
		exit(1);
	}

	for(i = 0; i < 1286; i ++)
	{
		for (j = 0; j < 3; j ++)
		{
			fscanf(fp, "%f", &mLAB[i][j]);
			mLAB[i][j] = mLAB[i][j]/30.0f;  //使坐标值能在坐标轴所给范围内(最大范围为4)显示,每个值除以30;
		}
	}
    
	fclose(fp);
}

void CMediaView::SetPointColor()
{
	int i, j;

	FILE *fp;

	fp=fopen("cmyk.txt", "rb");

	if(!fp)
	{
		AfxMessageBox(" 读取CMYK数据错误 !");
		exit(1);
	}

	float CMYK[1286][4],CMY[1286][3];
	for(i = 0; i < 1286; i ++)
	{
		for (j = 0; j < 4; j ++)
		{    
			fscanf(fp, "%f", &CMYK[i][j]);
		    CMYK[i][j] = CMYK[i][j]/100.0;
		}
	}
    //cmyk 转化 cmy	
	for(i=0;i<1286;i++)
	{
		CMY[i][0] = CMYK[i][0]*(1-CMYK[i][3]) + CMYK[i][3];
		CMY[i][1] = CMYK[i][1]*(1-CMYK[i][3]) + CMYK[i][3];
		CMY[i][2] = CMYK[i][2]*(1-CMYK[i][3]) + CMYK[i][3];
	}
	//cmy  转化 rgb
	for(i=0;i<1286;i++)
	{
		mColor[i][0] = 1 - CMY[i][0];
        mColor[i][1] = 1 - CMY[i][1];
        mColor[i][2] = 1 - CMY[i][2];
		
	}
	fclose(fp);
}

void CMediaView::Drawscene()
{	
	if(m_hRC)
		wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
	else
		return;
    glRotated(m_x,1,0,0);
    glRotated(m_y,0,1,0);
    glRotated(m_z,0,1,1);

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);			//设置背景颜色为黑色不透明
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		//清颜色缓存与深度缓存	

	    glLineWidth(2.0);
		/*glBegin(GL_LINES);
		
        glColor3f( 1.0, 1.0, 1.0);    // L
		glVertex3f(-4.0, 0.0, 0.0);
		glColor3f( 1.0, 1.0, 1.0);
		glVertex3f(4.0, 0.0, 0.0);

        glColor3f( 1.0, 0.0, 0.0);    //a
		glVertex3f(0.0, -4.0, 0.0);
		glColor3f( 1.0, 0.0, 0.0);
		glVertex3f(0.0, 4.0, 0.0);
		
	
		glColor3f( 0.0, 0.0, 1.0);    // b
		glVertex3f(0.0, 0.0, -4.0);
		glColor3f( 1.0, 1.0, 0.0);
		glVertex3f(0.0, 0.0, 4.0);*/

     glBegin(GL_LINES);//画坐标系,由三条线段组成     
     glColor3f(1,0,0);     
    glVertex3f(0,0,0);     
    glVertex3f(4,0,0);//R坐标(+a)     
     glColor3f(0,1,0);     
    glVertex3f(0,0,0);     
    glVertex3f(-4,0,0);//G坐标(-a)  
	 glColor3f(1,1,1);     
    glVertex3f(0,-4,0);     
    glVertex3f(0,4,0);//L坐标     
     glColor3f(0,0,1);     
    glVertex3f(0,0,0);     
    glVertex3f(0,0,-4);//B坐标(-b)
	 glColor3f(1,1,0);     
    glVertex3f(0,0,0);     
    glVertex3f(0,0,4);//Y坐标(+b)

		glEnd();
    int i;
//画喷墨打印机的LAB图
	glLineWidth(1.0);
	glPointSize(2.5);
	glBegin(GL_POINTS);
	for (i = 0; i < 1286; i ++)
	{
	

			glColor3f(mColor[i][0], mColor[i][1], mColor[i][2]);

			glVertex3f(mLAB[i][1],mLAB[i][0],mLAB[i][2]);

	
	}	glEnd();

	::SwapBuffers(m_pDC->GetSafeHdc());						//设置缓存


}


void CMediaView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	IsRotate = TRUE;
	CView::OnLButtonDown(nFlags, point);
}

void CMediaView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	IsRotate = FALSE;
	CView::OnLButtonUp(nFlags, point);
}

void CMediaView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(IsRotate == TRUE)
	{
	m_x = point.x;
    m_y = point.y;
    m_z = point.x+point.y;
	Invalidate(FALSE);
	}
	CView::OnMouseMove(nFlags, point);
}

⌨️ 快捷键说明

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