Tuesday, February 17, 2009

ppm is a developer's best friend!

OK, that is a crazy overstatement, but it is pretty useful bit to know about.

So what is it? It's a baby-simple image format. Let's say we wanted to write out a 320x240 image. The file would look something like:

P6
320 240
255
{ONE CHARACTER PER R/G/B FOR EACH PIXEL}

So... if the first pixel was black (0x000000), we would write 0, 0, followed by zero. Whereas if it were (0xFFAADD) we'd cleverly write 0xFF, 0xAA, 0xDD.

Ya dig it? Here is a snippet from TextHash2Ppm.hx after I loaded the text file into an array of strings called (creatively enuff) "lines":

out.writeString( 'P6\n' );
out.writeString( lines[ 0 ].length + ' ' + lines.length + '\n' );
out.writeString( '255\n' );

for ( line in lines ) {
for ( i in 0 ... line.length ) {
var char = line.charAt( i ) ;
var value = if ( '#' == char ) 255; else 0;
out.writeByte( value );
out.writeByte( value );
out.writeByte( value );
}
}

Here is a bit I wrote recently to show files like sample_dungeon.txt to
sample_dungeon.ppm which I then converted to

Which I can then use as input to this heightmap viewer even though it looks wonky as heck.

PPM is a handy format for when you need to create images and you don't have or don't want to use an image library to create jpgs, pngs, etc...

No comments:

Post a Comment