Browse Source

Add and utilize TryGetValue on ExifProfile

pull/506/head
Jason Nelson 8 years ago
parent
commit
9b1cd6dbb6
  1. 11
      src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs
  2. 11
      src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs
  3. 25
      src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs
  4. 3
      src/ImageSharp/MetaData/Profiles/Exif/ExifValue.cs

11
src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs

@ -427,10 +427,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort
{ {
if (this.isExif) if (this.isExif)
{ {
ExifValue horizontal = this.MetaData.ExifProfile.GetValue(ExifTag.XResolution); double horizontalValue = this.MetaData.ExifProfile.TryGetValue(ExifTag.XResolution, out var horizonalTag)
ExifValue vertical = this.MetaData.ExifProfile.GetValue(ExifTag.YResolution); ? ((Rational)horizonalTag.Value).ToDouble()
double horizontalValue = horizontal != null ? ((Rational)horizontal.Value).ToDouble() : 0; : 0;
double verticalValue = vertical != null ? ((Rational)vertical.Value).ToDouble() : 0;
double verticalValue = this.MetaData.ExifProfile.TryGetValue(ExifTag.YResolution, out var verticalTag)
? ((Rational)verticalTag.Value).ToDouble()
: 0;
if (horizontalValue > 0 && verticalValue > 0) if (horizontalValue > 0 && verticalValue > 0)
{ {

11
src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs

@ -380,10 +380,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort
{ {
if (this.isExif) if (this.isExif)
{ {
ExifValue horizontal = image.MetaData.ExifProfile.GetValue(ExifTag.XResolution); double horizontalValue = image.MetaData.ExifProfile.TryGetValue(ExifTag.XResolution, out var horizontalTag)
ExifValue vertical = image.MetaData.ExifProfile.GetValue(ExifTag.YResolution); ? ((Rational)horizontalTag.Value).ToDouble()
double horizontalValue = horizontal != null ? ((Rational)horizontal.Value).ToDouble() : 0; : 0;
double verticalValue = vertical != null ? ((Rational)vertical.Value).ToDouble() : 0;
double verticalValue = image.MetaData.ExifProfile.TryGetValue(ExifTag.YResolution, out var verticalTag)
? ((Rational)verticalTag.Value).ToDouble()
: 0;
if (horizontalValue > 0 && verticalValue > 0) if (horizontalValue > 0 && verticalValue > 0)
{ {

25
src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs

@ -160,6 +160,31 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
return null; return null;
} }
/// <summary>
/// Conditionally the value of the tag if it exists.
/// </summary>
/// <param name="tag">The tag of the EXIF value.</param>
/// <param name="value">The value of the tag, if found.</param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public bool TryGetValue(ExifTag tag, out ExifValue value)
{
foreach (ExifValue exifValue in this.Values)
{
if (exifValue.Tag == tag)
{
value = exifValue;
return true;
}
}
value = default;
return false;
}
/// <summary> /// <summary>
/// Removes the value with the specified tag. /// Removes the value with the specified tag.
/// </summary> /// </summary>

3
src/ImageSharp/MetaData/Profiles/Exif/ExifValue.cs

@ -691,8 +691,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
/// <returns>The <see cref="string"/></returns> /// <returns>The <see cref="string"/></returns>
private string ToString(object value) private string ToString(object value)
{ {
string description = ExifTagDescriptionAttribute.GetDescription(this.Tag, value); if (ExifTagDescriptionAttribute.GetDescription(this.Tag, value) is string description)
if (description != null)
{ {
return description; return description;
} }

Loading…
Cancel
Save