]> Cypherpunks repositories - gostls13.git/commitdiff
image: add image.Tiled type, the Go equivalent of Plan9's repl bit.
authorNigel Tao <nigeltao@golang.org>
Tue, 12 Oct 2010 02:44:11 +0000 (13:44 +1100)
committerNigel Tao <nigeltao@golang.org>
Tue, 12 Oct 2010 02:44:11 +0000 (13:44 +1100)
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
src/pkg/image/geom.go
src/pkg/image/names.go

index 3d8ee89b28ca7bd8599d82477b7a70ae3559b701..2f3139d69b4701c8e1577f54c7b119556813e709 100644 (file)
@@ -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
        }
index d031c37d1c8a378319a1b84bc0ee6c5663cf085b..5a7b0b8a87d80e6e316d95e6a039acbd793e31b6 100644 (file)
@@ -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
index 11f65259c9cccee958d6c394be682c3e0e0e6a1e..a8de71b7f7d7b3eb28f4bb7dfa6c67db8b8a7735 100644 (file)
@@ -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}
+}