Saving numerical 2D array to image
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 regard开发者_StackOverflows
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);
精彩评论