package ui import ( "image" "github.com/nfnt/resize" "opslag.de/schobers/zntg" ) type ImageSource interface { CreateImage() (image.Image, error) } type ImageSourceFile string var _ ImageSource = ImageSourceFile("") func (s ImageSourceFile) CreateImage() (image.Image, error) { return zntg.DecodeImage(string(s)) } type ImageSourceGo struct { image.Image } var _ ImageSource = ImageSourceGo{} func (s ImageSourceGo) CreateImage() (image.Image, error) { return s.Image, nil } type ImageSourceResource struct { Resources Resources Name string } func (s ImageSourceResource) CreateImage() (image.Image, error) { src, err := s.Resources.OpenResource(s.Name) if err != nil { return nil, err } defer src.Close() value, err := zntg.ImageDecoder(src) if err != nil { return nil, err } 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 }