-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathAddImageMetadataTests.cs
More file actions
227 lines (181 loc) · 7.71 KB
/
AddImageMetadataTests.cs
File metadata and controls
227 lines (181 loc) · 7.71 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using NUnit.Framework;
using System;
using TagLib;
using TagLib.IFD;
using TagLib.Xmp;
namespace TaglibSharp.Tests.Images
{
public static class AddImageMetadataTests
{
public static string test_comment = "This is a TagLib# &Test?Comment%$@_ ";
public static readonly DateTime date_time = new DateTime (2009, 10, 15, 12, 12, 59);
public static readonly string[] keywords = { "keyword 1", "§$&§%", "99 dsf" };
public static void AddExifTest (string sample_file, string tmp_file, bool contains_exif)
{
var file = Utils.CreateTmpFile (sample_file, tmp_file);
IFDTag exif_tag;
if (!contains_exif) {
exif_tag = file.GetTag (TagTypes.TiffIFD, false) as IFDTag;
Assert.IsNull (exif_tag, "Tiff Tag contained");
}
exif_tag = file.GetTag (TagTypes.TiffIFD, true) as IFDTag;
Assert.IsNotNull (exif_tag, "Tiff Tag not created");
exif_tag.Comment = test_comment;
exif_tag.DateTime = date_time;
exif_tag.DateTimeDigitized = date_time;
Assert.AreEqual (test_comment, exif_tag.Comment);
Assert.AreEqual (date_time, exif_tag.DateTime);
Assert.AreEqual (date_time, exif_tag.DateTimeDigitized);
Assert.AreEqual (date_time, exif_tag.DateTimeOriginal);
// Store and reload file
file.Save ();
file = File.Create (tmp_file);
exif_tag = file.GetTag (TagTypes.TiffIFD, false) as IFDTag;
Assert.IsNotNull (exif_tag, "Tiff Tag not read");
Assert.AreEqual (test_comment, exif_tag.Comment);
Assert.AreEqual (date_time, exif_tag.DateTime);
Assert.AreEqual (date_time, exif_tag.DateTimeDigitized);
Assert.AreEqual (date_time, exif_tag.DateTimeOriginal);
}
public static void AddGPSTest (string sample_file, string tmp_file, bool contains_tiff)
{
AddGPSTest (sample_file, tmp_file, contains_tiff, +53.231d, +168.19823d, 40.0d);
AddGPSTest (sample_file, tmp_file, contains_tiff, -21.342d, +88.18232d, -39.0d);
AddGPSTest (sample_file, tmp_file, contains_tiff, +75.12931d, -8.98712d, -10.0d);
AddGPSTest (sample_file, tmp_file, contains_tiff, -42.1023d, -113.12432d, 1920.0d);
AddGPSTest (sample_file, tmp_file, contains_tiff, -87.23d, +23.9743d, 0.0000123d);
AddGPSTest (sample_file, tmp_file, contains_tiff, +72.123d, +17.432d, -0.0000089d);
}
public static void AddXMPTest1 (string sample_file, string tmp_file, bool contains_xmp)
{
var file = Utils.CreateTmpFile (sample_file, tmp_file);
XmpTag xmp_tag;
if (!contains_xmp) {
xmp_tag = file.GetTag (TagTypes.XMP, false) as XmpTag;
Assert.IsNull (xmp_tag, "XMP Tag contained");
}
xmp_tag = file.GetTag (TagTypes.XMP, true) as XmpTag;
Assert.IsNotNull (xmp_tag, "XMP Tag not created");
xmp_tag.Keywords = keywords;
xmp_tag.Comment = test_comment;
xmp_tag.Software = null;
Assert.AreEqual (keywords, xmp_tag.Keywords);
Assert.AreEqual (test_comment, xmp_tag.Comment);
Assert.AreEqual (null, xmp_tag.Software);
// Store and reload file
file.Save ();
file = File.Create (tmp_file);
xmp_tag = file.GetTag (TagTypes.XMP, false) as XmpTag;
Assert.IsNotNull (xmp_tag, "XMP Tag not read");
Assert.AreEqual (keywords, xmp_tag.Keywords);
Assert.AreEqual (test_comment, xmp_tag.Comment);
Assert.AreEqual (null, xmp_tag.Software);
}
public static void AddXMPTest2 (string sample_file, string tmp_file, bool contains_xmp)
{
var file = Utils.CreateTmpFile (sample_file, tmp_file);
XmpTag xmp_tag;
if (!contains_xmp) {
xmp_tag = file.GetTag (TagTypes.XMP, false) as XmpTag;
Assert.IsNull (xmp_tag, "XMP Tag contained");
}
xmp_tag = file.GetTag (TagTypes.XMP, true) as XmpTag;
Assert.IsNotNull (xmp_tag, "XMP Tag not created");
xmp_tag.Keywords = null;
xmp_tag.Comment = null;
xmp_tag.Software = test_comment;
Assert.AreEqual (Array.Empty<string> (), xmp_tag.Keywords);
Assert.AreEqual (null, xmp_tag.Comment);
Assert.AreEqual (test_comment, xmp_tag.Software);
// Store and reload file
file.Save ();
file = File.Create (tmp_file);
xmp_tag = file.GetTag (TagTypes.XMP, false) as XmpTag;
Assert.IsNotNull (xmp_tag, "XMP Tag not read");
Assert.AreEqual (Array.Empty<string> (), xmp_tag.Keywords);
Assert.AreEqual (null, xmp_tag.Comment);
Assert.AreEqual (test_comment, xmp_tag.Software);
}
public static void AddAllTest (string sample_file, string tmp_file)
{
var file = Utils.CreateTmpFile (sample_file, tmp_file) as TagLib.Image.File;
Assert.IsNotNull (file, "file");
// ensure all tags are present
file.GetTag (TagTypes.XMP, true);
file.GetTag (TagTypes.TiffIFD, true);
file.ImageTag.Comment = test_comment;
file.ImageTag.Keywords = keywords;
file.ImageTag.Rating = 4;
file.ImageTag.DateTime = date_time;
file.ImageTag.Latitude = 3.0;
file.ImageTag.Longitude = 3.0;
file.ImageTag.Altitude = 3.0;
Assert.AreEqual (test_comment, file.ImageTag.Comment);
Assert.AreEqual (keywords, file.ImageTag.Keywords);
Assert.AreEqual (4, file.ImageTag.Rating);
Assert.AreEqual (date_time, file.ImageTag.DateTime);
Assert.AreEqual (3.0, file.ImageTag.Latitude);
Assert.AreEqual (3.0, file.ImageTag.Longitude);
Assert.AreEqual (3.0, file.ImageTag.Altitude);
// Store and reload file
file.Save ();
file = File.Create (tmp_file) as TagLib.Image.File;
Assert.IsNotNull (file, "tmp file");
Assert.AreEqual (test_comment, file.ImageTag.Comment);
Assert.AreEqual (keywords, file.ImageTag.Keywords);
Assert.AreEqual (4, file.ImageTag.Rating);
Assert.AreEqual (date_time, file.ImageTag.DateTime);
Assert.AreEqual (3.0, file.ImageTag.Latitude);
Assert.AreEqual (3.0, file.ImageTag.Longitude);
Assert.AreEqual (3.0, file.ImageTag.Altitude);
var xmp_tag = file.GetTag (TagTypes.XMP, false) as XmpTag;
Assert.IsNotNull (xmp_tag, "XMP Tag not read");
Assert.AreEqual (test_comment, xmp_tag.Comment);
Assert.AreEqual (keywords, xmp_tag.Keywords);
Assert.AreEqual (4, xmp_tag.Rating);
var ifd_tag = file.GetTag (TagTypes.TiffIFD, false) as IFDTag;
Assert.IsNotNull (ifd_tag, "Tiff Tag not read");
Assert.AreEqual (test_comment, ifd_tag.Comment);
Assert.AreEqual (date_time, ifd_tag.DateTime);
Assert.AreEqual (3.0, ifd_tag.Latitude);
Assert.AreEqual (3.0, ifd_tag.Longitude);
Assert.AreEqual (3.0, ifd_tag.Altitude);
}
static void AddGPSTest (string sample_file, string tmp_file, bool contains_tiff, double latitude, double longitude, double altitude)
{
var file = Utils.CreateTmpFile (sample_file, tmp_file);
IFDTag ifd;
if (!contains_tiff) {
ifd = file.GetTag (TagTypes.TiffIFD, false) as IFDTag;
Assert.IsNull (ifd, "Tiff IFD not contained");
}
ifd = file.GetTag (TagTypes.TiffIFD, true) as IFDTag;
Assert.IsNotNull (ifd, "Tiff IFD not created");
ifd.Latitude = latitude;
ifd.Longitude = longitude;
ifd.Altitude = altitude;
Assert.IsNotNull (ifd.Latitude, "Latitude");
Assert.IsNotNull (ifd.Longitude, "Longitude");
Assert.IsNotNull (ifd.Altitude, "Altitude");
AssertEqualDouble (latitude, ifd.Latitude.Value, 0.00000001);
AssertEqualDouble (longitude, ifd.Longitude.Value, 0.00000001);
AssertEqualDouble (altitude, ifd.Altitude.Value, 0.00000001);
// Store and reload file
file.Save ();
file = File.Create (tmp_file);
ifd = file.GetTag (TagTypes.TiffIFD, false) as IFDTag;
Assert.IsNotNull (ifd, "Tiff IFD not read");
Assert.IsNotNull (ifd.Latitude, "Latitude");
Assert.IsNotNull (ifd.Longitude, "Longitude");
Assert.IsNotNull (ifd.Altitude, "Altitude");
AssertEqualDouble (latitude, ifd.Latitude.Value, 0.00000001);
AssertEqualDouble (longitude, ifd.Longitude.Value, 0.00000001);
AssertEqualDouble (altitude, ifd.Altitude.Value, 0.00000001);
}
static void AssertEqualDouble (double d1, double d2, double acc)
{
Assert.Less (d1 - acc, d2);
Assert.Greater (d1 + acc, d2);
}
}
}