-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_handle.go
161 lines (126 loc) · 4.77 KB
/
image_handle.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
/*
* 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 (
"runtime"
)
// ImageHandle contains information about an image in a libheif Context.
type ImageHandle struct {
handle *C.struct_heif_image_handle
}
func freeHeifImageHandle(c *ImageHandle) {
C.heif_image_handle_release(c.handle)
c.handle = nil
}
// IsPrimaryImage checks if the image handle is for a primary image.
func (h *ImageHandle) IsPrimaryImage() bool {
defer runtime.KeepAlive(h)
return C.heif_image_handle_is_primary_image(h.handle) != 0
}
// GetWidth returns the width of the image handle.
func (h *ImageHandle) GetWidth() int {
defer runtime.KeepAlive(h)
return int(C.heif_image_handle_get_width(h.handle))
}
// GetHeight returns the height of the image handle.
func (h *ImageHandle) GetHeight() int {
defer runtime.KeepAlive(h)
return int(C.heif_image_handle_get_height(h.handle))
}
// HasAlphaChannel checks if the image handle has an alpha channel.
func (h *ImageHandle) HasAlphaChannel() bool {
defer runtime.KeepAlive(h)
return C.heif_image_handle_has_alpha_channel(h.handle) != 0
}
// HasDepthImage checks if the image handle has a depth channel.
func (h *ImageHandle) HasDepthImage() bool {
defer runtime.KeepAlive(h)
return C.heif_image_handle_has_depth_image(h.handle) != 0
}
// GetNumberOfDepthImages returns the number of depth images in the image handle.
func (h *ImageHandle) GetNumberOfDepthImages() int {
defer runtime.KeepAlive(h)
return int(C.heif_image_handle_get_number_of_depth_images(h.handle))
}
// GetListOfDepthImageIDs returns the list of depth image ids in the image handle.
func (h *ImageHandle) GetListOfDepthImageIDs() []int {
defer runtime.KeepAlive(h)
num := int(C.heif_image_handle_get_number_of_depth_images(h.handle))
if num == 0 {
return []int{}
}
origIDs := make([]C.heif_item_id, num)
C.heif_image_handle_get_list_of_depth_image_IDs(h.handle, &origIDs[0], C.int(num))
return convertItemIDs(origIDs, num)
}
// GetDepthImageHandle returns the image handle for the given depth image id.
func (h *ImageHandle) GetDepthImageHandle(depth_image_id int) (*ImageHandle, error) {
defer runtime.KeepAlive(h)
var handle ImageHandle
err := C.heif_image_handle_get_depth_image_handle(h.handle, C.heif_item_id(depth_image_id), &handle.handle)
if err := convertHeifError(err); err != nil {
return nil, err
}
runtime.SetFinalizer(&handle, freeHeifImageHandle)
return &handle, nil
}
// GetNumberOfThumbnails returns the number of thumbnails in the image handle.
func (h *ImageHandle) GetNumberOfThumbnails() int {
defer runtime.KeepAlive(h)
return int(C.heif_image_handle_get_number_of_thumbnails(h.handle))
}
// GetListOfThumbnailIDs returns the list of thumbnail ids in the image handle.
func (h *ImageHandle) GetListOfThumbnailIDs() []int {
defer runtime.KeepAlive(h)
num := int(C.heif_image_handle_get_number_of_thumbnails(h.handle))
if num == 0 {
return []int{}
}
origIDs := make([]C.heif_item_id, num)
C.heif_image_handle_get_list_of_thumbnail_IDs(h.handle, &origIDs[0], C.int(num))
return convertItemIDs(origIDs, num)
}
// GetThumbnail returns the image handle for the given thumbnail id.
func (h *ImageHandle) GetThumbnail(thumbnail_id int) (*ImageHandle, error) {
defer runtime.KeepAlive(h)
var handle ImageHandle
err := C.heif_image_handle_get_thumbnail(h.handle, C.heif_item_id(thumbnail_id), &handle.handle)
runtime.SetFinalizer(&handle, freeHeifImageHandle)
return &handle, convertHeifError(err)
}
// DecodeImage decodes the image to the provided colorspace and chroma.
func (h *ImageHandle) DecodeImage(colorspace Colorspace, chroma Chroma, options *DecodingOptions) (*Image, error) {
defer runtime.KeepAlive(h)
var image Image
var opt *C.struct_heif_decoding_options
if options != nil {
opt = options.options
}
err := C.heif_decode_image(h.handle, &image.image, uint32(colorspace), uint32(chroma), opt)
if err := convertHeifError(err); err != nil {
return nil, err
}
runtime.SetFinalizer(&image, freeHeifImage)
return &image, nil
}