DrawImage, LoadPNG

This commit is contained in:
Michael Fogleman 2016-02-19 13:49:03 -05:00
parent 207b76187a
commit d1da415ba6
3 changed files with 38 additions and 2 deletions

View File

@ -84,7 +84,7 @@ func (dc *Context) Height() int {
} }
func (dc *Context) WritePNG(path string) error { func (dc *Context) WritePNG(path string) error {
return writePNG(path, dc.im) return SavePNG(path, dc.im)
} }
func (dc *Context) SetLineWidth(lineWidth float64) { func (dc *Context) SetLineWidth(lineWidth float64) {
@ -303,6 +303,12 @@ func (dc *Context) DrawCircle(x, y, r float64) {
dc.DrawEllipticalArc(x, y, r, r, 0, 2*math.Pi) dc.DrawEllipticalArc(x, y, r, r, 0, 2*math.Pi)
} }
func (dc *Context) DrawImage(im image.Image, x, y int) {
p := image.Pt(x, y)
r := image.Rectangle{p, p.Add(im.Bounds().Size())}
draw.Draw(dc.im, r, im, image.ZP, draw.Src)
}
// Text Functions // Text Functions
func (dc *Context) SetFontFace(fontFace font.Face) { func (dc *Context) SetFontFace(fontFace font.Face) {

21
examples/tiling.go Normal file
View File

@ -0,0 +1,21 @@
package main
import "github.com/fogleman/gg"
func main() {
const NX = 4
const NY = 3
im, err := gg.LoadPNG("examples/gopher.png")
if err != nil {
panic(err)
}
w := im.Bounds().Size().X
h := im.Bounds().Size().Y
dc := gg.NewContext(w*NX, h*NY)
for y := 0; y < NY; y++ {
for x := 0; x < NX; x++ {
dc.DrawImage(im, x*w, y*h)
}
}
dc.WritePNG("out.png")
}

11
util.go
View File

@ -24,7 +24,16 @@ func Degrees(radians float64) float64 {
return radians * 180 / math.Pi return radians * 180 / math.Pi
} }
func writePNG(path string, im image.Image) error { func LoadPNG(path string) (image.Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return png.Decode(file)
}
func SavePNG(path string, im image.Image) error {
file, err := os.Create(path) file, err := os.Create(path)
if err != nil { if err != nil {
return err return err