/
apply.go
45 lines (35 loc) · 829 Bytes
/
apply.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
package adjust
import (
"image"
"image/color"
"github.com/anthonynsimon/bild/clone"
"github.com/anthonynsimon/bild/parallel"
)
// Apply returns a copy of the provided image after applying the provided color function to each pixel.
func Apply(img image.Image, fn func(color.RGBA) color.RGBA) *image.RGBA {
bounds := img.Bounds()
dst := clone.AsRGBA(img)
w, h := bounds.Dx(), bounds.Dy()
parallel.Line(h, func(start, end int) {
for y := start; y < end; y++ {
for x := 0; x < w; x++ {
dstPos := y*dst.Stride + x*4
c := color.RGBA{}
dr := &dst.Pix[dstPos+0]
dg := &dst.Pix[dstPos+1]
db := &dst.Pix[dstPos+2]
da := &dst.Pix[dstPos+3]
c.R = *dr
c.G = *dg
c.B = *db
c.A = *da
c = fn(c)
*dr = c.R
*dg = c.G
*db = c.B
*da = c.A
}
}
})
return dst
}