forked from oakmound/oak
/
modify.go
381 lines (342 loc) · 9.67 KB
/
modify.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package render
import (
// This file is being slowly converted to use gift over manual math and loops,
// because our math / loops will be more likely to have (and have already had)
// missable bugs.
//"github.com/anthonynsimon/bild/blend"
"github.com/oakmound/oak/alg"
"github.com/disintegration/gift"
"image"
"image/color"
//"image/draw"
"math"
)
var (
transparent = color.RGBA{0, 0, 0, 0}
)
// A Modification takes in an image buffer and returns a new image buffer
type Modification func(image.Image) *image.RGBA
// A Modifiable is a Renderable that has functions to change its
// underlying image.
// This may be replaced with the gift library down the line
type Modifiable interface {
Renderable
GetRGBA() *image.RGBA
Modify(...Modification) Modifiable
Copy() Modifiable
}
// And chains together multiple Modifications into a single Modification
func And(ms ...Modification) Modification {
return func(rgba image.Image) *image.RGBA {
rgba2 := ms[0](rgba)
for i := 1; i < len(ms); i++ {
rgba2 = ms[i](rgba2)
}
return rgba2
}
}
// Brighten brightens an image
func Brighten(brightenBy float32) Modification {
return func(rgba image.Image) *image.RGBA {
filter := gift.New(
gift.Brightness(brightenBy))
dst := image.NewRGBA(filter.Bounds(rgba.Bounds()))
filter.Draw(dst, rgba)
return dst
}
}
// FlipX returns a new rgba which is flipped
// over the horizontal axis.
func FlipX(rgba image.Image) *image.RGBA {
filter := gift.New(
gift.FlipHorizontal())
dst := image.NewRGBA(filter.Bounds(rgba.Bounds()))
filter.Draw(dst, rgba)
return dst
}
// FlipY returns a new rgba which is flipped
// over the vertical axis.
func FlipY(rgba *image.RGBA) *image.RGBA {
bounds := rgba.Bounds()
w := bounds.Max.X
h := bounds.Max.Y
newRgba := image.NewRGBA(image.Rect(0, 0, w, h))
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
newRgba.Set(x, y, rgba.At(x, h-y))
}
}
return newRgba
}
// todo: this should not be in this package
type point struct {
X, Y float64
}
// CutRound rounds the edges of the Modifiable with Bezier curves.
// Todo: A nice bezier curve toolset would be nice
func CutRound(xOff, yOff float64) Modification {
return func(rgba image.Image) *image.RGBA {
bds := rgba.Bounds()
newRgba := image.NewRGBA(bds)
// start off as a copy
for x := bds.Min.X; x < bds.Max.X; x++ {
for y := bds.Min.Y; y < bds.Max.Y; y++ {
newRgba.Set(x, y, rgba.At(x, y))
}
}
// For each corner, define directions
corners := [][4]int{
// X, Y, xDir, yDir
{bds.Min.X, bds.Min.Y, 1, 1},
{bds.Min.X, bds.Max.Y, 1, -1},
{bds.Max.X, bds.Max.Y, -1, -1},
{bds.Max.X, bds.Min.Y, -1, 1},
}
for _, c := range corners {
// 3 point Bezier curve
x1 := float64(c[0])
y1 := float64(c[1])
x2 := x1 + (float64(bds.Max.X*c[2]) * xOff)
y2 := y1 + (float64(bds.Max.Y*c[3]) * yOff)
p1 := point{x2, y1}
p2 := point{x1, y1}
p3 := point{x1, y2}
// Progressing along the curve, whenever a new y value is
// intersected at a pixel delete all values
// from the image above(or below, for negative c[3])
// that pixel
// todo: non-arbitrary progress increment
for progress := 0.0; progress < 1.0; progress += 0.01 {
p4 := pointBetween(p1, p2, progress)
p5 := pointBetween(p2, p3, progress)
curveAt := pointBetween(p4, p5, progress)
// Could only redo this loop at new y values to save time,
// but because this is currently just a pre-processing modification
// it should be okay
x := alg.RoundF64(curveAt.X)
for y := alg.RoundF64(curveAt.Y); y <= bds.Max.Y && y >= bds.Min.Y; y -= c[3] {
newRgba.Set(x, y, color.RGBA{0, 0, 0, 0})
}
}
}
return newRgba
}
}
// todo: this should not be in this package
func pointBetween(p1, p2 point, f float64) point {
return point{p1.X*(1-f) + p2.X*f, p1.Y*(1-f) + p2.Y*f}
}
// CutRel acts like Cut, but takes in a multiplier on the
// existing dimensions of the image.
func CutRel(relWidth, relHeight float64) Modification {
return func(rgba image.Image) *image.RGBA {
bds := rgba.Bounds()
newWidth := alg.RoundF64(float64(bds.Max.X) * relWidth)
newHeight := alg.RoundF64(float64(bds.Max.Y) * relHeight)
newRgba := image.NewRGBA(image.Rect(0, 0, newWidth, newHeight))
for x := 0; x < newWidth; x++ {
for y := 0; y < newHeight; y++ {
newRgba.Set(x, y, rgba.At(x, y))
}
}
return newRgba
}
}
// Cut reduces (or increases, adding nothing)
// the dimensions of the input image, setting them to newWidth and
// newHeight. (Consider: use generic int modifiers here so we
// don't need CutRel and Cut? i.e a function header like
// Cut(wMod, hMod func(int) int)? )
func Cut(newWidth, newHeight int) Modification {
return func(rgba image.Image) *image.RGBA {
newRgba := image.NewRGBA(image.Rect(0, 0, newWidth, newHeight))
for x := 0; x < newWidth; x++ {
for y := 0; y < newHeight; y++ {
newRgba.Set(x, y, rgba.At(x, y))
}
}
return newRgba
}
}
// Fade reduces the alpha of an image
func Fade(alpha int) Modification {
return func(rgba image.Image) *image.RGBA {
bounds := rgba.Bounds()
a2 := uint32(alpha * 257)
w := bounds.Max.X
h := bounds.Max.Y
newRgba := image.NewRGBA(image.Rect(0, 0, w, h))
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
r, g, b, a := rgba.At(x, y).RGBA()
var a3 uint16
if a2 > a {
a3 = 0
} else {
a3 = uint16(a - a2)
}
tmp := color.NRGBA64{
uint16(r),
uint16(g),
uint16(b),
a3}
newRgba.Set(x, y, tmp)
}
}
return newRgba
}
}
// ApplyColor mixes a color into the rgba values of an image
// and returns that new rgba.
func ApplyColor(c color.Color) Modification {
return func(rgba image.Image) *image.RGBA {
// u := image.NewUniform(c)
// bounds := rgba.Bounds()
// img := image.NewRGBA(bounds)
// draw.Draw(img, bounds, u, bounds.Min, draw.Src)
// return blend.Normal(rgba, img)
r1, g1, b1, a1 := c.RGBA()
// filter := gift.New(
// gift.ColorBalance(float32(r1*(255/100)), float32(g1*(255/100)), float32(b1*(255/100))))
// dst := image.NewRGBA(filter.Bounds(rgba.Bounds()))
// filter.Draw(dst, rgba)
// return dst
bounds := rgba.Bounds()
w := bounds.Max.X
h := bounds.Max.Y
newRgba := image.NewRGBA(image.Rect(0, 0, w, h))
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
r2, g2, b2, a2 := rgba.At(x, y).RGBA()
a3 := a1 + a2
if a2 == 0 {
newRgba.Set(x, y, color.RGBA{0, 0, 0, 0})
continue
}
tmp := color.RGBA64{
uint16(((a1 * r1) + (a2 * r2)) / a3),
uint16(((a1 * g1) + (a2 * g2)) / a3),
uint16(((a1 * b1) + (a2 * b2)) / a3),
uint16(a2)}
newRgba.Set(x, y, tmp)
}
}
return newRgba
}
}
// ColorBalance takes in 3 numbers between -100 and 500 and applies it to the given image
func ColorBalance(r, g, b float32) Modification {
return func(rgba image.Image) *image.RGBA {
filter := gift.New(gift.ColorBalance(r, g, b))
dst := image.NewRGBA(filter.Bounds(rgba.Bounds()))
filter.Draw(dst, rgba)
return dst
}
}
// FillMask replaces alpha 0 pixels in an RGBA with corresponding
// pixels in a second RGBA.
func FillMask(img image.RGBA) Modification {
return func(rgba image.Image) *image.RGBA {
// Instead of static color it just two buffers melding
bounds := rgba.Bounds()
w := bounds.Max.X
h := bounds.Max.Y
newRgba := image.NewRGBA(image.Rect(0, 0, w, h))
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
newRgba.Set(x, y, rgba.At(x, y))
}
}
bounds = img.Bounds()
w = bounds.Max.X
h = bounds.Max.Y
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
r1, g1, b1, a1 := rgba.At(x, y).RGBA()
r2, g2, b2, a2 := img.At(x, y).RGBA()
var tmp color.RGBA64
if a1 == 0 {
tmp = color.RGBA64{
uint16(r2),
uint16(g2),
uint16(b2),
uint16(a2),
}
} else {
tmp = color.RGBA64{
uint16(r1),
uint16(g1),
uint16(b1),
uint16(a1),
}
}
newRgba.Set(x, y, tmp)
}
}
return newRgba
}
}
// ApplyMask mixes the rgba values of two images, according to
// their alpha levels, and returns that as a new rgba.
func ApplyMask(img image.RGBA) Modification {
return func(rgba image.Image) *image.RGBA {
// Instead of static color it just two buffers melding
bounds := rgba.Bounds()
w := bounds.Max.X
h := bounds.Max.Y
newRgba := image.NewRGBA(image.Rect(0, 0, w, h))
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
newRgba.Set(x, y, rgba.At(x, y))
}
}
bounds = img.Bounds()
w = bounds.Max.X
h = bounds.Max.Y
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
r1, g1, b1, a1 := img.At(x, y).RGBA()
r2, g2, b2, a2 := rgba.At(x, y).RGBA()
var tmp color.RGBA64
a3 := a1 + a2
if a3 == 0 {
tmp = color.RGBA64{
0, 0, 0, 0,
}
newRgba.Set(x, y, tmp)
continue
}
tmp = color.RGBA64{
uint16(((a1 * r1) + (a2 * r2)) / a3),
uint16(((a1 * g1) + (a2 * g2)) / a3),
uint16(((a1 * b1) + (a2 * b2)) / a3),
uint16(math.Max(float64(a1), float64(a2)))}
newRgba.Set(x, y, tmp)
}
}
return newRgba
}
}
// Rotate returns a rotated rgba.
func Rotate(degrees int) Modification {
return func(rgba image.Image) *image.RGBA {
filter := gift.New(
gift.Rotate(float32(degrees), transparent, gift.CubicInterpolation))
dst := image.NewRGBA(filter.Bounds(rgba.Bounds()))
filter.Draw(dst, rgba)
return dst
}
}
// Scale returns a scaled rgba.
func Scale(xRatio, yRatio float64) Modification {
return func(rgba image.Image) *image.RGBA {
bounds := rgba.Bounds()
w := int(math.Floor(float64(bounds.Max.X) * xRatio))
h := int(math.Floor(float64(bounds.Max.Y) * yRatio))
filter := gift.New(
gift.Resize(w, h, gift.CubicResampling))
dst := image.NewRGBA(filter.Bounds(rgba.Bounds()))
filter.Draw(dst, rgba)
return dst
}
}