8 Star 40 Fork 10

Gitee 极速下载 / Caire

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/esimov/caire
克隆/下载
grayscale.go 1.08 KB
一键复制 编辑 原始数据 按行查看 历史
esimov 提交于 2022-10-22 07:53 . feat: code cleanup & documentation #88
package caire
import (
"image"
"image/color"
)
// Grayscale converts the image to grayscale mode.
func (p *Processor) Grayscale(src *image.NRGBA) *image.NRGBA {
dx, dy := src.Bounds().Max.X, src.Bounds().Max.Y
dst := image.NewNRGBA(src.Bounds())
for x := 0; x < dx; x++ {
for y := 0; y < dy; y++ {
r, g, b, _ := src.At(x, y).RGBA()
lum := float32(r)*0.299 + float32(g)*0.587 + float32(b)*0.114
pixel := color.Gray{Y: uint8(lum / 256)}
dst.Set(x, y, pixel)
}
}
return dst
}
// Dither converts an image to black and white image, where the white is fully transparent.
func (p *Processor) Dither(src *image.NRGBA) *image.NRGBA {
var (
bounds = src.Bounds()
dithered = image.NewNRGBA(bounds)
dx = bounds.Dx()
dy = bounds.Dy()
)
for x := 0; x < dx; x++ {
for y := 0; y < dy; y++ {
r, g, b, _ := src.At(x, y).RGBA()
threshold := func() color.Color {
if r > 127 && g > 127 && b > 127 {
return color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
}
return color.NRGBA{A: 0x00}
}
dithered.Set(x, y, threshold())
}
}
return dithered
}
Go
1
https://gitee.com/mirrors/Caire.git
git@gitee.com:mirrors/Caire.git
mirrors
Caire
Caire
master

搜索帮助