liveBook · Manning

Pixel

As explained in Learn Haskell by Example

A pixel is an individual picture element whose value represents visual information at a position in a digital image.

A pixel is one stored image value at a position. In portable anymap (PNM) images, width and height establish the pixel grid; the format determines whether each position holds a bitmap value, a grayscale value, or red, green, and blue channel values. section 12.1.1

Table 12.1 Overview of the magic numbers for the PNM formats
Format
Magic numbers
Extension
Image data (for one pixel)
ASCII
Binary
Portable bitmap format (PBM)
P1
P4
.pbm
0 (white) or 1 (black)
Portable graymap format (PGM)
P2
P5
.pgm
From 0 to maximum value (grayscale), where the maximum value is given in the header
Portable pixmap format (PPM)
P3
P6
.ppm
From 0 to maximum value for each RGB channel, where the maximum value is given in the header

Compare the value stored for one pixel in PBM, PGM, and PPM files, including their ASCII and binary magic numbers. section 12.1.1

PBM stores one value per pixel: 0 for white or 1 for black. PGM also stores one value per pixel, from 0 to the maximum declared in the header, producing grayscale. PPM stores three values per pixel—one for each RGB channel—using that same declared maximum. section 12.1.1

A 2×2 bitmap

The ASCII PBM data `1 0 / 0 1` follows a header declaring a 2-by-2 image. Reading the values in grid order gives two black pixels on one diagonal and two white pixels on the other. section 12.1.1

An image representation keeps its width, height, and pixel collection together. A vector is useful when filters need to read individual pixels repeatedly: it supports constant-time indexing, unlike a list's poorer access behavior. Build data as a list when convenient, then convert it to a vector for compact, faster reads. section 13.1.3

Pixel types can be handled at several levels:

  • Raw parsed pixels use either one `Word16` value or three `Word16` RGB values. section 12.2.2
  • Validated images distinguish 8-bit and 16-bit grayscale and RGB pixel vectors through their constructors. section 13.1.3
  • Generic conversion maps supported pixel types to normalized RGB triples with channels from 0 to 1, then converts results back. section 13.3

Normalized RGB values make a single image algorithm usable across grayscale and color pixels. Grayscale pixels become equal RGB channels; converting back averages the channels. Values are scaled between integral 8- or 16-bit ranges and `Double`, with clamping before conversion back. section 13.3

To export an image, generate a JuicyPixels image by supplying a function that returns the pixel at each `(x, y)` coordinate. For row-major vector storage, the corresponding index is `y * width + x`; selecting the matching grayscale or RGB constructor preserves the pixel type before writing PNG. section 13.3.2

How Learn Haskell by Example covers it

How deep it goes
Explains per-format values, typed storage, normalized conversion, and coordinate indexing without detailing image-filter implementations.
Examples
Uses a 2×2 PBM pattern, a 10×14 PGM grid, typed pixel vectors, and coordinate-based PNG generation.
What you can do after
Build it from scratch by parsing pixel values, storing them in typed vectors, converting representations, and exporting PNG images.
How it connects
Connects pixels to PNM, RGB, grayscale, Vector, type classes, GADTs, and PNG.

Also explained in

Compare how 10 books explain pixel →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.