From d8cf23c038ed90047bf2aa90c144cd8b0cb51a13 Mon Sep 17 00:00:00 2001 From: Erik Bunn Date: Mon, 27 Jun 2022 14:06:42 +0200 Subject: [PATCH] Added in-memory alternative to PNG renderer --- raster_renderer.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/raster_renderer.go b/raster_renderer.go index 4de2655..e3bc22c 100644 --- a/raster_renderer.go +++ b/raster_renderer.go @@ -23,6 +23,20 @@ func PNG(width, height int) (Renderer, error) { return nil, err } +func InMemory(width, height int) (Renderer, error) { + i := image.NewRGBA(image.Rect(0, 0, width, height)) + gc, err := drawing.NewRasterGraphicContext(i) + if err == nil { + return &memRenderer{ + rasterRenderer{ + i: i, + gc: gc, + }, + }, nil + } + return nil, err +} + // rasterRenderer renders chart commands to a bitmap. type rasterRenderer struct { i *image.RGBA @@ -228,3 +242,15 @@ func (rr *rasterRenderer) Save(w io.Writer) error { } return png.Encode(w, rr.i) } + +type memRenderer struct { + rasterRenderer +} + +func (m *memRenderer) Save(w io.Writer) error { + return nil +} + +func (m *memRenderer) AsImage() image.Image { + return m.i +}