-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
89 lines (70 loc) · 1.97 KB
/
main.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
/*
Golang bindings for libiptcdata.
*/
package iptc
/*
#cgo LDFLAGS: -liptcdata
#include <stdlib.h>
#include <libiptcdata/iptc-data.h>
IptcDataSet *get_iptc_dataset(IptcData *iptcData, unsigned int i);
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
)
type Data map[int]map[int]string
var (
ErrNoIptcData = errors.New("no IPTC data in file %s")
)
/**
* Opens the given path and attempts to return any IPTC data read.
*/
func Open(file string) (Data, error) {
cfile := C.CString(file)
iptcData := C.iptc_data_new_from_jpeg(cfile)
C.free(unsafe.Pointer(cfile))
if iptcData == nil {
return nil, fmt.Errorf(ErrNoIptcData.Error(), file)
}
defer func() {
C.iptc_data_unref(iptcData)
}()
return parseIptcData(iptcData)
}
/**
* Parses an IPTC data blob generating a map of records and tags to
* string values.
*/
func parseIptcData(iptcData *C.IptcData) (Data, error) {
parsed := Data{}
for i := C.uint(0); i < iptcData.count; i++ {
dataSet := C.get_iptc_dataset(iptcData, i)
if parsed[int(dataSet.record)] == nil {
parsed[int(dataSet.record)] = make(map[int]string)
}
switch C.iptc_dataset_get_format(dataSet) {
case C.IPTC_FORMAT_BYTE, C.IPTC_FORMAT_SHORT, C.IPTC_FORMAT_LONG:
parsed[int(dataSet.record)][int(dataSet.tag)] = fmt.Sprintf("%d", C.iptc_dataset_get_value(dataSet))
case C.IPTC_FORMAT_BINARY:
value := make([]C.char, 256)
C.iptc_dataset_get_as_str(dataSet, &value[0], C.uint(len(value)))
// Guard against data being shorter than specified.
dataLen := dataSet.size*3 - 1
if dataLen < 0 {
dataLen = 0
} else {
if int(dataLen) > len(value)-1 {
dataLen = C.uint(len(value) - 1)
}
}
parsed[int(dataSet.record)][int(dataSet.tag)] = fmt.Sprintf("%c", value[dataLen])
default:
value := make([]C.uchar, 256)
actualLength := C.iptc_dataset_get_data(dataSet, &value[0], C.uint(len(value)))
parsed[int(dataSet.record)][int(dataSet.tag)] = fmt.Sprintf("%s", value[:actualLength-1])
}
}
return parsed, nil
}