From 9d566815605a680cd5c5ab6f2ba3abe923587b92 Mon Sep 17 00:00:00 2001 From: Michael Fogleman Date: Thu, 25 Feb 2016 15:06:04 -0500 Subject: [PATCH] LoadImage --- README.md | 1 + examples/ellipse.go | 2 +- util.go | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dc93813..aabdd0b 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,7 @@ Sometimes you just don't want to write these yourself. ```go Radians(degrees float64) float64 Degrees(radians float64) float64 +LoadImage(path string) (image.Image, error) LoadPNG(path string) (image.Image, error) SavePNG(path string, im image.Image) error ``` diff --git a/examples/ellipse.go b/examples/ellipse.go index bb46543..e0de9f4 100644 --- a/examples/ellipse.go +++ b/examples/ellipse.go @@ -13,7 +13,7 @@ func main() { dc.Fill() dc.Pop() } - if im, err := gg.LoadPNG("examples/gopher.png"); err == nil { + if im, err := gg.LoadImage("examples/gopher.png"); err == nil { dc.DrawImageAnchored(im, S/2, S/2, 0.5, 0.5) } dc.SavePNG("out.png") diff --git a/util.go b/util.go index 4f1b6c0..62577f5 100644 --- a/util.go +++ b/util.go @@ -24,6 +24,16 @@ func Degrees(radians float64) float64 { return radians * 180 / math.Pi } +func LoadImage(path string) (image.Image, error) { + file, err := os.Open(path) + if err != nil { + return nil, err + } + defer file.Close() + im, _, err := image.Decode(file) + return im, err +} + func LoadPNG(path string) (image.Image, error) { file, err := os.Open(path) if err != nil {