2010-12-06 11:44:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"bufio"
|
|
|
|
"time"
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"image"
|
|
|
|
"image/png"
|
2011-03-17 22:32:06 +00:00
|
|
|
"draw2d.googlecode.com/hg/draw2d"
|
2010-12-06 11:44:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2010-12-06 13:57:26 +00:00
|
|
|
width, height = 300, 200
|
2010-12-06 11:44:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
lastTime int64
|
2011-03-17 22:32:06 +00:00
|
|
|
folder = "../resource/result/"
|
2010-12-06 11:44:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func initGc(w, h int) (image.Image, *draw2d.GraphicContext) {
|
|
|
|
i := image.NewRGBA(w, h)
|
|
|
|
gc := draw2d.NewGraphicContext(i)
|
|
|
|
lastTime = time.Nanoseconds()
|
|
|
|
|
|
|
|
gc.SetStrokeColor(image.Black)
|
|
|
|
gc.SetFillColor(image.White)
|
|
|
|
// fill the background
|
|
|
|
//gc.Clear()
|
|
|
|
|
|
|
|
return i, gc
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveToPngFile(TestName string, m image.Image) {
|
|
|
|
dt := time.Nanoseconds() - lastTime
|
2011-02-02 21:38:17 +00:00
|
|
|
fmt.Printf("%s during: %f ms\n", TestName, float64(dt)*10e-6)
|
2010-12-06 11:44:36 +00:00
|
|
|
filePath := folder + TestName + ".png"
|
|
|
|
f, err := os.Open(filePath, os.O_CREAT|os.O_WRONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
b := bufio.NewWriter(f)
|
|
|
|
err = png.Encode(b, m)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
err = b.Flush()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Printf("Wrote %s OK.\n", filePath)
|
|
|
|
}
|
|
|
|
|
2011-02-02 21:38:17 +00:00
|
|
|
func gordon(gc *draw2d.GraphicContext, x, y, w, h float64) {
|
2010-12-06 11:44:36 +00:00
|
|
|
h23 := (h * 2) / 3
|
|
|
|
|
2010-12-06 17:18:39 +00:00
|
|
|
blf := image.RGBAColor{0, 0, 0, 0xff}
|
2010-12-06 11:44:36 +00:00
|
|
|
wf := image.RGBAColor{0xff, 0xff, 0xff, 0xff}
|
|
|
|
nf := image.RGBAColor{0x8B, 0x45, 0x13, 0xff}
|
|
|
|
brf := image.RGBAColor{0x8B, 0x45, 0x13, 0x99}
|
|
|
|
brb := image.RGBAColor{0x8B, 0x45, 0x13, 0xBB}
|
2010-12-06 17:18:39 +00:00
|
|
|
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.MoveTo(x, y+h)
|
|
|
|
gc.CubicCurveTo(x, y+h, x+w/2, y-h, x+w, y+h)
|
|
|
|
gc.Close()
|
|
|
|
gc.SetFillColor(brb)
|
|
|
|
gc.Fill()
|
2010-12-06 17:18:39 +00:00
|
|
|
draw2d.RoundRect(gc, x, y+h, x+w, y+h+h, 10, 10)
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.Fill()
|
2010-12-06 17:18:39 +00:00
|
|
|
draw2d.Circle(gc, x, y+h, w/12) // left ear
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.SetFillColor(brf)
|
|
|
|
gc.Fill()
|
2010-12-06 12:11:57 +00:00
|
|
|
draw2d.Circle(gc, x, y+h, w/12-10)
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.SetFillColor(nf)
|
|
|
|
gc.Fill()
|
2010-12-06 17:18:39 +00:00
|
|
|
|
2010-12-06 12:11:57 +00:00
|
|
|
draw2d.Circle(gc, x+w, y+h, w/12) // right ear
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.SetFillColor(brf)
|
|
|
|
gc.Fill()
|
2010-12-06 12:11:57 +00:00
|
|
|
draw2d.Circle(gc, x+w, y+h, w/12-10)
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.SetFillColor(nf)
|
|
|
|
gc.Fill()
|
|
|
|
|
2010-12-06 12:11:57 +00:00
|
|
|
draw2d.Circle(gc, x+w/3, y+h23, w/9) // left eye
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.SetFillColor(wf)
|
|
|
|
gc.Fill()
|
2010-12-06 17:18:39 +00:00
|
|
|
draw2d.Circle(gc, x+w/3+10, y+h23, w/10-10)
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.SetFillColor(blf)
|
|
|
|
gc.Fill()
|
2010-12-06 12:11:57 +00:00
|
|
|
draw2d.Circle(gc, x+w/3+15, y+h23, 5)
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.SetFillColor(wf)
|
|
|
|
gc.Fill()
|
|
|
|
|
2010-12-06 12:11:57 +00:00
|
|
|
draw2d.Circle(gc, x+w-w/3, y+h23, w/9) // right eye
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.Fill()
|
2010-12-06 17:18:39 +00:00
|
|
|
draw2d.Circle(gc, x+w-w/3+10, y+h23, w/10-10)
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.SetFillColor(blf)
|
|
|
|
gc.Fill()
|
2010-12-06 12:11:57 +00:00
|
|
|
draw2d.Circle(gc, x+w-(w/3)+15, y+h23, 5)
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.SetFillColor(wf)
|
|
|
|
gc.Fill()
|
|
|
|
|
|
|
|
gc.SetFillColor(wf)
|
2010-12-06 17:18:39 +00:00
|
|
|
draw2d.RoundRect(gc, x+w/2-w/8, y+h+30, x+w/2-w/8+w/8, y+h+30+w/6, 5, 5) // left tooth
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.Fill()
|
2010-12-06 17:18:39 +00:00
|
|
|
draw2d.RoundRect(gc, x+w/2, y+h+30, x+w/2+w/8, y+h+30+w/6, 5, 5) // right tooth
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.Fill()
|
|
|
|
|
2010-12-06 17:18:39 +00:00
|
|
|
draw2d.Ellipse(gc, x+(w/2), y+h+30, w/6, w/12) // snout
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.SetFillColor(nf)
|
|
|
|
gc.Fill()
|
2010-12-06 12:11:57 +00:00
|
|
|
draw2d.Ellipse(gc, x+(w/2), y+h+10, w/10, w/12) // nose
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.SetFillColor(blf)
|
|
|
|
gc.Fill()
|
2010-12-06 17:18:39 +00:00
|
|
|
|
2010-12-06 11:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
i, gc := initGc(width, height)
|
|
|
|
gc.Clear()
|
2010-12-06 13:57:26 +00:00
|
|
|
gc.Translate(-75, 58)
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.Rotate(-30 * (math.Pi / 180.0))
|
|
|
|
gordon(gc, 48, 48, 240, 72)
|
|
|
|
saveToPngFile("TestGopher", i)
|
2010-12-06 17:18:39 +00:00
|
|
|
}
|