draw2d/cmd/testpostscript.go

54 lines
1 KiB
Go
Raw Normal View History

2011-04-27 08:06:14 +00:00
package main
import (
2012-01-13 09:14:12 +00:00
"bufio"
"github.com/llgcode/draw2d/draw2d"
"github.com/llgcode/draw2d/postscript"
2011-04-27 08:06:14 +00:00
"fmt"
2012-01-13 09:14:12 +00:00
"image"
"image/png"
"io/ioutil"
2011-04-27 08:06:14 +00:00
"log"
"os"
"strings"
)
func saveToPngFile(filePath string, m image.Image) {
f, err := os.Create(filePath)
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() {
2011-09-29 07:46:16 +00:00
i := image.NewRGBA(image.Rect(0, 0, 600, 800))
2011-04-27 08:06:14 +00:00
gc := draw2d.NewGraphicContext(i)
gc.Translate(0, 380)
gc.Scale(1, -1)
gc.Translate(0, -380)
src, err := os.OpenFile("../resource/postscript/tiger.ps", 0, 0)
if err != nil {
return
}
defer src.Close()
bytes, err := ioutil.ReadAll(src)
reader := strings.NewReader(string(bytes))
interpreter := postscript.NewInterpreter(gc)
interpreter.Execute(reader)
saveToPngFile("../resource/result/TestPostscript.png", i)
}