From b149b8e570c32abbebd3c25d0702cd77340f5921 Mon Sep 17 00:00:00 2001 From: mappu Date: Mon, 5 Dec 2016 19:05:59 +1300 Subject: [PATCH] move blending functions to separate file, use package const instead of magic number --- blendcolor.go | 35 +++++++++++++++++++++++++++++++++++ image.go | 51 +++------------------------------------------------ 2 files changed, 38 insertions(+), 48 deletions(-) create mode 100644 blendcolor.go diff --git a/blendcolor.go b/blendcolor.go new file mode 100644 index 0000000..2c597df --- /dev/null +++ b/blendcolor.go @@ -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), + } +} diff --git a/image.go b/image.go index 9e2eb07..a8a5c8b 100644 --- a/image.go +++ b/image.go @@ -2,39 +2,10 @@ package thumbnail import ( "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) { srcW := src.Bounds().Max.X 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, 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*/ + return crush(dest, imagequant.SPEED_FASTEST) }