struct RGB {
int r, g, b;
};
RGB operator>>(std::ifstream& in, RGB& rgb) {
in >> rgb.r;
in >> rgb.g;
in >> rgb.b;
return rgb;
}
std::ostream& operator<<(std::ostream &out, RGB& rgb) {
out << rgb.r << " ";
out << rgb.g << " ";
out << rgb.b << std::endl;
return out;
}
int main()
{
ifstream myFile;
myFile.open("Image01.ppm", std::ifstream::binary);
//Check for error
if (myFile.fail())
{
cerr << "Error loading file" << endl;
} else {
char magic[5];
int width, heigth;
int pixelValue;
myFile >> magic >> width >> heigth >> pixelValue;
cout << magic << endl << width << endl << heigth << endl << pixelValue << endl;
RGB rgb;
int size = width*heigth;
for (int i = 0;i < size;i++) {
myFile >> rgb;
std::cout << rgb;
}
}
myFile.close();
system("pause");
return 0;
}
大家好,我一直在尝试读取ppm文件图像,但是我遇到了一些问题,当我读到一个txt文件时,我用imageType,width,height和pixelValue创建了一些RGB三元组,一切都还可以,当我实际上尝试读取ppm文件,它读取imageType,width,height和pixelValue但是当我想要它打印像素时它只是打印-858993460永远,当我更改结构,所以rgb值是字符而不是整数,我得到了打印出一些奇怪的符号。那么我们如何用C ++打印和存储二进制数据呢?我在这里做错了什么?