cursor utils changed to generic CreatePNGImage.

This commit is contained in:
Miroslav Šedivý 2021-02-25 14:15:18 +01:00
parent 8f142e4b91
commit d2eda6f25f
4 changed files with 43 additions and 38 deletions

View File

@ -1,8 +1,6 @@
package room
import (
"bytes"
"image/jpeg"
"net/http"
"strconv"
@ -83,11 +81,8 @@ func (h *RoomHandler) screenShotGet(w http.ResponseWriter, r *http.Request) {
}
img := h.desktop.GetScreenshotImage()
out := new(bytes.Buffer)
if err := jpeg.Encode(out, img, &jpeg.Options{
Quality: quality,
}); err != nil {
bytes, err := utils.CreateJPGImage(img, quality)
if err != nil {
utils.HttpInternalServerError(w, err)
return
}
@ -95,7 +90,7 @@ func (h *RoomHandler) screenShotGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Content-Type", "image/jpeg")
//nolint
w.Write(out.Bytes())
w.Write(bytes)
}
func (h *RoomHandler) screenCastGet(w http.ResponseWriter, r *http.Request) {

View File

@ -1,29 +0,0 @@
package utils
import (
"bytes"
"encoding/base64"
"image/png"
"demodesk/neko/internal/types"
)
func GetCursorImage(cursor *types.CursorImage) ([]byte, error) {
out := new(bytes.Buffer)
err := png.Encode(out, cursor.Image)
if err != nil {
return nil, err
}
return out.Bytes(), nil
}
func GetCursorImageURI(cursor *types.CursorImage) (string, error) {
img, err := GetCursorImage(cursor)
if err != nil {
return "", err
}
uri := "data:image/png;base64," + base64.StdEncoding.EncodeToString(img)
return uri, nil
}

39
internal/utils/image.go Normal file
View File

@ -0,0 +1,39 @@
package utils
import (
"bytes"
"encoding/base64"
"image"
"image/jpeg"
"image/png"
)
func CreatePNGImage(img *image.RGBA) ([]byte, error) {
out := new(bytes.Buffer)
err := png.Encode(out, img)
if err != nil {
return nil, err
}
return out.Bytes(), nil
}
func CreateJPGImage(img *image.RGBA, quality int) ([]byte, error) {
out := new(bytes.Buffer)
err := jpeg.Encode(out, img, &jpeg.Options{ Quality: quality })
if err != nil {
return nil, err
}
return out.Bytes(), nil
}
func CreatePNGImageURI(img *image.RGBA) (string, error) {
data, err := CreatePNGImage(img)
if err != nil {
return "", err
}
uri := "data:image/png;base64," + base64.StdEncoding.EncodeToString(data)
return uri, nil
}

View File

@ -55,7 +55,7 @@ func (peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage) error {
return fmt.Errorf("no data channel")
}
img, err := utils.GetCursorImage(cur)
img, err := utils.CreatePNGImage(cur.Image)
if err != nil {
return err
}