#ifdef WIN32 #include #endif #include #include #include #include #include int winWidth ; int winHeight ; int imgWidth ; int imgHeight ; int KPOP = 256 ; byte *sourceImage = NULL ; // functions to load PPM files int getHeaderPPM(FILE *fp, int *x, int *y) ; int loadPPM(FILE *fp, byte *im, int x, int y) ; byte * loadPPM(char *fname, byte *im) ; void init() { glMatrixMode(GL_PROJECTION) ; glLoadIdentity() ; gluOrtho2D(0,winWidth,0,winHeight) ; } 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(); glRasterPos3d(0,0,0.0); glDrawPixels(imgWidth,imgHeight, GL_RGB,GL_UNSIGNED_BYTE,sourceImage); glRasterPos3d(imgWidth,0,0.0) ; // Here you draw the new image. You will have to creat an RGB image // and use glDrawPixels(......) as above glFlush(); glutSwapBuffers(); } void handleKey(byte key, int x, int y) { switch (key) { case 'q': exit(0); break ; } } main(int argc, char *argv[]) { // read image char fname[100] ; if( argc == 1 ) strcpy(fname,"test.ppm") ; else strcpy(fname,argv[1]) ; // load the file sourceImage = loadPPM(fname, sourceImage) ; if( sourceImage == NULL ) exit(-1) ; // now we know the dimensions winWidth = 2*imgWidth ; winHeight = imgHeight ; glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowPosition (0, 0); glutInitWindowSize(winWidth,winHeight); int mainWindowID = glutCreateWindow(argv[0]); glutKeyboardFunc(handleKey); glutDisplayFunc(display); printf("Press 'q' to quit\n"); init() ; glutMainLoop(); return 0; // never reached } byte *loadPPM(char *fname, byte *im) { FILE *fp ; if(im != NULL) { free(im) ; im = NULL ; } fp = fopen(fname,"r") ; if( fp == NULL ) { fprintf(stderr,"loadPPM: Cannot open file %s.", fname) ; return NULL; } // read the header if( getHeaderPPM(fp, &imgWidth, &imgHeight) == -1 ) { fprintf(stderr,"loadPPM: Error reading file.") ; return NULL ; } if( imgWidth * imgHeight > 10000000 ) { fprintf(stderr,"loadPPM: File too big.") ; return NULL ; } // alocate image if((im = (GLubyte *) malloc(sizeof(GLubyte *)*imgWidth*imgHeight*3)) == NULL) { fprintf(stderr,"Not enough memory!\n") ; return NULL ; } // read the image if( loadPPM(fp, im, imgWidth, imgHeight) == -1 ) { fprintf(stderr,"ERROR: image was not loaded.\n") ; return NULL ; } /** for( i = 0 ; i < x ; i++ ) for( j = 0 ; j < y; j++) printf("%d %d %d\n", im[i][j][0],im[i][j][1], im[i][j][2]) ; **/ fclose(fp) ; return im ; } int getHeaderPPM(FILE *fp, int *x, int *y) { char s[256] ; s[0] = '\0' ; fgets(s,100,fp) ; if( strncmp(s, "P6", 2) != 0 ) { fprintf(stderr,"Wrong format must be P6.") ; return -1; } fgets(s,100,fp); if( sscanf(s, "%d %d", x, y) != 2 ) { fprintf(stderr,"Wrong format expected .") ; return -1; } fgets(s,100,fp); int maxCol ; if( sscanf(s, "%d", &maxCol) != 1 ) { fprintf(stderr,"Wrong format expected .") ; return -1; } return 1 ; } // Header must be read already int loadPPM(FILE *fp, byte *im, int x, int y) { int i,j; int count = 0 ; for( i = 0 ; i < x ; i++ ) for( j = 0 ; j < y; j++) { if(feof(fp)) { fprintf(stderr,"loadPPM: unexpected end of file.") ; return -1 ; } else { fread((void *) &im[count] , sizeof(GLubyte), 3, fp) ; count += 3 ; } } fprintf(stderr,"File read properly.") ; return 1 ; }