draw2d/cmd/testimage.go

72 lines
1.5 KiB
Go
Raw Normal View History

2010-12-10 17:33:02 +00:00
package main
import (
"fmt"
"log"
"os"
"bufio"
"math"
"image"
"time"
"image/png"
2011-04-14 12:59:35 +00:00
"exp/draw"
"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)
2011-04-14 10:17:11 +00:00
return
2010-12-10 17:33:02 +00:00
}
defer f.Close()
b := bufio.NewWriter(f)
err = png.Encode(b, m)
if err != nil {
log.Println(err)
2011-04-14 10:17:11 +00:00
return
2010-12-10 17:33:02 +00:00
}
err = b.Flush()
if err != nil {
log.Println(err)
2011-04-14 10:17:11 +00:00
return
2010-12-10 17:33:02 +00:00
}
fmt.Printf("Wrote %s OK.\n", filePath)
}
func loadFromPngFile(filePath string) image.Image {
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
}
func main() {
2011-04-14 10:17:11 +00:00
source := loadFromPngFile("../resource/image/TestAndroid.png")
2011-04-14 12:59:35 +00:00
dest := image.NewRGBA(1024, 768)
width, height := float64(source.Bounds().Dx()), float64(source.Bounds().Dy())
tr := draw2d.NewIdentityMatrix()
tr.Translate(width/2, height/2)
tr.Rotate(30 * math.Pi / 180)
2011-04-14 13:42:13 +00:00
//tr.Scale(3, 3)
2011-04-14 12:59:35 +00:00
tr.Translate(-width/2, -height/2)
2011-04-14 13:42:13 +00:00
tr.Translate(200, 5)
2010-12-10 17:33:02 +00:00
lastTime := time.Nanoseconds()
2011-04-14 12:59:35 +00:00
draw2d.DrawImage(source, dest, tr, draw.Over, draw2d.BilinearFilter)
2010-12-10 17:33:02 +00:00
dt := time.Nanoseconds() - lastTime
fmt.Printf("Draw image: %f ms\n", float64(dt)*1e-6)
2011-04-14 12:59:35 +00:00
saveToPngFile("../resource/result/TestDrawImage.png", dest)
2010-12-10 17:33:02 +00:00
}