| 1 |
package sixel |
| 2 |
|
| 3 |
import ( |
| 4 |
"container/heap" |
| 5 |
"image" |
| 6 |
"image/color" |
| 7 |
"math" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
type sixelPalette struct { |
| 31 |
|
| 32 |
colorConvert map[sixelColor]sixelColor |
| 33 |
|
| 34 |
paletteIndexes map[sixelColor]int |
| 35 |
PaletteColors []sixelColor |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
type quantizationChannel int |
| 41 |
|
| 42 |
const ( |
| 43 |
|
| 44 |
MaxColors int = 256 |
| 45 |
|
| 46 |
quantizationRed quantizationChannel = iota |
| 47 |
quantizationGreen |
| 48 |
quantizationBlue |
| 49 |
quantizationAlpha |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
type quantizationCube struct { |
| 54 |
|
| 55 |
startIndex int |
| 56 |
|
| 57 |
length int |
| 58 |
|
| 59 |
sliceChannel quantizationChannel |
| 60 |
|
| 61 |
score uint64 |
| 62 |
|
| 63 |
pixelCount uint64 |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
type cubePriorityQueue []any |
| 69 |
|
| 70 |
func (p *cubePriorityQueue) Push(x any) { |
| 71 |
*p = append(*p, x) |
| 72 |
} |
| 73 |
|
| 74 |
func (p *cubePriorityQueue) Pop() any { |
| 75 |
popped := (*p)[len(*p)-1] |
| 76 |
*p = (*p)[:len(*p)-1] |
| 77 |
return popped |
| 78 |
} |
| 79 |
|
| 80 |
func (p *cubePriorityQueue) Len() int { |
| 81 |
return len(*p) |
| 82 |
} |
| 83 |
|
| 84 |
func (p *cubePriorityQueue) Less(i, j int) bool { |
| 85 |
left := (*p)[i].(quantizationCube) |
| 86 |
right := (*p)[j].(quantizationCube) |
| 87 |
|
| 88 |
|
| 89 |
return left.score > right.score |
| 90 |
} |
| 91 |
|
| 92 |
func (p *cubePriorityQueue) Swap(i, j int) { |
| 93 |
(*p)[i], (*p)[j] = (*p)[j], (*p)[i] |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
func (p *sixelPalette) createCube(uniqueColors []sixelColor, pixelCounts map[sixelColor]uint64, startIndex, bucketLength int) quantizationCube { |
| 98 |
minRed, minGreen, minBlue, minAlpha := uint32(0xffff), uint32(0xffff), uint32(0xffff), uint32(0xffff) |
| 99 |
maxRed, maxGreen, maxBlue, maxAlpha := uint32(0), uint32(0), uint32(0), uint32(0) |
| 100 |
totalWeight := uint64(0) |
| 101 |
|
| 102 |
|
| 103 |
for i := startIndex; i < startIndex+bucketLength; i++ { |
| 104 |
r, g, b, a := uniqueColors[i].Red, uniqueColors[i].Green, uniqueColors[i].Blue, uniqueColors[i].Alpha |
| 105 |
totalWeight += pixelCounts[uniqueColors[i]] |
| 106 |
|
| 107 |
if r < minRed { |
| 108 |
minRed = r |
| 109 |
} |
| 110 |
if r > maxRed { |
| 111 |
maxRed = r |
| 112 |
} |
| 113 |
if g < minGreen { |
| 114 |
minGreen = g |
| 115 |
} |
| 116 |
if g > maxGreen { |
| 117 |
maxGreen = g |
| 118 |
} |
| 119 |
if b < minBlue { |
| 120 |
minBlue = b |
| 121 |
} |
| 122 |
if b > maxBlue { |
| 123 |
maxBlue = b |
| 124 |
} |
| 125 |
if a < minAlpha { |
| 126 |
minAlpha = a |
| 127 |
} |
| 128 |
if a > maxAlpha { |
| 129 |
maxAlpha = a |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
dRed := maxRed - minRed |
| 134 |
dGreen := maxGreen - minGreen |
| 135 |
dBlue := maxBlue - minBlue |
| 136 |
dAlpha := maxAlpha - minAlpha |
| 137 |
|
| 138 |
cube := quantizationCube{ |
| 139 |
startIndex: startIndex, |
| 140 |
length: bucketLength, |
| 141 |
pixelCount: totalWeight, |
| 142 |
} |
| 143 |
|
| 144 |
if dRed >= dGreen && dRed >= dBlue && dRed >= dAlpha { |
| 145 |
cube.sliceChannel = quantizationRed |
| 146 |
cube.score = uint64(dRed) |
| 147 |
} else if dGreen >= dBlue && dGreen >= dAlpha { |
| 148 |
cube.sliceChannel = quantizationGreen |
| 149 |
cube.score = uint64(dGreen) |
| 150 |
} else if dBlue >= dAlpha { |
| 151 |
cube.sliceChannel = quantizationBlue |
| 152 |
cube.score = uint64(dBlue) |
| 153 |
} else { |
| 154 |
cube.sliceChannel = quantizationAlpha |
| 155 |
cube.score = uint64(dAlpha) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
cube.score *= totalWeight |
| 160 |
|
| 161 |
return cube |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
func (p *sixelPalette) quantize(uniqueColors []sixelColor, pixelCounts map[sixelColor]uint64, maxColors int) { |
| 167 |
p.colorConvert = make(map[sixelColor]sixelColor) |
| 168 |
p.paletteIndexes = make(map[sixelColor]int) |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
if len(uniqueColors) <= maxColors { |
| 173 |
p.PaletteColors = uniqueColors |
| 174 |
return |
| 175 |
} |
| 176 |
|
| 177 |
cubeHeap := make(cubePriorityQueue, 0, maxColors) |
| 178 |
|
| 179 |
|
| 180 |
heap.Init(&cubeHeap) |
| 181 |
heap.Push(&cubeHeap, p.createCube(uniqueColors, pixelCounts, 0, len(uniqueColors))) |
| 182 |
|
| 183 |
|
| 184 |
for cubeHeap.Len() < maxColors { |
| 185 |
cubeToSplit := heap.Pop(&cubeHeap).(quantizationCube) |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
sortFunc(uniqueColors[cubeToSplit.startIndex:cubeToSplit.startIndex+cubeToSplit.length], |
| 191 |
func(left sixelColor, right sixelColor) int { |
| 192 |
switch cubeToSplit.sliceChannel { |
| 193 |
case quantizationRed: |
| 194 |
return compare(left.Red, right.Red) |
| 195 |
case quantizationGreen: |
| 196 |
return compare(left.Green, right.Green) |
| 197 |
case quantizationBlue: |
| 198 |
return compare(left.Blue, right.Blue) |
| 199 |
default: |
| 200 |
return compare(left.Alpha, right.Alpha) |
| 201 |
} |
| 202 |
}) |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
countSoFar := pixelCounts[uniqueColors[cubeToSplit.startIndex]] |
| 208 |
targetCount := cubeToSplit.pixelCount / 2 |
| 209 |
leftLength := 1 |
| 210 |
|
| 211 |
for i := cubeToSplit.startIndex + 1; i < cubeToSplit.startIndex+cubeToSplit.length; i++ { |
| 212 |
c := uniqueColors[i] |
| 213 |
weight := pixelCounts[c] |
| 214 |
if countSoFar+weight > targetCount { |
| 215 |
break |
| 216 |
} |
| 217 |
leftLength++ |
| 218 |
countSoFar += weight |
| 219 |
} |
| 220 |
|
| 221 |
rightLength := cubeToSplit.length - leftLength |
| 222 |
rightIndex := cubeToSplit.startIndex + leftLength |
| 223 |
heap.Push(&cubeHeap, p.createCube(uniqueColors, pixelCounts, cubeToSplit.startIndex, leftLength)) |
| 224 |
heap.Push(&cubeHeap, p.createCube(uniqueColors, pixelCounts, rightIndex, rightLength)) |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
for cubeHeap.Len() > 0 { |
| 229 |
bucketToLoad := heap.Pop(&cubeHeap).(quantizationCube) |
| 230 |
p.loadColor(uniqueColors, pixelCounts, bucketToLoad.startIndex, bucketToLoad.length) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
func (p *sixelPalette) ColorIndex(c sixelColor) int { |
| 236 |
return p.paletteIndexes[c] |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
func (p *sixelPalette) loadColor(uniqueColors []sixelColor, pixelCounts map[sixelColor]uint64, startIndex, cubeLen int) { |
| 242 |
totalRed, totalGreen, totalBlue, totalAlpha := uint64(0), uint64(0), uint64(0), uint64(0) |
| 243 |
totalCount := uint64(0) |
| 244 |
for i := startIndex; i < startIndex+cubeLen; i++ { |
| 245 |
count := pixelCounts[uniqueColors[i]] |
| 246 |
totalRed += uint64(uniqueColors[i].Red) * count |
| 247 |
totalGreen += uint64(uniqueColors[i].Green) * count |
| 248 |
totalBlue += uint64(uniqueColors[i].Blue) * count |
| 249 |
totalAlpha += uint64(uniqueColors[i].Alpha) * count |
| 250 |
totalCount += count |
| 251 |
} |
| 252 |
|
| 253 |
averageColor := sixelColor{ |
| 254 |
Red: uint32(totalRed / totalCount), |
| 255 |
Green: uint32(totalGreen / totalCount), |
| 256 |
Blue: uint32(totalBlue / totalCount), |
| 257 |
Alpha: uint32(totalAlpha / totalCount), |
| 258 |
} |
| 259 |
|
| 260 |
p.PaletteColors = append(p.PaletteColors, averageColor) |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
type sixelColor struct { |
| 266 |
Red uint32 |
| 267 |
Green uint32 |
| 268 |
Blue uint32 |
| 269 |
Alpha uint32 |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
func sixelConvertColor(c color.Color) sixelColor { |
| 275 |
r, g, b, a := c.RGBA() |
| 276 |
return sixelColor{ |
| 277 |
Red: sixelConvertChannel(r), |
| 278 |
Green: sixelConvertChannel(g), |
| 279 |
Blue: sixelConvertChannel(b), |
| 280 |
Alpha: sixelConvertChannel(a), |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
func sixelConvertChannel(channel uint32) uint32 { |
| 287 |
|
| 288 |
|
| 289 |
return (channel + 328) * 100 / 0xffff |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
func newSixelPalette(image image.Image, maxColors int) sixelPalette { |
| 296 |
pixelCounts := make(map[sixelColor]uint64) |
| 297 |
|
| 298 |
height := image.Bounds().Dy() |
| 299 |
width := image.Bounds().Dx() |
| 300 |
|
| 301 |
|
| 302 |
for y := range height { |
| 303 |
for x := range width { |
| 304 |
c := sixelConvertColor(image.At(x, y)) |
| 305 |
count := pixelCounts[c] |
| 306 |
count++ |
| 307 |
|
| 308 |
pixelCounts[c] = count |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
p := sixelPalette{} |
| 313 |
uniqueColors := make([]sixelColor, 0, len(pixelCounts)) |
| 314 |
for c := range pixelCounts { |
| 315 |
uniqueColors = append(uniqueColors, c) |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
p.quantize(uniqueColors, pixelCounts, maxColors) |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
for _, c := range uniqueColors { |
| 325 |
var bestColor sixelColor |
| 326 |
var bestColorIndex int |
| 327 |
bestScore := uint32(math.MaxUint32) |
| 328 |
|
| 329 |
for paletteIndex, paletteColor := range p.PaletteColors { |
| 330 |
redDiff := c.Red - paletteColor.Red |
| 331 |
greenDiff := c.Green - paletteColor.Green |
| 332 |
blueDiff := c.Blue - paletteColor.Blue |
| 333 |
alphaDiff := c.Alpha - paletteColor.Alpha |
| 334 |
|
| 335 |
score := (redDiff * redDiff) + (greenDiff * greenDiff) + (blueDiff * blueDiff) + (alphaDiff * alphaDiff) |
| 336 |
if score < bestScore { |
| 337 |
bestColor = paletteColor |
| 338 |
bestColorIndex = paletteIndex |
| 339 |
bestScore = score |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
p.paletteIndexes[c] = bestColorIndex |
| 344 |
p.colorConvert[c] = bestColor |
| 345 |
} |
| 346 |
|
| 347 |
return p |
| 348 |
} |