Browse Source

Decoupled mcu size from the decoder, fixed SOF component bytes length check

pull/1702/head
Dmitry Pentin 5 years ago
parent
commit
7077473d71
  1. 5
      src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs
  2. 14
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
  3. 6
      tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs

5
src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs

@ -89,6 +89,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
/// </summary> /// </summary>
public int McusPerColumn { get; set; } public int McusPerColumn { get; set; }
/// <summary>
/// Gets the mcu size of the image.
/// </summary>
public Size McuSize => new Size(this.McusPerLine, this.McusPerColumn);
/// <summary> /// <summary>
/// Gets the color depth, in number of bits per pixel. /// Gets the color depth, in number of bits per pixel.
/// </summary> /// </summary>

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

@ -112,11 +112,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
/// <inheritdoc/> /// <inheritdoc/>
Size IImageDecoderInternals.Dimensions => this.Frame.PixelSize; Size IImageDecoderInternals.Dimensions => this.Frame.PixelSize;
/// <summary>
/// Gets the number of MCU blocks in the image as <see cref="Size"/>.
/// </summary>
public Size ImageSizeInMCU { get; private set; }
/// <summary> /// <summary>
/// Gets a value indicating whether the metadata should be ignored when the image is being decoded. /// Gets a value indicating whether the metadata should be ignored when the image is being decoded.
/// </summary> /// </summary>
@ -834,6 +829,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
byte componentCount = this.temp[5]; byte componentCount = this.temp[5];
this.ColorSpace = this.DeduceJpegColorSpace(componentCount); this.ColorSpace = this.DeduceJpegColorSpace(componentCount);
this.Metadata.GetJpegMetadata().ColorType = this.ColorSpace == JpegColorSpace.Grayscale ? JpegColorType.Luminance : JpegColorType.YCbCr;
this.Frame = new JpegFrame this.Frame = new JpegFrame
{ {
Extended = frameMarker.Marker == JpegConstants.Markers.SOF1, Extended = frameMarker.Marker == JpegConstants.Markers.SOF1,
@ -844,14 +841,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
ComponentCount = componentCount ComponentCount = componentCount
}; };
this.Metadata.GetJpegMetadata().ColorType = this.ColorSpace == JpegColorSpace.Grayscale ? JpegColorType.Luminance : JpegColorType.YCbCr;
if (!metadataOnly) if (!metadataOnly)
{ {
remaining -= length; remaining -= length;
const int componentBytes = 3; const int componentBytes = 3;
if (remaining > componentCount * componentBytes) if (remaining != componentCount * componentBytes)
{ {
JpegThrowHelper.ThrowBadMarker("SOFn", remaining); JpegThrowHelper.ThrowBadMarker("SOFn", remaining);
} }
@ -894,9 +889,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
this.Frame.MaxVerticalFactor = maxV; this.Frame.MaxVerticalFactor = maxV;
this.Frame.InitComponents(); this.Frame.InitComponents();
this.ImageSizeInMCU = new Size(this.Frame.McusPerLine, this.Frame.McusPerColumn);
// This can be injected in SOF marker callback
this.scanDecoder.InjectFrameData(this.Frame, this); this.scanDecoder.InjectFrameData(this.Frame, this);
} }
} }

6
tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs

@ -48,7 +48,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
Size expectedSizeInBlocks = decoder.ImageSizeInPixels.DivideRoundUp(8); Size expectedSizeInBlocks = decoder.ImageSizeInPixels.DivideRoundUp(8);
Assert.Equal(expectedSizeInBlocks, decoder.ImageSizeInMCU); Assert.Equal(expectedSizeInBlocks, decoder.Frame.McuSize);
var uniform1 = new Size(1, 1); var uniform1 = new Size(1, 1);
JpegComponent c0 = decoder.Components[0]; JpegComponent c0 = decoder.Components[0];
@ -70,7 +70,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
using (JpegDecoderCore decoder = JpegFixture.ParseJpegStream(imageFile)) using (JpegDecoderCore decoder = JpegFixture.ParseJpegStream(imageFile))
{ {
sb.AppendLine(imageFile); sb.AppendLine(imageFile);
sb.AppendLine($"Size:{decoder.ImageSizeInPixels} MCU:{decoder.ImageSizeInMCU}"); sb.AppendLine($"Size:{decoder.ImageSizeInPixels} MCU:{decoder.Frame.McuSize}");
JpegComponent c0 = decoder.Components[0]; JpegComponent c0 = decoder.Components[0];
JpegComponent c1 = decoder.Components[1]; JpegComponent c1 = decoder.Components[1];
@ -115,7 +115,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
var uniform1 = new Size(1, 1); var uniform1 = new Size(1, 1);
Size expectedLumaSizeInBlocks = decoder.ImageSizeInMCU.MultiplyBy(fLuma); Size expectedLumaSizeInBlocks = decoder.Frame.McuSize.MultiplyBy(fLuma);
Size divisor = fLuma.DivideBy(fChroma); Size divisor = fLuma.DivideBy(fChroma);

Loading…
Cancel
Save