exif.dart
19 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
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
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'dart:convert';
import 'dart:typed_data';
import 'image.dart';
/// Jpeg metadata extraction
class PdfJpegInfo {
/// Load a Jpeg image's metadata
factory PdfJpegInfo(Uint8List image) {
assert(image != null);
final buffer = image.buffer.asByteData();
int width;
int height;
int color;
var offset = 0;
while (offset < buffer.lengthInBytes) {
while (buffer.getUint8(offset) == 0xff) {
offset++;
}
final mrkr = buffer.getUint8(offset);
offset++;
if (mrkr == 0xd8) {
continue; // SOI
}
if (mrkr == 0xd9) {
break; // EOI
}
if (0xd0 <= mrkr && mrkr <= 0xd7) {
continue;
}
if (mrkr == 0x01) {
continue; // TEM
}
final len = buffer.getUint16(offset);
offset += 2;
if (mrkr >= 0xc0 && mrkr <= 0xc2) {
height = buffer.getUint16(offset + 1);
width = buffer.getUint16(offset + 3);
color = buffer.getUint8(offset + 5);
break;
}
offset += len - 2;
}
if (height == null) {
throw 'Unable to find a Jpeg image in the file';
}
final tags = _findExifInJpeg(buffer);
return PdfJpegInfo._(width, height, color, tags);
}
PdfJpegInfo._(this.width, this.height, this._color, this.tags);
/// Width of the image
final int width;
/// Height of the image
final int height;
final int _color;
/// Is the image color or greyscale
bool get isRGB => _color == 3;
/// Exif tags discovered
final Map<PdfExifTag, dynamic> tags;
/// EXIF version
String get exifVersion => tags == null || tags[PdfExifTag.ExifVersion] == null
? null
: utf8.decode(tags[PdfExifTag.ExifVersion]);
/// Flashpix format version
String get flashpixVersion =>
tags == null || tags[PdfExifTag.FlashpixVersion] == null
? null
: utf8.decode(tags[PdfExifTag.FlashpixVersion]);
/// Rotation angle of this image
PdfImageOrientation get orientation {
if (tags == null || tags[PdfExifTag.Orientation] == null) {
return PdfImageOrientation.topLeft;
}
try {
return PdfImageOrientation.values[tags[PdfExifTag.Orientation] - 1];
} on RangeError {
return PdfImageOrientation.topLeft;
}
}
/// Exif horizontal resolution
double get xResolution => tags == null || tags[PdfExifTag.XResolution] == null
? null
: tags[PdfExifTag.XResolution][0].toDouble() /
tags[PdfExifTag.XResolution][1].toDouble();
/// Exif vertical resolution
double get yResolution => tags == null || tags[PdfExifTag.YResolution] == null
? null
: tags[PdfExifTag.YResolution][0].toDouble() /
tags[PdfExifTag.YResolution][1].toDouble();
/// Exif horizontal pixel dimension
int get pixelXDimension =>
tags == null || tags[PdfExifTag.PixelXDimension] == null
? width
: tags[PdfExifTag.PixelXDimension];
/// Exif vertical pixel dimension
int get pixelYDimension =>
tags == null || tags[PdfExifTag.PixelYDimension] == null
? height
: tags[PdfExifTag.PixelYDimension];
@override
String toString() => '''width: $width height: $height
exifVersion: $exifVersion flashpixVersion: $flashpixVersion
xResolution: $xResolution yResolution: $yResolution
pixelXDimension: $pixelXDimension pixelYDimension: $pixelYDimension
orientation: $orientation''';
static Map<PdfExifTag, dynamic> _findExifInJpeg(ByteData buffer) {
if ((buffer.getUint8(0) != 0xFF) || (buffer.getUint8(1) != 0xD8)) {
return <PdfExifTag, dynamic>{}; // Not a valid JPEG
}
var offset = 2;
final length = buffer.lengthInBytes;
int marker;
while (offset < length) {
final lastValue = buffer.getUint8(offset);
if (lastValue != 0xFF) {
return <PdfExifTag,
dynamic>{}; // Not a valid marker at offset $offset, found: $lastValue
}
marker = buffer.getUint8(offset + 1);
// we could implement handling for other markers here,
// but we're only looking for 0xFFE1 for EXIF data
if (marker == 0xE1) {
return _readEXIFData(buffer, offset + 4);
} else {
offset += 2 + buffer.getUint16(offset + 2);
}
}
return <PdfExifTag, dynamic>{};
}
static Map<PdfExifTag, dynamic> _readTags(
ByteData file,
int tiffStart,
int dirStart,
Endian bigEnd,
) {
final entries = file.getUint16(dirStart, bigEnd);
final tags = <PdfExifTag, dynamic>{};
int entryOffset;
for (var i = 0; i < entries; i++) {
entryOffset = dirStart + i * 12 + 2;
final tagId = file.getUint16(entryOffset, bigEnd);
final tag = _exifTags[tagId];
if (tag != null) {
tags[tag] = _readTagValue(
file,
entryOffset,
tiffStart,
dirStart,
bigEnd,
);
}
}
return tags;
}
static dynamic _readTagValue(
ByteData file,
int entryOffset,
int tiffStart,
int dirStart,
Endian bigEnd,
) {
final type = file.getUint16(entryOffset + 2, bigEnd);
final numValues = file.getUint32(entryOffset + 4, bigEnd);
final valueOffset = file.getUint32(entryOffset + 8, bigEnd) + tiffStart;
switch (type) {
case 1: // byte, 8-bit unsigned int
case 7: // undefined, 8-bit byte, value depending on field
if (numValues == 1) {
return file.getUint8(entryOffset + 8);
}
final offset = numValues > 4 ? valueOffset : (entryOffset + 8);
final result = Uint8List(numValues);
for (var i = 0; i < result.length; ++i) {
result[i] = file.getUint8(offset + i);
}
return result;
case 2: // ascii, 8-bit byte
final offset = numValues > 4 ? valueOffset : (entryOffset + 8);
return _getStringFromDB(file, offset, numValues - 1);
case 3: // short, 16 bit int
if (numValues == 1) {
return file.getUint16(entryOffset + 8, bigEnd);
}
final offset = numValues > 2 ? valueOffset : (entryOffset + 8);
final result = Uint16List(numValues);
for (var i = 0; i < result.length; ++i) {
result[i] = file.getUint16(offset + i * 2, bigEnd);
}
return result;
case 4: // long, 32 bit int
if (numValues == 1) {
return file.getUint32(entryOffset + 8, bigEnd);
}
final offset = valueOffset;
final result = Uint32List(numValues);
for (var i = 0; i < result.length; ++i) {
result[i] = file.getUint32(offset + i * 4, bigEnd);
}
return result;
case 5: // rational = two long values, first is numerator, second is denominator
if (numValues == 1) {
final numerator = file.getUint32(valueOffset, bigEnd);
final denominator = file.getUint32(valueOffset + 4, bigEnd);
return <int>[numerator, denominator];
}
final offset = valueOffset;
final result = List<List<int>>(numValues);
for (var i = 0; i < result.length; ++i) {
final numerator = file.getUint32(offset + i * 8, bigEnd);
final denominator = file.getUint32(offset + i * 8 + 4, bigEnd);
result[i] = <int>[numerator, denominator];
}
return result;
case 9: // slong, 32 bit signed int
if (numValues == 1) {
return file.getInt32(entryOffset + 8, bigEnd);
}
final offset = valueOffset;
final result = Int32List(numValues);
for (var i = 0; i < result.length; ++i) {
result[i] = file.getInt32(offset + i * 4, bigEnd);
}
return result;
case 10: // signed rational, two slongs, first is numerator, second is denominator
if (numValues == 1) {
final numerator = file.getInt32(valueOffset, bigEnd);
final denominator = file.getInt32(valueOffset + 4, bigEnd);
return <int>[numerator, denominator];
}
final offset = valueOffset;
final result = List<List<int>>(numValues);
for (var i = 0; i < result.length; ++i) {
final numerator = file.getInt32(offset + i * 8, bigEnd);
final denominator = file.getInt32(offset + i * 8 + 4, bigEnd);
result[i] = <int>[numerator, denominator];
}
return result;
case 11: // single float, 32 bit float
if (numValues == 1) {
return file.getFloat32(entryOffset + 8, bigEnd);
}
final offset = valueOffset;
final result = Float32List(numValues);
for (var i = 0; i < result.length; ++i) {
result[i] = file.getFloat32(offset + i * 4, bigEnd);
}
return result;
case 12: // double float, 64 bit float
if (numValues == 1) {
return file.getFloat64(entryOffset + 8, bigEnd);
}
final offset = valueOffset;
final result = Float64List(numValues);
for (var i = 0; i < result.length; ++i) {
result[i] = file.getFloat64(offset + i * 8, bigEnd);
}
return result;
}
}
static String _getStringFromDB(ByteData buffer, int start, int length) {
return utf8.decode(
List<int>.generate(length, (int i) => buffer.getUint8(start + i)),
allowMalformed: true);
}
static Map<PdfExifTag, dynamic> _readEXIFData(ByteData buffer, int start) {
final startingString = _getStringFromDB(buffer, start, 4);
if (startingString != 'Exif') {
// Not valid EXIF data! $startingString
return null;
}
Endian bigEnd;
final tiffOffset = start + 6;
// test for TIFF validity and endianness
if (buffer.getUint16(tiffOffset) == 0x4949) {
bigEnd = Endian.little;
} else if (buffer.getUint16(tiffOffset) == 0x4D4D) {
bigEnd = Endian.big;
} else {
// Not valid TIFF data! (no 0x4949 or 0x4D4D)
return null;
}
if (buffer.getUint16(tiffOffset + 2, bigEnd) != 0x002A) {
// Not valid TIFF data! (no 0x002A)
return null;
}
final firstIFDOffset = buffer.getUint32(tiffOffset + 4, bigEnd);
if (firstIFDOffset < 0x00000008) {
// Not valid TIFF data! (First offset less than 8) $firstIFDOffset
return null;
}
final tags =
_readTags(buffer, tiffOffset, tiffOffset + firstIFDOffset, bigEnd);
if (tags.containsKey(PdfExifTag.ExifIFDPointer)) {
final exifData = _readTags(buffer, tiffOffset,
tiffOffset + tags[PdfExifTag.ExifIFDPointer], bigEnd);
tags.addAll(exifData);
}
return tags;
}
static const Map<int, PdfExifTag> _exifTags = <int, PdfExifTag>{
0x9000: PdfExifTag.ExifVersion,
0xA000: PdfExifTag.FlashpixVersion,
0xA001: PdfExifTag.ColorSpace,
0xA002: PdfExifTag.PixelXDimension,
0xA003: PdfExifTag.PixelYDimension,
0x9101: PdfExifTag.ComponentsConfiguration,
0x9102: PdfExifTag.CompressedBitsPerPixel,
0x927C: PdfExifTag.MakerNote,
0x9286: PdfExifTag.UserComment,
0xA004: PdfExifTag.RelatedSoundFile,
0x9003: PdfExifTag.DateTimeOriginal,
0x9004: PdfExifTag.DateTimeDigitized,
0x9290: PdfExifTag.SubsecTime,
0x9291: PdfExifTag.SubsecTimeOriginal,
0x9292: PdfExifTag.SubsecTimeDigitized,
0x829A: PdfExifTag.ExposureTime,
0x829D: PdfExifTag.FNumber,
0x8822: PdfExifTag.ExposureProgram,
0x8824: PdfExifTag.SpectralSensitivity,
0x8827: PdfExifTag.ISOSpeedRatings,
0x8828: PdfExifTag.OECF,
0x9201: PdfExifTag.ShutterSpeedValue,
0x9202: PdfExifTag.ApertureValue,
0x9203: PdfExifTag.BrightnessValue,
0x9204: PdfExifTag.ExposureBias,
0x9205: PdfExifTag.MaxApertureValue,
0x9206: PdfExifTag.SubjectDistance,
0x9207: PdfExifTag.MeteringMode,
0x9208: PdfExifTag.LightSource,
0x9209: PdfExifTag.Flash,
0x9214: PdfExifTag.SubjectArea,
0x920A: PdfExifTag.FocalLength,
0xA20B: PdfExifTag.FlashEnergy,
0xA20C: PdfExifTag.SpatialFrequencyResponse,
0xA20E: PdfExifTag.FocalPlaneXResolution,
0xA20F: PdfExifTag.FocalPlaneYResolution,
0xA210: PdfExifTag.FocalPlaneResolutionUnit,
0xA214: PdfExifTag.SubjectLocation,
0xA215: PdfExifTag.ExposureIndex,
0xA217: PdfExifTag.SensingMethod,
0xA300: PdfExifTag.FileSource,
0xA301: PdfExifTag.SceneType,
0xA302: PdfExifTag.CFAPattern,
0xA401: PdfExifTag.CustomRendered,
0xA402: PdfExifTag.ExposureMode,
0xA403: PdfExifTag.WhiteBalance,
0xA404: PdfExifTag.DigitalZoomRation,
0xA405: PdfExifTag.FocalLengthIn35mmFilm,
0xA406: PdfExifTag.SceneCaptureType,
0xA407: PdfExifTag.GainControl,
0xA408: PdfExifTag.Contrast,
0xA409: PdfExifTag.Saturation,
0xA40A: PdfExifTag.Sharpness,
0xA40B: PdfExifTag.DeviceSettingDescription,
0xA40C: PdfExifTag.SubjectDistanceRange,
0xA005: PdfExifTag.InteroperabilityIFDPointer,
0xA420: PdfExifTag.ImageUniqueID,
0x0100: PdfExifTag.ImageWidth,
0x0101: PdfExifTag.ImageHeight,
0x8769: PdfExifTag.ExifIFDPointer,
0x8825: PdfExifTag.GPSInfoIFDPointer,
0x0102: PdfExifTag.BitsPerSample,
0x0103: PdfExifTag.Compression,
0x0106: PdfExifTag.PhotometricInterpretation,
0x0112: PdfExifTag.Orientation,
0x0115: PdfExifTag.SamplesPerPixel,
0x011C: PdfExifTag.PlanarConfiguration,
0x0212: PdfExifTag.YCbCrSubSampling,
0x0213: PdfExifTag.YCbCrPositioning,
0x011A: PdfExifTag.XResolution,
0x011B: PdfExifTag.YResolution,
0x0128: PdfExifTag.ResolutionUnit,
0x0111: PdfExifTag.StripOffsets,
0x0116: PdfExifTag.RowsPerStrip,
0x0117: PdfExifTag.StripByteCounts,
0x0201: PdfExifTag.JPEGInterchangeFormat,
0x0202: PdfExifTag.JPEGInterchangeFormatLength,
0x012D: PdfExifTag.TransferFunction,
0x013E: PdfExifTag.WhitePoint,
0x013F: PdfExifTag.PrimaryChromaticities,
0x0211: PdfExifTag.YCbCrCoefficients,
0x0214: PdfExifTag.ReferenceBlackWhite,
0x0132: PdfExifTag.DateTime,
0x010E: PdfExifTag.ImageDescription,
0x010F: PdfExifTag.Make,
0x0110: PdfExifTag.Model,
0x0131: PdfExifTag.Software,
0x013B: PdfExifTag.Artist,
0x8298: PdfExifTag.Copyright,
};
}
/// Possible Exif tags
enum PdfExifTag {
// version tags
/// EXIF version
ExifVersion,
/// Flashpix format version
FlashpixVersion,
// colorspace tags
/// Color space information tag
ColorSpace,
// image configuration
/// Valid width of meaningful image
PixelXDimension,
/// Valid height of meaningful image
PixelYDimension,
/// Information about channels
ComponentsConfiguration,
/// Compressed bits per pixel
CompressedBitsPerPixel,
// user information
/// Any desired information written by the manufacturer
MakerNote,
/// Comments by user
UserComment,
// related file
/// Name of related sound file
RelatedSoundFile,
// date and time
/// Date and time when the original image was generated
DateTimeOriginal,
/// Date and time when the image was stored digitally
DateTimeDigitized,
/// Fractions of seconds for DateTime
SubsecTime,
/// Fractions of seconds for DateTimeOriginal
SubsecTimeOriginal,
/// Fractions of seconds for DateTimeDigitized
SubsecTimeDigitized,
// picture-taking conditions
/// Exposure time (in seconds)
ExposureTime,
/// F number
FNumber,
/// Exposure program
ExposureProgram,
/// Spectral sensitivity
SpectralSensitivity,
/// ISO speed rating
ISOSpeedRatings,
/// Optoelectric conversion factor
OECF,
/// Shutter speed
ShutterSpeedValue,
/// Lens aperture
ApertureValue,
/// Value of brightness
BrightnessValue,
/// Exposure bias
ExposureBias,
/// Smallest F number of lens
MaxApertureValue,
/// Distance to subject in meters
SubjectDistance,
/// Metering mode
MeteringMode,
/// Kind of light source
LightSource,
/// Flash status
Flash,
/// Location and area of main subject
SubjectArea,
/// Focal length of the lens in mm
FocalLength,
/// Strobe energy in BCPS
FlashEnergy,
/// Spatial Frequency Response
SpatialFrequencyResponse,
/// Number of pixels in width direction per FocalPlaneResolutionUnit
FocalPlaneXResolution,
/// Number of pixels in height direction per FocalPlaneResolutionUnit
FocalPlaneYResolution,
/// Unit for measuring FocalPlaneXResolution and FocalPlaneYResolution
FocalPlaneResolutionUnit,
/// Location of subject in image
SubjectLocation,
/// Exposure index selected on camera
ExposureIndex,
/// Image sensor type
SensingMethod,
/// Image source (3 == DSC)
FileSource,
/// Scene type (1 == directly photographed)
SceneType,
/// Color filter array geometric pattern
CFAPattern,
/// Special processing
CustomRendered,
/// Exposure mode
ExposureMode,
/// 1 = auto white balance, 2 = manual
WhiteBalance,
/// Digital zoom ratio
DigitalZoomRation,
/// Equivalent foacl length assuming 35mm film camera (in mm)
FocalLengthIn35mmFilm,
/// Type of scene
SceneCaptureType,
/// Degree of overall image gain adjustment
GainControl,
/// Direction of contrast processing applied by camera
Contrast,
/// Direction of saturation processing applied by camera
Saturation,
/// Direction of sharpness processing applied by camera
Sharpness,
/// Device Setting Description
DeviceSettingDescription,
/// Distance to subject
SubjectDistanceRange,
// other tags
/// Interoperability IFD Pointer
InteroperabilityIFDPointer,
//// Identifier assigned uniquely to each image
ImageUniqueID,
// tiff Tags
/// ImageWidth
ImageWidth,
/// ImageHeight
ImageHeight,
/// ExifIFDPointer
ExifIFDPointer,
/// GPSInfoIFDPointer
GPSInfoIFDPointer,
/// BitsPerSample
BitsPerSample,
/// Compression
Compression,
/// PhotometricInterpretation
PhotometricInterpretation,
/// Orientation
Orientation,
/// SamplesPerPixel
SamplesPerPixel,
/// PlanarConfiguration
PlanarConfiguration,
/// YCbCrSubSampling
YCbCrSubSampling,
/// YCbCrPositioning
YCbCrPositioning,
/// XResolution
XResolution,
/// YResolution
YResolution,
/// ResolutionUnit
ResolutionUnit,
/// StripOffsets
StripOffsets,
/// RowsPerStrip
RowsPerStrip,
/// StripByteCounts
StripByteCounts,
/// JPEGInterchangeFormat
JPEGInterchangeFormat,
/// JPEGInterchangeFormatLength
JPEGInterchangeFormatLength,
/// TransferFunction
TransferFunction,
/// WhitePoint
WhitePoint,
/// PrimaryChromaticities
PrimaryChromaticities,
/// YCbCrCoefficients
YCbCrCoefficients,
/// ReferenceBlackWhite
ReferenceBlackWhite,
/// DateTime
DateTime,
/// ImageDescription
ImageDescription,
/// Make
Make,
/// Model
Model,
/// Software
Software,
/// Artist
Artist,
/// Copyright
Copyright,
}