Skip to content

Commit e2f2342

Browse files
authored
Merge pull request #449 from drewnoakes/bits
Performance and code style
2 parents b5a4d3a + 6f4aedb commit e2f2342

16 files changed

+75
-65
lines changed

MetadataExtractor/Formats/Avi/AviRiffHandler.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,20 @@ public void ProcessChunk(string fourCc, byte[] payload)
7474

7575
if (fccType == "vids")
7676
{
77-
directory.Set(AviDirectory.TagFramesPerSecond, (dwRate / dwScale));
77+
directory.Set(AviDirectory.TagFramesPerSecond, dwRate / dwScale);
7878

7979
double duration = dwLength / (dwRate / dwScale);
80-
int hours = (int)duration / (int)(Math.Pow(60, 2));
81-
int minutes = ((int)duration / (int)(Math.Pow(60, 1))) - (hours * 60);
82-
int seconds = (int)Math.Round((duration / (Math.Pow(60, 0))) - (minutes * 60));
80+
int hours = (int)duration / (int)Math.Pow(60, 2);
81+
int minutes = ((int)duration / (int)Math.Pow(60, 1)) - (hours * 60);
82+
int seconds = (int)Math.Round((duration / Math.Pow(60, 0)) - (minutes * 60));
8383
string time = new DateTime(new TimeSpan(hours, minutes, seconds).Ticks).ToString("HH:mm:ss");
8484

8585
directory.Set(AviDirectory.TagDuration, time);
8686
directory.Set(AviDirectory.TagVideoCodec, fccHandler);
8787
}
8888
else if (fccType == "auds")
8989
{
90-
directory.Set(AviDirectory.TagSamplesPerSecond, (dwRate / dwScale));
90+
directory.Set(AviDirectory.TagSamplesPerSecond, dwRate / dwScale);
9191
}
9292
}
9393
catch (IOException e)

MetadataExtractor/Formats/Bmp/BmpHeaderDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public sealed class BmpHeaderDescriptor(BmpHeaderDirectory directory)
2929
case BmpHeaderDirectory.TagGammaRed:
3030
case BmpHeaderDirectory.TagGammaGreen:
3131
case BmpHeaderDirectory.TagGammaBlue:
32-
return $"{((double)Directory.GetInt64(tagType) / 0x10000):0.###}";
32+
return $"{(double)Directory.GetInt64(tagType) / 0x10000:0.###}";
3333
//return FormatFixed1616(Directory.GetInt64(tagType));
3434
case BmpHeaderDirectory.TagIntent:
3535
return GetRenderingIntentDescription();

MetadataExtractor/Formats/Exif/ExifDescriptorBase.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,8 @@ public abstract class ExifDescriptorBase<T>(T directory)
537537
/// </remarks>
538538
public string? GetCfaPatternDescription()
539539
{
540-
return FormatCfaPattern(DecodeCfaPattern(TagCfaPattern));
540+
int[]? pattern = DecodeCfaPattern(TagCfaPattern);
541+
return pattern is null ? null : FormatCfaPattern(pattern);
541542
}
542543

543544
/// <summary>
@@ -573,10 +574,8 @@ public abstract class ExifDescriptorBase<T>(T directory)
573574
return $"Unknown Pattern ({base.GetDescription(TagCfaPattern2)})";
574575
}
575576

