neko/internal/utils/cursor.go

50 lines
937 B
Go
Raw Normal View History

2021-01-10 11:01:18 +13:00
package utils
import (
"bytes"
2021-01-19 03:31:00 +13:00
"image"
"image/color"
2021-01-10 11:01:18 +13:00
"image/png"
"encoding/base64"
"demodesk/neko/internal/types"
)
2021-02-13 11:43:52 +13:00
func GetCursorImage(cursor *types.CursorImage) ([]byte, error) {
2021-01-10 11:01:18 +13:00
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],
})
}
}
2021-01-19 03:31:00 +13:00
out := new(bytes.Buffer)
err := png.Encode(out, img)
2021-02-13 11:43:52 +13:00
if err != nil {
return nil, err
}
return out.Bytes(), nil
}
func GetCursorImageURI(cursor *types.CursorImage) (string, error) {
img, err := GetCursorImage(cursor)
2021-01-10 11:01:18 +13:00
if err != nil {
return "", err
}
2021-02-13 11:43:52 +13:00
uri := "data:image/png;base64," + base64.StdEncoding.EncodeToString(img)
2021-01-10 11:01:18 +13:00
return uri, nil
}