// // cie.cpp: Program to display the CIE chromaticity diagram. // #ifdef WIN32 #include #endif #include #include #include #include #include #include #include #define byte unsigned char int winWidth = 300; int winHeight = 300; byte *img = NULL; float xMin = 0.0f; float xMax = 1.0f; float yMin = 0.0f; float yMax = 1.0f; void rc2xy (int row, int col, double *x, double *y); void xy2rc (double x, double y, int *row, int *col); void readCIEData(); double cieLambda[100]; double ciefX[100]; double ciefY[100]; double ciefZ[100]; int nCIE; ///////////////////////////////////////////////////////////// // // THE CODE THAT YOU MUST CHANGE IS IN THIS FUNCTION. // You will probably need to add other functions. // //////////////////////////////////////////////////////////// void generateDiagram() { int col,row; double x,y; byte r,g,b; int p; for (row=0; row> cieLambda[n] >> ciefX[n] >> ciefY[n] >> ciefZ[n]; } in.close(); nCIE = n; } ////////////////////////////////////////////////////// // // Take care of the window being re-sized. // (re-allocates the image buffer). // // **** DO NOT CHANGE **** // ////////////////////////////////////////////////////// void handleReshape(int w, int h) { winWidth = w; winHeight = h; // // Set the viewport // glViewport(0, 0, w, h); // resize the image to fit the screen. if (img != NULL) delete[] img; img = new byte[winWidth*winHeight*3]; generateDiagram(); // // Now start setting the projection matrix stack // glMatrixMode(GL_PROJECTION); glLoadIdentity(); double dx = (xMax-xMin)/2.0; double dy = (yMax-yMin)/2.0; glOrtho(-dx,dx, -dy,dy, 0.1, 2.0); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_PACK_ALIGNMENT, 1); } ////////////////////////////////////////////////////// // // Two convenience functions: // rc2xy converts pixel coords (row,col) to chromaticity (x,y) // xy2rc does the inverse conversion. // // **** DO NOT CHANGE **** // ////////////////////////////////////////////////////// void rc2xy (int row, int col, double *x, double *y) { *x = xMin + (double)col/winWidth * (xMax-xMin); *y = yMin + (double)(winHeight-row)/winHeight * (yMax-yMin); } ////////////////////////////////////////////////////// void xy2rc (double x, double y, int *row, int *col) { *col = (int) ((x-xMin) / (xMax-xMin) * winWidth); *row = (int) (winHeight - (y-yMin) / (yMax-yMin) * winHeight); } ////////////////////////////////////////////////////// // // Displays, on STDOUT, the colour of the pixel that // the user clicked on. // // **** DO NOT CHANGE **** // ////////////////////////////////////////////////////// void handleMouse (int button, int state, int col, int row) { // printf("handleMouse: but=%d state=%d (col row)=(%d %d)\n", // button,state,col,row); // printf("image at (%d %d)=(%d %d %d)\n",row,col,r,g,b); if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) { int i=(winHeight-row)*winWidth*3 + col*3; int r = img[i++]; int g = img[i++]; int b = img[i++]; double x,y; rc2xy (row,col, &x,&y); printf("(x y)=(%6.3lf %6.3lf) rgb=(%d %d %d)\n",x,y,r,g,b); } glutPostRedisplay(); } ////////////////////////////////////////////////////// // // Show the current diagram. // // **** DO NOT CHANGE **** // ////////////////////////////////////////////////////// void display () { glClearColor(.1f,.1f,.1f, 1.f); /* set the background colour */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt((xMin+xMax)/2.0, (yMin+yMax)/2.0, 1, (xMin+xMax)/2.0, (yMin+yMax)/2.0, 0.0, 0.0, 1.0, 0.0); glRasterPos3d(xMin,yMin,0.0); // printf("first pixel: (%d %d %d)\n",img[0],img[1],img[2]); glDrawPixels(winWidth,winHeight, GL_RGB,GL_UNSIGNED_BYTE,img); glFlush(); glutSwapBuffers(); } ////////////////////////////////////////////////////// // // Basically, quit if the user hits "q". // // **** DO NOT CHANGE **** // ////////////////////////////////////////////////////// void handleKey(byte key, int x, int y) { switch (key) { case 'q': exit(0); } } ////////////////////////////////////////////////////// // // Main program. // // **** DO NOT CHANGE **** // ////////////////////////////////////////////////////// main(int argc, char *argv[]) { readCIEData(); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowPosition (0, 0); glutInitWindowSize(winWidth,winHeight); int mainWindowID = glutCreateWindow(argv[0]); glutReshapeFunc (handleReshape); glutKeyboardFunc(handleKey); glutMouseFunc (handleMouse); glutDisplayFunc(display); printf("CIE diagram.\n"); printf("Press 'q' to quit\n"); glutMainLoop(); return 0; // never reached }