forked from kolesa-team/goexiv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmp.go
181 lines (141 loc) · 3.87 KB
/
xmp.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
package goexiv
// #cgo pkg-config: exiv2
// #include "helper.h"
// #include <stdlib.h>
import "C"
import (
"runtime"
"unsafe"
)
// XmpData contains all Xmp Data of an image.
type XmpData struct {
img *Image // We point to img to keep it alive
data *C.Exiv2XmpData
}
// XmpDatum stores the info of one xmp datum.
type XmpDatum struct {
data *XmpData
datum *C.Exiv2XmpDatum
}
// XmpDatumIterator wraps the respective C++ structure.
type XmpDatumIterator struct {
data *XmpData
iter *C.Exiv2XmpDatumIterator
}
func makeXmpData(img *Image, cdata *C.Exiv2XmpData) *XmpData {
data := &XmpData{
img,
cdata,
}
runtime.SetFinalizer(data, func(x *XmpData) {
C.exiv2_xmp_data_free(x.data)
})
return data
}
func makeXmpDatum(data *XmpData, cdatum *C.Exiv2XmpDatum) *XmpDatum {
if cdatum == nil {
return nil
}
datum := &XmpDatum{
data,
cdatum,
}
runtime.SetFinalizer(datum, func(x *XmpDatum) {
C.exiv2_xmp_datum_free(x.datum)
})
return datum
}
// GetXmpData returns the XmpData of an Image.
func (i *Image) GetXmpData() *XmpData {
return makeXmpData(i, C.exiv2_image_get_xmp_data(i.img))
}
// FindKey tries to find the specified key and returns its data.
// It returns an error if the key is invalid. If the key is not found, a
// nil pointer will be returned
func (d *XmpData) FindKey(key string) (*XmpDatum, error) {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
var cerr *C.Exiv2Error
cdatum := C.exiv2_xmp_data_find_key(d.data, ckey, &cerr)
if cerr != nil {
err := makeError(cerr)
C.exiv2_error_free(cerr)
return nil, err
}
runtime.KeepAlive(d)
return makeXmpDatum(d, cdatum), nil
}
func (d *XmpDatum) String() string {
cstr := C.exiv2_xmp_datum_to_string(d.datum)
defer C.free(unsafe.Pointer(cstr))
return C.GoString(cstr)
}
func (i *Image) XmpStripKey(key string) error {
return i.StripKey(XMP, key)
}
func (i *Image) SetXmpString(key, value string) error {
return i.SetMetadataString(XMP, key, value)
}
func (d *XmpData) GetString(key string) (string, error) {
datum, err := d.FindKey(key)
if err != nil {
return "", err
}
if datum == nil {
return "", ErrMetadataKeyNotFound
}
return datum.String(), nil
}
// AllTags returns all ZMP tags
func (d *XmpData) AllTags() map[string]string {
keyValues := map[string]string{}
for i := d.Iterator(); i.HasNext(); {
d := i.Next()
keyValues[d.Key()] = d.String()
}
return keyValues
}
// XmpStripMetadata removes all EXIF metadata except the keys in the unless array.
func (i *Image) XmpStripMetadata(unless []string) error {
var cErr *C.Exiv2Error
tagsToRemove := getKeysToRemove(i.GetXmpData(), unless)
if len(tagsToRemove) == 0 {
return nil
}
cTags := getCTags(tagsToRemove)
defer func() {
for _, cstr := range cTags {
C.free(unsafe.Pointer(cstr))
}
}()
C.exiv2_xmp_strip_data(i.img, &cTags[0], C.int(len(cTags)), &cErr)
if cErr != nil {
err := makeError(cErr)
C.exiv2_error_free(cErr)
return err
}
return nil
}
// Iterator returns a new XmpDatumIterator to iterate over all IPTC data.
func (d *XmpData) Iterator() *XmpDatumIterator {
return makeXmpDatumIterator(d, C.exiv2_xmp_data_iterator(d.data))
}
// HasNext returns true as long as the iterator has another datum to deliver.
func (i *XmpDatumIterator) HasNext() bool {
return C.exiv2_xmp_data_iterator_has_next(i.iter) != 0
}
// Next returns the next XmpDatum of the iterator or nil if iterator has reached the end.
func (i *XmpDatumIterator) Next() *XmpDatum {
return makeXmpDatum(i.data, C.exiv2_xmp_datum_iterator_next(i.iter))
}
func makeXmpDatumIterator(data *XmpData, cIter *C.Exiv2XmpDatumIterator) *XmpDatumIterator {
datum := &XmpDatumIterator{data, cIter}
runtime.SetFinalizer(datum, func(i *XmpDatumIterator) {
C.exiv2_xmp_datum_iterator_free(i.iter)
})
return datum
}
// Key returns the XMP key of the datum.
func (d *XmpDatum) Key() string {
return C.GoString(C.exiv2_xmp_datum_key(d.datum))
}