From 9112d99c5478155b3700e54bb185ad6b724ab02d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sat, 9 Jan 2021 23:01:18 +0100 Subject: [PATCH] add cursor image to uri convertor. --- internal/utils/cursor.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 internal/utils/cursor.go diff --git a/internal/utils/cursor.go b/internal/utils/cursor.go new file mode 100644 index 00000000..d1c148d2 --- /dev/null +++ b/internal/utils/cursor.go @@ -0,0 +1,40 @@ +package utils + +import ( + "bytes" + "image" + "image/color" + "image/png" + "encoding/base64" + + "demodesk/neko/internal/types" +) + +func GetCursorImageURI(cursor *types.CursorImage) (string, error) { + width := int(cursor.Width) + height := int(cursor.Height) + pixels := cursor.Pixels + + img := image.NewRGBA(image.Rect(0, 0, width, height)) + for row := 0; row < height; row++ { + for col := 0; col < width; col++ { + pos := ((row * height) + col) * 8 + + img.SetRGBA(col, row, color.RGBA{ + A: pixels[pos+3], + R: pixels[pos+2], + G: pixels[pos+1], + B: pixels[pos+0], + }) + } + } + + out := new(bytes.Buffer) + err := png.Encode(out, img) + if err != nil { + return "", err + } + + uri := "data:image/png;base64," + base64.StdEncoding.EncodeToString(out.Bytes()) + return uri, nil +}