| 1 |
|
| 2 |
package mosaic |
| 3 |
|
| 4 |
import ( |
| 5 |
"image" |
| 6 |
"image/color" |
| 7 |
"image/color/palette" |
| 8 |
"image/draw" |
| 9 |
"math" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"github.com/charmbracelet/x/ansi" |
| 13 |
xdraw "golang.org/x/image/draw" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
var ( |
| 18 |
halfBlocks = []block{ |
| 19 |
{Char: '▀', Coverage: [4]bool{true, true, false, false}, CoverageMap: "██\n block. |
| 20 |
{Char: '▄', Coverage: [4]bool{false, false, true, true}, CoverageMap: " \n██block. |
| 21 |
{Char: ' ', Coverage: [4]bool{false, false, false, false}, CoverageMap: " \n "}, |
| 22 |
{Char: '█', Coverage: [4]bool{true, true, true, true}, CoverageMap: "██\n█ull block. |
| 23 |
} |
| 24 |
quarterBlocks = []block{ |
| 25 |
{Char: '▘', Coverage: [4]bool{true, false, false, false}, CoverageMap: "█ \n eft. |
| 26 |
{Char: '▝', Coverage: [4]bool{false, true, false, false}, CoverageMap: " █\n ght. |
| 27 |
{Char: '▖', Coverage: [4]bool{false, false, true, false}, CoverageMap: " \n█ eft. |
| 28 |
{Char: '▗', Coverage: [4]bool{false, false, false, true}, CoverageMap: " \n █ght. |
| 29 |
{Char: '▌', Coverage: [4]bool{true, false, true, false}, CoverageMap: "█ \n█block. |
| 30 |
{Char: '▐', Coverage: [4]bool{false, true, false, true}, CoverageMap: " █\n block. |
| 31 |
{Char: '▀', Coverage: [4]bool{true, true, false, false}, CoverageMap: "██\n dded). |
| 32 |
{Char: '▄', Coverage: [4]bool{false, false, true, true}, CoverageMap: " \n█dded). |
| 33 |
} |
| 34 |
complexBlocks = []block{ |
| 35 |
{Char: '▙', Coverage: [4]bool{true, false, true, true}, CoverageMap: "█ \n█er half. |
| 36 |
{Char: '▟', Coverage: [4]bool{false, true, true, true}, CoverageMap: " █\n█er half. |
| 37 |
{Char: '▛', Coverage: [4]bool{true, true, true, false}, CoverageMap: "██\ner left. |
| 38 |
{Char: '▜', Coverage: [4]bool{true, true, false, true}, CoverageMap: "██\n r right. |
| 39 |
{Char: '▚', Coverage: [4]bool{true, false, false, true}, CoverageMap: "█ \n right. |
| 40 |
{Char: '▞', Coverage: [4]bool{false, true, true, false}, CoverageMap: " █\n left. |
| 41 |
} |
| 42 |
) |
| 43 |
|
| 44 |
|
| 45 |
type block struct { |
| 46 |
Char rune |
| 47 |
Coverage [4]bool |
| 48 |
CoverageMap string |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
type Symbol uint8 |
| 53 |
|
| 54 |
|
| 55 |
const ( |
| 56 |
All Symbol = iota |
| 57 |
Half |
| 58 |
Quarter |
| 59 |
) |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
const middleThresholdLevel = 128 |
| 66 |
|
| 67 |
|
| 68 |
func Render(img image.Image, width int, height int) string { |
| 69 |
m := New().Width(width).Height(height) |
| 70 |
return m.Render(img) |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
type Mosaic struct { |
| 83 |
outputWidth int |
| 84 |
outputHeight int |
| 85 |
thresholdLevel uint8 |
| 86 |
dither bool |
| 87 |
useFgBgOnly bool |
| 88 |
invertColors bool |
| 89 |
scale int |
| 90 |
symbols Symbol |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
func New() Mosaic { |
| 95 |
return Mosaic{ |
| 96 |
outputWidth: 0, |
| 97 |
outputHeight: 0, |
| 98 |
thresholdLevel: middleThresholdLevel, |
| 99 |
dither: false, |
| 100 |
useFgBgOnly: false, |
| 101 |
invertColors: false, |
| 102 |
scale: 1, |
| 103 |
symbols: Half, |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
type pixelBlock struct { |
| 109 |
Pixels [2][2]color.Color |
| 110 |
AvgFg color.Color |
| 111 |
AvgBg color.Color |
| 112 |
BestSymbol rune |
| 113 |
BestFgColor color.Color |
| 114 |
BestBgColor color.Color |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
const u8MaxValue = 0xff |
| 119 |
|
| 120 |
type shiftable interface { |
| 121 |
~uint | ~uint16 | ~uint32 | ~uint64 |
| 122 |
} |
| 123 |
|
| 124 |
func shift[T shiftable](x T) T { |
| 125 |
if x > u8MaxValue { |
| 126 |
x >>= 8 |
| 127 |
} |
| 128 |
return x |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
func (m Mosaic) Scale(scale int) Mosaic { |
| 133 |
m.scale = scale |
| 134 |
return m |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
func (m Mosaic) IgnoreBlockSymbols(fgOnly bool) Mosaic { |
| 139 |
m.useFgBgOnly = fgOnly |
| 140 |
return m |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
func (m Mosaic) Dither(dither bool) Mosaic { |
| 145 |
m.dither = dither |
| 146 |
return m |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
func (m Mosaic) Threshold(threshold int) Mosaic { |
| 152 |
if threshold >= 0 && threshold <= u8MaxValue { |
| 153 |
m.thresholdLevel = uint8(threshold) |
| 154 |
} |
| 155 |
|
| 156 |
return m |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
func (m Mosaic) InvertColors(invertColors bool) Mosaic { |
| 161 |
m.invertColors = invertColors |
| 162 |
return m |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
func (m Mosaic) Width(width int) Mosaic { |
| 167 |
m.outputWidth = width |
| 168 |
return m |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
func (m Mosaic) Height(height int) Mosaic { |
| 173 |
m.outputHeight = height |
| 174 |
return m |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
func (m Mosaic) Symbol(symbol Symbol) Mosaic { |
| 179 |
m.symbols = symbol |
| 180 |
return m |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
func (m *Mosaic) Render(img image.Image) string { |
| 185 |
|
| 186 |
bounds := img.Bounds() |
| 187 |
srcWidth := bounds.Max.X - bounds.Min.X |
| 188 |
srcHeight := bounds.Max.Y - bounds.Min.Y |
| 189 |
|
| 190 |
|
| 191 |
outWidth := srcWidth |
| 192 |
if m.outputWidth > 0 { |
| 193 |
outWidth = m.outputWidth |
| 194 |
} |
| 195 |
|
| 196 |
outHeight := srcHeight |
| 197 |
if m.outputHeight > 0 { |
| 198 |
outHeight = m.outputHeight |
| 199 |
} |
| 200 |
|
| 201 |
if outHeight <= 0 { |
| 202 |
|
| 203 |
|
| 204 |
const divider = 2 |
| 205 |
outHeight = int(float64(outWidth) * float64(srcHeight) / float64(srcWidth) / divider) |
| 206 |
if outHeight < 1 { |
| 207 |
outHeight = 1 |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
scaledImg := m.applyScaling(img, outWidth*m.scale, outHeight*m.scale) |
| 213 |
|
| 214 |
|
| 215 |
if m.dither { |
| 216 |
scaledImg = m.applyDithering(scaledImg) |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
if m.invertColors { |
| 221 |
scaledImg = m.invertImage(scaledImg) |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
var output strings.Builder |
| 226 |
|
| 227 |
|
| 228 |
imageBounds := scaledImg.Bounds() |
| 229 |
|
| 230 |
|
| 231 |
blocks := halfBlocks |
| 232 |
|
| 233 |
|
| 234 |
if m.symbols == Quarter || m.symbols == All { |
| 235 |
blocks = append(blocks, quarterBlocks...) |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
if m.symbols == All { |
| 240 |
blocks = append(blocks, complexBlocks...) |
| 241 |
} |
| 242 |
|
| 243 |
for y := 0; y < imageBounds.Max.Y; y += 2 { |
| 244 |
for x := 0; x < imageBounds.Max.X; x += 2 { |
| 245 |
|
| 246 |
block := m.createPixelBlock(scaledImg, x, y) |
| 247 |
|
| 248 |
|
| 249 |
m.findBestRepresentation(block, blocks) |
| 250 |
|
| 251 |
|
| 252 |
output.WriteString( |
| 253 |
ansi.Style{}.ForegroundColor(block.BestFgColor).BackgroundColor(block.BestBgColor).Styled(string(block.BestSymbol)), |
| 254 |
) |
| 255 |
} |
| 256 |
output.WriteString("\n") |
| 257 |
} |
| 258 |
|
| 259 |
return output.String() |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
func (m *Mosaic) createPixelBlock(img image.Image, x, y int) *pixelBlock { |
| 264 |
block := &pixelBlock{} |
| 265 |
|
| 266 |
|
| 267 |
for dy := 0; dy < 2; dy++ { |
| 268 |
for dx := 0; dx < 2; dx++ { |
| 269 |
block.Pixels[dy][dx] = m.getPixelSafe(img, x+dx, y+dy) |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
return block |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
func (m *Mosaic) findBestRepresentation(block *pixelBlock, availableBlocks []block) { |
| 278 |
|
| 279 |
if m.useFgBgOnly { |
| 280 |
|
| 281 |
block.BestSymbol = '▀' |
| 282 |
block.BestBgColor = m.averageColors(block.Pixels[0][0], block.Pixels[0][1]) |
| 283 |
block.BestFgColor = m.averageColors(block.Pixels[1][0], block.Pixels[1][1]) |
| 284 |
return |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
pixelMask := [2][2]bool{} |
| 289 |
for y := 0; y < 2; y++ { |
| 290 |
for x := 0; x < 2; x++ { |
| 291 |
|
| 292 |
luma := rgbaToLuminance(block.Pixels[y][x]) |
| 293 |
pixelMask[y][x] = luma >= m.thresholdLevel |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
bestChar := ' ' |
| 299 |
bestScore := math.MaxFloat64 |
| 300 |
|
| 301 |
for _, blockChar := range availableBlocks { |
| 302 |
score := 0.0 |
| 303 |
for i := 0; i < 4; i++ { |
| 304 |
y, x := i/2, i%2 |
| 305 |
if blockChar.Coverage[i] != pixelMask[y][x] { |
| 306 |
score += 1.0 |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
if score < bestScore { |
| 311 |
bestScore = score |
| 312 |
bestChar = blockChar.Char |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
var fgPixels, bgPixels []color.Color |
| 318 |
|
| 319 |
|
| 320 |
var coverage [4]bool |
| 321 |
for _, b := range availableBlocks { |
| 322 |
if b.Char == bestChar { |
| 323 |
coverage = b.Coverage |
| 324 |
break |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
for i := 0; i < 4; i++ { |
| 330 |
y, x := i/2, i%2 |
| 331 |
if coverage[i] { |
| 332 |
fgPixels = append(fgPixels, block.Pixels[y][x]) |
| 333 |
} else { |
| 334 |
bgPixels = append(bgPixels, block.Pixels[y][x]) |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
if len(fgPixels) > 0 { |
| 340 |
block.BestFgColor = m.averageColors(fgPixels...) |
| 341 |
} else { |
| 342 |
|
| 343 |
block.BestFgColor = color.Black |
| 344 |
} |
| 345 |
|
| 346 |
if len(bgPixels) > 0 { |
| 347 |
block.BestBgColor = m.averageColors(bgPixels...) |
| 348 |
} else { |
| 349 |
|
| 350 |
block.BestBgColor = color.Black |
| 351 |
} |
| 352 |
|
| 353 |
block.BestSymbol = bestChar |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
func (m *Mosaic) averageColors(colors ...color.Color) color.Color { |
| 358 |
if len(colors) == 0 { |
| 359 |
return color.Black |
| 360 |
} |
| 361 |
|
| 362 |
var sumR, sumG, sumB, sumA uint32 |
| 363 |
|
| 364 |
for _, c := range colors { |
| 365 |
r, g, b, a := c.RGBA() |
| 366 |
r, g, b, a = shift(r), shift(g), shift(b), shift(a) |
| 367 |
sumR += r |
| 368 |
sumG += g |
| 369 |
sumB += b |
| 370 |
sumA += a |
| 371 |
} |
| 372 |
|
| 373 |
count := uint32(len(colors)) |
| 374 |
return color.RGBA{ |
| 375 |
R: uint8(sumR / count), |
| 376 |
G: uint8(sumG / count), |
| 377 |
B: uint8(sumB / count), |
| 378 |
A: uint8(sumA / count), |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
func (m *Mosaic) getPixelSafe(img image.Image, x, y int) color.RGBA { |
| 384 |
bounds := img.Bounds() |
| 385 |
if x < bounds.Min.X || x >= bounds.Max.X || y < bounds.Min.Y || y >= bounds.Max.Y { |
| 386 |
return color.RGBA{0, 0, 0, 255} |
| 387 |
} |
| 388 |
|
| 389 |
r8, g8, b8, a8 := img.At(x, y).RGBA() |
| 390 |
return color.RGBA{ |
| 391 |
R: uint8(r8 >> 8), |
| 392 |
G: uint8(g8 >> 8), |
| 393 |
B: uint8(b8 >> 8), |
| 394 |
A: uint8(a8 >> 8), |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
func (m *Mosaic) applyScaling(img image.Image, width, height int) image.Image { |
| 400 |
rect := image.Rect(0, 0, width, height) |
| 401 |
dst := image.NewRGBA(rect) |
| 402 |
xdraw.ApproxBiLinear.Scale(dst, rect, img, img.Bounds(), draw.Over, nil) |
| 403 |
return dst |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
func (m *Mosaic) applyDithering(img image.Image) image.Image { |
| 408 |
b := img.Bounds() |
| 409 |
pm := image.NewPaletted(b, palette.Plan9) |
| 410 |
draw.FloydSteinberg.Draw(pm, b, img, image.Point{}) |
| 411 |
return pm |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
func (m *Mosaic) invertImage(img image.Image) image.Image { |
| 416 |
bounds := img.Bounds() |
| 417 |
width := bounds.Max.X - bounds.Min.X |
| 418 |
height := bounds.Max.Y - bounds.Min.Y |
| 419 |
|
| 420 |
result := image.NewRGBA(bounds) |
| 421 |
for y := 0; y < height; y++ { |
| 422 |
for x := 0; x < width; x++ { |
| 423 |
r8, g8, b8, a8 := img.At(x+bounds.Min.X, y+bounds.Min.Y).RGBA() |
| 424 |
result.Set(x, y, color.RGBA{ |
| 425 |
R: uint8(255 - (r8 >> 8)), |
| 426 |
G: uint8(255 - (g8 >> 8)), |
| 427 |
B: uint8(255 - (b8 >> 8)), |
| 428 |
A: uint8(a8 >> 8), |
| 429 |
}) |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
return result |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
func rgbaToLuminance(c color.Color) uint8 { |
| 438 |
r, g, b, _ := c.RGBA() |
| 439 |
r, g, b = shift(r), shift(g), shift(b) |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
return uint8(float64(r)*0.299 + float64(g)*0.587 + float64(b)*0.114) |
| 444 |
} |