-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmisc.go
66 lines (62 loc) · 1.63 KB
/
misc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package imagequant
// Collection of miscellaneous functions.
import (
"image"
"image/color"
)
// NRGBA converts a premultiplied color back to a normalized color with each component in range [0, 255].
func NRGBA(col color.Color) (r, g, b, a byte) {
if nrgba, ok := col.(color.NRGBA); ok {
r, g, b, a = nrgba.R, nrgba.G, nrgba.B, nrgba.A
} else {
pr, pg, pb, pa := col.RGBA()
pa >>= 8
if pa > 0 {
pr >>= 8
pr *= 0xff
pr /= pa
pg >>= 8
pg *= 0xff
pg /= pa
pb >>= 8
pb *= 0xff
pb /= pa
}
r = byte(pr)
g = byte(pg)
b = byte(pb)
a = byte(pa)
}
return
}
// imageToBytes32 converts the given image into a 32-bit byte array.
func imageToBytes32(img image.Image) []byte {
w := img.Bounds().Dx()
h := img.Bounds().Dy()
retVal := make([]byte, w*h*4)
dofs := 0
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
r, g, b, a := img.At(x, y).RGBA()
retVal[dofs] = byte(r >> 8)
retVal[dofs+1] = byte(g >> 8)
retVal[dofs+2] = byte(b >> 8)
retVal[dofs+3] = byte(a >> 8)
dofs += 4
}
}
return retVal
}
// bytesToPaletted creates a image.Paletted object out of the given palette and pixel data.
func bytesToPaletted(width, height int, palette color.Palette, pixels []byte) *image.Paletted {
retVal := image.NewPaletted(image.Rect(0, 0, width, height), palette)
y0, y1 := retVal.Bounds().Min.Y, retVal.Bounds().Max.Y
sofs := 0
dofs := y0 * retVal.Stride
for y := y0; y < y1; y++ {
copy(retVal.Pix[dofs:dofs+retVal.Stride], pixels[sofs:sofs+width])
sofs += width
dofs += retVal.Stride
}
return retVal
}