0

Tried to write a simple PPM image file in C++, here is the code:

#include <iostream>

const int image_height = 256, image_width = 256;

int main()
{
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";

for (int i = image_height - 1; 0 <= i; --i) { 
  for (int j = 0; j < image_width; ++j) { 
     int red = j; int green = i; int blue = 63;
     std::cout << red << ' ' << green << ' ' << blue << '\n';
  }
}

return 0;

}

I compiled it and then redirected the output to a file named "image.ppm". I tried to open it with GIMP but it failed. It says that it is an invalid file. The GIMP that I have is the latest version and I have checked the PPM file format; what I wrote does seem to conform with the general format. So what am I doing wrong?

3
  • Works fine with my GIMP (v 2.10.34). I don't see any problem with it. Commented Aug 6, 2023 at 17:44
  • @TobySpeight I was using the powershell in VsStudio for redirection... maybe that was the problem? Commented Aug 6, 2023 at 17:49
  • Could be. I just used an ordinary POSIX shell. Commented Aug 6, 2023 at 18:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.