-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimage.go
212 lines (172 loc) · 4.87 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* GML - Go QML
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Roland Singer <roland.singer[at]desertbit.com>
* Copyright (c) 2019 Sebastian Borchers <sebastian[at]desertbit.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package gml
// #include <gml.h>
import "C"
import (
"errors"
"fmt"
"image"
"image/draw"
"runtime"
"unsafe"
)
type Image struct {
freed bool
ptr C.gml_image
}
func NewImage() (img *Image) {
return newImage(C.gml_image_new(), true)
}
func newImage(imgC C.gml_image, free bool) (img *Image) {
// Check if something failed.
// This should never happen is signalizes a fatal error.
if imgC == nil {
panic(fmt.Errorf("gml image: C pointer is nil"))
}
img = &Image{
freed: !free,
ptr: imgC,
}
// Always free the C++ value if defined so.
if free {
runtime.SetFinalizer(img, freeImage)
}
return
}
func freeImage(img *Image) {
if img.freed {
return
}
img.freed = true
C.gml_image_free(img.ptr)
}
func (img *Image) Free() {
freeImage(img)
}
// Reset the image to an empty image.
func (img *Image) Reset() {
C.gml_image_reset(img.ptr)
// Prevent the GC from freeing. Go issue 13347
runtime.KeepAlive(img)
}
func (img *Image) Copy(dst *Image, x, y, width, height int) {
// Copy the image.
C.gml_image_copy(img.ptr, dst.ptr, C.int(x), C.int(y), C.int(width), C.int(height))
// Prevent the GC from freeing. Go issue 13347
runtime.KeepAlive(img)
runtime.KeepAlive(dst)
}
// SetTo performs a shallow copy.
func (img *Image) SetTo(other *Image) {
C.gml_image_set_to(img.ptr, other.ptr)
// Prevent the GC from freeing. Go issue 13347
runtime.KeepAlive(img)
runtime.KeepAlive(other)
}
func (img *Image) LoadFromGoImage(gimg image.Image) error {
b := gimg.Bounds()
// Get the RGBA image if present.
// Otherwise convert to RGBA.
imgRGBA, ok := gimg.(*image.RGBA)
if !ok {
imgRGBA = image.NewRGBA(b)
draw.Draw(imgRGBA, b, gimg, b.Min, draw.Src)
}
// Ensure the image is not empty.
if len(imgRGBA.Pix) == 0 {
return errors.New("empty rgba image")
}
apiErr := errorPool.Get()
defer errorPool.Put(apiErr)
ret := C.gml_image_load_from_rgba(
img.ptr,
(*C.char)(unsafe.Pointer(&imgRGBA.Pix[0])),
C.int(len(imgRGBA.Pix)),
C.int(b.Dx()),
C.int(b.Dy()),
C.int(imgRGBA.Stride),
apiErr.err,
)
if ret != 0 {
return apiErr.Err("failed to load from data")
}
// Prevent the GC from freeing. Go issue 13347
runtime.KeepAlive(img)
runtime.KeepAlive(gimg)
runtime.KeepAlive(imgRGBA)
return nil
}
func (img *Image) LoadFromFile(filename string) error {
if len(filename) == 0 {
return fmt.Errorf("empty filename")
}
filenameC := C.CString(filename)
defer C.free(unsafe.Pointer(filenameC))
apiErr := errorPool.Get()
defer errorPool.Put(apiErr)
ret := C.gml_image_load_from_file(img.ptr, filenameC, apiErr.err)
if ret != 0 {
return apiErr.Err("failed to load from file")
}
// Prevent the GC from freeing. Go issue 13347
runtime.KeepAlive(img)
return nil
}
func (img *Image) LoadFromData(data []byte) error {
if len(data) == 0 {
return fmt.Errorf("empty data")
}
apiErr := errorPool.Get()
defer errorPool.Put(apiErr)
ret := C.gml_image_load_from_data(img.ptr, (*C.char)(unsafe.Pointer(&data[0])), C.int(len(data)), apiErr.err)
if ret != 0 {
return apiErr.Err("failed to load from data")
}
// Prevent the GC from freeing. Go issue 13347
runtime.KeepAlive(img)
runtime.KeepAlive(data)
return nil
}
func (img *Image) Height() (height int) {
height = int(C.gml_image_height(img.ptr))
// Prevent the GC from freeing. Go issue 13347
runtime.KeepAlive(img)
return
}
func (img *Image) Width() (width int) {
width = int(C.gml_image_width(img.ptr))
// Prevent the GC from freeing. Go issue 13347
runtime.KeepAlive(img)
return
}
func (img *Image) IsEmpty() (empty bool) {
empty = (C.gml_image_is_empty(img.ptr) != 0)
// Prevent the GC from freeing. Go issue 13347
runtime.KeepAlive(img)
return
}