-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfifEncoder.go
More file actions
273 lines (236 loc) · 6.35 KB
/
fifEncoder.go
File metadata and controls
273 lines (236 loc) · 6.35 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package main
import (
"image"
"math"
"sort"
"sync"
)
var simpCache = make(map[uint32]uint32)
var simpMutex sync.Mutex
func simplifyColor(palette []uint32, color uint32) uint32 {
simpMutex.Lock()
if _, ok := simpCache[color]; ok == true {
simpMutex.Unlock()
return simpCache[color]
}
simpMutex.Unlock()
var closestDelta = int64(math.MaxInt64)
var pick uint32
r, g, b := splitColor(color)
for i := 0; i < len(palette); i++ {
col := palette[i]
if color == col {
simpMutex.Lock()
simpCache[color] = col
simpMutex.Unlock()
return col
} else {
pR, pG, pB := splitColor(col)
factorR := int64(int(pR) - int(r))
factorG := int64(int(pG) - int(g))
factorB := int64(int(pB) - int(b))
delta := (factorR * factorR) + (factorG * factorG) + (factorB * factorB)
if delta < closestDelta {
closestDelta = delta
pick = col
}
}
}
simpMutex.Lock()
simpCache[color] = pick
simpMutex.Unlock()
return pick
}
type CAEntry struct {
color uint64
entries int
}
func encodeFif(w int, h int, src image.Image, dontSimplify bool) []byte {
mimage := make([][]uint32, w)
imask := make([][]bool, w) // Pixels set to false in the imask will no longer be rendered.
// Later on when converting to characters, when even 1 character exists
// The imask for it is true.
fif := []byte("FastIF")
fif = append(fif, byte(w/2), byte(h/4))
for x := 0; x < w; x++ {
mimage[x] = make([]uint32, h)
imask[x] = make([]bool, h)
for y := 0; y < h; y++ {
r, g, b, a := src.At(x, y).RGBA()
r = r / 257
g = g / 257
b = b / 257
if a > 8 {
zcolor := ((r & 0xFF) << 16) + ((g & 0xFF) << 8) + b&0xFF
if dontSimplify == false {
mimage[x][y] = simplifyColor(palette, zcolor)
} else {
mimage[x][y] = zcolor
}
imask[x][y] = true
} else {
imask[x][y] = false
}
}
}
// Encode the whole thing as fif segments
fifParts := make([][]NonFinalFifSegment, w/2)
for i := 0; i < (w / 2); i++ {
fifParts[i] = make([]NonFinalFifSegment, h/4)
}
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
fifParts[x/2][y/4].Set(x%2, y%4, mimage[x][y])
}
}
// fmt.Println(fifParts)
// Simplify the fif segments
// TODO: This can be multithreaded!
sFifParts := make([][]*FifSegment, w/2)
for i := 0; i < (w / 2); i++ {
sFifParts[i] = make([]*FifSegment, h/4)
for j := 0; j < (h / 4); j++ {
sFifParts[i][j] = fifParts[i][j].ToFinalFifSegment()
}
}
maskParts := make([][]bool, w/2)
maskCount := 0
for i := 0; i < (w / 2); i++ {
maskParts[i] = make([]bool, h/4)
for j := 0; j < (h / 4); j++ {
maskParts[i][j] = false
for x := 0; x < 2; x++ {
for y := 0; y < 4; y++ {
if imask[(i*2)+x][(j*4)+y] == true {
maskParts[i][j] = true
}
}
}
if maskParts[i][j] == true {
maskCount++
}
}
}
colorCombos := make(map[uint64]int)
for i := 0; i < (w / 2); i++ {
for j := 0; j < (h / 4); j++ {
combo := uint64(sFifParts[i][j].fg) << 24
combo = combo + uint64(sFifParts[i][j].bg)
if _, ok := colorCombos[combo]; ok == false {
colorCombos[combo] = 0
}
colorCombos[combo] = colorCombos[combo] + 1
}
}
caArray := make([]CAEntry, 0)
for color, occurences := range colorCombos {
caArray = append(caArray, CAEntry{
color: color,
entries: occurences,
})
}
sort.Slice(caArray, func(i, j int) bool {
return caArray[i].entries > caArray[j].entries
})
oldbg := uint32(math.MaxUint32)
oldfg := uint32(math.MaxUint32)
for c := 0; c < len(caArray); c++ {
color := caArray[c].color
bg := uint32(color & 0xFFFFFF)
fg := uint32((color >> 24) & 0xFFFFFF)
// Write set bg and set fg opcodes
if bg != oldbg {
fif = append(fif, writeSetBg(bg)...)
oldbg = bg
}
if fg != oldfg {
fif = append(fif, writeSetFg(fg)...)
oldfg = fg
}
for y := 0; y < (h / 4); y++ {
for x := 0; x < (w / 2); x++ {
// First, check if not masked away.
if maskParts[x][y] == false {
continue
}
// Compare colors
if sFifParts[x][y].bg != bg || sFifParts[x][y].fg != fg {
continue
}
if FIF_altMode == false {
// Test all 3 cases
coveredSl, packetSl := tryStraightLine(x, y, sFifParts, maskParts)
coveredVl, packetVl := tryVerticalLine(x, y, sFifParts, maskParts)
coveredSq, packetSq := trySquare(x, y, sFifParts, maskParts)
greatest, gV := 0, 0
if coveredSl > gV {
greatest, gV = 0, coveredSl
}
if coveredVl > gV {
greatest, gV = 1, coveredVl
}
if coveredSq > gV {
greatest, gV = 2, coveredSq
}
if greatest == 0 {
fif = append(fif, packetSl...)
maskParts, _ = updateMaskStraightLine(packetSl, maskParts)
}
if greatest == 1 {
fif = append(fif, packetVl...)
maskParts, _ = updateMaskVerticalLine(packetVl, maskParts)
}
if greatest == 2 {
fif = append(fif, packetSq...)
maskParts, _ = updateMaskSquare(packetSq, maskParts)
}
} else {
// Test all 3 cases
coveredASl, packetASl, goodSl := tryStraightLineAlt(x, y, sFifParts, maskParts)
coveredAVl, packetAVl, goodVl := tryVerticalLineAlt(x, y, sFifParts, maskParts)
coveredSl, packetSl := tryStraightLine(x, y, sFifParts, maskParts)
coveredVl, packetVl := tryVerticalLine(x, y, sFifParts, maskParts)
coveredSq, packetSq := trySquare(x, y, sFifParts, maskParts)
greatest, gV := 0, 0
if coveredSl > gV {
greatest, gV = 0, coveredSl
}
if coveredVl > gV {
greatest, gV = 1, coveredVl
}
if coveredSq > gV {
greatest, gV = 2, coveredSq
}
if coveredASl > gV {
greatest, gV = 3, coveredASl
}
if coveredAVl > gV {
greatest, gV = 4, coveredAVl
}
if greatest == 0 {
fif = append(fif, packetSl...)
maskParts, _ = updateMaskStraightLine(packetSl, maskParts)
}
if greatest == 1 {
fif = append(fif, packetVl...)
maskParts, _ = updateMaskVerticalLine(packetVl, maskParts)
}
if greatest == 2 {
fif = append(fif, packetSq...)
maskParts, _ = updateMaskSquare(packetSq, maskParts)
}
if greatest == 3 {
fif = append(fif, packetASl...)
maskParts, _ = updateMaskStraightLineAlt(packetASl, maskParts, goodSl)
}
if greatest == 4 {
fif = append(fif, packetAVl...)
maskParts, _ = updateMaskVerticalLineAlt(packetAVl, maskParts, goodVl)
}
}
}
}
}
fif = append(fif, 0x20)
return fif
}