]> Cypherpunks repositories - gostls13.git/commitdiff
Add Opaque method to the image types.
authorNigel Tao <nigeltao@golang.org>
Fri, 4 Jun 2010 00:18:26 +0000 (17:18 -0700)
committerNigel Tao <nigeltao@golang.org>
Fri, 4 Jun 2010 00:18:26 +0000 (17:18 -0700)
R=rsc
CC=golang-dev
https://golang.org/cl/1533041

src/pkg/image/image.go
src/pkg/image/names.go

index 3ac7d4eb2a8fe1a0464342ecfa0d594ec5e4d680..ba2c986a4ef9e9577182c8fcd99557bad5e835bd 100644 (file)
@@ -36,6 +36,23 @@ func (p *RGBA) At(x, y int) Color { return p.Pixel[y][x] }
 
 func (p *RGBA) Set(x, y int, c Color) { p.Pixel[y][x] = toRGBAColor(c).(RGBAColor) }
 
+// Opaque scans the entire image and returns whether or not it is fully opaque.
+func (p *RGBA) Opaque() bool {
+       h := len(p.Pixel)
+       if h > 0 {
+               w := len(p.Pixel[0])
+               for y := 0; y < h; y++ {
+                       pix := p.Pixel[y]
+                       for x := 0; x < w; x++ {
+                               if pix[x].A != 0xff {
+                                       return false
+                               }
+                       }
+               }
+       }
+       return true
+}
+
 // NewRGBA returns a new RGBA with the given width and height.
 func NewRGBA(w, h int) *RGBA {
        buf := make([]RGBAColor, w*h)
@@ -67,6 +84,23 @@ func (p *RGBA64) At(x, y int) Color { return p.Pixel[y][x] }
 
 func (p *RGBA64) Set(x, y int, c Color) { p.Pixel[y][x] = toRGBA64Color(c).(RGBA64Color) }
 
+// Opaque scans the entire image and returns whether or not it is fully opaque.
+func (p *RGBA64) Opaque() bool {
+       h := len(p.Pixel)
+       if h > 0 {
+               w := len(p.Pixel[0])
+               for y := 0; y < h; y++ {
+                       pix := p.Pixel[y]
+                       for x := 0; x < w; x++ {
+                               if pix[x].A != 0xffff {
+                                       return false
+                               }
+                       }
+               }
+       }
+       return true
+}
+
 // NewRGBA64 returns a new RGBA64 with the given width and height.
 func NewRGBA64(w, h int) *RGBA64 {
        buf := make([]RGBA64Color, w*h)
@@ -98,6 +132,23 @@ func (p *NRGBA) At(x, y int) Color { return p.Pixel[y][x] }
 
 func (p *NRGBA) Set(x, y int, c Color) { p.Pixel[y][x] = toNRGBAColor(c).(NRGBAColor) }
 
+// Opaque scans the entire image and returns whether or not it is fully opaque.
+func (p *NRGBA) Opaque() bool {
+       h := len(p.Pixel)
+       if h > 0 {
+               w := len(p.Pixel[0])
+               for y := 0; y < h; y++ {
+                       pix := p.Pixel[y]
+                       for x := 0; x < w; x++ {
+                               if pix[x].A != 0xff {
+                                       return false
+                               }
+                       }
+               }
+       }
+       return true
+}
+
 // NewNRGBA returns a new NRGBA with the given width and height.
 func NewNRGBA(w, h int) *NRGBA {
        buf := make([]NRGBAColor, w*h)
@@ -129,6 +180,23 @@ func (p *NRGBA64) At(x, y int) Color { return p.Pixel[y][x] }
 
 func (p *NRGBA64) Set(x, y int, c Color) { p.Pixel[y][x] = toNRGBA64Color(c).(NRGBA64Color) }
 
+// Opaque scans the entire image and returns whether or not it is fully opaque.
+func (p *NRGBA64) Opaque() bool {
+       h := len(p.Pixel)
+       if h > 0 {
+               w := len(p.Pixel[0])
+               for y := 0; y < h; y++ {
+                       pix := p.Pixel[y]
+                       for x := 0; x < w; x++ {
+                               if pix[x].A != 0xffff {
+                                       return false
+                               }
+                       }
+               }
+       }
+       return true
+}
+
 // NewNRGBA64 returns a new NRGBA64 with the given width and height.
 func NewNRGBA64(w, h int) *NRGBA64 {
        buf := make([]NRGBA64Color, w*h)
@@ -160,6 +228,23 @@ func (p *Alpha) At(x, y int) Color { return p.Pixel[y][x] }
 
 func (p *Alpha) Set(x, y int, c Color) { p.Pixel[y][x] = toAlphaColor(c).(AlphaColor) }
 
+// Opaque scans the entire image and returns whether or not it is fully opaque.
+func (p *Alpha) Opaque() bool {
+       h := len(p.Pixel)
+       if h > 0 {
+               w := len(p.Pixel[0])
+               for y := 0; y < h; y++ {
+                       pix := p.Pixel[y]
+                       for x := 0; x < w; x++ {
+                               if pix[x].A != 0xff {
+                                       return false
+                               }
+                       }
+               }
+       }
+       return true
+}
+
 // NewAlpha returns a new Alpha with the given width and height.
 func NewAlpha(w, h int) *Alpha {
        buf := make([]AlphaColor, w*h)
@@ -235,6 +320,17 @@ func (p *Paletted) SetColorIndex(x, y int, index uint8) {
        p.Pixel[y][x] = index
 }
 
+// Opaque scans the entire image and returns whether or not it is fully opaque.
+func (p *Paletted) Opaque() bool {
+       for _, c := range p.Palette {
+               _, _, _, a := c.RGBA()
+               if a != 0xffff {
+                       return false
+               }
+       }
+       return true
+}
+
 // NewPaletted returns a new Paletted with the given width, height and palette.
 func NewPaletted(w, h int, m PalettedColorModel) *Paletted {
        buf := make([]uint8, w*h)
index 8defb0f0558ee906060eef91cdc2287936438782..0b621cff53048779765654e9a0b0b041cbce0c70 100644 (file)
@@ -48,3 +48,9 @@ func (c ColorImage) Width() int { return 1e9 }
 func (c ColorImage) Height() int { return 1e9 }
 
 func (c ColorImage) At(x, y int) Color { return c.C }
+
+// Opaque scans the entire image and returns whether or not it is fully opaque.
+func (c ColorImage) Opaque() bool {
+       _, _, _, a := c.C.RGBA()
+       return a == 0xffff
+}