-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture.go
49 lines (37 loc) · 1.03 KB
/
texture.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
package main
import (
"fmt"
"image"
"image/draw"
"image/jpeg"
"image/png"
"os"
)
func loadImage(filepath string) ([]uint8, int32, int32) {
// You can register another format here
image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
image.RegisterFormat("jpeg", "jpeg", jpeg.Decode, jpeg.DecodeConfig)
file, err := os.Open(filepath)
if err != nil {
fmt.Println("Error: File could not be opened")
os.Exit(1)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
fmt.Println("Error: Image could not be decoded")
os.Exit(1)
}
// b := img.Bounds()
// var pixels []float32
// for y := b.Min.Y; y < b.Max.Y; y++ {
// for x := b.Min.X; x < b.Max.X; x++ {
// r, g, b, a := img.At(x, y).RGBA()
// pixels = append(pixels, float32(r)/float32(a), float32(g)/float32(a), float32(b)/float32(a))
// }
// }
rect := img.Bounds()
rgba := image.NewRGBA(rect)
draw.Draw(rgba, rect, img, rect.Min, draw.Src)
return rgba.Pix, int32(rect.Max.X - rect.Min.X), int32(rect.Max.Y - rect.Min.Y)
}