25 lines
537 B
Go
25 lines
537 B
Go
package filter
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
)
|
|
|
|
func Grayscale(src image.Image) *image.Gray {
|
|
b := src.Bounds()
|
|
out := image.NewGray(b)
|
|
|
|
for y := b.Min.Y; y < b.Max.Y; y++ {
|
|
for x := b.Min.X; x < b.Max.X; x++ {
|
|
r, g, b2, _ := src.At(x, y).RGBA()
|
|
luma := uint8(
|
|
(0.2126*float64(r) +
|
|
0.7152*float64(g) +
|
|
0.0722*float64(b2)) / 256,
|
|
)
|
|
out.SetGray(x, y, color.Gray{Y: luma})
|
|
}
|
|
}
|
|
return out
|
|
}
|