From 2dea3087b392a8c9ba80a98b66a7dcc39ee280e3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 9 Oct 2025 16:52:13 +0530 Subject: [PATCH] Faster is_opaque implementation for paletted images --- tools/utils/images/opaque.go | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/tools/utils/images/opaque.go b/tools/utils/images/opaque.go index d0e72c605..6d62181dd 100644 --- a/tools/utils/images/opaque.go +++ b/tools/utils/images/opaque.go @@ -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: