CrImage::Gray
Inherits CrImage::Image / Reference / Object
Gray is an in-memory 8-bit grayscale image.
Each pixel is stored as a single byte representing luminance (0-255). This format uses 4x less memory than RGBA for grayscale images.
Memory layout: Contiguous byte array with 1 byte per pixel Color model: Grayscale (8-bit)
Example:
img = CrImage::Gray.new(CrImage.rect(0, 0, 640, 480))
img.set_gray(100, 100, CrImage::Color::Gray.new(128))
Constructors
Creates an image from an existing pixel buffer.
Convenience constructor that calculates stride automatically. The buffer is used directly (not copied), so modifications to the image will affect the original buffer.
Parameters:
buffer: Raw pixel data in row-major orderwidth: Image width in pixelsheight: Image height in pixels
Raises: ArgumentError if buffer size doesn't match expected size
Example:
# Create 100x100 RGBA image from raw bytes
pixels = Bytes.new(100 * 100 * 4)
img = CrImage::RGBA.from_buffer(pixels, 100, 100)
Instance methods
Returns true if this reference is the same as other. Invokes same?.
Returns the color at the specified coordinates.
If the coordinates are outside the image bounds, returns a default color (typically transparent black) rather than raising an exception. This is intentional behavior to simplify image processing algorithms that may sample outside bounds.
For explicit bounds checking, use bounds.in?(Point.new(x, y)) before calling.
Parameters:
x: X coordinatey: Y coordinate
Returns: The color at (x, y), or default color if out of bounds
Example:
img = CrImage.rgba(100, 100)
color = img.at(50, 50) # Returns actual color
color = img.at(200, 200) # Returns default color (out of bounds)
# Explicit bounds check:
if img.bounds.in?(CrImage.point(x, y))
color = img.at(x, y) # Guaranteed to be actual pixel
end
Returns the color at the specified coordinates, or nil if out of bounds.
This is an alternative to at that explicitly returns nil for out-of-bounds
coordinates, making bounds checking more explicit in the type system.
Parameters:
x: X coordinatey: Y coordinate
Returns: The color at (x, y), or nil if out of bounds
Example:
img = CrImage.rgba(100, 100)
if color = img.at?(50, 50)
# color is guaranteed to be from the image
end
img.at?(200, 200) # => nil (out of bounds)
bounds returns the domain for which at can return non-zero color.
The bounds do not necessarily contain the point(0,0).
See Object#hash(hasher)
Sets the color at the specified coordinates.
If the coordinates are outside the image bounds, this method does nothing (no-op) rather than raising an exception. This is intentional behavior to simplify drawing operations that may extend beyond image boundaries.
For explicit bounds checking, use bounds.in?(Point.new(x, y)) before calling.
Parameters:
x: X coordinatey: Y coordinatec: Color to set
Example:
img = CrImage.rgba(100, 100)
img.set(50, 50, CrImage::Color::RED) # Sets pixel
img.set(200, 200, CrImage::Color::RED) # No-op (out of bounds)
Swaps the underlying pixel buffer without allocation.
Useful for video/realtime processing where you want to reuse an image object with different frame data.
Parameters:
buffer: New pixel buffer (must match current dimensions)
Raises: ArgumentError if buffer size doesn't match
Example:
img = CrImage::RGBA.from_buffer(frame1, 1920, 1080)
process(img)
img.swap_buffer(frame2) # Zero allocation
process(img)