-
Notifications
You must be signed in to change notification settings - Fork 4
/
quantize.go
72 lines (58 loc) · 1.74 KB
/
quantize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package juroku
import (
"fmt"
"image"
"github.com/1lann/imagequant"
)
// Quantize quantizes an image into a maximum of 16 colors with the given
// parameters.
func Quantize(ref, img image.Image, speed int, dither float64) (image.Image, error) {
attr, err := getAttributes(speed)
if err != nil {
return nil, fmt.Errorf("Attribues: %s", err.Error())
}
defer attr.Release()
quant, err := imagequant.NewImage(attr, imagequant.GoImageToRgba32(ref),
ref.Bounds().Dx(), ref.Bounds().Dy(), 0)
if err != nil {
return nil, fmt.Errorf("ref NewImage: %s", err.Error())
}
defer quant.Release()
res, err := quant.Quantize(attr)
if err != nil {
return nil, fmt.Errorf("Quantize: %s", err.Error())
}
outputImg, err := imagequant.NewImage(attr, imagequant.GoImageToRgba32(img),
img.Bounds().Dx(), img.Bounds().Dy(), 0)
if err != nil {
return nil, fmt.Errorf("img NewImage: %s", err.Error())
}
defer outputImg.Release()
res.SetOutputImage(outputImg)
err = res.SetDitheringLevel(float32(dither))
if err != nil {
return nil, fmt.Errorf("SetDitheringLevel: %s", err.Error())
}
rgb8data, err := res.WriteRemappedImage()
if err != nil {
return nil, fmt.Errorf("WriteRemappedImage: %s", err.Error())
}
result := imagequant.Rgb8PaletteToGoImage(res.GetImageWidth(),
res.GetImageHeight(), rgb8data, res.GetPalette())
return result, nil
}
func getAttributes(speed int) (*imagequant.Attributes, error) {
attr, err := imagequant.NewAttributes()
if err != nil {
return nil, fmt.Errorf("NewAttributes: %s", err.Error())
}
err = attr.SetSpeed(speed)
if err != nil {
return nil, fmt.Errorf("SetSpeed: %s", err.Error())
}
err = attr.SetMaxColors(16)
if err != nil {
return nil, fmt.Errorf("SetMaxColors: %s", err.Error())
}
return attr, nil
}