-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExifReader.cs
823 lines (677 loc) · 29.2 KB
/
ExifReader.cs
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace ExifLib
{
/// <summary>
/// A class for reading Exif data from a JPEG file. The file will be open for reading for as long as the class exists.
/// <seealso cref="http://gvsoft.homedns.org/exif/Exif-explanation.html"/>
/// </summary>
public class ExifReader : IDisposable
{
private static readonly Regex _nullDateTimeMatcher = new Regex(@"^[\s0]{4}[:\s][\s0]{2}[:\s][\s0]{5}[:\s][\s0]{2}[:\s][\s0]{2}$");
private readonly bool _leaveOpen;
private Stream _stream;
private BinaryReader _reader;
/// <summary>
/// The main tag id/absolute file offset catalogue
/// </summary>
private Dictionary<ushort, long> _ifd0Catalogue;
/// <summary>
/// The thumbnail tag id/absolute file offset catalogue
/// </summary>
/// <remarks>JPEG images contain 2 main sections - one for the main image (which contains most of the useful EXIF data), and one for the thumbnail
/// image (which contains little more than the thumbnail itself). This catalogue is only used by <see cref="GetJpegThumbnailBytes"/>.</remarks>
private Dictionary<ushort, long> _ifd1Catalogue;
/// <summary>
/// Indicates whether to read data using big or little endian byte aligns
/// </summary>
private bool _isLittleEndian;
/// <summary>
/// The position in the filestream at which the TIFF header starts
/// </summary>
private long _tiffHeaderStart;
/// <summary>
/// The location of the thumbnail IFD
/// </summary>
private uint _ifd1Offset;
private bool _isInitialized;
public ExifReader(string fileName)
: this(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { }
public ExifReader(Stream stream)
: this(stream, false) { }
public ExifReader(Stream stream, bool leaveOpen)
{
if (stream == null)
throw new ArgumentNullException("stream");
if (!stream.CanSeek)
throw new ExifLibException("ExifLib requires a seekable stream");
// Leave the stream open if the user wants it so
_leaveOpen = leaveOpen;
_stream = stream;
}
private void Initialize()
{
if (_isInitialized)
return;
_isInitialized = true;
// JPEG encoding uses big endian (i.e. Motorola) byte aligns. The TIFF encoding
// found later in the document will specify the byte aligns used for the
// rest of the document.
_isLittleEndian = false;
// Open the file in a stream
_reader = new BinaryReader(_stream, Encoding.UTF8, _leaveOpen);
// Make sure the file's a JPEG.
if (ReadUShort() != 0xFFD8)
throw new ExifLibException("File is not a valid JPEG");
// Scan to the start of the Exif content
ReadToExifStart();
// Create an index of all Exif tags found within the document
CreateTagIndex();
}
#region TIFF methods
/// <summary>
/// Returns the length (in bytes) per component of the specified TIFF data type
/// </summary>
/// <returns></returns>
private static byte GetTIFFFieldLength(ushort tiffDataType)
{
switch (tiffDataType)
{
case 1:
case 2:
case 7:
case 6:
return 1;
case 3:
case 8:
return 2;
case 4:
case 9:
case 11:
return 4;
case 5:
case 10:
case 12:
return 8;
default:
throw new ExifLibException(string.Format("Unknown TIFF datatype: {0}", tiffDataType));
}
}
#endregion
#region Methods for reading data directly from the filestream
/// <summary>
/// Gets a 2 byte unsigned integer from the file
/// </summary>
/// <returns></returns>
private ushort ReadUShort()
{
return ToUShort(ReadBytes(2));
}
/// <summary>
/// Gets a 4 byte unsigned integer from the file
/// </summary>
/// <returns></returns>
private uint ReadUint()
{
return ToUint(ReadBytes(4));
}
private string ReadString(int chars)
{
var bytes = ReadBytes(chars);
return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
}
private byte[] ReadBytes(int byteCount)
{
return _reader.ReadBytes(byteCount);
}
/// <summary>
/// Reads some bytes from the specified TIFF offset
/// </summary>
/// <param name="tiffOffset"></param>
/// <param name="byteCount"></param>
/// <returns></returns>
private byte[] ReadBytes(ushort tiffOffset, int byteCount)
{
// Keep the current file offset
var originalOffset = _stream.Position;
// Move to the TIFF offset and retrieve the data
_stream.Seek(tiffOffset + _tiffHeaderStart, SeekOrigin.Begin);
var data = _reader.ReadBytes(byteCount);
// Restore the file offset
_stream.Position = originalOffset;
return data;
}
#endregion
#region Data conversion methods for interpreting datatypes from a byte array
/// <summary>
/// Converts 2 bytes to a ushort using the current byte aligns
/// </summary>
/// <returns></returns>
private ushort ToUShort(byte[] data)
{
if (_isLittleEndian != BitConverter.IsLittleEndian)
Array.Reverse(data);
return BitConverter.ToUInt16(data, 0);
}
/// <summary>
/// Converts 8 bytes to the numerator and denominator
/// components of an unsigned rational using the current byte aligns
/// </summary>
private uint[] ToURationalFraction(byte[] data)
{
var numeratorData = new byte[4];
var denominatorData = new byte[4];
Array.Copy(data, numeratorData, 4);
Array.Copy(data, 4, denominatorData, 0, 4);
var numerator = ToUint(numeratorData);
var denominator = ToUint(denominatorData);
return new[] { numerator, denominator };
}
/// <summary>
/// Converts 8 bytes to an unsigned rational using the current byte aligns
/// </summary>
/// <seealso cref="ToRational"/>
private double ToURational(byte[] data)
{
var fraction = ToURationalFraction(data);
return fraction[0] / (double)fraction[1];
}
/// <summary>
/// Converts 8 bytes to the numerator and denominator
/// components of an unsigned rational using the current byte aligns
/// </summary>
/// <remarks>
/// A TIFF rational contains 2 4-byte integers, the first of which is
/// the numerator, and the second of which is the denominator.
/// </remarks>
private int[] ToRationalFraction(byte[] data)
{
var numeratorData = new byte[4];
var denominatorData = new byte[4];
Array.Copy(data, numeratorData, 4);
Array.Copy(data, 4, denominatorData, 0, 4);
int numerator = ToInt(numeratorData);
int denominator = ToInt(denominatorData);
return new[] { numerator, denominator };
}
/// <summary>
/// Converts 8 bytes to a signed rational using the current byte aligns.
/// </summary>
/// <seealso cref="ToRationalFraction"/>
private double ToRational(byte[] data)
{
var fraction = ToRationalFraction(data);
return fraction[0] / (double)fraction[1];
}
/// <summary>
/// Converts 4 bytes to a uint using the current byte aligns
/// </summary>
private uint ToUint(byte[] data)
{
if (_isLittleEndian != BitConverter.IsLittleEndian)
Array.Reverse(data);
return BitConverter.ToUInt32(data, 0);
}
/// <summary>
/// Converts 4 bytes to an int using the current byte aligns
/// </summary>
private int ToInt(byte[] data)
{
if (_isLittleEndian != BitConverter.IsLittleEndian)
Array.Reverse(data);
return BitConverter.ToInt32(data, 0);
}
private double ToDouble(byte[] data)
{
if (_isLittleEndian != BitConverter.IsLittleEndian)
Array.Reverse(data);
return BitConverter.ToDouble(data, 0);
}
private float ToSingle(byte[] data)
{
if (_isLittleEndian != BitConverter.IsLittleEndian)
Array.Reverse(data);
return BitConverter.ToSingle(data, 0);
}
private short ToShort(byte[] data)
{
if (_isLittleEndian != BitConverter.IsLittleEndian)
Array.Reverse(data);
return BitConverter.ToInt16(data, 0);
}
private sbyte ToSByte(byte[] data)
{
// An sbyte should just be a byte with an offset range.
return (sbyte)(data[0] - byte.MaxValue);
}
/// <summary>
/// Retrieves an array from a byte array using the supplied converter
/// to read each individual element from the supplied byte array
/// </summary>
/// <param name="data"></param>
/// <param name="elementLengthBytes"></param>
/// <param name="converter"></param>
/// <returns></returns>
private static Array GetArray<T>(byte[] data, int elementLengthBytes, ConverterMethod<T> converter)
{
Array convertedData = new T[data.Length / elementLengthBytes];
var buffer = new byte[elementLengthBytes];
// Read each element from the array
for (int elementCount = 0; elementCount < data.Length / elementLengthBytes; elementCount++)
{
// Place the data for the current element into the buffer
Array.Copy(data, elementCount * elementLengthBytes, buffer, 0, elementLengthBytes);
// Process the data and place it into the output array
convertedData.SetValue(converter(buffer), elementCount);
}
return convertedData;
}
/// <summary>
/// A delegate used to invoke any of the data conversion methods
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
/// <remarks>Although this could be defined as covariant, it wouldn't work on Windows Phone</remarks>
private delegate T ConverterMethod<out T>(byte[] data);
#endregion
#region Stream seek methods - used to get to locations within the JPEG
/// <summary>
/// Scans to the Exif block
/// </summary>
private void ReadToExifStart()
{
// The file has a number of blocks (Exif/JFIF), each of which
// has a tag number followed by a length. We scan the document until the required tag (0xFFE1)
// is found. All tags start with FF, so a non FF tag indicates an error.
// Get the next tag.
byte markerStart;
byte markerNumber = 0;
while (((markerStart = _reader.ReadByte()) == 0xFF) && (markerNumber = _reader.ReadByte()) != 0xE1)
{
// Get the length of the data.
ushort dataLength = ReadUShort();
// Jump to the end of the data (note that the size field includes its own size)!
_stream.Seek(dataLength - 2, SeekOrigin.Current);
}
// It's only success if we found the 0xFFE1 marker
if (markerStart != 0xFF || markerNumber != 0xE1)
throw new ExifLibException("Could not find Exif data block");
}
/// <summary>
/// Reads through the Exif data and builds an index of all Exif tags in the document
/// </summary>
/// <returns></returns>
private void CreateTagIndex()
{
// The next 4 bytes are the size of the Exif data.
ReadUShort();
// Next is the Exif data itself. It starts with the ASCII "Exif" followed by 2 zero bytes.
if (ReadString(4) != "Exif")
throw new ExifLibException("Exif data not found");
// 2 zero bytes
if (ReadUShort() != 0)
throw new ExifLibException("Malformed Exif data");
// We're now into the TIFF format
_tiffHeaderStart = _stream.Position;
// What byte align will be used for the TIFF part of the document? II for Intel, MM for Motorola
_isLittleEndian = ReadString(2) == "II";
// Next 2 bytes are always the same.
if (ReadUShort() != 0x002A)
throw new ExifLibException("Error in TIFF data");
// Get the offset to the IFD (image file directory)
var ifdOffset = ReadUint();
// Note that this offset is from the first byte of the TIFF header. Jump to the IFD.
_stream.Position = ifdOffset + _tiffHeaderStart;
// Catalogue this first IFD (there will be another IFD)
_ifd0Catalogue = new Dictionary<ushort, long>();
CatalogueIFD(ref _ifd0Catalogue);
// The address to the IFD1 (the thumbnail IFD) is located immediately after the main IFD
_ifd1Offset = ReadUint();
// There's more data stored in the subifd, the offset to which is found in tag 0x8769.
// As with all TIFF offsets, it will be relative to the first byte of the TIFF header.
uint offset;
if (!GetTagValue(_ifd0Catalogue, 0x8769, out offset))
throw new ExifLibException("Unable to locate Exif data");
// Jump to the exif SubIFD
_stream.Position = offset + _tiffHeaderStart;
// Add the subIFD to the catalogue too
CatalogueIFD(ref _ifd0Catalogue);
// Go to the GPS IFD and catalogue that too. It's an optional
// section.
if (GetTagValue(_ifd0Catalogue, 0x8825, out offset))
{
// Jump to the GPS SubIFD
_stream.Position = offset + _tiffHeaderStart;
// Add the subIFD to the catalogue too
CatalogueIFD(ref _ifd0Catalogue);
}
// Finally, catalogue the thumbnail IFD if it's present
if (_ifd1Offset != 0)
{
_stream.Position = _ifd1Offset + _tiffHeaderStart;
_ifd1Catalogue = new Dictionary<ushort, long>();
CatalogueIFD(ref _ifd1Catalogue);
}
}
#endregion
#region Exif data catalog and retrieval methods
public bool GetTagValue<T>(ExifTags tag, out T result)
{
return GetTagValue((ushort)tag, out result);
}
public bool GetTagValue<T>(ushort tagId, out T result)
{
// All useful EXIF tags are stored in the ifd0 catalogue. The ifd1 catalogue is only for thumbnail retrieval.
Initialize();
return GetTagValue(_ifd0Catalogue, tagId, out result);
}
/// <summary>
/// Retrieves an Exif value with the requested tag ID
/// </summary>
private bool GetTagValue<T>(IDictionary<ushort, long> tagDictionary, ushort tagId, out T result)
{
ushort tiffDataType;
uint numberOfComponents;
var tagData = GetTagBytes(tagDictionary, tagId, out tiffDataType, out numberOfComponents);
if (tagData == null)
{
result = default(T);
return false;
}
var fieldLength = GetTIFFFieldLength(tiffDataType);
// Convert the data to the appropriate datatype. Note the weird boxing via object.
// The compiler doesn't like it otherwise.
switch (tiffDataType)
{
case 1:
// unsigned byte
if (numberOfComponents == 1)
result = (T)(object)tagData[0];
else
result = (T)(object)tagData;
return true;
case 2:
// ascii string
var str = Encoding.UTF8.GetString(tagData, 0, tagData.Length);
// There may be a null character within the string
var nullCharIndex = str.IndexOf('\0');
if (nullCharIndex != -1)
str = str.Substring(0, nullCharIndex);
// Special processing for dates.
if (typeof(T) == typeof(DateTime))
{
DateTime dateResult;
var success = ToDateTime(str, out dateResult);
result = (T)(object)dateResult;
return success;
}
result = (T)(object)str;
return true;
case 3:
// unsigned short
if (numberOfComponents == 1)
result = (T)(object)ToUShort(tagData);
else
result = (T)(object)GetArray(tagData, fieldLength, ToUShort);
return true;
case 4:
// unsigned long
if (numberOfComponents == 1)
result = (T)(object)ToUint(tagData);
else
result = (T)(object)GetArray(tagData, fieldLength, ToUint);
return true;
case 5:
// unsigned rational
if (numberOfComponents == 1)
{
// Special case - sometimes it's useful to retrieve the numerator and
// denominator in their raw format
if (typeof(T).IsArray)
result = (T)(object)ToURationalFraction(tagData);
else
result = (T)(object)ToURational(tagData);
}
else
result = (T)(object)GetArray(tagData, fieldLength, ToURational);
return true;
case 6:
// signed byte
if (numberOfComponents == 1)
result = (T)(object)ToSByte(tagData);
else
result = (T)(object)GetArray(tagData, fieldLength, ToSByte);
return true;
case 7:
// undefined. Treat it as a byte.
if (numberOfComponents == 1)
result = (T)(object)tagData[0];
else
result = (T)(object)tagData;
return true;
case 8:
// Signed short
if (numberOfComponents == 1)
result = (T)(object)ToShort(tagData);
else
result = (T)(object)GetArray(tagData, fieldLength, ToShort);
return true;
case 9:
// Signed long
if (numberOfComponents == 1)
result = (T)(object)ToInt(tagData);
else
result = (T)(object)GetArray(tagData, fieldLength, ToInt);
return true;
case 10:
// signed rational
if (numberOfComponents == 1)
{
// Special case - sometimes it's useful to retrieve the numerator and
// denominator in their raw format
if (typeof(T).IsArray)
result = (T)(object)ToRationalFraction(tagData);
else
result = (T)(object)ToRational(tagData);
}
else
result = (T)(object)GetArray(tagData, fieldLength, ToRational);
return true;
case 11:
// single float
if (numberOfComponents == 1)
result = (T)(object)ToSingle(tagData);
else
result = (T)(object)GetArray(tagData, fieldLength, ToSingle);
return true;
case 12:
// double float
if (numberOfComponents == 1)
result = (T)(object)ToDouble(tagData);
else
result = (T)(object)GetArray(tagData, fieldLength, ToDouble);
return true;
default:
throw new ExifLibException(string.Format("Unknown TIFF datatype: {0}", tiffDataType));
}
}
private static bool ToDateTime(string str, out DateTime result)
{
// From page 28 of the Exif 2.2 spec (http://www.exif.org/Exif2-2.PDF):
// "When the field is left blank, it is treated as unknown ... When the date and time are unknown,
// all the character spaces except colons (":") may be filled with blank characters"
if (string.IsNullOrEmpty(str) || _nullDateTimeMatcher.IsMatch(str))
{
result = DateTime.MinValue;
return false;
}
// There are 2 types of date - full date/time stamps, and plain dates. Dates are 10 characters long.
if (str.Length == 10)
{
result = DateTime.ParseExact(str, "yyyy:MM:dd", CultureInfo.InvariantCulture);
return true;
}
// "The format is "YYYY:MM:DD HH:MM:SS" with time shown in 24-hour format, and the date and time separated by one blank character [20.H].
result = DateTime.ParseExact(str, "yyyy:MM:dd HH:mm:ss", CultureInfo.InvariantCulture);
return true;
}
/// <summary>
/// Gets the data in the specified tag ID, starting from before the IFD block.
/// </summary>
/// <param name="tiffDataType"></param>
/// <param name="numberOfComponents">The number of items which make up the data item - i.e. for a string, this will be the
/// number of characters in the string</param>
/// <param name="tagDictionary"></param>
/// <param name="tagId"></param>
private byte[] GetTagBytes(IDictionary<ushort, long> tagDictionary, ushort tagId, out ushort tiffDataType, out uint numberOfComponents)
{
// Get the tag's offset from the catalogue and do some basic error checks
if (_stream == null || _reader == null || tagDictionary == null || !tagDictionary.ContainsKey(tagId))
{
tiffDataType = 0;
numberOfComponents = 0;
return null;
}
var tagOffset = tagDictionary[tagId];
// Jump to the TIFF offset
_stream.Position = tagOffset;
// Read the tag number from the file
var currenttagId = ReadUShort();
if (currenttagId != tagId)
throw new ExifLibException("Tag number not at expected offset");
// Read the offset to the Exif IFD
tiffDataType = ReadUShort();
numberOfComponents = ReadUint();
var tagData = ReadBytes(4);
// If the total space taken up by the field is longer than the
// 2 bytes afforded by the tagData, tagData will contain an offset
// to the actual data.
var dataSize = (int)(numberOfComponents * GetTIFFFieldLength(tiffDataType));
if (dataSize > 4)
{
var offsetAddress = ToUShort(tagData);
return ReadBytes(offsetAddress, dataSize);
}
// The value is stored in the tagData starting from the left
Array.Resize(ref tagData, dataSize);
return tagData;
}
/// <summary>
/// Records all Exif tags and their offsets within
/// the file from the current IFD
/// </summary>
private void CatalogueIFD(ref Dictionary<ushort, long> tagOffsets)
{
// Assume we're just before the IFD.
// First 2 bytes is the number of entries in this IFD
var entryCount = ReadUShort();
for (ushort currentEntry = 0; currentEntry < entryCount; currentEntry++)
{
var currentTagNumber = ReadUShort();
// Record this in the catalogue
tagOffsets[currentTagNumber] = _stream.Position - 2;
// Go to the end of this item (10 bytes, as each entry is 12 bytes long)
_stream.Seek(10, SeekOrigin.Current);
}
}
#endregion
#region Thumbnail retrieval
/// <summary>
/// Retrieves a JPEG thumbnail from the image if one is present. Note that this method cannot retrieve thumbnails encoded in other formats,
/// but since the DCF specification specifies that thumbnails must be JPEG, this method will be sufficient for most purposes
/// See http://gvsoft.homedns.org/exif/exif-explanation.html#TIFFThumbs or http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf for
/// details on the encoding of TIFF thumbnails
/// </summary>
/// <returns></returns>
public byte[] GetJpegThumbnailBytes()
{
Initialize();
if (_ifd1Catalogue == null)
return null;
// Get the thumbnail encoding
ushort compression;
if (!GetTagValue(_ifd1Catalogue, (ushort)ExifTags.Compression, out compression))
return null;
// This method only handles JPEG thumbnails (compression type 6)
if (compression != 6)
return null;
// Get the location of the thumbnail
uint offset;
if (!GetTagValue(_ifd1Catalogue, (ushort)ExifTags.JPEGInterchangeFormat, out offset))
return null;
// Get the length of the thumbnail data
uint length;
if (!GetTagValue(_ifd1Catalogue, (ushort)ExifTags.JPEGInterchangeFormatLength, out length))
return null;
_stream.Position = offset;
// The thumbnail may be padded, so we scan forward until we reach the JPEG header (0xFFD8) or the end of the file
int currentByte;
var previousByte = -1;
while ((currentByte = _stream.ReadByte()) != -1)
{
if (previousByte == 0xFF && currentByte == 0xD8)
break;
previousByte = currentByte;
}
if (currentByte != 0xD8)
return null;
// Step back to the start of the JPEG header
_stream.Position -= 2;
var imageBytes = new byte[length];
_stream.Read(imageBytes, 0, (int)length);
// A valid JPEG stream ends with 0xFFD9. The stream may be padded at the end with multiple 0xFF bytes.
var jpegStreamEnd = (int)length - 1;
while (jpegStreamEnd > 0 && imageBytes[jpegStreamEnd] == 0xFF)
jpegStreamEnd--;
if (jpegStreamEnd <= 0 || imageBytes[jpegStreamEnd] != 0xD9 || imageBytes[jpegStreamEnd - 1] != 0xFF)
return null;
return imageBytes;
}
#endregion
#region IDisposable Members
~ExifReader()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (_reader != null)
_reader.Dispose();
if (!_leaveOpen)
{
// Make sure the file handle is released
if (_stream != null)
_stream.Dispose();
}
}
_reader = null;
_stream = null;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
public class ExifLibException : Exception
{
public ExifLibException()
{
}
public ExifLibException(string message)
: base(message)
{
}
public ExifLibException(string message, Exception innerException)
: base(message, innerException)
{
}
}
}