class
Eagle::Image
- Eagle::Image
- Reference
- Object
Overview
An RGBA image in CPU memory: 8 bits per channel, rows from top to bottom.
Images are what you load, generate, edit and save. To draw one, upload it with
Texture.new(image). Eagle decodes PNG, QOI and BMP itself, with no system libraries.
img = Image.new(64, 64, Color::TRANSPARENT)
img.fill_rect(8, 8, 48, 48, Color::RED)
img[32, 32] = Color::WHITE
img.blit(Image.circle(16, Color::YELLOW), 24, 24, blend: true)
tex = Texture.new(img)
img.save("out.png") # format follows the extension: .png, .qoi or .bmp
The .checkerboard, .circle and .gradient helpers make placeholder art, so a
prototype can run before any assets exist.
Defined in:
eagle/assets/image.crConstructors
-
.checkerboard(w : Int32, h : Int32, cell : Int32 = 8, a : Color = Color::WHITE, b : Color = Color::GRAY) : Image
A checkerboard of two colors, handy as a placeholder or a debug floor.
-
.circle(diameter : Int32, color : Color = Color::WHITE) : Image
A filled, anti-aliased circle on a transparent background.
-
.decode(data : Bytes, hint : String = "") : Image
Decodes PNG, QOI or BMP bytes.
-
.gradient(w : Int32, h : Int32, from : Color, to : Color, horizontal : Bool = false) : Image
A linear gradient from from to to, top to bottom or left to right.
-
.load(path : String) : Image
Loads an image file through the asset cache.
-
.new(width : Int32, height : Int32, pixels : Bytes)
Wraps existing RGBA bytes.
-
.new(width : Int32, height : Int32, fill : Color = Color::TRANSPARENT)
Creates an image filled with one color, transparent by default.
Instance Method Summary
-
#==(o : Image) : Bool
True when sizes and every pixel match.
-
#[](x : Int, y : Int) : Color
The color of pixel x, y.
-
#[]=(x : Int, y : Int, c : Color)
Sets pixel x, y.
-
#[]?(x : Int, y : Int) : Color | Nil
The color of pixel x, y, or
nilwhen out of bounds. -
#average(x = 0, y = 0, w = @width, h = @height) : Color
The average color over a rectangle, or the whole image.
-
#blit(src : Image, x : Int, y : Int, blend : Bool = false) : self
Copies src onto this image at x, y.
-
#clone : Image
A deep copy.
-
#difference(o : Image) : Float64
Mean absolute difference per channel, from 0 to 255.
-
#fill(c : Color) : self
Fills the whole image with c.
-
#fill_rect(x : Int, y : Int, w : Int, h : Int, c : Color) : self
Fills a rectangle with c, clipped to the image.
-
#flip_vertical! : self
Flips the image upside down in place.
-
#height : Int32
Height in pixels.
-
#in_bounds?(x : Int, y : Int) : Bool
True when x, y is inside the image.
-
#index(x : Int, y : Int) : Int32
Byte offset of pixel x, y in
#pixels. -
#pixels : Bytes
Raw RGBA bytes, row by row from the top.
-
#premultiply! : self
Multiplies color by alpha in place, for premultiplied-alpha blending.
-
#resized(w : Int, h : Int) : Image
A copy scaled to w by h with nearest-neighbor sampling, which keeps pixel art crisp.
-
#save(path : String) : Nil
Writes the image to path, choosing PNG, QOI or BMP from the extension.
-
#size : Vec2
(width, height). -
#sub(x : Int, y : Int, w : Int, h : Int) : Image
A new image holding the rectangle x, y, w, h.
-
#width : Int32
Width in pixels.
Constructor Detail
A checkerboard of two colors, handy as a placeholder or a debug floor.
A filled, anti-aliased circle on a transparent background.
Decodes PNG, QOI or BMP bytes. The format is detected from the data, and hint (a file name) is used only in error messages.
A linear gradient from from to to, top to bottom or left to right.
Wraps existing RGBA bytes. pixels must hold width * height * 4 bytes.
Creates an image filled with one color, transparent by default.
Instance Method Detail
Sets pixel x, y. Raises when out of bounds, so check #in_bounds? for coordinates you didn't compute yourself.
The average color over a rectangle, or the whole image.
Copies src onto this image at x, y. With blend, transparent parts of src let this image show through; without it, pixels are overwritten. Returns self.
Mean absolute difference per channel, from 0 to 255. Use it in tests to compare rendered frames with a tolerance.
Fills a rectangle with c, clipped to the image. Returns self.
A copy scaled to w by h with nearest-neighbor sampling, which keeps pixel art crisp.
Writes the image to path, choosing PNG, QOI or BMP from the extension.