-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrayscale.go
38 lines (31 loc) · 905 Bytes
/
grayscale.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
package image
import (
"sync"
)
// Grayscale turns the images to grayscale.
func (img *Image) Grayscale(algorithm int) *Image {
var wg sync.WaitGroup
for rowIndex := 0; rowIndex < img.Height; rowIndex++ {
wg.Add(1)
go (func(rowIndex int, img *Image) {
for colIndex := 0; colIndex < img.Width; colIndex++ {
pixel := img.Pixels[rowIndex][colIndex]
var gray int
if algorithm == GrayscaleLuma {
gray = int(float32(pixel.R)*0.2126 + float32(pixel.G)*0.7152 + float32(pixel.B)*0.0722)
} else if algorithm == GrayscaleDesaturation {
gray = int((max(pixel.R, pixel.G, pixel.B) + min(pixel.R, pixel.G, pixel.B)) / 2)
} else {
gray = int((pixel.R + pixel.G + pixel.B) / 3)
}
pixel.Set("R", gray)
pixel.Set("G", gray)
pixel.Set("B", gray)
img.Pixels[rowIndex][colIndex] = pixel
}
wg.Done()
})(rowIndex, img)
}
wg.Wait()
return img
}