0

I'm writing a simple PGM file reader for a basic CV idea, and I'm having a weird issue. My method seems to work alright for symmetric files (255 x 255, for example), but when I try to read an asymmetric file (300 x 246), I get some weird input. One file reads to a certain point and then dumps ESCAPE characters (ASCII 27) into the remainder of the image (see below), and others just won't read. I think this might be some flawed logic or a memory issue. Any help would be appreciated.

enter image description hereenter image description here

        // Process files of binary type (P5)
    else if(holdString[1] == '5') {
        // Assign fileType value
        fileType = 5;

        // Read in comments and discard
        getline(fileIN, holdString);

        // Read in image Width value
        fileIN >> width;

        // Read in image Height value
        fileIN >> height;

        // Read in Maximum Grayscale Value
        fileIN >> max;

        // Determine byte size if Maximum value is over 256 (1 byte)
        if(max < 256) {
            // Collection variable for bytes
            char readChar;

            // Assign image dynamic memory
            *image = new int*[height];
            for(int index = 0; index < height; index++) {
                (*image)[index] = new int[width];
            }

            // Read in 1 byte at a time
            for(int row = 0; row < height; row++) {
                for(int column = 0; column < width; column++) {
                    fileIN.get(readChar);
                    (*image)[row][column] = (int) readChar;
                }
            }

            // Close the file
            fileIN.close();
        } else {
            // Assign image dynamic memory
            // Read in 2 bytes at a time
            // Close the file
        }
    }
  • Try creating a hardcoded image[row][column] where you aren't dynamically allocating memory. That will allow you to determine if its reading the file or if there is some other error with allocating the memory dynamically. Often in cases like this, the [y,x] index are reversed or are hardcoded to be like [x,x] somewhere. – Mark Aug 05 '15 at 01:01

1 Answers1

0

Tinkered with it a bit, and came up with at least most of a solution. Using the .read() function, I was able to draw the whole file in and then read it piece by piece into the int array. I kept the dynamic memory because I wanted to draw in files of different sizes, but I did pay more attention to how it was read into the array, so thank you for the suggestion, Mark. The edits seem to work well on files up to 1000 pixels wide or tall, which is fine for what I'm using it for. After, it distorts, but I'll still take that over not reading the file.

            if(max < 256) {
            // Collection variable for bytes
            int size = height * width;
            unsigned char* data = new unsigned char[size];

            // Assign image dynamic memory
            *image = new int*[height];
            for(int index = 0; index < height; index++) {
                (*image)[index] = new int[width];
            }

            // Read in 1 byte at a time
            fileIN.read(reinterpret_cast<char*>(data), size * sizeof(unsigned char));

            // Close the file
            fileIN.close();

            // Set data to the image
            for(int row = 0; row < height; row++) {
                for(int column = 0; column < width; column++) {
                    (*image)[row][column] = (int) data[row*width+column];
                }
            }

            // Delete temporary memory
            delete[] data;
        }