576-
private static string? FormatCfaPattern(int[]? pattern)
577+
private static string FormatCfaPattern(ReadOnlySpan<int> pattern)
577578
{
578-
if (pattern is null)
579-
return null;
580579
if (pattern.Length < 2)
581580
return "<truncated data>";
582581
if (pattern[0] == 0 && pattern[1] == 0)
@@ -586,16 +585,11 @@ public abstract class ExifDescriptorBase<T>(T directory)
586585
if (end > pattern.Length)
587586
return "<invalid pattern size>";
588587

589-
string[] cfaColors = ["Red", "Green", "Blue", "Cyan", "Magenta", "Yellow", "White"];
590-
591588
var ret = new StringBuilder();
592589
ret.Append('[');
593590
for (var pos = 2; pos < end; pos++)
594591
{
595-
if (pattern[pos] <= cfaColors.Length - 1)
596-
ret.Append(cfaColors[pattern[pos]]);
597-
else
598-
ret.Append("Unknown"); // indicated pattern position is outside the array bounds
592+
ret.Append(ToColor(pattern[pos]));
599593

600594
if ((pos - 2) % pattern[1] == 0)
601595
ret.Append(',');
@@ -605,6 +599,21 @@ public abstract class ExifDescriptorBase<T>(T directory)
605599
ret.Append(']');
606600

607601
return ret.ToString();
602+
603+
static string ToColor(int i)
604+
{
605+
return i switch
606+
{
607+
0 => "Red",
608+
1 => "Green",
609+
2 => "Blue",
610+
3 => "Cyan",
611+
4 => "Magenta",
612+
5 => "Yellow",
613+
6 => "White",
614+
_ => "Unknown"
615+
};
616+
}
608617
}
609618

610619
/// <summary>

MetadataExtractor/Formats/Exif/GpsDescriptor.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ public sealed class GpsDescriptor(GpsDirectory directory)
130130
if (value is null)
131131
return null;
132132

133-
return (value.Trim().ToUpper()) switch
133+
return value.Trim() switch
134134
{
135-
"K" => "kilometers",
136-
"M" => "miles",
137-
"N" => "knots",
135+
"k" or "K" => "kilometers",
136+
"m" or "M" => "miles",
137+
"n" or "N" => "knots",
138138

139-
_ => "Unknown (" + value.Trim() + ")",
139+
string other => "Unknown (" + other + ")",
140140
};
141141
}
142142

@@ -163,12 +163,12 @@ public sealed class GpsDescriptor(GpsDirectory directory)
163163
if (value is null)
164164
return null;
165165

166-
return (value.Trim().ToUpper()) switch
166+
return value.Trim() switch
167167
{
168-
"T" => "True direction",
169-
"M" => "Magnetic direction",
168+
"t" or "T" => "True direction",
169+
"m" or "M" => "Magnetic direction",
170170

171-
_ => "Unknown (" + value.Trim() + ")",
171+
string other => "Unknown (" + other + ")",
172172
};
173173
}
174174

@@ -185,13 +185,13 @@ public sealed class GpsDescriptor(GpsDirectory directory)
185185
if (value is null)
186186
return null;
187187

188-
return (value.Trim().ToUpper()) switch
188+
return value.Trim() switch
189189
{
190-
"K" => "km/h",
191-
"M" => "mph",
192-
"N" => "knots",
190+
"k" or "K" => "km/h",
191+
"m" or "M" => "mph",
192+
"n" or "N" => "knots",
193193

194-
_ => "Unknown (" + value.Trim() + ")",
194+
string other => "Unknown (" + other + ")",
195195
};
196196
}
197197

@@ -210,27 +210,26 @@ public sealed class GpsDescriptor(GpsDirectory directory)
210210
if (value is null)
211211
return null;
212212

213-
return (value.Trim()) switch
213+
return value.Trim() switch
214214
{
215215
"2" => "2-dimensional measurement",
216216
"3" => "3-dimensional measurement",
217217
_ => "Unknown (" + value.Trim() + ")",
218218
};
219219
}
220220

221-
222221
public string? GetGpsStatusDescription()
223222
{
224223
var value = Directory.GetString(GpsDirectory.TagStatus);
225224
if (value is null)
226225
return null;
227226

228-
return (value.Trim().ToUpper()) switch
227+
return value.Trim() switch
229228
{
230-
"A" => "Active (Measurement in progress)",
231-
"V" => "Void (Measurement Interoperability)",
229+
"a" or "A" => "Active (Measurement in progress)",
230+
"v" or "V" => "Void (Measurement Interoperability)",
232231

233-
_ => "Unknown (" + value.Trim() + ")",
232+
string other => "Unknown (" + other + ")",
234233
};
235234
}
236235

