forked from kolesa-team/goexiv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exiv.go
317 lines (256 loc) · 6.57 KB
/
exiv.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
package goexiv
// #cgo pkg-config: exiv2
// #include "helper.h"
// #include <stdlib.h>
import "C"
import (
"errors"
"runtime"
"unsafe"
)
type Error struct {
code int
what string
}
type Image struct {
bytesArrayPtr unsafe.Pointer
img *C.Exiv2Image
}
type MetadataProvider interface {
GetString(key string) (string, error)
}
type MetadataFormat int
const (
EXIF MetadataFormat = iota
IPTC
XMP
)
var ErrMetadataKeyNotFound = errors.New("key not found")
func (e *Error) Error() string {
return e.what
}
func (e *Error) Code() int {
return e.code
}
func makeError(cerr *C.Exiv2Error) *Error {
return &Error{
int(C.exiv2_error_code(cerr)),
C.GoString(C.exiv2_error_what(cerr)),
}
}
func makeImage(cimg *C.Exiv2Image, bytesPtr unsafe.Pointer) *Image {
img := &Image{
bytesArrayPtr: bytesPtr,
img: cimg,
}
runtime.SetFinalizer(img, func(x *Image) {
C.exiv2_image_free(x.img)
if x.bytesArrayPtr != nil {
C.free(x.bytesArrayPtr)
}
})
return img
}
// Open opens an image file from the filesystem and returns a pointer to
// the corresponding Image object, but does not read the Metadata.
// Start the parsing with a call to ReadMetadata()
func Open(path string) (*Image, error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
var cerr *C.Exiv2Error
cimg := C.exiv2_image_factory_open(cpath, &cerr)
if cerr != nil {
err := makeError(cerr)
C.exiv2_error_free(cerr)
return nil, err
}
return makeImage(cimg, nil), nil
}
// OpenBytes opens a byte slice with image data and returns a pointer to
// the corresponding Image object, but does not read the Metadata.
// Start the parsing with a call to ReadMetadata()
func OpenBytes(input []byte) (*Image, error) {
if len(input) == 0 {
return nil, &Error{0, "input is empty"}
}
var cerr *C.Exiv2Error
bytesArrayPtr := C.CBytes(input)
cimg := C.exiv2_image_factory_open_bytes(
(*C.uchar)(bytesArrayPtr),
C.long(len(input)),
&cerr,
)
if cerr != nil {
err := makeError(cerr)
C.exiv2_error_free(cerr)
return nil, err
}
return makeImage(cimg, bytesArrayPtr), nil
}
type LogMsgLevel int
const (
LogMsgDebug LogMsgLevel = 0
LogMsgInfo = 1
LogMsgWarn = 2
LogMsgError = 3
LogMsgMute = 4
)
// SetLogMsgLevel Set the log level (outputs to stderr)
func SetLogMsgLevel(level LogMsgLevel) {
C.exiv2_log_msg_set_level(C.int(level))
}
// ReadMetadata reads the metadata of an Image
func (i *Image) ReadMetadata() error {
var cerr *C.Exiv2Error
C.exiv2_image_read_metadata(i.img, &cerr)
if cerr != nil {
err := makeError(cerr)
C.exiv2_error_free(cerr)
return err
}
return nil
}
// GetBytes returns an image contents.
// If its metadata has been changed, the changes are reflected here.
func (i *Image) GetBytes() []byte {
size := C.exiv_image_get_size(i.img)
ptr := C.exiv_image_get_bytes_ptr(i.img)
return C.GoBytes(unsafe.Pointer(ptr), C.int(size))
}
// PixelWidth returns the width of the image in pixels
func (i *Image) PixelWidth() int64 {
return int64(C.exiv2_image_get_pixel_width(i.img))
}
// PixelHeight returns the height of the image in pixels
func (i *Image) PixelHeight() int64 {
return int64(C.exiv2_image_get_pixel_height(i.img))
}
// ICCProfile returns the ICC profile or nil if the image doesn't has one.
func (i *Image) ICCProfile() []byte {
size := C.int(C.exiv2_image_icc_profile_size(i.img))
if size <= 0 {
return nil
}
return C.GoBytes(unsafe.Pointer(C.exiv2_image_icc_profile(i.img)), size)
}
// SetMetadataString Sets an exif or iptc key with a given string value
func (i *Image) SetMetadataString(f MetadataFormat, key, value string) error {
cKey := C.CString(key)
cValue := C.CString(value)
defer func() {
C.free(unsafe.Pointer(cKey))
C.free(unsafe.Pointer(cValue))
}()
var cerr *C.Exiv2Error
switch f {
case EXIF:
C.exiv2_image_set_exif_string(i.img, cKey, cValue, &cerr)
case IPTC:
C.exiv2_image_set_iptc_string(i.img, cKey, cValue, &cerr)
case XMP:
C.exiv2_image_set_xmp_string(i.img, cKey, cValue, &cerr)
default:
return errors.New("invalid metadata type")
}
if cerr != nil {
err := makeError(cerr)
C.exiv2_error_free(cerr)
return err
}
return nil
}
// SetMetadataShort Sets an exif or iptc key with a given short value
func (i *Image) SetMetadataShort(f MetadataFormat, key, value string) error {
cKey := C.CString(key)
cValue := C.CString(value)
defer func() {
C.free(unsafe.Pointer(cKey))
C.free(unsafe.Pointer(cValue))
}()
var cerr *C.Exiv2Error
switch f {
case EXIF:
C.exiv2_image_set_exif_short(i.img, cKey, cValue, &cerr)
case IPTC:
C.exiv2_image_set_iptc_short(i.img, cKey, cValue, &cerr)
default:
return errors.New("invalid metadata type")
}
if cerr != nil {
err := makeError(cerr)
C.exiv2_error_free(cerr)
return err
}
return nil
}
// StripKey removes a key from the metadata
func (i *Image) StripKey(f MetadataFormat, key string) error {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
var cErr *C.Exiv2Error
switch f {
case EXIF:
C.exiv2_exif_strip_key(i.img, ckey, &cErr)
case IPTC:
C.exiv2_iptc_strip_key(i.img, ckey, &cErr)
case XMP:
C.exiv2_xmp_strip_key(i.img, ckey, &cErr)
default:
return errors.New("invalid metadata format")
}
if cErr != nil {
err := makeError(cErr)
C.exiv2_error_free(cErr)
return err
}
return nil
}
// StripMetadata removes all metadata from the image except the keys in unless
func (i *Image) StripMetadata(unless []string) error {
var err error
err = i.ExifStripMetadata(unless)
if err != nil {
return err
}
err = i.IptcStripMetadata(unless)
if err != nil {
return err
}
err = i.XmpStripMetadata(unless)
if err != nil {
return err
}
return nil
}
// contains checks if a string is present in a string slice
func contains(needle string, haystack []string) bool {
for _, s := range haystack {
if s == needle {
return true
}
}
return false
}
// getCTags converts a map of tags to a C array of C strings
func getCTags(goTags []string) (input []*C.char) {
for _, value := range goTags {
input = append(input, C.CString(value))
}
return
}
// This interface is used to get all the tags from a metadata format.
// Won't be available in the public API.
type dataFormat interface {
AllTags() map[string]string
}
// getTagsToRemove returns a list of tags to remove from the metadata
// For now this method won't be added to the public API. We must see if it's
// useful or not.
func getKeysToRemove(m dataFormat, unless []string) (tagsToRemove []string) {
for key, _ := range m.AllTags() {
if !contains(key, unless) {
tagsToRemove = append(tagsToRemove, key)
}
}
return
}