Netpbm (formerly Pbmplus) is an open-source package of graphics programs and a programming library. It is used primarily in Unix, where it is found in all major open-source operating system distributions, but also works on Microsoft Windows, macOS, and other operating systems.
File formats
Several graphics formats are used and defined by the Netpbm project:
portable bitmap format (PBM) portable graymap format (PGM) portable pixmap format (PPM) PBM, PGM and PPM, sometimes collectively referred to as the portable anymap format (PNM) are image file formats designed to be easily exchanged between platforms. The magic number at the beginning of a file determines the type. PNM files use a magic number of an ASCII "P" followed by a number defining the file type. The PBM format was invented by Jef Poskanzer in the 1980s. The format allowed monochrome bitmaps to be transmitted within an email message as plain ASCII text, so that it would survive any changes in text formatting. The first library to handle the PBM format was released in 1988 under the name Pbmplus, containing tools to convert between PBM and other graphics formats. By the end of 1988, Poskanzer had developed the PGM and PPM formats along with their associated tools and added them to Pbmplus. The final release of Pbmplus was December 10, 1991. The Netpbm library was released in 1993 to replace the then-unmaintained Pbmplus. It repackaged the original library and incorporated fixes created by other developers.
Description Each file starts with a two-byte magic number in ASCII that identifies the file type (PBM, PGM, or PPM) and its encoding (ASCII/"plain" or binary/"raw"). The magic number is a capital P followed by a single-digit number.
The Netpbm library supports an additional PAM file format with a magic number of P7. PNM files can be created as both plain text and raw binary. The ASCII ("plain") formats allow for human readability and easy transfer to other platforms, whereas the binary ("raw") formats are easier to parse by programs and more efficient in file size. In the binary formats, PBM uses 1 bit per pixel, PGM uses 8 or 16 bits per pixel, and PPM uses 24 or 48 bits per pixel: 8/16 for red, 8/16 for green, 8/16 for blue. Application support for the 16 bit variants is rare. In either form, the header remains in ASCII format and the arguments are separated by a whitespace. PGM and PPM documentation defines that gray and color values use the BT.709 color space and gamma transfer function. However, depending on the application, the used color space may be sRGB, linear or some other color space. There is no metadata in the file to indicate which color space is being used.
PBM example A simple example of the PBM format is as follows:
P1 # This is an example bitmap of the letter "J" 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 number sign introduces a comment. The next two numbers give the width and the height. The following matrix gives the pixel values. As the PBM format is monochromatic and only supports two shades, namely, black and white, the image is described in only ones and zeros. This is known as a bitmap. The matrix cells do not need to be aligned; the format ignores whitespace and line feeds in the data section. The following displays the same image:
P1 # This is an example bitmap of the letter "J" 6 10 000010000010000010000010000010000010100010011100000000000000
A value of 0 signifies a white pixel, and a 1 signifies a black pixel. This differs from the other formats, where higher values signify brighter pixels. The P4 binary format of the same image represents each pixel with a single bit. The width of the row in pixels is given in the header, packed to the length of 8 pixels or a byte. The first pixel in a row is the most significant bit. The extra bits used to make the length equal to a byte are ignored. The following formula can be used to calculate the number of required bytes ⌈width / 8⌉ * height. Using the example above a ⌈6 / 8⌉ * 10 grid would take 10 instead of 7.5 bytes.
P4 # This is an example binary format of the letter "J" with each byte in decimal notation 6 10 8 8 8 8 8 8 136 112 0 0
PGM example The PGM and PPM formats have an additional parameter for the maximum value (numbers of grey between black and white) after the X and Y dimensions and before the actual pixel data. Black is 0 and max value is white.
P2 # Shows the word "FEEP" 24 7 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0 0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0 0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
PPM example This is an example of a color RGB image stored in PPM format. (Not shown are the newline character(s) at the end of each line.)
P3 # "P3" means this is a RGB color image in ASCII # "3 2" is the width and height of the image in pixels # "255" is the maximum value for each color # This, up through the "255" line below are the header. # Everything after that is the image data: RGB triplets. # In order: red, green, blue, yellow, white, and black. 3 2 255 255 0 0 0 255 0 0 0 255 255 255 0 255 255 255 0 0 0
… excerpt ends here. Continue reading the full article.



