-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_highlevel.go
346 lines (302 loc) · 9.41 KB
/
encode_highlevel.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* 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"
"image"
"runtime"
)
func imageFromRGBA(i *image.RGBA) (*Image, error) {
min := i.Bounds().Min
max := i.Bounds().Max
w := max.X - min.X
h := max.Y - min.Y
out, err := NewImage(w, h, ColorspaceRGB, ChromaInterleavedRGBA)
if err != nil {
return nil, fmt.Errorf("failed to create image: %v", err)
}
p, err := out.NewPlane(ChannelInterleaved, w, h, 8)
if err != nil {
return nil, fmt.Errorf("failed to add plane: %v", err)
}
p.setData([]byte(i.Pix), w*4)
return out, nil
}
func imageFromNRGBA(i *image.NRGBA) (*Image, error) {
min := i.Bounds().Min
max := i.Bounds().Max
w := max.X - min.X
h := max.Y - min.Y
out, err := NewImage(w, h, ColorspaceRGB, ChromaInterleavedRGBA)
if err != nil {
return nil, fmt.Errorf("failed to create image: %v", err)
}
p, err := out.NewPlane(ChannelInterleaved, w, h, 8)
if err != nil {
return nil, fmt.Errorf("failed to add plane: %v", err)
}
p.setData([]byte(i.Pix), w*4)
return out, nil
}
func imageFromRGBA64(i *image.RGBA64) (*Image, error) {
min := i.Bounds().Min
max := i.Bounds().Max
w := max.X - min.X
h := max.Y - min.Y
out, err := NewImage(w, h, ColorspaceRGB, ChromaInterleavedRRGGBBAA_BE)
if err != nil {
return nil, fmt.Errorf("failed to create image: %v", err)
}
p, err := out.NewPlane(ChannelInterleaved, w, h, 10)
if err != nil {
return nil, fmt.Errorf("failed to add plane: %v", err)
}
pix := make([]byte, w*h*8)
read_pos := 0
write_pos := 0
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
r := (uint16(i.Pix[read_pos]) << 8) | uint16(i.Pix[read_pos+1])
r = r >> 6
pix[write_pos] = byte(r >> 8)
pix[write_pos+1] = byte(r & 0xff)
read_pos += 2
g := (uint16(i.Pix[read_pos]) << 8) | uint16(i.Pix[read_pos+1])
g = g >> 6
pix[write_pos+2] = byte(g >> 8)
pix[write_pos+3] = byte(g & 0xff)
read_pos += 2
b := (uint16(i.Pix[read_pos]) << 8) | uint16(i.Pix[read_pos+1])
b = b >> 6
pix[write_pos+4] = byte(b >> 8)
pix[write_pos+5] = byte(b & 0xff)
read_pos += 2
a := (uint16(i.Pix[read_pos]) << 8) | uint16(i.Pix[read_pos+1])
a = a >> 6
pix[write_pos+6] = byte(a >> 8)
pix[write_pos+7] = byte(a & 0xff)
pix[write_pos+6] = byte(a >> 8)
pix[write_pos+7] = byte(a & 0xff)
read_pos += 2
write_pos += 8
}
}
p.setData(pix, w*8)
return out, nil
}
func imageFromGray(i *image.Gray) (*Image, error) {
min := i.Bounds().Min
max := i.Bounds().Max
w := max.X - min.X
h := max.Y - min.Y
out, err := NewImage(w, h, ColorspaceYCbCr, ChromaMonochrome)
if err != nil {
return nil, fmt.Errorf("failed to create image: %v", err)
}
const depth = 8
pY, err := out.NewPlane(ChannelY, w, h, depth)
if err != nil {
return nil, fmt.Errorf("failed to add Y plane: %v", err)
}
pY.setData([]byte(i.Pix), i.Stride)
return out, nil
}
func imageFromYCbCr(i *image.YCbCr) (*Image, error) {
min := i.Bounds().Min
max := i.Bounds().Max
w := max.X - min.X
h := max.Y - min.Y
var cm Chroma
switch sr := i.SubsampleRatio; sr {
case image.YCbCrSubsampleRatio420:
cm = Chroma420
case image.YCbCrSubsampleRatio422:
cm = Chroma422
case image.YCbCrSubsampleRatio444:
cm = Chroma444
default:
return nil, fmt.Errorf("unsupported subsample ratio: %s", sr.String())
}
out, err := NewImage(w, h, ColorspaceYCbCr, cm)
if err != nil {
return nil, fmt.Errorf("failed to create image: %v", err)
}
const depth = 8
pY, err := out.NewPlane(ChannelY, w, h, depth)
if err != nil {
return nil, fmt.Errorf("failed to add Y plane: %v", err)
}
pY.setData([]byte(i.Y), i.YStride)
switch cm {
case Chroma420:
halfW, halfH := (w+1)/2, (h+1)/2
pCb, err := out.NewPlane(ChannelCb, halfW, halfH, depth)
if err != nil {
return nil, fmt.Errorf("failed to add Cb plane: %v", err)
}
pCb.setData([]byte(i.Cb), i.CStride)
pCr, err := out.NewPlane(ChannelCr, halfW, halfH, depth)
if err != nil {
return nil, fmt.Errorf("failed to add Cr plane: %v", err)
}
pCr.setData([]byte(i.Cr), i.CStride)
case Chroma422:
halfW := (w + 1) / 2
pCb, err := out.NewPlane(ChannelCb, halfW, h, depth)
if err != nil {
return nil, fmt.Errorf("failed to add Cb plane: %v", err)
}
pCb.setData([]byte(i.Cb), i.CStride)
pCr, err := out.NewPlane(ChannelCr, halfW, h, depth)
if err != nil {
return nil, fmt.Errorf("failed to add Cr plane: %v", err)
}
pCr.setData([]byte(i.Cr), i.CStride)
case Chroma444:
pCb, err := out.NewPlane(ChannelCb, w, h, depth)
if err != nil {
return nil, fmt.Errorf("failed to add Cb plane: %v", err)
}
pCb.setData([]byte(i.Cb), i.CStride)
pCr, err := out.NewPlane(ChannelCr, w, h, depth)
if err != nil {
return nil, fmt.Errorf("failed to add Cr plane: %v", err)
}
pCr.setData([]byte(i.Cr), i.CStride)
}
return out, nil
}
// EncoderParameterSetter is a function that can configure an encoder.
type EncoderParameterSetter func(encoder *Encoder) error
// SetEncoderQuality returns a function that sets the quality of an encoder.
func SetEncoderQuality(quality int) EncoderParameterSetter {
return func(encoder *Encoder) error {
return encoder.SetQuality(quality)
}
}
// SetEncoderLossless returns a function that sets the lossless mode of an encoder.
func SetEncoderLossless(lossless LosslessMode) EncoderParameterSetter {
return func(encoder *Encoder) error {
return encoder.SetLossless(lossless)
}
}
// SetEncoderLoggingLevel returns a function that sets the logging level of an encoder.
func SetEncoderLoggingLevel(level LoggingLevel) EncoderParameterSetter {
return func(encoder *Encoder) error {
return encoder.SetLoggingLevel(level)
}
}
// SetEncoderParameterBool returns a function that sets a boolean parameter in an encoder.
func SetEncoderParameterBool(name string, value bool) EncoderParameterSetter {
return func(encoder *Encoder) error {
return encoder.SetParameterBool(name, value)
}
}
// SetEncoderParameterInteger returns a function that sets an integer parameter in an encoder.
func SetEncoderParameterInteger(name string, value int) EncoderParameterSetter {
return func(encoder *Encoder) error {
return encoder.SetParameterInteger(name, value)
}
}
// SetEncoderParameterString returns a function that sets a string parameter in an encoder.
func SetEncoderParameterString(name string, value string) EncoderParameterSetter {
return func(encoder *Encoder) error {
return encoder.SetParameterString(name, value)
}
}
// SetEncoderParameter returns a function that sets an arbitrary parameter in an encoder.
func SetEncoderParameter(name string, value string) EncoderParameterSetter {
return func(encoder *Encoder) error {
return encoder.SetParameter(name, value)
}
}
// EncodeFromImage is a high-level function to encode a Go Image to a new Context.
func EncodeFromImage(img image.Image, compression CompressionFormat, params ...EncoderParameterSetter) (*Context, error) {
if err := checkLibraryVersion(); err != nil {
return nil, err
}
var out *Image
switch i := img.(type) {
default:
return nil, fmt.Errorf("unsupported image type: %T", i)
case *image.RGBA:
tmp, err := imageFromRGBA(i)
if err != nil {
return nil, fmt.Errorf("failed to create image: %v", err)
}
out = tmp
case *image.NRGBA:
tmp, err := imageFromNRGBA(i)
if err != nil {
return nil, fmt.Errorf("failed to create image: %v", err)
}
out = tmp
case *image.RGBA64:
tmp, err := imageFromRGBA64(i)
if err != nil {
return nil, fmt.Errorf("failed to create image: %v", err)
}
out = tmp
case *image.Gray:
tmp, err := imageFromGray(i)
if err != nil {
return nil, fmt.Errorf("failed to create image: %v", err)
}
out = tmp
case *image.YCbCr:
tmp, err := imageFromYCbCr(i)
if err != nil {
return nil, fmt.Errorf("failed to create image: %v", err)
}
out = tmp
}
ctx, err := NewContext()
if err != nil {
return nil, fmt.Errorf("failed to create HEIF context: %v", err)
}
enc, err := ctx.NewEncoder(compression)
if err != nil {
return nil, fmt.Errorf("failed to create encoder: %v", err)
}
for _, param := range params {
if err := param(enc); err != nil {
return nil, fmt.Errorf("error setting parameter: %w", err)
}
}
encOpts, err := NewEncodingOptions()
if err != nil {
return nil, fmt.Errorf("failed to get encoding options: %v", err)
}
defer runtime.KeepAlive(ctx)
defer runtime.KeepAlive(out)
defer runtime.KeepAlive(enc)
defer runtime.KeepAlive(encOpts)
var handle ImageHandle
err2 := C.heif_context_encode_image(ctx.context, out.image, enc.encoder, encOpts.options, &handle.handle)
if err := convertHeifError(err2); err != nil {
return nil, fmt.Errorf("failed to encode image: %v", err)
}
runtime.SetFinalizer(&handle, freeHeifImageHandle)
return ctx, nil
}