From 78e6288748e0076269c3e65a479b6ba72be61f1d Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Sat, 27 Aug 2016 14:24:15 -0700 Subject: [PATCH] image collector --- image_writer.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 image_writer.go diff --git a/image_writer.go b/image_writer.go new file mode 100644 index 0000000..b4ddb7a --- /dev/null +++ b/image_writer.go @@ -0,0 +1,41 @@ +package chart + +import ( + "bytes" + "errors" + "image" + "image/png" +) + +type RGBACollector interface { + SetRGBA(i *image.RGBA) +} + +// ImageWriter is a special type of io.Writer that produces a final image. +type ImageWriter struct { + rgba *image.RGBA + contents *bytes.Buffer +} + +func (ir *ImageWriter) Write(buffer []byte) (int, error) { + if ir.contents == nil { + ir.contents = bytes.NewBuffer([]byte{}) + } + return ir.contents.Write(buffer) +} + +// SetRGBA sets a raw version of the image. +func (ir *ImageWriter) SetRGBA(i *image.RGBA) { + ir.rgba = i +} + +// Image returns an *image.Image for the result. +func (ir *ImageWriter) Image() (image.Image, error) { + if ir.rgba != nil { + return ir.rgba, nil + } + if ir.contents != nil && ir.contents.Len() > 0 { + return png.Decode(ir.contents) + } + return nil, errors.New("No valid sources for image data, cannot continue.") +}