简体   繁体   中英

C++ - Trying to read a .PPM image, unexpected output

I'm developing a uni project for reading the image data of P6-type, 255-depth .ppm images. The problem I encounter is that when I try to print the average values of each color (R,G,B) for an Image, the output I get is wrong (the proffessor has given us an output file which says what float values to expect for each given image).

Now, I'm at a loss here. Through many checks, I have concluded that the function reads the whole data from the image, without leaving out pixels or whatever, converts them correctly from 0-255 to 0.f - 1.f values (by dividing with 255.0), adds every red, every green and every blue value to three seperate counters and then divides them by the Width*Height of the given image to get the desired average brightness of each colour. I will provide part of the function that does this process for calculating the average red for a 960*642 Image (sorry for the hardcoded stuff, it's just there for debugging purposes).

The output I get for this is 0.58... when it should be 0.539068. seekg() is called with 14 as an argument because position 14 is the last space after the header and before the data. Could you provide any insight to why this isn't working as expected? One thing I found through the checks is the sum I get after adding all the red float values, is not a float but an int. Possible loss of data? I'm grasping at straws here.

Here is the code:

    std::ifstream infile;
    infile.open("Image02.ppm", std::ios::in | std::ios::binary);
    const unsigned char* buffer;
    float * data_ptr;

    infile.seekg(0, std::ios::end);
    int length = infile.tellg();  //calculating length of data

    buffer = new unsigned char[length];
    ptr = new unsigned char[length];
    data_ptr = new float[length];

    infile.seekg(14, std::ios::beg);  //restoring pointer to the start of data stream
    infile.read((char*)buffer, length);  //reading the image

    for (int i = 0; i < length; i++){     //casting the char data to floats to get the 0-255 values

        data_ptr[i] = ((float)buffer[i]);
        data_ptr[i] = data_ptr[i] / 255.f;  // converting to 0.0 - 1.0
    }

    int j = 0;
    float a = 0.f;
    while (j < length){   //calculating sum of red pixel values

        a = a + data_ptr[j];

        j = j + 3;
    }

    std::cout << a / (960*642);   //calculating average

FYI, PPM image files that are P6 have their image data stored from left to right, with the first line being line 0 and the last line of the image being the last. They are structured like this RGBRGBRGB so on, where the first RGB correspond to the first pixel and so forth.

Thanks in advance!

You need pixels only for average calculation. But in your source code, additional 14 garbage values are being used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM