Sunday, May 31, 2009

Quick and dirty image creation in Fortran

Well, maybe not dirty image creation. Let's make that "quick and easy" image creation instead.

When you're analyzing tons of data, Gnuplot is an indispensable tool. But sometimes you just want to create an image quickly and easily. Gnuplot is also very fussy about the format of its data for 3D plots and its not always practical to reformat your data to fit Gnuplot's needs. The PPM format is in ASCII, which makes reading and writing it extremely straightforward. An example PPM file might be:

P3
2 3
255
255 0 0
0 0 0
0 255 0
128 128 128
0 0 255
255 255 255


-The first line sets the format of the PPM file to be ASCII.
-The second line sets the image to be 2 columns wide and 3 rows tall.
-The third line sets the largest color to 255.
-Each line after is an RGB triplet defining one pixel.

And it looks something like this:



I've blown this image up by a factor of 100 in each direction.

A quick Fortran program which creates this image is:

        program view

open(unit=8, file="image.ppm")

write(8,100) "P3"
write(8,100) "2 3"
write(8,100) "255"
write(8,101) 255, 0, 0
write(8,101) 0, 0, 0
write(8,101) 0, 255, 0
write(8,101) 128, 128, 128
write(8,101) 0, 0, 255
write(8,101) 255, 255, 255

close(8)

100 FORMAT(a)
101 FORMAT(3(i3,1x))

end program


One other quick note: I used Image Magick's "convert" command to convert from the PPM format to GIFs. To simply convert from PPM to GIF (or JPG, etc), do this:

convert image.ppm image.gif


To hard scale it:

convert -scale 200x300 image.ppm image.gif


And you can also soft scale it:

convert -geometry 200x300 image.ppm image.gif


Which produces a final product that looks like this:

No comments:

Post a Comment