forked from anthonynsimon/bild
/
apply.go
40 lines (31 loc) · 824 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
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{}
c.R = dst.Pix[dstPos+0]
c.G = dst.Pix[dstPos+1]
c.B = dst.Pix[dstPos+2]
c.A = dst.Pix[dstPos+3]
c = fn(c)
dst.Pix[dstPos+0] = c.R
dst.Pix[dstPos+1] = c.G
dst.Pix[dstPos+2] = c.B
dst.Pix[dstPos+3] = c.A
}
}
})
return dst
}