当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
17 Star 66 Fork 14

wzshiming / pic2ascii
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
resize.go 1.24 KB
一键复制 编辑 原始数据 按行查看 历史
wzshiming 提交于 2018-01-25 13:56 . Remove useless resize
package pic2ascii
import (
"image"
"image/color"
)
type Resize struct {
image.Image
x, y int
dx, dy int
}
// Resize image
func NewResize(img image.Image, x, y int) image.Image {
if x == 0 && y == 0 {
return img
}
rect := img.Bounds()
dx := rect.Dx()
dy := rect.Dy()
if x == dx && y == dy {
return img
}
if x == 0 {
x = y * dx / dy
} else if y == 0 {
y = x * dy / dx
}
return Resize{img, x, y, dx, dy}
}
func (c Resize) Bounds() image.Rectangle {
return image.Rectangle{
image.Point{0, 0},
image.Point{c.x, c.y},
}
}
func (c Resize) At(x, y int) color.Color {
ats := []color.Color{}
j0, l0 := y*c.dy/c.y, (y+1)*c.dy/c.y
i0, k0 := x*c.dx/c.x, (x+1)*c.dx/c.x
for i, k := i0, k0; i != k; i++ {
for j, l := j0, l0; j != l; j++ {
ats = append(ats, c.Image.At(i, j))
}
}
if len(ats) == 0 {
return c.Image.At(i0, j0)
}
return Sum(ats)
}
func Sum(cs []color.Color) color.Color {
switch len(cs) {
case 0:
return color.White
case 1:
return cs[0]
}
var sr, sg, sb, sa uint32 = 0, 0, 0, 0
for _, v := range cs {
r, g, b, a := v.RGBA()
sr += r
sg += g
sb += b
sa += a
}
cl := uint32(len(cs))
sr /= cl
sg /= cl
sb /= cl
sa /= cl
return color.RGBA64{uint16(sr), uint16(sg), uint16(sb), uint16(sa)}
}
Go
1
https://gitee.com/wzshiming/pic2ascii.git
git@gitee.com:wzshiming/pic2ascii.git
wzshiming
pic2ascii
pic2ascii
master

搜索帮助