From 5904c7bb6d5f74d98d64c8c1c657cc822560eb5f Mon Sep 17 00:00:00 2001 From: Julius Friedman Date: Fri, 25 Oct 2024 20:37:55 -0400 Subject: [PATCH] Fix up DerivedTable Fix up HuffmanTable.Tables Add ZigZag. Trying to enjoy the weekend. --- Codecs/Image/Jpeg/Classes/DerivedTable.cs | 18 ++++++++++++------ Codecs/Image/Jpeg/JpegCodec.cs | 11 +++++++++++ Codecs/Image/Jpeg/Markers/HuffmanTable.cs | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Codecs/Image/Jpeg/Classes/DerivedTable.cs b/Codecs/Image/Jpeg/Classes/DerivedTable.cs index 0ec221fe..18f3b9c4 100644 --- a/Codecs/Image/Jpeg/Classes/DerivedTable.cs +++ b/Codecs/Image/Jpeg/Classes/DerivedTable.cs @@ -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]) + { + } + /// /// Table class, 0 = DC table or lossless table, 1 = AC table /// Baseline 0 or 1, Progressive DCT or Lossless = 0 @@ -52,11 +58,11 @@ public int Th /// 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 { @@ -70,7 +76,7 @@ public int ValuesCount /// 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); } } diff --git a/Codecs/Image/Jpeg/JpegCodec.cs b/Codecs/Image/Jpeg/JpegCodec.cs index 19498c1b..3f158797 100644 --- a/Codecs/Image/Jpeg/JpegCodec.cs +++ b/Codecs/Image/Jpeg/JpegCodec.cs @@ -283,6 +283,17 @@ internal static void Decompress(JpegImage jpegImage) #region Compress + internal ReadOnlySpan 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 input, Span output) { for (int u = 0; u < BlockSize; u++) diff --git a/Codecs/Image/Jpeg/Markers/HuffmanTable.cs b/Codecs/Image/Jpeg/Markers/HuffmanTable.cs index 259a1739..e47476c4 100644 --- a/Codecs/Image/Jpeg/Markers/HuffmanTable.cs +++ b/Codecs/Image/Jpeg/Markers/HuffmanTable.cs @@ -33,7 +33,7 @@ internal IEnumerable 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; } } }