Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 1 of 1

Thread: Problem with loading PPM

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2010
    Posts
    4

    Problem with loading PPM

    Hello,

    im trying load two ppm files and show the first image with glDrawPixels, but im only getting a white screen. This is the Code:

    Code :
    #include "../common/cg_common.h"
    #include <iostream>
    #include <fstream>
    #include <string>
     
    GLubyte* first_image;
    GLubyte* second_image;
    int win_width, win_height;
     
    void skipComment(FILE *fin)
    {
        char buf[120];
     
        while (true) {
           fscanf(fin, "%1s", &buf);
           if (buf[0] != '#') { 
               ungetc(buf[0], fin); 
               return; 
           }
           else fgets(buf, 120, fin);
        }
    }
     
    GLubyte *readPPM(FILE *fin, GLsizei *wid, GLsizei *ht)
    {
        GLubyte  *bytes;
        char cookie[3];
        int width, height, maxComp;
        int n, r,c;
     
        fscanf(fin, "%2s", &cookie);
     
        if (strcmp("P6", cookie)) 
            return NULL;
        skipComment(fin);
        fscanf(fin, "%d", &width); *wid = width;
        skipComment(fin);    
        fscanf(fin, "%d", &height); *ht = height;
        skipComment(fin);        
        fscanf(fin, "%d", &maxComp);
     
        if (maxComp > 255) 
            return NULL;
        fgetc(fin);    
     
        bytes = (GLubyte  *) malloc(width*height*3);
        if (bytes == NULL) 
            return NULL;
     
        for (r=0; r < height; r++)
          for (c=0; c < width; c++) {
             bytes[3*(r*width + c)] = fgetc(fin); 
             bytes[3*(r*width + c)+1] = fgetc(fin); 
             bytes[3*(r*width + c)+2] = fgetc(fin);
             //std::cout << "Rot: " << bytes[3*(r*width + c)] << " Gelb: " << bytes[3*(r*width + c)+1] << " Blau: " << bytes[3*(r*width + c)+2] << std::endl;
          }
     
        return bytes;
    }
     
    void initView() {
        glMatrixMode (GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0, 256.0, 0, 256.0);
        glMatrixMode(GL_MODELVIEW);
    }
     
    void init() {
        GLsizei width;
        GLsizei height;
        std::string filename = "image1.ppm";
     
        GLubyte* image = readPPM(fopen(filename.c_str(),"r"),&width,&height);
     
        if(image != NULL)
            first_image = image;
        else
            std::cout << filename << " konnte nicht richtig geladen werden " << std::endl;
     
        filename = "image2.ppm";
     
        image = readPPM(fopen(filename.c_str(),"r"),&width,&height);
     
        if(image != NULL)
            second_image = image;
        else
            std::cout << filename << " konnte nicht richtig geladen werden " << std::endl;
     
        win_height = height;
        win_width = width;
     
        initView();
    }
     
    void initGL() {
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // black background
    }
     
    void display() {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
        glDrawPixels(win_width,win_height,GL_RGB,GL_UNSIGNED_BYTE,first_image);
     
        glFlush();
    }
     
    void reshape(int /*width*/, int /*height*/) {
     
    }
     
    void key(unsigned char k, int /*x*/, int /*y*/) {
        if (k == 27)
            exit(0);
    }
     
    // Main function for bringing it all together
    int main(int argc, char** argv) {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGB);
        glutInitWindowSize(256, 256);
        glutCreateWindow("Aufgabe 3.6");
     
        initGL();
        init();
     
        // setup GLUT callbacks
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
        glutKeyboardFunc(key);
    //    glutIdleFunc(display);  // if continuous animation is required
     
        glutMainLoop(); // start the main event loop
    }

    These are the two images:
    http://www.file-upload.net/download-...mages.zip.html

    I have no Idea what im doing wrong

    thank you!

    *EDIT*: Okay i think i found the failure. The read method reads only -1 after a few correct numbers
    Last edited by raven22k; 05-01-2012 at 08:13 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •