I am working in C on a physics experiment, Young's interference experiment and i made a program who prints to file
a huge bunch of pixels :
for (i=0; i < width*width; i++)
{
fwrite(hue(raster_matrix[i]), 1, 3, file);
}
Where hue
, when given a value [0..255], gives back a char *
with 3 bytes, R,G,B.
I would like to put a minimal header in my image file in order to make this raw file a valid image file.
More concise : Switching from :
offset
0000 : height * width : data } my data, 24bit RGB pixels
to
offset
0000 : dword : magic \
: /* ?? */ \
0012 : dword : height } Header <--> common image file
0016 : dword : width /
: /* ?? */ /
0040 : height * width : data } my data, 24bit RGB pixels
Thank you.
TARGA (file name extension
.tga
) may be the simplest widely supported binary image file format if you don't use compression and don't use any of its extensions. It's even simpler than Windows.bmp
files and is supported by ImageMagick and many paint programs. It has been my go-to format when I just need to output some pixels from a throwaway program.Here's a minimal C program to generate an image to standard output:
You probably want to use the PPM format which is what you're looking for: a minimal header followed by raw RGB.
The recently created farbfeld format is quite minimal, though there is not much software supporting it (at least so far).
Here's a minimal example that writes your image file with a minimal PPM header:
I wrote the header code based on the specs at http://netpbm.sourceforge.net/doc/ppm.html.
I hacked in some setup code so I could incorporate the
for
loop given in the question. :)