-
Notifications
You must be signed in to change notification settings - Fork 4
/
exif.go
147 lines (113 loc) · 3.05 KB
/
exif.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
package goexiv
// #cgo pkg-config: exiv2
// #include "helper.h"
// #include <stdlib.h>
import "C"
import (
"runtime"
"unsafe"
)
type ExifData struct {
img *Image // We point to img to keep it alive
data *C.Exiv2ExifData
}
type ExifDatum struct {
data *ExifData
datum *C.Exiv2ExifDatum
}
// ExifDatumIterator wraps the respective C++ structure.
type ExifDatumIterator struct {
data *ExifData
iter *C.Exiv2ExifDatumIterator
}
func makeExifData(img *Image, cdata *C.Exiv2ExifData) *ExifData {
data := &ExifData{
img,
cdata,
}
runtime.SetFinalizer(data, func(x *ExifData) {
C.exiv2_exif_data_free(x.data)
})
return data
}
func makeExifDatum(data *ExifData, cdatum *C.Exiv2ExifDatum) *ExifDatum {
if cdatum == nil {
return nil
}
datum := &ExifDatum{
data,
cdatum,
}
runtime.SetFinalizer(datum, func(x *ExifDatum) {
C.exiv2_exif_datum_free(x.datum)
})
return datum
}
func (i *Image) GetExifData() *ExifData {
return makeExifData(i, C.exiv2_image_get_exif_data(i.img))
}
func (i *Image) SetExifString(key, value string) error {
return i.SetMetadataString("exif", key, value)
}
func (d *ExifData) GetString(key string) (string, error) {
datum, err := d.FindKey(key)
if err != nil {
return "", err
}
if datum == nil {
return "", ErrMetadataKeyNotFound
}
return datum.String(), nil
}
func (d *ExifData) FindKey(key string) (*ExifDatum, error) {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
var cerr *C.Exiv2Error
cdatum := C.exiv2_exif_data_find_key(d.data, ckey, &cerr)
if cerr != nil {
err := makeError(cerr)
C.exiv2_error_free(cerr)
return nil, err
}
return makeExifDatum(d, cdatum), nil
}
// Key returns the Exif key of the datum.
func (d *ExifDatum) Key() string {
return C.GoString(C.exiv2_exif_datum_key(d.datum))
}
func (d *ExifDatum) String() string {
cstr := C.exiv2_exif_datum_to_string(d.datum)
defer C.free(unsafe.Pointer(cstr))
return C.GoString(cstr)
}
// Returns all EXIF tags
func (d *ExifData) AllTags() map[string]string {
keyValues := map[string]string{}
for i := d.Iterator(); i.HasNext(); {
d := i.Next()
keyValues[d.Key()] = d.String()
}
return keyValues
}
// Iterator returns a new ExifDatumIterator to iterate over all Exif data.
func (d *ExifData) Iterator() *ExifDatumIterator {
return makeExifDatumIterator(d, C.exiv2_exif_data_iterator(d.data))
}
// HasNext returns true as long as the iterator has another datum to deliver.
func (i *ExifDatumIterator) HasNext() bool {
return C.exiv2_exif_data_iterator_has_next(i.iter) != 0
}
// Next returns the next ExifDatum of the iterator or nil if iterator has reached the end.
func (i *ExifDatumIterator) Next() *ExifDatum {
return makeExifDatum(i.data, C.exiv2_exif_datum_iterator_next(i.iter))
}
func makeExifDatumIterator(data *ExifData, cIter *C.Exiv2ExifDatumIterator) *ExifDatumIterator {
datum := &ExifDatumIterator{data, cIter}
runtime.SetFinalizer(datum, func(i *ExifDatumIterator) {
C.exiv2_exif_datum_iterator_free(i.iter)
})
return datum
}
func (i *Image) ExifStripKey(key string) error {
return i.StripKey(EXIF, key)
}