【发布时间】:2015-06-14 12:25:30
【问题描述】:
我在 SO 上找到了一些用于读取/写入图像的 C++ 代码。我想改进它,以便我可以旋转等图像。但是,一开始我遇到了一些问题。当我写图像时,我的读取函数似乎只读取了其中的一部分,因为它只写入了原始图像的一部分。请查看我的代码和输入、输出图像。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char **argv)
{
ifstream in;
in.open("OldImage.ppm", std::ios::binary);
ofstream out;
std::string magic_number;
int width, height, maxColVal, i, j;
in >> magic_number;
in >> width >> height >> maxColVal;
in.get();
char **image;
image = new char* [width];
for(i=0; i<width; i++)
{
image[i] = new char [height];
for(j=0; j<height; j++)
{
in >> image[i][j];
}
}
out.open("NewImage.ppm", std::ios::binary);
out << "P3" << "\n"
<< width << " "
<< height << "\n"
<< maxColVal << "\n"
;
for(i=0; i<width; i++)
{
for(j=0; j<height; j++)
{
out << image[i][j];
}
}
in.clear();
in.close();
out.clear();
out.close();
return 0;
}
输入图片: https://www.dropbox.com/s/c0103eyhxzimk0j/OldImage.ppm?dl=0
输出图片: https://www.dropbox.com/s/429i114c05gb8au/NewImage.ppm?dl=0
【问题讨论】: