Version 14 of PPM

Updated 2010-08-12 00:07:36 by AKgnome

Portable Pix Map, an image format of the PNM family. It is a part of PBM Plus - a large numbers of image converters and manipulators available on the internet.

Every pixel is represented by three bytes for red, green, and blue channel. No compression, no transparency, as far as RS is aware.

Tk images can be read and written in PPM format using photo [as of which version?]. See also PGM.

RS 2006-06-02: Here is a reader that parses a "P3" format PPM string (like read from a file) and creates a photo from it:

 proc ppm-photo ppm {
   regsub -all {#[^\n]*\n} $ppm " " ppm ;# strip out comments
   foreach {type w h max} $ppm break
   foreach {r g b} [lrange $ppm 4 end] {
      set r [expr {int(255.*$r/$max)}]
      set g [expr {int(255.*$g/$max)}]
      set b [expr {int(255.*$b/$max)}]
      lappend row [format #%02X%02X%02X $r $g $b]
      if {[llength $row] == $w} {
         lappend rows $row
         set row {}
      }
   }
   set im [image create photo]
   $im put $rows
   set im
 }

#-- Testing:

 set data {P3
 4 4
 15
 0 0 0 0 0 0 0 0 0 15 0 15
 0 0 0 0 15 7 0 0 0 0 0 0
 0 0 0 0 0 0 0 15 7 0 0 0
 15 0 15 0 0 0 0 0 0 0 0 0
 }
 ppm-photo $data

#-- produces the following image (zoomed by a factor of 9): WikiDbImage ppm.jpg A larger ray-tracing image to test with is at http://www.cpsc.ucalgary.ca/~cherlin/sample.ppm - I was happy to see that IrfanView and my little proc agreed on how to render it :^)

aricb From P3 PPM to photo image in 17 lines! I'm impressed.

<<categories Graphics|Image Processing