From 5aaad40422d06da5d5c1c00daaae5bfb0ecfb45d Mon Sep 17 00:00:00 2001 From: Allen Ray Date: Wed, 21 Aug 2024 11:20:18 -0400 Subject: [PATCH] add nilable option for decoder --- data.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/data.go b/data.go index 4f0ab90..8561fb3 100644 --- a/data.go +++ b/data.go @@ -181,6 +181,11 @@ func verticalFlip(rgba *image.RGBA) { } } +func defaultDecoder(r io.Reader) (image.Image, error) { + i, _, err := image.Decode(r) + return i, err +} + // PictureDataFromFile loads an image from a file using the given decoder and converts it into PictureData. // // We take a decoder function (png.Decode, jpeg.Decode, etc.) as an argument; in order to decode images, @@ -191,6 +196,9 @@ func verticalFlip(rgba *image.RGBA) { // With this argument, you implicitly import and register the file formats you need and the Pixel project // doesn't have to carry all formats around. // +// The decoder can be nil, and Pixel will fallback onto using image.Decode and require you to import the +// formats you wish to use. +// // See the example https://github.com/gopxl/pixel-examples/tree/main/core/loadingpictures. func PictureDataFromFile(path string, decoder func(r io.Reader) (image.Image, error)) (*PictureData, error) { f, err := os.Open(path) @@ -199,6 +207,10 @@ func PictureDataFromFile(path string, decoder func(r io.Reader) (image.Image, er } defer f.Close() + if decoder == nil { + decoder = defaultDecoder + } + img, err := decoder(f) if err != nil { return nil, err