/* pngTest.cc * * Created by Robert R. Snapp, for CS 274, Copyright (C) 2006. */ #ifdef mac_osx #include // GLUT for the Macintosh #else #include // GLUT for Linux and Unix. #endif #include #include #include #include #include #include #include "rgbImage.h" // Custom template class /* Initial dimensions and attributes of the GUI Window */ const int INITIAL_WINDOW_WIDTH = 512; const int INITIAL_WINDOW_HEIGHT = 512; const int INITIAL_HORIZONTAL_OFFSET = 10; const int INITIAL_VERTICAL_OFFSET = 40; typedef struct rect_tag { float xmin; float xmax; float ymin; float ymax;} rect; /* World coordinate extrema */ const float XMIN = 0.0; const float XMAX = 1.0; const float YMIN = 0.0; const float YMAX = 1.0; const rect defaultWorld= {XMIN, XMAX, YMIN, YMAX}; using namespace std; typedef struct glutWindowInfo_tag { int height; int width; std::string title; int id; rect world;} glutWindowInfo; glutWindowInfo inputWindow = {INITIAL_WINDOW_HEIGHT, INITIAL_WINDOW_WIDTH, "Image", 0, defaultWorld}; rgbImage inputImage; /* Function prototypes */ void display(void); void reshape(int w, int h); /* reshape is called each time the user changes the GUI window dimensions. It updates the viewport and the world coordinate -> device coordinate transformation */ void reshape(int w, int h) { int currentWindowID = glutGetWindow(); glutWindowInfo* wind; if (currentWindowID == inputWindow.id) { wind = &inputWindow; } wind->width = w; wind->height = h; glViewport(0, 0, (GLsizei) w, (GLsizei) h); /* We will draw in the entire window */ /* using an orthographic projection */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D((wind->world).xmin, (wind->world).xmax, (wind->world).ymin, (wind->world).ymax); glMatrixMode(GL_MODELVIEW); glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */ } /* display is called each time a portion of the window is exposed, or the window is resized. It is always called after reshape. It generates a recursive illustration of Sierpinski's triangular gasket. */ void display( void ) { int currentWindowID = glutGetWindow(); glClear(GL_COLOR_BUFFER_BIT); // clear the window (paint it with the background color) glRasterPos2i(0,0); if (currentWindowID == inputWindow.id) { glPixelZoom(1.0, 1.0); inputImage.drawPixels(); } glFlush(); /* clear buffers */ } void keyboard(unsigned char c, int x, int y) { switch(c) { case 'q': case 'Q': exit(0); break; } return; } int main(int argc, char** argv) { glutInit(&argc,argv); if (argc < 2) { std::cerr << "Syntax: " << argv[0] << " " << std::endl; exit(1); } inputImage.readPNGfile(argv[1]); // Load the image inputWindow.width = inputImage.cols(); inputWindow.height = inputImage.rows(); inputWindow.title = static_cast(argv[1]); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */ glutInitWindowSize(inputWindow.width, inputWindow.height); glutInitWindowPosition(INITIAL_HORIZONTAL_OFFSET, INITIAL_VERTICAL_OFFSET); inputWindow.id = glutCreateWindow(inputWindow.title.c_str()); glutReshapeFunc(reshape); // register reshape as a callback function glutDisplayFunc(display); // register display as a callback function glutKeyboardFunc(keyboard); glutMainLoop(); // enter event loop return 0; }