-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
45 lines (34 loc) · 909 Bytes
/
image.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
package camalian
import (
"image"
"os"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
)
// The Image construct which implements camalian.Quantizeable interface
type Image struct {
FilePath string
}
func (i *Image) Pixels() []RGB {
existingImageFile, err := os.Open(i.FilePath)
if err != nil {
panic(err)
}
defer existingImageFile.Close()
imageData, _, err := image.Decode(existingImageFile)
if err != nil {
panic(err)
}
bounds := imageData.Bounds()
var colors = make([]RGB, (bounds.Max.X * bounds.Max.Y))
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, _ := imageData.At(x, y).RGBA()
// A color's RGBA method returns values in the range [0, 65535].
// Shifting by 8 reduces this to the range [0, 255].
colors[x+(y*bounds.Max.X)] = RGB{R: uint8(r >> 8), G: uint8(g >> 8), B: uint8(b >> 8)}
}
}
return colors
}