| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package meta |
| 15 |
|
| 16 |
import ( |
| 17 |
"encoding/json" |
| 18 |
"os" |
| 19 |
"path/filepath" |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/bep/imagemeta" |
| 24 |
"github.com/google/go-cmp/cmp" |
| 25 |
|
| 26 |
qt "github.com/frankban/quicktest" |
| 27 |
) |
| 28 |
|
| 29 |
func TestExif(t *testing.T) { |
| 30 |
c := qt.New(t) |
| 31 |
f, err := os.Open(filepath.FromSlash("../../testdata/sunset.jpg")) |
| 32 |
c.Assert(err, qt.IsNil) |
| 33 |
defer f.Close() |
| 34 |
|
| 35 |
d, err := NewDecoder(IncludeFields("Lens|Date")) |
| 36 |
c.Assert(err, qt.IsNil) |
| 37 |
x, err := d.Decode("", imagemeta.JPEG, f) |
| 38 |
c.Assert(err, qt.IsNil) |
| 39 |
c.Assert(x.Date.Format("2006-01-02"), qt.Equals, "2017-10-27") |
| 40 |
|
| 41 |
// Malaga: https://goo.gl/taazZy |
| 42 |
|
| 43 |
c.Assert(x.Lat, qt.Equals, float64(36.59744166666667)) |
| 44 |
c.Assert(x.Long, qt.Equals, float64(-4.50846)) |
| 45 |
|
| 46 |
v, found := x.Tags["LensModel"] |
| 47 |
c.Assert(found, qt.Equals, true) |
| 48 |
lensModel, ok := v.(string) |
| 49 |
c.Assert(ok, qt.Equals, true) |
| 50 |
c.Assert(lensModel, qt.Equals, "smc PENTAX-DA* 16-50mm F2.8 ED AL [IF] SDM") |
| 51 |
|
| 52 |
v, found = x.Tags["ModifyDate"] |
| 53 |
c.Assert(found, qt.Equals, true) |
| 54 |
c.Assert(v, qt.Equals, "2017:11:23 09:56:54") |
| 55 |
|
| 56 |
// Verify that it survives a round-trip to JSON and back. |
| 57 |
data, err := json.Marshal(x) |
| 58 |
c.Assert(err, qt.IsNil) |
| 59 |
x2 := &ExifInfo{} |
| 60 |
err = json.Unmarshal(data, x2) |
| 61 |
c.Assert(err, qt.IsNil) |
| 62 |
|
| 63 |
c.Assert(x2, eq, x) |
| 64 |
} |
| 65 |
|
| 66 |
func TestExifPNG(t *testing.T) { |
| 67 |
c := qt.New(t) |
| 68 |
|
| 69 |
f, err := os.Open(filepath.FromSlash("../../testdata/gohugoio.png")) |
| 70 |
c.Assert(err, qt.IsNil) |
| 71 |
defer f.Close() |
| 72 |
|
| 73 |
d, err := NewDecoder() |
| 74 |
c.Assert(err, qt.IsNil) |
| 75 |
_, err = d.Decode("", imagemeta.PNG, f) |
| 76 |
c.Assert(err, qt.IsNil) |
| 77 |
} |
| 78 |
|
| 79 |
func TestIssue8079(t *testing.T) { |
| 80 |
c := qt.New(t) |
| 81 |
|
| 82 |
f, err := os.Open(filepath.FromSlash("../../testdata/iss8079.jpg")) |
| 83 |
c.Assert(err, qt.IsNil) |
| 84 |
defer f.Close() |
| 85 |
|
| 86 |
d, err := NewDecoder() |
| 87 |
c.Assert(err, qt.IsNil) |
| 88 |
x, err := d.Decode("", imagemeta.JPEG, f) |
| 89 |
c.Assert(err, qt.IsNil) |
| 90 |
c.Assert(x.Tags["ImageDescription"], qt.Equals, "Città del Vaticano #nanoblock #vatican #vaticancity") |
| 91 |
} |
| 92 |
|
| 93 |
func BenchmarkDecodeExif(b *testing.B) { |
| 94 |
c := qt.New(b) |
| 95 |
f, err := os.Open(filepath.FromSlash("../../testdata/sunset.jpg")) |
| 96 |
c.Assert(err, qt.IsNil) |
| 97 |
defer f.Close() |
| 98 |
|
| 99 |
d, err := NewDecoder() |
| 100 |
c.Assert(err, qt.IsNil) |
| 101 |
|
| 102 |
for b.Loop() { |
| 103 |
_, err = d.Decode("", imagemeta.JPEG, f) |
| 104 |
c.Assert(err, qt.IsNil) |
| 105 |
f.Seek(0, 0) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
var eq = qt.CmpEquals( |
| 110 |
cmp.Comparer( |
| 111 |
func(v1, v2 imagemeta.Rat[uint32]) bool { |
| 112 |
return v1.String() == v2.String() |
| 113 |
}, |
| 114 |
), |
| 115 |
cmp.Comparer( |
| 116 |
func(v1, v2 imagemeta.Rat[int32]) bool { |
| 117 |
return v1.String() == v2.String() |
| 118 |
}, |
| 119 |
), |
| 120 |
cmp.Comparer(func(v1, v2 time.Time) bool { |
| 121 |
return v1.Unix() == v2.Unix() |
| 122 |
}), |
| 123 |
) |
| 124 |
|
| 125 |
func TestIssue10738(t *testing.T) { |
| 126 |
c := qt.New(t) |
| 127 |
|
| 128 |
testFunc := func(c *qt.C, path, include string) any { |
| 129 |
c.Helper() |
| 130 |
f, err := os.Open(filepath.FromSlash(path)) |
| 131 |
c.Assert(err, qt.IsNil) |
| 132 |
defer f.Close() |
| 133 |
|
| 134 |
d, err := NewDecoder(IncludeFields(include)) |
| 135 |
c.Assert(err, qt.IsNil) |
| 136 |
x, err := d.Decode("", imagemeta.JPEG, f) |
| 137 |
c.Assert(err, qt.IsNil) |
| 138 |
|
| 139 |
// Verify that it survives a round-trip to JSON and back. |
| 140 |
data, err := json.Marshal(x) |
| 141 |
c.Assert(err, qt.IsNil) |
| 142 |
x2 := &ExifInfo{} |
| 143 |
err = json.Unmarshal(data, x2) |
| 144 |
c.Assert(err, qt.IsNil) |
| 145 |
|
| 146 |
c.Assert(x2, eq, x) |
| 147 |
|
| 148 |
v, found := x.Tags["ExposureTime"] |
| 149 |
c.Assert(found, qt.Equals, true) |
| 150 |
return v |
| 151 |
} |
| 152 |
|
| 153 |
type args struct { |
| 154 |
path string // imagePath |
| 155 |
include string // includeFields |
| 156 |
} |
| 157 |
|
| 158 |
type want struct { |
| 159 |
vN int64 // numerator |
| 160 |
vD int64 // denominator |
| 161 |
} |
| 162 |
|
| 163 |
type testCase struct { |
| 164 |
name string |
| 165 |
args args |
| 166 |
want want |
| 167 |
} |
| 168 |
|
| 169 |
tests := []testCase{ |
| 170 |
{ |
| 171 |
"canon_cr2_fraction", args{ |
| 172 |
path: "../../testdata/issue10738/canon_cr2_fraction.jpg", |
| 173 |
include: "Lens|Date|ExposureTime", |
| 174 |
}, want{ |
| 175 |
1, |
| 176 |
500, |
| 177 |
}, |
| 178 |
}, |
| 179 |
{ |
| 180 |
"canon_cr2_integer", args{ |
| 181 |
path: "../../testdata/issue10738/canon_cr2_integer.jpg", |
| 182 |
include: "Lens|Date|ExposureTime", |
| 183 |
}, want{ |
| 184 |
10, |
| 185 |
1, |
| 186 |
}, |
| 187 |
}, |
| 188 |
{ |
| 189 |
"dji_dng_fraction", args{ |
| 190 |
path: "../../testdata/issue10738/dji_dng_fraction.jpg", |
| 191 |
include: "Lens|Date|ExposureTime", |
| 192 |
}, want{ |
| 193 |
1, |
| 194 |
4000, |
| 195 |
}, |
| 196 |
}, |
| 197 |
{ |
| 198 |
"fuji_raf_fraction", args{ |
| 199 |
path: "../../testdata/issue10738/fuji_raf_fraction.jpg", |
| 200 |
include: "Lens|Date|ExposureTime", |
| 201 |
}, want{ |
| 202 |
1, |
| 203 |
250, |
| 204 |
}, |
| 205 |
}, |
| 206 |
{ |
| 207 |
"fuji_raf_integer", args{ |
| 208 |
path: "../../testdata/issue10738/fuji_raf_integer.jpg", |
| 209 |
include: "Lens|Date|ExposureTime", |
| 210 |
}, want{ |
| 211 |
1, |
| 212 |
1, |
| 213 |
}, |
| 214 |
}, |
| 215 |
{ |
| 216 |
"leica_dng_fraction", args{ |
| 217 |
path: "../../testdata/issue10738/leica_dng_fraction.jpg", |
| 218 |
include: "Lens|Date|ExposureTime", |
| 219 |
}, want{ |
| 220 |
1, |
| 221 |
100, |
| 222 |
}, |
| 223 |
}, |
| 224 |
{ |
| 225 |
"lumix_rw2_fraction", args{ |
| 226 |
path: "../../testdata/issue10738/lumix_rw2_fraction.jpg", |
| 227 |
include: "Lens|Date|ExposureTime", |
| 228 |
}, want{ |
| 229 |
1, |
| 230 |
400, |
| 231 |
}, |
| 232 |
}, |
| 233 |
{ |
| 234 |
"nikon_nef_d5600", args{ |
| 235 |
path: "../../testdata/issue10738/nikon_nef_d5600.jpg", |
| 236 |
include: "Lens|Date|ExposureTime", |
| 237 |
}, want{ |
| 238 |
1, |
| 239 |
1000, |
| 240 |
}, |
| 241 |
}, |
| 242 |
{ |
| 243 |
"nikon_nef_fraction", args{ |
| 244 |
path: "../../testdata/issue10738/nikon_nef_fraction.jpg", |
| 245 |
include: "Lens|Date|ExposureTime", |
| 246 |
}, want{ |
| 247 |
1, |
| 248 |
640, |
| 249 |
}, |
| 250 |
}, |
| 251 |
{ |
| 252 |
"nikon_nef_integer", args{ |
| 253 |
path: "../../testdata/issue10738/nikon_nef_integer.jpg", |
| 254 |
include: "Lens|Date|ExposureTime", |
| 255 |
}, want{ |
| 256 |
30, |
| 257 |
1, |
| 258 |
}, |
| 259 |
}, |
| 260 |
{ |
| 261 |
"nikon_nef_fraction_2", args{ |
| 262 |
path: "../../testdata/issue10738/nikon_nef_fraction_2.jpg", |
| 263 |
include: "Lens|Date|ExposureTime", |
| 264 |
}, want{ |
| 265 |
1, |
| 266 |
6400, |
| 267 |
}, |
| 268 |
}, |
| 269 |
{ |
| 270 |
"sony_arw_fraction", args{ |
| 271 |
path: "../../testdata/issue10738/sony_arw_fraction.jpg", |
| 272 |
include: "Lens|Date|ExposureTime", |
| 273 |
}, want{ |
| 274 |
1, |
| 275 |
160, |
| 276 |
}, |
| 277 |
}, |
| 278 |
{ |
| 279 |
"sony_arw_integer", args{ |
| 280 |
path: "../../testdata/issue10738/sony_arw_integer.jpg", |
| 281 |
include: "Lens|Date|ExposureTime", |
| 282 |
}, want{ |
| 283 |
4, |
| 284 |
1, |
| 285 |
}, |
| 286 |
}, |
| 287 |
} |
| 288 |
|
| 289 |
for _, tt := range tests { |
| 290 |
c.Run(tt.name, func(c *qt.C) { |
| 291 |
got := testFunc(c, tt.args.path, tt.args.include) |
| 292 |
switch v := got.(type) { |
| 293 |
case float64: |
| 294 |
c.Assert(v, qt.Equals, float64(tt.want.vN)) |
| 295 |
case imagemeta.Rat[uint32]: |
| 296 |
r, err := imagemeta.NewRat[uint32](uint32(tt.want.vN), uint32(tt.want.vD)) |
| 297 |
c.Assert(err, qt.IsNil) |
| 298 |
c.Assert(v, eq, r) |
| 299 |
default: |
| 300 |
c.Fatalf("unexpected type: %T", got) |
| 301 |
} |
| 302 |
}) |
| 303 |
} |
| 304 |
} |