0

I'm trying to filter out comments from PPM file (P6) but I have no idea how to do it. I tried catching '#'s and from then skipping every character until '\n' but it doesn't work :/ or I messed up somewhere (mostly just giving me endless loops). My code looks like this, I do this for every value so there is a lot of loops. Sorry if my code looks like a mess, I've been trying to do it for a few days now. I'm trying to do it as i go through the file but maybe there is a better way to do it.

void ImagePPM::Read(const std::string& path){
unsigned char dump;

        for (int i = 0; i < imageHeight ; ++i) {
            for (int j = 0; j < imageWidth; ++j) {

                f.read((char *) &dump, sizeof(char));
                if(!isspace(dump)){
                    if(dump=='#'){
                        while(dump!='\n'){
                            f.read((char *) &dump, sizeof(char));
                        }
                    }
                    imagePixels[i][j].r = dump;
                }

                f.read((char *) &dump, sizeof(char));
                if(!isspace(dump)){
                    if(dump=='#'){
                        while(dump!='\n'){
                            f.read((char *) &dump, sizeof(char));
                        }
                    }
                    imagePixels[i][j].g = dump;
                }

                f.read((char *) &dump, sizeof(char));
                if(!isspace(dump)){
                    if(dump=='#'){
                        while(dump!='\n'){
                            f.read((char *) &dump, sizeof(char));
                        }
                    }
                    imagePixels[i][j].b = dump;
                }

              
            }
}

EDIT : This is my Write funcion. After reading the image as stated above if i use this to Write it something breaks my image and im not sure what. I know p3 file can have max value bigger than 255 will fix it later.

void ImagePPM::Write(const std::string& path) {
    std::ofstream f;
    f.open(path,std::ios::out,std::ios::binary);

    if(type == PPMtype::P3PPM){
        f << "P3" << std::endl;
        f << imageWidth << std::endl;
        f << imageHeight << std::endl;
        f << 255 << std::endl;

        for (int i = 0; i < imageWidth; ++i) {
            for (int j = 0; j < imageHeight; ++j) {
                f << (int) imagePixels[i][j].r << ' ';
                f << (int) imagePixels[i][j].g << ' ';
                f << (int) imagePixels[i][j].b << '\n';
            }
        }




    }else{
        f << "P6" << std::endl;
        f << imageWidth << std::endl;
        f << imageHeight << std::endl;
        f << maxValue << std::endl;

        for (int i = 0; i < imageHeight; ++i) {
            for (int j = 0; j < imageWidth; ++j) {
                f.write((char *) &imagePixels[i][j], sizeof(Pixel));
            }

        }
    }
    f.close();
    std::cout<< "IMAGE SAVED" << std::endl;



}
egrees
  • 11
  • 2
  • So what is the issue you are facing? – kiner_shah Jan 07 '22 at 05:55
  • `P6` indicates the _binary_ format of the [PPM format](https://en.wikipedia.org/wiki/Netpbm). Filtering out comments will clearly corrupt the image contents. Why do you think the file may contain comments? – Scheff's Cat Jan 07 '22 at 08:50
  • FYI: my answer to [SO: Scaling png font down](https://stackoverflow.com/a/56850226/7478597). It contains PPM file I/O using format `P6`. It handles comment lines which may occur at the beginning of the file (before the binary contents starts). (AFAIK...) – Scheff's Cat Jan 07 '22 at 08:59
  • As pointed out by @Scheff'sCat there cannot be comments in the binary part of a PPM file - they are only permitted in the initial header lines. – Mark Setchell Jan 07 '22 at 09:25
  • I was told they can be in the binary as well, but maybe you are right. Still something breaks my image and i don't know what. Do you guys think everything is correcty about this code? @Scheff'sCat i'm making a project whitch is supposed to handle any ppm image. – egrees Jan 07 '22 at 15:17
  • I updated my post with my Read function, maybe there you will see any problems. So far I'm not able to fix it. Image when saved appears "broken" – egrees Jan 07 '22 at 15:38
  • You didn't show your `#include` lines, nor the definition of your class... – Mark Setchell Jan 07 '22 at 18:02

0 Answers0