/
ExifReader.cs
185 lines (150 loc) · 5.18 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
using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
namespace PhotoSauce.MagicScaler;
internal readonly record struct ExifItem(ushort ID, ExifType Type, uint Count, Range Range);
internal unsafe ref struct ExifReader
{
private readonly ReadOnlySpan<byte> span;
private readonly int firstTag;
private readonly ushort count;
private readonly bool swap;
private ushort read;
private ExifItem curr;
public readonly int TagCount => count;
private ExifReader(ReadOnlySpan<byte> src, int offset, bool bswap)
{
ushort cnt = MemoryMarshal.Read<ushort>(src[offset..]);
if (bswap)
cnt = BufferUtil.ReverseEndianness(cnt);
span = src;
firstTag = offset + sizeof(ushort);
count = cnt;
swap = bswap;
}
public static ExifReader Create(ReadOnlySpan<byte> span)
{
if (span.Length < ExifConstants.MinExifLength)
return default;
bool bswap = !BitConverter.IsLittleEndian;
var rdr = span.AsReader(..ExifConstants.TiffHeadLength);
uint mark = rdr.Read<uint>(bswap);
if (mark is ExifConstants.MarkerII or ExifConstants.MarkerMM)
{
bswap = bswap == (mark is ExifConstants.MarkerII);
uint offs = rdr.Read<uint>(bswap);
if (offs <= (uint)(span.Length - ExifConstants.MinDirLength))
return new(span, (int)offs, bswap);
}
return default;
}
public readonly T GetValue<T>(in ExifItem item) where T : unmanaged
{
T val = MemoryMarshal.Read<T>(span[item.Range]);
if (sizeof(T) > sizeof(byte) && swap)
val = BufferUtil.ReverseEndianness(val);
return val;
}
public readonly void GetValues<T>(in ExifItem item, Span<T> dest) where T : unmanaged
{
int cv = Math.Min((int)item.Count, dest.Length);
MemoryMarshal.Cast<byte, T>(span[item.Range])[..cv].CopyTo(dest);
if (sizeof(T) == sizeof(byte) || !swap)
return;
for (int i = 0; i < dest.Length; i++)
dest[i] = BufferUtil.ReverseEndianness(dest[i]);
}
public readonly T CoerceValue<T>(in ExifItem item) where T : unmanaged
{
if (!UnsafeUtil.IsIntegerPrimitive<T>() && !UnsafeUtil.IsFloatPrimitive<T>())
throw new ArgumentException($"Coercion not implemented for type {typeof(T).Name}.", nameof(T));
int dlen = item.Type.GetElementSize();
if (dlen == sizeof(T) && item.Type.IsFloating() == UnsafeUtil.IsFloatPrimitive<T>())
return GetValue<T>(item);
if (typeof(T) == typeof(float))
{
if (item.Type == ExifType.Double)
return Unsafe.As<float, T>(ref Unsafe.AsRef((float)GetValue<double>(item)));
return Unsafe.As<float, T>(ref Unsafe.AsRef((float)CoerceValue<int>(item)));
}
if (typeof(T) == typeof(double))
{
if (item.Type == ExifType.Float)
return Unsafe.As<double, T>(ref Unsafe.AsRef((double)GetValue<float>(item)));
return Unsafe.As<double, T>(ref Unsafe.AsRef((double)CoerceValue<long>(item)));
}
if (dlen >= sizeof(T))
{
if (item.Type == ExifType.Float)
return Unsafe.As<int, T>(ref Unsafe.AsRef((int)GetValue<float>(item)));
if (item.Type == ExifType.Double)
return Unsafe.As<long, T>(ref Unsafe.AsRef((long)GetValue<double>(item)));
if (dlen == sizeof(short))
return Unsafe.As<short, T>(ref Unsafe.AsRef(GetValue<short>(item)));
if (dlen == sizeof(int))
return Unsafe.As<int, T>(ref Unsafe.AsRef(GetValue<int>(item)));
return Unsafe.As<long, T>(ref Unsafe.AsRef(GetValue<long>(item)));
}
long val;
if (item.Type == ExifType.Float)
val = (long)GetValue<float>(item);
else if (item.Type.IsSigned())
val = dlen switch {
sizeof(sbyte) => GetValue<sbyte>(item),
sizeof(short) => GetValue<short>(item),
sizeof(int) => GetValue<int>(item),
sizeof(long) => GetValue<long>(item),
_ => default
};
else
val = (long)(dlen switch {
sizeof(byte) => GetValue<byte>(item),
sizeof(ushort) => GetValue<ushort>(item),
sizeof(uint) => GetValue<uint>(item),
sizeof(ulong) => GetValue<ulong>(item),
_ => default
});
return Unsafe.As<long, T>(ref Unsafe.AsRef(val));
}
public readonly ExifReader GetReader(in ExifItem item)
{
uint offs = GetValue<uint>(item);
if (offs <= (uint)(span.Length - ExifConstants.MinDirLength))
return new ExifReader(span, (int)offs, swap);
return default;
}
public ExifReader GetEnumerator()
{
read = 0;
return this;
}
public bool MoveNext()
{
int pos = read++;
int offs = firstTag + pos * ExifConstants.MinTagLength;
if (pos >= count || offs + ExifConstants.MinTagLength > span.Length)
return false;
var rdr = span.AsReader(offs, ExifConstants.MinTagLength);
ushort id = rdr.Read<ushort>(swap);
ushort dt = rdr.Read<ushort>(swap);
uint cv = rdr.Read<uint>(swap);
uint dpos = (uint)offs + (uint)rdr.Position;
uint cb = cv * (uint)((ExifType)dt).GetElementSize();
if (cb > sizeof(uint))
dpos = rdr.Read<uint>(swap);
uint dend = dpos + cb;
if (cb == 0 || dend > (uint)span.Length)
return false;
curr = new ExifItem(id, (ExifType)dt, cv, (int)dpos..(int)dend);
return true;
}
public readonly ref readonly ExifItem Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#if BUILTIN_SPAN
get => ref MemoryMarshal.GetReference(MemoryMarshal.CreateSpan(ref Unsafe.AsRef(curr), 1));
#else
get => ref *(ExifItem*)Unsafe.AsPointer(ref Unsafe.AsRef(curr));
#endif
}
}