diff --git a/cmd/streamdeck/display_exec.go b/cmd/streamdeck/display_exec.go index 33ee824..9c56dac 100644 --- a/cmd/streamdeck/display_exec.go +++ b/cmd/streamdeck/display_exec.go @@ -81,6 +81,8 @@ func (d displayElementExec) Display(idx int, attributes map[string]interface{}) return errors.Wrap(err, "Unable to get image from disk") } + bgi = autoSizeImage(bgi, sd.IconSize()) + draw.Draw(img, img.Bounds(), bgi, image.ZP, draw.Src) } diff --git a/cmd/streamdeck/display_image.go b/cmd/streamdeck/display_image.go index 1f22862..95c73ab 100644 --- a/cmd/streamdeck/display_image.go +++ b/cmd/streamdeck/display_image.go @@ -32,5 +32,7 @@ func (d displayElementImage) Display(idx int, attributes map[string]interface{}) return errors.Wrap(err, "Umable to decode image") } + img = autoSizeImage(img, sd.IconSize()) + return errors.Wrap(sd.FillImage(idx, img), "Unable to set image") } diff --git a/cmd/streamdeck/go.mod b/cmd/streamdeck/go.mod index 1593a83..11e4c3d 100644 --- a/cmd/streamdeck/go.mod +++ b/cmd/streamdeck/go.mod @@ -11,6 +11,7 @@ require ( github.com/fsnotify/fsnotify v1.4.7 github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect + github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/pkg/errors v0.8.1 github.com/sashko/go-uinput v0.0.0-20180923134002-15fcac7aa54a github.com/sirupsen/logrus v1.4.2 diff --git a/cmd/streamdeck/go.sum b/cmd/streamdeck/go.sum index 1dcb04b..a212240 100644 --- a/cmd/streamdeck/go.sum +++ b/cmd/streamdeck/go.sum @@ -16,6 +16,8 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/leekchan/gtf v0.0.0-20190214083521-5fba33c5b00b/go.mod h1:thNruaSwydMhkQ8dXzapABF9Sc1Tz08ZBcDdgott9RA= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/cmd/streamdeck/helpers.go b/cmd/streamdeck/helpers.go new file mode 100644 index 0000000..f0e5eb0 --- /dev/null +++ b/cmd/streamdeck/helpers.go @@ -0,0 +1,38 @@ +package main + +import ( + "image" + "image/color" + + "github.com/nfnt/resize" + "golang.org/x/image/draw" +) + +func autoSizeImage(img image.Image, size int) image.Image { + if img.Bounds().Max.X == size && img.Bounds().Max.Y == size { + // Image has perfect size: Nothing to change + return img + } + + // Scale down when required + img = resize.Thumbnail(uint(size), uint(size), img, resize.Lanczos2) + if img.Bounds().Max.X == size && img.Bounds().Max.Y == size { + // Image has perfect size now: Nothing to change + return img + } + + // Image is too small, need to pad it + var ( + dimg = image.NewNRGBA(image.Rect(0, 0, size, size)) + pt = image.Point{ + X: (size - img.Bounds().Max.X) / 2, + Y: (size - img.Bounds().Max.Y) / 2, + } + ) + // Draw black background + draw.Copy(dimg, image.Point{}, image.NewUniform(color.RGBA{0x0, 0x0, 0x0, 0xff}), dimg.Bounds(), draw.Src, nil) + // Copy in image + draw.Copy(dimg, pt, img, img.Bounds(), draw.Src, nil) + + return dimg +}