Thread: Reading PPM format images

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    20

    Reading PPM format images

    I’m fairly new to C programming and I’ve been trying to figure out how to do this for 6 hours without any success.

    I’m trying to read this PPM format image, which contains an ASCII header followed by binary pixel data. The header consists of something like this:

    P6
    650 652
    255

    P6 indicates that it is a PPM image. The next two fields are the width and height of the image. The last field gives the maximum pixel value. At the end of the header is a \n and then the binary pixel data. The image is in color so there are three bytes (red, green, blue). The goal of my readPPM function is to return the pixel data in a one-dimensional array of unsigned chars, plus the width and height of the image. The goal of my writePPM function (I haven't done anything for that function yet) is to write the PPM format image to an empty file from the given information returned from my readPPM function. I'm still not sure how to make this program work so I don't store the 650 in width and 652 in height. I'll worry about that once I can actually read and write the text files.

    Now for practice, I’ve tried creating a text file that contains just the header and I tried reading and storing the numbers in my unsigned char* pixels, but I have trouble even reading that. It continuously prints 0 in the terminal nonstop. My readPPM function is quite a mess.

    I'm very frustrated, any help would be appreciated. Thanks.


    Error:
    g++ -c -Werror main.cc
    main.cc:15:17: error: variable-sized object may not be initialized
    unsigned char* pixelArray[size] = readPPM(fileName, &width, &height);

    1 error generated.
    make: *** [main.o] Error 1


    main.cc
    Code:
        int main() {
    
        char fileName[50] = "binary_pixels.txt";
        int width = 0;    // width of the image
        int height = 0;    // heigt of the image
        int size = 128;    // size of the array
    
        // read the PPM file and store its contents inside an array and return 
        // the pointer to that array to pixelArray
        unsigned char* pixelArray[size] = readPPM(fileName, &width, &height);
     
         // print the contents of the array
         for (int i = 0; i < 128; i++) {
             printf("%s\n", pixelArray[i]);
         } // end of for loop
        
        } // end of main

    readWritePPM.cc
    Code:
        unsigned char* readPPM(const char* fileName, int* width, int* height) {
    
        // allocate array for pixels
        unsigned char* pixels = new unsigned char[128];
    
        // open the file to read just the header reading
        FILE* fr = fopen(fileName, "r");
    
        // formatted read of header
        while (fscanf(fr, "%c", &pixels) != EOF) {
            printf("%c", *pixels);
        } // end of while
    
        // unformatted read of pixel data
    
        // close the file
        fclose(fr);
    
        // return the array
        return pixels;
        
        } // end of readPPM

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted here and here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading and Displaying Other Format Images (than bmp)
    By Brokenhope in forum Windows Programming
    Replies: 13
    Last Post: 01-28-2011, 06:42 AM
  2. Problem reading 8 bit greyscale images !
    By Clueless@work in forum C Programming
    Replies: 12
    Last Post: 07-01-2007, 11:07 AM
  3. reading images
    By hkl01 in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 10:25 PM
  4. Reading color tiff images?
    By compz in forum Game Programming
    Replies: 1
    Last Post: 11-21-2003, 12:48 AM

Tags for this Thread