Commit b5a480f0 authored by Nigel Tao's avatar Nigel Tao

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
parent fcc0c002
......@@ -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
}
......
......@@ -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
......
......@@ -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}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment