summaryrefslogtreecommitdiff
path: root/src/pkg/image/image.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/image/image.go')
-rw-r--r--src/pkg/image/image.go482
1 files changed, 299 insertions, 183 deletions
diff --git a/src/pkg/image/image.go b/src/pkg/image/image.go
index decf1ce43..c0e96e1f7 100644
--- a/src/pkg/image/image.go
+++ b/src/pkg/image/image.go
@@ -5,50 +5,67 @@
// The image package implements a basic 2-D image library.
package image
-// An Image is a rectangular grid of Colors drawn from a ColorModel.
+// A Config consists of an image's color model and dimensions.
+type Config struct {
+ ColorModel ColorModel
+ Width, Height int
+}
+
+// An Image is a finite rectangular grid of Colors drawn from a ColorModel.
type Image interface {
+ // ColorModel returns the Image's ColorModel.
ColorModel() ColorModel
- Width() int
- Height() int
- // At(0, 0) returns the upper-left pixel of the grid.
- // At(Width()-1, Height()-1) returns the lower-right pixel.
+ // Bounds returns the domain for which At can return non-zero color.
+ // The bounds do not necessarily contain the point (0, 0).
+ Bounds() Rectangle
+ // At returns the color of the pixel at (x, y).
+ // At(Bounds().Min.X, Bounds().Min.Y) returns the upper-left pixel of the grid.
+ // At(Bounds().Max.X-1, Bounds().Max.Y-1) returns the lower-right one.
At(x, y int) Color
}
-// An RGBA is an in-memory image backed by a 2-D slice of RGBAColor values.
+// An RGBA is an in-memory image of RGBAColor values.
type RGBA struct {
- // The Pixel field's indices are y first, then x, so that At(x, y) == Pixel[y][x].
- Pixel [][]RGBAColor
+ // Pix holds the image's pixels. The pixel at (x, y) is Pix[y*Stride+x].
+ Pix []RGBAColor
+ Stride int
+ // Rect is the image's bounds.
+ Rect Rectangle
}
func (p *RGBA) ColorModel() ColorModel { return RGBAColorModel }
-func (p *RGBA) Width() int {
- if len(p.Pixel) == 0 {
- return 0
+func (p *RGBA) Bounds() Rectangle { return p.Rect }
+
+func (p *RGBA) At(x, y int) Color {
+ if !p.Rect.Contains(Point{x, y}) {
+ return RGBAColor{}
}
- return len(p.Pixel[0])
+ return p.Pix[y*p.Stride+x]
}
-func (p *RGBA) Height() int { return len(p.Pixel) }
-
-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) }
+func (p *RGBA) Set(x, y int, c Color) {
+ if !p.Rect.Contains(Point{x, y}) {
+ return
+ }
+ p.Pix[y*p.Stride+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
- }
+ if p.Rect.Empty() {
+ return true
+ }
+ 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] {
+ if c.A != 0xff {
+ return false
}
}
+ i0 += p.Stride
+ i1 += p.Stride
}
return true
}
@@ -56,251 +73,343 @@ func (p *RGBA) Opaque() bool {
// NewRGBA returns a new RGBA with the given width and height.
func NewRGBA(w, h int) *RGBA {
buf := make([]RGBAColor, w*h)
- pix := make([][]RGBAColor, h)
- for y := range pix {
- pix[y] = buf[w*y : w*(y+1)]
- }
- return &RGBA{pix}
+ return &RGBA{buf, w, Rectangle{ZP, Point{w, h}}}
}
-// An RGBA64 is an in-memory image backed by a 2-D slice of RGBA64Color values.
+// An RGBA64 is an in-memory image of RGBA64Color values.
type RGBA64 struct {
- // The Pixel field's indices are y first, then x, so that At(x, y) == Pixel[y][x].
- Pixel [][]RGBA64Color
+ // Pix holds the image's pixels. The pixel at (x, y) is Pix[y*Stride+x].
+ Pix []RGBA64Color
+ Stride int
+ // Rect is the image's bounds.
+ Rect Rectangle
}
func (p *RGBA64) ColorModel() ColorModel { return RGBA64ColorModel }
-func (p *RGBA64) Width() int {
- if len(p.Pixel) == 0 {
- return 0
+func (p *RGBA64) Bounds() Rectangle { return p.Rect }
+
+func (p *RGBA64) At(x, y int) Color {
+ if !p.Rect.Contains(Point{x, y}) {
+ return RGBA64Color{}
}
- return len(p.Pixel[0])
+ return p.Pix[y*p.Stride+x]
}
-func (p *RGBA64) Height() int { return len(p.Pixel) }
-
-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) }
+func (p *RGBA64) Set(x, y int, c Color) {
+ if !p.Rect.Contains(Point{x, y}) {
+ return
+ }
+ p.Pix[y*p.Stride+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
- }
+ if p.Rect.Empty() {
+ return true
+ }
+ 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] {
+ if c.A != 0xffff {
+ return false
}
}
+ i0 += p.Stride
+ i1 += p.Stride
}
return true
}
// NewRGBA64 returns a new RGBA64 with the given width and height.
func NewRGBA64(w, h int) *RGBA64 {
- buf := make([]RGBA64Color, w*h)
- pix := make([][]RGBA64Color, h)
- for y := range pix {
- pix[y] = buf[w*y : w*(y+1)]
- }
- return &RGBA64{pix}
+ pix := make([]RGBA64Color, w*h)
+ return &RGBA64{pix, w, Rectangle{ZP, Point{w, h}}}
}
-// A NRGBA is an in-memory image backed by a 2-D slice of NRGBAColor values.
+// An NRGBA is an in-memory image of NRGBAColor values.
type NRGBA struct {
- // The Pixel field's indices are y first, then x, so that At(x, y) == Pixel[y][x].
- Pixel [][]NRGBAColor
+ // Pix holds the image's pixels. The pixel at (x, y) is Pix[y*Stride+x].
+ Pix []NRGBAColor
+ Stride int
+ // Rect is the image's bounds.
+ Rect Rectangle
}
func (p *NRGBA) ColorModel() ColorModel { return NRGBAColorModel }
-func (p *NRGBA) Width() int {
- if len(p.Pixel) == 0 {
- return 0
+func (p *NRGBA) Bounds() Rectangle { return p.Rect }
+
+func (p *NRGBA) At(x, y int) Color {
+ if !p.Rect.Contains(Point{x, y}) {
+ return NRGBAColor{}
}
- return len(p.Pixel[0])
+ return p.Pix[y*p.Stride+x]
}
-func (p *NRGBA) Height() int { return len(p.Pixel) }
-
-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) }
+func (p *NRGBA) Set(x, y int, c Color) {
+ if !p.Rect.Contains(Point{x, y}) {
+ return
+ }
+ p.Pix[y*p.Stride+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
- }
+ if p.Rect.Empty() {
+ return true
+ }
+ 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] {
+ if c.A != 0xff {
+ return false
}
}
+ i0 += p.Stride
+ i1 += p.Stride
}
return true
}
// NewNRGBA returns a new NRGBA with the given width and height.
func NewNRGBA(w, h int) *NRGBA {
- buf := make([]NRGBAColor, w*h)
- pix := make([][]NRGBAColor, h)
- for y := range pix {
- pix[y] = buf[w*y : w*(y+1)]
- }
- return &NRGBA{pix}
+ pix := make([]NRGBAColor, w*h)
+ return &NRGBA{pix, w, Rectangle{ZP, Point{w, h}}}
}
-// A NRGBA64 is an in-memory image backed by a 2-D slice of NRGBA64Color values.
+// An NRGBA64 is an in-memory image of NRGBA64Color values.
type NRGBA64 struct {
- // The Pixel field's indices are y first, then x, so that At(x, y) == Pixel[y][x].
- Pixel [][]NRGBA64Color
+ // Pix holds the image's pixels. The pixel at (x, y) is Pix[y*Stride+x].
+ Pix []NRGBA64Color
+ Stride int
+ // Rect is the image's bounds.
+ Rect Rectangle
}
func (p *NRGBA64) ColorModel() ColorModel { return NRGBA64ColorModel }
-func (p *NRGBA64) Width() int {
- if len(p.Pixel) == 0 {
- return 0
+func (p *NRGBA64) Bounds() Rectangle { return p.Rect }
+
+func (p *NRGBA64) At(x, y int) Color {
+ if !p.Rect.Contains(Point{x, y}) {
+ return NRGBA64Color{}
}
- return len(p.Pixel[0])
+ return p.Pix[y*p.Stride+x]
}
-func (p *NRGBA64) Height() int { return len(p.Pixel) }
-
-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) }
+func (p *NRGBA64) Set(x, y int, c Color) {
+ if !p.Rect.Contains(Point{x, y}) {
+ return
+ }
+ p.Pix[y*p.Stride+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
- }
+ if p.Rect.Empty() {
+ return true
+ }
+ 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] {
+ if c.A != 0xffff {
+ return false
}
}
+ i0 += p.Stride
+ i1 += p.Stride
}
return true
}
// NewNRGBA64 returns a new NRGBA64 with the given width and height.
func NewNRGBA64(w, h int) *NRGBA64 {
- buf := make([]NRGBA64Color, w*h)
- pix := make([][]NRGBA64Color, h)
- for y := range pix {
- pix[y] = buf[w*y : w*(y+1)]
- }
- return &NRGBA64{pix}
+ pix := make([]NRGBA64Color, w*h)
+ return &NRGBA64{pix, w, Rectangle{ZP, Point{w, h}}}
}
-// An Alpha is an in-memory image backed by a 2-D slice of AlphaColor values.
+// An Alpha is an in-memory image of AlphaColor values.
type Alpha struct {
- // The Pixel field's indices are y first, then x, so that At(x, y) == Pixel[y][x].
- Pixel [][]AlphaColor
+ // Pix holds the image's pixels. The pixel at (x, y) is Pix[y*Stride+x].
+ Pix []AlphaColor
+ Stride int
+ // Rect is the image's bounds.
+ Rect Rectangle
}
func (p *Alpha) ColorModel() ColorModel { return AlphaColorModel }
-func (p *Alpha) Width() int {
- if len(p.Pixel) == 0 {
- return 0
+func (p *Alpha) Bounds() Rectangle { return p.Rect }
+
+func (p *Alpha) At(x, y int) Color {
+ if !p.Rect.Contains(Point{x, y}) {
+ return AlphaColor{}
}
- return len(p.Pixel[0])
+ return p.Pix[y*p.Stride+x]
}
-func (p *Alpha) Height() int { return len(p.Pixel) }
-
-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) }
+func (p *Alpha) Set(x, y int, c Color) {
+ if !p.Rect.Contains(Point{x, y}) {
+ return
+ }
+ p.Pix[y*p.Stride+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
- }
+ if p.Rect.Empty() {
+ return true
+ }
+ 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] {
+ if c.A != 0xff {
+ return false
}
}
+ i0 += p.Stride
+ i1 += p.Stride
}
return true
}
// NewAlpha returns a new Alpha with the given width and height.
func NewAlpha(w, h int) *Alpha {
- buf := make([]AlphaColor, w*h)
- pix := make([][]AlphaColor, h)
- for y := range pix {
- pix[y] = buf[w*y : w*(y+1)]
- }
- return &Alpha{pix}
+ pix := make([]AlphaColor, w*h)
+ return &Alpha{pix, w, Rectangle{ZP, Point{w, h}}}
}
-// An Alpha16 is an in-memory image backed by a 2-D slice of Alpha16Color values.
+// An Alpha16 is an in-memory image of Alpha16Color values.
type Alpha16 struct {
- // The Pixel field's indices are y first, then x, so that At(x, y) == Pixel[y][x].
- Pixel [][]Alpha16Color
+ // Pix holds the image's pixels. The pixel at (x, y) is Pix[y*Stride+x].
+ Pix []Alpha16Color
+ Stride int
+ // Rect is the image's bounds.
+ Rect Rectangle
}
func (p *Alpha16) ColorModel() ColorModel { return Alpha16ColorModel }
-func (p *Alpha16) Width() int {
- if len(p.Pixel) == 0 {
- return 0
+func (p *Alpha16) Bounds() Rectangle { return p.Rect }
+
+func (p *Alpha16) At(x, y int) Color {
+ if !p.Rect.Contains(Point{x, y}) {
+ return Alpha16Color{}
}
- return len(p.Pixel[0])
+ return p.Pix[y*p.Stride+x]
}
-func (p *Alpha16) Height() int { return len(p.Pixel) }
-
-func (p *Alpha16) At(x, y int) Color { return p.Pixel[y][x] }
-
-func (p *Alpha16) Set(x, y int, c Color) { p.Pixel[y][x] = toAlpha16Color(c).(Alpha16Color) }
+func (p *Alpha16) Set(x, y int, c Color) {
+ if !p.Rect.Contains(Point{x, y}) {
+ return
+ }
+ p.Pix[y*p.Stride+x] = toAlpha16Color(c).(Alpha16Color)
+}
// Opaque scans the entire image and returns whether or not it is fully opaque.
func (p *Alpha16) 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
- }
+ if p.Rect.Empty() {
+ return true
+ }
+ 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] {
+ if c.A != 0xffff {
+ return false
}
}
+ i0 += p.Stride
+ i1 += p.Stride
}
return true
}
// NewAlpha16 returns a new Alpha16 with the given width and height.
func NewAlpha16(w, h int) *Alpha16 {
- buf := make([]Alpha16Color, w*h)
- pix := make([][]Alpha16Color, h)
- for y := range pix {
- pix[y] = buf[w*y : w*(y+1)]
+ pix := make([]Alpha16Color, w*h)
+ return &Alpha16{pix, w, Rectangle{ZP, Point{w, h}}}
+}
+
+// A Gray is an in-memory image of GrayColor values.
+type Gray struct {
+ // Pix holds the image's pixels. The pixel at (x, y) is Pix[y*Stride+x].
+ Pix []GrayColor
+ Stride int
+ // Rect is the image's bounds.
+ Rect Rectangle
+}
+
+func (p *Gray) ColorModel() ColorModel { return GrayColorModel }
+
+func (p *Gray) Bounds() Rectangle { return p.Rect }
+
+func (p *Gray) At(x, y int) Color {
+ if !p.Rect.Contains(Point{x, y}) {
+ return GrayColor{}
}
- return &Alpha16{pix}
+ return p.Pix[y*p.Stride+x]
+}
+
+func (p *Gray) Set(x, y int, c Color) {
+ if !p.Rect.Contains(Point{x, y}) {
+ return
+ }
+ p.Pix[y*p.Stride+x] = toGrayColor(c).(GrayColor)
+}
+
+// Opaque scans the entire image and returns whether or not it is fully opaque.
+func (p *Gray) Opaque() bool {
+ return true
+}
+
+// NewGray returns a new Gray with the given width and height.
+func NewGray(w, h int) *Gray {
+ pix := make([]GrayColor, w*h)
+ return &Gray{pix, w, Rectangle{ZP, Point{w, h}}}
+}
+
+// A Gray16 is an in-memory image of Gray16Color values.
+type Gray16 struct {
+ // Pix holds the image's pixels. The pixel at (x, y) is Pix[y*Stride+x].
+ Pix []Gray16Color
+ Stride int
+ // Rect is the image's bounds.
+ Rect Rectangle
+}
+
+func (p *Gray16) ColorModel() ColorModel { return Gray16ColorModel }
+
+func (p *Gray16) Bounds() Rectangle { return p.Rect }
+
+func (p *Gray16) At(x, y int) Color {
+ if !p.Rect.Contains(Point{x, y}) {
+ return Gray16Color{}
+ }
+ return p.Pix[y*p.Stride+x]
+}
+
+func (p *Gray16) Set(x, y int, c Color) {
+ if !p.Rect.Contains(Point{x, y}) {
+ return
+ }
+ p.Pix[y*p.Stride+x] = toGray16Color(c).(Gray16Color)
+}
+
+// Opaque scans the entire image and returns whether or not it is fully opaque.
+func (p *Gray16) Opaque() bool {
+ return true
+}
+
+// NewGray16 returns a new Gray16 with the given width and height.
+func NewGray16(w, h int) *Gray16 {
+ pix := make([]Gray16Color, w*h)
+ return &Gray16{pix, w, Rectangle{ZP, Point{w, h}}}
}
// A PalettedColorModel represents a fixed palette of colors.
@@ -342,30 +451,41 @@ func (p PalettedColorModel) Convert(c Color) Color {
// A Paletted is an in-memory image backed by a 2-D slice of uint8 values and a PalettedColorModel.
type Paletted struct {
- // The Pixel field's indices are y first, then x, so that At(x, y) == Palette[Pixel[y][x]].
- Pixel [][]uint8
+ // Pix holds the image's pixels. The pixel at (x, y) is Pix[y*Stride+x].
+ Pix []uint8
+ Stride int
+ // Rect is the image's bounds.
+ Rect Rectangle
+ // Palette is the image's palette.
Palette PalettedColorModel
}
func (p *Paletted) ColorModel() ColorModel { return p.Palette }
-func (p *Paletted) Width() int {
- if len(p.Pixel) == 0 {
- return 0
+func (p *Paletted) Bounds() Rectangle { return p.Rect }
+
+func (p *Paletted) At(x, y int) Color {
+ if len(p.Palette) == 0 {
+ return nil
+ }
+ if !p.Rect.Contains(Point{x, y}) {
+ return p.Palette[0]
}
- return len(p.Pixel[0])
+ return p.Palette[p.Pix[y*p.Stride+x]]
}
-func (p *Paletted) Height() int { return len(p.Pixel) }
-
-func (p *Paletted) At(x, y int) Color { return p.Palette[p.Pixel[y][x]] }
-
func (p *Paletted) ColorIndexAt(x, y int) uint8 {
- return p.Pixel[y][x]
+ if !p.Rect.Contains(Point{x, y}) {
+ return 0
+ }
+ return p.Pix[y*p.Stride+x]
}
func (p *Paletted) SetColorIndex(x, y int, index uint8) {
- p.Pixel[y][x] = index
+ if !p.Rect.Contains(Point{x, y}) {
+ return
+ }
+ p.Pix[y*p.Stride+x] = index
}
// Opaque scans the entire image and returns whether or not it is fully opaque.
@@ -381,10 +501,6 @@ func (p *Paletted) Opaque() bool {
// 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)
- pix := make([][]uint8, h)
- for y := range pix {
- pix[y] = buf[w*y : w*(y+1)]
- }
- return &Paletted{pix, m}
+ pix := make([]uint8, w*h)
+ return &Paletted{pix, w, Rectangle{ZP, Point{w, h}}, m}
}