From 28a8acac01acab6537ca36cdb87c3e9ccfa02d65 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 27 Jun 2017 09:22:09 +1000 Subject: [PATCH] Fix progressive scan decoding --- .../Formats/Jpeg/Port/Components/Component.cs | 4 +-- .../Jpeg/Port/Components/ScanDecoder.cs | 31 ++++++++++--------- .../Formats/Jpeg/Port/JpegDecoderCore.cs | 4 +-- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Port/Components/Component.cs b/src/ImageSharp/Formats/Jpeg/Port/Components/Component.cs index 3b462514c..db3170613 100644 --- a/src/ImageSharp/Formats/Jpeg/Port/Components/Component.cs +++ b/src/ImageSharp/Formats/Jpeg/Port/Components/Component.cs @@ -22,12 +22,12 @@ namespace ImageSharp.Formats.Jpeg.Port.Components /// /// Gets or sets the horizontal scaling factor /// - public int ScaleX; + public float ScaleX; /// /// Gets or sets the vertical scaling factor /// - public int ScaleY; + public float ScaleY; /// /// Gets or sets the number of blocks per line diff --git a/src/ImageSharp/Formats/Jpeg/Port/Components/ScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Port/Components/ScanDecoder.cs index 59867006f..2ec9ea905 100644 --- a/src/ImageSharp/Formats/Jpeg/Port/Components/ScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Port/Components/ScanDecoder.cs @@ -135,20 +135,20 @@ namespace ImageSharp.Formats.Jpeg.Port.Components int mcuToRead = resetInterval > 0 ? Math.Min(mcuExpected - mcu, resetInterval) : mcuExpected; // TODO: We might just be able to loop here. - if (componentsLength == 1) + // if (componentsLength == 1) + // { + // ref FrameComponent c = ref components[componentIndex]; + // c.Pred = 0; + // } + // else + // { + for (int i = 0; i < components.Length; i++) { - ref FrameComponent c = ref components[componentIndex]; + ref FrameComponent c = ref components[i]; c.Pred = 0; } - else - { - for (int i = 0; i < componentsLength; i++) - { - ref FrameComponent c = ref components[i]; - c.Pred = 0; - } - } + // } this.eobrun = 0; if (componentsLength == 1) @@ -280,12 +280,13 @@ namespace ImageSharp.Formats.Jpeg.Port.Components { int index = this.ReadBit(stream); HuffmanBranch branch = node[index]; - node = branch.Children; if (branch.Value > -1) { return branch.Value; } + + node = branch.Children; } } @@ -438,13 +439,13 @@ namespace ImageSharp.Formats.Jpeg.Port.Components } this.successiveACNextValue = this.ReceiveAndExtend(s, stream); - this.successiveACState = r > 0 ? 2 : 3; + this.successiveACState = r != 0 ? 2 : 3; } continue; case 1: // Skipping r zero items case 2: - if (component.BlockData[offset + z] > 0) + if (component.BlockData[offset + z] != 0) { component.BlockData[offset + z] += (short)(this.ReadBit(stream) << this.successiveState); } @@ -459,7 +460,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components break; case 3: // Set value for a zero item - if (component.BlockData[offset + z] > 0) + if (component.BlockData[offset + z] != 0) { component.BlockData[offset + z] += (short)(this.ReadBit(stream) << this.successiveState); } @@ -471,7 +472,7 @@ namespace ImageSharp.Formats.Jpeg.Port.Components break; case 4: // Eob - if (component.BlockData[offset + z] > 0) + if (component.BlockData[offset + z] != 0) { component.BlockData[offset + z] += (short)(this.ReadBit(stream) << this.successiveState); } diff --git a/src/ImageSharp/Formats/Jpeg/Port/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/Port/JpegDecoderCore.cs index 21c28043b..b608b4951 100644 --- a/src/ImageSharp/Formats/Jpeg/Port/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/Port/JpegDecoderCore.cs @@ -326,8 +326,8 @@ namespace ImageSharp.Formats.Jpeg.Port ref var frameComponent = ref this.frame.Components[i]; var component = new Component { - ScaleX = frameComponent.HorizontalFactor / this.frame.MaxHorizontalFactor, - ScaleY = frameComponent.VerticalFactor / this.frame.MaxVerticalFactor, + ScaleX = frameComponent.HorizontalFactor / (float)this.frame.MaxHorizontalFactor, + ScaleY = frameComponent.VerticalFactor / (float)this.frame.MaxVerticalFactor, BlocksPerLine = frameComponent.BlocksPerLine, BlocksPerColumn = frameComponent.BlocksPerColumn };