1 package quantize
2
3 import (
4 "bufio"
5 "fmt"
6 "image"
7 "image/color"
8 "image/gif"
9 "os"
10 "testing"
11
12 _ "image/jpeg"
13 )
14
15 func TestBuildBucket(t *testing.T) {
16 file, err := os.Open("test_image.jpg")
17 if err != nil {
18 t.Fatal("Couldn't open test file")
19 }
20 i, _, err := image.Decode(file)
21 if err != nil {
22 t.Fatal("Couldn't decode test file")
23 }
24
25 q := MedianCutQuantizer{Mode, nil, false}
26
27 colors := q.buildBucket(i)
28 t.Logf("Naive color map contains %d elements", len(colors))
29
30 for _, p := range colors {
31 if p.p == 0 {
32 t.Fatal("Bucket had a 0 priority element")
33 }
34 }
35
36 q = MedianCutQuantizer{Mode, func(i image.Image, x int, y int) uint32 {
37 if x < 2 || y < 2 || x > i.Bounds().Max.X-2 || y > i.Bounds().Max.X-2 {
38 return 1
39 }
40 return 0
41 }, false}
42
43 colors = q.buildBucket(i)
44 t.Logf("Color map contains %d elements", len(colors))
45 }
46
47 func ExampleMedianCutQuantizer() {
48 file, err := os.Open("test_image.jpg")
49 if err != nil {
50 fmt.Println("Couldn't open test file")
51 return
52 }
53 i, _, err := image.Decode(file)
54 if err != nil {
55 fmt.Println("Couldn't decode test file")
56 return
57 }
58 q := MedianCutQuantizer{}
59 p := q.Quantize(make([]color.Color, 0, 256), i)
60 fmt.Println(p)
61 }
62
63 func TestQuantize(t *testing.T) {
64 file, err := os.Open("test_image.jpg")
65 if err != nil {
66 t.Fatal("Couldn't open test file")
67 }
68 i, _, err := image.Decode(file)
69 if err != nil {
70 t.Fatal("Couldn't decode test file")
71 }
72 q := MedianCutQuantizer{Mean, nil, false}
73 p := q.Quantize(make([]color.Color, 0, 256), i)
74 t.Logf("Created palette with %d colors", len(p))
75
76 q = MedianCutQuantizer{Mean, nil, false}
77 p2 := q.Quantize(make([]color.Color, 0, 256), i)
78
79 if len(p) != len(p2) {
80 t.Fatal("Quantize is not deterministic")
81 }
82
83 for i := range p {
84 if p[i] != p2[i] {
85 t.Fatal("Quantize is not deterministic")
86 }
87 }
88
89 q = MedianCutQuantizer{Mode, nil, false}
90 p = q.Quantize(make([]color.Color, 0, 256), i)
91 t.Logf("Created palette with %d colors", len(p))
92
93 q = MedianCutQuantizer{Mean, nil, true}
94 p = q.Quantize(color.Palette{color.RGBA{0, 0, 0, 0}}, i)
95 t.Logf("Created palette with %d colors", len(p))
96
97 q = MedianCutQuantizer{Mean, nil, true}
98 p = q.Quantize(make([]color.Color, 0, 256), i)
99 t.Logf("Created palette with %d colors", len(p))
100 }
101
102 func BenchmarkQuantize(b *testing.B) {
103 file, err := os.Open("test_image.jpg")
104 if err != nil {
105 b.Fatal("Couldn't open test file")
106 }
107 m, _, err := image.Decode(file)
108 if err != nil {
109 b.Fatal("Couldn't decode test file")
110 }
111 q := MedianCutQuantizer{Mean, nil, false}
112 b.ReportAllocs()
113 b.ResetTimer()
114 for i := 0; i < b.N; i++ {
115 q.Quantize(make([]color.Color, 0, 256), m)
116 }
117 }
118
119 func TestRGBAQuantize(t *testing.T) {
120 i := image.NewRGBA(image.Rect(0, 0, 1, 1))
121 q := MedianCutQuantizer{Mean, nil, false}
122 p := q.Quantize(make([]color.Color, 0, 256), i)
123 t.Logf("Created palette with %d colors", len(p))
124 }
125
126
127 func TestOverQuantize(t *testing.T) {
128 file, err := os.Open("test_image2.gif")
129 if err != nil {
130 t.Fatal("Couldn't open test file")
131 }
132 i, _, err := image.Decode(file)
133 if err != nil {
134 t.Fatal("Couldn't decode test file")
135 }
136 q := MedianCutQuantizer{Mean, nil, false}
137 p := q.Quantize(make([]color.Color, 0, 256), i)
138 t.Logf("Created palette with %d colors", len(p))
139 }
140
141 func TestEmptyQuantize(t *testing.T) {
142 i := image.NewNRGBA(image.Rect(0, 0, 0, 0))
143
144 q := MedianCutQuantizer{Mean, nil, false}
145 p := q.Quantize(make([]color.Color, 0, 256), i)
146 if len(p) != 0 {
147 t.Fatal("Quantizer returned colors for empty image")
148 }
149 t.Logf("Created palette with %d colors", len(p))
150 }
151
152 func TestGif(t *testing.T) {
153 file, err := os.Open("test_image.jpg")
154 if err != nil {
155 t.Fatal("Couldn't open test file")
156 }
157 i, _, err := image.Decode(file)
158 if err != nil {
159 t.Fatal("Couldn't decode test file")
160 }
161
162 q := MedianCutQuantizer{Mode, nil, false}
163 f, err := os.Create("test_output.gif")
164 if err != nil {
165 t.Fatal("Couldn't open output file")
166 }
167
168 options := gif.Options{NumColors: 128, Quantizer: q, Drawer: nil}
169
170 w := bufio.NewWriter(f)
171
172 gif.Encode(w, i, &options)
173 }
174
View as plain text