package image
type Image interface {
ColorModel() ColorModel
Width() int
Height() int
At(x, y int) Color
}
type RGBA struct {
Pixel [][]RGBAColor
}
func (p *RGBA) ColorModel() ColorModel { return RGBAColorModel }
func (p *RGBA) Width() int {
if len(p.Pixel) == 0 {
return 0
}
return len(p.Pixel[0])
}
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) 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
}
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}
}
type RGBA64 struct {
Pixel [][]RGBA64Color
}
func (p *RGBA64) ColorModel() ColorModel { return RGBA64ColorModel }
func (p *RGBA64) Width() int {
if len(p.Pixel) == 0 {
return 0
}
return len(p.Pixel[0])
}
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) 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
}
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}
}
type NRGBA struct {
Pixel [][]NRGBAColor
}
func (p *NRGBA) ColorModel() ColorModel { return NRGBAColorModel }
func (p *NRGBA) Width() int {
if len(p.Pixel) == 0 {
return 0
}
return len(p.Pixel[0])
}
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) 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
}
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}
}
type NRGBA64 struct {
Pixel [][]NRGBA64Color
}
func (p *NRGBA64) ColorModel() ColorModel { return NRGBA64ColorModel }
func (p *NRGBA64) Width() int {
if len(p.Pixel) == 0 {
return 0
}
return len(p.Pixel[0])
}
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) 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
}
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}
}
type Alpha struct {
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) }
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
}
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}
}
type Alpha16 struct {
Pixel [][]Alpha16Color
}
func (p *Alpha16) ColorModel() ColorModel { return Alpha16ColorModel }
func (p *Alpha16) Width() int {
if len(p.Pixel) == 0 {
return 0
}
return len(p.Pixel[0])
}
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) 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
}
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)]
}
return &Alpha16{pix}
}
type PalettedColorModel []Color
func diff(a, b uint32) uint32 {
if a > b {
return a - b
}
return b - a
}
func (p PalettedColorModel) Convert(c Color) Color {
if len(p) == 0 {
return nil
}
cr, cg, cb, _ := c.RGBA()
cr >>= 1
cg >>= 1
cb >>= 1
result := Color(nil)
bestSSD := uint32(1<<32 - 1)
for _, v := range p {
vr, vg, vb, _ := v.RGBA()
vr >>= 1
vg >>= 1
vb >>= 1
dr, dg, db := diff(cr, vr), diff(cg, vg), diff(cb, vb)
ssd := (dr * dr) + (dg * dg) + (db * db)
if ssd < bestSSD {
bestSSD = ssd
result = v
}
}
return result
}
type Paletted struct {
Pixel [][]uint8
Palette PalettedColorModel
}
func (p *Paletted) ColorModel() ColorModel { return p.Palette }
func (p *Paletted) Width() int {
if len(p.Pixel) == 0 {
return 0
}
return len(p.Pixel[0])
}
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]
}
func (p *Paletted) SetColorIndex(x, y int, index uint8) {
p.Pixel[y][x] = index
}
func (p *Paletted) Opaque() bool {
for _, c := range p.Palette {
_, _, _, a := c.RGBA()
if a != 0xffff {
return false
}
}
return true
}
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}
}