zntg/ui/imagesource.go

71 lines
1.4 KiB
Go
Raw Normal View History

package ui
import (
"image"
2020-05-15 12:20:07 +00:00
2020-12-12 13:41:14 +00:00
"github.com/nfnt/resize"
2020-05-15 12:20:07 +00:00
"opslag.de/schobers/zntg"
)
type ImageSource interface {
CreateImage() (image.Image, error)
}
2020-05-15 12:20:07 +00:00
type ImageSourceFile string
2020-05-15 12:20:07 +00:00
var _ ImageSource = ImageSourceFile("")
2020-05-15 12:20:07 +00:00
func (s ImageSourceFile) CreateImage() (image.Image, error) {
return zntg.DecodeImage(string(s))
}
2020-05-15 12:20:07 +00:00
type ImageSourceGo struct {
image.Image
}
2020-05-15 12:20:07 +00:00
var _ ImageSource = ImageSourceGo{}
2020-05-15 12:20:07 +00:00
func (s ImageSourceGo) CreateImage() (image.Image, error) {
return s.Image, nil
}
2020-05-15 12:20:07 +00:00
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
}
2020-12-12 13:41:14 +00:00
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
}