2010-12-10 17:33:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"bufio"
|
|
|
|
"math"
|
|
|
|
"image"
|
|
|
|
"time"
|
|
|
|
"image/png"
|
2011-03-17 22:32:06 +00:00
|
|
|
"draw2d.googlecode.com/hg/draw2d"
|
2010-12-10 17:33:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func saveToPngFile(filePath string, m image.Image) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadFromPngFile(filePath string) image.Image {
|
2011-02-02 21:38:17 +00:00
|
|
|
f, err := os.Open(filePath, 0, 0)
|
2010-12-10 17:33:02 +00:00
|
|
|
if f == nil {
|
|
|
|
log.Printf("can't open file; err=%s\n", err.String())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
b := bufio.NewReader(f)
|
|
|
|
i, err := png.Decode(b)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Printf("Read %s OK.\n", filePath)
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-20 21:31:54 +00:00
|
|
|
func testBubble(gc draw2d.GraphicContext) {
|
2010-12-10 17:33:02 +00:00
|
|
|
gc.BeginPath()
|
|
|
|
gc.MoveTo(75, 25)
|
|
|
|
gc.QuadCurveTo(25, 25, 25, 62.5)
|
|
|
|
gc.QuadCurveTo(25, 100, 50, 100)
|
|
|
|
gc.QuadCurveTo(50, 120, 30, 125)
|
|
|
|
gc.QuadCurveTo(60, 120, 65, 100)
|
|
|
|
gc.QuadCurveTo(125, 100, 125, 62.5)
|
|
|
|
gc.QuadCurveTo(125, 25, 75, 25)
|
|
|
|
gc.Stroke()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
2011-03-17 22:32:06 +00:00
|
|
|
source := loadFromPngFile("../resource/image/Varna_Railway_Station_HDR.png")
|
2010-12-10 17:33:02 +00:00
|
|
|
i := image.NewRGBA(1024, 768)
|
2011-03-20 21:31:54 +00:00
|
|
|
gc := draw2d.NewImageGraphicContext(i)
|
2010-12-10 17:33:02 +00:00
|
|
|
gc.Scale(2, 0.5)
|
|
|
|
//gc.Translate(75, 25)
|
2011-02-02 21:38:17 +00:00
|
|
|
gc.Rotate(30 * math.Pi / 180)
|
2010-12-10 17:33:02 +00:00
|
|
|
lastTime := time.Nanoseconds()
|
|
|
|
gc.DrawImage(source)
|
|
|
|
dt := time.Nanoseconds() - lastTime
|
2011-02-02 21:38:17 +00:00
|
|
|
fmt.Printf("Draw image: %f ms\n", float64(dt)*1e-6)
|
2011-03-17 22:32:06 +00:00
|
|
|
saveToPngFile("../resource/result/TestDrawImage.png", i)
|
2010-12-10 17:33:02 +00:00
|
|
|
}
|