| 1 |
package chart |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"image" |
| 7 |
"image/png" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
type RGBACollector interface { |
| 12 |
SetRGBA(i *image.RGBA) |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
type ImageWriter struct { |
| 17 |
rgba *image.RGBA |
| 18 |
contents *bytes.Buffer |
| 19 |
} |
| 20 |
|
| 21 |
func (ir *ImageWriter) Write(buffer []byte) (int, error) { |
| 22 |
if ir.contents == nil { |
| 23 |
ir.contents = bytes.NewBuffer([]byte{}) |
| 24 |
} |
| 25 |
return ir.contents.Write(buffer) |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
func (ir *ImageWriter) SetRGBA(i *image.RGBA) { |
| 30 |
ir.rgba = i |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
func (ir *ImageWriter) Image() (image.Image, error) { |
| 35 |
if ir.rgba != nil { |
| 36 |
return ir.rgba, nil |
| 37 |
} |
| 38 |
if ir.contents != nil && ir.contents.Len() > 0 { |
| 39 |
return png.Decode(ir.contents) |
| 40 |
} |
| 41 |
return nil, errors.New("no valid sources for image data, cannot continue") |
| 42 |
} |