-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathimage.go
79 lines (72 loc) · 1.89 KB
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package streamdeck
import (
"bytes"
"errors"
"fmt"
"image"
"image/color"
"image/draw"
_ "image/gif" // Allow gifs to be loaded
"image/jpeg"
_ "image/png" // Allow pngs to be loaded
"os"
"github.com/disintegration/gift"
"golang.org/x/image/bmp"
)
func resizeAndRotate(img image.Image, width, height int, devname string) image.Image {
g, _ := deviceSpecifics(devname, width, height)
res := image.NewRGBA(g.Bounds(img.Bounds()))
g.Draw(res, img)
return res
}
func deviceSpecifics(devName string, width, height int) (*gift.GIFT, error) {
switch devName {
case "Streamdeck XL", "Streamdeck (original v2)", "Stream Deck MK.2":
return gift.New(
gift.Resize(width, height, gift.LanczosResampling),
gift.Rotate180(),
), nil
case "Streamdeck Mini":
return gift.New(
gift.Resize(width, height, gift.LanczosResampling),
gift.Rotate90(),
gift.FlipVertical(),
), nil
case "Stream Deck Original":
return gift.New(
gift.Resize(width, height, gift.LanczosResampling),
gift.Rotate180(),
), nil
default:
return nil, errors.New(fmt.Sprintf("Unsupported Device: %s", devName))
}
}
func getImageForButton(img image.Image, btnFormat string) ([]byte, error) {
var b bytes.Buffer
switch btnFormat {
case "JPEG":
jpeg.Encode(&b, img, &jpeg.Options{Quality: 100})
case "BMP":
bmp.Encode(&b, img)
default:
return nil, errors.New("Unknown button image format: " + btnFormat)
}
return b.Bytes(), nil
}
func getSolidColourImage(colour color.Color, btnSize int) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, btnSize, btnSize))
// colour := color.RGBA{red, green, blue, 0}
draw.Draw(img, img.Bounds(), image.NewUniform(colour), image.Point{0, 0}, draw.Src)
return img
}
func getImageFile(filename string) (image.Image, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
img, _, err := image.Decode(f)
if err != nil {
return nil, err
}
return img, nil
}