PBM, PGM, PNM, and PPM File Format Summary

 

PBM, PGM, PNM, and PPM File Format Summary

Also Known As: Portable Bitmap Utilities, pbmplus

 


 

TypeBitmap
ColorsUp to 24-bit
CompressionNone
Maximum Image SizeNA
Multiple Images Per FileNA
Numerical FormatNA
OriginatorJef Poskanzer
PlatformUNIX, Intel-based PCs
Supporting Applicationspbmplus, others
See AlsoMost of the formats in this book

Usage
File format conversion through an intermediary least-common-denominator format.

Comments
PBM, PGM, PNM, and PPM are intermediate formats used in the conversion of many little known formats via pbmplus, the Portable Bitmap Utilities. These formats are mainly available under UNIX and on Intel-based PCs.

Vendor specifications are available for this format.

Code fragments are available for this format.

Sample images are available for this format.

 


The Portable Bitmap Utilities (PBM) is a collection of programs organized, maintained, and primarily written by Jef Poskanzer. Although owned and copyrighted by Mr. Poskanzer, they are freely available in both source and executable form on the Internet and on many BBS systems. The "bitmap" in PBM is used in the older sense to refer to monochrome images. There are actually three other sets of programs encompassed by the PBM utilities. These are the Portable Greymap Utilities (PGM), the Portable Pixmap Utilities (PPM), and the Portable Anymap Utilities (PNM). PBM programs manipulate monochrome bitmaps, and PGM and PPM programs manipulate gray-scale bitmaps and color bitmaps, respectively. PNM programs operate on all of the bitmaps produced by the other programs. There is no file format associated with PNM itself. Most people call the overall set of programs PBM and the newer version pbmplus, however, and we'll follow this convention.

Contents:
File Organization
File Details
For Further Information

Associated with pbmplus are three least-common-denominator intermediate formats. When converting a graphics file from one format to another, we speak of the source file (in the current format) and the destination file (in the desired new format). pbmplus works by taking a source file and converting it into one of the intermediate formats. That intermediate format file is then converted into the destination format.

To see how this works, here are the steps necessary to convert a Microsoft Windows Bitmap (BMP) format file named testfile.bmp to a GIF format file. These are 256-color files, so we use the PPM utilities bmptoppm and ppmtogif:

bmptoppm testfile.bmpThis produces testfile.ppm
ppmtogif testfile.ppmThis produces testfile.gif

 

The latest version of pbmplus is available on the CD-ROM that accompanies this book.

File Organization

The PBM, PGM, and PPM formats are each designed to be as simple as possible. Each starts out with a header, and the bitmap data follows immediately after. The header is always written in ASCII, and data items are separated by white space (blanks, tabs, carriage returns, or linefeeds). The data portion of each file type can be written in either ASCII or binary form.

File Details

There are two versions of each of the the PBM, PGM, and PPM headers. Although all the headers are in ASCII format, one is used for the ASCII version of the format, and the other is used for the binary version.

PBM Header

A PBM header consists of the following entries, each separated by white space:

MagicValueLiterally P1 for ASCII version, P4 for binary
ImageWidthWidth of image in pixels (ASCII decimal value)
ImageHeightHeight of image in pixels (ASCII decimal value)

 

PGM Header

A PGM header consists of the following entries, each separated by white space:

MagicValueLiterally P2 for ASCII version, P5 for binary
ImageWidthWidth of image in pixels (ASCII decimal value)
ImageHeightHeight of image in pixels (ASCII decimal value)
MaxGreyMaximum gray value (ASCII decimal value)

 

PPM Header

A PPM header consists of the following entries, each separated by white space:

MagicValueLiterally P3 for ASCII version, P6 for binary
ImageWidthWidth of image in pixels (ASCII decimal value)
ImageHeightHeight of image in pixels (ASCII decimal value)
MaxGreyMaximum color value (ASCII decimal value)

 

Image Data

After the header is a series of lines describing widthxheight pixels. For PPM, each pixel contains three ASCII decimal values between 0 and the specified maximum value, starting at the top-left corner of the pixmap, proceeding in normal English reading order. The three values for each pixel represent red, green, and blue, respectively; a value of 0 means that color is turned off, and the maximum value means that color is "maxxed out."

For PBM and PGM, there is only one ASCII decimal value per pixel. For PBM, the maximum value is implicitly 1.

Here is an example of a small pixmap in this format:

P3
# feep.ppm
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

You can include comments in the PBM file. Characters from a # character to the next end-of-line are ignored. There is a suggested maximum of 70 characters per line, but this is not an actual restriction.

Mr. Poskanzer cautions that programs that read this format should be as lenient as possible, accepting anything that looks remotely like a pixmap.

RAWBITS Variant

There is also a variant on the format, available by setting the RAWBITS option at compile time. This variant differs from the traditional format in the following ways:

 

  • The "magic numbers" are as follows:

    Format

    Normal

    RAWBITS Variant

    PBM

    P1

    P4

    PGM

    P2

    P5

    PPM

    P3

    P6

     

     

  • The pixel values are stored as plain bytes, instead of ASCII decimal:

    PBMRAWBITS is eight pixels per byte
    PGMRAWBITS is one pixel per byte
    PPMRAWBITS is three bytes per pixel

     

     

  • White space is not allowed in the pixel area, and only a single character of white space (typically a newline) is allowed after the MaxGrey value.

     

  • The files are smaller and many times faster to read and write.

     

  • Bit order within the byte is most significant bit (MSB) first.

     

 

Note that this raw format can only be used for maximum values less than or equal to 255. If you use the PPM library and try to write a file with a larger maximum value, it automatically uses the slower, but more general, plain format.

For Further Information

For further information about the PBM, PGM, PNM, and PPM utilities, see the documentation on the CD-ROM. See also the code and documentation for the pbmplus utilities, also included on the CD-ROM.

For more information about PBM, PGM, PNM, and PPM, you can contact:

Jef Poskanzer
Email: jef@well.sf.ca.us

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值