Commit 4e2035bd authored by Nigel Tao's avatar Nigel Tao

In draw.Draw, separate the source-point and mask-point.

This lets you draw text (i.e. with mask = a font image) with
sources that aren't uniform colors.

R=r, rsc
CC=golang-dev
https://golang.org/cl/193067
parent f2317d3e
...@@ -226,8 +226,8 @@ func collider(pt, pmax draw.Point) bool { ...@@ -226,8 +226,8 @@ func collider(pt, pmax draw.Point) bool {
} }
func setpiece(p *Piece) { func setpiece(p *Piece) {
draw.Draw(bb, bbr, draw.White, nil, draw.ZP) draw.Draw(bb, bbr, draw.White, draw.ZP)
draw.Draw(bbmask, bbr, draw.Transparent, nil, draw.ZP) draw.Draw(bbmask, bbr, draw.Transparent, draw.ZP)
br = draw.Rect(0, 0, 0, 0) br = draw.Rect(0, 0, 0, 0)
br2 = br br2 = br
piece = p piece = p
...@@ -243,13 +243,13 @@ func setpiece(p *Piece) { ...@@ -243,13 +243,13 @@ func setpiece(p *Piece) {
r.Max.X = r.Min.X + pcsz r.Max.X = r.Min.X + pcsz
r.Max.Y = r.Min.Y + pcsz r.Max.Y = r.Min.Y + pcsz
if i == 0 { if i == 0 {
draw.Draw(bb, r, draw.Black, nil, draw.ZP) draw.Draw(bb, r, draw.Black, draw.ZP)
draw.Draw(bb, r.Inset(1), txpix[piece.tx], nil, draw.ZP) draw.Draw(bb, r.Inset(1), txpix[piece.tx], draw.ZP)
draw.Draw(bbmask, r, draw.Opaque, nil, draw.ZP) draw.Draw(bbmask, r, draw.Opaque, draw.ZP)
op = r.Min op = r.Min
} else { } else {
draw.Draw(bb, r, bb, nil, op) draw.Draw(bb, r, bb, op)
draw.Draw(bbmask, r, bbmask, nil, op) draw.Draw(bbmask, r, bbmask, op)
} }
if br.Max.X < r.Max.X { if br.Max.X < r.Max.X {
br.Max.X = r.Max.X br.Max.X = r.Max.X
...@@ -263,17 +263,17 @@ func setpiece(p *Piece) { ...@@ -263,17 +263,17 @@ func setpiece(p *Piece) {
br2.Max = br.Max.Add(delta) br2.Max = br.Max.Add(delta)
r = br.Add(bb2r.Min) r = br.Add(bb2r.Min)
r2 := br2.Add(bb2r.Min) r2 := br2.Add(bb2r.Min)
draw.Draw(bb2, r2, draw.White, nil, draw.ZP) draw.Draw(bb2, r2, draw.White, draw.ZP)
draw.Draw(bb2, r.Add(delta), bb, nil, bbr.Min) draw.Draw(bb2, r.Add(delta), bb, bbr.Min)
draw.Draw(bb2mask, r2, draw.Transparent, nil, draw.ZP) draw.Draw(bb2mask, r2, draw.Transparent, draw.ZP)
draw.Draw(bb2mask, r, draw.Opaque, bbmask, bbr.Min) draw.DrawMask(bb2mask, r, draw.Opaque, bbr.Min, bbmask, draw.ZP, draw.SoverD)
draw.Draw(bb2mask, r.Add(delta), draw.Opaque, bbmask, bbr.Min) draw.DrawMask(bb2mask, r.Add(delta), draw.Opaque, bbr.Min, bbmask, draw.ZP, draw.SoverD)
} }
func drawpiece() { func drawpiece() {
draw.Draw(screen, br.Add(pos), bb, bbmask, bbr.Min) draw.DrawMask(screen, br.Add(pos), bb, bbr.Min, bbmask, draw.ZP, draw.SoverD)
if suspended { if suspended {
draw.Draw(screen, br.Add(pos), draw.White, whitemask, draw.ZP) draw.DrawMask(screen, br.Add(pos), draw.White, draw.ZP, whitemask, draw.ZP, draw.SoverD)
} }
} }
...@@ -282,7 +282,7 @@ func undrawpiece() { ...@@ -282,7 +282,7 @@ func undrawpiece() {
if collider(pos, br.Max) { if collider(pos, br.Max) {
mask = bbmask mask = bbmask
} }
draw.Draw(screen, br.Add(pos), draw.White, mask, bbr.Min) draw.DrawMask(screen, br.Add(pos), draw.White, bbr.Min, mask, bbr.Min, draw.SoverD)
} }
func rest() { func rest() {
...@@ -323,7 +323,7 @@ func canfit(p *Piece) bool { ...@@ -323,7 +323,7 @@ func canfit(p *Piece) bool {
func score(p int) { func score(p int) {
points += p points += p
// snprint(buf, sizeof(buf), "%.6ld", points); // snprint(buf, sizeof(buf), "%.6ld", points);
// draw.Draw(screen, draw.Rpt(pscore, pscore.Add(scoresz)), draw.White, nil, draw.ZP); // draw.Draw(screen, draw.Rpt(pscore, pscore.Add(scoresz)), draw.White, draw.ZP);
// string(screen, pscore, draw.Black, draw.ZP, font, buf); // string(screen, pscore, draw.Black, draw.ZP, font, buf);
} }
...@@ -332,14 +332,14 @@ func drawsq(b draw.Image, p draw.Point, ptx int) { ...@@ -332,14 +332,14 @@ func drawsq(b draw.Image, p draw.Point, ptx int) {
r.Min = p r.Min = p
r.Max.X = r.Min.X + pcsz r.Max.X = r.Min.X + pcsz
r.Max.Y = r.Min.Y + pcsz r.Max.Y = r.Min.Y + pcsz
draw.Draw(b, r, draw.Black, nil, draw.ZP) draw.Draw(b, r, draw.Black, draw.ZP)
draw.Draw(b, r.Inset(1), txpix[ptx], nil, draw.ZP) draw.Draw(b, r.Inset(1), txpix[ptx], draw.ZP)
} }
func drawboard() { func drawboard() {
draw.Border(screen, rboard.Inset(-2), 2, draw.Black, draw.ZP) draw.Border(screen, rboard.Inset(-2), 2, draw.Black, draw.ZP)
draw.Draw(screen, draw.Rect(rboard.Min.X, rboard.Min.Y-2, rboard.Max.X, rboard.Min.Y), draw.Draw(screen, draw.Rect(rboard.Min.X, rboard.Min.Y-2, rboard.Max.X, rboard.Min.Y),
draw.White, nil, draw.ZP) draw.White, draw.ZP)
for i := 0; i < NY; i++ { for i := 0; i < NY; i++ {
for j := 0; j < NX; j++ { for j := 0; j < NX; j++ {
if board[i][j] != 0 { if board[i][j] != 0 {
...@@ -349,7 +349,7 @@ func drawboard() { ...@@ -349,7 +349,7 @@ func drawboard() {
} }
score(0) score(0)
if suspended { if suspended {
draw.Draw(screen, screenr, draw.White, whitemask, draw.ZP) draw.DrawMask(screen, screenr, draw.White, draw.ZP, whitemask, draw.ZP, draw.SoverD)
} }
} }
...@@ -375,7 +375,7 @@ func movepiece() bool { ...@@ -375,7 +375,7 @@ func movepiece() bool {
if collider(pos, br2.Max) { if collider(pos, br2.Max) {
mask = bb2mask mask = bb2mask
} }
draw.Draw(screen, br2.Add(pos), bb2, mask, bb2r.Min) draw.DrawMask(screen, br2.Add(pos), bb2, bb2r.Min, mask, bb2r.Min, draw.SoverD)
pos.Y += DY pos.Y += DY
display.FlushImage() display.FlushImage()
return true return true
...@@ -444,7 +444,7 @@ func horiz() bool { ...@@ -444,7 +444,7 @@ func horiz() bool {
for j := 0; j < h; j++ { for j := 0; j < h; j++ {
r.Min.Y = rboard.Min.Y + lev[j]*pcsz r.Min.Y = rboard.Min.Y + lev[j]*pcsz
r.Max.Y = r.Min.Y + pcsz r.Max.Y = r.Min.Y + pcsz
draw.Draw(screen, r, draw.White, whitemask, draw.ZP) draw.DrawMask(screen, r, draw.White, draw.ZP, whitemask, draw.ZP, draw.SoverD)
display.FlushImage() display.FlushImage()
} }
PlaySound(whoosh) PlaySound(whoosh)
...@@ -457,7 +457,7 @@ func horiz() bool { ...@@ -457,7 +457,7 @@ func horiz() bool {
for j := 0; j < h; j++ { for j := 0; j < h; j++ {
r.Min.Y = rboard.Min.Y + lev[j]*pcsz r.Min.Y = rboard.Min.Y + lev[j]*pcsz
r.Max.Y = r.Min.Y + pcsz r.Max.Y = r.Min.Y + pcsz
draw.Draw(screen, r, draw.White, whitemask, draw.ZP) draw.DrawMask(screen, r, draw.White, draw.ZP, whitemask, draw.ZP, draw.SoverD)
} }
display.FlushImage() display.FlushImage()
} }
...@@ -467,9 +467,9 @@ func horiz() bool { ...@@ -467,9 +467,9 @@ func horiz() bool {
score(250 + 10*i*i) score(250 + 10*i*i)
r.Min.Y = rboard.Min.Y r.Min.Y = rboard.Min.Y
r.Max.Y = rboard.Min.Y + lev[j]*pcsz r.Max.Y = rboard.Min.Y + lev[j]*pcsz
draw.Draw(screen, r.Add(draw.Pt(0, pcsz)), screen, nil, r.Min) draw.Draw(screen, r.Add(draw.Pt(0, pcsz)), screen, r.Min)
r.Max.Y = rboard.Min.Y + pcsz r.Max.Y = rboard.Min.Y + pcsz
draw.Draw(screen, r, draw.White, nil, draw.ZP) draw.Draw(screen, r, draw.White, draw.ZP)
for k := lev[j] - 1; k >= 0; k-- { for k := lev[j] - 1; k >= 0; k-- {
board[k+1] = board[k] board[k+1] = board[k]
} }
...@@ -703,7 +703,7 @@ func redraw(new bool) { ...@@ -703,7 +703,7 @@ func redraw(new bool) {
bb2r = draw.Rect(0, 0, N*pcsz, N*pcsz+DY) bb2r = draw.Rect(0, 0, N*pcsz, N*pcsz+DY)
bb2 = image.NewRGBA(bb2r.Dx(), bb2r.Dy()) bb2 = image.NewRGBA(bb2r.Dx(), bb2r.Dy())
bb2mask = image.NewRGBA(bb2r.Dx(), bb2r.Dy()) // actually just a bitmap bb2mask = image.NewRGBA(bb2r.Dx(), bb2r.Dy()) // actually just a bitmap
draw.Draw(screen, screenr, draw.White, nil, draw.ZP) draw.Draw(screen, screenr, draw.White, draw.ZP)
drawboard() drawboard()
setpiece(piece) setpiece(piece)
if piece != nil { if piece != nil {
......
...@@ -12,32 +12,37 @@ package draw ...@@ -12,32 +12,37 @@ package draw
import "image" import "image"
// A Porter-Duff compositing operator.
type Op int
const SoverD Op = 0
// A draw.Image is an image.Image with a Set method to change a single pixel. // A draw.Image is an image.Image with a Set method to change a single pixel.
type Image interface { type Image interface {
image.Image image.Image
Set(x, y int, c image.Color) Set(x, y int, c image.Color)
} }
// Draw aligns r.Min in dst with pt in src and mask // Draw calls DrawMask with a nil mask and an SoverD op.
// and then replaces the rectangle r in dst with the func Draw(dst Image, r Rectangle, src image.Image, sp Point) {
// result of the Porter-Duff compositing operation DrawMask(dst, r, src, sp, nil, ZP, SoverD)
// ``(src in mask) over dst.'' If mask is nil, the operation }
// simplifies to ``src over dst.''
// The implementation is simple and slow.
func Draw(dst Image, r Rectangle, src, mask image.Image, pt Point) {
// Plenty of room for optimizations here.
dx, dy := src.Width(), src.Height() // DrawMask aligns r.Min in dst with sp in src and mp in mask and then replaces the rectangle r
// in dst with the result of a Porter-Duff composition. For the SoverD operator, the result
// is ``(src in mask) over dst''. If mask is nil, this simplifies to ``src over dst''.
// The implementation is simple and slow.
// TODO(nigeltao): Optimize this.
func DrawMask(dst Image, r Rectangle, src image.Image, sp Point, mask image.Image, mp Point, op Op) {
dx, dy := src.Width()-sp.X, src.Height()-sp.Y
if mask != nil { if mask != nil {
if dx > mask.Width() { if dx > mask.Width()-mp.X {
dx = mask.Width() dx = mask.Width() - mp.X
} }
if dy > mask.Height() { if dy > mask.Height()-mp.Y {
dy = mask.Height() dy = mask.Height() - mp.Y
} }
} }
dx -= pt.X
dy -= pt.Y
if r.Dx() > dx { if r.Dx() > dx {
r.Max.X = r.Min.X + dx r.Max.X = r.Min.X + dx
} }
...@@ -45,26 +50,31 @@ func Draw(dst Image, r Rectangle, src, mask image.Image, pt Point) { ...@@ -45,26 +50,31 @@ func Draw(dst Image, r Rectangle, src, mask image.Image, pt Point) {
r.Max.Y = r.Min.Y + dy r.Max.Y = r.Min.Y + dy
} }
// TODO(nigeltao): Clip r to dst's bounding box, and handle the case when sp or mp has negative X or Y.
x0, x1, dx := r.Min.X, r.Max.X, 1 x0, x1, dx := r.Min.X, r.Max.X, 1
y0, y1, dy := r.Min.Y, r.Max.Y, 1 y0, y1, dy := r.Min.Y, r.Max.Y, 1
if image.Image(dst) == src && r.Overlaps(r.Add(pt.Sub(r.Min))) { if image.Image(dst) == src && r.Overlaps(r.Add(sp.Sub(r.Min))) {
// Rectangles overlap: process backward? // Rectangles overlap: process backward?
if pt.Y < r.Min.Y || pt.Y == r.Min.Y && pt.X < r.Min.X { if sp.Y < r.Min.Y || sp.Y == r.Min.Y && sp.X < r.Min.X {
x0, x1, dx = x1-1, x0-1, -1 x0, x1, dx = x1-1, x0-1, -1
y0, y1, dy = y1-1, y0-1, -1 y0, y1, dy = y1-1, y0-1, -1
} }
} }
var out *image.RGBA64Color var out *image.RGBA64Color
for y := y0; y != y1; y += dy { sy := sp.Y + y0 - r.Min.Y
for x := x0; x != x1; x += dx { my := mp.Y + y0 - r.Min.Y
sx := pt.X + x - r.Min.X for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy {
sy := pt.Y + y - r.Min.Y sx := sp.X + x0 - r.Min.X
mx := mp.X + x0 - r.Min.X
for x := x0; x != x1; x, sx, mx = x+dx, sx+dx, mx+dx {
// TODO(nigeltao): Check that op == SoverD.
if mask == nil { if mask == nil {
dst.Set(x, y, src.At(sx, sy)) dst.Set(x, y, src.At(sx, sy))
continue continue
} }
_, _, _, ma := mask.At(sx, sy).RGBA() _, _, _, ma := mask.At(mx, my).RGBA()
switch ma { switch ma {
case 0: case 0:
continue continue
...@@ -109,17 +119,17 @@ func Border(dst Image, r Rectangle, w int, src image.Image, sp Point) { ...@@ -109,17 +119,17 @@ func Border(dst Image, r Rectangle, w int, src image.Image, sp Point) {
i := w i := w
if i > 0 { if i > 0 {
// inside r // inside r
Draw(dst, Rect(r.Min.X, r.Min.Y, r.Max.X, r.Min.Y+i), src, nil, sp) // top Draw(dst, Rect(r.Min.X, r.Min.Y, r.Max.X, r.Min.Y+i), src, sp) // top
Draw(dst, Rect(r.Min.X, r.Min.Y+i, r.Min.X+i, r.Max.Y-i), src, nil, sp.Add(Pt(0, i))) // left Draw(dst, Rect(r.Min.X, r.Min.Y+i, r.Min.X+i, r.Max.Y-i), src, sp.Add(Pt(0, i))) // left
Draw(dst, Rect(r.Max.X-i, r.Min.Y+i, r.Max.X, r.Max.Y-i), src, nil, sp.Add(Pt(r.Dx()-i, i))) // right Draw(dst, Rect(r.Max.X-i, r.Min.Y+i, r.Max.X, r.Max.Y-i), src, sp.Add(Pt(r.Dx()-i, i))) // right
Draw(dst, Rect(r.Min.X, r.Max.Y-i, r.Max.X, r.Max.Y), src, nil, sp.Add(Pt(0, r.Dy()-i))) // bottom Draw(dst, Rect(r.Min.X, r.Max.Y-i, r.Max.X, r.Max.Y), src, sp.Add(Pt(0, r.Dy()-i))) // bottom
return return
} }
// outside r; // outside r;
i = -i i = -i
Draw(dst, Rect(r.Min.X-i, r.Min.Y-i, r.Max.X+i, r.Min.Y), src, nil, sp.Add(Pt(-i, -i))) // top Draw(dst, Rect(r.Min.X-i, r.Min.Y-i, r.Max.X+i, r.Min.Y), src, sp.Add(Pt(-i, -i))) // top
Draw(dst, Rect(r.Min.X-i, r.Min.Y, r.Min.X, r.Max.Y), src, nil, sp.Add(Pt(-i, 0))) // left Draw(dst, Rect(r.Min.X-i, r.Min.Y, r.Min.X, r.Max.Y), src, sp.Add(Pt(-i, 0))) // left
Draw(dst, Rect(r.Max.X, r.Min.Y, r.Max.X+i, r.Max.Y), src, nil, sp.Add(Pt(r.Dx(), 0))) // right Draw(dst, Rect(r.Max.X, r.Min.Y, r.Max.X+i, r.Max.Y), src, sp.Add(Pt(r.Dx(), 0))) // right
Draw(dst, Rect(r.Min.X-i, r.Max.Y, r.Max.X+i, r.Max.Y+i), src, nil, sp.Add(Pt(-i, 0))) // bottom Draw(dst, Rect(r.Min.X-i, r.Max.Y, r.Max.X+i, r.Max.Y+i), src, sp.Add(Pt(-i, 0))) // bottom
} }
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