From abe02719660cc4afd93d2f4f9be59bd360919083 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sat, 10 Oct 2020 19:10:17 +0200 Subject: [PATCH] Fix in BackwardRefsWithLocalCache: mode was not changed to CacheIdx when there was a color cache hit --- .../WebP/Lossless/BackwardReferenceEncoder.cs | 25 +++++++++++-------- .../Formats/WebP/Lossless/PixOrCopy.cs | 12 +++------ 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/ImageSharp/Formats/WebP/Lossless/BackwardReferenceEncoder.cs b/src/ImageSharp/Formats/WebP/Lossless/BackwardReferenceEncoder.cs index 8af42a8b49..0be198c15d 100644 --- a/src/ImageSharp/Formats/WebP/Lossless/BackwardReferenceEncoder.cs +++ b/src/ImageSharp/Formats/WebP/Lossless/BackwardReferenceEncoder.cs @@ -130,7 +130,7 @@ namespace SixLabors.ImageSharp.Formats.WebP.Lossless uint bestBgra; int minPos = (basePosition > windowSize) ? basePosition - windowSize : 0; int lengthMax = (maxLen < 256) ? maxLen : 256; - pos = (int)chain[basePosition]; + pos = chain[basePosition]; int currLength; // Heuristic: use the comparison with the above line as an initialization. @@ -240,7 +240,6 @@ namespace SixLabors.ImageSharp.Formats.WebP.Lossless Vp8LBackwardRefs best, Vp8LBackwardRefs worst) { - var histo = new Vp8LHistogram[WebPConstants.MaxColorCacheBits]; int lz77TypeBest = 0; double bitCostBest = -1; int cacheBitsInitial = cacheBits; @@ -276,8 +275,8 @@ namespace SixLabors.ImageSharp.Formats.WebP.Lossless } // Keep the best backward references. - histo[0] = new Vp8LHistogram(worst, cacheBitsTmp); - var bitCost = histo[0].EstimateBits(); + var histo = new Vp8LHistogram(worst, cacheBitsTmp); + var bitCost = histo.EstimateBits(); if (lz77TypeBest == 0 || bitCost < bitCostBest) { @@ -295,8 +294,8 @@ namespace SixLabors.ImageSharp.Formats.WebP.Lossless { Vp8LHashChain hashChainTmp = (lz77TypeBest == (int)Vp8LLz77Type.Lz77Standard) ? hashChain : hashChainBox; BackwardReferencesTraceBackwards(width, height, bgra, cacheBits, hashChainTmp, best, worst); - histo[0] = new Vp8LHistogram(worst, cacheBits); - double bitCostTrace = histo[0].EstimateBits(); + var histo = new Vp8LHistogram(worst, cacheBits); + double bitCostTrace = histo.EstimateBits(); if (bitCostTrace < bitCostBest) { best = worst; @@ -335,7 +334,7 @@ namespace SixLabors.ImageSharp.Formats.WebP.Lossless } // TODO: Don't use the enumerator here. - // Find the cache_bits giving the lowest entropy. + // Find the cacheBits giving the lowest entropy. using List.Enumerator c = refs.Refs.GetEnumerator(); while (c.MoveNext()) { @@ -384,11 +383,13 @@ namespace SixLabors.ImageSharp.Formats.WebP.Lossless uint bgraPrev = bgra[pos] ^ 0xffffffffu; // TODO: Original has this loop? - // VP8LPrefixEncode(len, &code, &extra_bits, &extra_bits_value); - // for (i = 0; i <= cache_bits_max; ++i) + // int extraBits = 0, extraBitsValue = 0; + // int code = LosslessUtils.PrefixEncode(len, ref extraBits, ref extraBitsValue); + // for (int i = 0; i <= cacheBitsMax; ++i) // { - // ++histos[i]->literal_[NUM_LITERAL_CODES + code]; + // ++histos[i].Literal[WebPConstants.NumLiteralCodes + code]; // } + // Update the color caches. do { @@ -957,7 +958,9 @@ namespace SixLabors.ImageSharp.Formats.WebP.Lossless if (ix >= 0) { // Color cache contains bgraLiteral - PixOrCopy.CreateCacheIdx(ix); + v.Mode = PixOrCopyMode.CacheIdx; + v.BgraOrDistance = (uint)ix; + v.Len = 1; } else { diff --git a/src/ImageSharp/Formats/WebP/Lossless/PixOrCopy.cs b/src/ImageSharp/Formats/WebP/Lossless/PixOrCopy.cs index 8974092e67..96f90c0293 100644 --- a/src/ImageSharp/Formats/WebP/Lossless/PixOrCopy.cs +++ b/src/ImageSharp/Formats/WebP/Lossless/PixOrCopy.cs @@ -16,38 +16,32 @@ namespace SixLabors.ImageSharp.Formats.WebP.Lossless public static PixOrCopy CreateCacheIdx(int idx) { - var retval = new PixOrCopy() + return new PixOrCopy() { Mode = PixOrCopyMode.CacheIdx, BgraOrDistance = (uint)idx, Len = 1 }; - - return retval; } public static PixOrCopy CreateLiteral(uint bgra) { - var retval = new PixOrCopy() + return new PixOrCopy() { Mode = PixOrCopyMode.Literal, BgraOrDistance = bgra, Len = 1 }; - - return retval; } public static PixOrCopy CreateCopy(uint distance, ushort len) { - var retval = new PixOrCopy() + return new PixOrCopy() { Mode = PixOrCopyMode.Copy, BgraOrDistance = distance, Len = len }; - - return retval; } public uint Literal(int component)