Added in-memory alternative to PNG renderer

This commit is contained in:
Erik Bunn 2022-06-27 14:06:42 +02:00
parent c1468e8ae4
commit d8cf23c038

View file

@ -23,6 +23,20 @@ func PNG(width, height int) (Renderer, error) {
return nil, err 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. // rasterRenderer renders chart commands to a bitmap.
type rasterRenderer struct { type rasterRenderer struct {
i *image.RGBA i *image.RGBA
@ -228,3 +242,15 @@ func (rr *rasterRenderer) Save(w io.Writer) error {
} }
return png.Encode(w, rr.i) 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
}