/* * readImage.c * This program reads a raw image and displays it. * Author: Judy Challinger * * To create a raw image from another image type, use * the "convert" utility that is part of ImageMagick * on the Unix system. Try "man convert." For example: * * convert -flip CindiPup.jpg CindiPup.rgb * . */ #include #include #include GLubyte *imageData; GLsizei width, height; /* * Reads a raw image file into an array. * * filename - name of the raw image file * width - width of the image * height - height of the image * * returns - pointer to an array of bytes containing the image data */ GLubyte* readImage(const char* filename, GLsizei width, GLsizei height) { FILE * file; // open file file = fopen(filename, "rb"); if (file == NULL) return 0; // allocate back buffer imageData = (GLubyte*) malloc(width * height * 3); // read data fread(imageData, width * height * 3, 1, file); fclose(file); return imageData; } /** * Initializes OpenGL */ void init(void) { glPixelStorei (GL_UNPACK_ALIGNMENT, 1); glClearColor (0.0, 0.0, 0.0, 0.0); } /* * Display callback */ void display(void) { glClear(GL_COLOR_BUFFER_BIT); glRasterPos2i(0, 0); glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, imageData); glFlush(); } /* * Reshape callback * * w - new window width * h - new window height */ void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho (0, w, 0, h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); } /* * Keyboard callback * * key - the key that was pressed * x - cursor x coordinate * y - cursor y coordinate */ void keyboard(unsigned char key, int x, int y) { switch (key) { // escape key case 27: exit(0); } } /* * Main */ int main(int argc, char** argv) { width = 256; height = 256; imageData = readImage("CindiPup.rgb", width, height); glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(width, height); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); init(); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutMainLoop(); return 0; }