Search code examples
c++ifstreamreinterpret-castunsigned-char

ifstream::read not reading unsigned char, even with reinterpret_cast


I am trying to get my code to read a PPM image (P3) and it isn't working as it should. The idea is to get a 3 unsigned chars and store them in RGB. But at the moment it is only resulting in taking the first character and ignoring the rest.

Image Image::readImg(std::string const &filename) {
    std::ifstream ifs;
    ifs.open(filename.c_str(), std::ios::binary);
    Image _in;
    try {
        if (ifs.fail()) {
            throw("Can't open input file");
        }
        std::string header;
        int w, h, max;
        ifs >> header;
        if (strcmp(header.c_str(), "P3") != 0) throw("Can't read input file");
        ifs >> w >> h >> max;
        _in.init(w, h);
        ifs.ignore(256, '\n');
        unsigned char pix[3];
        for (int i = 0; i < h; ++i){
            for (int j = 0; j < w; ++j){
                ifs.read(reinterpret_cast<char *>(pix), 3);
                _in.pixels[i][j].R = pix[0];
                _in.pixels[i][j].G = pix[1];
                _in.pixels[i][j].B = pix[2];
            }
        }
        std::cout << "|" << _in.pixels[0][0].R << " " << _in.pixels[0][0].G << " " << _in.pixels[0][0].B << "|";
        ifs.close();
    }
    catch (const char *err) {
        fprintf(stderr, "%s\n", err);
        ifs.close();
    }
    return _in;
}

Note the std::cout is supposed to output in my scenario |186 0 255|, but instead I get |1 8 6|.


EDIT: The file (original.ppm), when opened in Notepad++, looks like this (UNIX / UTF-8):

P3
1024 768
255
186 0 255 186 0 255 186 0 255 186 0 255 186 0 255 186 0 255 186 1 255 
186 1 254 186 1 254 185 2 254 185 2 254 185 1 254 185 2 253 185 3 253 
185 2 252 185 3 252 185 3 252 185 3 252 185 3 251 184 4 251 184 4 251 
184 4 251 184 4 251 184 5 250 184 5 250 183 5 250 183 6 249 183 6 249 
183 6 249 183 6 248 183 7 249 183 7 249 183 7 248 183 7 247 183 8 247 
182 7 247 182 8 246 183 9 247 183 9 246 183 9 246 182 9 246 181 9 246 
182 9 246 181 10 245 182 10 245 181 10 244 181 10 245 181 11 244 181 11 244
...

The result should be: _in.pixels[0][0].R = 186 _in.pixels[0][0].G = 0 _in.pixels[0][0].B = 255 and to continue collecting the RGB of all the pixels in the file.


Solution

  • [Updated] This should work:

    int pix[3]; // not an unsigned char
    ...
    ifs >> pix[0] >> pix[1] >> pix[2];
    

    instead of:

    ifs.read(reinterpret_cast<char *>(pix), 3);
    

    Like you do for width and height?

    ifs >> w >> h >> max;