-
Notifications
You must be signed in to change notification settings - Fork 84
/
modify.go
479 lines (429 loc) · 12.5 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
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/oakmound/oak/alg"
"github.com/disintegration/gift"
"image"
"image/color"
"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
}
}
// GiftFilter converts any set of gift.Filters into a Modification.
func GiftFilter(fis ...gift.Filter) Modification {
return func(rgba image.Image) *image.RGBA {
filter := gift.New(fis...)
dst := image.NewRGBA(filter.Bounds(rgba.Bounds()))
filter.Draw(dst, rgba)
return dst
}
}
// Brighten brightens an image between -100 and 100. 100 will be solid white,
// -100 will be solid black.
func Brighten(brightenBy float32) Modification {
return GiftFilter(gift.Brightness(brightenBy))
}
// Saturate saturates the input between -100 and 500 percent.
func Saturate(saturateBy float32) Modification {
return GiftFilter(gift.Saturation(saturateBy))
}
// FlipX returns a new rgba which is flipped
// over the horizontal axis.
var FlipX = GiftFilter(gift.FlipHorizontal())
// FlipY returns a new rgba which is flipped
// over the vertical axis.
var FlipY = GiftFilter(gift.FlipVertical())
// ColorBalance takes in 3 numbers between -100 and 500 and applies it to the given image
func ColorBalance(r, g, b float32) Modification {
return GiftFilter(gift.ColorBalance(r, g, b))
}
// Rotate returns a rotated rgba.
func Rotate(degrees int) Modification {
return RotateInterpolated(degrees, gift.CubicInterpolation)
}
// RotateInterpolated acts as Rotate, but accepts an interpolation argument.
// standard rotation does this with Cubic Interpolation.
func RotateInterpolated(degrees int, interpolation gift.Interpolation) Modification {
return GiftFilter(gift.Rotate(float32(degrees), transparent, interpolation))
}
// 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
}
}
// 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}
}
// CutFn can reduce or add blank space to an input image.
// Each input function decides the starting location or offset of a cut,
// depending on input width and height for functions on the appropriate axis.
func CutFn(xMod, yMod, wMod, hMod func(int) int) Modification {
return func(rgba image.Image) *image.RGBA {
bds := rgba.Bounds()
startX := xMod(bds.Max.X)
startY := yMod(bds.Max.Y)
newWidth := wMod(bds.Max.X)
newHeight := hMod(bds.Max.Y)
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+startX, y+startY))
}
}
return newRgba
}
}
// CutFromLeft acts like cut but removes from the left hand side rather than the right
func CutFromLeft(newWidth, newHeight int) Modification {
return CutFn(
func(w int) int { return w - newWidth },
func(h int) int { return h - newHeight },
func(int) int { return newWidth },
func(int) int { return newHeight })
}
// CutRel acts like Cut, but takes in a multiplier on the
// existing dimensions of the image.
func CutRel(relWidth, relHeight float64) Modification {
return CutFn(func(int) int { return 0 },
func(int) int { return 0 },
func(w int) int { return alg.RoundF64(float64(w) * relWidth) },
func(h int) int { return alg.RoundF64(float64(h) * relHeight) })
}
// Cut reduces (or increases, adding black transparent color)
// the dimensions of the input image, setting them to newWidth and
// newHeight.
func Cut(newWidth, newHeight int) Modification {
return CutFn(func(int) int { return 0 },
func(int) int { return 0 },
func(int) int { return newWidth },
func(int) int { return newHeight })
}
// 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 {
r1, g1, b1, a1 := c.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++ {
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
}
}
// 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 {
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 {
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
}
}
// TrimColor will trim inputs so that any rows or columns where each pixel is
// less than or equal to the input color are removed. This will change the dimensions
// of the image.
func TrimColor(trimUnder color.Color) Modification {
r, g, b, a := trimUnder.RGBA()
return func(rgba image.Image) *image.RGBA {
bounds := rgba.Bounds()
w := bounds.Max.X
h := bounds.Max.Y
xOff := 0
yOff := 0
trimouter1:
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
r2, g2, b2, a2 := rgba.At(x, y).RGBA()
if !colorBelow(r, g, b, a, r2, g2, b2, a2) {
break trimouter1
}
}
xOff++
}
trimouter2:
for x := w; x >= 0; x-- {
for y := 0; y < h; y++ {
r2, g2, b2, a2 := rgba.At(x, y).RGBA()
if !colorBelow(r, g, b, a, r2, g2, b2, a2) {
break trimouter2
}
}
w--
}
trimouter3:
for y := h; y >= 0; y-- {
for x := 0; x < w; x++ {
r2, g2, b2, a2 := rgba.At(x, y).RGBA()
if !colorBelow(r, g, b, a, r2, g2, b2, a2) {
break trimouter3
}
}
h--
}
trimouter4:
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
r2, g2, b2, a2 := rgba.At(x, y).RGBA()
if !colorBelow(r, g, b, a, r2, g2, b2, a2) {
break trimouter4
}
}
yOff++
}
out := image.NewRGBA(image.Rect(0, 0, w-xOff+1, h-yOff+1))
for x := xOff; x <= w; x++ {
for y := yOff; y <= h; y++ {
c := rgba.At(x, y)
out.Set(x-xOff, y-yOff, c)
}
}
return out
}
}
func colorBelow(r, g, b, a, r2, g2, b2, a2 uint32) bool {
return r2 <= r && g2 <= g && b2 <= b && a2 <= a
}
// ConformToPallete modifies the input image so that it's colors all fall
// in the input color palette.
func ConformToPallete(p color.Palette) Modification {
return func(rgba image.Image) *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, p.Convert(rgba.At(x, y)))
}
}
return newRgba
}
}
// Zoom zooms into a position on the input image.
// The position is determined by the input percentages, and how far the zoom
// is deep depends on the input zoom level-- 2.0 would quarter the number of
// unique pixels from the input to the output.
func Zoom(xPerc, yPerc, zoom float64) func(rgba image.Image) *image.RGBA {
return func(rgba image.Image) *image.RGBA {
bounds := rgba.Bounds()
w := float64(bounds.Max.X)
h := float64(bounds.Max.Y)
newRgba := image.NewRGBA(image.Rect(0, 0, int(w), int(h)))
newW := w / zoom
newH := h / zoom
minX := (w - newW) * xPerc
minY := (h - newH) * yPerc
for x := 0.0; x < w; x++ {
for y := 0.0; y < h; y++ {
x2 := int(((x * xPerc) / (zoom * xPerc)) + minX)
y2 := int(((y * yPerc) / (zoom * yPerc)) + minY)
newRgba.Set(int(x), int(y), rgba.At(x2, y2))
}
}
return newRgba
}
}