/
image.go
63 lines (54 loc) · 1.28 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package internal
import (
"image"
"image/draw"
"github.com/go-gl/gl/v4.6-core/gl"
"github.com/mokiat/lacking/framework/opengl"
"github.com/mokiat/lacking/ui"
)
func NewImage() *Image {
return &Image{
texture: opengl.NewTwoDTexture(),
size: ui.NewSize(0, 0),
}
}
type Image struct {
texture *opengl.TwoDTexture
size ui.Size
}
func (i *Image) Allocate(img image.Image) {
bounds := img.Bounds()
var rgbaImg *image.NRGBA
switch img := img.(type) {
case *image.NRGBA:
rgbaImg = img
default:
rgbaImg = image.NewNRGBA(bounds)
draw.Draw(rgbaImg, bounds, img, bounds.Min, draw.Src)
}
i.size = ui.NewSize(bounds.Dx(), bounds.Dy())
i.texture.Allocate(opengl.TwoDTextureAllocateInfo{
Width: int32(bounds.Dx()),
Height: int32(bounds.Dy()),
WrapS: gl.CLAMP_TO_EDGE,
WrapT: gl.CLAMP_TO_EDGE,
MinFilter: gl.LINEAR,
MagFilter: gl.LINEAR,
UseAnisotropy: false,
GenerateMipmaps: false,
InternalFormat: gl.SRGB8_ALPHA8,
DataFormat: gl.RGBA,
DataComponentType: gl.UNSIGNED_BYTE,
Data: rgbaImg.Pix,
})
}
func (i *Image) Release() {
i.size = ui.NewSize(0, 0)
i.texture.Release()
}
func (i *Image) Size() ui.Size {
return i.size
}
func (i *Image) Destroy() {
i.Release()
}