[英]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. 我正在读取一个ppm文件,然后尝试将其保存,以查找加载和保存模块的准确性。 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.
我使用MATLAB检查了像素值,因此我非常确定我的加载模块运行良好。 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. 我正在链接实际文件( https://github.com/aditisingh/Image_convolution_2D/blob/master/start_1.ppm )和保存的文件( https://github.com/aditisingh/Image_convolution_2D/blob/master/output.ppm ), 这里。 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.