thumbnail/image.go

54 lines
1.3 KiB
Go
Raw Normal View History

2016-10-13 09:25:11 +00:00
package thumbnail
import (
"image"
"code.ivysaur.me/imagequant"
)
2016-10-13 09:25:11 +00:00
func (this *Thumbnailer) RenderScaledImage(src image.Image) ([]byte, error) {
srcW := src.Bounds().Max.X
srcH := src.Bounds().Max.Y
destW := 0
destH := 0
if srcW > srcH {
destW = this.Width
destH = this.Height * srcH / srcW
} else {
destW = this.Width * srcW / srcH
destH = this.Height
}
offsetX := (this.Width - destW) / 2
offsetY := (this.Height - destH) / 2
scaleW := float64(srcW) / float64(destW)
scaleH := float64(srcH) / float64(destH)
dest := image.NewRGBA(image.Rectangle{Max: image.Point{X: this.Width, Y: this.Height}})
for y := 0; y < destH; y += 1 {
for x := 0; x < destW; x += 1 {
/*
// NN
mapx := int(float64(x) * scaleW)
mapy := int(float64(y) * scaleH)
dest.Set(x+offsetX, y+offsetY, src.At(mapx, mapy))
*/
// Bilinear
c00 := src.At(int(float64(x)*scaleW), int(float64(y)*scaleH))
c01 := src.At(int((float64(x)+0.5)*scaleW), int(float64(y)*scaleH))
c10 := src.At(int(float64(x)*scaleW), int((float64(y)+0.5)*scaleH))
c11 := src.At(int((float64(x)+0.5)*scaleW), int((float64(y)+0.5)*scaleH))
cBlend := Blend(Blend(c00, c01), Blend(c10, c11))
dest.Set(x+offsetX, y+offsetY, cBlend)
}
}
return crush(dest, imagequant.SPEED_FASTEST)
2016-10-13 09:25:11 +00:00
}