提交 3657abe8 编写于 作者: N Nigel Tao

freetype: update for image representation change (1849041).

R=r, adg
CC=golang-dev
http://codereview.appspot.com/1850041
上级 2e8bf4f4
...@@ -88,7 +88,7 @@ func main() { ...@@ -88,7 +88,7 @@ func main() {
ruler = image.RGBAColor{0x22, 0x22, 0x22, 0xff} ruler = image.RGBAColor{0x22, 0x22, 0x22, 0xff}
} }
rgba := image.NewRGBA(640, 480) 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 := freetype.NewContext()
c.SetDPI(*dpi) c.SetDPI(*dpi)
c.SetFont(font) c.SetFont(font)
......
...@@ -22,9 +22,12 @@ func p(x, y int) raster.Point { ...@@ -22,9 +22,12 @@ func p(x, y int) raster.Point {
} }
func clear(m *image.Alpha) { func clear(m *image.Alpha) {
for y := 0; y < m.Height(); y++ { b := m.Bounds()
for x := 0; x < m.Width(); x++ { for y := b.Min.Y; y < b.Max.Y; y++ {
m.Pixel[y][x] = image.AlphaColor{0} base := y * m.Stride
p := m.Pix[base+b.Min.X : base+b.Max.X]
for i, _ := range p {
p[i] = image.AlphaColor{0}
} }
} }
} }
......
...@@ -113,7 +113,7 @@ func showNodes(m *image.RGBA, ns []node) { ...@@ -113,7 +113,7 @@ func showNodes(m *image.RGBA, ns []node) {
for _, n := range ns { for _, n := range ns {
p := p(n) p := p(n)
x, y := int(p.X)/256, int(p.Y)/256 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 continue
} }
var c image.Color var c image.Color
......
...@@ -31,9 +31,10 @@ func main() { ...@@ -31,9 +31,10 @@ func main() {
t := raster.Fix32(r * math.Tan(math.Pi/8)) t := raster.Fix32(r * math.Tan(math.Pi/8))
m := image.NewRGBA(800, 600) m := image.NewRGBA(800, 600)
for y := 0; y < m.Height(); y++ { for y := 0; y < 600; y++ {
for x := 0; x < m.Width(); x++ { p := m.Pix[y*m.Stride : y*m.Stride+800]
m.Pixel[y][x] = image.RGBAColor{63, 63, 63, 255} for i := range p {
p[i] = image.RGBAColor{63, 63, 63, 255}
} }
} }
mp := raster.NewRGBAPainter(m) mp := raster.NewRGBAPainter(m)
......
...@@ -43,31 +43,33 @@ type AlphaPainter struct { ...@@ -43,31 +43,33 @@ type AlphaPainter struct {
// Paint satisfies the Painter interface by painting ss onto an image.Alpha. // Paint satisfies the Painter interface by painting ss onto an image.Alpha.
func (r *AlphaPainter) Paint(ss []Span, done bool) { func (r *AlphaPainter) Paint(ss []Span, done bool) {
b := r.Image.Bounds()
for _, s := range ss { for _, s := range ss {
if s.Y < 0 { if s.Y < b.Min.Y {
continue continue
} }
if s.Y >= len(r.Image.Pixel) { if s.Y >= b.Max.Y {
return return
} }
p := r.Image.Pixel[s.Y] if s.X0 < b.Min.X {
if s.X0 < 0 { s.X0 = b.Min.X
s.X0 = 0
} }
if s.X1 > len(p) { if s.X1 > b.Max.X {
s.X1 = len(p) 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 { if r.Op == draw.Over {
a := int(s.A >> 24) a := int(s.A >> 24)
for x := s.X0; x < s.X1; x++ { for i, c := range p {
ax := int(p[x].A) ax := int(c.A)
ax = (ax*255 + (255-ax)*a) / 255 ax = (ax*255 + (255-ax)*a) / 255
p[x] = image.AlphaColor{uint8(ax)} p[i] = image.AlphaColor{uint8(ax)}
} }
} else { } else {
color := image.AlphaColor{uint8(s.A >> 24)} color := image.AlphaColor{uint8(s.A >> 24)}
for x := s.X0; x < s.X1; x++ { for i := range p {
p[x] = color p[i] = color
} }
} }
} }
...@@ -89,28 +91,29 @@ type RGBAPainter struct { ...@@ -89,28 +91,29 @@ type RGBAPainter struct {
// Paint satisfies the Painter interface by painting ss onto an image.RGBA. // Paint satisfies the Painter interface by painting ss onto an image.RGBA.
func (r *RGBAPainter) Paint(ss []Span, done bool) { func (r *RGBAPainter) Paint(ss []Span, done bool) {
b := r.Image.Bounds()
for _, s := range ss { for _, s := range ss {
if s.Y < 0 { if s.Y < b.Min.Y {
continue continue
} }
if s.Y >= len(r.Image.Pixel) { if s.Y >= b.Max.Y {
return return
} }
p := r.Image.Pixel[s.Y] if s.X0 < b.Min.X {
if s.X0 < 0 { s.X0 = b.Min.X
s.X0 = 0
} }
if s.X1 > len(p) { if s.X1 > b.Max.X {
s.X1 = len(p) 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. // 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 // TODO(nigeltao): Factor out common code into a utility function, once the compiler
// can inline such function calls. // can inline such function calls.
ma := s.A >> 16 ma := s.A >> 16
const M = 1<<16 - 1 const M = 1<<16 - 1
if r.Op == draw.Over { if r.Op == draw.Over {
rgba := p[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)
...@@ -121,13 +124,13 @@ func (r *RGBAPainter) Paint(ss []Span, done bool) { ...@@ -121,13 +124,13 @@ func (r *RGBAPainter) Paint(ss []Span, done bool) {
dg = (dg*a + r.cg*ma) / M dg = (dg*a + r.cg*ma) / M
db = (db*a + r.cb*ma) / M db = (db*a + r.cb*ma) / M
da = (da*a + r.ca*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 { } else {
dr := r.cr * ma / M dr := r.cr * ma / M
dg := r.cg * ma / M dg := r.cg * ma / M
db := r.cb * ma / M db := r.cb * ma / M
da := r.ca * 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)}
} }
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册