From 6d967bc1f9bec76a6eb2627079cd77b90cf0c5ab Mon Sep 17 00:00:00 2001 From: Alex X Date: Thu, 16 May 2024 17:33:09 +0300 Subject: [PATCH] Improve ascii stream for any one symbol --- internal/mjpeg/README.md | 7 ++++--- pkg/ascii/ascii.go | 35 +++++++++++++++-------------------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/internal/mjpeg/README.md b/internal/mjpeg/README.md index eabe62e0..e261d0d5 100644 --- a/internal/mjpeg/README.md +++ b/internal/mjpeg/README.md @@ -9,18 +9,19 @@ - escape text param with urlencode - you can stream any camera or file from a disc -**go2rtc.yaml** - transcoding to MJPEG, terminal size - 210x60, fps - 4 +**go2rtc.yaml** - transcoding to MJPEG, terminal size - 210x59 (16/9), fps - 10 ```yaml streams: - macarena: ffmpeg:macarena.mp4#video=mjpeg#hardware#width=210#height=60#raw=-r 4 + macarena: ffmpeg:macarena.mp4#video=mjpeg#hardware#width=210#height=59#raw=-r 10 ``` **API params** - `color` - foreground color, values: empty, `8`, `256`, `rgb` - `back` - background color, values: empty, `8`, `256`, `rgb` -- `text` - character set, values: empty, one space, two spaces, anything you like (in order of brightness) +- `text` - character set, values: empty, one char, `block`, list of chars (in order of brightness) + - example: `%20` (space), `block` (block elements), `ox` (two chars) **Examples** diff --git a/pkg/ascii/ascii.go b/pkg/ascii/ascii.go index 959ab265..c75d7f88 100644 --- a/pkg/ascii/ascii.go +++ b/pkg/ascii/ascii.go @@ -61,35 +61,30 @@ func NewWriter(w io.Writer, foreground, background, text string) io.Writer { } } - var ascii string - switch text { - case "": - ascii = ` .::--~~==++**##%%$@` - case " ": - a.text = func(r, g, b uint32) { - a.buf = append(a.buf, ' ') + if len(text) == 1 { + // fast 1 symbol version + a.text = func(_, _, _ uint32) { + a.buf = append(a.buf, text[0]) } - case " ": - a.text = func(r, g, b uint32) { - a.buf = append(a.buf, ' ', ' ') + } else { + switch text { + case "": + text = ` .::--~~==++**##%%$@` // default for empty text + case "block": + text = " ░░▒▒▓▓█" // https://en.wikipedia.org/wiki/Block_Elements } - case "block": - ascii = " ░░▒▒▓▓█" // https://en.wikipedia.org/wiki/Block_Elements - default: - ascii = text - } - if ascii != "" { - if runes := []rune(ascii); len(runes) != len(ascii) { + + if runes := []rune(text); len(runes) != len(text) { k := float32(len(runes)-1) / 255 a.text = func(r, g, b uint32) { i := gray(r, g, b, k) a.buf = utf8.AppendRune(a.buf, runes[i]) } } else { - k := float32(len(ascii)-1) / 255 + k := float32(len(text)-1) / 255 a.text = func(r, g, b uint32) { i := gray(r, g, b, k) - a.buf = append(a.buf, ascii[i]) + a.buf = append(a.buf, text[i]) } } } @@ -130,7 +125,7 @@ func (a *writer) Write(p []byte) (n int, err error) { a.buf = append(a.buf, '\n') } - a.buf = append(a.buf, "\033[0m\n"...) + a.buf = append(a.buf, "\033[0m"...) if n, err = a.wr.Write(a.buf); err == nil { a.wr.(http.Flusher).Flush()