This commit is contained in:
bel
2023-04-04 22:51:14 -06:00
parent 997c725752
commit 67b7cd4579
9 changed files with 252 additions and 0 deletions

7
testdata/draw.d/go.mod vendored Normal file
View File

@@ -0,0 +1,7 @@
module gogs.inhome.blapointe.com/anki.d/testdata/draw.d
go 1.19
require github.com/nsf/termbox-go v1.1.1
require github.com/mattn/go-runewidth v0.0.9 // indirect

4
testdata/draw.d/go.sum vendored Normal file
View File

@@ -0,0 +1,4 @@
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=

146
testdata/draw.d/main.go vendored Normal file
View File

@@ -0,0 +1,146 @@
package main
import (
"image"
"image/color"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"log"
"math"
"os"
termbox "github.com/nsf/termbox-go"
)
var palette color.Palette = []color.Color{
attrColor(termbox.ColorBlack),
attrColor(termbox.ColorRed),
attrColor(termbox.ColorGreen),
attrColor(termbox.ColorYellow),
attrColor(termbox.ColorBlue),
attrColor(termbox.ColorMagenta),
attrColor(termbox.ColorCyan),
attrColor(termbox.ColorWhite),
}
func upgrade(palette color.Palette) color.Palette {
for i := 16; i <= 255; i++ {
palette = append(palette, attrColor(i))
}
return palette
}
type attrColor termbox.Attribute
var base = [...]uint8{0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff}
func (c attrColor) RGBA() (r, g, b, a uint32) {
switch termbox.Attribute(c) {
case termbox.ColorBlack:
return 0, 0, 0, math.MaxUint16
case termbox.ColorRed:
return math.MaxUint16, 0, 0, math.MaxUint16
case termbox.ColorGreen:
return 0, math.MaxUint16, 0, math.MaxUint16
case termbox.ColorYellow:
return math.MaxUint16, math.MaxUint16, 0, math.MaxUint16
case termbox.ColorBlue:
return 0, 0, math.MaxUint16, math.MaxUint16
case termbox.ColorMagenta:
return math.MaxUint16, 0, math.MaxUint16, math.MaxUint16
case termbox.ColorCyan:
return 0, math.MaxUint16, math.MaxUint16, math.MaxUint16
case termbox.ColorWhite:
return math.MaxUint16, math.MaxUint16, math.MaxUint16, math.MaxUint16
}
switch {
case c >= 16 && c <= 231:
c -= 16
rgba := color.RGBA{R: base[(c/36)%6], G: base[(c/6)%6], B: base[c%6], A: 0xff}
return rgba.RGBA()
case c >= 232 && c <= 255:
x := uint8(0x08 + 0xa*(c-232))
rgba := color.RGBA{R: x, G: x, B: x, A: 0xff}
return rgba.RGBA()
}
panic("not found")
}
func fit(w, h, iw, ih int) (sw, sh int) {
if w >= iw && h >= ih {
// image is smaller than screen
return iw, ih
} else if w >= iw {
// image is taller than the screen
return iw * h / ih, h
} else if h >= ih {
// image is skinnier than the screen
return w, ih * w / iw
} else {
sw, sh = iw*h/ih, h
if sw <= w {
return sw, sh
}
return w, ih * w / iw
}
}
func draw(img image.Image) {
w, h := termbox.Size()
iw, ih := img.Bounds().Max.X, img.Bounds().Max.Y
sw, sh := fit(w, h, iw*2, ih)
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
if x >= sw || y >= sh {
termbox.SetCell(x, y, ' ', termbox.ColorDefault, termbox.ColorDefault)
continue
}
xi := x * img.Bounds().Max.X / sw
yi := y * img.Bounds().Max.Y / sh
a := palette.Convert(img.At(xi, yi))
if at, ok := a.(attrColor); ok {
termbox.SetCell(x, y, ' ', termbox.ColorDefault,
termbox.Attribute(at))
}
}
}
termbox.Flush()
}
func main() {
fname := os.Args[1]
f, err := os.Open(fname)
if err != nil {
log.Fatal(err)
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
log.Fatal(err)
}
err = termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
if termbox.SetOutputMode(termbox.Output256) == termbox.Output256 {
palette = upgrade(palette)
}
lw, lh := termbox.Size()
draw(img)
termbox.PollEvent()
termbox.Flush()
if w, h := termbox.Size(); lw != w || lh != h {
lw, lh = w, h
draw(img)
}
}

2
testdata/tiv.sh vendored
View File

@@ -13,6 +13,8 @@
# Unreadable: 1×2 (high resolution, slowest rendering, without double horizontal resolution)
char_w=5
char_h=8
char_w=1
char_h=2
# If file doesn't exist, then die with 'No such file or directory' exit code
if [ ! -f "$1" ]

BIN
testdata/tofugu.d/a-hiragana-0.sm.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB