implemented FillImageMask in software backend

This commit is contained in:
Thomas Friedel 2019-04-16 16:33:55 +02:00
parent faf179caa0
commit 7fa1f9096f
6 changed files with 37 additions and 5 deletions

View file

@ -20,7 +20,7 @@ type Backend interface {
Clear(pts [4][2]float64)
Fill(style *FillStyle, pts [][2]float64)
DrawImage(dimg Image, sx, sy, sw, sh float64, pts [4][2]float64, alpha float64)
FillImageMask(style *FillStyle, mask *image.Alpha, pts [][2]float64) // pts must have four points
FillImageMask(style *FillStyle, mask *image.Alpha, pts [4][2]float64) // pts must have four points
ClearClip()
Clip(pts [][2]float64)

View file

@ -146,7 +146,7 @@ func (b *GoGLBackend) Fill(style *backendbase.FillStyle, pts [][2]float64) {
}
}
func (b *GoGLBackend) FillImageMask(style *backendbase.FillStyle, mask *image.Alpha, pts [][2]float64) {
func (b *GoGLBackend) FillImageMask(style *backendbase.FillStyle, mask *image.Alpha, pts [4][2]float64) {
b.activate()
w, h := mask.Rect.Dx(), mask.Rect.Dy()

View file

@ -1,6 +1,7 @@
package softwarebackend
import (
"image"
"image/color"
"github.com/tfriedel6/canvas/backend/backendbase"
@ -28,6 +29,35 @@ func (b *SoftwareBackend) Fill(style *backendbase.FillStyle, pts [][2]float64) {
})
}
func mix(col color.Color, alpha color.Alpha) color.RGBA {
ir, ig, ib, ia := col.RGBA()
a2 := float64(alpha.A) / 255.0
r := float64(ir) * a2 / 65535.0
g := float64(ig) * a2 / 65535.0
b := float64(ib) * a2 / 65535.0
a := float64(ia) * a2 / 65535.0
return color.RGBA{
R: uint8(r * 255.0),
G: uint8(g * 255.0),
B: uint8(b * 255.0),
A: uint8(a * 255.0),
}
}
func (b *SoftwareBackend) FillImageMask(style *backendbase.FillStyle, mask *image.Alpha, pts [4][2]float64) {
mw := float64(mask.Bounds().Dx())
mh := float64(mask.Bounds().Dy())
b.fillQuad(pts, func(x, y int, sx2, sy2 float64) {
sxi := int(mw*sx2 + 0.5)
syi := int(mh*sy2 + 0.5)
a := mask.AlphaAt(sxi, syi)
if a.A == 0 {
return
}
b.Image.SetRGBA(x, y, mix(style.Color, a))
})
}
func (b *SoftwareBackend) ClearClip() {
p := b.clip.Pix
for i := range p {

View file

@ -146,7 +146,7 @@ func (b *XMobileBackend) Fill(style *backendbase.FillStyle, pts [][2]float64) {
}
}
func (b *XMobileBackend) FillImageMask(style *backendbase.FillStyle, mask *image.Alpha, pts [][2]float64) {
func (b *XMobileBackend) FillImageMask(style *backendbase.FillStyle, mask *image.Alpha, pts [4][2]float64) {
b.activate()
w, h := mask.Rect.Dx(), mask.Rect.Dy()

View file

@ -34,7 +34,9 @@ func (cv *Canvas) drawShadow(pts [][2]float64, mask *image.Alpha) {
if len(cv.shadowBuf) != 4 {
panic("invalid number of points to fill with mask, must be 4")
}
cv.b.FillImageMask(&style, mask, cv.shadowBuf)
var quad [4][2]float64
copy(quad[:], cv.shadowBuf)
cv.b.FillImageMask(&style, mask, quad)
} else {
cv.b.Fill(&style, cv.shadowBuf)
}

View file

@ -218,7 +218,7 @@ func (cv *Canvas) FillText(str string, x, y float64) {
cv.drawShadow(pts[:], mask)
stl := cv.backendFillStyle(&cv.state.fill, 1)
cv.b.FillImageMask(&stl, mask, pts[:])
cv.b.FillImageMask(&stl, mask, pts)
}
// StrokeText draws the given string at the given coordinates