2
2
// Licensed under the Six Labors Split License.
3
3
#nullable disable
4
4
5
+ using System . Buffers ;
5
6
using System . Runtime . CompilerServices ;
6
7
using SixLabors . ImageSharp . Memory ;
7
8
@@ -45,9 +46,9 @@ public static void GetHistoImageSymbols(
45
46
int imageHistoRawSize = histoXSize * histoYSize ;
46
47
const int entropyCombineNumBins = BinSize ;
47
48
48
- // TODO: Allocations!
49
- ushort [ ] mapTmp = new ushort [ imageHistoRawSize ] ;
50
- ushort [ ] clusterMappings = new ushort [ imageHistoRawSize ] ;
49
+ using IMemoryOwner < ushort > tmp = memoryAllocator . Allocate < ushort > ( imageHistoRawSize * 2 , AllocationOptions . Clean ) ;
50
+ Span < ushort > mapTmp = tmp . Slice ( 0 , imageHistoRawSize ) ;
51
+ Span < ushort > clusterMappings = tmp . Slice ( imageHistoRawSize , imageHistoRawSize ) ;
51
52
52
53
using Vp8LHistogramSet origHisto = new ( memoryAllocator , imageHistoRawSize , cacheBits ) ;
53
54
@@ -60,13 +61,12 @@ public static void GetHistoImageSymbols(
60
61
bool entropyCombine = numUsed > entropyCombineNumBins * 2 && quality < 100 ;
61
62
if ( entropyCombine )
62
63
{
63
- ushort [ ] binMap = mapTmp ;
64
64
int numClusters = numUsed ;
65
65
double combineCostFactor = GetCombineCostFactor ( imageHistoRawSize , quality ) ;
66
- HistogramAnalyzeEntropyBin ( imageHisto , binMap ) ;
66
+ HistogramAnalyzeEntropyBin ( imageHisto , mapTmp ) ;
67
67
68
68
// Collapse histograms with similar entropy.
69
- HistogramCombineEntropyBin ( imageHisto , histogramSymbols , clusterMappings , tmpHisto , binMap , entropyCombineNumBins , combineCostFactor ) ;
69
+ HistogramCombineEntropyBin ( imageHisto , histogramSymbols , clusterMappings , tmpHisto , mapTmp , entropyCombineNumBins , combineCostFactor ) ;
70
70
71
71
OptimizeHistogramSymbols ( clusterMappings , numClusters , mapTmp , histogramSymbols ) ;
72
72
}
@@ -128,7 +128,7 @@ private static void HistogramBuild(
128
128
/// Partition histograms to different entropy bins for three dominant (literal,
129
129
/// red and blue) symbol costs and compute the histogram aggregate bitCost.
130
130
/// </summary>
131
- private static void HistogramAnalyzeEntropyBin ( Vp8LHistogramSet histograms , ushort [ ] binMap )
131
+ private static void HistogramAnalyzeEntropyBin ( Vp8LHistogramSet histograms , Span < ushort > binMap )
132
132
{
133
133
int histoSize = histograms . Count ;
134
134
DominantCostRange costRange = new ( ) ;
@@ -198,9 +198,9 @@ private static int HistogramCopyAndAnalyze(
198
198
private static void HistogramCombineEntropyBin (
199
199
Vp8LHistogramSet histograms ,
200
200
Span < ushort > clusters ,
201
- ushort [ ] clusterMappings ,
201
+ Span < ushort > clusterMappings ,
202
202
Vp8LHistogram curCombo ,
203
- ushort [ ] binMap ,
203
+ ReadOnlySpan < ushort > binMap ,
204
204
int numBins ,
205
205
double combineCostFactor )
206
206
{
@@ -276,7 +276,7 @@ private static void HistogramCombineEntropyBin(
276
276
/// Given a Histogram set, the mapping of clusters 'clusterMapping' and the
277
277
/// current assignment of the cells in 'symbols', merge the clusters and assign the smallest possible clusters values.
278
278
/// </summary>
279
- private static void OptimizeHistogramSymbols ( ushort [ ] clusterMappings , int numClusters , ushort [ ] clusterMappingsTmp , Span < ushort > symbols )
279
+ private static void OptimizeHistogramSymbols ( Span < ushort > clusterMappings , int numClusters , Span < ushort > clusterMappingsTmp , Span < ushort > symbols )
280
280
{
281
281
bool doContinue = true ;
282
282
@@ -303,7 +303,7 @@ private static void OptimizeHistogramSymbols(ushort[] clusterMappings, int numCl
303
303
304
304
// Create a mapping from a cluster id to its minimal version.
305
305
int clusterMax = 0 ;
306
- clusterMappingsTmp . AsSpan ( ) . Clear ( ) ;
306
+ clusterMappingsTmp . Clear ( ) ;
307
307
308
308
// Re-map the ids.
309
309
for ( int i = 0 ; i < symbols . Length ; i ++ )
@@ -515,7 +515,6 @@ private static void HistogramCombineGreedy(Vp8LHistogramSet histograms)
515
515
histograms . DisposeAt ( idx2 ) ;
516
516
517
517
// Remove pairs intersecting the just combined best pair.
518
- // TODO: Reversing this will avoid the need to remove from the end of the list.
519
518
for ( int i = 0 ; i < histoPriorityList . Count ; )
520
519
{
521
520
HistogramPair p = histoPriorityList [ i ] ;
0 commit comments