-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_parameter.go
150 lines (130 loc) · 3.68 KB
/
encoder_parameter.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
/*
* Go interface to libheif
*
* Copyright (c) 2018-2024 struktur AG, Joachim Bauch <[email protected]>
*
* libheif is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* libheif is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libheif. If not, see <http://www.gnu.org/licenses/>.
*/
package libheif
// #cgo pkg-config: libheif
// #include <stdlib.h>
// #include <string.h>
// #include <libheif/heif.h>
import "C"
import (
"fmt"
"runtime"
"strings"
)
// EncoderParameter defines a parameter that can be set on an encoder.
type EncoderParameter struct {
param *C.struct_heif_encoder_parameter
}
func newEncoderParameter(p *C.struct_heif_encoder_parameter) EncoderParameter {
return EncoderParameter{
param: p,
}
}
// String returns information on the encoder parameter.
func (p EncoderParameter) String() string {
props := []string{
"name=" + p.Name(),
}
if min, max, values, err := p.IntegerValues(); err == nil {
if min != nil {
props = append(props,
fmt.Sprintf("min=%d", *min),
)
}
if max != nil {
props = append(props,
fmt.Sprintf("max=%d", *max),
)
}
if len(values) > 0 {
props = append(props,
fmt.Sprintf("values=%q", values),
)
}
}
if values, err := p.StringValues(); err == nil && len(values) > 0 {
props = append(props,
fmt.Sprintf("values=%q", values),
)
}
return fmt.Sprintf("EncoderParameter[%s]",
strings.Join(props, ", "),
)
}
// Name returns the name of the encoder parameter.
func (p EncoderParameter) Name() string {
defer runtime.KeepAlive(p)
return C.GoString(C.heif_encoder_parameter_get_name(p.param))
}
// Type returns the type of the encoder parameter.
func (p EncoderParameter) Type() EncoderParameterType {
defer runtime.KeepAlive(p)
return EncoderParameterType(C.heif_encoder_parameter_get_type(p.param))
}
// IntegerValues returns the minimum and maximum values (if present) or a list
// of allowed values (if defined).
func (p EncoderParameter) IntegerValues() (*int, *int, []int, error) {
defer runtime.KeepAlive(p)
var have_minimum C.int
var have_maximum C.int
var min C.int
var max C.int
var num_values C.int
var values_array *C.int
err := C.heif_encoder_parameter_get_valid_integer_values(p.param,
&have_minimum, &have_maximum,
&min, &max,
&num_values, &values_array,
)
if err := convertHeifError(err); err != nil {
return nil, nil, nil, err
}
var minValue *int
if have_minimum != 0 {
minValue = makePointer(int(min))
}
var maxValue *int
if have_maximum != 0 {
maxValue = makePointer(int(max))
}
var values []int
if num_values > 0 {
values = make([]int, num_values)
for i := 0; i < int(num_values); i++ {
values[i] = int(*values_array)
values_array = nextPointer(values_array)
}
}
return minValue, maxValue, values, nil
}
// StringValues returns a list of allowed values (if defined).
func (p EncoderParameter) StringValues() ([]string, error) {
defer runtime.KeepAlive(p)
var values_array **C.char
err := C.heif_encoder_parameter_get_valid_string_values(p.param, &values_array)
if err := convertHeifError(err); err != nil {
return nil, err
}
var values []string
for *values_array != nil {
values = append(values, C.GoString(*values_array))
values_array = nextPointer(values_array)
}
return values, nil
}