Files
sshtermy/third_party/ansi/kitty/encoder.go
T
db9979andClaude Opus 5.5 a68a375e3d Zeichnen noch einmal fast doppelt so schnell (#109)
* Leistungsanzeige verständlicher, Zeichnen etwa doppelt so schnell

- Anzeige bewertet die Zeichenzeit in Worten (flüssig/gut/etwas träge/ruckelt),
  zwei Zeilen Balken je Bild statt Braille-Punkte, "möglich"-Bildrate,
  1 Bild/s im Leerlauf wird erklärt; im Terminal links neben der F6-Leiste,
  Toasts weichen darunter aus
- Overlays (Toasts, Tooltip, Ausblenden, Leistungsanzeige) ohne Compositor:
  stamp() fasst nur die überdeckten Zeilen an
- Terminalbild wird gemerkt, solange keine neue Ausgabe kam (Term.Gen)
- Seitenleiste ohne lipgloss-Umbruch anfügen (beside), Verläufe in einem Render
- ViewHL misst bereits aufgefüllte Zeilen nicht noch einmal
- Benchmarks: Terminal 2,9 → 0,9 ms, mit F6 + Anzeige 6,5 → 2,3 ms,
  mit laufender Ausgabe 6,9 → 3,5 ms je Bild

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018i4GUk5KxaHH89KrFPon9n

* Zeichnen noch einmal fast doppelt so schnell

- x/ansi als Fork (third_party/ansi): ASCII-Schnellweg in FirstGraphemeCluster,
  lipgloss misst jede Zeile jedes Bildes
- Terminal-Zeilen unter einer Sperre holen (vt LineCopy), Farbregeln aus
  denselben Zellen statt jede Zelle zweimal einzeln
- Seitenleiste (F6, Ziel, …) gemerkt, solange ihr Inhalt gleich bleibt
- Balken der Leistungsanzeige je Farbe in einem Render
- Benchmark mit F6, Anzeige und laufender Ausgabe: 3,5 → 1,8 ms je Bild

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018i4GUk5KxaHH89KrFPon9n

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-09 13:02:55 +01:00

65 lines
1.3 KiB
Go

package kitty
import (
"compress/zlib"
"fmt"
"image"
"image/png"
"io"
)
// Encoder is an encoder for the Kitty graphics protocol. It supports encoding
// images in the 24-bit [RGB], 32-bit [RGBA], and [PNG] formats, and
// compressing the data using zlib.
// The default format is 32-bit [RGBA].
type Encoder struct {
// Uses zlib compression.
Compress bool
// Can be one of [RGBA], [RGB], or [PNG].
Format int
}
// Encode encodes the image data in the specified format and writes it to w.
func (e *Encoder) Encode(w io.Writer, m image.Image) error {
if m == nil {
return nil
}
if e.Compress {
zw := zlib.NewWriter(w)
defer zw.Close() //nolint:errcheck
w = zw
}
if e.Format == 0 {
e.Format = RGBA
}
switch e.Format {
case RGBA, RGB:
bounds := m.Bounds()
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, a := m.At(x, y).RGBA()
switch e.Format {
case RGBA:
w.Write([]byte{byte(r >> 8), byte(g >> 8), byte(b >> 8), byte(a >> 8)}) //nolint:errcheck,gosec
case RGB:
w.Write([]byte{byte(r >> 8), byte(g >> 8), byte(b >> 8)}) //nolint:errcheck,gosec
}
}
}
case PNG:
if err := png.Encode(w, m); err != nil {
return fmt.Errorf("failed to encode PNG: %w", err)
}
default:
return fmt.Errorf("unsupported format: %d", e.Format)
}
return nil
}