-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsurface.go
177 lines (124 loc) · 3.66 KB
/
surface.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
package cairo
// #cgo pkg-config: cairo cairo-gobject
// #include <stdlib.h>
// #include <string.h>
// #include <cairo.h>
// #include <cairo-gobject.h>
import "C"
import (
"runtime"
"unsafe"
)
type Surface struct {
surfaceNative *C.cairo_surface_t
}
func newSurface(surfaceNative *C.cairo_surface_t) (*Surface, error) {
err := checkCairoStatus(C.cairo_surface_status(surfaceNative))
if err != nil {
return nil, err
}
s := &Surface{surfaceNative}
runtime.SetFinalizer(s, (*Surface).destroy)
return s, nil
}
func (s *Surface) destroy() {
C.cairo_surface_destroy(s.surfaceNative)
}
func (s *Surface) Destroy() {
if s.surfaceNative == nil {
return
}
s.destroy()
s.surfaceNative = nil
runtime.SetFinalizer(s, nil)
}
func NewSurface(format Format, width, height int) (*Surface, error) {
surfaceNative := C.cairo_image_surface_create(C.cairo_format_t(format), C.int(width), C.int(height))
return newSurface(surfaceNative)
}
func NewSurfaceFromPNG(fileName string) (*Surface, error) {
cstr := newCString(fileName)
defer freeCString(cstr)
surfaceNative := C.cairo_image_surface_create_from_png(cstr)
return newSurface(surfaceNative)
}
func NewSurfaceNative(ptr uintptr) (*Surface, error) {
surfaceNative := (*C.cairo_surface_t)(unsafe.Pointer(ptr))
reference := C.cairo_surface_reference(surfaceNative)
return newSurface(reference)
}
func CreateSurfaceForData(data []byte, format Format, width, height, stride int) (*Surface, error) {
surfaceNative := C.cairo_image_surface_create_for_data(
(*C.uchar)(&data[0]),
C.cairo_format_t(format),
C.int(width),
C.int(height),
C.int(stride),
)
return newSurface(surfaceNative)
}
func (s *Surface) Native() uintptr {
return uintptr(unsafe.Pointer(s.surfaceNative))
}
func (s *Surface) Reference() *Surface {
reference := C.cairo_surface_reference(s.surfaceNative)
sr, _ := newSurface(reference)
return sr
}
func (s *Surface) Finish() {
C.cairo_surface_finish(s.surfaceNative)
}
func (s *Surface) WriteToPNG(fileName string) error {
cstr := newCString(fileName)
defer freeCString(cstr)
return checkCairoStatus(C.cairo_surface_write_to_png(s.surfaceNative, cstr))
}
func (s *Surface) GetFormat() Format {
return Format(C.cairo_image_surface_get_format(s.surfaceNative))
}
func (s *Surface) GetWidth() int {
return int(C.cairo_image_surface_get_width(s.surfaceNative))
}
func (s *Surface) GetHeight() int {
return int(C.cairo_image_surface_get_height(s.surfaceNative))
}
func (s *Surface) GetStride() int {
return int(C.cairo_image_surface_get_stride(s.surfaceNative))
}
func (s *Surface) Flush() {
C.cairo_surface_flush(s.surfaceNative)
}
func (s *Surface) MarkDirty() {
C.cairo_surface_mark_dirty(s.surfaceNative)
}
func (s *Surface) GetDataLength() int {
stride := s.GetStride()
height := s.GetHeight()
return stride * height
}
func (s *Surface) GetData(data []byte) error {
dataLen := s.GetDataLength()
if len(data) != dataLen {
return newCairoError("Surface.GetData(): invalid data size")
}
dataPtr := unsafe.Pointer(C.cairo_image_surface_get_data(s.surfaceNative))
if dataPtr == nil {
return newCairoError("Surface.GetData(): can't access surface pixel data")
}
C.memcpy(unsafe.Pointer(&data[0]), dataPtr, C.size_t(dataLen))
return nil
}
func (s *Surface) SetData(data []byte) error {
dataLen := s.GetDataLength()
if len(data) != dataLen {
return newCairoError("Surface.SetData(): invalid data size")
}
s.Flush()
dataPtr := unsafe.Pointer(C.cairo_image_surface_get_data(s.surfaceNative))
if dataPtr == nil {
return newCairoError("Surface.SetData(): can't access surface pixel data")
}
C.memcpy(dataPtr, unsafe.Pointer(&data[0]), C.size_t(dataLen))
s.MarkDirty()
return nil
}