The PPM Format

[ LiB ]

The PPM Format

You now have a nice color data structure with methods to work with, and it would be wise to test it. The problem is that you haven't written any software that allows you to draw to the video card directly, so you need a simpler way of outputting colored pixels to the monitor. A common way is to write the colored pixels to a file where it can be read in by a program into video memory. This means all you need to do is select a file format that is commonly used.

I've found that the PPM (Portable Pixel Map) format is so simple that I can't pass up the opportunity to use it. It may be old, but it has what you need. Please install the companion program called IrfanView (http://www.irfan-view.com), located in path Software/IrfanView on the CD. This application makes it easy to open *.PPM formatted files. It also lets you convert the PPM files into many other file formats.

Let's look at the PPM file format. A PPM file normally has two parts, the header information and the raw image data. The header information normally consists of at least three parts , each part separated by a carriage return or a linefeed (because the PPM specification requires only white space for character separation). It can also include the hash character (#) for user comments, and it can be placed anywhere in the header. The first line is the PPM identifier (P3 or P6), similar to how the BMP format has a hex code at the beginning. The second line consists of the width and height of the image, or its resolution. The data is stored as ASCII, and it can be any image size you choose. The third line consists of the maximum range in color values you can choose for each pixel. For example, you're using single bytes for each color component, so your range will be from 0-255.

NOTE

NOTE

The PPM file format is a bit old and many people choose not to use it because it has no compression and thus results in large files.However,it is extremely easy to use because the data is packed in a non-optimized layout.

Here's an example file:

  Header Information   P3     # identifier   5 4    # width and height   # Marlon is cool!   255    # range for each color pixel   Raw Data Stream   0 25 0   0 3 0   0 0 0   1 0 0   0 5 0   0 0 128   0  0 0   0 0 6   2 0 0   0 0 0   0 0 0   0 4 0   32 0 0   0 0 0   0 0 0   0 0 0   0 0 6   3 0 255  

Consider the following function, which writes a data buffer to a PPM formatted file:

 bool WritePPM(const char * FileName, color3 *Image, int iWidth, int iHeight) {      // define some locals      FILE * FileHandle;      int i, j;      // return FALSE if you can't get the file handle      if ((FileHandle = fopen(FileName,"w")) == NULL)           return false;      // write the PPM Header      fprintf(FileHandle,"P3\n%d %d\n255\n",iWidth,iHeight);     // scene description     int index;     for (i = 0; i < iHeight; i++)     {           for (j = 0; j < iWidth; j++)           { // computer offset index = iWidth * i + j; // write colored pixel + scale up   fprintf(FileHandle,"%1.0f %1.0f %1.0f\t",Image[index].r * 255.0F ,                             Image[index].g * 255.0F,                             Image[index].b * 255.0F);           }           // end this line           fprintf(FileHandle,"\n");     }     // close file handle     fclose(FileHandle);     // return good :)    return true; } 

[ LiB ]


Focus On Photon Mapping
Focus On Photon Mapping (Premier Press Game Development)
ISBN: 1592000088
EAN: 2147483647
Year: 2005
Pages: 128
Authors: Marlon John

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net