vcom/internal/imgview/scale.go
vrubelroman 6c13b19ad1 feat(imgview): image actor with kitty/ueberzugpp, SyncTTY and off-UI decoding (5C.1)
Adds golang.org/x/image v0.39.0 (draw, webp; vendored; x/sys and x/text
unchanged). SyncTTY serialises renderer frames and image blocks under one
mutex. The actor keeps only the latest Desired state (100 Apply → ≤ 2
decodes), refuses images above 50 Mpx from the header alone, scales with
ApproxBiLinear and writes kitty graphics as one ESC7/CUP/APC/ESC8 block
(raw RGBA, chunked or via a temp file, tmux passthrough). ueberzugpp gets
JSON lines through a pipe with a 200 ms write deadline; a helper that stops
reading is killed with its process group and the backend falls back to
none with an event. Stop removes the placement, temp files and processes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 09:01:26 +03:00

92 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package imgview
import (
"context"
"errors"
"fmt"
"image"
_ "image/gif" // decoders
_ "image/jpeg"
_ "image/png"
"os"
"golang.org/x/image/draw"
_ "golang.org/x/image/webp"
)
// MaxPixels: images above this (by DecodeConfig) are refused without a
// full decode.
const MaxPixels = 50_000_000
// ErrTooLarge: the image has more than MaxPixels.
var ErrTooLarge = errors.New("image too large to display")
// decodeHook counts full decodes (tests).
var decodeHook func()
// loadScaled decodes the file and scales it to fit maxW×maxH pixels
// (keeping the aspect ratio, never enlarging).
func loadScaled(ctx context.Context, path string, maxW, maxH int) (*image.RGBA, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
if _, err := configOf(f); err != nil {
return nil, err
}
if _, err := f.Seek(0, 0); err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
if decodeHook != nil {
decodeHook()
}
src, _, err := image.Decode(f)
if err != nil {
return nil, err
}
if err := ctx.Err(); err != nil {
return nil, err
}
return scaleToFit(src, maxW, maxH), nil
}
// scaleToFit returns src scaled into maxW×maxH with ApproxBiLinear.
func scaleToFit(src image.Image, maxW, maxH int) *image.RGBA {
b := src.Bounds()
w, h := b.Dx(), b.Dy()
if maxW > 0 && maxH > 0 && (w > maxW || h > maxH) {
if w*maxH > h*maxW {
w, h = maxW, max(1, h*maxW/w)
} else {
w, h = max(1, w*maxH/h), maxH
}
}
dst := image.NewRGBA(image.Rect(0, 0, w, h))
draw.ApproxBiLinear.Scale(dst, dst.Bounds(), src, b, draw.Src, nil)
return dst
}
func configOf(f *os.File) (image.Config, error) {
cfg, _, err := image.DecodeConfig(f)
if err != nil {
return cfg, err
}
if int64(cfg.Width)*int64(cfg.Height) > MaxPixels {
return cfg, fmt.Errorf("%w: %dx%d", ErrTooLarge, cfg.Width, cfg.Height)
}
return cfg, nil
}
// imageConfig reads only the header and enforces MaxPixels.
func imageConfig(path string) (image.Config, error) {
f, err := os.Open(path)
if err != nil {
return image.Config{}, err
}
defer f.Close()
return configOf(f)
}