move blending functions to separate file, use package const instead of magic number

This commit is contained in:
mappu 2016-12-05 19:05:59 +13:00
parent 97f4702af1
commit b149b8e570
2 changed files with 38 additions and 48 deletions

35
blendcolor.go Normal file
View File

@ -0,0 +1,35 @@
package thumbnail
import (
"image/color"
)
func Blend(a, b color.Color) color.Color {
switch a.(type) {
case color.RGBA:
return BlendRGBA(a.(color.RGBA), b.(color.RGBA)) // FIXME there's syntax for this
case color.YCbCr:
return BlendYCbCr(a.(color.YCbCr), b.(color.YCbCr))
default:
return a // ??? unknown color format
}
}
func BlendYCbCr(a, b color.YCbCr) color.YCbCr {
return color.YCbCr{
Y: uint8((int(a.Y) + int(b.Y)) / 2),
Cb: uint8((int(a.Cb) + int(b.Cb)) / 2),
Cr: uint8((int(a.Cr) + int(b.Cr)) / 2),
}
}
func BlendRGBA(a, b color.RGBA) color.RGBA {
return color.RGBA{
R: uint8((int(a.R) + int(b.R)) / 2),
G: uint8((int(a.G) + int(b.G)) / 2),
B: uint8((int(a.B) + int(b.B)) / 2),
A: uint8((int(a.A) + int(b.A)) / 2),
}
}

View File

@ -2,39 +2,10 @@ package thumbnail
import ( import (
"image" "image"
"image/color"
"code.ivysaur.me/imagequant"
) )
func Blend(a, b color.Color) color.Color {
switch a.(type) {
case color.RGBA:
return BlendRGBA(a.(color.RGBA), b.(color.RGBA)) // FIXME there's syntax for this
case color.YCbCr:
return BlendYCbCr(a.(color.YCbCr), b.(color.YCbCr))
default:
return a // ??? unknown color format
}
}
func BlendYCbCr(a, b color.YCbCr) color.YCbCr {
return color.YCbCr{
Y: uint8((int(a.Y) + int(b.Y)) / 2),
Cb: uint8((int(a.Cb) + int(b.Cb)) / 2),
Cr: uint8((int(a.Cr) + int(b.Cr)) / 2),
}
}
func BlendRGBA(a, b color.RGBA) color.RGBA {
return color.RGBA{
R: uint8((int(a.R) + int(b.R)) / 2),
G: uint8((int(a.G) + int(b.G)) / 2),
B: uint8((int(a.B) + int(b.B)) / 2),
A: uint8((int(a.A) + int(b.A)) / 2),
}
}
func (this *Thumbnailer) RenderScaledImage(src image.Image) ([]byte, error) { func (this *Thumbnailer) RenderScaledImage(src image.Image) ([]byte, error) {
srcW := src.Bounds().Max.X srcW := src.Bounds().Max.X
srcH := src.Bounds().Max.Y srcH := src.Bounds().Max.Y
@ -78,21 +49,5 @@ func (this *Thumbnailer) RenderScaledImage(src image.Image) ([]byte, error) {
} }
} }
/*var b bytes.Buffer*/ return crush(dest, imagequant.SPEED_FASTEST)
return crush(dest, 3)
/*
err := png.Encode(&b, dest)
if err != nil {
return nil, err
}
*/
/*
err := jpeg.Encode(&b, dest, nil)
if err != nil {
return nil, err
}
return b.Bytes(), nil*/
} }