aboutsummaryrefslogtreecommitdiff
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.go23
1 files changed, 17 insertions, 6 deletions
diff --git a/src/pkg/image/image.go b/src/pkg/image/image.go
index bf75a51261..f4c38d28a6 100644
--- a/src/pkg/image/image.go
+++ b/src/pkg/image/image.go
@@ -563,14 +563,19 @@ func (p PalettedColorModel) Convert(c Color) Color {
if len(p) == 0 {
return nil
}
+ return p[p.Index(c)]
+}
+
+// Index returns the index of the palette color closest to c in Euclidean
+// R,G,B space.
+func (p PalettedColorModel) Index(c Color) int {
cr, cg, cb, _ := c.RGBA()
// Shift by 1 bit to avoid potential uint32 overflow in sum-squared-difference.
cr >>= 1
cg >>= 1
cb >>= 1
- result := Color(nil)
- bestSSD := uint32(1<<32 - 1)
- for _, v := range p {
+ ret, bestSSD := 0, uint32(1<<32-1)
+ for i, v := range p {
vr, vg, vb, _ := v.RGBA()
vr >>= 1
vg >>= 1
@@ -578,11 +583,10 @@ func (p PalettedColorModel) Convert(c Color) Color {
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
+ ret, bestSSD = i, ssd
}
}
- return result
+ return ret
}
// A Paletted is an in-memory image backed by a 2-D slice of uint8 values and a PalettedColorModel.
@@ -610,6 +614,13 @@ func (p *Paletted) At(x, y int) Color {
return p.Palette[p.Pix[y*p.Stride+x]]
}
+func (p *Paletted) Set(x, y int, c Color) {
+ if !(Point{x, y}.In(p.Rect)) {
+ return
+ }
+ p.Pix[y*p.Stride+x] = uint8(p.Palette.Index(c))
+}
+
func (p *Paletted) ColorIndexAt(x, y int) uint8 {
if !(Point{x, y}.In(p.Rect)) {
return 0