]> Cypherpunks repositories - gostls13.git/commitdiff
image: tighten Paletted.Opaque to check only those palette entries
authorNigel Tao <nigeltao@golang.org>
Thu, 7 Jul 2011 06:32:19 +0000 (16:32 +1000)
committerNigel Tao <nigeltao@golang.org>
Thu, 7 Jul 2011 06:32:19 +0000 (16:32 +1000)
in the image, not all palette entries.

R=r
CC=golang-dev
https://golang.org/cl/4672049

src/pkg/image/image.go
src/pkg/image/image_test.go

index f4c38d28a6dab92e9f6ba04cf98264cfc94bc2a0..5ea302d0da526e27a0f2b29dc9ca68dc314b6d9e 100644 (file)
@@ -548,7 +548,7 @@ func NewGray16(w, h int) *Gray16 {
        return &Gray16{pix, w, Rectangle{ZP, Point{w, h}}}
 }
 
-// A PalettedColorModel represents a fixed palette of colors.
+// A PalettedColorModel represents a fixed palette of at most 256 colors.
 type PalettedColorModel []Color
 
 func diff(a, b uint32) uint32 {
@@ -648,7 +648,20 @@ func (p *Paletted) SubImage(r Rectangle) Image {
 
 // Opaque scans the entire image and returns whether or not it is fully opaque.
 func (p *Paletted) Opaque() bool {
-       for _, c := range p.Palette {
+       var present [256]bool
+       base := p.Rect.Min.Y * p.Stride
+       i0, i1 := base+p.Rect.Min.X, base+p.Rect.Max.X
+       for y := p.Rect.Min.Y; y < p.Rect.Max.Y; y++ {
+               for _, c := range p.Pix[i0:i1] {
+                       present[c] = true
+               }
+               i0 += p.Stride
+               i1 += p.Stride
+       }
+       for i, c := range p.Palette {
+               if !present[i] {
+                       continue
+               }
                _, _, _, a := c.RGBA()
                if a != 0xffff {
                        return false
index f167f7f28d8d808d18525b47f7ca662da27c21c4..5469d64230dec583270115eae15f88048e502bff 100644 (file)
@@ -10,6 +10,7 @@ import (
 
 type image interface {
        Image
+       Opaque() bool
        Set(int, int, Color)
        SubImage(Rectangle) Image
 }
@@ -49,6 +50,10 @@ func TestImage(t *testing.T) {
                        t.Errorf("%T: at (6, 3), want a non-zero color, got %v", m, m.At(6, 3))
                        continue
                }
+               if !m.SubImage(Rect(6, 3, 7, 4)).(image).Opaque() {
+                       t.Errorf("%T: at (6, 3) was not opaque", m)
+                       continue
+               }
                m = m.SubImage(Rect(3, 2, 9, 8)).(image)
                if !Rect(3, 2, 9, 8).Eq(m.Bounds()) {
                        t.Errorf("%T: sub-image want bounds %v, got %v", m, Rect(3, 2, 9, 8), m.Bounds())