pnm

package module
v0.0.0-...-e891a41 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2020 License: BSD-3-Clause Imports: 9 Imported by: 1

README

pnm

This package implements an encoder and decoder for PNM image formats. It can be used with Go's image library. It covers all formats as defined by the 'P1' to 'P6' specifications.

File formats

Each format differs in what colors it is designed to represent:

  • PBM is for bitmaps (black and white, no grays).
  • PGM is for grayscale.
  • PPM is for "pixmaps" which represent full RGB color.

Each file starts with a two-byte magic number (in ASCII) that identifies the type of file it is (PBM, PGM, and PPM) and its encoding (ASCII or binary). The magic number is a capital P followed by a single-digit number.

Magic Number Type Encoding
P1 bitmap ASCII
P2 graymap ASCII
P3 pixmap ASCII
P4 bitmap Binary
P5 graymap Binary
P6 pixmap Binary

The ASCII formats allow for human readability and easy transfer to other platforms (so long as those platforms understand ASCII), while the binary formats are more efficient both in file size and in ease of parsing, due to the absence of whitespace.

In the binary formats, PBM uses 1 bit per pixel, PGM uses 8 bits per pixel, and PPM uses 24 bits per pixel: 8 for red, 8 for green, 8 for blue.

Usage

go get github.com/hexaflex/pnm


import "image"
import _ "github.com/hexaflex/pnm"

...
img, format, err := image.Decode("myfile.pnm)
...

References

License

Unless otherwise stated, all of the work in this project is subject to a 3-clause BSD license. Its contents can be found in the enclosed LICENSE file.

Documentation

Overview

Package pnm implements an encoder and decoder for `PNM` image formats. It can be used with Go's image library. It covers all formats as defined by the 'P1' to 'P6' specifications.

Each format differs in what colors it is designed to represent:

* PBM is for bitmaps (black and white, no grays). * PGM is for grayscale. * PPM is for "pixmaps" which represent full RGB color.

Each file starts with a two-byte magic number (in ASCII) that identifies the type of file it is (PBM, PGM, and PPM) and its encoding (ASCII or binary). The magic number is a capital P followed by a single-digit number.

Magic Number | Type    | Encoding
-------------|---------|-------------
P1           | bitmap  | ASCII
P2           | graymap | ASCII
P3           | pixmap  | ASCII
P4           | bitmap  | Binary
P5           | graymap | Binary
P6           | pixmap  | Binary

The ASCII formats allow for human readability and easy transfer to other platforms (so long as those platforms understand ASCII), while the binary formats are more efficient both in file size and in ease of parsing, due to the absence of whitespace.

In the binary formats, PBM uses 1 bit per pixel, PGM uses 8 bits per pixel, and PPM uses 24 bits per pixel: 8 for red, 8 for green, 8 for blue.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(r io.Reader) (img image.Image, err error)

Decode decodes an image from the given data.

func DecodeConfig

func DecodeConfig(r io.Reader) (cfg image.Config, err error)

DecodeConfig decodes image configuration/metadata.

func Encode

func Encode(w io.Writer, m image.Image, ptype PNMType) (err error)

Encode writes the Image m to w in PPM format. The type of output file is determined by the given PNM type value. Any image can be encoded, but depending on the chosen type, the encoding may be lossy.

Types

type PNMType

type PNMType int
const (
	BitmapAscii PNMType = iota
	BitmapBinary
	GraymapAscii
	GraymapBinary
	PixmapAscii
	PixmapBinary
)

Known PNM file types.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL