Skip to content

Commit

Permalink
Merge pull request #2767 from SixLabors/bp/Issue2763
Browse files Browse the repository at this point in the history
Webp: Fix Issue 2763
  • Loading branch information
JimBobSquarePants authored Jul 10, 2024
2 parents 23e000f + 9a9390c commit 10e9c49
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,8 @@ private void EncodeImage(int width, int height, bool useCache, CrunchConfig conf
}
}

histogramImageSize = maxIndex;

this.bitWriter.PutBits((uint)(this.HistoBits - 2), 3);
this.EncodeImageNoHuffman(
histogramBgra,
Expand All @@ -713,7 +715,7 @@ private void EncodeImage(int width, int height, bool useCache, CrunchConfig conf
// Store Huffman codes.
// Find maximum number of symbols for the huffman tree-set.
int maxTokens = 0;
for (int i = 0; i < 5 * histogramImage.Count; i++)
for (int i = 0; i < 5 * histogramImageSize; i++)
{
HuffmanTreeCode codes = huffmanCodes[i];
if (maxTokens < codes.NumSymbols)
Expand All @@ -728,7 +730,7 @@ private void EncodeImage(int width, int height, bool useCache, CrunchConfig conf
tokens[i] = new HuffmanTreeToken();
}

for (int i = 0; i < 5 * histogramImage.Count; i++)
for (int i = 0; i < 5 * histogramImageSize; i++)
{
HuffmanTreeCode codes = huffmanCodes[i];
this.StoreHuffmanCode(huffTree, tokens, codes);
Expand Down
17 changes: 17 additions & 0 deletions tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,23 @@ public void Encode_Lossy_WorksWithTestPattern<TPixel>(TestImageProvider<TPixel>
image.VerifyEncoder(provider, "webp", string.Empty, encoder, ImageComparer.Tolerant(0.04f));
}

// https://github.com/SixLabors/ImageSharp/issues/2763
[Theory]
[WithFile(Lossy.Issue2763, PixelTypes.Rgba32)]
public void WebpDecoder_CanDecode_Issue2763<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
WebpEncoder encoder = new()
{
Quality = 84,
FileFormat = WebpFileFormatType.Lossless
};

using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
image.DebugSave(provider);
image.VerifyEncoder(provider, "webp", string.Empty, encoder);
}

public static void RunEncodeLossy_WithPeakImage()
{
TestImageProvider<Rgba32> provider = TestImageProvider<Rgba32>.File(TestImageLossyFullPath);
Expand Down
1 change: 1 addition & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,7 @@ public static class Lossy
public const string Issue2243 = "Webp/issues/Issue2243.webp";
public const string Issue2257 = "Webp/issues/Issue2257.webp";
public const string Issue2670 = "Webp/issues/Issue2670.webp";
public const string Issue2763 = "Webp/issues/Issue2763.png";
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Images/Input/Webp/issues/Issue2763.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 10e9c49

Please sign in to comment.