import (
"bytes"
"image"
"image/color"
"image/draw"
"image/png"
"log"
"math/rand"
)
func newImage() image.Image {
sz := 50
// create the whole image canvas
canvas := image.Rect(0, 0, 2*sz, 2*sz)
img := image.NewRGBA(canvas)
// draw a gray background
//bkg := color.RGBA{0x66, 0x66, 0x66, 0xff}
bkg := color.RGBA{102, 102, 102, 255}
draw.Draw(img, canvas, image.NewUniform(bkg), image.ZP, draw.Src)
// create a randomly sized, randomly centered, red square
x1 := rand.Intn(sz)
y1 := rand.Intn(sz)
x2 := rand.Intn(sz) + sz
y2 := rand.Intn(sz) + sz
// red foreground
red := color.RGBA{255,0,0,255}
draw.Draw(
img,
image.Rect(x1, y1, x2, y2),
image.NewUniform(red),
image.ZP, draw.Src,
)
return img
}
func getRaw(img image.Image) []byte {
buf := bytes.Buffer{}
err := png.Encode(&buf, img)
if err != nil {
log.Printf("could not encode to PNG: %v", err)
return nil
}
return buf.Bytes()
}