From 3657abe8845c862cd870e923996e8d7608e4ccef Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Tue, 10 Aug 2010 17:39:32 +1000 Subject: [PATCH] freetype: update for image representation change (1849041). R=r, adg CC=golang-dev http://codereview.appspot.com/1850041 --- example/freetype/main.go | 2 +- example/gamma/main.go | 9 +++++--- example/raster/main.go | 2 +- example/round/main.go | 7 +++--- freetype/raster/paint.go | 49 +++++++++++++++++++++------------------- 5 files changed, 38 insertions(+), 31 deletions(-) diff --git a/example/freetype/main.go b/example/freetype/main.go index f28d3cd..3467d3b 100644 --- a/example/freetype/main.go +++ b/example/freetype/main.go @@ -88,7 +88,7 @@ func main() { ruler = image.RGBAColor{0x22, 0x22, 0x22, 0xff} } rgba := image.NewRGBA(640, 480) - draw.Draw(rgba, draw.Rect(0, 0, rgba.Width(), rgba.Height()), bg, draw.ZP) + draw.Draw(rgba, draw.Rect(0, 0, 640, 480), bg, draw.ZP) c := freetype.NewContext() c.SetDPI(*dpi) c.SetFont(font) diff --git a/example/gamma/main.go b/example/gamma/main.go index a86cd45..7845b7f 100644 --- a/example/gamma/main.go +++ b/example/gamma/main.go @@ -22,9 +22,12 @@ func p(x, y int) raster.Point { } func clear(m *image.Alpha) { - for y := 0; y < m.Height(); y++ { - for x := 0; x < m.Width(); x++ { - m.Pixel[y][x] = image.AlphaColor{0} + b := m.Bounds() + for y := b.Min.Y; y < b.Max.Y; y++ { + base := y * m.Stride + p := m.Pix[base+b.Min.X : base+b.Max.X] + for i, _ := range p { + p[i] = image.AlphaColor{0} } } } diff --git a/example/raster/main.go b/example/raster/main.go index 685e6e8..b63a51b 100644 --- a/example/raster/main.go +++ b/example/raster/main.go @@ -113,7 +113,7 @@ func showNodes(m *image.RGBA, ns []node) { for _, n := range ns { p := p(n) x, y := int(p.X)/256, int(p.Y)/256 - if x < 0 || x >= m.Width() || y < 0 || y >= m.Height() { + if !m.Bounds().Contains(image.Point{x, y}) { continue } var c image.Color diff --git a/example/round/main.go b/example/round/main.go index 0fad99d..6f51d95 100644 --- a/example/round/main.go +++ b/example/round/main.go @@ -31,9 +31,10 @@ func main() { t := raster.Fix32(r * math.Tan(math.Pi/8)) m := image.NewRGBA(800, 600) - for y := 0; y < m.Height(); y++ { - for x := 0; x < m.Width(); x++ { - m.Pixel[y][x] = image.RGBAColor{63, 63, 63, 255} + for y := 0; y < 600; y++ { + p := m.Pix[y*m.Stride : y*m.Stride+800] + for i := range p { + p[i] = image.RGBAColor{63, 63, 63, 255} } } mp := raster.NewRGBAPainter(m) diff --git a/freetype/raster/paint.go b/freetype/raster/paint.go index beac34c..f2f093b 100644 --- a/freetype/raster/paint.go +++ b/freetype/raster/paint.go @@ -43,31 +43,33 @@ type AlphaPainter struct { // Paint satisfies the Painter interface by painting ss onto an image.Alpha. func (r *AlphaPainter) Paint(ss []Span, done bool) { + b := r.Image.Bounds() for _, s := range ss { - if s.Y < 0 { + if s.Y < b.Min.Y { continue } - if s.Y >= len(r.Image.Pixel) { + if s.Y >= b.Max.Y { return } - p := r.Image.Pixel[s.Y] - if s.X0 < 0 { - s.X0 = 0 + if s.X0 < b.Min.X { + s.X0 = b.Min.X } - if s.X1 > len(p) { - s.X1 = len(p) + if s.X1 > b.Max.X { + s.X1 = b.Max.X } + base := s.Y * r.Image.Stride + p := r.Image.Pix[base+s.X0 : base+s.X1] if r.Op == draw.Over { a := int(s.A >> 24) - for x := s.X0; x < s.X1; x++ { - ax := int(p[x].A) + for i, c := range p { + ax := int(c.A) ax = (ax*255 + (255-ax)*a) / 255 - p[x] = image.AlphaColor{uint8(ax)} + p[i] = image.AlphaColor{uint8(ax)} } } else { color := image.AlphaColor{uint8(s.A >> 24)} - for x := s.X0; x < s.X1; x++ { - p[x] = color + for i := range p { + p[i] = color } } } @@ -89,28 +91,29 @@ type RGBAPainter struct { // Paint satisfies the Painter interface by painting ss onto an image.RGBA. func (r *RGBAPainter) Paint(ss []Span, done bool) { + b := r.Image.Bounds() for _, s := range ss { - if s.Y < 0 { + if s.Y < b.Min.Y { continue } - if s.Y >= len(r.Image.Pixel) { + if s.Y >= b.Max.Y { return } - p := r.Image.Pixel[s.Y] - if s.X0 < 0 { - s.X0 = 0 + if s.X0 < b.Min.X { + s.X0 = b.Min.X } - if s.X1 > len(p) { - s.X1 = len(p) + if s.X1 > b.Max.X { + s.X1 = b.Max.X } - for x := s.X0; x < s.X1; x++ { + base := s.Y * r.Image.Stride + p := r.Image.Pix[base+s.X0 : base+s.X1] + for i, rgba := range p { // This code is duplicated from drawGlyphOver in $GOROOT/src/pkg/exp/draw/draw.go. // TODO(nigeltao): Factor out common code into a utility function, once the compiler // can inline such function calls. ma := s.A >> 16 const M = 1<<16 - 1 if r.Op == draw.Over { - rgba := p[x] dr := uint32(rgba.R) dg := uint32(rgba.G) db := uint32(rgba.B) @@ -121,13 +124,13 @@ func (r *RGBAPainter) Paint(ss []Span, done bool) { dg = (dg*a + r.cg*ma) / M db = (db*a + r.cb*ma) / M da = (da*a + r.ca*ma) / M - p[x] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)} + p[i] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)} } else { dr := r.cr * ma / M dg := r.cg * ma / M db := r.cb * ma / M da := r.ca * ma / M - p[x] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)} + p[i] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)} } } }