Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow zero DPI and only throw at EOF when not enough data #2324

Merged
merged 3 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions src/ImageSharp/Formats/Jpeg/Components/Decoder/JFifMarker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder;
/// <param name="yDensity">The vertical pixel density.</param>
private JFifMarker(byte majorVersion, byte minorVersion, byte densityUnits, short xDensity, short yDensity)
{
if (xDensity <= 0)
{
JpegThrowHelper.ThrowInvalidImageContentException($"X-Density {xDensity} must be greater than 0.");
}

if (yDensity <= 0)
{
JpegThrowHelper.ThrowInvalidImageContentException($"Y-Density {yDensity} must be greater than 0.");
}

this.MajorVersion = majorVersion;
this.MinorVersion = minorVersion;

Expand Down Expand Up @@ -64,12 +54,12 @@ private JFifMarker(byte majorVersion, byte minorVersion, byte densityUnits, shor
public PixelResolutionUnit DensityUnits { get; }

/// <summary>
/// Gets the horizontal pixel density. Must not be zero.
/// Gets the horizontal pixel density.
/// </summary>
public short XDensity { get; }

/// <summary>
/// Gets the vertical pixel density. Must not be zero.
/// Gets the vertical pixel density.
/// </summary>
public short YDensity { get; }

Expand All @@ -88,12 +78,8 @@ public static bool TryParse(byte[] bytes, out JFifMarker marker)
byte densityUnits = bytes[7];
short xDensity = (short)((bytes[8] << 8) | bytes[9]);
short yDensity = (short)((bytes[10] << 8) | bytes[11]);

if (xDensity > 0 && yDensity > 0)
{
marker = new JFifMarker(majorVersion, minorVersion, densityUnits, xDensity, yDensity);
return true;
}
marker = new JFifMarker(majorVersion, minorVersion, densityUnits, xDensity, yDensity);
return true;
}

marker = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,10 @@ public static Size CalculateResultingImageSize(Size size, Size? targetSize, out

return size;
}

/// <summary>
/// Gets a value indicating whether the converter has a pixel buffer.
/// </summary>
/// <returns><see langword="true"/> if the converter has a pixel buffer; otherwise, <see langword="false"/>.</returns>
public abstract bool HasPixelBuffer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public SpectralConverter(Configuration configuration, Size? targetSize = null)
/// </summary>
public Configuration Configuration { get; }

/// <summary>
/// Gets a value indicating whether the converter has a pixel buffer.
/// </summary>
/// <returns><see langword="true"/> if the converter has a pixel buffer; otherwise, <see langword="false"/>.</returns>
public override bool HasPixelBuffer() => this.pixelBuffer is not null;

/// <summary>
/// Gets converted pixel buffer.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,18 @@ internal void ParseStream(BufferedReadStream stream, SpectralConverter spectralC
// to uint to avoid sign extension.
if (stream.RemainingBytes < (uint)markerContentByteSize)
{
if (metadataOnly && this.Metadata != null && this.Frame != null)
{
// We have enough data to decode the image, so we can stop parsing.
return;
}

if (this.Metadata != null && this.Frame != null && spectralConverter.HasPixelBuffer())
Copy link
Contributor

@br3aker br3aker Jan 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we want to know if image was decoded at this point - we could use Converted of SpectralConverter as it's called right after baseline image is decoded.

Two problems:

  1. It's protected, not really a problem creating a public getter though
  2. It's not set for progressive images as those are lazy-converted during pixel buffer -> Image<T> step which won't be called until parsing loop is finished, not the wisest solution from me

I'd probably refactor spectral converter so it could better describe its state.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. This felt a little hacky. State matching would be better.

{
// We have enough data to decode the image, so we can stop parsing.
return;
}

JpegThrowHelper.ThrowNotEnoughBytesForMarker(fileMarker.Marker);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public override void ConvertStrideBaseline()
{
}

public override bool HasPixelBuffer() => throw new NotImplementedException();

public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData)
{
}
Expand Down
9 changes: 0 additions & 9 deletions tests/ImageSharp.Tests/Formats/Jpg/JFifMarkerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,6 @@ public void MarkerIgnoresIncorrectValue()
Assert.Equal(default, marker);
}

[Fact]
public void MarkerIgnoresCorrectHeaderButInvalidDensities()
{
bool isJFif = JFifMarker.TryParse(this.bytes3, out JFifMarker marker);

Assert.False(isJFif);
Assert.Equal(default, marker);
}

[Fact]
public void MarkerEqualityIsCorrect()
{
Expand Down
11 changes: 11 additions & 0 deletions tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,15 @@ public void Issue2136_DecodeWorks<TPixel>(TestImageProvider<TPixel> provider)
image.DebugSave(provider);
image.CompareToOriginal(provider);
}

// https://github.com/SixLabors/ImageSharp/issues/2315
[Theory]
[WithFile(TestImages.Jpeg.Issues.Issue2315_NotEnoughBytes, PixelTypes.Rgba32)]
public void Issue2315_DecodeWorks<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage(JpegDecoder.Instance);
image.DebugSave(provider);
image.CompareToOriginal(provider);
}
}
2 changes: 2 additions & 0 deletions tests/ImageSharp.Tests/Formats/Jpg/SpectralJpegTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ public override void ConvertStrideBaseline()
}
}

public override bool HasPixelBuffer() => throw new NotImplementedException();

public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData)
{
this.frame = frame;
Expand Down
1 change: 1 addition & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ public static class Issues
public const string ValidExifArgumentNullExceptionOnEncode = "Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg";
public const string Issue2133_DeduceColorSpace = "Jpg/issues/Issue2133.jpg";
public const string Issue2136_ScanMarkerExtraneousBytes = "Jpg/issues/Issue2136-scan-segment-extraneous-bytes.jpg";
public const string Issue2315_NotEnoughBytes = "Jpg/issues/issue-2315.jpg";

public static class Fuzz
{
Expand Down
3 changes: 3 additions & 0 deletions tests/Images/Input/Jpg/issues/issue-2315.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.