From 4d599d14f63d15f06838f530d067a7558b1d734a Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 16 Jul 2021 13:32:38 +0300 Subject: [PATCH] Comments, docs, decoupling, removed redundant properties --- .../Jpeg/Components/Decoder/JpegComponent.cs | 11 +++++++--- .../Jpeg/Components/Decoder/JpegFrame.cs | 20 ++++++------------- .../Formats/Jpeg/JpegDecoderCore.cs | 9 +++------ .../Formats/Jpg/ParseStreamTests.cs | 4 ++-- 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs index 614e96e54a..33b5ef77ad 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs +++ b/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() + /// + /// Initializes component for future buffers initialization. + /// + /// Maximal horizontal subsampling factor among all the components. + /// Maximal vertical subsampling factor among all the components. + 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; diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs index b8f88cfe0e..01863b7a8c 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegFrame.cs @@ -69,16 +69,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder /// public JpegComponent[] Components { get; set; } - /// - /// Gets or sets the maximum horizontal sampling factor. - /// - public int MaxHorizontalFactor { get; set; } - - /// - /// Gets or sets the maximum vertical sampling factor. - /// - public int MaxVerticalFactor { get; set; } - /// /// Gets or sets the number of MCU's per line. /// @@ -116,15 +106,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder /// /// Allocates the frame component blocks. /// - public void InitComponents() + /// Maximal horizontal subsampling factor among all the components. + /// Maximal vertical subsampling factor among all the components. + 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); } } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 3c48aabee0..406458fe37 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -106,9 +106,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg /// public JpegFrame Frame { get; private set; } - /// - public Size ImageSizeInPixels { get; private set; } - /// 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); } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs index e1307d3fc6..0a4d85344b 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/ParseStreamTests.cs +++ b/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];