Thursday, November 17, 2011

Viewing a PPM File

November 15, 2011
Step 1: Intro to PPM File Format

 
What is in a PPM file? 


     PPM file description: http://netpbm.sourceforge.net/doc/ppm.html

   
   
    This 5 x 3 pixel image (drawn in GIMP, using only 255 and 0 for RGB values) gave the following when opened in TextEdit:
   
P6                                                                       // "Magic Number" Image type
# CREATOR: GIMP PNM Filter Version 1.1   // Comment
5 3                                                                    // Image Width and Height
255                                                                     // Max color value
ˇˇQˇQˇQˇ6ˇ6ˇ                                                    // Actual Data?


At first glance, I wanted each character to correspond to a specific color because the pattern almost matched. I looked up the individual ASCII characters and their binary forms to further investigate...


ASCIIDECIMALBINARYCOLOR I WANT IT TO BE
NA?       3200100000 Red??
ˇ      13610001000 Black??
Q       8101010001 Green??
6       6 00000110 Blue??


**HOWEVER**

P 6 means the data is stored in a binary format. Each pixel uses three bytes of data - one byte each for R, G, and B. ( Max color value makes sense : 2^8 = 256 ). 

                    Expected # Bytes = Width x Height x 3 = 15 x 3 = 45 bytes.

If a char is 1 byte, why am I only seeing 12 characters? What happened to the other 33 bytes? Are there characters I cannot see?


PPM code references: http://www.cse.unr.edu/~bebis/CS302/





November 16, 2011


The TextEdit version of my PPM image was NOT what I expected. From my understanding, it should have been something like:

00000000 00000000 00000000 --for black    
11111111 00000000 00000000 --for red          
00000000 11111111 00000000 --for green
00000000 00000000 11111111 --for blue

I coded a tack to read from the file, store it in a char array, and print it to the screen. The data portion was seemingly missing... (Turns out the ascii codes for 0 and 255 are not visible characters.)


Changed the colors from 255 (1111 1111 = (null) ) to 125 ( 0111 1101 = } ) and 0 (0000 0000) to 33 (00100001 = !), I would expect to see :
!!!   }!!   !!!   }!!  !!!
!}!   !!!   !}!   !!!   !{!
!!!   !!}   !!!   !!}   !!!




Instead, I opened the .ppm with TextEdit and got this:
P6
# CREATOR: GIMP PNM Filter Version 1.1
5 3
255
ˇˇˇˇˇˇˇ
Maybe TextEdit isn't the correct way to view the data. How does it open files? Can it open files with binary, and does it display binary as ascii?

For a different viewing method, I consulted J Bowles who showed me the following terminal commands for a more reliable way of viewing:


od -xc ColorTestGrid.ppm | less
od -bc ColorTestGrid.ppm | less 
Detailed description found HERE.


These (as well as emacs) gave:


P6
# CREATOR: GIMP PNM Filter Version 1.1
5 3
255
!!!}!!!!!}!!!!!!}!!!!!}!!!!!}!!!!!!}!!!!!}!!!
 As expected. Viewing mystery solved!


References : http://www.cplusplus.com/doc/tutorial/files/
                    http://en.wikipedia.org/wiki/Netpbm_format
                    http://www.asciitable.com/
                    http://linux.about.com/library/cmd/blcmdl_od.htm
                

No comments:

Post a Comment