sophuwu.site > sketch
lol forgot to commit was too focused
its mostly done i think?
sophuwu sophie@skisiel.com
Sun, 26 Nov 2023 12:02:17 +0100
commit

4477518811df0079eeee848e6daf1d19993444b5

parent

20d71991d95c6a37ecb4dafce0f3c5251d355bd8

4 files changed, 104 insertions(+), 1 deletions(-)

jump to
M .gitignore.gitignore

@@ -1,3 +1,4 @@

.idea sketch build +hotdog.png
M go.modgo.mod

@@ -1,3 +1,10 @@

module sketch go 1.21.3 + +require ( + github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 + golang.org/x/term v0.14.0 +) + +require golang.org/x/sys v0.14.0 // indirect
A go.sum

@@ -0,0 +1,6 @@

+github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= +golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.14.0 h1:LGK9IlZ8T9jvdy6cTdfKUCltatMFOehAQo9SRC46UQ8= +golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww=
M main.gomain.go

@@ -1,3 +1,92 @@

package main -func main() {}+import ( + "fmt" + "github.com/nfnt/resize" + "golang.org/x/term" + "image" + _ "image/jpeg" + _ "image/png" + "os" +) + +var w, h int + +func getTermSize() { + W, H, err := term.GetSize(int(os.Stdout.Fd())) + if err != nil { + fmt.Fprintln(os.Stderr, "fatal: could not get terminal size") + os.Exit(1) + } + w, h = W, H +} + +func getSize(x, y int) (uint, uint) { + if y > h { + x = x * h / y + y = h + } + if x > w { + y = y * w / x + x = w + } + return uint(x), uint(y) / 2 +} + +func getImg(path string) (image.Image, error) { + var img image.Image + file, err := os.Open(path) + if err != nil { + return img, fmt.Errorf("error opening file: %s", path) + } + defer file.Close() + img, _, err = image.Decode(file) + if err != nil { + return img, fmt.Errorf("error decoding file: %s", path) + } + return img, nil +} + +func printImg(path string) error { + + img, err := getImg(path) + if err != nil { + return err + } + + W, H := getSize(img.Bounds().Dx(), img.Bounds().Dy()) + + img = resize.Resize(W, H, img, resize.MitchellNetravali) + + bounds := img.Bounds() + for y := bounds.Min.Y; y < bounds.Max.Y; y++ { + for x := bounds.Min.X; x < bounds.Max.X; x++ { + r, g, b, _ := img.At(x, y).RGBA() + fmt.Printf("\033[48;2;%d;%d;%dm \033[0m", r>>8, g>>8, b>>8) + } + fmt.Println() + } + fmt.Println() + return nil +} + +func main() { + if len(os.Args) < 2 { + fmt.Fprintln(os.Stderr, "fatal: no image specified") + os.Exit(1) + } + getTermSize() + var errs []error + for _, path := range os.Args[1:] { + err := printImg(path) + if err != nil { + errs = append(errs, err) + } + } + if len(errs) > 0 { + for _, err := range errs { + fmt.Fprintln(os.Stderr, err) + } + os.Exit(1) + } +}