diff --git a/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs b/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs index 9050c20e1..eb70c4f8c 100644 --- a/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs @@ -2,6 +2,7 @@ // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // + namespace ImageSharp.Formats { using System; @@ -66,6 +67,11 @@ namespace ImageSharp.Formats /// private bool isJfif; + /// + /// Whether the image has a EXIF header + /// + private bool isExif; + /// /// The vertical resolution. Calculated if the image has a JFIF header. /// @@ -550,6 +556,19 @@ namespace ImageSharp.Formats image.MetaData.HorizontalResolution = this.horizontalResolution; image.MetaData.VerticalResolution = this.verticalResolution; } + else if (this.isExif) + { + ExifValue horizontal = image.MetaData.ExifProfile.GetValue(ExifTag.XResolution); + ExifValue vertical = image.MetaData.ExifProfile.GetValue(ExifTag.YResolution); + double horizontalValue = horizontal != null ? ((Rational)horizontal.Value).ToDouble() : 0; + double verticalValue = vertical != null ? ((Rational)vertical.Value).ToDouble() : 0; + + if (horizontalValue > 0 && verticalValue > 0) + { + image.MetaData.HorizontalResolution = horizontalValue; + image.MetaData.VerticalResolution = verticalValue; + } + } } /// @@ -951,6 +970,7 @@ namespace ImageSharp.Formats if (profile[0] == 'E' && profile[1] == 'x' && profile[2] == 'i' && profile[3] == 'f' && profile[4] == '\0' && profile[5] == '\0') { + this.isExif = true; image.MetaData.ExifProfile = new ExifProfile(profile); } } @@ -976,8 +996,8 @@ namespace ImageSharp.Formats if (this.isJfif) { - this.horizontalResolution = (short)(this.Temp[9] + (this.Temp[10] << 8)); - this.verticalResolution = (short)(this.Temp[11] + (this.Temp[12] << 8)); + this.horizontalResolution = (short)(this.Temp[9] + (this.Temp[8] << 8)); + this.verticalResolution = (short)(this.Temp[11] + (this.Temp[10] << 8)); } if (remaining > 0) diff --git a/src/ImageSharp/Image/Image{TColor}.cs b/src/ImageSharp/Image/Image{TColor}.cs index 7223bc1d8..c16bba344 100644 --- a/src/ImageSharp/Image/Image{TColor}.cs +++ b/src/ImageSharp/Image/Image{TColor}.cs @@ -127,13 +127,7 @@ namespace ImageSharp /// the default value is used. /// /// The width of the image in inches. - public double InchWidth - { - get - { - return this.Width / this.MetaData.HorizontalResolution; - } - } + public double InchWidth => this.Width / this.MetaData.HorizontalResolution; /// /// Gets the height of the image in inches. It is calculated as the height of the image @@ -141,13 +135,7 @@ namespace ImageSharp /// the default value is used. /// /// The height of the image in inches. - public double InchHeight - { - get - { - return this.Height / this.MetaData.VerticalResolution; - } - } + public double InchHeight => this.Height / this.MetaData.VerticalResolution; /// /// Gets a value indicating whether this image is animated. diff --git a/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj b/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj index 1bce8003e..2444db031 100644 --- a/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj +++ b/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj @@ -238,8 +238,8 @@ Tests\Formats\Jpg\YCbCrImageTests.cs - - Tests\Image\ImagePropertyTests.cs + + Tests\MetaData\ImagePropertyTests.cs Tests\Image\ImageTests.cs diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index 25329d93a..8bce4c4e8 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -100,5 +100,25 @@ namespace ImageSharp.Tests } } } + + [Fact] + public void Decoder_Reads_Correct_Resolution_From_Jfif() + { + using (Image image = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan).CreateImage()) + { + Assert.Equal(300, image.MetaData.HorizontalResolution); + Assert.Equal(300, image.MetaData.VerticalResolution); + } + } + + [Fact] + public void Decoder_Reads_Correct_Resolution_From_Exif() + { + using (Image image = TestFile.Create(TestImages.Jpeg.Baseline.Jpeg420).CreateImage()) + { + Assert.Equal(72, image.MetaData.HorizontalResolution); + Assert.Equal(72, image.MetaData.VerticalResolution); + } + } } } \ No newline at end of file