Skip to content

Commit 10e9c49

Browse files
Merge pull request #2767 from SixLabors/bp/Issue2763
Webp: Fix Issue 2763
2 parents 23e000f + 9a9390c commit 10e9c49

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,8 @@ private void EncodeImage(int width, int height, bool useCache, CrunchConfig conf
698698
}
699699
}
700700

701+
histogramImageSize = maxIndex;
702+
701703
this.bitWriter.PutBits((uint)(this.HistoBits - 2), 3);
702704
this.EncodeImageNoHuffman(
703705
histogramBgra,
@@ -713,7 +715,7 @@ private void EncodeImage(int width, int height, bool useCache, CrunchConfig conf
713715
// Store Huffman codes.
714716
// Find maximum number of symbols for the huffman tree-set.
715717
int maxTokens = 0;
716-
for (int i = 0; i < 5 * histogramImage.Count; i++)
718+
for (int i = 0; i < 5 * histogramImageSize; i++)
717719
{
718720
HuffmanTreeCode codes = huffmanCodes[i];
719721
if (maxTokens < codes.NumSymbols)
@@ -728,7 +730,7 @@ private void EncodeImage(int width, int height, bool useCache, CrunchConfig conf
728730
tokens[i] = new HuffmanTreeToken();
729731
}
730732

731-
for (int i = 0; i < 5 * histogramImage.Count; i++)
733+
for (int i = 0; i < 5 * histogramImageSize; i++)
732734
{
733735
HuffmanTreeCode codes = huffmanCodes[i];
734736
this.StoreHuffmanCode(huffTree, tokens, codes);

tests/ImageSharp.Tests/Formats/WebP/WebpEncoderTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,23 @@ public void Encode_Lossy_WorksWithTestPattern<TPixel>(TestImageProvider<TPixel>
497497
image.VerifyEncoder(provider, "webp", string.Empty, encoder, ImageComparer.Tolerant(0.04f));
498498
}
499499

500+
// https://github.com/SixLabors/ImageSharp/issues/2763
501+
[Theory]
502+
[WithFile(Lossy.Issue2763, PixelTypes.Rgba32)]
503+
public void WebpDecoder_CanDecode_Issue2763<TPixel>(TestImageProvider<TPixel> provider)
504+
where TPixel : unmanaged, IPixel<TPixel>
505+
{
506+
WebpEncoder encoder = new()
507+
{
508+
Quality = 84,
509+
FileFormat = WebpFileFormatType.Lossless
510+
};
511+
512+
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
513+
image.DebugSave(provider);
514+
image.VerifyEncoder(provider, "webp", string.Empty, encoder);
515+
}
516+
500517
public static void RunEncodeLossy_WithPeakImage()
501518
{
502519
TestImageProvider<Rgba32> provider = TestImageProvider<Rgba32>.File(TestImageLossyFullPath);

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ public static class Lossy
823823
public const string Issue2243 = "Webp/issues/Issue2243.webp";
824824
public const string Issue2257 = "Webp/issues/Issue2257.webp";
825825
public const string Issue2670 = "Webp/issues/Issue2670.webp";
826+
public const string Issue2763 = "Webp/issues/Issue2763.png";
826827
}
827828
}
828829

Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)