【发布时间】:2017-03-01 06:36:51
【问题描述】:
我正在尝试编写一个程序来读取 ppm 图像,将它们存储为对象,然后再次将它们写出来。理想情况下,我想将像素存储为 int 类型的对象,但我什至只能获得带有字符的相似图像。不幸的是,即使使用 char 对象也会导致图像变灰。我不知道为什么更改存储类型会导致如此大的变化,或者为什么保留形状时图像颜色会丢失。
我试图在这里查看无数其他 ppm 程序问题,但我无法对他们的答案做出正面或反面(或者它们是否相关)。我对这种语言非常陌生,不知道会发生什么。
如果有人能解释我做错了什么,以及我可以用来以 int 格式而不是 char 格式存储数据的策略,我将不胜感激。
下面是我的 ppm 类的文件读取和文件写入代码,我的主函数只是初始化一个 ppm 对象,调用 readfile(),然后调用 writefile()。其中某处无法保存图像。
void PPM::readFile(std::string filename)
{
std::ifstream file;
std::string stuff;
char junk;
file.open(filename, std::ios::binary);
file >> stuff >> width >> height >> maxCol;
file >> std::noskipws >> junk;
int i = 0;
char r, g, b;
std::cout << width*height;
while (i < width*height)
{
file.read(&r, 1);
file.read(&g, 1);
file.read(&b, 1);
red.push_back(b);
grn.push_back(b);
blu.push_back(b);
i++;
std::cout << i << std::endl;
}
}
void PPM::writeFile(std::string filename) const
{
std::ofstream file;
file.open(filename, std::ios::binary);
file << "P6 " << width << " " << height << " " << maxCol << std::endl;
int i = 0;
std::cout << width << " " << height;
while (i < width*height)
{
file.write(&red[i], sizeof(red[i]));
file.write(&grn[i], sizeof(grn[i]));
file.write(&blu[i], sizeof(blu[i]));
std::cout << "iteration " << i << std::endl;
i++;
}
}
再次感谢您提供的任何帮助
【问题讨论】:
-
red、green和blu是什么?