-
Notifications
You must be signed in to change notification settings - Fork 0
/
pikster.go
258 lines (209 loc) · 4.91 KB
/
pikster.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package pikster
import (
"fmt"
"image"
"log"
"math"
"math/rand"
"os"
// Package image/{jpeg,gif,png} is not used explicitly in the code below
_ "image/gif"
_ "image/png"
"image/jpeg"
)
// ImageType is the type of image
type ImageType int
// ImageType constants
const (
JPEG ImageType = 0
GIF ImageType = 1
PNG ImageType = 2
UNKNOWN ImageType = 10
)
type colorMapVal struct {
colorPoint *ColorPoint
index int
}
// PImage holds all the details needed to cluster the image
type PImage struct {
imgType ImageType
height int
width int
clusterCount uint
colorMap map[ColorPoint]*colorMapVal
colorList []*ColorPoint
clusterList []*ColorPoint
pixelList []*PixelPoint
}
func NewPImageFromFile(filename string, clusterCount uint) *PImage {
reader, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer reader.Close()
img, name, err := image.Decode(reader)
if err != nil {
log.Fatal(err)
}
imgType := getImageType(name)
if imgType == UNKNOWN {
log.Fatal("Unknown image Type")
}
return NewPImageFromImage(img, imgType, clusterCount)
}
func NewPImageFromImage(img image.Image, imageType ImageType, clusterCount uint) *PImage {
bounds := img.Bounds()
pimage := &PImage{
imgType: imageType,
colorMap: map[ColorPoint]*colorMapVal{},
colorList: make([]*ColorPoint, 0),
clusterList: make([]*ColorPoint, 0),
pixelList: make([]*PixelPoint, 0),
height: bounds.Max.Y,
width: bounds.Max.X,
clusterCount: clusterCount,
}
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, _ := img.At(x, y).RGBA()
r, g, b = r/0x101, g/0x101, b/0x101
p := NewColorPoint(uint8(r), uint8(g), uint8(b))
cmv, ok := pimage.colorMap[*p]
if !ok {
pimage.colorList = append(pimage.colorList, p)
cmv = &colorMapVal{
colorPoint: p,
index: -1,
}
pimage.colorMap[*p] = cmv
}
pimage.pixelList = append(pimage.pixelList, NewPixelPoint(uint32(x), uint32(y), cmv.colorPoint))
}
}
if len(pimage.colorList) < int(clusterCount) {
log.Fatal("Error more clusters")
}
clusterPointIndexes := getNfromM(int(clusterCount), len(pimage.colorList))
for _, i := range clusterPointIndexes {
pimage.clusterList = append(pimage.clusterList, pimage.colorList[i].Copy())
}
return pimage
}
// SaveFile saves the image to a file
func (p *PImage) SaveFile(name string) {
filename := name + "." + getImageTypeExt(p.imgType)
imgRect := image.Rect(0, 0, p.width, p.height)
img := image.NewRGBA(imgRect)
// color the image
for _, pp := range p.pixelList {
cmv := p.colorMap[*pp.color]
cp := p.clusterList[cmv.index]
img.SetRGBA(int(pp.x), int(pp.y), cp.ToColor())
}
saveJPEG(filename, img)
}
func (p *PImage) RunClusteringStep() int {
for _, cp := range p.colorList {
closest := cp.Distance(p.clusterList[0])
closestCluster := 0
for i := 1; i < len(p.clusterList); i++ {
t := cp.Distance(p.clusterList[i])
if t < closest {
closest = t
closestCluster = i
}
}
cmv := p.colorMap[*cp]
cmv.index = closestCluster
}
var totalChange = 0
for i, cp := range p.clusterList {
var sr float64
var sg float64
var sb float64
var n uint64
prevR := cp.r
prevG := cp.g
prevB := cp.b
for k, v := range p.colorMap {
if v.index != i {
continue
}
sr += float64(k.r)
sg += float64(k.g)
sb += float64(k.b)
n++
}
sr, sg, sb = sr/float64(n), sg/float64(n), sb/float64(n)
cp.r = uint8(sr)
cp.g = uint8(sg)
cp.b = uint8(sb)
totalChange += int(math.Abs(float64(prevR-cp.r)) + math.Abs(float64(prevG-cp.g)) + math.Abs(float64(prevB-cp.b)))
}
fmt.Printf("change: %d\n", totalChange)
fmt.Println("Clustering Step Done!")
return totalChange
}
func getNfromM(n int, m int) []int {
pickedColors := map[int]bool{}
ret := make([]int, 0)
for len(ret) < n {
r := rand.Intn(m)
_, v := pickedColors[r]
if !v {
pickedColors[r] = true
ret = append(ret, r)
}
}
return ret
}
func saveJPEG(filename string, img image.Image) {
var opt jpeg.Options
opt.Quality = 80
out, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
err = jpeg.Encode(out, img, &opt)
if err != nil {
log.Fatal(err)
}
}
func getImageTypeExt(t ImageType) string {
switch t {
case JPEG:
return "jpeg"
case PNG:
return "png"
case GIF:
return "gif"
}
return "unknown"
}
func getImageType(t string) ImageType {
switch t {
case "jpeg":
fallthrough
case "jpg":
return JPEG
case "png":
return PNG
case "gif":
return GIF
}
return UNKNOWN
}
// // Run runs
// func Run() {
// pimage := NewPImage("./img2.png", 10)
// // fmt.Println(len(pimage.colorMap))
// // fmt.Println(len(pimage.pixelList))
// // fmt.Println(len(pimage.colorList))
// // fmt.Println(len(pimage.clusterList))
// for i := 0; i < 100; i++ {
// if pimage.runClusteringStep() == 0 {
// break
// }
// }
// pimage.SaveFile("xxx")
// }