Skip to content

Commit

Permalink
add nilable option for decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Ray committed Aug 21, 2024
1 parent 6a359ba commit 5aaad40
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 5aaad40

Please sign in to comment.