-
Notifications
You must be signed in to change notification settings - Fork 7
/
goexif.go
182 lines (153 loc) · 3.34 KB
/
goexif.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
package goexif
import (
"bytes"
"context"
"os"
"photofield/io"
"strconv"
"time"
goio "io"
"image/jpeg"
"github.com/rwcarlsen/goexif/exif"
)
type Exif struct {
// Tag string `json:"tag"`
Width int `json:"width"`
Height int `json:"height"`
Fit io.AspectRatioFit
}
func (e Exif) Close() error {
return nil
}
func (e Exif) Name() string {
return "goexif"
}
func (e Exif) DisplayName() string {
return "EXIF Thumbnail"
}
func (e Exif) Ext() string {
return ".jpg"
}
func (e Exif) Size(size io.Size) io.Size {
return io.Size{X: e.Width, Y: e.Height}.Fit(size, e.Fit)
}
func (e Exif) GetDurationEstimate(size io.Size) time.Duration {
// return 862 * time.Microsecond // SSD
// return 10 * time.Millisecond // SSD - real world
return 100 * time.Millisecond // penalized
// return 930 * time.Microsecond // HDD
}
func (e Exif) Rotate() bool {
return false
}
func (e Exif) Exists(ctx context.Context, id io.ImageId, path string) bool {
exists := false
e.Reader(ctx, id, path, func(r goio.ReadSeeker, err error) {
if r != nil || err == nil {
exists = true
}
})
return exists
}
func (e Exif) Get(ctx context.Context, id io.ImageId, path string) io.Result {
b, o, err := load(path)
if err != nil {
return io.Result{Error: err}
}
r := bytes.NewReader(b)
img, err := jpeg.Decode(r)
return io.Result{
Image: img,
Orientation: o,
Error: err,
}
}
func (e Exif) Reader(ctx context.Context, id io.ImageId, path string, fn func(r goio.ReadSeeker, err error)) {
b, _, err := load(path)
if err != nil {
fn(nil, err)
return
}
fn(bytes.NewReader(b), nil)
}
func (e Exif) Decode(ctx context.Context, r goio.Reader) io.Result {
img, err := jpeg.Decode(r)
return io.Result{
Image: img,
Orientation: io.Normal,
Error: err,
}
}
func (e Exif) Set(ctx context.Context, id io.ImageId, path string, r io.Result) bool {
return false
}
func load(path string) ([]byte, io.Orientation, error) {
f, err := os.Open(path)
if err != nil {
return nil, io.Normal, err
}
defer f.Close()
x, err := exif.Decode(f)
if err != nil {
return nil, io.Normal, err
}
b, err := x.JpegThumbnail()
o := getOrientation(x)
return b, o, err
}
func getOrientation(x *exif.Exif) io.Orientation {
i, err := getTagInt(x, exif.Orientation)
if err != nil {
return io.Normal
}
return io.Orientation(i)
// s, err := getTagString(x, exif.Orientation)
// if err != nil {
// println(err.Error())
// return io.Normal
// }
// return parseOrientation(s)
}
func getTagString(x *exif.Exif, name exif.FieldName) (string, error) {
t, err := x.Get(exif.Orientation)
if err != nil {
return "", err
}
s, err := t.StringVal()
if err != nil {
return "", err
}
return s, nil
}
func getTagInt(x *exif.Exif, name exif.FieldName) (int, error) {
t, err := x.Get(exif.Orientation)
if err != nil {
return 0, err
}
i, err := t.Int(0)
if err != nil {
return i, err
}
return i, nil
}
func parseOrientation(orientation string) io.Orientation {
n, err := strconv.Atoi(orientation)
if err != nil || n < 1 || n > 8 {
return io.Normal
}
return io.Orientation(n)
}
// func getOrientationFromRotation(rotation string) io.Orientation {
// switch rotation {
// case "0":
// return Normal
// case "90":
// return Rotate90
// case "180":
// return Rotate180
// case "270":
// return Rotate270
// default:
// return Normal
// }
// }