aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNigel Tao <nigeltao@golang.org>2010-06-03 17:18:26 -0700
committerNigel Tao <nigeltao@golang.org>2010-06-03 17:18:26 -0700
commitc2e9f0c5625d3f365620aa420771f1dcd56b6666 (patch)
tree7648dcfde97875c0e31ebf0d07f595dcbed3fe9f
parent9a70762ac95fc6e6cba5f96e9363c7ea6bee5c62 (diff)
downloadgo-c2e9f0c5625d3f365620aa420771f1dcd56b6666.tar.gz
go-c2e9f0c5625d3f365620aa420771f1dcd56b6666.zip
Add Opaque method to the image types.
R=rsc CC=golang-dev https://golang.org/cl/1533041
-rw-r--r--src/pkg/image/image.go96
-rw-r--r--src/pkg/image/names.go6
2 files changed, 102 insertions, 0 deletions
diff --git a/src/pkg/image/image.go b/src/pkg/image/image.go
index 3ac7d4eb2a..ba2c986a4e 100644
--- a/src/pkg/image/image.go
+++ b/src/pkg/image/image.go
@@ -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)
diff --git a/src/pkg/image/names.go b/src/pkg/image/names.go
index 8defb0f055..0b621cff53 100644
--- a/src/pkg/image/names.go
+++ b/src/pkg/image/names.go
@@ -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
+}