2022-08-29 21:51:59 +03:00
|
|
|
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
|
|
|
|
|
|
|
package at
|
|
|
|
|
|
|
|
import (
|
2022-09-01 19:01:31 +03:00
|
|
|
"bytes"
|
2022-08-29 21:51:59 +03:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2022-09-01 19:01:31 +03:00
|
|
|
"image"
|
2022-08-29 21:51:59 +03:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2022-09-01 19:01:31 +03:00
|
|
|
|
2022-12-01 11:28:06 +03:00
|
|
|
"kitty/tools/utils/images"
|
2022-08-29 21:51:59 +03:00
|
|
|
)
|
|
|
|
|
2022-08-31 17:33:51 +03:00
|
|
|
func set_payload_data(io_data *rc_io_data, data string) {
|
|
|
|
set_payload_string_field(io_data, "Data", data)
|
|
|
|
}
|
|
|
|
|
2022-09-01 19:06:22 +03:00
|
|
|
func read_window_logo(io_data *rc_io_data, path string) (func(io_data *rc_io_data) (bool, error), error) {
|
2022-08-29 21:51:59 +03:00
|
|
|
if strings.ToLower(path) == "none" {
|
2022-09-01 19:06:22 +03:00
|
|
|
io_data.rc.Stream = false
|
2022-08-29 21:51:59 +03:00
|
|
|
return func(io_data *rc_io_data) (bool, error) {
|
2022-08-31 17:33:51 +03:00
|
|
|
set_payload_data(io_data, "-")
|
2022-08-29 21:51:59 +03:00
|
|
|
return true, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-01 19:01:31 +03:00
|
|
|
var image_data_stream io.Reader
|
|
|
|
image_data_stream = f
|
|
|
|
config, format, ierr := image.DecodeConfig(f)
|
|
|
|
if ierr != nil {
|
|
|
|
return nil, fmt.Errorf("%s is not a supported image format", path)
|
2022-08-29 21:51:59 +03:00
|
|
|
}
|
2022-09-01 19:01:31 +03:00
|
|
|
f.Seek(0, 0)
|
2022-08-29 21:51:59 +03:00
|
|
|
|
2022-09-01 19:01:31 +03:00
|
|
|
if format != "png" {
|
|
|
|
f.Seek(0, 0)
|
|
|
|
img, _, err := image.Decode(f)
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
|
|
|
}
|
2022-08-29 21:51:59 +03:00
|
|
|
f.Close()
|
2022-09-01 19:01:31 +03:00
|
|
|
b := bytes.Buffer{}
|
|
|
|
b.Grow(config.Height * config.Width * 4)
|
2022-12-01 11:28:06 +03:00
|
|
|
err = images.Encode(&b, img, "image/png")
|
2022-09-01 19:01:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
image_data_stream = &b
|
2022-08-29 21:51:59 +03:00
|
|
|
}
|
2022-08-31 17:33:51 +03:00
|
|
|
is_first_call := true
|
2022-09-01 19:01:31 +03:00
|
|
|
buf := make([]byte, 2048)
|
2022-08-29 21:51:59 +03:00
|
|
|
|
|
|
|
return func(io_data *rc_io_data) (bool, error) {
|
2022-08-31 17:33:51 +03:00
|
|
|
if is_first_call {
|
|
|
|
is_first_call = false
|
|
|
|
} else {
|
|
|
|
io_data.rc.Stream = false
|
|
|
|
}
|
2022-08-29 21:51:59 +03:00
|
|
|
buf = buf[:cap(buf)]
|
2022-09-01 19:01:31 +03:00
|
|
|
n, err := image_data_stream.Read(buf)
|
2022-08-29 21:51:59 +03:00
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
buf = buf[:n]
|
2022-09-01 19:01:31 +03:00
|
|
|
set_payload_data(io_data, base64.StdEncoding.EncodeToString(buf))
|
|
|
|
if err == io.EOF {
|
|
|
|
return true, nil
|
|
|
|
}
|
2022-08-30 13:19:26 +03:00
|
|
|
return false, nil
|
2022-08-29 21:51:59 +03:00
|
|
|
}, nil
|
|
|
|
}
|