Browse Source

Comments, docs, decoupling, removed redundant properties

pull/1702/head
Dmitry Pentin 5 years ago
parent
commit
4d599d14f6
  1. 11
      src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs
  2. 20
      src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs
  3. 9
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
  4. 4
      tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs

11
src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs

@ -106,13 +106,18 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
this.SpectralBlocks = null;
}
public void Init()
/// <summary>
/// Initializes component for future buffers initialization.
/// </summary>
/// <param name="maxSubFactorH">Maximal horizontal subsampling factor among all the components.</param>
/// <param name="maxSubFactorV">Maximal vertical subsampling factor among all the components.</param>
public void Init(int maxSubFactorH, int maxSubFactorV)
{
this.WidthInBlocks = (int)MathF.Ceiling(
MathF.Ceiling(this.Frame.PixelWidth / 8F) * this.HorizontalSamplingFactor / this.Frame.MaxHorizontalFactor);
MathF.Ceiling(this.Frame.PixelWidth / 8F) * this.HorizontalSamplingFactor / maxSubFactorH);
this.HeightInBlocks = (int)MathF.Ceiling(
MathF.Ceiling(this.Frame.PixelHeight / 8F) * this.VerticalSamplingFactor / this.Frame.MaxVerticalFactor);
MathF.Ceiling(this.Frame.PixelHeight / 8F) * this.VerticalSamplingFactor / maxSubFactorV);
int blocksPerLineForMcu = this.Frame.McusPerLine * this.HorizontalSamplingFactor;
int blocksPerColumnForMcu = this.Frame.McusPerColumn * this.VerticalSamplingFactor;

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

@ -69,16 +69,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
/// </summary>
public JpegComponent[] Components { get; set; }
/// <summary>
/// Gets or sets the maximum horizontal sampling factor.
/// </summary>
public int MaxHorizontalFactor { get; set; }
/// <summary>
/// Gets or sets the maximum vertical sampling factor.
/// </summary>
public int MaxVerticalFactor { get; set; }
/// <summary>
/// Gets or sets the number of MCU's per line.
/// </summary>
@ -116,15 +106,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
/// <summary>
/// Allocates the frame component blocks.
/// </summary>
public void InitComponents()
/// <param name="maxSubFactorH">Maximal horizontal subsampling factor among all the components.</param>
/// <param name="maxSubFactorV">Maximal vertical subsampling factor among all the components.</param>
public void Init(int maxSubFactorH, int maxSubFactorV)
{
this.McusPerLine = (int)Numerics.DivideCeil((uint)this.PixelWidth, (uint)this.MaxHorizontalFactor * 8);
this.McusPerColumn = (int)Numerics.DivideCeil((uint)this.PixelHeight, (uint)this.MaxVerticalFactor * 8);
this.McusPerLine = (int)Numerics.DivideCeil((uint)this.PixelWidth, (uint)maxSubFactorH * 8);
this.McusPerColumn = (int)Numerics.DivideCeil((uint)this.PixelHeight, (uint)maxSubFactorV * 8);
for (int i = 0; i < this.ComponentCount; i++)
{
JpegComponent component = this.Components[i];
component.Init();
component.Init(maxSubFactorH, maxSubFactorV);
}
}

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

@ -106,9 +106,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
/// </summary>
public JpegFrame Frame { get; private set; }
/// <inheritdoc/>
public Size ImageSizeInPixels { get; private set; }
/// <inheritdoc/>
Size IImageDecoderInternals.Dimensions => this.Frame.PixelSize;
@ -845,12 +842,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
{
remaining -= length;
// Validity check: remaining part must be equal to components * 3
const int componentBytes = 3;
if (remaining != componentCount * componentBytes)
{
JpegThrowHelper.ThrowBadMarker("SOFn", remaining);
}
// components*3 bytes: component data
stream.Read(this.temp, 0, remaining);
// No need to pool this. They max out at 4
@ -885,9 +884,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
index += componentBytes;
}
this.Frame.MaxHorizontalFactor = maxH;
this.Frame.MaxVerticalFactor = maxV;
this.Frame.InitComponents();
this.Frame.Init(maxH, maxV);
this.scanDecoder.InjectFrameData(this.Frame, this);
}

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

@ -46,7 +46,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
Assert.Equal(1, decoder.Frame.ComponentCount);
Assert.Equal(1, decoder.Components.Length);
Size expectedSizeInBlocks = decoder.ImageSizeInPixels.DivideRoundUp(8);
Size expectedSizeInBlocks = decoder.Frame.PixelSize.DivideRoundUp(8);
Assert.Equal(expectedSizeInBlocks, decoder.Frame.McuSize);
@ -70,7 +70,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
using (JpegDecoderCore decoder = JpegFixture.ParseJpegStream(imageFile))
{
sb.AppendLine(imageFile);
sb.AppendLine($"Size:{decoder.ImageSizeInPixels} MCU:{decoder.Frame.McuSize}");
sb.AppendLine($"Size:{decoder.Frame.PixelSize} MCU:{decoder.Frame.McuSize}");
JpegComponent c0 = decoder.Components[0];
JpegComponent c1 = decoder.Components[1];

Loading…
Cancel
Save