簡體   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