zntg/ui/imagesource.go

48 lines
821 B
Go
Raw Normal View History

package ui
import (
"image"
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
}