-
Notifications
You must be signed in to change notification settings - Fork 0
/
grayscale.go
45 lines (36 loc) · 817 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
39
40
41
42
43
44
45
package dither
import (
"image"
"image/color"
)
func threshold(pixel color.Gray, th uint8) color.Gray {
white := color.Gray{Y: 255}
black := color.Gray{Y: 0}
if pixel.Y < th {
return white
} else {
return black
}
}
func Threshold(img *image.Gray, th uint8) *image.Gray {
bounds := img.Bounds()
dx, dy := bounds.Dx(), bounds.Dy()
thresholdImg := image.NewGray(bounds)
for x := 0; x < dx; x++ {
for y := 0; y < dy; y++ {
thresholdImg.Set(x, y, threshold(img.GrayAt(x, y), th))
}
}
return thresholdImg
}
func Grayscale(img image.Image) *image.Gray {
bounds := img.Bounds()
dx, dy := bounds.Dx(), bounds.Dy()
grayscale := image.NewGray(bounds)
for x := bounds.Min.X; x < dx; x++ {
for y := bounds.Min.Y; y < dy; y++ {
grayscale.Set(x, y, img.At(x, y))
}
}
return grayscale
}