2010-12-13 08:59:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2010-12-13 14:54:47 +00:00
|
|
|
"time"
|
2010-12-13 08:59:26 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2011-03-15 17:27:27 +00:00
|
|
|
"io/ioutil"
|
2010-12-13 08:59:26 +00:00
|
|
|
"bufio"
|
2011-03-15 17:27:27 +00:00
|
|
|
"strings"
|
2010-12-13 08:59:26 +00:00
|
|
|
"image"
|
|
|
|
"image/png"
|
2011-03-17 22:32:06 +00:00
|
|
|
"draw2d.googlecode.com/hg/draw2d"
|
|
|
|
"draw2d.googlecode.com/hg/postscript"
|
2010-12-13 08:59:26 +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 main() {
|
2010-12-13 23:04:53 +00:00
|
|
|
i := image.NewRGBA(600, 800)
|
2011-03-20 21:31:54 +00:00
|
|
|
gc := draw2d.NewImageGraphicContext(i)
|
2010-12-13 08:59:26 +00:00
|
|
|
gc.Translate(0, 380)
|
2011-02-02 21:38:05 +00:00
|
|
|
gc.Scale(1, -1)
|
2010-12-13 08:59:26 +00:00
|
|
|
gc.Translate(0, -380)
|
2010-12-13 14:54:47 +00:00
|
|
|
lastTime := time.Nanoseconds()
|
2011-03-20 21:31:54 +00:00
|
|
|
src, err := os.Open("../resource/postscript/Mand.ps", 0, 0)
|
2011-03-15 17:27:27 +00:00
|
|
|
if err != nil {
|
2011-03-22 19:58:56 +00:00
|
|
|
return
|
2011-03-15 17:27:27 +00:00
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
bytes, err := ioutil.ReadAll(src)
|
|
|
|
reader := strings.NewReader(string(bytes))
|
2010-12-13 08:59:26 +00:00
|
|
|
interpreter := postscript.NewInterpreter(gc)
|
2011-03-15 17:27:27 +00:00
|
|
|
interpreter.Execute(reader)
|
2010-12-13 14:54:47 +00:00
|
|
|
dt := time.Nanoseconds() - lastTime
|
2011-02-02 21:38:05 +00:00
|
|
|
fmt.Printf("Draw image: %f ms\n", float64(dt)*1e-6)
|
2011-03-17 22:32:06 +00:00
|
|
|
saveToPngFile("../resource/result/TestPostscript.png", i)
|
2010-12-13 08:59:26 +00:00
|
|
|
}
|