From 3d875397ab4f21f06bc490982a62fdd62aa76a91 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Thu, 31 Aug 2023 08:46:26 -0700 Subject: Use bufio for output. This is a lot faster for a large image. --- icat.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'icat.go') diff --git a/icat.go b/icat.go index ae1b7a0..f2c3f08 100644 --- a/icat.go +++ b/icat.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "fmt" "golang.org/x/image/draw" "golang.org/x/term" @@ -45,12 +46,14 @@ func printImg(filename string, cols, lines int) error { dst := image.NewRGBA(image.Rect(0, 0, cols, lines)) draw.ApproxBiLinear.Scale(dst, dst.Bounds(), img, img.Bounds(), draw.Over, nil) + buf := bufio.NewWriter(os.Stdout) + defer buf.Flush() for y := 0; y < lines; y++ { for x := 0; x < cols; x++ { r, g, b, _ := dst.At(x, y).RGBA() - fmt.Printf("\033[48;2;%d;%d;%dm ", r>>8, g>>8, b>>8) + fmt.Fprintf(buf, "\033[48;2;%d;%d;%dm ", r>>8, g>>8, b>>8) } - fmt.Println("\033[49m") + fmt.Fprintln(buf, "\033[49m") } return nil } -- cgit v1.3.1