go-chart/image_writer.go

43 lines
912 B
Go
Raw Normal View History

2016-08-27 23:24:15 +02:00
package chart
import (
"bytes"
"errors"
"image"
"image/png"
)
2016-10-22 02:01:02 +02:00
// RGBACollector is a render target for a chart.
2016-08-27 23:24:15 +02:00
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)
}
2017-02-03 20:26:53 +01:00
return nil, errors.New("no valid sources for image data, cannot continue")
2016-08-27 23:24:15 +02:00
}