back to my top page



For those who love doing it yourself.

A sample FORTRAN 77 code (named "pixelfrt.for") will generate PPM or BMP files. One purpose of codes like this is to make frames of movie. Some good softwares such as tmpgenc on MS Windows can make mpeg files from BMP or PPM files. Most of movies and animations listed in my gallery page were made in this way.

Why PPM and BMP ?

Both are in simple bitmap/pixel format and widely used. Popularity of these formats greatly helps us when viewing them and distributing to other people. Simplicity helps and encourages us when getting started to make code, especially with Fortran that is not exactly versatile and flexible. The Fortran code linked above can generate three types of image format; P6 (binary, 24-bit color depth) and P3 (text-based, any color depth) of PPM format and Microsoft BMP format (here fixed to 24-bit depth).
   PPM format is a standard graphic format on Linux / Unix, and lots of image viewers (such as xv) and command-line conversion tools (such as netpbm) support this format. On MS Windows system, many softwares such as IrfanView can display PPM files and convert them to other formats. MS-BMP format is being used widely in the monopolized world. There are some versions of BMP format. This code will generate files in uncompressed 24-bit full color version that is simple and coding-friendly. Note that when the horizontal pixel size is not multiple of 4, this format needs "padding" (filling with meaningless data so that apparent horizontal size will be multiple of 4). This format rule was made for faster loading and displaying by computers in early days. For simplicity, my code does not handle this rule.

What this code will do.

This sample code will generate PPM or BMP files named with three-digit serial number (smpl001.ppm, smpl002.ppm .....), and the first one must be
if you set ichoice = 0 in subroutine mkbitmap, or
(mpeg movie ; 53kB) otherwise.
By setting the integer ipixout in a subroutine pixout, you can choose the format of output image file;
ipixout=1PPM-P6 (binary)
ipixout=2PPM-P3 (plain text; this is most compiler-friendly.)
otherwise BMP (binary; 24-bit color depth, uncompressed)

Notes :

  1. Some mpeg encoder will require that both horizontal and vertical pixel size should be multiple of 16 (or 8) and that file name be given serial numbers. This sample code satisfies these.
  2. When you choose to make BMP file (by setting ipixel = 3 or so), the horizontal pixel number (ihpixf) must be multiple of 4. If not, the generated files will not be fully BMP-compatible.
  3. I hope you will modify subroutine mkbitmap so that you can make your own images and then movies, which must be fun for you even though it will cost you substantial labor and time. Good luck !
  4. This code assumes the color depth of three primary colors (red, green, blue) is 256, or 8-bit, each. That is, color value is integer and ranges from 0 to 255. This matches with PPM-P6 and BMP format, while PPM-P3 does not have any limit on color depth. If you really need color-depth greater than 256, choose PPM-P3.
  5. Some lines of this program use "$" at Input/Output parts. Also some of I/O formats are defined with character variables (fmt=string). These are based on good old DEC Fortran extensions, and thus, may not be standard today. Whether or not these lines can be handled will depend on the operating system and compiler you use. If you have problem when compiling this code, choose PPM-P3 as output format by setting ipixout = 2, with which this code will become, I think, free from such non-standard features.
  6. The executable generated with debug mode will fail in some situations; some of 255 possible characters must have been assigned to mean abnormality such as NaN (not a number) or infinity or undefined number. When pixel data with these special values are referred, naturally in the debug mode, processes may stop leaving lots of kind and least understandable messages.
  7. I have confirmed that this code can be successfully compiled and run with g77 version 3.3.3 on Red Hat (version ?), Intel Fortran version 9.1 on Red Hat (version ?), and with Digital Fortran 5 on MS-DOS prompt of Windows 98SE. No other combinations of compiler and OS have been tested. Also, compiling with aggressive optimization options may fail. So, Good Luck !

Last modified: June, 2008
© K. Hayashi