From 0fb6d8a6b2434dc5c78dcf169822e147a727d8d9 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 17 Nov 2022 14:09:31 -0500 Subject: [PATCH] Added an image struct --- image.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 image.go diff --git a/image.go b/image.go new file mode 100644 index 0000000..15a4aa8 --- /dev/null +++ b/image.go @@ -0,0 +1,85 @@ +package stone + +import "image" +import "image/color" + +type ColorImage struct { + x, y int + width, height int + bufferWidth, bufferHeight int + cellWidth, cellHeight int + buffer []color.RGBA + + clean bool +} + +func (im *ColorImage) Model () (model color.Model) { + model = color.RGBAModel + return +} + +func (im *ColorImage) Bounds () (bounds image.Rectangle) { + bounds.Max.X = im.width + bounds.Max.Y = im.height + return +} + +func (im *ColorImage) Size () (width, height int) { + width = im.width + height = im.height + return +} + +func (im *ColorImage) SetSize (width, height int) { + im.width = width + im.height = height + im.bufferWidth = im.cellWidth * im.width + im.bufferHeight = im.cellHeight * im.height + im.buffer = make([]color.RGBA, im.bufferWidth * im.bufferHeight) + im.clean = false +} + +func (im *ColorImage) At (x, y int) (pixel color.Color) { + if im.outOfBounds(x, y) { return } + pixel = im.buffer[x + y * im.width] + return +} + +func (im *ColorImage) AtRGBA (x, y int) (pixel color.RGBA) { + if im.outOfBounds(x, y) { return } + pixel = im.buffer[x + y * im.width] + return +} + +func (im *ColorImage) Set (x, y int, pixel color.Color) { + if im.outOfBounds(x, y) { return } + r, g, b, a := pixel.RGBA() + im.buffer[x + y * im.width] = color.RGBA { + R: uint8(r >> 8), + G: uint8(g >> 8), + B: uint8(b >> 8), + A: uint8(a >> 8), + } + + im.clean = false + return +} + +func (im *ColorImage) SetRGBA (x, y int, pixel color.RGBA) { + if im.outOfBounds(x, y) { return } + im.buffer[x + y * im.width] = pixel + return +} + +func (im *ColorImage) MarkClean () { + im.clean = true +} + +func (im *ColorImage) outOfBounds (x, y int) (outOfBounds bool) { + outOfBounds = + x >= im.width || + y >= im.height || + x < 0 || + y < 0 + return +}