From f21ad568cc7bcec4907fa8a8402a729678a6ffca Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Wed, 14 Oct 2020 17:10:31 +0200 Subject: [PATCH] Don't always reallocate memory to store ascii chars --- stream/telnet/telnet.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/stream/telnet/telnet.go b/stream/telnet/telnet.go index 1b5e66d..2a125f8 100644 --- a/stream/telnet/telnet.go +++ b/stream/telnet/telnet.go @@ -15,6 +15,8 @@ var ( Cfg *Options currentMessage map[string]*string clientCount map[string]int + + ASCIICHARS = []byte{' ', '.', ',', ':', ';', '+', '*', '?', '%', '$', '#', '@'} ) // Options holds telnet package configuration @@ -128,11 +130,6 @@ func GetNumberConnectedSessions(streamID string) int { return clientCount[streamID] } -func asciiChar(pixel byte) byte { - asciiChars := []byte{'@', '#', '$', '%', '?', '*', '+', ';', ':', ',', '.', ' '} - return asciiChars[(255-pixel)/22] -} - // StartASCIIArtStream send all packets received by ffmpeg as ASCII Art to telnet clients func StartASCIIArtStream(streamID string, reader io.ReadCloser) { if !Cfg.Enabled { @@ -164,8 +161,8 @@ func StartASCIIArtStream(streamID string, reader io.ReadCloser) { // Convert image to ASCII for j := 0; j < Cfg.Height; j++ { for i := 0; i < Cfg.Width; i++ { - textBuff.WriteByte(asciiChar(pixelBuff[Cfg.Width*j+i])) - textBuff.WriteByte(asciiChar(pixelBuff[Cfg.Width*j+i])) + textBuff.WriteByte(ASCIICHARS[pixelBuff[Cfg.Width*j+i]/22]) + textBuff.WriteByte(ASCIICHARS[pixelBuff[Cfg.Width*j+i]/22]) } textBuff.WriteByte('\n') }