Files
bbs/internal/textui/logo.go
T
Claude 27377f2a49 Sixel graphics for telnet callers
Every ANSI terminal is asked for its device attributes (CSI c) and its
cell size in pixels (CSI 16 t); one that has sixel (attribute 4) sees
the logo as a picture above the system's name: [general] logo, a PNG,
or the name drawn in a pixel font, fitted to the window.  Callers turn
it off or on in their profile.  Listeners have sixel = auto (telnet's
default) or no (raw and rlogin: modem lines are slow).

The encoder (internal/sixel) keeps a picture's own colors up to 255
and dithers to the web-safe palette beyond, leaves transparent pixels
as the background, run-length codes, and keeps register 0 black for
decoders that fill unset pixels with it; a test decodes its output
with ImageMagick.  The logo's PNG and pixel font move to internal/logo,
shared with the 3270 logo; the PNG's size is checked before it is
decoded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
2026-10-10 17:35:12 +00:00

73 lines
1.6 KiB
Go

package textui
import (
"image"
"image/color"
"github.com/Neo-Desktop/bbs-sec/internal/logo"
"github.com/Neo-Desktop/bbs-sec/internal/sixel"
)
// the cell assumed when the terminal does not say
const (
defaultCellW = 10
defaultCellH = 20
logoRows = 6 // the most text rows the logo takes
)
// cyan is the name logo's color
var cyan = color.NRGBA{0x00, 0xb4, 0xff, 0xff}
// sixelLogo is the logo as sixel graphics for t: the PNG in file, or name
// in a pixel font two text rows high; nil if there is none to show.
func sixelLogo(t *Term, name, file string) []byte {
cw, ch := t.CellW, t.CellH
if cw == 0 || ch == 0 {
cw, ch = defaultCellW, defaultCellH
}
var img image.Image
if file != "" {
src, err := logo.Load(file)
if err != nil {
return nil
}
img = src
} else {
mono := logo.Name(name, 2*ch)
col := image.NewNRGBA(mono.Bounds())
for y := 0; y < mono.Bounds().Dy(); y++ {
for x := 0; x < mono.Bounds().Dx(); x++ {
if mono.GrayAt(x, y).Y >= 128 {
col.SetNRGBA(x, y, cyan)
}
}
}
img = col
}
img = shrink(img, (t.Cols-1)*cw, logoRows*ch)
data, err := sixel.Encode(img)
if err != nil {
return nil
}
return data
}
// shrink scales img down by a whole factor to fit maxW by maxH.
func shrink(img image.Image, maxW, maxH int) image.Image {
b := img.Bounds()
f := 1
for b.Dx()/f > maxW || b.Dy()/f > maxH {
f++
}
if f == 1 {
return img
}
out := image.NewNRGBA(image.Rect(0, 0, b.Dx()/f, b.Dy()/f))
for y := 0; y < out.Bounds().Dy(); y++ {
for x := 0; x < out.Bounds().Dx(); x++ {
out.Set(x, y, img.At(b.Min.X+x*f, b.Min.Y+y*f))
}
}
return out
}