Browse Source

Correctly read resolution. Fix #96

af/merge-core
James Jackson-South 9 years ago
parent
commit
1dd168e90d
  1. 24
      src/ImageSharp.Formats.Jpeg/JpegDecoderCore.cs
  2. 16
      src/ImageSharp/Image/Image{TColor}.cs
  3. 4
      tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj
  4. 20
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

24
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.
// </copyright>
namespace ImageSharp.Formats
{
using System;
@ -66,6 +67,11 @@ namespace ImageSharp.Formats
/// </summary>
private bool isJfif;
/// <summary>
/// Whether the image has a EXIF header
/// </summary>
private bool isExif;
/// <summary>
/// The vertical resolution. Calculated if the image has a JFIF header.
/// </summary>
@ -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;
}
}
}
/// <summary>
@ -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)

16
src/ImageSharp/Image/Image{TColor}.cs

@ -127,13 +127,7 @@ namespace ImageSharp
/// the default value is used.
/// </summary>
/// <value>The width of the image in inches.</value>
public double InchWidth
{
get
{
return this.Width / this.MetaData.HorizontalResolution;
}
}
public double InchWidth => this.Width / this.MetaData.HorizontalResolution;
/// <summary>
/// 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.
/// </summary>
/// <value>The height of the image in inches.</value>
public double InchHeight
{
get
{
return this.Height / this.MetaData.VerticalResolution;
}
}
public double InchHeight => this.Height / this.MetaData.VerticalResolution;
/// <summary>
/// Gets a value indicating whether this image is animated.

4
tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj

@ -238,8 +238,8 @@
<Compile Include="..\ImageSharp.Tests\Formats\Jpg\YCbCrImageTests.cs">
<Link>Tests\Formats\Jpg\YCbCrImageTests.cs</Link>
</Compile>
<Compile Include="..\ImageSharp.Tests\Image\ImagePropertyTests.cs">
<Link>Tests\Image\ImagePropertyTests.cs</Link>
<Compile Include="..\ImageSharp.Tests\MetaData\ImagePropertyTests.cs">
<Link>Tests\MetaData\ImagePropertyTests.cs</Link>
</Compile>
<Compile Include="..\ImageSharp.Tests\Image\ImageTests.cs">
<Link>Tests\Image\ImageTests.cs</Link>

20
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);
}
}
}
}
Loading…
Cancel
Save