Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a0a4bac5e2 | |||
| dd0655ea3c | |||
| 41720caef5 | |||
| cc921ddb29 | |||
| c8b022f5f1 | |||
| 1003203bb1 | |||
| 5ca69d90e6 | |||
| 85b71a91a3 | |||
| 70a8567646 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
TEST.png
|
||||
39
examples/glaggle/main.go
Normal file
39
examples/glaggle/main.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "image"
|
||||
import "image/png"
|
||||
import "git.tebibyte.media/tomo/ggfx"
|
||||
|
||||
func main () {
|
||||
img := image.NewRGBA(image.Rect(0, 0, 300, 300))
|
||||
canvas := ggfx.Image[uint8] {
|
||||
Pix: img.Pix,
|
||||
Stride: img.Stride,
|
||||
Rect: img.Rect,
|
||||
Width: 4,
|
||||
}
|
||||
black := []uint8 { 0, 0, 0, 255 }
|
||||
yellow := []uint8 { 255, 255, 0, 255 }
|
||||
midpoint := image.Pt(canvas.Rect.Dx() / 2, canvas.Rect.Dy() / 2)
|
||||
|
||||
face := ggfx.Circle(32, 100, midpoint)
|
||||
canvas.FillPolygon(yellow, face...)
|
||||
canvas.StrokePolygon(black, 2, face...)
|
||||
|
||||
eye := ggfx.Circle(12, 16, midpoint.Add(image.Pt(-40, -30)))
|
||||
canvas.FillPolygon(black, eye...)
|
||||
eye = ggfx.Circle(12, 16, midpoint.Add(image.Pt(40, -30)))
|
||||
canvas.FillPolygon(black, eye...)
|
||||
|
||||
mouth := ggfx.Circle(24, 70, midpoint)
|
||||
canvas.PolyLine(black, 2, mouth[1:12]...)
|
||||
|
||||
file, err := os.Create("TEST.png")
|
||||
if err != nil { panic(err.Error()) }
|
||||
defer file.Close()
|
||||
err = png.Encode(file, img)
|
||||
if err != nil { panic(err.Error()) }
|
||||
|
||||
println("Open TEST.png to see the result.")
|
||||
}
|
||||
40
examples/icon/main.go
Normal file
40
examples/icon/main.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "image"
|
||||
import "image/png"
|
||||
import "git.tebibyte.media/tomo/ggfx"
|
||||
|
||||
func main () {
|
||||
img := image.NewRGBA(image.Rect(0, 0, 256, 256))
|
||||
canvas := ggfx.Image[uint8] {
|
||||
Pix: img.Pix,
|
||||
Stride: img.Stride,
|
||||
Rect: img.Rect,
|
||||
Width: 4,
|
||||
}
|
||||
black := []uint8 { 0, 0, 0, 255 }
|
||||
orange := []uint8 { 255, 48, 0, 255 }
|
||||
midpoint := image.Pt(canvas.Rect.Dx() / 2, canvas.Rect.Dy() / 2)
|
||||
|
||||
canvas.FillRectangle(orange, canvas.Rect)
|
||||
for x := 1; x <= 6; x ++ {
|
||||
point := image.Pt(x, 7 - x)
|
||||
canvas.FillRectangle(black, image.Rectangle {
|
||||
Min: point.Mul(32),
|
||||
Max: point.Add(image.Pt(1, 1)).Mul(32),
|
||||
})
|
||||
}
|
||||
|
||||
ring := ggfx.Circle(32, 88, midpoint)
|
||||
canvas.StrokePolygon(orange, 32, ring...)
|
||||
canvas.StrokePolygon(black, 16, ring...)
|
||||
|
||||
file, err := os.Create("TEST.png")
|
||||
if err != nil { panic(err.Error()) }
|
||||
defer file.Close()
|
||||
err = png.Encode(file, img)
|
||||
if err != nil { panic(err.Error()) }
|
||||
|
||||
println("Open TEST.png to see the result.")
|
||||
}
|
||||
12
ggfx.go
12
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) {
|
||||
|
||||
5
line.go
5
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,
|
||||
|
||||
11
plot.go
11
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
|
||||
}}
|
||||
}
|
||||
|
||||
51
polygon.go
51
polygon.go
@@ -5,7 +5,7 @@ import "image"
|
||||
|
||||
func (this Image[T]) FillPolygon (fill []T, points ...image.Point) {
|
||||
this.assertWidth(fill)
|
||||
if len(points) < 2 { return }
|
||||
if len(points) < 3 { return }
|
||||
|
||||
// figure out the bounds of the polygon so we don't test empty space
|
||||
var area image.Rectangle
|
||||
@@ -14,10 +14,11 @@ func (this Image[T]) FillPolygon (fill []T, points ...image.Point) {
|
||||
for _, point := range points[1:] {
|
||||
if point.X < area.Min.X { area.Min.X = point.X }
|
||||
if point.Y < area.Min.Y { area.Min.Y = point.Y }
|
||||
if point.X < area.Max.X { area.Max.X = point.X }
|
||||
if point.Y < area.Max.Y { area.Max.Y = point.Y }
|
||||
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:
|
||||
// https://www.alienryderflex.com/polygon_fill/
|
||||
@@ -28,30 +29,35 @@ func (this Image[T]) FillPolygon (fill []T, points ...image.Point) {
|
||||
boundaryCount := 0
|
||||
prevPoint := points[len(points) - 1]
|
||||
for _, point := range points {
|
||||
fy := float64(y)
|
||||
fPointX := float64(point.X)
|
||||
fPointY := float64(point.Y)
|
||||
fPrevX := float64(prevPoint.X)
|
||||
fPrevY := float64(prevPoint.Y)
|
||||
addboundary :=
|
||||
point.Y < y && prevPoint.Y >= y ||
|
||||
prevPoint.Y < y && point.Y >= y
|
||||
(fPointY < fy && fPrevY >= fy) ||
|
||||
(fPrevY < fy && fPointY >= fy)
|
||||
if addboundary {
|
||||
boundaries[boundaryCount] =
|
||||
point.X +
|
||||
(y - point.Y) /
|
||||
(prevPoint.Y - point.Y) *
|
||||
(prevPoint.X - point.X)
|
||||
boundaries[boundaryCount] = int (
|
||||
fPointX +
|
||||
(fy - fPointY) /
|
||||
(fPrevY - fPointY) *
|
||||
(fPrevX - fPointX))
|
||||
boundaryCount ++
|
||||
}
|
||||
prevPoint = point
|
||||
}
|
||||
|
||||
// sort boundary list
|
||||
boundaries = boundaries[:boundaryCount]
|
||||
sort.Ints(boundaries)
|
||||
cutBoundaries := boundaries[:boundaryCount]
|
||||
sort.Ints(cutBoundaries)
|
||||
|
||||
// fill pixels between boundary pairs
|
||||
min := area.Min.X
|
||||
max := area.Max.X
|
||||
for index := 0; index < len(boundaries); index ++ {
|
||||
left := boundaries[index]
|
||||
right := boundaries[index + 1]
|
||||
for index := 0; index < len(cutBoundaries); index += 2 {
|
||||
left := cutBoundaries[index]
|
||||
right := cutBoundaries[index + 1]
|
||||
|
||||
// stop if we have exited the polygon
|
||||
if left >= max { break }
|
||||
@@ -64,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
|
||||
}
|
||||
}
|
||||
@@ -82,3 +86,12 @@ func (this Image[T]) StrokePolygon (stroke []T, weight int, points ...image.Poin
|
||||
prevPoint = point
|
||||
}
|
||||
}
|
||||
|
||||
func (this Image[T]) PolyLine (stroke []T, weight int, points ...image.Point) {
|
||||
if len(points) < 2 { return }
|
||||
prevPoint := points[0]
|
||||
for _, point := range points[1:] {
|
||||
this.Line(stroke, weight, prevPoint, point)
|
||||
prevPoint = point
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}}}
|
||||
}
|
||||
|
||||
16
shapes.go
Normal file
16
shapes.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package ggfx
|
||||
|
||||
import "math"
|
||||
import "image"
|
||||
|
||||
func Circle (resolution int, radius float64, center image.Point) []image.Point {
|
||||
points := make([]image.Point, resolution)
|
||||
for index := range points {
|
||||
angle := float64(index) * ((2 * math.Pi) / float64(len(points)))
|
||||
point := image.Pt (
|
||||
int(math.Cos(angle) * radius),
|
||||
int(math.Sin(angle) * radius))
|
||||
points[index] = point.Add(center)
|
||||
}
|
||||
return points
|
||||
}
|
||||
Reference in New Issue
Block a user