forked from felixpalmer/go_images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.go
181 lines (158 loc) · 3.66 KB
/
canvas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"image"
"image/color"
"image/draw"
"log"
"math"
"os"
)
type Canvas struct {
image.RGBA
}
func NewCanvas(r image.Rectangle) *Canvas {
canvas := new(Canvas)
canvas.RGBA = *image.NewRGBA(r)
return canvas
}
func (c Canvas) Clone() *Canvas {
clone := NewCanvas(c.Bounds())
copy(clone.Pix, c.Pix)
return clone
}
func CanvasFromFile(filename string) *Canvas {
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
m, _, err := image.Decode(file)
if err != nil {
log.Fatal(err)
}
canvas := NewCanvas(m.Bounds())
draw.Draw(canvas, m.Bounds(), m, image.ZP, draw.Src)
return canvas
}
func (c Canvas) DrawGradient() {
size := c.Bounds().Size()
for x := 0; x < size.X; x++ {
for y := 0; y < size.Y; y++ {
color := color.RGBA{
uint8(255 * x / size.X),
uint8(255 * y / size.Y),
55,
255}
c.Set(x, y, color)
}
}
}
func (c Canvas) DrawLine(color color.RGBA, from Vector, to Vector) {
delta := to.Sub(from)
length := delta.Length()
x_step, y_step := delta.X/length, delta.Y/length
limit := int(length + 0.5)
for i := 0; i < limit; i++ {
x := from.X + float64(i)*x_step
y := from.Y + float64(i)*y_step
c.Set(int(x), int(y), color)
}
}
func (c Canvas) DrawCircle(color color.RGBA, at Vector, radius int) {
for x := -radius; x <= radius; x++ {
for y := -radius; y <= radius; y++ {
if x*x+y*y <= radius*radius {
c.Set(int(at.X)+x, int(at.Y)+y, color)
}
}
}
}
func (c Canvas) DrawRect(color color.RGBA, min Vector, max Vector) {
for x := int(min.X); x <= int(max.X); x++ {
for y := int(min.Y); y <= int(max.Y); y++ {
c.Set(x, y, color)
}
}
}
func (c Canvas) DrawSpiral(color color.RGBA, from Vector) {
dir := Vector{0, 2}
last := from
for i := 0; i < 10000; i++ {
next := last.Add(dir)
c.DrawLine(color, last, next)
dir.Rotate(0.03)
dir.Scale(0.999)
last = next
}
}
func (c Canvas) Blur(radius int, weight WeightFunction) {
clone := c.Clone()
size := c.Bounds().Size()
for x := 0; x < size.X; x++ {
for y := 0; y < size.Y; y++ {
color := c.BlurPixel(x, y, radius, weight)
clone.Set(x, y, color)
}
}
copy(c.Pix, clone.Pix)
}
func (c Canvas) BlurPixel(x int, y int, radius int, weight WeightFunction) color.Color {
weightSum := float64(0)
size := c.Bounds().Size()
outR, outG, outB := float64(0), float64(0), float64(0)
for i := x - radius; i < x+radius+1; i++ {
if i < 0 || i > size.X {
continue
}
for j := y - radius; j < y+radius+1; j++ {
if j < 0 || j > size.Y {
continue
}
weight := weight.Weight(i-x, j-y)
r, g, b, _ := c.At(i, j).RGBA()
outR += float64(r) * weight
outG += float64(g) * weight
outB += float64(b) * weight
weightSum += weight
}
}
// Need to divide by 0xFF as the RGBA() function returns color values as uint32
// and we need uint8
return color.RGBA{
uint8(outR / (weightSum * 0xFF)),
uint8(outG / (weightSum * 0xFF)),
uint8(outB / (weightSum * 0xFF)),
255}
}
// Blur weighting functions
type WeightFunction interface {
Weight(x int, y int) float64
}
type WeightFunctionBox struct{}
func (w WeightFunctionBox) Weight(x int, y int) float64 { return 1.0 }
type WeightFunctionDist struct{}
func (w WeightFunctionDist) Weight(x int, y int) float64 {
d := math.Hypot(float64(x), float64(y))
return 1 / (1 + d)
}
type WeightFunctionMotion struct {
}
func (w WeightFunctionMotion) Weight(x int, y int) float64 {
if y != 0 {
return 0
}
if x < 0 {
return 0
}
return 0.3 + 0.7/math.Sqrt(1+float64(x))
}
type WeightFunctionDouble struct {
split int
}
func (w WeightFunctionDouble) Weight(x int, y int) float64 {
if y == 0 && (x == w.split || x == -w.split) {
return 1.0
} else {
return 0
}
}