Skip to content

Commit 3e7b2c9

Browse files
authored
Merge pull request #2073 from SixLabors/bp/arithmeticcoding
Add support for decoding jpeg's with arithmetic coding
2 parents cfd19ca + 5ed690d commit 3e7b2c9

38 files changed

+1764
-190
lines changed

src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public BmpDecoderCore(Configuration configuration, IBmpDecoderOptions options)
116116
/// <summary>
117117
/// Gets the dimensions of the image.
118118
/// </summary>
119-
public Size Dimensions => new Size(this.infoHeader.Width, this.infoHeader.Height);
119+
public Size Dimensions => new(this.infoHeader.Width, this.infoHeader.Height);
120120

121121
/// <inheritdoc />
122122
public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken)
@@ -389,7 +389,7 @@ private void ReadRle24<TPixel>(Buffer2D<TPixel> pixels, int width, int height, b
389389
if (rowHasUndefinedPixels)
390390
{
391391
// Slow path with undefined pixels.
392-
var yMulWidth = y * width;
392+
int yMulWidth = y * width;
393393
int rowStartIdx = yMulWidth * 3;
394394
for (int x = 0; x < width; x++)
395395
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
namespace SixLabors.ImageSharp.Formats.Jpeg.Components
5+
{
6+
internal enum ComponentType
7+
{
8+
Huffman = 0,
9+
10+
Arithmetic = 1
11+
}
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using SixLabors.ImageSharp.Memory;
5+
6+
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
7+
{
8+
internal class ArithmeticDecodingComponent : JpegComponent
9+
{
10+
public ArithmeticDecodingComponent(MemoryAllocator memoryAllocator, JpegFrame frame, byte id, int horizontalFactor, int verticalFactor, byte quantizationTableIndex, int index)
11+
: base(memoryAllocator, frame, id, horizontalFactor, verticalFactor, quantizationTableIndex, index)
12+
{
13+
}
14+
15+
/// <summary>
16+
/// Gets or sets the dc context.
17+
/// </summary>
18+
public int DcContext { get; set; }
19+
20+
/// <summary>
21+
/// Gets or sets the dc statistics.
22+
/// </summary>
23+
public ArithmeticStatistics DcStatistics { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets the ac statistics.
27+
/// </summary>
28+
public ArithmeticStatistics AcStatistics { get; set; }
29+
}
30+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
5+
{
6+
internal class ArithmeticDecodingTable
7+
{
8+
public ArithmeticDecodingTable(byte tableClass, byte identifier)
9+
{
10+
this.TableClass = tableClass;
11+
this.Identifier = identifier;
12+
}
13+
14+
public byte TableClass { get; }
15+
16+
public byte Identifier { get; }
17+
18+
public byte ConditioningTableValue { get; private set; }
19+
20+
public int DcL { get; private set; }
21+
22+
public int DcU { get; private set; }
23+
24+
public int AcKx { get; private set; }
25+
26+
public void Configure(byte conditioningTableValue)
27+
{
28+
this.ConditioningTableValue = conditioningTableValue;
29+
if (this.TableClass == 0)
30+
{
31+
this.DcL = conditioningTableValue & 0x0F;
32+
this.DcU = conditioningTableValue >> 4;
33+
this.AcKx = 0;
34+
}
35+
else
36+
{
37+
this.DcL = 0;
38+
this.DcU = 0;
39+
this.AcKx = conditioningTableValue;
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)