From b5a480f035e546c31680596bb51b9148de85dbad Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Tue, 12 Oct 2010 13:44:11 +1100 Subject: [PATCH] image: add image.Tiled type, the Go equivalent of Plan9's repl bit. Make ColorImage methods' receiver type be a pointer. R=r, rsc CC=golang-dev https://golang.org/cl/2345043 --- src/pkg/exp/draw/draw.go | 12 ++++++------ src/pkg/image/geom.go | 18 ++++++++++++++++++ src/pkg/image/names.go | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/pkg/exp/draw/draw.go b/src/pkg/exp/draw/draw.go index 3d8ee89b28..2f3139d69b 100644 --- a/src/pkg/exp/draw/draw.go +++ b/src/pkg/exp/draw/draw.go @@ -65,7 +65,7 @@ func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mas if dst0, ok := dst.(*image.RGBA); ok { if op == Over { if mask == nil { - if src0, ok := src.(image.ColorImage); ok { + if src0, ok := src.(*image.ColorImage); ok { drawFillOver(dst0, r, src0) return } @@ -74,14 +74,14 @@ func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mas return } } else if mask0, ok := mask.(*image.Alpha); ok { - if src0, ok := src.(image.ColorImage); ok { + if src0, ok := src.(*image.ColorImage); ok { drawGlyphOver(dst0, r, src0, mask0, mp) return } } } else { if mask == nil { - if src0, ok := src.(image.ColorImage); ok { + if src0, ok := src.(*image.ColorImage); ok { drawFillSrc(dst0, r, src0) return } @@ -149,7 +149,7 @@ func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mas } } -func drawFillOver(dst *image.RGBA, r image.Rectangle, src image.ColorImage) { +func drawFillOver(dst *image.RGBA, r image.Rectangle, src *image.ColorImage) { cr, cg, cb, ca := src.RGBA() // The 0x101 is here for the same reason as in drawRGBA. a := (m - ca) * 0x101 @@ -224,7 +224,7 @@ func drawCopyOver(dst *image.RGBA, r image.Rectangle, src *image.RGBA, sp image. } } -func drawGlyphOver(dst *image.RGBA, r image.Rectangle, src image.ColorImage, mask *image.Alpha, mp image.Point) { +func drawGlyphOver(dst *image.RGBA, r image.Rectangle, src *image.ColorImage, mask *image.Alpha, mp image.Point) { x0, x1 := r.Min.X, r.Max.X y0, y1 := r.Min.Y, r.Max.Y cr, cg, cb, ca := src.RGBA() @@ -254,7 +254,7 @@ func drawGlyphOver(dst *image.RGBA, r image.Rectangle, src image.ColorImage, mas } } -func drawFillSrc(dst *image.RGBA, r image.Rectangle, src image.ColorImage) { +func drawFillSrc(dst *image.RGBA, r image.Rectangle, src *image.ColorImage) { if r.Dy() < 1 { return } diff --git a/src/pkg/image/geom.go b/src/pkg/image/geom.go index d031c37d1c..5a7b0b8a87 100644 --- a/src/pkg/image/geom.go +++ b/src/pkg/image/geom.go @@ -38,6 +38,24 @@ func (p Point) Div(k int) Point { return Point{p.X / k, p.Y / k} } +// Mod returns the point q in r such that p.X-q.X is a multiple of r's width +// and p.Y-q.Y is a multiple of r's height. +func (p Point) Mod(r Rectangle) Point { + w, h := r.Dx(), r.Dy() + p = p.Sub(r.Min) + if p.X >= 0 { + p.X = p.X % w + } else { + p.X = w - 1 - (-1-p.X)%w + } + if p.Y >= 0 { + p.Y = p.Y % h + } else { + p.Y = h - 1 - (-1-p.Y)%h + } + return p.Add(r.Min) +} + // Eq returns whether p and q are equal. func (p Point) Eq(q Point) bool { return p.X == q.X && p.Y == q.Y diff --git a/src/pkg/image/names.go b/src/pkg/image/names.go index 11f65259c9..a8de71b7f7 100644 --- a/src/pkg/image/names.go +++ b/src/pkg/image/names.go @@ -21,20 +21,46 @@ type ColorImage struct { C Color } -func (c ColorImage) RGBA() (r, g, b, a uint32) { +func (c *ColorImage) RGBA() (r, g, b, a uint32) { return c.C.RGBA() } -func (c ColorImage) ColorModel() ColorModel { +func (c *ColorImage) ColorModel() ColorModel { return ColorModelFunc(func(Color) Color { return c.C }) } -func (c ColorImage) Bounds() Rectangle { return Rectangle{Point{-1e9, -1e9}, Point{1e9, 1e9}} } +func (c *ColorImage) Bounds() Rectangle { return Rectangle{Point{-1e9, -1e9}, Point{1e9, 1e9}} } -func (c ColorImage) At(x, y int) Color { return c.C } +func (c *ColorImage) At(x, y int) Color { return c.C } // Opaque scans the entire image and returns whether or not it is fully opaque. -func (c ColorImage) Opaque() bool { +func (c *ColorImage) Opaque() bool { _, _, _, a := c.C.RGBA() return a == 0xffff } + +func NewColorImage(c Color) *ColorImage { + return &ColorImage{c} +} + +// A Tiled is a practically infinite-sized Image that repeats another Image in +// both directions. Tiled{i}.At(x, y) will equal i.At(x, y) for all points +// within i's Bounds. +type Tiled struct { + I Image +} + +func (t *Tiled) ColorModel() ColorModel { + return t.I.ColorModel() +} + +func (t *Tiled) Bounds() Rectangle { return Rectangle{Point{-1e9, -1e9}, Point{1e9, 1e9}} } + +func (t *Tiled) At(x, y int) Color { + p := Point{x, y}.Mod(t.I.Bounds()) + return t.I.At(p.X, p.Y) +} + +func NewTiled(i Image) *Tiled { + return &Tiled{i} +} -- 2.48.1