Browse Source

Added debug methods

Fixed System namespace usage
pull/1673/head
Dmitry Pentin 5 years ago
parent
commit
10b4a59c7c
  1. 49
      src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanScanEncoder.cs

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

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using SixLabors.ImageSharp.Memory;
@ -427,5 +428,53 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder
return Numerics.Log2(value << 1);
#endif
}
// !!! DO NOT DELETE THIS !!! until custom huffman tables are supported - this is very handy for debugging
/// <summary>
/// Prints given huffman codes to the System.Console for debugging puroses.
/// </summary>
/// <param name="codes">Codes array.</param>
/// <param name="title">Custom title to print.</param>
/// <param name="printCode">Flag indicating if all codes should be printed.</param>
/// <param name="codePrintBase">Indicates which number base will be used to print codes</param>
private static void PrintHuffmanSummary(int[] codes, string title, bool printCode, int codePrintBase = 2)
{
System.Console.WriteLine(title);
System.Console.WriteLine($"Codes count: {codes.Length}");
// This is possible if custom tree is provided, especially for per-image optimized tree
if (codes.Length == 0)
{
return;
}
// Min
int min = codes.Min();
int min_len = min >> 24;
string min_code = System.Convert.ToString(min & ((1 << 24) - 1), codePrintBase);
// Max
int max = codes.Max();
int max_len = max >> 24;
string max_code = System.Convert.ToString(max & ((1 << 24) - 1), codePrintBase);
System.Console.WriteLine($"Min code: {min_code}, len: {min_len} \nMax code: {max_code}, len: {max_len}");
// Printing codes
if (printCode)
{
PrintHuffmanCodes(codes, codePrintBase);
}
}
private static void PrintHuffmanCodes(int[] codes, int codePrintBase)
{
for (int i = 0; i < codes.Length; i++)
{
int huffCode = codes[i];
string code = System.Convert.ToString(huffCode & ((1 << 24) - 1), codePrintBase);
System.Console.WriteLine($"\t{code}");
}
}
}
}

Loading…
Cancel
Save