7

Lately I have been doing some numerical method programming in C. For the bug fixing and throubleshooting it's nice to have some visual representation of what is happening. So far I have been outputting areas of array to the standard output, but that doesn't give that much information. I have also been playing around a bit with gnuplot, but I can't get it too save only image, not the coordinate system and all the other stuff.

So I am looking for a tutorial or maybe a library to show me how to save array from c into an image, it would be especially nice to be possible to save to color images. The transformation from numerical value to a color is not a problem, I can calculate that. It would just be nice of someone to point me in the direction of some useful libraries in this field.

best regards

ruslik
  • 14,714
  • 1
  • 39
  • 40
Rok
  • 494
  • 6
  • 15

1 Answers1

10

You could use the .ppm file format... it's so simple that no library is necessary...

FILE *f = fopen("out.ppm", "wb");
fprintf(f, "P6\n%i %i 255\n", width, height);
for (int y=0; y<height; y++) {
    for (int x=0; x<width; x++) {
        fputc(red_value, f);   // 0 .. 255
        fputc(green_value, f); // 0 .. 255
        fputc(blue_value, f);  // 0 .. 255
    }
}
fclose(f);
6502
  • 112,025
  • 15
  • 165
  • 265
  • Or, if you are masochist, try BMP: http://en.wikipedia.org/wiki/BMP_file_format or http://msdn.microsoft.com/en-us/library/ms969901.aspx – ruslik Dec 03 '10 at 15:19
  • Well, actually OP asked for a library, and it should hide all the ugly thing of the binary format. – ruslik Dec 03 '10 at 15:30
  • 2
    Most libraries would require actually more code than shown above just to be used, let alone dealing with the licensing, portability, dependencies and build problems they may introduce. Of course they're going to give you much more (e.g. image compression) but if you just need generating an image from a C program i think that ppm (or pgm for grayscale) is just wonderful. The image format is supported by many programs and viewers or you can just use imagemagick later to convert. – 6502 Dec 03 '10 at 17:44
  • Thanks for this, it's really useful! I'll be using this and later conversion to some other format with imagemagick's 'convert' tool. – Rok Dec 03 '10 at 19:08