@@ -50,8 +50,10 @@ public static Vp8LBackwardRefs GetBackwardReferences(
50
50
double bitCostBest = - 1 ;
51
51
int cacheBitsInitial = cacheBits ;
52
52
Vp8LHashChain ? hashChainBox = null ;
53
- var stats = new Vp8LStreaks ( ) ;
54
- var bitsEntropy = new Vp8LBitEntropy ( ) ;
53
+ Vp8LStreaks stats = new ( ) ;
54
+ Vp8LBitEntropy bitsEntropy = new ( ) ;
55
+
56
+ ColorCache [ ] colorCache = new ColorCache [ WebpConstants . MaxColorCacheBits + 1 ] ;
55
57
for ( int lz77Type = 1 ; lz77TypesToTry > 0 ; lz77TypesToTry &= ~ lz77Type , lz77Type <<= 1 )
56
58
{
57
59
int cacheBitsTmp = cacheBitsInitial ;
@@ -76,21 +78,19 @@ public static Vp8LBackwardRefs GetBackwardReferences(
76
78
}
77
79
78
80
// Next, try with a color cache and update the references.
79
- cacheBitsTmp = CalculateBestCacheSize ( bgra , quality , worst , cacheBitsTmp ) ;
81
+ cacheBitsTmp = CalculateBestCacheSize ( memoryAllocator , colorCache , bgra , quality , worst , cacheBitsTmp ) ;
80
82
if ( cacheBitsTmp > 0 )
81
83
{
82
84
BackwardRefsWithLocalCache ( bgra , cacheBitsTmp , worst ) ;
83
85
}
84
86
85
87
// Keep the best backward references.
86
- var histo = new Vp8LHistogram ( worst , cacheBitsTmp ) ;
88
+ using OwnedVp8LHistogram histo = OwnedVp8LHistogram . Create ( memoryAllocator , worst , cacheBitsTmp ) ;
87
89
double bitCost = histo . EstimateBits ( stats , bitsEntropy ) ;
88
90
89
91
if ( lz77TypeBest == 0 || bitCost < bitCostBest )
90
92
{
91
- Vp8LBackwardRefs tmp = worst ;
92
- worst = best ;
93
- best = tmp ;
93
+ ( best , worst ) = ( worst , best ) ;
94
94
bitCostBest = bitCost ;
95
95
cacheBits = cacheBitsTmp ;
96
96
lz77TypeBest = lz77Type ;
@@ -102,7 +102,7 @@ public static Vp8LBackwardRefs GetBackwardReferences(
102
102
{
103
103
Vp8LHashChain hashChainTmp = lz77TypeBest == ( int ) Vp8LLz77Type . Lz77Standard ? hashChain : hashChainBox ! ;
104
104
BackwardReferencesTraceBackwards ( width , height , memoryAllocator , bgra , cacheBits , hashChainTmp , best , worst ) ;
105
- var histo = new Vp8LHistogram ( worst , cacheBits ) ;
105
+ using OwnedVp8LHistogram histo = OwnedVp8LHistogram . Create ( memoryAllocator , worst , cacheBits ) ;
106
106
double bitCostTrace = histo . EstimateBits ( stats , bitsEntropy ) ;
107
107
if ( bitCostTrace < bitCostBest )
108
108
{
@@ -123,7 +123,13 @@ public static Vp8LBackwardRefs GetBackwardReferences(
123
123
/// The local color cache is also disabled for the lower (smaller then 25) quality.
124
124
/// </summary>
125
125
/// <returns>Best cache size.</returns>
126
- private static int CalculateBestCacheSize ( ReadOnlySpan < uint > bgra , uint quality , Vp8LBackwardRefs refs , int bestCacheBits )
126
+ private static int CalculateBestCacheSize (
127
+ MemoryAllocator memoryAllocator ,
128
+ Span < ColorCache > colorCache ,
129
+ ReadOnlySpan < uint > bgra ,
130
+ uint quality ,
131
+ Vp8LBackwardRefs refs ,
132
+ int bestCacheBits )
127
133
{
128
134
int cacheBitsMax = quality <= 25 ? 0 : bestCacheBits ;
129
135
if ( cacheBitsMax == 0 )
@@ -134,11 +140,11 @@ private static int CalculateBestCacheSize(ReadOnlySpan<uint> bgra, uint quality,
134
140
135
141
double entropyMin = MaxEntropy ;
136
142
int pos = 0 ;
137
- var colorCache = new ColorCache [ WebpConstants . MaxColorCacheBits + 1 ] ;
138
- var histos = new Vp8LHistogram [ WebpConstants . MaxColorCacheBits + 1 ] ;
139
- for ( int i = 0 ; i <= WebpConstants . MaxColorCacheBits ; i ++ )
143
+
144
+ using Vp8LHistogramSet histos = new ( memoryAllocator , colorCache . Length , 0 ) ;
145
+ for ( int i = 0 ; i < colorCache . Length ; i ++ )
140
146
{
141
- histos [ i ] = new Vp8LHistogram ( paletteCodeBits : i ) ;
147
+ histos [ i ] . PaletteCodeBits = i ;
142
148
colorCache [ i ] = new ColorCache ( i ) ;
143
149
}
144
150
@@ -149,10 +155,10 @@ private static int CalculateBestCacheSize(ReadOnlySpan<uint> bgra, uint quality,
149
155
if ( v . IsLiteral ( ) )
150
156
{
151
157
uint pix = bgra [ pos ++ ] ;
152
- uint a = ( pix >> 24 ) & 0xff ;
153
- uint r = ( pix >> 16 ) & 0xff ;
154
- uint g = ( pix >> 8 ) & 0xff ;
155
- uint b = ( pix >> 0 ) & 0xff ;
158
+ int a = ( int ) ( pix >> 24 ) & 0xff ;
159
+ int r = ( int ) ( pix >> 16 ) & 0xff ;
160
+ int g = ( int ) ( pix >> 8 ) & 0xff ;
161
+ int b = ( int ) ( pix >> 0 ) & 0xff ;
156
162
157
163
// The keys of the caches can be derived from the longest one.
158
164
int key = ColorCache . HashPix ( pix , 32 - cacheBitsMax ) ;
@@ -218,8 +224,8 @@ private static int CalculateBestCacheSize(ReadOnlySpan<uint> bgra, uint quality,
218
224
}
219
225
}
220
226
221
- var stats = new Vp8LStreaks ( ) ;
222
- var bitsEntropy = new Vp8LBitEntropy ( ) ;
227
+ Vp8LStreaks stats = new ( ) ;
228
+ Vp8LBitEntropy bitsEntropy = new ( ) ;
223
229
for ( int i = 0 ; i <= cacheBitsMax ; i ++ )
224
230
{
225
231
double entropy = histos [ i ] . EstimateBits ( stats , bitsEntropy ) ;
@@ -266,7 +272,7 @@ private static void BackwardReferencesHashChainDistanceOnly(
266
272
int pixCount = xSize * ySize ;
267
273
bool useColorCache = cacheBits > 0 ;
268
274
int literalArraySize = WebpConstants . NumLiteralCodes + WebpConstants . NumLengthCodes + ( cacheBits > 0 ? 1 << cacheBits : 0 ) ;
269
- var costModel = new CostModel ( literalArraySize ) ;
275
+ CostModel costModel = new ( memoryAllocator , literalArraySize ) ;
270
276
int offsetPrev = - 1 ;
271
277
int lenPrev = - 1 ;
272
278
double offsetCost = - 1 ;
@@ -280,7 +286,7 @@ private static void BackwardReferencesHashChainDistanceOnly(
280
286
}
281
287
282
288
costModel . Build ( xSize , cacheBits , refs ) ;
283
- using var costManager = new CostManager ( memoryAllocator , distArrayBuffer , pixCount , costModel ) ;
289
+ using CostManager costManager = new ( memoryAllocator , distArrayBuffer , pixCount , costModel ) ;
284
290
Span < float > costManagerCosts = costManager . Costs . GetSpan ( ) ;
285
291
Span < ushort > distArray = distArrayBuffer . GetSpan ( ) ;
286
292
@@ -441,12 +447,12 @@ private static void AddSingleLiteralWithCostModel(
441
447
int ix = useColorCache ? colorCache ! . Contains ( color ) : - 1 ;
442
448
if ( ix >= 0 )
443
449
{
444
- double mul0 = 0.68 ;
450
+ const double mul0 = 0.68 ;
445
451
costVal += costModel . GetCacheCost ( ( uint ) ix ) * mul0 ;
446
452
}
447
453
else
448
454
{
449
- double mul1 = 0.82 ;
455
+ const double mul1 = 0.82 ;
450
456
if ( useColorCache )
451
457
{
452
458
colorCache ! . Insert ( color ) ;
@@ -693,10 +699,8 @@ private static void BackwardReferencesLz77Box(int xSize, int ySize, ReadOnlySpan
693
699
bestLength = MaxLength ;
694
700
break ;
695
701
}
696
- else
697
- {
698
- bestLength = currLength ;
699
- }
702
+
703
+ bestLength = currLength ;
700
704
}
701
705
}
702
706
}
0 commit comments