Difference between revisions of "Portable pixmap (file format)"
(→File format description) |
|||
(One intermediate revision by the same user not shown) | |||
Line 31: | Line 31: | ||
The most basic (monochrome) '''PBM''' format represents it as follows: | The most basic (monochrome) '''PBM''' format represents it as follows: | ||
+ | ===P1=== | ||
P1 | P1 | ||
# This is an example bit map file j.pbm | # This is an example bit map file j.pbm | ||
Line 48: | Line 49: | ||
The PGM and PPM formats (both ASCII and binary versions) have an additional parameter for the maximum value in a line between the X and Y dimensions and the actual pixel data. | The PGM and PPM formats (both ASCII and binary versions) have an additional parameter for the maximum value in a line between the X and Y dimensions and the actual pixel data. | ||
+ | |||
+ | ===P3=== | ||
+ | P3 | ||
+ | #the P3 means colors are in ascii, then 3 columns and 2 rows, then 255 for max color, then RGB triplets | ||
+ | 3 2 | ||
+ | 255 | ||
+ | 255 0 0 | ||
+ | 0 255 0 | ||
+ | 0 0 255 | ||
+ | 255 255 0 | ||
+ | 255 255 255 | ||
+ | 0 0 0 | ||
+ | |||
+ | You can convert the above PPM (foo.ppm) to a PNG image (expanded ''without interpolation'') using the [[ImageMagick]] command | ||
+ | convert -sample %6400 foo.ppm foo.png | ||
+ | |||
+ | ===P6=== | ||
+ | The P6 format of the same image will store a colour with one byte. The file will be smaller but the colour information will not be readable by humans: | ||
+ | P6 | ||
+ | #any comment string | ||
+ | 3 2 | ||
+ | 255 | ||
+ | !@#$%^&*()_+|{}:"< | ||
+ | |||
+ | The above 192x128 PNG image has a file size of 552 bytes. When converted to a 192x128 PPM image, the file size is 73848 bytes. The PPM format can be converted to PNG without loss of information. | ||
+ | |||
+ | ===Python code=== | ||
+ | The following [[Python]] code makes the above example image. It can be adapted to making useful images by reading or constructing an array of numerical data, and programming a conversion of the data to color triplets. | ||
+ | |||
+ | #!/usr/bin/python | ||
+ | triplets=[ | ||
+ | [255, 0, 0], | ||
+ | [0, 255, 0], | ||
+ | [0, 0, 255], | ||
+ | [255, 255, 0], | ||
+ | [255, 255, 255], | ||
+ | [0, 0, 0] | ||
+ | ] | ||
+ | |||
+ | width=3 | ||
+ | height=2 | ||
+ | |||
+ | comment='any comment string' | ||
+ | |||
+ | ftype='P6' #use 'P3' for ascii, 'P6' for binary | ||
+ | |||
+ | ppmfile=open('testimage.ppm','wb') | ||
+ | ppmfile.write("%s\n" % (ftype)) | ||
+ | ppmfile.write("#%s\n" % comment ) | ||
+ | ppmfile.write("%d %d\n" % (width, height)) | ||
+ | ppmfile.write("255\n") | ||
+ | |||
+ | if ftype=='P3': | ||
+ | for red,green,blue in triplets: | ||
+ | ppmfile.write("%d %d %d\n" % (red,green,blue)) | ||
+ | elif ftype=='P6': #print 1 byte per color | ||
+ | for red,green,blue in triplets: | ||
+ | ppmfile.write("%c%c%c" % (red,green,blue)) | ||
+ | |||
+ | ppmfile.close() | ||
==16-bit extensions== | ==16-bit extensions== | ||
Line 61: | Line 122: | ||
**http://netpbm.sourceforge.net/doc/pam.html | **http://netpbm.sourceforge.net/doc/pam.html | ||
− | [[Category: | + | [[Category:Graphics software]] |
Latest revision as of 04:02, 14 April 2008
The portable pixmap file format (PPM), the portable graymap file format (PGM) and the portable bitmap file format (PBM) specify rules for exchanging graphics files. They provide very basic functionality and serve as a least-common-denominator for converting pixmap, graymap, or bitmap files between different platforms. Several applications refer to them collectively as the PNM format (portable anymap).
Contents
Netpbm usage
The Netpbm package can, for example, use two successive conversion programs to turn this code into a bmp file:
pgmtoppm "#FFFFFF" j.pbm > j.ppm ppmtobmp j.ppm > j.bmp
Depending on the identification of the file format, portable pixmap systems can distinguish three similar file formats, each with two versions:
- pbm - portable bitmap file format (P1/P4) - 1 bit per pixel
- pgm - portable graymap file format (P2/P5) - 8 bits per pixel
- ppm - portable pixmap file format (P3/P6) - 24 bits per pixel, 8 for red, 8 for green, 8 for blue
In each case, the lower-numbered version (P1, P2 or P3) refers to a human-readable, ASCII-based format similar to the one in the example above; and the higher-numbered version (P4, P5 or P6) refers to a binary format, not human-readable but more efficient at saving some space in the file, as well as easier to parse due to the lack of whitespace.
If you search for finding a way to convert PDF files to a raster format, xPDf provides such a conversion with a free tool called pdf2ppm. Using this tool you can export your pdf files into images in ppm format.
File format description
Take the example of the letter "J" from the bitmap article:
....X. ....X. ....X. ....X. ....X. ....X. X...X. .XXX.. ...... ......
The most basic (monochrome) PBM format represents it as follows:
P1
P1 # This is an example bit map file j.pbm 6 10 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
The string P1 identifies the file format. The hash sign introduces a comment. The next two numbers give the width and the height. Then follows the matrix with the pixel values (in the monochrome case here, only zeros and ones).
The PGM and PPM formats (both ASCII and binary versions) have an additional parameter for the maximum value in a line between the X and Y dimensions and the actual pixel data.
P3
P3 #the P3 means colors are in ascii, then 3 columns and 2 rows, then 255 for max color, then RGB triplets 3 2 255 255 0 0 0 255 0 0 0 255 255 255 0 255 255 255 0 0 0
You can convert the above PPM (foo.ppm) to a PNG image (expanded without interpolation) using the ImageMagick command
convert -sample %6400 foo.ppm foo.png
P6
The P6 format of the same image will store a colour with one byte. The file will be smaller but the colour information will not be readable by humans:
P6 #any comment string 3 2 255 !@#$%^&*()_+|{}:"<
The above 192x128 PNG image has a file size of 552 bytes. When converted to a 192x128 PPM image, the file size is 73848 bytes. The PPM format can be converted to PNG without loss of information.
Python code
The following Python code makes the above example image. It can be adapted to making useful images by reading or constructing an array of numerical data, and programming a conversion of the data to color triplets.
#!/usr/bin/python triplets=[ [255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0], [255, 255, 255], [0, 0, 0] ] width=3 height=2 comment='any comment string' ftype='P6' #use 'P3' for ascii, 'P6' for binary ppmfile=open('testimage.ppm','wb') ppmfile.write("%s\n" % (ftype)) ppmfile.write("#%s\n" % comment ) ppmfile.write("%d %d\n" % (width, height)) ppmfile.write("255\n") if ftype=='P3': for red,green,blue in triplets: ppmfile.write("%d %d %d\n" % (red,green,blue)) elif ftype=='P6': #print 1 byte per color for red,green,blue in triplets: ppmfile.write("%c%c%c" % (red,green,blue)) ppmfile.close()
16-bit extensions
The original definition of the PGM and the PPM binary formats (the P5 and P6 formats) did not support bit-depths greater than 8 bits. One can of course use the ASCII format, but this format both slows down reading and makes the files much larger. Accordingly, many programmers have attempted to extend the format to support higher bit-depths. Using higher bit-depths encounters the problem of having to decide on the endianess of the file. Unfortunately it appears that the various implementations could not agree on which byte order to use! (Netpbm, the de facto standard implementation of the PNM formats, uses big-endian.)
External links
- Format details for the various pnm formats: