I'm going through the rasterizer tutorial from Scratchapixel, but I encountered a problem with the generation of the .ppm file. Since something was off, I reduced the example to the bare minimum, so I currently simply generate a 512x512 image of a plain color.
#include <fstream>
using Rgb = unsigned char[3];
int main()
{
const uint32_t w = 512;
const uint32_t h = 512;
Rgb *framebuffer = new Rgb[w * h];
for (uint32_t j = 0; j < h; ++j) {
for (uint32_t i = 0; i < w; ++i) {
framebuffer[j * w + i][0] = static_cast<unsigned char>(255);
framebuffer[j * w + i][1] = static_cast<unsigned char>(0);
framebuffer[j * w + i][2] = static_cast<unsigned char>(0);
}
}
std::ofstream ofs;
ofs.open("./example.ppm");
ofs << "P6\n" << w << " " << h << "\n255\n";
ofs.write((char*)framebuffer, w * h * 3);
ofs.close();
delete[] framebuffer;
return 0;
}
The code above should generate a red square image, but if I open the file with Gimp, I get a green image.
Do you have any idea of what's happening?
EDIT: This is the .ppm file with a hex editor: