diff options
author | Ondřej Surý <ondrej@sury.org> | 2011-01-17 12:40:45 +0100 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2011-01-17 12:40:45 +0100 |
commit | 3e45412327a2654a77944249962b3652e6142299 (patch) | |
tree | bc3bf69452afa055423cbe0c5cfa8ca357df6ccf /src/pkg/exp/draw/draw.go | |
parent | c533680039762cacbc37db8dc7eed074c3e497be (diff) | |
download | golang-upstream/2011.01.12.tar.gz |
Imported Upstream version 2011.01.12upstream/2011.01.12
Diffstat (limited to 'src/pkg/exp/draw/draw.go')
-rw-r--r-- | src/pkg/exp/draw/draw.go | 207 |
1 files changed, 116 insertions, 91 deletions
diff --git a/src/pkg/exp/draw/draw.go b/src/pkg/exp/draw/draw.go index 415dd99ac..1d0729d92 100644 --- a/src/pkg/exp/draw/draw.go +++ b/src/pkg/exp/draw/draw.go @@ -8,8 +8,6 @@ // and the X Render extension. package draw -// BUG(rsc): This is a toy library and not ready for production use. - import "image" // m is the maximum color value returned by image.Color.RGBA. @@ -34,22 +32,22 @@ type Image interface { } // Draw calls DrawMask with a nil mask and an Over op. -func Draw(dst Image, r Rectangle, src image.Image, sp Point) { - DrawMask(dst, r, src, sp, nil, ZP, Over) +func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point) { + DrawMask(dst, r, src, sp, nil, image.ZP, Over) } // 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. A nil mask is treated as opaque. -// 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 +func DrawMask(dst Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op) { + sb := src.Bounds() + dx, dy := sb.Max.X-sp.X, sb.Max.Y-sp.Y if mask != nil { - if dx > mask.Width()-mp.X { - dx = mask.Width() - mp.X + mb := mask.Bounds() + if dx > mb.Max.X-mp.X { + dx = mb.Max.X - mp.X } - if dy > mask.Height()-mp.Y { - dy = mask.Height() - mp.Y + if dy > mb.Max.Y-mp.Y { + dy = mb.Max.Y - mp.Y } } if r.Dx() > dx { @@ -58,45 +56,38 @@ func DrawMask(dst Image, r Rectangle, src image.Image, sp Point, mask image.Imag if r.Dy() > 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. - // TODO(nigeltao): Ensure that r is well formed, i.e. r.Max.X >= r.Min.X and likewise for Y. + r = r.Intersect(dst.Bounds()) + if r.Empty() { + return + } // Fast paths for special cases. If none of them apply, then we fall back to a general but slow implementation. 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 } 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 { - drawCopyOver(dst0, r, src0, sp) - return - } + drawCopyOver(dst0, r, src0, sp) + 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 } 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 { - drawCopySrc(dst0, r, src0, sp) - return - } + drawCopySrc(dst0, r, src0, sp) + return } } } @@ -158,66 +149,96 @@ func DrawMask(dst Image, r Rectangle, src image.Image, sp Point, mask image.Imag } } -func drawFillOver(dst *image.RGBA, r 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 x0, x1 := r.Min.X, r.Max.X y0, y1 := r.Min.Y, r.Max.Y for y := y0; y != y1; y++ { - dpix := dst.Pixel[y] - for x := x0; x != x1; x++ { - rgba := dpix[x] + dbase := y * dst.Stride + dpix := dst.Pix[dbase+x0 : dbase+x1] + for i, rgba := range dpix { dr := (uint32(rgba.R)*a)/m + cr dg := (uint32(rgba.G)*a)/m + cg db := (uint32(rgba.B)*a)/m + cb da := (uint32(rgba.A)*a)/m + ca - dpix[x] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)} + dpix[i] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)} } } } -func drawCopyOver(dst *image.RGBA, r Rectangle, src *image.RGBA, sp Point) { - x0, x1 := r.Min.X, r.Max.X - y0, y1 := r.Min.Y, r.Max.Y - for y, sy := y0, sp.Y; y != y1; y, sy = y+1, sy+1 { - dpix := dst.Pixel[y] - spix := src.Pixel[sy] - for x, sx := x0, sp.X; x != x1; x, sx = x+1, sx+1 { - // For unknown reasons, even though both dpix[x] and spix[sx] are +func drawCopyOver(dst *image.RGBA, r image.Rectangle, src *image.RGBA, sp image.Point) { + dx0, dx1 := r.Min.X, r.Max.X + dy0, dy1 := r.Min.Y, r.Max.Y + nrows := dy1 - dy0 + sx0, sx1 := sp.X, sp.X+dx1-dx0 + d0 := dy0*dst.Stride + dx0 + d1 := dy0*dst.Stride + dx1 + s0 := sp.Y*src.Stride + sx0 + s1 := sp.Y*src.Stride + sx1 + var ( + ddelta, sdelta int + i0, i1, idelta int + ) + if r.Min.Y < sp.Y || r.Min.Y == sp.Y && r.Min.X <= sp.X { + ddelta = dst.Stride + sdelta = src.Stride + i0, i1, idelta = 0, d1-d0, +1 + } else { + // If the source start point is higher than the destination start point, or equal height but to the left, + // then we compose the rows in right-to-left, bottom-up order instead of left-to-right, top-down. + d0 += (nrows - 1) * dst.Stride + d1 += (nrows - 1) * dst.Stride + s0 += (nrows - 1) * src.Stride + s1 += (nrows - 1) * src.Stride + ddelta = -dst.Stride + sdelta = -src.Stride + i0, i1, idelta = d1-d0-1, -1, -1 + } + for ; nrows > 0; nrows-- { + dpix := dst.Pix[d0:d1] + spix := src.Pix[s0:s1] + for i := i0; i != i1; i += idelta { + // For unknown reasons, even though both dpix[i] and spix[i] are // image.RGBAColors, on an x86 CPU it seems fastest to call RGBA // for the source but to do it manually for the destination. - sr, sg, sb, sa := spix[sx].RGBA() - drgba := dpix[x] - dr := uint32(drgba.R) - dg := uint32(drgba.G) - db := uint32(drgba.B) - da := uint32(drgba.A) + sr, sg, sb, sa := spix[i].RGBA() + rgba := dpix[i] + dr := uint32(rgba.R) + dg := uint32(rgba.G) + db := uint32(rgba.B) + da := uint32(rgba.A) // The 0x101 is here for the same reason as in drawRGBA. a := (m - sa) * 0x101 dr = (dr*a)/m + sr dg = (dg*a)/m + sg db = (db*a)/m + sb da = (da*a)/m + sa - dpix[x] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)} + dpix[i] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)} } + d0 += ddelta + d1 += ddelta + s0 += sdelta + s1 += sdelta } } -func drawGlyphOver(dst *image.RGBA, r Rectangle, src image.ColorImage, mask *image.Alpha, mp 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() for y, my := y0, mp.Y; y != y1; y, my = y+1, my+1 { - dpix := dst.Pixel[y] - mpix := mask.Pixel[my] - for x, mx := x0, mp.X; x != x1; x, mx = x+1, mx+1 { - ma := uint32(mpix[mx].A) + dbase := y * dst.Stride + dpix := dst.Pix[dbase+x0 : dbase+x1] + mbase := my * mask.Stride + mpix := mask.Pix[mbase+mp.X:] + for i, rgba := range dpix { + ma := uint32(mpix[i].A) if ma == 0 { continue } ma |= ma << 8 - rgba := dpix[x] dr := uint32(rgba.R) dg := uint32(rgba.G) db := uint32(rgba.B) @@ -228,12 +249,12 @@ func drawGlyphOver(dst *image.RGBA, r Rectangle, src image.ColorImage, mask *ima dg = (dg*a + cg*ma) / m db = (db*a + cb*ma) / m da = (da*a + ca*ma) / m - dpix[x] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)} + dpix[i] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)} } } } -func drawFillSrc(dst *image.RGBA, r Rectangle, src image.ColorImage) { +func drawFillSrc(dst *image.RGBA, r image.Rectangle, src *image.ColorImage) { if r.Dy() < 1 { return } @@ -244,26 +265,53 @@ func drawFillSrc(dst *image.RGBA, r Rectangle, src image.ColorImage) { // 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 + dbase := dy0 * dst.Stride + i0, i1 := dbase+dx0, dbase+dx1 + firstRow := dst.Pix[i0:i1] + for i := range firstRow { + firstRow[i] = color } - copySrc := firstRow[dx0:dx1] for y := dy0 + 1; y < dy1; y++ { - copy(dst.Pixel[y][dx0:dx1], copySrc) + i0 += dst.Stride + i1 += dst.Stride + copy(dst.Pix[i0:i1], firstRow) } } -func drawCopySrc(dst *image.RGBA, r Rectangle, src *image.RGBA, sp Point) { +func drawCopySrc(dst *image.RGBA, r image.Rectangle, src *image.RGBA, sp image.Point) { dx0, dx1 := r.Min.X, r.Max.X dy0, dy1 := r.Min.Y, r.Max.Y + nrows := dy1 - dy0 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]) + d0 := dy0*dst.Stride + dx0 + d1 := dy0*dst.Stride + dx1 + s0 := sp.Y*src.Stride + sx0 + s1 := sp.Y*src.Stride + sx1 + var ddelta, sdelta int + if r.Min.Y <= sp.Y { + ddelta = dst.Stride + sdelta = src.Stride + } else { + // If the source start point is higher than the destination start point, then we compose the rows + // in bottom-up order instead of top-down. Unlike the drawCopyOver function, we don't have to + // check the x co-ordinates because the built-in copy function can handle overlapping slices. + d0 += (nrows - 1) * dst.Stride + d1 += (nrows - 1) * dst.Stride + s0 += (nrows - 1) * src.Stride + s1 += (nrows - 1) * src.Stride + ddelta = -dst.Stride + sdelta = -src.Stride + } + for ; nrows > 0; nrows-- { + copy(dst.Pix[d0:d1], src.Pix[s0:s1]) + d0 += ddelta + d1 += ddelta + s0 += sdelta + s1 += sdelta } } -func drawRGBA(dst *image.RGBA, r Rectangle, src image.Image, sp Point, mask image.Image, mp Point, op Op) { +func drawRGBA(dst *image.RGBA, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.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))) { @@ -278,7 +326,7 @@ func drawRGBA(dst *image.RGBA, r Rectangle, src image.Image, sp Point, mask imag 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 - dpix := dst.Pixel[y] + dpix := dst.Pix[y*dst.Stride : (y+1)*dst.Stride] for x := x0; x != x1; x, sx, mx = x+dx, sx+dx, mx+dx { ma := uint32(m) if mask != nil { @@ -313,26 +361,3 @@ func drawRGBA(dst *image.RGBA, r Rectangle, src image.Image, sp Point, mask imag } } } - -// 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) { - i := w - if i > 0 { - // inside r - 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 - return - } - - // outside r; - i = -i - 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 -} |