Browse Source

Merge branch 'master' into js/format-info

af/merge-core
James Jackson-South 8 years ago
committed by GitHub
parent
commit
45a075bb98
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 34
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
  2. 24
      src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs
  3. 6
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs
  4. 2
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
  5. 3
      tests/ImageSharp.Tests/TestImages.cs
  6. 2
      tests/Images/External
  7. 3
      tests/Images/Input/Jpg/issues/Issue694-Decode-Exif-OutOfRange.jpg
  8. 3
      tests/Images/Input/Jpg/issues/Issue695-Invalid-EOI.jpg
  9. 3
      tests/Images/Input/Jpg/issues/Issue696-Resize-Exif-OutOfRange.jpg

34
src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

@ -269,7 +269,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
this.fastACTables = new FastACTables(this.configuration.MemoryAllocator); this.fastACTables = new FastACTables(this.configuration.MemoryAllocator);
} }
while (fileMarker.Marker != JpegConstants.Markers.EOI) // Break only when we discover a valid EOI marker.
// https://github.com/SixLabors/ImageSharp/issues/695
while (fileMarker.Marker != JpegConstants.Markers.EOI
|| (fileMarker.Marker == JpegConstants.Markers.EOI && fileMarker.Invalid))
{ {
if (!fileMarker.Invalid) if (!fileMarker.Invalid)
{ {
@ -464,13 +467,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
} }
else if (this.isExif) else if (this.isExif)
{ {
double horizontalValue = this.MetaData.ExifProfile.TryGetValue(ExifTag.XResolution, out ExifValue horizontalTag) double horizontalValue = this.GetExifResolutionValue(ExifTag.XResolution);
? ((Rational)horizontalTag.Value).ToDouble() double verticalValue = this.GetExifResolutionValue(ExifTag.YResolution);
: 0;
double verticalValue = this.MetaData.ExifProfile.TryGetValue(ExifTag.YResolution, out ExifValue verticalTag)
? ((Rational)verticalTag.Value).ToDouble()
: 0;
if (horizontalValue > 0 && verticalValue > 0) if (horizontalValue > 0 && verticalValue > 0)
{ {
@ -481,6 +479,26 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
} }
} }
private double GetExifResolutionValue(ExifTag tag)
{
if (!this.MetaData.ExifProfile.TryGetValue(tag, out ExifValue exifValue))
{
return 0;
}
switch (exifValue.DataType)
{
case ExifDataType.Rational:
return ((Rational)exifValue.Value).ToDouble();
case ExifDataType.Long:
return (uint)exifValue.Value;
case ExifDataType.DoubleFloat:
return (double)exifValue.Value;
default:
return 0;
}
}
/// <summary> /// <summary>
/// Extends the profile with additional data. /// Extends the profile with additional data.
/// </summary> /// </summary>

24
src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs

@ -88,19 +88,19 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
} }
uint ifdOffset = this.ReadUInt32(); uint ifdOffset = this.ReadUInt32();
this.AddValues(values, (int)ifdOffset); this.AddValues(values, ifdOffset);
uint thumbnailOffset = this.ReadUInt32(); uint thumbnailOffset = this.ReadUInt32();
this.GetThumbnail((int)thumbnailOffset); this.GetThumbnail(thumbnailOffset);
if (this.exifOffset != 0) if (this.exifOffset != 0)
{ {
this.AddValues(values, (int)this.exifOffset); this.AddValues(values, this.exifOffset);
} }
if (this.gpsOffset != 0) if (this.gpsOffset != 0)
{ {
this.AddValues(values, (int)this.gpsOffset); this.AddValues(values, this.gpsOffset);
} }
return values; return values;
@ -153,9 +153,14 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
/// </summary> /// </summary>
/// <param name="values">The values.</param> /// <param name="values">The values.</param>
/// <param name="index">The index.</param> /// <param name="index">The index.</param>
private void AddValues(List<ExifValue> values, int index) private void AddValues(List<ExifValue> values, uint index)
{ {
this.position = index; if (index > (uint)this.exifData.Length)
{
return;
}
this.position = (int)index;
int count = this.ReadUInt16(); int count = this.ReadUInt16();
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
@ -431,7 +436,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
return null; return null;
} }
private void GetThumbnail(int offset) private void GetThumbnail(uint offset)
{ {
var values = new List<ExifValue>(); var values = new List<ExifValue>();
this.AddValues(values, offset); this.AddValues(values, offset);
@ -515,10 +520,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
return new Rational(numerator, denominator, false); return new Rational(numerator, denominator, false);
} }
private sbyte ConvertToSignedByte(ReadOnlySpan<byte> buffer) private sbyte ConvertToSignedByte(ReadOnlySpan<byte> buffer) => unchecked((sbyte)buffer[0]);
{
return unchecked((sbyte)buffer[0]);
}
private int ConvertToInt32(ReadOnlySpan<byte> buffer) // SignedLong in Exif Specification private int ConvertToInt32(ReadOnlySpan<byte> buffer) // SignedLong in Exif Specification
{ {

6
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs

@ -21,7 +21,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
TestImages.Jpeg.Baseline.Bad.BadEOF, TestImages.Jpeg.Baseline.Bad.BadEOF,
TestImages.Jpeg.Issues.MultiHuffmanBaseline394, TestImages.Jpeg.Issues.MultiHuffmanBaseline394,
TestImages.Jpeg.Baseline.MultiScanBaselineCMYK, TestImages.Jpeg.Baseline.MultiScanBaselineCMYK,
TestImages.Jpeg.Baseline.Bad.BadRST TestImages.Jpeg.Baseline.Bad.BadRST,
TestImages.Jpeg.Issues.MultiHuffmanBaseline394,
TestImages.Jpeg.Issues.ExifDecodeOutOfRange694,
TestImages.Jpeg.Issues.InvalidEOI695,
TestImages.Jpeg.Issues.ExifResizeOutOfRange696
}; };
public static string[] ProgressiveTestJpegs = public static string[] ProgressiveTestJpegs =

