draw: implement Copy; add an example_test.

Change-Id: Ia9cceac17c0326702530eac3a205308b02b85986
Reviewed-on: https://go-review.googlesource.com/7262
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Nigel Tao 2015-03-10 13:04:11 +11:00
parent 08593990c4
commit efa0c0977f
2 changed files with 79 additions and 1 deletions

66
draw/example_test.go Normal file
View File

@ -0,0 +1,66 @@
// 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.
package draw
import (
"fmt"
"image"
"image/png"
"log"
"math"
"os"
"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))
qs := []Interpolator{
NearestNeighbor,
ApproxBiLinear,
CatmullRom,
}
c, s := math.Cos(math.Pi/3), math.Sin(math.Pi/3)
t := &f64.Aff3{
+2 * c, -2 * s, 100,
+2 * s, +2 * c, 100,
}
Copy(dst, image.Point{20, 30}, src, sr, nil)
for i, q := range qs {
q.Scale(dst, image.Rect(200+10*i, 100*i, 600+10*i, 150+100*i), src, sr, nil)
}
// TODO: delete the "_ = t" and uncomment this when Transform is implemented.
// NearestNeighbor.Transform(dst, t, src, sr, nil)
_ = t
// 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).
}

View File

@ -13,6 +13,18 @@ import (
"golang.org/x/image/math/f64"
)
// Copy copies the part of the source image defined by src and sr and writes to
// the part of the destination image defined by dst and the translation of sr
// so that sr.Min translates to dp.
func Copy(dst Image, dp image.Point, src image.Image, sr image.Rectangle, opts *Options) {
mask, mp, op := image.Image(nil), image.Point{}, Over
if opts != nil {
// TODO: set mask, mp and op.
}
dr := sr.Add(dp.Sub(sr.Min))
DrawMask(dst, dr, src, sr.Min, mask, mp, op)
}
// Scaler scales the part of the source image defined by src and sr and writes
// to the part of the destination image defined by dst and dr.
//
@ -38,7 +50,7 @@ type Transformer interface {
Transform(dst Image, m *f64.Aff3, src image.Image, sr image.Rectangle, opts *Options)
}
// Options are optional parameters to Scale and Transform.
// Options are optional parameters to Copy, Scale and Transform.
//
// A nil *Options means to use the default (zero) values of each field.
type Options struct {