-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
519 lines (487 loc) · 14.9 KB
/
image.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
* 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"
"unsafe"
)
// Image contains information on a libheif image. It is either returned when
// decoding images or can be creates to encode an image using libheif.
type Image struct {
image *C.struct_heif_image
}
// NewImage creates a new image to be used by libheif.
func NewImage(width, height int, colorspace Colorspace, chroma Chroma) (*Image, error) {
if err := checkLibraryVersion(); err != nil {
return nil, err
}
var image Image
err := C.heif_image_create(C.int(width), C.int(height), uint32(colorspace), uint32(chroma), &image.image)
if err := convertHeifError(err); err != nil {
return nil, err
}
runtime.SetFinalizer(&image, freeHeifImage)
return &image, nil
}
func freeHeifImage(image *Image) {
C.heif_image_release(image.image)
image.image = nil
}
// GetColorspace returns the colorspace of the image.
func (img *Image) GetColorspace() Colorspace {
defer runtime.KeepAlive(img)
return Colorspace(C.heif_image_get_colorspace(img.image))
}
// GetChromaFormat returns the chroma format of the image.
func (img *Image) GetChromaFormat() Chroma {
defer runtime.KeepAlive(img)
return Chroma(C.heif_image_get_chroma_format(img.image))
}
// GetWidth returns the width of the given channel of the image.
func (img *Image) GetWidth(channel Channel) int {
defer runtime.KeepAlive(img)
return int(C.heif_image_get_width(img.image, uint32(channel)))
}
// GetHeight returns the width of the given channel of the image.
func (img *Image) GetHeight(channel Channel) int {
defer runtime.KeepAlive(img)
return int(C.heif_image_get_height(img.image, uint32(channel)))
}
// GetBitsPerPixel returns the bits per pixel of the given channel of the image.
// Note that this is the number of bits used for storage of each pixel. Especially
// for HDR images this is probably not what you want, use "GetBitsPerPixelRange"
// instead.
func (img *Image) GetBitsPerPixel(channel Channel) int {
defer runtime.KeepAlive(img)
return int(C.heif_image_get_bits_per_pixel(img.image, uint32(channel)))
}
// GetBitsPerPixelRange returns the bits per pixel given channel of the image.
// This is the number of bits used for representing the pixel value, i.e. it will
// return "12" for 12bit HDR images (instead of "16" which would be the amount
// of bits used for storage).
func (img *Image) GetBitsPerPixelRange(channel Channel) int {
defer runtime.KeepAlive(img)
return int(C.heif_image_get_bits_per_pixel_range(img.image, uint32(channel)))
}
// GetImage convers the image to a Go Image object.
func (img *Image) GetImage() (image.Image, error) {
var i image.Image
cf := img.GetChromaFormat()
switch cs := img.GetColorspace(); cs {
case ColorspaceYCbCr:
var subsample image.YCbCrSubsampleRatio
switch cf {
case Chroma420:
subsample = image.YCbCrSubsampleRatio420
case Chroma422:
subsample = image.YCbCrSubsampleRatio422
case Chroma444:
subsample = image.YCbCrSubsampleRatio444
default:
return nil, fmt.Errorf("Unsupported YCbCr chroma format: %v", cf)
}
y, err := img.GetPlane(ChannelY)
if err != nil {
return nil, err
}
cb, err := img.GetPlane(ChannelCb)
if err != nil {
return nil, err
}
cr, err := img.GetPlane(ChannelCr)
if err != nil {
return nil, err
}
i = &image.YCbCr{
Y: y.Plane,
Cb: cb.Plane,
Cr: cr.Plane,
YStride: y.Stride,
CStride: cb.Stride,
SubsampleRatio: subsample,
Rect: image.Rectangle{
Min: image.Point{
X: 0,
Y: 0,
},
Max: image.Point{
X: img.GetWidth(ChannelY),
Y: img.GetHeight(ChannelY),
},
},
}
case ColorspaceRGB:
switch cf {
case Chroma444:
r, err := img.GetPlane(ChannelR)
if err != nil {
return nil, err
}
g, err := img.GetPlane(ChannelG)
if err != nil {
return nil, err
}
b, err := img.GetPlane(ChannelB)
if err != nil {
return nil, err
}
width := img.GetWidth(ChannelR)
height := img.GetHeight(ChannelR)
read_pos_r := 0
read_pos_g := 0
read_pos_b := 0
write_pos := 0
var rgba []byte
var stride int
if bpp := img.GetBitsPerPixelRange(ChannelR); bpp > 8 {
// NOTE: We only support the same bits per pixel on all components.
stride = width * 8
rgba = make([]byte, height*stride)
stride_add_r := r.Stride - width*2
stride_add_g := g.Stride - width*2
stride_add_b := b.Stride - width*2
if bpp == 16 {
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
rgba[write_pos] = r.Plane[read_pos_r]
rgba[write_pos+1] = r.Plane[read_pos_r+1]
rgba[write_pos+2] = g.Plane[read_pos_g]
rgba[write_pos+3] = g.Plane[read_pos_g+1]
rgba[write_pos+4] = b.Plane[read_pos_b]
rgba[write_pos+5] = b.Plane[read_pos_b+1]
rgba[write_pos+6] = 0xff
rgba[write_pos+7] = 0xff
read_pos_r += 2
read_pos_g += 2
read_pos_b += 2
write_pos += 8
}
read_pos_r += stride_add_r
read_pos_g += stride_add_g
read_pos_b += stride_add_b
}
} else {
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
r_value := (int16(r.Plane[read_pos_r+1]) << 8) | int16(r.Plane[read_pos_r])
r_value = (r_value << (16 - uint(bpp))) | (r_value >> (2*uint(bpp) - 16))
rgba[write_pos] = byte(r_value >> 8)
rgba[write_pos+1] = byte(r_value & 0xff)
g_value := (int16(g.Plane[read_pos_g+1]) << 8) | int16(g.Plane[read_pos_g])
g_value = (g_value << (16 - uint(bpp))) | (g_value >> (2*uint(bpp) - 16))
rgba[write_pos+2] = byte(g_value >> 8)
rgba[write_pos+3] = byte(g_value & 0xff)
b_value := (int16(b.Plane[read_pos_b+1]) << 8) | int16(b.Plane[read_pos_b])
b_value = (b_value << (16 - uint(bpp))) | (b_value >> (2*uint(bpp) - 16))
rgba[write_pos+4] = byte(b_value >> 8)
rgba[write_pos+5] = byte(b_value & 0xff)
rgba[write_pos+6] = 0xff
rgba[write_pos+7] = 0xff
read_pos_r += 2
read_pos_g += 2
read_pos_b += 2
write_pos += 8
}
read_pos_r += stride_add_r
read_pos_g += stride_add_g
read_pos_b += stride_add_b
}
}
i = &image.RGBA64{
Pix: rgba,
Stride: stride,
Rect: image.Rectangle{
Min: image.Point{
X: 0,
Y: 0,
},
Max: image.Point{
X: width,
Y: height,
},
},
}
} else {
stride = width * 4
rgba = make([]byte, height*stride)
stride_add_r := r.Stride - width
stride_add_g := g.Stride - width
stride_add_b := b.Stride - width
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
rgba[write_pos] = r.Plane[read_pos_r]
rgba[write_pos+1] = g.Plane[read_pos_g]
rgba[write_pos+2] = b.Plane[read_pos_b]
rgba[write_pos+3] = 0xff
read_pos_r++
read_pos_g++
read_pos_b++
write_pos += 4
}
read_pos_r += stride_add_r
read_pos_g += stride_add_g
read_pos_b += stride_add_b
}
i = &image.RGBA{
Pix: rgba,
Stride: stride,
Rect: image.Rectangle{
Min: image.Point{
X: 0,
Y: 0,
},
Max: image.Point{
X: width,
Y: height,
},
},
}
}
case ChromaInterleavedRGB:
rgb, err := img.GetPlane(ChannelInterleaved)
if err != nil {
return nil, err
}
width := img.GetWidth(ChannelInterleaved)
height := img.GetHeight(ChannelInterleaved)
rgba := make([]byte, width*height*4)
read_pos := 0
write_pos := 0
stride_add := rgb.Stride - width*3
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
rgba[write_pos] = rgb.Plane[read_pos]
rgba[write_pos+1] = rgb.Plane[read_pos+1]
rgba[write_pos+2] = rgb.Plane[read_pos+2]
rgba[write_pos+3] = 0xff
read_pos += 3
write_pos += 4
}
read_pos += stride_add
}
i = &image.RGBA{
Pix: rgba,
Stride: width * 4,
Rect: image.Rectangle{
Min: image.Point{
X: 0,
Y: 0,
},
Max: image.Point{
X: width,
Y: height,
},
},
}
case ChromaInterleavedRGBA:
rgba, err := img.GetPlane(ChannelInterleaved)
if err != nil {
return nil, err
}
i = &image.RGBA{
Pix: rgba.Plane,
Stride: rgba.Stride,
Rect: image.Rectangle{
Min: image.Point{
X: 0,
Y: 0,
},
Max: image.Point{
X: img.GetWidth(ChannelInterleaved),
Y: img.GetHeight(ChannelInterleaved),
},
},
}
case ChromaInterleavedRRGGBB_BE:
rgb, err := img.GetPlane(ChannelInterleaved)
if err != nil {
return nil, err
}
width := img.GetWidth(ChannelInterleaved)
height := img.GetHeight(ChannelInterleaved)
rgba := make([]byte, width*height*8)
read_pos := 0
write_pos := 0
stride_add := rgb.Stride - width*6
if bpp := img.GetBitsPerPixelRange(ChannelInterleaved); bpp != 16 {
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
r_value := (int16(rgb.Plane[read_pos]) << 8) | int16(rgb.Plane[read_pos+1])
r_value = (r_value << (16 - uint(bpp))) | (r_value >> (2*uint(bpp) - 16))
rgba[write_pos] = byte(r_value >> 8)
rgba[write_pos+1] = byte(r_value & 0xff)
g_value := (int16(rgb.Plane[read_pos+2]) << 8) | int16(rgb.Plane[read_pos+3])
g_value = (g_value << (16 - uint(bpp))) | (g_value >> (2*uint(bpp) - 16))
rgba[write_pos+2] = byte(g_value >> 8)
rgba[write_pos+3] = byte(g_value & 0xff)
b_value := (int16(rgb.Plane[read_pos+4]) << 8) | int16(rgb.Plane[read_pos+5])
b_value = (b_value << (16 - uint(bpp))) | (b_value >> (2*uint(bpp) - 16))
rgba[write_pos+4] = byte(b_value >> 8)
rgba[write_pos+5] = byte(b_value & 0xff)
rgba[write_pos+6] = 0xff
rgba[write_pos+7] = 0xff
read_pos += 6
write_pos += 8
}
read_pos += stride_add
}
} else {
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
rgba[write_pos] = rgb.Plane[read_pos]
rgba[write_pos+1] = rgb.Plane[read_pos+1]
rgba[write_pos+2] = rgb.Plane[read_pos+2]
rgba[write_pos+3] = rgb.Plane[read_pos+3]
rgba[write_pos+4] = rgb.Plane[read_pos+4]
rgba[write_pos+5] = rgb.Plane[read_pos+5]
rgba[write_pos+6] = 0xff
rgba[write_pos+7] = 0xff
read_pos += 6
write_pos += 8
}
read_pos += stride_add
}
}
i = &image.RGBA64{
Pix: rgba,
Stride: width * 4,
Rect: image.Rectangle{
Min: image.Point{
X: 0,
Y: 0,
},
Max: image.Point{
X: width,
Y: height,
},
},
}
case ChromaInterleavedRRGGBBAA_BE:
rgba, err := img.GetPlane(ChannelInterleaved)
if err != nil {
return nil, err
}
width := img.GetWidth(ChannelInterleaved)
height := img.GetHeight(ChannelInterleaved)
var plane []byte
if bpp := img.GetBitsPerPixelRange(ChannelInterleaved); bpp != 16 {
read_pos := 0
write_pos := 0
stride_add := rgba.Stride - width*8
plane = make([]byte, width*height*8)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
r_value := (int16(rgba.Plane[read_pos]) << 8) | int16(rgba.Plane[read_pos+1])
r_value = (r_value << (16 - uint(bpp))) | (r_value >> (2*uint(bpp) - 16))
plane[write_pos] = byte(r_value >> 8)
plane[write_pos+1] = byte(r_value & 0xff)
g_value := (int16(rgba.Plane[read_pos+2]) << 8) | int16(rgba.Plane[read_pos+3])
g_value = (g_value << (16 - uint(bpp))) | (g_value >> (2*uint(bpp) - 16))
plane[write_pos+2] = byte(g_value >> 8)
plane[write_pos+3] = byte(g_value & 0xff)
b_value := (int16(rgba.Plane[read_pos+4]) << 8) | int16(rgba.Plane[read_pos+5])
b_value = (b_value << (16 - uint(bpp))) | (b_value >> (2*uint(bpp) - 16))
plane[write_pos+4] = byte(b_value >> 8)
plane[write_pos+5] = byte(b_value & 0xff)
a_value := (int16(rgba.Plane[read_pos+6]) << 8) | int16(rgba.Plane[read_pos+7])
a_value = (a_value << (16 - uint(bpp))) | (a_value >> (2*uint(bpp) - 16))
plane[write_pos+6] = byte(a_value >> 8)
plane[write_pos+7] = byte(a_value & 0xff)
read_pos += 8
write_pos += 8
}
read_pos += stride_add
}
} else {
plane = rgba.Plane
}
i = &image.RGBA64{
Pix: plane,
Stride: rgba.Stride,
Rect: image.Rectangle{
Min: image.Point{
X: 0,
Y: 0,
},
Max: image.Point{
X: width,
Y: height,
},
},
}
default:
return nil, fmt.Errorf("Unsupported RGB chroma format: %v", cf)
}
default:
return nil, fmt.Errorf("Unsupported colorspace: %v", cs)
}
return i, nil
}
// GetPlane returns an ImageAccess object that can be used to access the raw
// pixel values of the given channel.
func (img *Image) GetPlane(channel Channel) (*ImageAccess, error) {
defer runtime.KeepAlive(img)
height := C.heif_image_get_height(img.image, uint32(channel))
if height == -1 {
return nil, fmt.Errorf("No such channel %v", channel)
}
var stride C.int
plane := C.heif_image_get_plane(img.image, uint32(channel), &stride)
if plane == nil {
return nil, fmt.Errorf("No such channel %v", channel)
}
ptr := unsafe.Pointer(plane)
size := stride * height
access := &ImageAccess{
Plane: C.GoBytes(ptr, size),
planePtr: ptr,
Stride: int(stride),
height: int(height),
image: img,
}
return access, nil
}
// NewPlane creates a new plane for the image. Use this to set the pixel values
// of the image to encode.
func (img *Image) NewPlane(channel Channel, width, height, depth int) (*ImageAccess, error) {
defer runtime.KeepAlive(img)
err := C.heif_image_add_plane(img.image, uint32(channel), C.int(width), C.int(height), C.int(depth))
if err := convertHeifError(err); err != nil {
return nil, err
}
return img.GetPlane(channel)
}
// ScaleImage scales the image to the given width and height.
func (img *Image) ScaleImage(width int, height int) (*Image, error) {
defer runtime.KeepAlive(img)
var scaled_image Image
err := C.heif_image_scale_image(img.image, &scaled_image.image, C.int(width), C.int(height), nil)
if err := convertHeifError(err); err != nil {
return nil, err
}
runtime.SetFinalizer(&scaled_image, freeHeifImage)
return &scaled_image, nil
}