Skip to content

Commit

Permalink
Start to quantize entropy.
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusfriedman committed Oct 24, 2024
1 parent 27ef62e commit 8c184fe
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 153 deletions.
41 changes: 41 additions & 0 deletions Codecs/Image/Jpeg/Classes/HuffmanTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Collections.Generic;

namespace Codec.Jpeg.Classes;

internal class HuffmanTable
{
public byte Id;

public int[] MinCode { get; set; }
public int[] MaxCode { get; set; }
public int[] ValPtr { get; set; }
public byte[] Values { get; set; }
public Dictionary<int, (int code, int length)> CodeTable { get; set; }

public HuffmanTable()
{
MinCode = new int[16];
MaxCode = new int[16];
ValPtr = new int[16];
Values = new byte[256];
CodeTable = new Dictionary<int, (int code, int length)>();
}

public (int code, int length) GetCode(int value)
{
if (CodeTable.TryGetValue(value, out var codeInfo))
{
return codeInfo;
}
return (0, 0);
}

public int GetCodeLength(int value)
{
if (CodeTable.TryGetValue(value, out var codeInfo))
{
return codeInfo.length;
}
return 0;
}
}
58 changes: 58 additions & 0 deletions Codecs/Image/Jpeg/Classes/JpegState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Codec.Jpeg.Markers;
using System;
using static Media.Codec.Jpeg.JpegCodec;
using System.Collections.Generic;

namespace Codec.Jpeg.Classes;

Expand Down Expand Up @@ -33,6 +35,62 @@ internal sealed class JpegState : IEquatable<JpegState>
/// </summary>
public byte Al;

public HuffmanTable DcTable = new HuffmanTable
{
Id = 0,
MinCode = [0, 1, 5, 6, 14, 30, 62, 126, 254, 510, 1022, 2046, 4094, 8190, 16382, 32766],
MaxCode = [0, 1, 5, 6, 14, 30, 62, 126, 254, 510, 1022, 2046, 4094, 8190, 16382, 32766],
ValPtr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
Values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
CodeTable = new Dictionary<int, (int code, int length)>
{
{ 0, (0b00, 2) },
{ 1, (0b01, 2) },
{ 2, (0b100, 3) },
{ 3, (0b101, 3) },
{ 4, (0b1100, 4) },
{ 5, (0b1101, 4) },
{ 6, (0b11100, 5) },
{ 7, (0b11101, 5) },
{ 8, (0b111100, 6) },
{ 9, (0b111101, 6) },
{ 10, (0b1111100, 7) },
{ 11, (0b1111101, 7) },
{ 12, (0b11111100, 8) },
{ 13, (0b11111101, 8) },
{ 14, (0b111111100, 9) },
{ 15, (0b111111101, 9) }
}
};

public HuffmanTable AcTable = new HuffmanTable
{
Id = 1,
MinCode = [0, 1, 5, 6, 14, 30, 62, 126, 254, 510, 1022, 2046, 4094, 8190, 16382, 32766],
MaxCode = [0, 1, 5, 6, 14, 30, 62, 126, 254, 510, 1022, 2046, 4094, 8190, 16382, 32766],
ValPtr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
Values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
CodeTable = new Dictionary<int, (int code, int length)>
{
{ 0, (0b00, 2) },
{ 1, (0b01, 2) },
{ 2, (0b100, 3) },
{ 3, (0b101, 3) },
{ 4, (0b1100, 4) },
{ 5, (0b1101, 4) },
{ 6, (0b11100, 5) },
{ 7, (0b11101, 5) },
{ 8, (0b111100, 6) },
{ 9, (0b111101, 6) },
{ 10, (0b1111100, 7) },
{ 11, (0b1111101, 7) },
{ 12, (0b11111100, 8) },
{ 13, (0b11111101, 8) },
{ 14, (0b111111100, 9) },
{ 15, (0b111111101, 9) }
}
};

/// <summary>
/// Constructors a <see cref="JpegState"/>
/// </summary>
Expand Down
Loading

0 comments on commit 8c184fe

Please sign in to comment.