tins2020/cmd/imadapt/gray.go

32 lines
646 B
Go
Raw Normal View History

2020-05-10 07:11:08 +00:00
package main
import (
"image"
"image/color"
"opslag.de/schobers/tins2020/img"
2020-05-10 07:11:08 +00:00
)
func convertToGray(path string) error {
src, err := img.DecodeImage(path)
2020-05-10 07:11:08 +00:00
if err != nil {
return err
}
bounds := src.Bounds()
dst := image.NewRGBA(bounds)
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
c := src.At(x, y)
rgba := color.NRGBAModel.Convert(c).(color.NRGBA)
if rgba.A > 0 {
gray := color.GrayModel.Convert(c).(color.Gray)
rgba.R, rgba.G, rgba.B = gray.Y, gray.Y, gray.Y
dst.Set(x, y, rgba)
} else {
dst.Set(x, y, c)
}
}
}
return img.EncodePNG(path, dst)
2020-05-10 07:11:08 +00:00
}