-
Notifications
You must be signed in to change notification settings - Fork 216
/
blend.go
389 lines (326 loc) · 10.2 KB
/
blend.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
/*Package blend provides common image blending modes.*/
package blend
import (
"image"
"math"
"github.com/anthonynsimon/bild/clone"
"github.com/anthonynsimon/bild/fcolor"
"github.com/anthonynsimon/bild/math/f64"
"github.com/anthonynsimon/bild/parallel"
)
// Normal combines the foreground and background images by placing the foreground over the
// background using alpha compositing. The resulting image is then returned.
func Normal(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
return alphaComp(c0, c1)
})
return dst
}
// Add combines the foreground and background images by adding their values and
// returns the resulting image.
func Add(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
r := c0.R + c1.R
g := c0.G + c1.G
b := c0.B + c1.B
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// Multiply combines the foreground and background images by multiplying their
// normalized values and returns the resulting image.
func Multiply(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
r := c0.R * c1.R
g := c0.G * c1.G
b := c0.B * c1.B
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// Overlay combines the foreground and background images by using Multiply when channel values < 0.5
// or using Screen otherwise and returns the resulting image.
func Overlay(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
var r, g, b float64
if c0.R > 0.5 {
r = 1 - (1-2*(c0.R-0.5))*(1-c1.R)
} else {
r = 2 * c0.R * c1.R
}
if c0.G > 0.5 {
g = 1 - (1-2*(c0.G-0.5))*(1-c1.G)
} else {
g = 2 * c0.G * c1.G
}
if c0.B > 0.5 {
b = 1 - (1-2*(c0.B-0.5))*(1-c1.B)
} else {
b = 2 * c0.B * c1.B
}
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// SoftLight combines the foreground and background images by using Pegtop's Soft Light formula and
// returns the resulting image.
func SoftLight(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
r := (1-2*c1.R)*c0.R*c0.R + 2*c0.R*c1.R
g := (1-2*c1.G)*c0.G*c0.G + 2*c0.G*c1.G
b := (1-2*c1.B)*c0.B*c0.B + 2*c0.B*c1.B
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// Screen combines the foreground and background images by inverting, multiplying and inverting the output.
// The result is a brighter image which is then returned.
func Screen(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
r := 1 - (1-c0.R)*(1-c1.R)
g := 1 - (1-c0.G)*(1-c1.G)
b := 1 - (1-c0.B)*(1-c1.B)
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// Difference calculates the absolute difference between the foreground and background images and
// returns the resulting image.
func Difference(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
r := math.Abs(c0.R - c1.R)
g := math.Abs(c0.G - c1.G)
b := math.Abs(c0.B - c1.B)
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// Divide combines the foreground and background images by diving the values from the background
// by the foreground and returns the resulting image.
func Divide(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
var r, g, b float64
if c1.R == 0 {
r = 1
} else {
r = c0.R / c1.R
}
if c1.G == 0 {
g = 1
} else {
g = c0.G / c1.G
}
if c1.B == 0 {
b = 1
} else {
b = c0.B / c1.B
}
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// ColorBurn combines the foreground and background images by dividing the inverted
// background by the foreground image and then inverting the result which is then returned.
func ColorBurn(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
var r, g, b float64
if c1.R == 0 {
r = 0
} else {
r = 1 - (1-c0.R)/c1.R
}
if c1.G == 0 {
g = 0
} else {
g = 1 - (1-c0.G)/c1.G
}
if c1.B == 0 {
b = 0
} else {
b = 1 - (1-c0.B)/c1.B
}
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// Exclusion combines the foreground and background images applying the Exclusion blend mode and
// returns the resulting image.
func Exclusion(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
r := 0.5 - 2*(c0.R-0.5)*(c1.R-0.5)
g := 0.5 - 2*(c0.G-0.5)*(c1.G-0.5)
b := 0.5 - 2*(c0.B-0.5)*(c1.B-0.5)
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// ColorDodge combines the foreground and background images by dividing background by the
// inverted foreground image and returns the result.
func ColorDodge(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
var r, g, b float64
if c1.R == 1 {
r = 1
} else {
r = c0.R / (1 - c1.R)
}
if c1.G == 1 {
g = 1
} else {
g = c0.G / (1 - c1.G)
}
if c1.B == 1 {
b = 1
} else {
b = c0.B / (1 - c1.B)
}
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// LinearBurn combines the foreground and background images by adding them and
// then subtracting 255 (1.0 in normalized scale). The resulting image is then returned.
func LinearBurn(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
r := c0.R + c1.R - 1
g := c0.G + c1.G - 1
b := c0.B + c1.B - 1
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// LinearLight combines the foreground and background images by a mix of a Linear Dodge and
// Linear Burn operation. The resulting image is then returned.
func LinearLight(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
var r, g, b float64
if c1.R > 0.5 {
r = c0.R + 2*c1.R - 0.5
} else {
r = c0.R + 2*c1.R - 1
}
if c1.G > 0.5 {
g = c0.G + 2*c1.G - 0.5
} else {
g = c0.G + 2*c1.G - 1
}
if c1.B > 0.5 {
b = c0.B + 2*c1.B - 0.5
} else {
b = c0.B + 2*c1.B - 1
}
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// Subtract combines the foreground and background images by Subtracting the background from the
// foreground. The result is then returned.
func Subtract(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
r := c1.R - c0.R
g := c1.G - c0.G
b := c1.B - c0.B
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// Opacity returns an image which blends the two input images by the percentage provided.
// Percent must be of range 0 <= percent <= 1.0
func Opacity(bg image.Image, fg image.Image, percent float64) *image.RGBA {
percent = f64.Clamp(percent, 0, 1.0)
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
r := c1.R*percent + (1-percent)*c0.R
g := c1.G*percent + (1-percent)*c0.G
b := c1.B*percent + (1-percent)*c0.B
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// Darken combines the foreground and background images by picking the darkest value per channel
// for each pixel. The result is then returned.
func Darken(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
r := math.Min(c0.R, c1.R)
g := math.Min(c0.G, c1.G)
b := math.Min(c0.B, c1.B)
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// Lighten combines the foreground and background images by picking the brightest value per channel
// for each pixel. The result is then returned.
func Lighten(bg image.Image, fg image.Image) *image.RGBA {
dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
r := math.Max(c0.R, c1.R)
g := math.Max(c0.G, c1.G)
b := math.Max(c0.B, c1.B)
c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
return alphaComp(c0, c2)
})
return dst
}
// Blend two images together by applying the provided function for each pixel.
// If images differ in size, the minimum width and height will be picked from each one
// when creating the resulting image.
func blend(bg image.Image, fg image.Image, fn func(fcolor.RGBAF64, fcolor.RGBAF64) fcolor.RGBAF64) *image.RGBA {
bgBounds := bg.Bounds()
fgBounds := fg.Bounds()
var w, h int
if bgBounds.Dx() < fgBounds.Dx() {
w = bgBounds.Dx()
} else {
w = fgBounds.Dx()
}
if bgBounds.Dy() < fgBounds.Dy() {
h = bgBounds.Dy()
} else {
h = fgBounds.Dy()
}
bgSrc := clone.AsRGBA(bg)
fgSrc := clone.AsRGBA(fg)
dst := image.NewRGBA(image.Rect(0, 0, w, h))
parallel.Line(h, func(start, end int) {
for y := start; y < end; y++ {
for x := 0; x < w; x++ {
bgPos := y*bgSrc.Stride + x*4
fgPos := y*fgSrc.Stride + x*4
result := fn(
fcolor.NewRGBAF64(bgSrc.Pix[bgPos+0], bgSrc.Pix[bgPos+1], bgSrc.Pix[bgPos+2], bgSrc.Pix[bgPos+3]),
fcolor.NewRGBAF64(fgSrc.Pix[fgPos+0], fgSrc.Pix[fgPos+1], fgSrc.Pix[fgPos+2], fgSrc.Pix[fgPos+3]))
result.Clamp()
dstPos := y*dst.Stride + x*4
dst.Pix[dstPos+0] = uint8(result.R * 255)
dst.Pix[dstPos+1] = uint8(result.G * 255)
dst.Pix[dstPos+2] = uint8(result.B * 255)
dst.Pix[dstPos+3] = uint8(result.A * 255)
}
}
})
return dst
}
// alphaComp returns a new color after compositing the two colors
// based on the foreground's alpha channel.
func alphaComp(bg, fg fcolor.RGBAF64) fcolor.RGBAF64 {
fg.Clamp()
fga := fg.A
r := (fg.R * fga / 1) + ((1 - fga) * bg.R / 1)
g := (fg.G * fga / 1) + ((1 - fga) * bg.G / 1)
b := (fg.B * fga / 1) + ((1 - fga) * bg.B / 1)
a := bg.A + fga
return fcolor.RGBAF64{R: r, G: g, B: b, A: a}
}