繁体   English   中英

PPM文件,用C ++读写

[英]PPM file, reading and writing in C++

我正在读取一个ppm文件,然后尝试将其保存,以查找加载和保存模块的准确性。 但是我最终在保存上得到了不同的结果。 我使用MATLAB检查了像素值,因此我非常确定我的加载模块运行良好。 保存模块如下:

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;
}

我正在链接实际文件( https://github.com/aditisingh/Image_convolution_2D/blob/master/start_1.ppm )和保存的文件( https://github.com/aditisingh/Image_convolution_2D/blob/master/output.ppm ), 这里。 任何建议,意见都会有所帮助。 谢谢!

我认为的原因是类型转换和空格。

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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