MetadataExtractor/Formats/Exif/makernotes/NikonType2MakernoteDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public sealed class NikonType2MakernoteDescriptor(NikonType2MakernoteDirectory d
137137
if (values.Length != 4 || values[0] != 0 || values[2] != 0 || values[3] != 0)
138138
return "Unknown (" + Directory.GetString(NikonType2MakernoteDirectory.TagAfFocusPosition) + ")";
139139

140-
return (values[1]) switch
140+
return values[1] switch
141141
{
142142
0 => "Centre",
143143
1 => "Top",

MetadataExtractor/Formats/Exif/makernotes/OlympusCameraSettingsMakernoteDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ public sealed class OlympusCameraSettingsMakernoteDescriptor(OlympusCameraSettin
914914

915915
if (values.Length == 0 || values[0] == 0)
916916
return "Off";
917-
var a = (values[0]) switch
917+
var a = values[0] switch
918918
{
919919
1 => "Left to Right",
920920
2 => "Right to Left",

MetadataExtractor/Formats/Exif/makernotes/PanasonicMakernoteDescriptor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ public sealed class PanasonicMakernoteDescriptor(PanasonicMakernoteDirectory dir
425425
{
426426
if (!Directory.TryGetInt32(PanasonicMakernoteDirectory.TagUptime, out int value))
427427
return null;
428-
return $"{(value / 100f):0.0##}" + " s";
428+
return $"{value / 100f:0.0##}" + " s";
429429
}
430430

431431
public string? GetBurstModeDescription()
@@ -561,7 +561,7 @@ public sealed class PanasonicMakernoteDescriptor(PanasonicMakernoteDirectory dir
561561
{
562562
case 0:
563563
{
564-
return (value[1]) switch
564+
return value[1] switch
565565
{
566566
1 => "Spot Mode On",
567567
16 => "Spot Mode Off",
@@ -570,7 +570,7 @@ public sealed class PanasonicMakernoteDescriptor(PanasonicMakernoteDirectory dir
570570
}
571571
case 1:
572572
{
573-
return (value[1]) switch
573+
return value[1] switch
574574
{
575575
0 => "Spot Focusing",
576576
1 => "5-area",
@@ -579,7 +579,7 @@ public sealed class PanasonicMakernoteDescriptor(PanasonicMakernoteDirectory dir
579579
}
580580
case 16:
581581
{
582-
return (value[1]) switch
582+
return value[1] switch
583583
{
584584
0 => "1-area",
585585
16 => "1-area (high speed)",
@@ -588,7 +588,7 @@ public sealed class PanasonicMakernoteDescriptor(PanasonicMakernoteDirectory dir
588588
}
589589
case 32:
590590
{
591-
return (value[1]) switch
591+
return value[1] switch
592592
{
593593
0 => "Auto or Face Detect",
594594
1 => "3-area (left)",

MetadataExtractor/Formats/Exif/makernotes/SigmaMakernoteDescriptor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class SigmaMakernoteDescriptor(SigmaMakernoteDirectory directory)
2525
if (string.IsNullOrEmpty(value))
2626
return null;
2727

28-
return (value![0]) switch
28+
return value![0] switch
2929
{
3030
'8' => "Multi Segment",
3131
'A' => "Average",
@@ -40,7 +40,7 @@ public class SigmaMakernoteDescriptor(SigmaMakernoteDirectory directory)
4040
if (string.IsNullOrEmpty(value))
4141
return null;
4242

43-
return (value![0]) switch
43+
return value![0] switch
4444
{
4545
'A' => "Aperture Priority AE",
4646
'M' => "Manual",

MetadataExtractor/Formats/Flir/FlirCameraInfoDescriptor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ string KelvinToCelcius()
3535
string RelativeHumidity()
3636
{
3737
float f = Directory.GetSingle(tagType);
38-
float val = (f > 2 ? f / 100 : f);
39-
return $"{(val * 100):N1} %";
38+
float val = f > 2 ? f / 100 : f;
39+
return $"{val * 100:N1} %";
4040
}
4141
}
4242
}

MetadataExtractor/Formats/Jpeg/JpegDhtReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void Extract(SequentialReader reader, HuffmanTablesDirectory directory)
4343
int vCount = 0;
4444
foreach (byte b in lBytes)
4545
{
46-
vCount += (b & 0xFF);
46+
vCount += b & 0xFF;
4747
}
4848
byte[] vBytes = GetBytes(reader, vCount);
4949
directory.AddTable(new HuffmanTable(tableClass, tableDestinationId, lBytes, vBytes));

MetadataExtractor/Formats/Tiff/TiffReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public static void ProcessIfd(ITiffHandler handler, TiffReaderContext context, i
166166
//
167167
// Per tag: 12 bytes / BigTIFF 20 bytes (see above docs for breakdown).
168168
// Finally, a pointer to a "follower" IFD (optional)
169-
var tagTableLength = (tagCount * entryLength);
169+
var tagTableLength = tagCount * entryLength;
170170

171171
if (tagTableOffset + tagTableLength > checked((int)context.Reader.Length))
172172
{

MetadataExtractor/GeoLocation.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,23 @@
44

55
namespace MetadataExtractor
66
{
7-
/// <summary>Represents a latitude and longitude pair, giving a position on earth in spherical coordinates.</summary>
7+
/// <summary>
8+
/// Represents a latitude and longitude pair, giving a position on earth in spherical coordinates.
9+
/// </summary>
810
/// <remarks>
911
/// Values of latitude and longitude are given in degrees.
1012
/// <para />
1113
/// This type is immutable.
1214
/// </remarks>
13-
public readonly struct GeoLocation : IEquatable<GeoLocation>
15+
/// <param name="latitude">the latitude, in degrees</param>
16+
/// <param name="longitude">the longitude, in degrees</param>
17+
public readonly struct GeoLocation(double latitude, double longitude) : IEquatable<GeoLocation>
1418
{
15-
/// <summary>
16-
/// Initialises an instance of <see cref="GeoLocation"/>.
17-
/// </summary>
18-
/// <param name="latitude">the latitude, in degrees</param>
19-
/// <param name="longitude">the longitude, in degrees</param>
20-
public GeoLocation(double latitude, double longitude)
21-
{
22-
Latitude = latitude;
23-
Longitude = longitude;
24-
}
25-
2619
/// <value>the latitudinal angle of this location, in degrees.</value>
27-
public double Latitude { get; }
20+
public double Latitude { get; } = latitude;
2821

2922
/// <value>the longitudinal angle of this location, in degrees.</value>
30-
public double Longitude { get; }
23+
public double Longitude { get; } = longitude;
3124

3225
/// <value>true, if both latitude and longitude are equal to zero</value>
3326
public bool IsZero => Latitude == 0 && Longitude == 0;
@@ -98,6 +91,9 @@ public override int GetHashCode()
9891
#endif
9992
}
10093

94+
public static bool operator ==(GeoLocation left, GeoLocation right) => left.Equals(right);
95+
public static bool operator !=(GeoLocation left, GeoLocation right) => !(left == right);
96+
10197
#endregion
10298

10399
#region ToString

MetadataExtractor/IO/SequentialStreamReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public override void Skip(long n)
8585
if (n < 0)
8686
throw new ArgumentException("n must be zero or greater.");
8787

88-
if ((ulong)(_stream.Position + n) > (ulong)(_stream.Length))
88+
if ((ulong)(_stream.Position + n) > (ulong)_stream.Length)
8989
throw new IOException($"Unable to skip. Requested {n} bytes but only {_stream.Length - _stream.Position} remained.");
9090

9191
_stream.Seek(n, SeekOrigin.Current);

MetadataExtractor/PublicAPI/net8.0/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ static MetadataExtractor.Formats.Photoshop.DuckyReader.JpegSegmentPreamble.get -
155155
static MetadataExtractor.Formats.Photoshop.PhotoshopReader.JpegSegmentPreamble.get -> System.ReadOnlySpan<byte>
156156
static MetadataExtractor.Formats.Png.PngChunkType.operator !=(MetadataExtractor.Formats.Png.PngChunkType left, MetadataExtractor.Formats.Png.PngChunkType right) -> bool
157157
static MetadataExtractor.Formats.Png.PngChunkType.operator ==(MetadataExtractor.Formats.Png.PngChunkType left, MetadataExtractor.Formats.Png.PngChunkType right) -> bool
158+
static MetadataExtractor.GeoLocation.operator !=(MetadataExtractor.GeoLocation left, MetadataExtractor.GeoLocation right) -> bool
159+
static MetadataExtractor.GeoLocation.operator ==(MetadataExtractor.GeoLocation left, MetadataExtractor.GeoLocation right) -> bool
158160
static readonly MetadataExtractor.Formats.Png.PngChunkType.bKGD -> MetadataExtractor.Formats.Png.PngChunkType
159161
static readonly MetadataExtractor.Formats.Png.PngChunkType.cHRM -> MetadataExtractor.Formats.Png.PngChunkType
160162
static readonly MetadataExtractor.Formats.Png.PngChunkType.eXIf -> MetadataExtractor.Formats.Png.PngChunkType

MetadataExtractor/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ static MetadataExtractor.Formats.Apple.BplistReader.Parse(System.ReadOnlySpan<by
157157
static MetadataExtractor.Formats.Exif.Makernotes.AppleRunTimeMakernoteDirectory.Parse(byte[]! bytes) -> MetadataExtractor.Formats.Exif.Makernotes.AppleRunTimeMakernoteDirectory!
158158
static MetadataExtractor.Formats.Png.PngChunkType.operator !=(MetadataExtractor.Formats.Png.PngChunkType left, MetadataExtractor.Formats.Png.PngChunkType right) -> bool
159159
static MetadataExtractor.Formats.Png.PngChunkType.operator ==(MetadataExtractor.Formats.Png.PngChunkType left, MetadataExtractor.Formats.Png.PngChunkType right) -> bool
160+
static MetadataExtractor.GeoLocation.operator !=(MetadataExtractor.GeoLocation left, MetadataExtractor.GeoLocation right) -> bool
161+
static MetadataExtractor.GeoLocation.operator ==(MetadataExtractor.GeoLocation left, MetadataExtractor.GeoLocation right) -> bool
160162
static readonly MetadataExtractor.Formats.Png.PngChunkType.bKGD -> MetadataExtractor.Formats.Png.PngChunkType
161163
static readonly MetadataExtractor.Formats.Png.PngChunkType.cHRM -> MetadataExtractor.Formats.Png.PngChunkType
162164
static readonly MetadataExtractor.Formats.Png.PngChunkType.eXIf -> MetadataExtractor.Formats.Png.PngChunkType

MetadataExtractor/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ static MetadataExtractor.Formats.Photoshop.DuckyReader.JpegSegmentPreamble.get -
155155
static MetadataExtractor.Formats.Photoshop.PhotoshopReader.JpegSegmentPreamble.get -> System.ReadOnlySpan<byte>
156156
static MetadataExtractor.Formats.Png.PngChunkType.operator !=(MetadataExtractor.Formats.Png.PngChunkType left, MetadataExtractor.Formats.Png.PngChunkType right) -> bool
157157
static MetadataExtractor.Formats.Png.PngChunkType.operator ==(MetadataExtractor.Formats.Png.PngChunkType left, MetadataExtractor.Formats.Png.PngChunkType right) -> bool
158+
static MetadataExtractor.GeoLocation.operator !=(MetadataExtractor.GeoLocation left, MetadataExtractor.GeoLocation right) -> bool
159+
static MetadataExtractor.GeoLocation.operator ==(MetadataExtractor.GeoLocation left, MetadataExtractor.GeoLocation right) -> bool
158160
static readonly MetadataExtractor.Formats.Png.PngChunkType.bKGD -> MetadataExtractor.Formats.Png.PngChunkType
159161
static readonly MetadataExtractor.Formats.Png.PngChunkType.cHRM -> MetadataExtractor.Formats.Png.PngChunkType
160162
static readonly MetadataExtractor.Formats.Png.PngChunkType.eXIf -> MetadataExtractor.Formats.Png.PngChunkType

0 commit comments

Comments
 (0)