From 5e735440ecd3ca997eb115809be51acdd0984075 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Sat, 12 Dec 2020 14:41:14 +0100 Subject: [PATCH] Added scaled ImageSource. --- ui/imagesource.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/ui/imagesource.go b/ui/imagesource.go index adab905..25aae8f 100644 --- a/ui/imagesource.go +++ b/ui/imagesource.go @@ -3,6 +3,7 @@ package ui import ( "image" + "github.com/nfnt/resize" "opslag.de/schobers/zntg" ) @@ -45,3 +46,25 @@ func (s ImageSourceResource) CreateImage() (image.Image, error) { } return value.(image.Image), nil } + +func ImageSourceFromResources(res Resources, name string) ImageSourceResource { + return ImageSourceResource{Resources: res, Name: name} +} + +func Scaled(source ImageSource, width, height int) ImageSource { + return scaledImageSource{ImageSource: source, width: width, height: height} +} + +type scaledImageSource struct { + ImageSource + + width, height int +} + +func (s scaledImageSource) CreateImage() (image.Image, error) { + im, err := s.ImageSource.CreateImage() + if err != nil { + return nil, err + } + return resize.Resize(uint(s.width), uint(s.height), im, resize.Bicubic), nil +}