2015-03-10 03:04:11 +01:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-03-15 05:56:43 +01:00
|
|
|
package draw_test
|
2015-03-10 03:04:11 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/png"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2015-03-15 05:56:43 +01:00
|
|
|
"golang.org/x/image/draw"
|
2015-03-10 03:04:11 +01:00
|
|
|
"golang.org/x/image/math/f64"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleDraw() {
|
|
|
|
fSrc, err := os.Open("../testdata/blue-purple-pink.png")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fSrc.Close()
|
|
|
|
src, err := png.Decode(fSrc)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sr := src.Bounds()
|
|
|
|
dst := image.NewRGBA(image.Rect(0, 0, 400, 300))
|
2015-03-15 05:56:43 +01:00
|
|
|
qs := []draw.Interpolator{
|
|
|
|
draw.NearestNeighbor,
|
|
|
|
draw.ApproxBiLinear,
|
|
|
|
draw.CatmullRom,
|
2015-03-10 03:04:11 +01:00
|
|
|
}
|
2015-03-13 07:44:34 +01:00
|
|
|
const cos60, sin60 = 0.5, 0.866025404
|
2015-03-10 03:04:11 +01:00
|
|
|
t := &f64.Aff3{
|
2015-03-13 07:44:34 +01:00
|
|
|
+2 * cos60, -2 * sin60, 100,
|
|
|
|
+2 * sin60, +2 * cos60, 100,
|
2015-03-10 03:04:11 +01:00
|
|
|
}
|
|
|
|
|
2015-03-15 05:56:43 +01:00
|
|
|
draw.Copy(dst, image.Point{20, 30}, src, sr, nil)
|
2015-03-10 03:04:11 +01:00
|
|
|
for i, q := range qs {
|
|
|
|
q.Scale(dst, image.Rect(200+10*i, 100*i, 600+10*i, 150+100*i), src, sr, nil)
|
|
|
|
}
|
2015-03-13 07:44:34 +01:00
|
|
|
draw.NearestNeighbor.Transform(dst, t, src, sr, nil)
|
2015-03-10 03:04:11 +01:00
|
|
|
|
|
|
|
// Change false to true to write the resultant image to disk.
|
|
|
|
if false {
|
|
|
|
fDst, err := os.Create("out.png")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fDst.Close()
|
|
|
|
err = png.Encode(fDst, dst)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("dst has bounds %v.\n", dst.Bounds())
|
|
|
|
// Output:
|
|
|
|
// dst has bounds (0,0)-(400,300).
|
|
|
|
}
|