#define GLUT_DISABLE_ATEXIT_HACK
#include
#include
#include
#include
#pragma comment(lib,"glaux.lib")
#pragma comment(lib, "glut32.lib")
AUX_RGBImageRec* Image;
GLint iWidth,iHeight,iMode=1;
GLubyte* pImage;
void MenuCreate(void)
{
glutAddMenuEntry("正常显示", 1);
glutAddMenuEntry("翻转图像", 2);
glutAddMenuEntry("缩小图像", 3);
glutAddMenuEntry("红色通道", 4);
glutAddMenuEntry("绿色通道", 5);
glutAddMenuEntry("蓝色通道", 6);
glutAddMenuEntry(" 灰度图 ", 7);
glutAddMenuEntry("反转颜色", 8);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void Initialization(void)
{
glClearColor(0,0,0,1);
MenuCreate();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Image = auxDIBImageLoad("11.bmp");
iWidth = Image->sizeX;
iHeight = Image->sizeY;
pImage = Image->data;
}
void OnDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
GLbyte *pModified = NULL;
GLfloat invert[256];
GLint i;
glRasterPos2i(0, 0);
switch(iMode)
{
case 2:
glPixelZoom(-1.0f, -1.0f);
glRasterPos2i(iWidth - 1, iHeight - 1);
break;
case 3:
glPixelZoom(0.5f, 0.5f);
break;
case 4:
glPixelTransferf(GL_RED_SCALE, 1.0f);
glPixelTransferf(GL_GREEN_SCALE, 0.0f);
glPixelTransferf(GL_BLUE_SCALE, 0.0f);
break;
case 5:
glPixelTransferf(GL_RED_SCALE, 0.0f);
glPixelTransferf(GL_GREEN_SCALE, 1.0f);
glPixelTransferf(GL_BLUE_SCALE, 0.0f);
break;
case 6:
glPixelTransferf(GL_RED_SCALE, 0.0f);
glPixelTransferf(GL_GREEN_SCALE, 0.0f);
glPixelTransferf(GL_BLUE_SCALE, 1.0f);
break;
case 7:
glDrawPixels(iWidth,iHeight, GL_RGB, GL_UNSIGNED_BYTE, pImage);
pModified = (GLbyte*)new GLbyte[iWidth * iHeight];
glPixelTransferf(GL_RED_SCALE, 0.3f);
glPixelTransferf(GL_GREEN_SCALE, 0.59f);
glPixelTransferf(GL_BLUE_BITS, 0.11f);
glReadPixels(0, 0, iWidth, iHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, pModified);
glPixelTransferf(GL_RED_SCALE, 1.0f);
glPixelTransferf(GL_GREEN_SCALE, 1.0f);
glPixelTransferf(GL_BLUE_SCALE, 1.0f);
break;
case 8:
invert[0] = 1.0f;
for(i = 1; i < 256; i++)
{
invert[i] = 1.0f - (1.0f / 255.0f * GLfloat(i));
}
glPixelMapfv(GL_PIXEL_MAP_R_TO_R, 255, invert);
glPixelMapfv(GL_PIXEL_MAP_G_TO_G, 255, invert);
glPixelMapfv(GL_PIXEL_MAP_B_TO_B, 255, invert);
glPixelTransferf(GL_MAP_COLOR,GL_TRUE);
break;
case 1:
default:
break;
}
if(pModified == NULL)
{
glDrawPixels(iWidth, iHeight, GL_RGB,GL_UNSIGNED_BYTE, pImage);
}
else
{
glDrawPixels(iWidth, iHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, pModified);
delete [] pModified;
}
glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
glPixelTransferf(GL_RED_SCALE, 1.0f);
glPixelTransferf(GL_GREEN_SCALE, 1.0f);
glPixelTransferf(GL_BLUE_SCALE, 1.0f);
glPixelZoom(1.0f, 1.0f);
glutSwapBuffers();
}
void OnReshape(int w,int h)
{
GLfloat aspectRatio=(GLfloat)w / (GLfloat)h;//计算窗口的纵横比
GLfloat winWidth,winHeight;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w <= h)
{ //根据纵横比计算绘图区域的宽和高
winWidth = 400;
winHeight = 400 * aspectRatio;
}
else
{
winWidth = 400 * aspectRatio;
winHeight= 400;
}
gluOrtho2D(0, winWidth, 0, winHeight);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void OnMenu(int value)
{
iMode = value;
glutPostRedisplay();
}
void main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("EXAM703");
glutCreateMenu(OnMenu);
glutDisplayFunc(OnDisplay);
glutReshapeFunc(OnReshape);
Initialization();
glutMainLoop();
}
运行结果: