From ea2ed0e25b3e7b7d35325dd1c0b250ac13d50810 Mon Sep 17 00:00:00 2001 From: mappu Date: Thu, 24 Nov 2016 20:09:36 +1300 Subject: [PATCH] replace custom/libimagequant Color and Palette types with the identical go stdlib ones --- Color.go | 5 ----- Palette.go | 29 ----------------------------- Result.go | 22 +++++++++++++++++++--- 3 files changed, 19 insertions(+), 37 deletions(-) delete mode 100644 Color.go delete mode 100644 Palette.go diff --git a/Color.go b/Color.go deleted file mode 100644 index 3922de3..0000000 --- a/Color.go +++ /dev/null @@ -1,5 +0,0 @@ -package imagequant - -type Color struct { - r, g, b, a uint8 -} diff --git a/Palette.go b/Palette.go deleted file mode 100644 index 7b57320..0000000 --- a/Palette.go +++ /dev/null @@ -1,29 +0,0 @@ -package imagequant - -/* -#include "libimagequant.h" -*/ -import "C" - -// This struct has standard Go lifetime and does not need manual release. -type Palette struct { - p C.struct_liq_palette -} - -func (this *Palette) Count() uint { - return uint(this.p.count) -} - -func (this *Palette) At(idx int) (Color, error) { - if idx < 0 || idx >= int(this.Count()) { - return Color{}, ErrValueOutOfRange - } - - return Color{ - r: uint8(this.p.entries[idx].r), - g: uint8(this.p.entries[idx].g), - b: uint8(this.p.entries[idx].b), - a: uint8(this.p.entries[idx].a), - }, nil - -} diff --git a/Result.go b/Result.go index eb156d0..cd4836a 100644 --- a/Result.go +++ b/Result.go @@ -1,5 +1,9 @@ package imagequant +import ( + "image/color" +) + /* #include "libimagequant.h" */ @@ -68,9 +72,21 @@ func (this *Result) WriteRemappedImage() ([]byte, error) { return buff, nil } -func (this *Result) GetPalette() *Palette { - ptr := *C.liq_get_palette(this.p) // copy struct content - return &Palette{p: ptr} +func (this *Result) GetPalette() color.Palette { + ptr := C.liq_get_palette(this.p) // copy struct content + max := int(ptr.count) + + ret := make([]color.Color, max) + for i := 0; i < max; i += 1 { + ret[i] = color.RGBA{ + R: uint8(ptr.entries[i].r), + G: uint8(ptr.entries[i].g), + B: uint8(ptr.entries[i].b), + A: uint8(ptr.entries[i].a), + } + } + + return ret } // Free memory. Callers must not use this object after Release has been called.