-
Notifications
You must be signed in to change notification settings - Fork 0
/
golibimagequant_test.go
202 lines (173 loc) · 4.23 KB
/
golibimagequant_test.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package golibimagequant
import (
"image"
"image/color"
"image/png"
_ "image/png"
"os"
"testing"
)
func Test_Version(t *testing.T) {
var expected uint = 40202
result := Version()
if result != expected {
t.Errorf("result = %d, expected = %d", result, expected)
}
}
// -------
// attr creation
// -------
func Test_CopyAttr(t *testing.T) {
attr := CreateAttr()
attrCopy := CopyAttr(attr)
if attr == nil || attrCopy == nil {
t.Error("error copying attr")
}
}
// -------
// quality controls
// -------
func Test_SetMaxColors(t *testing.T) {
attr := CreateAttr()
expected := 2
SetMaxColors(attr, expected)
result := GetMaxColors(attr)
DestroyAttr(attr)
if result != expected {
t.Errorf("have = %d , want %d\n", result, expected)
}
}
func Test_SetSpeed(t *testing.T) {
attr := CreateAttr()
speedSet := 2
err := SetSpeed(attr, speedSet)
if err != 0 {
t.Errorf("error setting speed")
}
speedGet := GetSpeed(attr)
DestroyAttr(attr)
if speedSet != speedGet {
t.Errorf("speed: have = %d , want = %d\n", speedGet, speedSet)
}
}
func Test_SetMinPosterization(t *testing.T) {
attr := CreateAttr()
minPosterizationSet := 2
err := SetMinPosterization(attr, minPosterizationSet)
if err != 0 {
t.Errorf("error setting min posterization")
}
minPosterizationGet := GetMinPosterization(attr)
if minPosterizationSet != minPosterizationGet {
t.Errorf("min posterization: have = %d , want = %d\n", minPosterizationGet, minPosterizationSet)
}
}
func Test_SetQuality(t *testing.T) {
attr := CreateAttr()
minSet := 2
maxSet := 50
err := SetQuality(attr, minSet, maxSet)
if err != 0 {
t.Errorf("error setting quality")
}
minGet := GetMinQuantity(attr)
maxGet := GetMaxQuantity(attr)
DestroyAttr(attr)
if minGet != minSet {
t.Errorf("min: have = %d , want = %d\n", minSet, minGet)
}
if maxGet != maxSet {
t.Errorf("max: have = %d , want = %d\n", maxSet, maxGet)
}
}
// -------
// image controls
// -------
func Test_AddFixedColor(t *testing.T) {
attr := CreateAttr()
data := [4]uint8{0, 0, 0, 0}
image := CreateImageRGBA(attr, &data[0], 1, 1, 0)
firstColor := LiqColor{
R: 1,
G: 1,
B: 1,
A: 1,
}
AddFixedColor(image, firstColor)
secondColor := LiqColor{
R: 3,
G: 3,
B: 3,
A: 3,
}
AddFixedColor(image, secondColor)
var result *LiqResult
err := QuantizeImage(attr, image, &result)
if err != 0 {
t.Errorf("error quantizing image")
}
palette := GetPalette(result)
firstResult := palette.Entries[0]
secondResult := palette.Entries[1]
if firstColor != firstResult {
t.Fatalf("have = %+v , want = %+v", firstResult, firstColor)
}
if secondColor != secondResult {
t.Fatalf("have = %+v , want = %+v", firstResult, firstColor)
}
}
func Test_SimplePNGQuant(t *testing.T) {
raw, width, height := loadRawData("./images/example.png")
cattr := CreateAttr()
SetQuality(cattr, 0, 25)
SetSpeed(cattr, 1)
cimage := CreateImageRGBA(cattr, &raw[0], width, height, 0)
var cresult *LiqResult
_ = QuantizeImage(cattr, cimage, &cresult)
pixels := make([]uint8, width*height)
WriteRemappedImage(cresult, cimage, &pixels[0], uint64(len(pixels)))
cpalette := GetPalette(cresult)
rectangle := image.Rect(0, 0, width, height)
palette := make(color.Palette, 0)
for _, entry := range cpalette.Entries {
palette = append(palette, NewRGBA(entry))
}
image := image.NewPaletted(rectangle, palette)
image.Pix = pixels
file, err := os.Create("./images/example_compressed.png")
if err != nil {
t.Fatalf("error creating file")
}
defer file.Close()
err = png.Encode(file, image)
if err != nil {
t.Fatalf("error encoding png")
}
DestroyResult(cresult)
DestroyImage(cimage)
DestroyAttr(cattr)
}
func loadRawData(filepath string) ([]uint8, int, int) {
file, err := os.Open(filepath)
if err != nil {
panic(err)
}
defer file.Close()
image, _, err := image.Decode(file)
if err != nil {
panic(err)
}
size := image.Bounds()
width := size.Max.X - size.Min.X
height := size.Max.Y - size.Min.Y
raw := make([]uint8, width*height*4)
index := 0
for y := size.Min.Y; y < size.Max.Y; y += 1 {
for x := size.Min.X; x < size.Max.X; x += 1 {
r, g, b, a := image.At(x, y).RGBA()
raw[index], raw[index+1], raw[index+2], raw[index+3] = uint8(r), uint8(g), uint8(b), uint8(a)
index += 4
}
}
return raw, width, height
}