diff options
author | Nigel Tao <nigeltao@golang.org> | 2010-06-03 17:18:26 -0700 |
---|---|---|
committer | Nigel Tao <nigeltao@golang.org> | 2010-06-03 17:18:26 -0700 |
commit | 5d08b8ce80933f22acb50b415092a060a9418111 (patch) | |
tree | aa4365a1b008008ed1238a0e79b7c27abf56f04e /src | |
parent | 0086eaafe84f703df233949a18f8e6c701f3e697 (diff) | |
download | golang-5d08b8ce80933f22acb50b415092a060a9418111.tar.gz |
Add Opaque method to the image types.
R=rsc
CC=golang-dev
http://codereview.appspot.com/1533041
Diffstat (limited to 'src')
-rw-r--r-- | src/pkg/image/image.go | 96 | ||||
-rw-r--r-- | src/pkg/image/names.go | 6 |
2 files changed, 102 insertions, 0 deletions
diff --git a/src/pkg/image/image.go b/src/pkg/image/image.go index 3ac7d4eb2..ba2c986a4 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 8defb0f05..0b621cff5 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 +} |