forked from davidbyttow/govips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
139 lines (121 loc) · 3.57 KB
/
options.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
package vips
// #cgo pkg-config: vips
// #include "vips/vips.h"
import "C"
import (
"unsafe"
)
// Option is a type that is passed to internal libvips functions
type Option struct {
Name string
ref interface{}
gvalue C.GValue
closer func(gv *C.GValue)
output bool
}
// NewOption returns a new option instance
func NewOption(name string, gtype C.GType, output bool, closer func(gv *C.GValue)) *Option {
v := &Option{
Name: name,
output: output,
closer: closer,
}
C.g_value_init(&v.gvalue, gtype)
return v
}
// Output returns true if this option is an output-only option
func (v *Option) Output() bool {
return v.output
}
// Close releases memory associated with this option
func (v *Option) Close() {
if v.closer != nil {
v.closer(&v.gvalue)
}
C.g_value_unset(&v.gvalue)
}
// GValue returns the internal gvalue type
func (v *Option) GValue() *C.GValue {
return &v.gvalue
}
// InputBool represents a boolean input option
func InputBool(name string, v bool) *Option {
o := NewOption(name, C.G_TYPE_BOOLEAN, false, nil)
C.g_value_set_boolean(&o.gvalue, toGboolean(v))
return o
}
// OutputBool represents a boolean output option
func OutputBool(name string, v *bool) *Option {
o := NewOption(name, C.G_TYPE_BOOLEAN, true, func(gv *C.GValue) {
*v = fromGboolean(C.g_value_get_boolean(gv))
})
return o
}
// InputInt represents a int input option
func InputInt(name string, v int) *Option {
o := NewOption(name, C.G_TYPE_INT, false, nil)
C.g_value_set_int(&o.gvalue, C.gint(v))
return o
}
// OutputInt represents a int output option
func OutputInt(name string, v *int) *Option {
o := NewOption(name, C.G_TYPE_INT, true, func(gv *C.GValue) {
*v = int(C.g_value_get_int(gv))
})
return o
}
// InputDouble represents a float64 input option
func InputDouble(name string, v float64) *Option {
o := NewOption(name, C.G_TYPE_DOUBLE, false, nil)
C.g_value_set_double(&o.gvalue, C.gdouble(v))
return o
}
// OutputDouble represents a float output option
func OutputDouble(name string, v *float64) *Option {
o := NewOption(name, C.G_TYPE_DOUBLE, true, func(gv *C.GValue) {
*v = float64(C.g_value_get_double(gv))
})
return o
}
// InputString represents a string input option
func InputString(name string, v string) *Option {
cStr := C.CString(v)
o := NewOption(name, C.G_TYPE_STRING, false, func(gv *C.GValue) {
freeCString(cStr)
})
C.g_value_set_string(&o.gvalue, (*C.gchar)(cStr))
return o
}
// OutputString represents a string output option
func OutputString(name string, v *string) *Option {
o := NewOption(name, C.G_TYPE_STRING, true, func(gv *C.GValue) {
*v = C.GoString((*C.char)(unsafe.Pointer(C.g_value_get_string(gv))))
})
return o
}
// InputImage represents a VipsImage input option
func InputImage(name string, v *C.VipsImage) *Option {
o := NewOption(name, C.vips_image_get_type(), false, nil)
C.g_value_set_object(&o.gvalue, C.gpointer(v))
o.ref = v
return o
}
// OutputImage represents a VipsImage output option
func OutputImage(name string, v **C.VipsImage) *Option {
o := NewOption(name, C.vips_image_get_type(), true, func(gv *C.GValue) {
*v = (*C.VipsImage)(C.g_value_get_object(gv))
})
return o
}
// InputInterpolator represents a Interpolator input option
func InputInterpolator(name string, interp Interpolator) *Option {
cStr := C.CString(interp.String())
defer freeCString(cStr)
interpolator := C.vips_interpolate_new(cStr)
o := NewOption(name, C.vips_interpolate_get_type(), false, func(gv *C.GValue) {
defer C.g_object_unref(C.gpointer(interpolator))
})
C.g_value_set_object(&o.gvalue, C.gpointer(interpolator))
o.ref = interpolator
return o
}