Skip to content

Commit

Permalink
Fix up DerivedTable
Browse files Browse the repository at this point in the history
Fix up HuffmanTable.Tables
Add ZigZag.

Trying to enjoy the weekend.
  • Loading branch information
juliusfriedman committed Oct 26, 2024
1 parent 21de2ae commit 5904c7b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
18 changes: 12 additions & 6 deletions Codecs/Image/Jpeg/Classes/DerivedTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ namespace Codec.Jpeg.Classes;

internal class DerivedTable : MemorySegment
{
public const int Length = 17;
public const int Length = 1;

public const int CodeLength = 16;

public DerivedTable(MemorySegment segment) : base(segment)
{
}

public DerivedTable(int size) : base(new byte[Length + size])
{
}

/// <summary>
/// Table class, 0 = DC table or lossless table, 1 = AC table
/// Baseline 0 or 1, Progressive DCT or Lossless = 0
Expand Down Expand Up @@ -52,11 +58,11 @@ public int Th
/// </summary>
public MemorySegment Li
{
get => this.Slice(Offset + 1, Length - 1);
set => value.CopyTo(Array, Offset + 1, Length - 1);
get => this.Slice(Offset + Length, CodeLength);
set => value.CopyTo(Array, Offset + Length, CodeLength);
}

public int ValuesCount
public int CodeLengthSum
{
get
{
Expand All @@ -70,7 +76,7 @@ public int ValuesCount
/// </summary>
public MemorySegment Vi
{
get => this.Slice(Offset + Length, ValuesCount);
set => value.CopyTo(Array, Offset + Length, ValuesCount);
get => this.Slice(Offset + CodeLength, CodeLengthSum);
set => value.CopyTo(Array, Offset + CodeLength, CodeLengthSum);
}
}
11 changes: 11 additions & 0 deletions Codecs/Image/Jpeg/JpegCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,17 @@ internal static void Decompress(JpegImage jpegImage)

#region Compress

internal ReadOnlySpan<byte> ZigZag => [
0, 1, 5, 6,14,15,27,28,
2, 4, 7,13,16,26,29,42,
3, 8,12,17,25,30,41,43,
9,11,18,24,31,40,44,53,
10,19,23,32,39,45,52,54,
20,22,33,38,46,51,55,60,
21,34,37,47,50,56,59,61,
35,36,48,49,57,58,62,63
];

internal static void FDCT(Span<double> input, Span<double> output)
{
for (int u = 0; u < BlockSize; u++)
Expand Down
2 changes: 1 addition & 1 deletion Codecs/Image/Jpeg/Markers/HuffmanTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal IEnumerable<DerivedTable> Tables
using var slice = this.Slice(offset);
using var derivedTable = new DerivedTable(slice);
yield return derivedTable;
offset += DerivedTable.Length + derivedTable.ValuesCount;
offset += DerivedTable.Length + DerivedTable.CodeLength + derivedTable.CodeLengthSum;
}
}
}
Expand Down

0 comments on commit 5904c7b

Please sign in to comment.