2
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

@ -48,6 +48,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
TestImages.Jpeg.Issues.BadZigZagProgressive385, TestImages.Jpeg.Issues.BadZigZagProgressive385,
TestImages.Jpeg.Issues.NoEoiProgressive517, TestImages.Jpeg.Issues.NoEoiProgressive517,
TestImages.Jpeg.Issues.BadRstProgressive518, TestImages.Jpeg.Issues.BadRstProgressive518,
TestImages.Jpeg.Issues.InvalidEOI695,
TestImages.Jpeg.Issues.ExifResizeOutOfRange696
}; };
return !TestEnvironment.Is64BitProcess && largeImagesToSkipOn32Bit.Contains(provider.SourceFileOrDescription); return !TestEnvironment.Is64BitProcess && largeImagesToSkipOn32Bit.Contains(provider.SourceFileOrDescription);

3
tests/ImageSharp.Tests/TestImages.cs

@ -152,6 +152,9 @@ namespace SixLabors.ImageSharp.Tests
public const string BadRstProgressive518 = "Jpg/issues/Issue518-Bad-RST-Progressive.jpg"; public const string BadRstProgressive518 = "Jpg/issues/Issue518-Bad-RST-Progressive.jpg";
public const string InvalidCast520 = "Jpg/issues/Issue520-InvalidCast.jpg"; public const string InvalidCast520 = "Jpg/issues/Issue520-InvalidCast.jpg";
public const string DhtHasWrongLength624 = "Jpg/issues/Issue624-DhtHasWrongLength-Progressive-N.jpg"; public const string DhtHasWrongLength624 = "Jpg/issues/Issue624-DhtHasWrongLength-Progressive-N.jpg";
public const string ExifDecodeOutOfRange694 = "Jpg/issues/Issue694-Decode-Exif-OutOfRange.jpg";
public const string InvalidEOI695 = "Jpg/issues/Issue695-Invalid-EOI.jpg";
public const string ExifResizeOutOfRange696 = "Jpg/issues/Issue696-Resize-Exif-OutOfRange.jpg";
} }
public static readonly string[] All = Baseline.All.Concat(Progressive.All).ToArray(); public static readonly string[] All = Baseline.All.Concat(Progressive.All).ToArray();

2
tests/Images/External

@ -1 +1 @@
Subproject commit fcf311bf15bea061e552e4cc357cafe2d4f4bd70 Subproject commit 6abc3bc0ac253a24c9e88e68d7b7d853350a85da

3
tests/Images/Input/Jpg/issues/Issue694-Decode-Exif-OutOfRange.jpg

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:1b94a283fbe8927ab59745dd67d0b33e90a253e674e5fe4f0ad7594ff868cca2
size 226421

3
tests/Images/Input/Jpg/issues/Issue695-Invalid-EOI.jpg

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:e748a684c318b7424dd77b008fe92e179201d0a55106021b453a3fd2a22e9ab6
size 4805575

3
tests/Images/Input/Jpg/issues/Issue696-Resize-Exif-OutOfRange.jpg

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:bb22005e1db0f6da8f49ca57979f8b9aa5db7111b717a53e817e24c04283ab43
size 3196058
Loading…
Cancel
Save