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 finally make movies. Some good software such as tmpgenc on MS Windows can make mpeg files from BMP files (and this software well supports other formats like JPEG etc.), and on Linux system ppmtompeg can do so from PPM files. Most of movies and animations listed in my gallery page were made in these ways.

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 toolkit package) support this format. On MS Windows system, many software 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
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)
An mpeg movie (53kB) here was made by following 3 lines (on Linux):
ifort pixelfrt.for ; ./a.out ; ppmtompeg pixelfrt.par,
here, pixelfrt.par is a parameter file that contains minimum information for ppmtompeg to do things. It may not be difficult to edit/modify these files for your own purposes.

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 constraint matches with that of PPM-P6 and BMP format. Because the PPM-P3 format does not have any color-depth limit, you may choose PPM-P3 in case you really need color-depth greater than 256.
  5. This program uses "$" in the I/O format. Also some of I/O formats are restored in character arrays then referred by "fmt=string". These are based on good old DEC Fortran extensions, and thus, may not be standard. Whether or not these lines can be handled will depend upon the 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 free from such non-standard features.
  6. Some system had assigned some of 256 characters to abnormalities such as NaN (not a number) or +/-Inf (infinity), or "undefined". Under some debugging environment, therefore, the process will be terminated when such "abnormal" numbers are given or referred. This is normal; both the sample code above and the debugger do what they are supposed to do.
  7. I have confirmed that this code can be successfully compiled and run with g77 version 3.4.2, gfortran version 4.2.0, and Intel Fortran version 9.1, 10.1 and 11.0 on Linux (Red Hat, version ???), with Digital Fortran 5 on MS-DOS prompt of Windows 98SE, and gfortran version 4.2.0(?) on command prompt of MS Windows Vista (64bit) SP2. No other combinations of compiler and OS have been tested. Also, compiling with aggressive optimization options may fail. So, Good Luck !

modified/edited : August, 2009
© K. Hayashi