PPM数据格式
举例:
image.ppm
第一部分:头部分
P3代表使用pixmap以及ASCII格式
第一个256代表宽度,第二个256代表高度
255代表最大像素值是255
第二部分:图像数据部分
0 0 0
1 0 0
2 0 0
分表表示RGB三个通道
实践
#include <iostream>
int main()
{
int image_width = 256;
int image_height = 256;
//头部分
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
//图像数据部分
for(int j = 0; j < image_height; ++j) {
for(int i = 0; i < image_width; ++i) {
auto r = double(i) / (image_width - 1);
auto g = double(j) / (image_height - 1);
auto b = 0;
int ir = static_cast<int>(255.999 * r);
int ig = static_cast<int>(255.999 * g);
int ib = static_cast<int>(255.999 * b);
std::cout << ir << ' ' << ig << ' ' << ib << "\n";
}
}
}
编译
g++ example.cpp -o example
生成图片
./example > image.ppm