Browse Source

Simplified huffman encoding via compiled tree

Squash

squash
pull/1673/head
Dmitry Pentin 5 years ago
parent
commit
3eb6d6d1d6
  1. 8
      src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanLut.cs
  2. 3
      src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanScanEncoder.cs
  3. 6
      src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanSpec.cs

8
src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanLut.cs

@ -5,8 +5,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
{ {
/// <summary> /// <summary>
/// A compiled look-up table representation of a huffmanSpec. /// A compiled look-up table representation of a huffmanSpec.
/// Each value maps to a uint32 of which the 8 most significant bits hold the /// Each value maps to a int32 of which the 24 most significant bits hold the
/// codeword size in bits and the 24 least significant bits hold the codeword. /// codeword in bits and the 8 least significant bits hold the codeword size.
/// The maximum codeword size is 16 bits. /// The maximum codeword size is 16 bits.
/// </summary> /// </summary>
internal readonly struct HuffmanLut internal readonly struct HuffmanLut
@ -51,10 +51,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
for (int i = 0; i < spec.Count.Length; i++) for (int i = 0; i < spec.Count.Length; i++)
{ {
int bits = (i + 1) << 24; int len = i + 1;
for (int j = 0; j < spec.Count[i]; j++) for (int j = 0; j < spec.Count[i]; j++)
{ {
this.Values[spec.Values[k]] = bits | code; this.Values[spec.Values[k]] = len | (code << 8);
code++; code++;
k++; k++;
} }

3
src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanScanEncoder.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -354,7 +355,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
private void EmitHuff(int[] table, int value) private void EmitHuff(int[] table, int value)
{ {
int x = table[value]; int x = table[value];
this.Emit(x & ((1 << 24) - 1), x >> 24); this.Emit(x >> 8, x & 0xff);
} }
/// <summary> /// <summary>

6
src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanSpec.cs

@ -24,9 +24,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
0, 0, 0 0, 0, 0
}, },
new byte[] new byte[]
{ {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
}), }),
// Luminance AC. // Luminance AC.
new HuffmanSpec( new HuffmanSpec(

Loading…
Cancel
Save