-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoding_options.go
156 lines (132 loc) · 5.46 KB
/
decoding_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* 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 (
"errors"
"runtime"
"unsafe"
)
// DecodingOptions contain options that are used for decoding.
type DecodingOptions struct {
options *C.struct_heif_decoding_options
}
func freeHeifDecodingOptions(options *DecodingOptions) {
if options.options.decoder_id != nil {
C.free(unsafe.Pointer(options.options.decoder_id))
}
C.heif_decoding_options_free(options.options)
options.options = nil
}
// NewDecodingOptions creates new decoding options.
func NewDecodingOptions() (*DecodingOptions, error) {
if err := checkLibraryVersion(); err != nil {
return nil, err
}
options := &DecodingOptions{
options: C.heif_decoding_options_alloc(),
}
if options.options == nil {
return nil, errors.New("Could not allocate decoding options")
}
runtime.SetFinalizer(options, freeHeifDecodingOptions)
options.options.version = 5
options.options.color_conversion_options.version = 1
return options, nil
}
// SetIgnoreTransformations sets whether geometric transformations like
// cropping, rotation, mirroring should be ignored.
func (o *DecodingOptions) SetIgnoreTransformations(ignore bool) {
o.options.ignore_transformations = convertBool[C.uchar](ignore)
}
// GetIgnoreTransformations returns true if geometric transformations like
// cropping, rotation, mirroring should be ignored.
func (o *DecodingOptions) GetIgnoreTransformations() bool {
return o.options.ignore_transformations != 0
}
// SetConvertHDRTo8Bit defines whether HDR images should be converted to 8bit
// during decoding.
func (o *DecodingOptions) SetConvertHDRTo8Bit(convert bool) {
o.options.convert_hdr_to_8bit = convertBool[C.uchar](convert)
}
// GetConvertHDRTo8Bit returns true if HDR images will be converted to 8bit
// during decoding.
func (o *DecodingOptions) GetConvertHDRTo8Bit() bool {
return o.options.convert_hdr_to_8bit != 0
}
// SetStrictDecoding enabled strict decoding and an error is returned for
// invalid input. Otherwise, it will try its best and add decoding warnings
// to the decoded heif_image. Default is non-strict.
func (o *DecodingOptions) SetStrictDecoding(strict bool) {
o.options.strict_decoding = convertBool[C.uchar](strict)
}
// GetStrictDecoding returns true if strict decoding is enabled.
func (o *DecodingOptions) GetStrictDecoding() bool {
return o.options.strict_decoding != 0
}
// SetDecoderId sets the id of the decoder to use. If an empty id is specified
// (the default), the highest priority decoder is chosen.
// The priority is defined in the plugin.
func (o *DecodingOptions) SetDecoderId(decoder string) {
if o.options.decoder_id != nil {
C.free(unsafe.Pointer(o.options.decoder_id))
}
if decoder == "" {
o.options.decoder_id = nil
} else {
o.options.decoder_id = C.CString(decoder)
}
}
// GetDecoderId returns the decoder id that should be used.
func (o *DecodingOptions) GetDecoderId() string {
if o.options.decoder_id == nil {
return ""
}
return C.GoString(o.options.decoder_id)
}
// SetChromaDownsamplingAlgorithm sets the chroma downsampling algorithm to use.
func (o *DecodingOptions) SetChromaDownsamplingAlgorithm(algorithm ChromaDownsamplingAlgorithm) {
o.options.color_conversion_options.preferred_chroma_downsampling_algorithm = uint32(algorithm)
}
// GetChromaDownsamplingAlgorithm returns the chroma downsampling algorithm to use.
func (o *DecodingOptions) GetChromaDownsamplingAlgorithm() ChromaDownsamplingAlgorithm {
return ChromaDownsamplingAlgorithm(o.options.color_conversion_options.preferred_chroma_downsampling_algorithm)
}
// SetChromaUpsamplingAlgorithm sets the chroma upsampling algorithm to use.
func (o *DecodingOptions) SetChromaUpsamplingAlgorithm(algorithm ChromaUpsamplingAlgorithm) {
o.options.color_conversion_options.preferred_chroma_upsampling_algorithm = uint32(algorithm)
}
// GetChromaUpsamplingAlgorithm returns the chroma upsampling algorithm to use.
func (o *DecodingOptions) GetChromaUpsamplingAlgorithm() ChromaUpsamplingAlgorithm {
return ChromaUpsamplingAlgorithm(o.options.color_conversion_options.preferred_chroma_upsampling_algorithm)
}
// SetOnlyUsePreferredChromaAlgorithm enforces to use the preferred algorithm.
// If set to false, libheif may also use a different algorithm if the preferred
// one is not available.
func (o *DecodingOptions) SetOnlyUsePreferredChromaAlgorithm(preferred bool) {
o.options.color_conversion_options.only_use_preferred_chroma_algorithm = convertBool[C.uchar](preferred)
}
// GetOnlyUsePreferredChromaAlgorithm returns true if only the preferred chroma algorithm should be used
func (o *DecodingOptions) GetOnlyUsePreferredChromaAlgorithm() bool {
return o.options.color_conversion_options.only_use_preferred_chroma_algorithm != 0
}