| 1 |
package gfx |
| 2 |
|
| 3 |
import ( |
| 4 |
"image" |
| 5 |
"image/color" |
| 6 |
"image/draw" |
| 7 |
) |
| 8 |
|
| 9 |
// PalettedImage interface is implemented by *Paletted |
| 10 |
type PalettedImage interface { |
| 11 |
GfxPalette() Palette |
| 12 |
ColorPalette() color.Palette |
| 13 |
NRGBAAt(int, int) color.NRGBA |
| 14 |
AlphaAt(int, int) uint8 |
| 15 |
image.PalettedImage |
| 16 |
} |
| 17 |
|
| 18 |
// PalettedDrawImage interface is implemented by *Paletted |
| 19 |
type PalettedDrawImage interface { |
| 20 |
SetColorIndex(int, int, uint8) |
| 21 |
PalettedImage |
| 22 |
} |
| 23 |
|
| 24 |
// Paletted is an in-memory image of uint8 indices into a given palette. |
| 25 |
type Paletted struct { |
| 26 |
// Pix holds the image's pixels, as palette indices. The pixel at |
| 27 |
// (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*1]. |
| 28 |
Pix []uint8 |
| 29 |
// Stride is the Pix stride (in bytes) between vertically adjacent pixels. |
| 30 |
Stride int |
| 31 |
// Rect is the image's bounds. |
| 32 |
Rect image.Rectangle |
| 33 |
// Palette is the image's palette. |
| 34 |
Palette Palette |
| 35 |
} |
| 36 |
|
| 37 |
// NewPaletted returns a new paletted image with the given width, height and palette. |
| 38 |
func NewPaletted(w, h int, p Palette, colors ...color.Color) *Paletted { |
| 39 |
m := NewPalettedImage(IR(0, 0, w, h), p) |
| 40 |
|
| 41 |
if len(colors) > 0 { |
| 42 |
DrawSrc(m, m.Bounds(), NewUniform(colors[0]), ZP) |
| 43 |
} |
| 44 |
|
| 45 |
return m |
| 46 |
} |
| 47 |
|
| 48 |
// NewPalettedImage returns a new paletted image with the given bounds and palette. |
| 49 |
func NewPalettedImage(r image.Rectangle, p Palette) *Paletted { |
| 50 |
w, h := r.Dx(), r.Dy() |
| 51 |
|
| 52 |
pix := make([]uint8, 1*w*h) |
| 53 |
|
| 54 |
return &Paletted{ |
| 55 |
Pix: pix, |
| 56 |
Stride: 1 * w, |
| 57 |
Rect: r, |
| 58 |
Palette: p, |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// NewResizedPalettedImage returns an image with the provided dimensions. |
| 63 |
func NewResizedPalettedImage(src PalettedImage, w, h int) *Paletted { |
| 64 |
dst := NewPalettedImage(IR(0, 0, w, h), src.GfxPalette()) |
| 65 |
|
| 66 |
ResizeImage(dst, src) |
| 67 |
|
| 68 |
return dst |
| 69 |
} |
| 70 |
|
| 71 |
// NewScaledPalettedImage returns a paletted image scaled by the provided scaling factor. |
| 72 |
func NewScaledPalettedImage(src PalettedImage, s float64) *Paletted { |
| 73 |
b := src.Bounds() |
| 74 |
|
| 75 |
return NewResizedPalettedImage(src, int(float64(b.Dx())*s), int(float64(b.Dy())*s)) |
| 76 |
} |
| 77 |
|
| 78 |
// ColorModel returns the color model of the paletted image. |
| 79 |
func (p *Paletted) ColorModel() color.Model { |
| 80 |
return p.Palette |
| 81 |
} |
| 82 |
|
| 83 |
// Bounds returns the bounds of the paletted image. |
| 84 |
func (p *Paletted) Bounds() image.Rectangle { |
| 85 |
return p.Rect |
| 86 |
} |
| 87 |
|
| 88 |
// GfxPalette returns the gfx palette of the paletted image. |
| 89 |
func (p *Paletted) GfxPalette() Palette { |
| 90 |
return p.Palette |
| 91 |
} |
| 92 |
|
| 93 |
// ColorPalette returns the color palette of the paletted image. |
| 94 |
func (p *Paletted) ColorPalette() color.Palette { |
| 95 |
return p.Palette.AsColorPalette() |
| 96 |
} |
| 97 |
|
| 98 |
// At returns the color at (x, y). |
| 99 |
func (p *Paletted) At(x, y int) color.Color { |
| 100 |
return p.NRGBAAt(x, y) |
| 101 |
} |
| 102 |
|
| 103 |
// NRGBAAt returns the color.NRGBA at (x, y). |
| 104 |
func (p *Paletted) NRGBAAt(x, y int) color.NRGBA { |
| 105 |
i := p.ColorIndexAt(x, y) |
| 106 |
|
| 107 |
if int(i) >= len(p.Palette) { |
| 108 |
return p.Palette[0] |
| 109 |
} |
| 110 |
|
| 111 |
return p.Palette[i] |
| 112 |
} |
| 113 |
|
| 114 |
// AlphaAt returns the alpha value at (x, y). |
| 115 |
func (p *Paletted) AlphaAt(x, y int) uint8 { |
| 116 |
return p.Palette[p.ColorIndexAt(x, y)].A |
| 117 |
} |
| 118 |
|
| 119 |
// Pixels returns the pixels of the paletted image as a []uint8. |
| 120 |
func (p *Paletted) Pixels() []uint8 { |
| 121 |
pix := make([]uint8, len(p.Pix)*4) |
| 122 |
|
| 123 |
for i, n := range p.Pix { |
| 124 |
o := i * 4 |
| 125 |
c := p.Palette[int(n)] |
| 126 |
pix[o], pix[o+1], pix[o+2], pix[o+3] = c.R, c.G, c.B, c.A |
| 127 |
} |
| 128 |
|
| 129 |
return pix |
| 130 |
} |
| 131 |
|
| 132 |
// PixOffset returns the index of the first element of Pix |
| 133 |
// that corresponds to the pixel at (x, y). |
| 134 |
func (p *Paletted) PixOffset(x, y int) int { |
| 135 |
return y*p.Stride + x |
| 136 |
//return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*1 |
| 137 |
} |
| 138 |
|
| 139 |
// Set changes the color at (x, y). |
| 140 |
func (p *Paletted) Set(x, y int, c color.Color) { |
| 141 |
if !(image.Point{x, y}.In(p.Rect)) { |
| 142 |
return |
| 143 |
} |
| 144 |
|
| 145 |
i := p.PixOffset(x, y) |
| 146 |
|
| 147 |
p.Pix[i] = uint8(p.Palette.Index(c)) |
| 148 |
} |
| 149 |
|
| 150 |
// Index returns the color index at (x, y). (Short for ColorIndexAt) |
| 151 |
func (p *Paletted) Index(x, y int) uint8 { |
| 152 |
return p.ColorIndexAt(x, y) |
| 153 |
} |
| 154 |
|
| 155 |
// Put changes the color index at (x, y). (Short for SetColorIndex) |
| 156 |
func (p *Paletted) Put(x, y int, index uint8) { |
| 157 |
p.SetColorIndex(x, y, index) |
| 158 |
} |
| 159 |
|
| 160 |
// ColorIndexAt returns the color index at (x, y). |
| 161 |
func (p *Paletted) ColorIndexAt(x, y int) uint8 { |
| 162 |
if o := p.PixOffset(x, y); o < len(p.Pix) { |
| 163 |
return p.Pix[o] |
| 164 |
} |
| 165 |
|
| 166 |
return 0 |
| 167 |
} |
| 168 |
|
| 169 |
// SetColorIndex changes the color index at (x, y). |
| 170 |
func (p *Paletted) SetColorIndex(x, y int, index uint8) { |
| 171 |
p.Pix[p.PixOffset(x, y)] = index |
| 172 |
} |
| 173 |
|
| 174 |
// SubImage returns an image representing the portion of the image p visible |
| 175 |
// through r. The returned value shares pixels with the original image. |
| 176 |
func (p *Paletted) SubImage(r image.Rectangle) image.Image { |
| 177 |
r = r.Intersect(p.Rect) |
| 178 |
|
| 179 |
// If r1 and r2 are Rectangles, r1.Intersect(r2) is not guaranteed to be inside |
| 180 |
// either r1 or r2 if the intersection is empty. Without explicitly checking for |
| 181 |
// this, the Pix[i:] expression below can panic. |
| 182 |
if r.Empty() { |
| 183 |
return &Paletted{ |
| 184 |
Palette: p.Palette, |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
i := p.PixOffset(r.Min.X, r.Min.Y) |
| 189 |
|
| 190 |
return &Paletted{ |
| 191 |
Pix: p.Pix[i:], |
| 192 |
Stride: p.Stride, |
| 193 |
Rect: p.Rect.Intersect(r), |
| 194 |
Palette: p.Palette, |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
// Opaque scans the entire image and reports whether it is fully opaque. |
| 199 |
func (p *Paletted) Opaque() bool { |
| 200 |
var present [256]bool |
| 201 |
|
| 202 |
i0, i1 := 0, p.Rect.Dx() |
| 203 |
|
| 204 |
for y := p.Rect.Min.Y; y < p.Rect.Max.Y; y++ { |
| 205 |
for _, c := range p.Pix[i0:i1] { |
| 206 |
present[c] = true |
| 207 |
} |
| 208 |
|
| 209 |
i0 += p.Stride |
| 210 |
i1 += p.Stride |
| 211 |
} |
| 212 |
|
| 213 |
for i, c := range p.Palette { |
| 214 |
if !present[i] { |
| 215 |
continue |
| 216 |
} |
| 217 |
|
| 218 |
_, _, _, a := c.RGBA() |
| 219 |
|
| 220 |
if a != 0xffff { |
| 221 |
return false |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
return true |
| 226 |
} |
| 227 |
|
| 228 |
// Make sure that *PalettedImage implements these interfaces |
| 229 |
var ( |
| 230 |
_ PalettedImage = &Paletted{} |
| 231 |
_ draw.Image = &Paletted{} |
| 232 |
) |