2009-08-26 22:51:03 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// The image package implements a basic 2-D image library.
|
|
|
|
package image
|
|
|
|
|
|
|
|
// An Image is a rectangular grid of Colors drawn from a ColorModel.
|
|
|
|
type Image interface {
|
2009-12-15 16:35:38 -07:00
|
|
|
ColorModel() ColorModel
|
|
|
|
Width() int
|
|
|
|
Height() int
|
2009-08-26 22:51:03 -06:00
|
|
|
// At(0, 0) returns the upper-left pixel of the grid.
|
|
|
|
// At(Width()-1, Height()-1) returns the lower-right pixel.
|
2009-12-15 16:35:38 -07:00
|
|
|
At(x, y int) Color
|
2009-08-26 22:51:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// An RGBA is an in-memory image backed by a 2-D slice of RGBAColor values.
|
|
|
|
type RGBA struct {
|
|
|
|
// The Pixel field's indices are y first, then x, so that At(x, y) == Pixel[y][x].
|
2009-12-15 16:35:38 -07:00
|
|
|
Pixel [][]RGBAColor
|
2009-08-26 22:51:03 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *RGBA) ColorModel() ColorModel { return RGBAColorModel }
|
2009-08-26 22:51:03 -06:00
|
|
|
|
|
|
|
func (p *RGBA) Width() int {
|
|
|
|
if len(p.Pixel) == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0
|
2009-08-26 22:51:03 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return len(p.Pixel[0])
|
2009-08-26 22:51:03 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *RGBA) Height() int { return len(p.Pixel) }
|
2009-08-26 22:51:03 -06:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *RGBA) At(x, y int) Color { return p.Pixel[y][x] }
|
2009-08-26 22:51:03 -06:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *RGBA) Set(x, y int, c Color) { p.Pixel[y][x] = toRGBAColor(c).(RGBAColor) }
|
2009-08-26 22:51:03 -06:00
|
|
|
|
2009-09-10 22:33:44 -06:00
|
|
|
// NewRGBA returns a new RGBA with the given width and height.
|
|
|
|
func NewRGBA(w, h int) *RGBA {
|
2010-05-23 12:12:00 -06:00
|
|
|
buf := make([]RGBAColor, w*h)
|
|
|
|
pix := make([][]RGBAColor, h)
|
|
|
|
for y := range pix {
|
|
|
|
pix[y] = buf[w*y : w*(y+1)]
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
2010-05-23 12:12:00 -06:00
|
|
|
return &RGBA{pix}
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
|
|
|
|
2009-08-26 22:51:03 -06:00
|
|
|
// An RGBA64 is an in-memory image backed by a 2-D slice of RGBA64Color values.
|
|
|
|
type RGBA64 struct {
|
|
|
|
// The Pixel field's indices are y first, then x, so that At(x, y) == Pixel[y][x].
|
2009-12-15 16:35:38 -07:00
|
|
|
Pixel [][]RGBA64Color
|
2009-08-26 22:51:03 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *RGBA64) ColorModel() ColorModel { return RGBA64ColorModel }
|
2009-08-26 22:51:03 -06:00
|
|
|
|
|
|
|
func (p *RGBA64) Width() int {
|
|
|
|
if len(p.Pixel) == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0
|
2009-08-26 22:51:03 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return len(p.Pixel[0])
|
2009-08-26 22:51:03 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *RGBA64) Height() int { return len(p.Pixel) }
|
2009-08-26 22:51:03 -06:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *RGBA64) At(x, y int) Color { return p.Pixel[y][x] }
|
2009-08-26 22:51:03 -06:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *RGBA64) Set(x, y int, c Color) { p.Pixel[y][x] = toRGBA64Color(c).(RGBA64Color) }
|
2009-08-26 22:51:03 -06:00
|
|
|
|
2009-09-10 22:33:44 -06:00
|
|
|
// NewRGBA64 returns a new RGBA64 with the given width and height.
|
|
|
|
func NewRGBA64(w, h int) *RGBA64 {
|
2010-05-23 12:12:00 -06:00
|
|
|
buf := make([]RGBA64Color, w*h)
|
|
|
|
pix := make([][]RGBA64Color, h)
|
|
|
|
for y := range pix {
|
|
|
|
pix[y] = buf[w*y : w*(y+1)]
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
2010-05-23 12:12:00 -06:00
|
|
|
return &RGBA64{pix}
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A NRGBA is an in-memory image backed by a 2-D slice of NRGBAColor values.
|
|
|
|
type NRGBA struct {
|
|
|
|
// The Pixel field's indices are y first, then x, so that At(x, y) == Pixel[y][x].
|
2009-12-15 16:35:38 -07:00
|
|
|
Pixel [][]NRGBAColor
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *NRGBA) ColorModel() ColorModel { return NRGBAColorModel }
|
2009-09-10 22:33:44 -06:00
|
|
|
|
|
|
|
func (p *NRGBA) Width() int {
|
|
|
|
if len(p.Pixel) == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return len(p.Pixel[0])
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *NRGBA) Height() int { return len(p.Pixel) }
|
2009-09-10 22:33:44 -06:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *NRGBA) At(x, y int) Color { return p.Pixel[y][x] }
|
2009-09-10 22:33:44 -06:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *NRGBA) Set(x, y int, c Color) { p.Pixel[y][x] = toNRGBAColor(c).(NRGBAColor) }
|
2009-09-10 22:33:44 -06:00
|
|
|
|
|
|
|
// NewNRGBA returns a new NRGBA with the given width and height.
|
|
|
|
func NewNRGBA(w, h int) *NRGBA {
|
2010-05-23 12:12:00 -06:00
|
|
|
buf := make([]NRGBAColor, w*h)
|
|
|
|
pix := make([][]NRGBAColor, h)
|
|
|
|
for y := range pix {
|
|
|
|
pix[y] = buf[w*y : w*(y+1)]
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
2010-05-23 12:12:00 -06:00
|
|
|
return &NRGBA{pix}
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A NRGBA64 is an in-memory image backed by a 2-D slice of NRGBA64Color values.
|
|
|
|
type NRGBA64 struct {
|
|
|
|
// The Pixel field's indices are y first, then x, so that At(x, y) == Pixel[y][x].
|
2009-12-15 16:35:38 -07:00
|
|
|
Pixel [][]NRGBA64Color
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *NRGBA64) ColorModel() ColorModel { return NRGBA64ColorModel }
|
2009-09-10 22:33:44 -06:00
|
|
|
|
|
|
|
func (p *NRGBA64) Width() int {
|
|
|
|
if len(p.Pixel) == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return len(p.Pixel[0])
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *NRGBA64) Height() int { return len(p.Pixel) }
|
2009-09-10 22:33:44 -06:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *NRGBA64) At(x, y int) Color { return p.Pixel[y][x] }
|
2009-09-10 22:33:44 -06:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *NRGBA64) Set(x, y int, c Color) { p.Pixel[y][x] = toNRGBA64Color(c).(NRGBA64Color) }
|
2009-09-10 22:33:44 -06:00
|
|
|
|
|
|
|
// NewNRGBA64 returns a new NRGBA64 with the given width and height.
|
|
|
|
func NewNRGBA64(w, h int) *NRGBA64 {
|
2010-05-23 12:12:00 -06:00
|
|
|
buf := make([]NRGBA64Color, w*h)
|
|
|
|
pix := make([][]NRGBA64Color, h)
|
|
|
|
for y := range pix {
|
|
|
|
pix[y] = buf[w*y : w*(y+1)]
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
2010-05-23 12:12:00 -06:00
|
|
|
return &NRGBA64{pix}
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
|
|
|
|
2010-02-02 00:38:04 -07:00
|
|
|
// An Alpha is an in-memory image backed by a 2-D slice 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Alpha) ColorModel() ColorModel { return AlphaColorModel }
|
|
|
|
|
|
|
|
func (p *Alpha) Width() int {
|
|
|
|
if len(p.Pixel) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return len(p.Pixel[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
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) }
|
|
|
|
|
|
|
|
// NewAlpha returns a new Alpha with the given width and height.
|
|
|
|
func NewAlpha(w, h int) *Alpha {
|
2010-05-23 12:12:00 -06:00
|
|
|
buf := make([]AlphaColor, w*h)
|
|
|
|
pix := make([][]AlphaColor, h)
|
|
|
|
for y := range pix {
|
|
|
|
pix[y] = buf[w*y : w*(y+1)]
|
2010-02-02 00:38:04 -07:00
|
|
|
}
|
2010-05-23 12:12:00 -06:00
|
|
|
return &Alpha{pix}
|
2010-02-02 00:38:04 -07:00
|
|
|
}
|
|
|
|
|
2009-08-26 22:51:03 -06:00
|
|
|
// A PalettedColorModel represents a fixed palette of colors.
|
2009-10-06 20:41:51 -06:00
|
|
|
type PalettedColorModel []Color
|
2009-08-26 22:51:03 -06:00
|
|
|
|
|
|
|
func diff(a, b uint32) uint32 {
|
|
|
|
if a > b {
|
2009-11-09 22:23:52 -07:00
|
|
|
return a - b
|
2009-08-26 22:51:03 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return b - a
|
2009-08-26 22:51:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert returns the palette color closest to c in Euclidean R,G,B space.
|
|
|
|
func (p PalettedColorModel) Convert(c Color) Color {
|
|
|
|
if len(p) == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2009-08-26 22:51:03 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
cr, cg, cb, _ := c.RGBA()
|
2010-05-28 13:59:21 -06:00
|
|
|
// Shift by 1 bit to avoid potential uint32 overflow in sum-squared-difference.
|
|
|
|
cr >>= 1
|
|
|
|
cg >>= 1
|
|
|
|
cb >>= 1
|
2009-12-15 16:35:38 -07:00
|
|
|
result := Color(nil)
|
|
|
|
bestSSD := uint32(1<<32 - 1)
|
2009-08-26 22:51:03 -06:00
|
|
|
for _, v := range p {
|
2009-12-15 16:35:38 -07:00
|
|
|
vr, vg, vb, _ := v.RGBA()
|
2010-05-28 13:59:21 -06:00
|
|
|
vr >>= 1
|
|
|
|
vg >>= 1
|
|
|
|
vb >>= 1
|
2009-12-15 16:35:38 -07:00
|
|
|
dr, dg, db := diff(cr, vr), diff(cg, vg), diff(cb, vb)
|
|
|
|
ssd := (dr * dr) + (dg * dg) + (db * db)
|
2009-08-26 22:51:03 -06:00
|
|
|
if ssd < bestSSD {
|
2009-12-15 16:35:38 -07:00
|
|
|
bestSSD = ssd
|
|
|
|
result = v
|
2009-08-26 22:51:03 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return result
|
2009-08-26 22:51:03 -06:00
|
|
|
}
|
|
|
|
|
2009-09-10 22:33:44 -06:00
|
|
|
// A Paletted is an in-memory image backed by a 2-D slice of uint8 values and a PalettedColorModel.
|
2009-08-26 22:51:03 -06:00
|
|
|
type Paletted struct {
|
|
|
|
// The Pixel field's indices are y first, then x, so that At(x, y) == Palette[Pixel[y][x]].
|
2009-12-15 16:35:38 -07:00
|
|
|
Pixel [][]uint8
|
|
|
|
Palette PalettedColorModel
|
2009-08-26 22:51:03 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *Paletted) ColorModel() ColorModel { return p.Palette }
|
2009-08-26 22:51:03 -06:00
|
|
|
|
|
|
|
func (p *Paletted) Width() int {
|
|
|
|
if len(p.Pixel) == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0
|
2009-08-26 22:51:03 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return len(p.Pixel[0])
|
2009-08-26 22:51:03 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *Paletted) Height() int { return len(p.Pixel) }
|
2009-08-26 22:51:03 -06:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (p *Paletted) At(x, y int) Color { return p.Palette[p.Pixel[y][x]] }
|
2009-09-10 22:33:44 -06:00
|
|
|
|
2009-09-20 20:08:03 -06:00
|
|
|
func (p *Paletted) ColorIndexAt(x, y int) uint8 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return p.Pixel[y][x]
|
2009-09-20 20:08:03 -06:00
|
|
|
}
|
|
|
|
|
2009-09-10 22:33:44 -06:00
|
|
|
func (p *Paletted) SetColorIndex(x, y int, index uint8) {
|
2009-11-09 13:07:39 -07:00
|
|
|
p.Pixel[y][x] = index
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPaletted returns a new Paletted with the given width, height and palette.
|
|
|
|
func NewPaletted(w, h int, m PalettedColorModel) *Paletted {
|
2010-05-23 12:12:00 -06:00
|
|
|
buf := make([]uint8, w*h)
|
|
|
|
pix := make([][]uint8, h)
|
|
|
|
for y := range pix {
|
|
|
|
pix[y] = buf[w*y : w*(y+1)]
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|
2010-05-23 12:12:00 -06:00
|
|
|
return &Paletted{pix, m}
|
2009-09-10 22:33:44 -06:00
|
|
|
}
|