-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpixelate.go
78 lines (67 loc) · 1.82 KB
/
pixelate.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
package texture
import (
"image/color"
)
// Pixelate provides pixelation for a field.
type Pixelate struct {
Name string
Src Field
Resolution float64
}
// NewPixelate creates a new Pixelate with the specified resolution.
func NewPixelate(src Field, resolution float64) *Pixelate {
return &Pixelate{"Pixelate", src, resolution}
}
// Eval2 implements the Field interface.
func (p *Pixelate) Eval2(x, y float64) float64 {
x, y = pixelInd(x, y, p.Resolution)
res := p.Src.Eval2(x, y)
return res
}
// PixelateVF provides pixelation for a vector field.
type PixelateVF struct {
Name string
Src VectorField
Resolution float64
}
// NewPixelateVF creates a new Pixelate with the specified resolution.
func NewPixelateVF(src VectorField, resolution float64) *PixelateVF {
return &PixelateVF{"PixelateVF", src, resolution}
}
// Eval2 implements the Field interface.
func (p *PixelateVF) Eval2(x, y float64) []float64 {
x, y = pixelInd(x, y, p.Resolution)
res := p.Src.Eval2(x, y)
return res
}
// PixelateCF provides pixelation for a color field.
type PixelateCF struct {
Name string
Src ColorField
Resolution float64
}
// NewPixelateCF creates a new Pixelate with the specified resolution.
func NewPixelateCF(src ColorField, resolution float64) *PixelateCF {
return &PixelateCF{"PixelateCF", src, resolution}
}
// Eval2 implements the Field interface.
func (p *PixelateCF) Eval2(x, y float64) color.Color {
x, y = pixelInd(x, y, p.Resolution)
res := p.Src.Eval2(x, y)
return res
}
func pixelInd(x, y, res float64) (float64, float64) {
oneovrres, halfres := 1/res, res/2
x *= oneovrres
if x < 0 {
x -= 1
}
ix := int(x)
y *= oneovrres
if y < 0 {
y -= 1
}
iy := int(y)
// Actual value returned is the value at the pixel's center
return float64(ix)*res + halfres, float64(iy)*res + halfres
}