module

Image::PixelBuffer(T)

PixelBuffer represents a finite rectangular grid of pixels.

This is the core interface that all image types implement. It provides a uniform way to access and modify pixels regardless of the underlying storage mechanism.

Type parameter T specifies the pixel type (Color::RGBA8, Color::Gray8, etc.)

Key concepts:

  • Bounds define the valid coordinate range (may not start at 0,0)
  • Coordinates outside bounds return default values (no exceptions)
  • The pixel at (x,y) represents the grid square from (x,y) to (x+1,y+1)

Implementations:

  • Slice-based: RGBA8, Gray8 (most common - pixels stored in memory)
  • Procedural: Uniform (computed on-the-fly, no storage)
  • Custom: Libraries can implement their own

Example iteration:

b = img.bounds
b.min.y.upto(b.max.y - 1) do |y|
  b.min.x.upto(b.max.x - 1) do |x|
    pixel = img[x, y]
    # process pixel
  end
end

Instance methods

[](x : Int32, y : Int32) : T

Alias for at(x, y)

Source
[](x_range : Range(Int32 | Nil, Int32 | Nil), y_range : Range(Int32 | Nil, Int32 | Nil))

Returns a sub-image view defined by x and y ranges.

Example:

sub = img[10..20, 5...15]
Source
[]=(x : Int32, y : Int32, pixel : T) : Nil

Alias for set(x, y, pixel)

Source
at(x : Int32, y : Int32) : T

Returns the pixel at (x, y), or default value if out of bounds

Source
bounds

Returns the rectangular bounds where pixels are defined

Source
each_pixel

Yields each pixel and its coordinates to the block

Source
fill(color : T) : Nil

Fills the entire buffer (or view) with a solid color

Source
height

Height of the buffer

Source
map!

Modifies pixels in-place by yielding current pixel and setting the result

Source
opaque?

Reports whether the image is fully opaque (no transparency).

Iterates over all pixels and checks if they are fully opaque. Short-circuits on the first non-opaque pixel.

Source
set(x : Int32, y : Int32, pixel : T) : Nil

Sets the pixel at (x, y), no-op if out of bounds

Source
stride

Stride (bytes per row) of the buffer

Source
sub_image(rect : Rectangle)

Returns a sub-image view shared with this buffer

Source
unsafe_at(x : Int32, y : Int32) : T

Returns the pixel at (x, y) without bounds checking. undefined behavior if out of bounds.

Source
width

Width of the buffer

Source