diff --git a/examples/glaggle/main.go b/examples/glaggle/main.go index 7818372..a9ffe26 100644 --- a/examples/glaggle/main.go +++ b/examples/glaggle/main.go @@ -10,12 +10,12 @@ func main () { canvas := ggfx.Image[uint8] { Pix: img.Pix, Stride: img.Stride, - Bounds: img.Rect, + Rect: img.Rect, Width: 4, } black := []uint8 { 0, 0, 0, 255 } yellow := []uint8 { 255, 255, 0, 255 } - midpoint := image.Pt(canvas.Bounds.Dx() / 2, canvas.Bounds.Dy() / 2) + midpoint := image.Pt(canvas.Rect.Dx() / 2, canvas.Rect.Dy() / 2) face := ggfx.Circle(32, 100, midpoint) canvas.FillPolygon(yellow, face...) diff --git a/examples/icon/main.go b/examples/icon/main.go index 69df3bd..8713f45 100644 --- a/examples/icon/main.go +++ b/examples/icon/main.go @@ -10,14 +10,14 @@ func main () { canvas := ggfx.Image[uint8] { Pix: img.Pix, Stride: img.Stride, - Bounds: img.Rect, + Rect: img.Rect, Width: 4, } black := []uint8 { 0, 0, 0, 255 } orange := []uint8 { 255, 48, 0, 255 } - midpoint := image.Pt(canvas.Bounds.Dx() / 2, canvas.Bounds.Dy() / 2) + midpoint := image.Pt(canvas.Rect.Dx() / 2, canvas.Rect.Dy() / 2) - canvas.FillRectangle(orange, canvas.Bounds) + canvas.FillRectangle(orange, canvas.Rect) for x := 1; x <= 6; x ++ { point := image.Pt(x, 7 - x) canvas.FillRectangle(black, image.Rectangle { diff --git a/ggfx.go b/ggfx.go index deab74b..80d7f02 100644 --- a/ggfx.go +++ b/ggfx.go @@ -13,10 +13,18 @@ type Image[T any] struct { // Width specifies how many slice elements make up a single pixel. Width int - // Bounds specifies a rectangle that all drawing operations will be + // Rect specifies a rectangle that all drawing operations will be // clipped to. It must not reside outside of Data. It is measured in // pixels and not slice elements. - Bounds image.Rectangle + Rect image.Rectangle +} + +func (this Image[T]) Bounds () image.Rectangle { + return this.Rect +} + +func (this Image[T]) PixOffset (x, y int) int { + return (y - this.Rect.Min.Y) * this.Stride + (x - this.Rect.Min.X) * this.Width } func (this Image[T]) assertWidth (pixel []T) { diff --git a/line.go b/line.go index 38592cb..c25e754 100644 --- a/line.go +++ b/line.go @@ -25,12 +25,9 @@ func (this Image[T]) lineInternal ( ) { context := linePlottingContext[T] { plottingContext: plottingContext[T] { - data: this.Pix, - stride: this.Stride, + Image: this, color: stroke, weight: weight, - bounds: this.Bounds, - width: this.Width, offset: offset, }, min: min, diff --git a/plot.go b/plot.go index 8e8d27f..b62188f 100644 --- a/plot.go +++ b/plot.go @@ -3,12 +3,9 @@ package ggfx import "image" type plottingContext[T any] struct { - data []T - stride int + Image[T] color T weight int - bounds image.Rectangle - width int offset int } @@ -16,14 +13,14 @@ func (context plottingContext[T]) square (center image.Point) (square image.Rect return image.Rect(0, 0, context.weight, context.weight). Sub(image.Pt(context.weight / 2, context.weight / 2)). Add(center). - Intersect(context.bounds) + Intersect(context.Rect) } func (context plottingContext[T]) plot (center image.Point) { square := context.square(center) for y := square.Min.Y; y < square.Max.Y; y ++ { for x := square.Min.X; x < square.Max.X; x ++ { - index := y * context.stride + x * context.width + context.offset - context.data[index] = context.color + index := context.PixOffset(x, y) + context.offset + context.Pix[index] = context.color }} } diff --git a/polygon.go b/polygon.go index 56e901c..e5196e7 100644 --- a/polygon.go +++ b/polygon.go @@ -17,7 +17,7 @@ func (this Image[T]) FillPolygon (fill []T, points ...image.Point) { if point.X > area.Max.X { area.Max.X = point.X } if point.Y > area.Max.Y { area.Max.Y = point.Y } } - area = this.Bounds.Intersect(area) + area = this.Rect.Intersect(area) if area.Empty() { return } // this algorithm is adapted from: @@ -70,9 +70,7 @@ func (this Image[T]) FillPolygon (fill []T, points ...image.Point) { // fill for each pixel part for offset, part := range fill { for x := left; x < right; x ++ { - index := - y * this.Stride + x * - this.Width + offset + index := this.PixOffset(x, y) + offset this.Pix[index] = part } } diff --git a/rectangle.go b/rectangle.go index 437cd25..5ebbed7 100644 --- a/rectangle.go +++ b/rectangle.go @@ -4,14 +4,14 @@ import "image" func (this Image[T]) FillRectangle (fill []T, rectangle image.Rectangle) { this.assertWidth(fill) - rectangle = this.Bounds.Intersect(rectangle.Canon()) + rectangle = this.Rect.Intersect(rectangle.Canon()) if rectangle.Empty () { return } var pos image.Point for pos.Y = rectangle.Min.Y; pos.Y < rectangle.Max.Y; pos.Y ++ { for offset, part := range fill { for pos.X = rectangle.Min.X; pos.X < rectangle.Max.X; pos.X ++ { - index := pos.Y * this.Stride + pos.X * this.Width + offset + index := this.PixOffset(pos.X, pos.Y) + offset this.Pix[index] = part }}} }