简体   繁体   中英

PPM file, reading and writing in C++

I am reading a ppm file, and then trying to save it, to find how accurate are my loading and saving module. But I end up getting different results on saving. I checked the pixel values with MATLAB, so I am pretty much sure that my loading module works well. The saving module is below:

ofstream ofs;
ofs.open("output.ppm", ofstream::out);
ofs<<"P6"<<endl;
ofs<<"# File after convolution"<<endl;
ofs<<img_wd<<" "<<img_ht<<endl; //check if ASCII conversion is needed
ofs<<max_val<<endl;

for(int j=0; j <img_ht;j++)
{
    for (int i=0; i<img_wd;i++)
    {
        ofs<<static_cast<char>(Pixel[j][i].r)<<static_cast<char>(Pixel[j][i].g)<<static_cast<char>(Pixel[j][i].b);  //write as ascii
    }
    ofs<<endl;
}

I am linking the actual file( https://github.com/aditisingh/Image_convolution_2D/blob/master/start_1.ppm ) and saved file( https://github.com/aditisingh/Image_convolution_2D/blob/master/output.ppm ), here. Any suggestions, inputs would be helpful. Thanks!

The reasons I think were, typecasting and whitespaces.

ofstream ofs;
ofs.open("output.ppm", ofstream::out);
ofs<<"P6\n"<<img_wd<<" "<<img_ht<<"\n"<<max_val<<"\n";

for(int j=0; j <img_ht;j++)
{
    for (int i=0; i<img_wd;i++)
    {
        ofs<<static_cast<unsigned char>(Pixel_tmp[j][i].r)<<static_cast<unsigned char>(Pixel_tmp[j][i].g)<<static_cast<unsigned char>(Pixel_tmp[j][i].b);   //write as ascii
    }
}

ofs.close();

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