Commit b50a3d95 authored by Nigel Tao's avatar Nigel Tao

image: change image representation from slice-of-slices to linear buffer,

stride and rect.

R=r
CC=golang-dev, rog
https://golang.org/cl/1849041
parent 56b989f1
...@@ -167,14 +167,14 @@ func drawFillOver(dst *image.RGBA, r Rectangle, src image.ColorImage) { ...@@ -167,14 +167,14 @@ func drawFillOver(dst *image.RGBA, r Rectangle, src image.ColorImage) {
x0, x1 := r.Min.X, r.Max.X x0, x1 := r.Min.X, r.Max.X
y0, y1 := r.Min.Y, r.Max.Y y0, y1 := r.Min.Y, r.Max.Y
for y := y0; y != y1; y++ { for y := y0; y != y1; y++ {
dpix := dst.Pixel[y] dbase := y * dst.Stride
for x := x0; x != x1; x++ { dpix := dst.Pix[dbase+x0 : dbase+x1]
rgba := dpix[x] for i, rgba := range dpix {
dr := (uint32(rgba.R)*a)/m + cr dr := (uint32(rgba.R)*a)/m + cr
dg := (uint32(rgba.G)*a)/m + cg dg := (uint32(rgba.G)*a)/m + cg
db := (uint32(rgba.B)*a)/m + cb db := (uint32(rgba.B)*a)/m + cb
da := (uint32(rgba.A)*a)/m + ca 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)}
} }
} }
} }
...@@ -183,25 +183,26 @@ func drawCopyOver(dst *image.RGBA, r Rectangle, src *image.RGBA, sp Point) { ...@@ -183,25 +183,26 @@ func drawCopyOver(dst *image.RGBA, r Rectangle, src *image.RGBA, sp Point) {
x0, x1 := r.Min.X, r.Max.X x0, x1 := r.Min.X, r.Max.X
y0, y1 := r.Min.Y, r.Max.Y y0, y1 := r.Min.Y, r.Max.Y
for y, sy := y0, sp.Y; y != y1; y, sy = y+1, sy+1 { for y, sy := y0, sp.Y; y != y1; y, sy = y+1, sy+1 {
dpix := dst.Pixel[y] dbase := y * dst.Stride
spix := src.Pixel[sy] dpix := dst.Pix[dbase+x0 : dbase+x1]
for x, sx := x0, sp.X; x != x1; x, sx = x+1, sx+1 { sbase := sy * src.Stride
// For unknown reasons, even though both dpix[x] and spix[sx] are spix := src.Pix[sbase+sp.X:]
for i, rgba := range dpix {
// For unknown reasons, even though both dpix[i] and spix[i] are
// image.RGBAColors, on an x86 CPU it seems fastest to call RGBA // image.RGBAColors, on an x86 CPU it seems fastest to call RGBA
// for the source but to do it manually for the destination. // for the source but to do it manually for the destination.
sr, sg, sb, sa := spix[sx].RGBA() sr, sg, sb, sa := spix[i].RGBA()
drgba := dpix[x] dr := uint32(rgba.R)
dr := uint32(drgba.R) dg := uint32(rgba.G)
dg := uint32(drgba.G) db := uint32(rgba.B)
db := uint32(drgba.B) da := uint32(rgba.A)
da := uint32(drgba.A)
// The 0x101 is here for the same reason as in drawRGBA. // The 0x101 is here for the same reason as in drawRGBA.
a := (m - sa) * 0x101 a := (m - sa) * 0x101
dr = (dr*a)/m + sr dr = (dr*a)/m + sr
dg = (dg*a)/m + sg dg = (dg*a)/m + sg
db = (db*a)/m + sb db = (db*a)/m + sb
da = (da*a)/m + sa 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)}
} }
} }
} }
...@@ -211,15 +212,16 @@ func drawGlyphOver(dst *image.RGBA, r Rectangle, src image.ColorImage, mask *ima ...@@ -211,15 +212,16 @@ func drawGlyphOver(dst *image.RGBA, r Rectangle, src image.ColorImage, mask *ima
y0, y1 := r.Min.Y, r.Max.Y y0, y1 := r.Min.Y, r.Max.Y
cr, cg, cb, ca := src.RGBA() cr, cg, cb, ca := src.RGBA()
for y, my := y0, mp.Y; y != y1; y, my = y+1, my+1 { for y, my := y0, mp.Y; y != y1; y, my = y+1, my+1 {
dpix := dst.Pixel[y] dbase := y * dst.Stride
mpix := mask.Pixel[my] dpix := dst.Pix[dbase+x0 : dbase+x1]
for x, mx := x0, mp.X; x != x1; x, mx = x+1, mx+1 { mbase := my * mask.Stride
ma := uint32(mpix[mx].A) mpix := mask.Pix[mbase+mp.X:]
for i, rgba := range dpix {
ma := uint32(mpix[i].A)
if ma == 0 { if ma == 0 {
continue continue
} }
ma |= ma << 8 ma |= ma << 8
rgba := dpix[x]
dr := uint32(rgba.R) dr := uint32(rgba.R)
dg := uint32(rgba.G) dg := uint32(rgba.G)
db := uint32(rgba.B) db := uint32(rgba.B)
...@@ -230,7 +232,7 @@ func drawGlyphOver(dst *image.RGBA, r Rectangle, src image.ColorImage, mask *ima ...@@ -230,7 +232,7 @@ func drawGlyphOver(dst *image.RGBA, r Rectangle, src image.ColorImage, mask *ima
dg = (dg*a + cg*ma) / m dg = (dg*a + cg*ma) / m
db = (db*a + cb*ma) / m db = (db*a + cb*ma) / m
da = (da*a + ca*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)}
} }
} }
} }
...@@ -246,13 +248,16 @@ func drawFillSrc(dst *image.RGBA, r Rectangle, src image.ColorImage) { ...@@ -246,13 +248,16 @@ func drawFillSrc(dst *image.RGBA, r Rectangle, src image.ColorImage) {
// then use the first row as the slice source for the remaining rows. // then use the first row as the slice source for the remaining rows.
dx0, dx1 := r.Min.X, r.Max.X dx0, dx1 := r.Min.X, r.Max.X
dy0, dy1 := r.Min.Y, r.Max.Y dy0, dy1 := r.Min.Y, r.Max.Y
firstRow := dst.Pixel[dy0] dbase := dy0 * dst.Stride
for x := dx0; x < dx1; x++ { i0, i1 := dbase+dx0, dbase+dx1
firstRow[x] = color firstRow := dst.Pix[i0:i1]
for i, _ := range firstRow {
firstRow[i] = color
} }
copySrc := firstRow[dx0:dx1]
for y := dy0 + 1; y < dy1; y++ { 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)
} }
} }
...@@ -260,8 +265,16 @@ func drawCopySrc(dst *image.RGBA, r Rectangle, src *image.RGBA, sp Point) { ...@@ -260,8 +265,16 @@ func drawCopySrc(dst *image.RGBA, r Rectangle, src *image.RGBA, sp Point) {
dx0, dx1 := r.Min.X, r.Max.X dx0, dx1 := r.Min.X, r.Max.X
dy0, dy1 := r.Min.Y, r.Max.Y dy0, dy1 := r.Min.Y, r.Max.Y
sx0, sx1 := sp.X, sp.X+dx1-dx0 sx0, sx1 := sp.X, sp.X+dx1-dx0
for y, sy := dy0, sp.Y; y < dy1; y, sy = y+1, sy+1 { d0 := dy0*dst.Stride + dx0
copy(dst.Pixel[y][dx0:dx1], src.Pixel[sy][sx0:sx1]) d1 := dy0*dst.Stride + dx1
s0 := sp.Y*dst.Stride + sx0
s1 := sp.Y*dst.Stride + sx1
for y := dy0; y < dy1; y++ {
copy(dst.Pix[d0:d1], src.Pix[s0:s1])
d0 += dst.Stride
d1 += dst.Stride
s0 += src.Stride
s1 += src.Stride
} }
} }
...@@ -280,8 +293,9 @@ func drawRGBA(dst *image.RGBA, r Rectangle, src image.Image, sp Point, mask imag ...@@ -280,8 +293,9 @@ 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 { for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy {
sx := sp.X + x0 - r.Min.X sx := sp.X + x0 - r.Min.X
mx := mp.X + x0 - r.Min.X mx := mp.X + x0 - r.Min.X
dpix := dst.Pixel[y] dbase := y * dst.Stride
for x := x0; x != x1; x, sx, mx = x+dx, sx+dx, mx+dx { dpix := dst.Pix[dbase+x0 : dbase+x1]
for i, rgba := range dpix {
ma := uint32(m) ma := uint32(m)
if mask != nil { if mask != nil {
_, _, _, ma = mask.At(mx, my).RGBA() _, _, _, ma = mask.At(mx, my).RGBA()
...@@ -289,7 +303,6 @@ func drawRGBA(dst *image.RGBA, r Rectangle, src image.Image, sp Point, mask imag ...@@ -289,7 +303,6 @@ func drawRGBA(dst *image.RGBA, r Rectangle, src image.Image, sp Point, mask imag
sr, sg, sb, sa := src.At(sx, sy).RGBA() sr, sg, sb, sa := src.At(sx, sy).RGBA()
var dr, dg, db, da uint32 var dr, dg, db, da uint32
if op == Over { if op == Over {
rgba := dpix[x]
dr = uint32(rgba.R) dr = uint32(rgba.R)
dg = uint32(rgba.G) dg = uint32(rgba.G)
db = uint32(rgba.B) db = uint32(rgba.B)
...@@ -311,7 +324,8 @@ func drawRGBA(dst *image.RGBA, r Rectangle, src image.Image, sp Point, mask imag ...@@ -311,7 +324,8 @@ func drawRGBA(dst *image.RGBA, r Rectangle, src image.Image, sp Point, mask imag
db = sb * ma / m db = sb * ma / m
da = sa * ma / m da = sa * 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)}
sx, mx = sx+dx, mx+dx
} }
} }
} }
......
...@@ -99,7 +99,7 @@ func (c *conn) flusher() { ...@@ -99,7 +99,7 @@ func (c *conn) flusher() {
close(c.flush) close(c.flush)
return return
} }
p := c.img.Pixel[y] p := c.img.Pix[y*c.img.Stride : (y+1)*c.img.Stride]
for x := b.Min.X; x < b.Max.X; { for x := b.Min.X; x < b.Max.X; {
nx := b.Max.X - x nx := b.Max.X - x
if nx > len(c.flushBuf1)/4 { if nx > len(c.flushBuf1)/4 {
......
...@@ -97,6 +97,12 @@ func (r Rectangle) Overlaps(s Rectangle) bool { ...@@ -97,6 +97,12 @@ func (r Rectangle) Overlaps(s Rectangle) bool {
r.Min.Y < s.Max.Y && s.Min.Y < r.Max.Y r.Min.Y < s.Max.Y && s.Min.Y < r.Max.Y
} }
// Contains returns whether r contains p.
func (r Rectangle) Contains(p Point) bool {
return p.X >= r.Min.X && p.X < r.Max.X &&
p.Y >= r.Min.Y && p.Y < r.Max.Y
}
// Canon returns the canonical version of r. The returned rectangle has // Canon returns the canonical version of r. The returned rectangle has
// minimum and maximum coordinates swapped if necessary so that Min.X <= Max.X // minimum and maximum coordinates swapped if necessary so that Min.X <= Max.X
// and Min.Y <= Max.Y. // and Min.Y <= Max.Y.
......
This diff is collapsed.
...@@ -206,7 +206,7 @@ func (d *decoder) calcPixel(px, py, lumaBlock, lumaIndex, chromaIndex int) { ...@@ -206,7 +206,7 @@ func (d *decoder) calcPixel(px, py, lumaBlock, lumaIndex, chromaIndex int) {
} else if b > 255 { } else if b > 255 {
b = 255 b = 255
} }
d.image.Pixel[py][px] = image.RGBAColor{uint8(r), uint8(g), uint8(b), 0xff} d.image.Pix[py*d.image.Stride+px] = image.RGBAColor{uint8(r), uint8(g), uint8(b), 0xff}
} }
// Convert the MCU from YCbCr to RGB. // Convert the MCU from YCbCr to RGB.
......
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