// // Adapted from: OpenGL, A Primer, E. Angel, Addison Wesley // #include #include #include int n; int m; GLubyte *image; void display(void) { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); glTexCoord2f(0.0,0.0); glVertex2i(10,10); glTexCoord2f(0.0,1.0); glVertex2i(10,m-11); glTexCoord2f(1.0,1.0); glVertex2i(n-11,m-11); glTexCoord2f(1.0,0.0); glVertex2i(n-11,10); glEnd(); glFlush(); } void myreshape(int h, int w) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, (GLfloat) n, 0.0, (GLfloat) m); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0,0,h,w); } int main(int argc, char **argv) { FILE *fd; int k, nm; char c; int i; char b[100]; printf("enter file name\n"); scanf("%s", b); fd = fopen(b, "rb"); // flag b may be not necessary on Unix fscanf(fd,"%[^\n] ", b); if(b[0]!='P'|| b[1] != '6') // P6 is binary encoded, P3 is ASCII { printf("%s is not a PPM file!\n", b); exit(0); } printf("%s is a PPM file\n",b); fscanf(fd, "%c",&c); while(c == '#') { fscanf(fd, "%[^\n] ", b); printf("%s\n",b); fscanf(fd, "%c",&c); } ungetc(c,fd); fscanf(fd, "%d %d %d ", &n, &m, &k); printf("%d rows %d columns max value= %d\n",n,m,k); nm = n*m; image=malloc(3 * sizeof(GLubyte) * nm); { GLubyte * tmp=malloc(3 * sizeof(GLubyte) * nm); fread(tmp, sizeof(GLubyte), 3 * nm, fd); // // The image in tmp is not in the correct order... // for(i=0;i