forked from go-gl/glfw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
46 lines (37 loc) · 949 Bytes
/
util.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
package glfw
//#include <stdlib.h>
//#define GLFW_INCLUDE_NONE
//#include "glfw/include/GLFW/glfw3.h"
import "C"
import (
"image"
"image/draw"
)
func glfwbool(b C.int) bool {
return b == C.int(True)
}
func bytes(origin []byte) (pointer *uint8, free func()) {
n := len(origin)
if n == 0 {
return nil, func() {}
}
ptr := C.CBytes(origin)
return (*uint8)(ptr), func() { C.free(ptr) }
}
// imageToGLFW converts img to be compatible with C.GLFWimage.
func imageToGLFW(img image.Image) (r C.GLFWimage, free func()) {
b := img.Bounds()
r.width = C.int(b.Dx())
r.height = C.int(b.Dy())
var pixels []byte
if m, ok := img.(*image.NRGBA); ok && m.Stride == b.Dx()*4 {
pixels = m.Pix[:m.PixOffset(m.Rect.Min.X, m.Rect.Max.Y)]
} else {
m := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(m, m.Bounds(), img, b.Min, draw.Src)
pixels = m.Pix
}
pix, free := bytes(pixels)
r.pixels = (*C.uchar)(pix)
return r, free
}