forked from dchesterton/goexiv
-
Notifications
You must be signed in to change notification settings - Fork 2
/
exiv.go
158 lines (124 loc) · 3.21 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
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 {
img *C.Exiv2Image
}
type MetadataProvider interface {
GetString(key string) (string, error)
}
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) *Image {
img := &Image{
cimg,
}
runtime.SetFinalizer(img, func(x *Image) {
C.exiv2_image_free(x.img)
})
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
}
// 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(b []byte) (*Image, error) {
if len(b) == 0 {
return nil, &Error{0, "input is empty"}
}
var cerr *C.Exiv2Error
cimg := C.exiv2_image_factory_open_bytes((*C.uchar)(unsafe.Pointer(&b[0])), C.long(len(b)), &cerr)
if cerr != nil {
err := makeError(cerr)
C.exiv2_error_free(cerr)
return nil, err
}
return makeImage(cimg), nil
}
// 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
}
// 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)
}
// Sets an exif or iptc key with a given string value
func (i *Image) SetMetadataString(format, key, value string) error {
if format != "iptc" && format != "exif" {
return errors.New("invalid metadata type: " + format)
}
cKey := C.CString(key)
cValue := C.CString(value)
defer func() {
C.free(unsafe.Pointer(cKey))
C.free(unsafe.Pointer(cValue))
}()
var cerr *C.Exiv2Error
if format == "iptc" {
C.exiv2_image_set_iptc_string(i.img, cKey, cValue, &cerr)
} else {
C.exiv2_image_set_exif_string(i.img, cKey, cValue, &cerr)
}
if cerr != nil {
err := makeError(cerr)
C.exiv2_error_free(cerr)
return err
}
return nil
}