22// Licensed under the Six Labors Split License.
33#nullable disable
44
5+ using System . Buffers ;
56using System . Runtime . CompilerServices ;
67using SixLabors . ImageSharp . Memory ;
78
@@ -45,9 +46,9 @@ public static void GetHistoImageSymbols(
4546 int imageHistoRawSize = histoXSize * histoYSize ;
4647 const int entropyCombineNumBins = BinSize ;
4748
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 ) ;
5152
5253 using Vp8LHistogramSet origHisto = new ( memoryAllocator , imageHistoRawSize , cacheBits ) ;
5354
@@ -60,13 +61,12 @@ public static void GetHistoImageSymbols(
6061 bool entropyCombine = numUsed > entropyCombineNumBins * 2 && quality < 100 ;
6162 if ( entropyCombine )
6263 {
63- ushort [ ] binMap = mapTmp ;
6464 int numClusters = numUsed ;
6565 double combineCostFactor = GetCombineCostFactor ( imageHistoRawSize , quality ) ;
66- HistogramAnalyzeEntropyBin ( imageHisto , binMap ) ;
66+ HistogramAnalyzeEntropyBin ( imageHisto , mapTmp ) ;
6767
6868 // Collapse histograms with similar entropy.
69- HistogramCombineEntropyBin ( imageHisto , histogramSymbols , clusterMappings , tmpHisto , binMap , entropyCombineNumBins , combineCostFactor ) ;
69+ HistogramCombineEntropyBin ( imageHisto , histogramSymbols , clusterMappings , tmpHisto , mapTmp , entropyCombineNumBins , combineCostFactor ) ;
7070
7171 OptimizeHistogramSymbols ( clusterMappings , numClusters , mapTmp , histogramSymbols ) ;
7272 }
@@ -128,7 +128,7 @@ private static void HistogramBuild(
128128 /// Partition histograms to different entropy bins for three dominant (literal,
129129 /// red and blue) symbol costs and compute the histogram aggregate bitCost.
130130 /// </summary>
131- private static void HistogramAnalyzeEntropyBin ( Vp8LHistogramSet histograms , ushort [ ] binMap )
131+ private static void HistogramAnalyzeEntropyBin ( Vp8LHistogramSet histograms , Span < ushort > binMap )
132132 {
133133 int histoSize = histograms . Count ;
134134 DominantCostRange costRange = new ( ) ;
@@ -198,9 +198,9 @@ private static int HistogramCopyAndAnalyze(
198198 private static void HistogramCombineEntropyBin (
199199 Vp8LHistogramSet histograms ,
200200 Span < ushort > clusters ,
201- ushort [ ] clusterMappings ,
201+ Span < ushort > clusterMappings ,
202202 Vp8LHistogram curCombo ,
203- ushort [ ] binMap ,
203+ ReadOnlySpan < ushort > binMap ,
204204 int numBins ,
205205 double combineCostFactor )
206206 {
@@ -276,7 +276,7 @@ private static void HistogramCombineEntropyBin(
276276 /// Given a Histogram set, the mapping of clusters 'clusterMapping' and the
277277 /// current assignment of the cells in 'symbols', merge the clusters and assign the smallest possible clusters values.
278278 /// </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 )
280280 {
281281 bool doContinue = true ;
282282
@@ -303,7 +303,7 @@ private static void OptimizeHistogramSymbols(ushort[] clusterMappings, int numCl
303303
304304 // Create a mapping from a cluster id to its minimal version.
305305 int clusterMax = 0 ;
306- clusterMappingsTmp . AsSpan ( ) . Clear ( ) ;
306+ clusterMappingsTmp . Clear ( ) ;
307307
308308 // Re-map the ids.
309309 for ( int i = 0 ; i < symbols . Length ; i ++ )
@@ -515,7 +515,6 @@ private static void HistogramCombineGreedy(Vp8LHistogramSet histograms)
515515 histograms . DisposeAt ( idx2 ) ;
516516
517517 // Remove pairs intersecting the just combined best pair.
518- // TODO: Reversing this will avoid the need to remove from the end of the list.
519518 for ( int i = 0 ; i < histoPriorityList . Count ; )
520519 {
521520 HistogramPair p = histoPriorityList [ i ] ;
0 commit comments