replace custom/libimagequant Color and Palette types with the identical go stdlib ones

This commit is contained in:
mappu 2016-11-24 20:09:36 +13:00
parent deecf65b22
commit ea2ed0e25b
3 changed files with 19 additions and 37 deletions

View File

@ -1,5 +0,0 @@
package imagequant
type Color struct {
r, g, b, a uint8
}

View File

@ -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
}

View File

@ -1,5 +1,9 @@
package imagequant package imagequant
import (
"image/color"
)
/* /*
#include "libimagequant.h" #include "libimagequant.h"
*/ */
@ -68,9 +72,21 @@ func (this *Result) WriteRemappedImage() ([]byte, error) {
return buff, nil return buff, nil
} }
func (this *Result) GetPalette() *Palette { func (this *Result) GetPalette() color.Palette {
ptr := *C.liq_get_palette(this.p) // copy struct content ptr := C.liq_get_palette(this.p) // copy struct content
return &Palette{p: ptr} 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. // Free memory. Callers must not use this object after Release has been called.