1
0
mirror of https://github.com/golang/go synced 2024-11-23 14:20:05 -07:00
go/doc/progs/image_draw.go
Josh Bleecher Snyder 5451fff475 doc/progs: rewrite test driver in Go
The tests in doc/progs appear to have been originally written
for use with the old test driver. At some later point,
they acquired their own test driver.
Both ran tests in serial.

This CL rewrites the current test driver in Go,
runs tests concurrently, and cleans up
historical artifacts from the old drivers.

The primary motivation is to speed up all.bash.
On my laptop, using tip, this CL reduces doc/progs test
wall time from 26s to 7s. The savings will remain
even when the compiler gets faster. Using Go 1.4,
this CL reduces test wall time from 15s to 4s.

Change-Id: Iae945a8490222beee76e8a2118a0d7956092f543
Reviewed-on: https://go-review.googlesource.com/8410
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-04-07 00:41:59 +00:00

143 lines
3.1 KiB
Go

// Copyright 2012 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.
// This file contains the code snippets included in "The Go image/draw package."
package main
import (
"image"
"image/color"
"image/draw"
)
func main() {
Color()
Rect()
RectAndScroll()
ConvAndCircle()
Glyph()
}
func Color() {
c := color.RGBA{255, 0, 255, 255}
r := image.Rect(0, 0, 640, 480)
dst := image.NewRGBA(r)
// ZERO OMIT
// image.ZP is the zero point -- the origin.
draw.Draw(dst, r, &image.Uniform{c}, image.ZP, draw.Src)
// STOP OMIT
// BLUE OMIT
m := image.NewRGBA(image.Rect(0, 0, 640, 480))
blue := color.RGBA{0, 0, 255, 255}
draw.Draw(m, m.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src)
// STOP OMIT
// RESET OMIT
draw.Draw(m, m.Bounds(), image.Transparent, image.ZP, draw.Src)
// STOP OMIT
}
func Rect() {
dst := image.NewRGBA(image.Rect(0, 0, 640, 480))
sr := image.Rect(0, 0, 200, 200)
src := image.Black
dp := image.Point{100, 100}
// RECT OMIT
r := image.Rectangle{dp, dp.Add(sr.Size())}
draw.Draw(dst, r, src, sr.Min, draw.Src)
// STOP OMIT
}
func RectAndScroll() {
dst := image.NewRGBA(image.Rect(0, 0, 640, 480))
sr := image.Rect(0, 0, 200, 200)
src := image.Black
dp := image.Point{100, 100}
// RECT2 OMIT
r := sr.Sub(sr.Min).Add(dp)
draw.Draw(dst, r, src, sr.Min, draw.Src)
// STOP OMIT
m := dst
// SCROLL OMIT
b := m.Bounds()
p := image.Pt(0, 20)
// Note that even though the second argument is b,
// the effective rectangle is smaller due to clipping.
draw.Draw(m, b, m, b.Min.Add(p), draw.Src)
dirtyRect := b.Intersect(image.Rect(b.Min.X, b.Max.Y-20, b.Max.X, b.Max.Y))
// STOP OMIT
_ = dirtyRect // noop
}
func ConvAndCircle() {
src := image.NewRGBA(image.Rect(0, 0, 640, 480))
dst := image.NewRGBA(image.Rect(0, 0, 640, 480))
// CONV OMIT
b := src.Bounds()
m := image.NewRGBA(b)
draw.Draw(m, b, src, b.Min, draw.Src)
// STOP OMIT
p := image.Point{100, 100}
r := 50
// CIRCLE2 OMIT
draw.DrawMask(dst, dst.Bounds(), src, image.ZP, &circle{p, r}, image.ZP, draw.Over)
// STOP OMIT
}
func theGlyphImageForAFont() image.Image {
return image.NewRGBA(image.Rect(0, 0, 640, 480))
}
func theBoundsFor(index int) image.Rectangle {
return image.Rect(0, 0, 32, 32)
}
func Glyph() {
p := image.Point{100, 100}
dst := image.NewRGBA(image.Rect(0, 0, 640, 480))
glyphIndex := 42
// GLYPH OMIT
src := &image.Uniform{color.RGBA{0, 0, 255, 255}}
mask := theGlyphImageForAFont()
mr := theBoundsFor(glyphIndex)
draw.DrawMask(dst, mr.Sub(mr.Min).Add(p), src, image.ZP, mask, mr.Min, draw.Over)
// STOP OMIT
}
//CIRCLESTRUCT OMIT
type circle struct {
p image.Point
r int
}
func (c *circle) ColorModel() color.Model {
return color.AlphaModel
}
func (c *circle) Bounds() image.Rectangle {
return image.Rect(c.p.X-c.r, c.p.Y-c.r, c.p.X+c.r, c.p.Y+c.r)
}
func (c *circle) At(x, y int) color.Color {
xx, yy, rr := float64(x-c.p.X)+0.5, float64(y-c.p.Y)+0.5, float64(c.r)
if xx*xx+yy*yy < rr*rr {
return color.Alpha{255}
}
return color.Alpha{0}
}
//STOP OMIT