Faster is_opaque implementation for paletted images

This commit is contained in:
Kovid Goyal
2025-10-09 16:52:13 +05:30
parent c48ed15007
commit 2dea3087b3

View File

@@ -9,6 +9,35 @@ import (
var _ = fmt.Print
func paletted_is_opaque(p *image.Paletted) bool {
if len(p.Palette) > 256 {
return p.Opaque()
}
var is_alpha [256]bool
has_alpha := false
for i, c := range p.Palette {
_, _, _, a := c.RGBA()
if a != 0xffff {
is_alpha[i] = true
has_alpha = true
}
}
if !has_alpha {
return true
}
i0, i1 := 0, p.Rect.Dx()
for y := p.Rect.Min.Y; y < p.Rect.Max.Y; y++ {
for _, c := range p.Pix[i0:i1] {
if is_alpha[c] {
return false
}
}
i0 += p.Stride
i1 += p.Stride
}
return true
}
func IsOpaque(img image.Image) bool {
switch i := img.(type) {
case *image.RGBA:
@@ -30,7 +59,7 @@ func IsOpaque(img image.Image) bool {
case *image.CMYK:
return i.Opaque()
case *image.Paletted:
return i.Opaque()
return paletted_is_opaque(i)
case *image.Uniform:
return i.Opaque()
case *image.YCbCr: