Why don't you create the images in PPM format and use the
netpbm package to convert to other formats (PNG, GIF, JPEG)? Specifically, write your programs to output images in the PPM format, then use the netpbm package to convert the PPM files to JPEG, GIF, and/or PNG formats.
The PPM format is very simple. The file has an ASCII header, followed by uncompressed/raw binary data describing each pixel in the image. See
man ppm for details.
For most PPM images, you'll want to use a header similar to what is produced using command
Code:
printf 'P6\n%d %d\n255\n' width height
This is followed by
height rows of
width pixels each. Each pixel is three bytes long, and uses the RGB color model: first byte is for red, second is for green, and third for blue. The interpretation is the same as HTML/CSS colors, i.e. #FFFFFF (255,255,255) for white, #FF0000 (255,0,0) for red, #00FF00 (0,255,0) for green, #0000FF (0,0,255) for blue, and #000000 (0,0,0) for black. You have 16777216 colors/shades to play with, although many LCD displays will only really display 262144 of them.
In general, PPM format allows either one or two bytes per component, depending on whether the maximum (specified in the header, 255 in my example) is less than 256, or between 256 and 65535 (inclusive). For this kind of use, where the result is soon converted to PNG or GIF or JPEG format anyway, I recommend just sticking with the example above.
If you wish to experiment with High Dynamic Range images, then using two bytes per component (0 to 65535) is useful; but not really otherwise.
I'm doing some research on how to generate better visualizations for atomic simulations (molecular dynamics simulations), and I've used PPM format extensively: it allows you to concentrate on just generating the pixel images, and let specialized tools worry about compression and conversion to other formats.