2009-09-29 14:04:08 -07:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
|
|
|
|
|
// Package draw provides basic graphics and drawing primitives,
|
|
|
|
|
// in the style of the Plan 9 graphics library
|
|
|
|
|
// (see http://plan9.bell-labs.com/magic/man2html/2/draw)
|
|
|
|
|
// and the X Render extension.
|
|
|
|
|
package draw
|
|
|
|
|
|
|
|
|
|
// BUG(rsc): This is a toy library and not ready for production use.
|
|
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
|
|
2010-02-04 21:21:32 +11:00
|
|
|
// A Porter-Duff compositing operator.
|
|
|
|
|
type Op int
|
|
|
|
|
|
2010-02-17 14:34:51 +11:00
|
|
|
const (
|
|
|
|
|
// Over specifies ``(src in mask) over dst''.
|
|
|
|
|
Over Op = iota
|
|
|
|
|
// Src specifies ``src in mask''.
|
|
|
|
|
Src
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var zeroColor image.Color = image.AlphaColor{0}
|
2010-02-04 21:21:32 +11:00
|
|
|
|
2009-09-29 14:04:08 -07:00
|
|
|
// A draw.Image is an image.Image with a Set method to change a single pixel.
|
|
|
|
|
type Image interface {
|
2009-12-15 15:27:16 -08:00
|
|
|
image.Image
|
|
|
|
|
Set(x, y int, c image.Color)
|
2009-09-29 14:04:08 -07:00
|
|
|
}
|
|
|
|
|
|
2010-02-17 14:34:51 +11:00
|
|
|
// Draw calls DrawMask with a nil mask and an Over op.
|
2010-02-04 21:21:32 +11:00
|
|
|
func Draw(dst Image, r Rectangle, src image.Image, sp Point) {
|
2010-02-17 14:34:51 +11:00
|
|
|
DrawMask(dst, r, src, sp, nil, ZP, Over)
|
2010-02-04 21:21:32 +11:00
|
|
|
}
|
2009-09-29 14:04:08 -07:00
|
|
|
|
2010-02-04 21:21:32 +11:00
|
|
|
// DrawMask aligns r.Min in dst with sp in src and mp in mask and then replaces the rectangle r
|
2010-02-17 14:34:51 +11:00
|
|
|
// in dst with the result of a Porter-Duff composition. A nil mask is treated as opaque.
|
2010-02-04 21:21:32 +11:00
|
|
|
// 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
|
2009-09-29 14:04:08 -07:00
|
|
|
if mask != nil {
|
2010-02-04 21:21:32 +11:00
|
|
|
if dx > mask.Width()-mp.X {
|
|
|
|
|
dx = mask.Width() - mp.X
|
2009-09-29 14:04:08 -07:00
|
|
|
}
|
2010-02-04 21:21:32 +11:00
|
|
|
if dy > mask.Height()-mp.Y {
|
|
|
|
|
dy = mask.Height() - mp.Y
|
2009-09-29 14:04:08 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if r.Dx() > dx {
|
2009-11-09 12:07:39 -08:00
|
|
|
r.Max.X = r.Min.X + dx
|
2009-09-29 14:04:08 -07:00
|
|
|
}
|
|
|
|
|
if r.Dy() > dy {
|
2009-11-09 12:07:39 -08:00
|
|
|
r.Max.Y = r.Min.Y + dy
|
2009-09-29 14:04:08 -07:00
|
|
|
}
|
|
|
|
|
|
2010-02-04 21:21:32 +11:00
|
|
|
// TODO(nigeltao): Clip r to dst's bounding box, and handle the case when sp or mp has negative X or Y.
|
2010-02-06 15:57:19 +11:00
|
|
|
// TODO(nigeltao): Ensure that r is well formed, i.e. r.Max.X >= r.Min.X and likewise for Y.
|
|
|
|
|
|
|
|
|
|
// Fast paths for special cases. If none of them apply, then we fall back to a general but slow implementation.
|
2010-02-17 14:34:51 +11:00
|
|
|
if dst0, ok := dst.(*image.RGBA); ok {
|
|
|
|
|
if op == Over {
|
2010-05-10 10:32:08 +10:00
|
|
|
if mask0, ok := mask.(*image.Alpha); ok {
|
|
|
|
|
if src0, ok := src.(image.ColorImage); ok {
|
|
|
|
|
drawGlyphOver(dst0, r, src0, mask0, mp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-02-17 14:34:51 +11:00
|
|
|
} else {
|
|
|
|
|
if mask == nil {
|
|
|
|
|
if src0, ok := src.(image.ColorImage); ok {
|
|
|
|
|
drawFill(dst0, r, src0)
|
2010-02-06 15:57:19 +11:00
|
|
|
return
|
|
|
|
|
}
|
2010-02-17 14:34:51 +11:00
|
|
|
if src0, ok := src.(*image.RGBA); ok {
|
|
|
|
|
if dst0 == src0 && r.Overlaps(r.Add(sp.Sub(r.Min))) {
|
|
|
|
|
// TODO(nigeltao): Implement a fast path for the overlapping case.
|
|
|
|
|
} else {
|
|
|
|
|
drawCopy(dst0, r, src0, sp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-02-06 15:57:19 +11:00
|
|
|
}
|
|
|
|
|
}
|
2010-05-20 13:57:18 -07:00
|
|
|
drawRGBA(dst0, r, src, sp, mask, mp, op)
|
|
|
|
|
return
|
2010-02-06 15:57:19 +11:00
|
|
|
}
|
2010-02-04 21:21:32 +11:00
|
|
|
|
2009-12-15 15:27:16 -08:00
|
|
|
x0, x1, dx := r.Min.X, r.Max.X, 1
|
|
|
|
|
y0, y1, dy := r.Min.Y, r.Max.Y, 1
|
2010-02-04 21:21:32 +11:00
|
|
|
if image.Image(dst) == src && r.Overlaps(r.Add(sp.Sub(r.Min))) {
|
2009-09-29 14:04:08 -07:00
|
|
|
// Rectangles overlap: process backward?
|
2010-02-04 21:21:32 +11:00
|
|
|
if sp.Y < r.Min.Y || sp.Y == r.Min.Y && sp.X < r.Min.X {
|
2009-12-15 15:27:16 -08:00
|
|
|
x0, x1, dx = x1-1, x0-1, -1
|
|
|
|
|
y0, y1, dy = y1-1, y0-1, -1
|
2009-09-29 14:04:08 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-15 15:27:16 -08:00
|
|
|
var out *image.RGBA64Color
|
2010-02-04 21:21:32 +11:00
|
|
|
sy := sp.Y + y0 - r.Min.Y
|
|
|
|
|
my := mp.Y + y0 - r.Min.Y
|
|
|
|
|
for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy {
|
|
|
|
|
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 {
|
2010-02-17 14:34:51 +11:00
|
|
|
// A nil mask is equivalent to a fully opaque, infinitely large mask.
|
|
|
|
|
// We work in 16-bit color, so that multiplying two values does not overflow a uint32.
|
|
|
|
|
const M = 1<<16 - 1
|
|
|
|
|
ma := uint32(M)
|
|
|
|
|
if mask != nil {
|
|
|
|
|
_, _, _, ma = mask.At(mx, my).RGBA()
|
|
|
|
|
ma >>= 16
|
2009-09-29 14:04:08 -07:00
|
|
|
}
|
2010-02-17 14:34:51 +11:00
|
|
|
switch {
|
|
|
|
|
case ma == 0:
|
|
|
|
|
if op == Over {
|
|
|
|
|
// No-op.
|
|
|
|
|
} else {
|
|
|
|
|
dst.Set(x, y, zeroColor)
|
|
|
|
|
}
|
|
|
|
|
case ma == M && op == Src:
|
2009-11-09 12:07:39 -08:00
|
|
|
dst.Set(x, y, src.At(sx, sy))
|
2009-09-29 14:04:08 -07:00
|
|
|
default:
|
2009-12-15 15:27:16 -08:00
|
|
|
sr, sg, sb, sa := src.At(sx, sy).RGBA()
|
|
|
|
|
sr >>= 16
|
|
|
|
|
sg >>= 16
|
|
|
|
|
sb >>= 16
|
|
|
|
|
sa >>= 16
|
2009-09-29 14:04:08 -07:00
|
|
|
if out == nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
out = new(image.RGBA64Color)
|
2009-09-29 14:04:08 -07:00
|
|
|
}
|
2010-02-17 14:34:51 +11:00
|
|
|
if op == Over {
|
|
|
|
|
dr, dg, db, da := dst.At(x, y).RGBA()
|
|
|
|
|
dr >>= 16
|
|
|
|
|
dg >>= 16
|
|
|
|
|
db >>= 16
|
|
|
|
|
da >>= 16
|
|
|
|
|
a := M - (sa * ma / M)
|
|
|
|
|
out.R = uint16((dr*a + sr*ma) / M)
|
|
|
|
|
out.G = uint16((dg*a + sg*ma) / M)
|
|
|
|
|
out.B = uint16((db*a + sb*ma) / M)
|
|
|
|
|
out.A = uint16((da*a + sa*ma) / M)
|
|
|
|
|
} else {
|
|
|
|
|
out.R = uint16(sr * ma / M)
|
|
|
|
|
out.G = uint16(sg * ma / M)
|
|
|
|
|
out.B = uint16(sb * ma / M)
|
|
|
|
|
out.A = uint16(sa * ma / M)
|
|
|
|
|
}
|
2009-12-15 15:27:16 -08:00
|
|
|
dst.Set(x, y, out)
|
2009-09-29 14:04:08 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-10 10:32:08 +10:00
|
|
|
func drawGlyphOver(dst *image.RGBA, r Rectangle, src image.ColorImage, mask *image.Alpha, mp Point) {
|
|
|
|
|
x0, x1 := r.Min.X, r.Max.X
|
|
|
|
|
y0, y1 := r.Min.Y, r.Max.Y
|
|
|
|
|
cr, cg, cb, ca := src.RGBA()
|
|
|
|
|
cr >>= 16
|
|
|
|
|
cg >>= 16
|
|
|
|
|
cb >>= 16
|
|
|
|
|
ca >>= 16
|
|
|
|
|
for y, my := y0, mp.Y; y != y1; y, my = y+1, my+1 {
|
2010-05-20 13:57:18 -07:00
|
|
|
p := dst.Pixel[y]
|
2010-05-10 10:32:08 +10:00
|
|
|
for x, mx := x0, mp.X; x != x1; x, mx = x+1, mx+1 {
|
|
|
|
|
ma := uint32(mask.Pixel[my][mx].A)
|
|
|
|
|
if ma == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
ma |= ma << 8
|
2010-05-20 13:57:18 -07:00
|
|
|
rgba := p[x]
|
2010-05-11 13:35:16 +10:00
|
|
|
dr := uint32(rgba.R)
|
|
|
|
|
dg := uint32(rgba.G)
|
|
|
|
|
db := uint32(rgba.B)
|
|
|
|
|
da := uint32(rgba.A)
|
2010-05-10 10:32:08 +10:00
|
|
|
const M = 1<<16 - 1
|
2010-05-20 13:57:18 -07:00
|
|
|
// The 0x101 is here for the same reason as in drawRGBA.
|
|
|
|
|
a := (M - (ca * ma / M)) * 0x101
|
2010-05-10 10:32:08 +10:00
|
|
|
dr = (dr*a + cr*ma) / M
|
|
|
|
|
dg = (dg*a + cg*ma) / M
|
|
|
|
|
db = (db*a + cb*ma) / M
|
|
|
|
|
da = (da*a + ca*ma) / M
|
2010-05-20 13:57:18 -07:00
|
|
|
p[x] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)}
|
2010-05-10 10:32:08 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-06 15:57:19 +11:00
|
|
|
func drawFill(dst *image.RGBA, r Rectangle, src image.ColorImage) {
|
|
|
|
|
if r.Dy() < 1 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
cr, cg, cb, ca := src.RGBA()
|
|
|
|
|
color := image.RGBAColor{uint8(cr >> 24), uint8(cg >> 24), uint8(cb >> 24), uint8(ca >> 24)}
|
|
|
|
|
// The built-in copy function is faster than a straightforward for loop to fill the destination with
|
|
|
|
|
// the color, but copy requires a slice source. We therefore use a for loop to fill the first row, and
|
|
|
|
|
// then use the first row as the slice source for the remaining rows.
|
|
|
|
|
dx0, dx1 := r.Min.X, r.Max.X
|
|
|
|
|
dy0, dy1 := r.Min.Y, r.Max.Y
|
|
|
|
|
firstRow := dst.Pixel[dy0]
|
|
|
|
|
for x := dx0; x < dx1; x++ {
|
|
|
|
|
firstRow[x] = color
|
|
|
|
|
}
|
|
|
|
|
copySrc := firstRow[dx0:dx1]
|
|
|
|
|
for y := dy0 + 1; y < dy1; y++ {
|
|
|
|
|
copy(dst.Pixel[y][dx0:dx1], copySrc)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func drawCopy(dst *image.RGBA, r Rectangle, src *image.RGBA, sp Point) {
|
|
|
|
|
dx0, dx1 := r.Min.X, r.Max.X
|
|
|
|
|
dy0, dy1 := r.Min.Y, r.Max.Y
|
|
|
|
|
sx0, sx1 := sp.X, sp.X+dx1-dx0
|
|
|
|
|
for y, sy := dy0, sp.Y; y < dy1; y, sy = y+1, sy+1 {
|
|
|
|
|
copy(dst.Pixel[y][dx0:dx1], src.Pixel[sy][sx0:sx1])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-20 13:57:18 -07:00
|
|
|
func drawRGBA(dst *image.RGBA, r Rectangle, src image.Image, sp Point, mask image.Image, mp Point, op Op) {
|
|
|
|
|
x0, x1, dx := r.Min.X, r.Max.X, 1
|
|
|
|
|
y0, y1, dy := r.Min.Y, r.Max.Y, 1
|
|
|
|
|
if image.Image(dst) == src && r.Overlaps(r.Add(sp.Sub(r.Min))) {
|
|
|
|
|
if sp.Y < r.Min.Y || sp.Y == r.Min.Y && sp.X < r.Min.X {
|
|
|
|
|
x0, x1, dx = x1-1, x0-1, -1
|
|
|
|
|
y0, y1, dy = y1-1, y0-1, -1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sy := sp.Y + y0 - r.Min.Y
|
|
|
|
|
my := mp.Y + y0 - r.Min.Y
|
|
|
|
|
for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy {
|
|
|
|
|
sx := sp.X + x0 - r.Min.X
|
|
|
|
|
mx := mp.X + x0 - r.Min.X
|
|
|
|
|
p := dst.Pixel[y]
|
|
|
|
|
for x := x0; x != x1; x, sx, mx = x+dx, sx+dx, mx+dx {
|
|
|
|
|
const M = 1<<16 - 1
|
|
|
|
|
ma := uint32(M)
|
|
|
|
|
if mask != nil {
|
|
|
|
|
_, _, _, ma = mask.At(mx, my).RGBA()
|
|
|
|
|
ma >>= 16
|
|
|
|
|
}
|
|
|
|
|
sr, sg, sb, sa := src.At(sx, sy).RGBA()
|
|
|
|
|
sr >>= 16
|
|
|
|
|
sg >>= 16
|
|
|
|
|
sb >>= 16
|
|
|
|
|
sa >>= 16
|
|
|
|
|
var dr, dg, db, da uint32
|
|
|
|
|
if op == Over {
|
|
|
|
|
rgba := p[x]
|
|
|
|
|
dr = uint32(rgba.R)
|
|
|
|
|
dg = uint32(rgba.G)
|
|
|
|
|
db = uint32(rgba.B)
|
|
|
|
|
da = uint32(rgba.A)
|
|
|
|
|
// dr, dg, db and da are all 8-bit color at the moment, ranging in [0,255].
|
|
|
|
|
// We work in 16-bit color, and so would normally do:
|
|
|
|
|
// dr |= dr << 8
|
|
|
|
|
// and similarly for dg, db and da, but instead we multiply a
|
|
|
|
|
// (which is a 16-bit color, ranging in [0,65535]) by 0x101.
|
|
|
|
|
// This yields the same result, but is fewer arithmetic operations.
|
|
|
|
|
a := (M - (sa * ma / M)) * 0x101
|
|
|
|
|
dr = (dr*a + sr*ma) / M
|
|
|
|
|
dg = (dg*a + sg*ma) / M
|
|
|
|
|
db = (db*a + sb*ma) / M
|
|
|
|
|
da = (da*a + sa*ma) / M
|
|
|
|
|
} else {
|
|
|
|
|
dr = sr * ma / M
|
|
|
|
|
dg = sg * ma / M
|
|
|
|
|
db = sb * ma / M
|
|
|
|
|
da = sa * ma / M
|
|
|
|
|
}
|
|
|
|
|
p[x] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-29 14:04:08 -07:00
|
|
|
// Border aligns r.Min in dst with sp in src and then replaces pixels
|
|
|
|
|
// in a w-pixel border around r in dst with the result of the Porter-Duff compositing
|
|
|
|
|
// operation ``src over dst.'' If w is positive, the border extends w pixels inside r.
|
|
|
|
|
// If w is negative, the border extends w pixels outside r.
|
|
|
|
|
func Border(dst Image, r Rectangle, w int, src image.Image, sp Point) {
|
2009-12-15 15:27:16 -08:00
|
|
|
i := w
|
2009-09-29 14:04:08 -07:00
|
|
|
if i > 0 {
|
|
|
|
|
// inside r
|
2010-02-04 21:21:32 +11:00
|
|
|
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, 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, 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, sp.Add(Pt(0, r.Dy()-i))) // bottom
|
2009-12-15 15:27:16 -08:00
|
|
|
return
|
2009-09-29 14:04:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// outside r;
|
2009-12-15 15:27:16 -08:00
|
|
|
i = -i
|
2010-02-04 21:21:32 +11:00
|
|
|
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, sp.Add(Pt(-i, 0))) // left
|
|
|
|
|
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, sp.Add(Pt(-i, 0))) // bottom
|
2009-09-29 14:04:08 -07:00
|
|
|
}
|