replace custom/libimagequant Color and Palette types with the identical go stdlib ones
This commit is contained in:
parent
deecf65b22
commit
ea2ed0e25b
29
Palette.go
29
Palette.go
@ -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
|
||||
|
||||
}
|
22
Result.go
22
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.
|
||||
|
Loading…
Reference in New Issue
Block a user