Skip to content

Commit 449dba3

Browse files
Backport fix.
1 parent b496109 commit 449dba3

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

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

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

702+
histogramImageSize = maxIndex;
703+
702704
this.bitWriter.PutBits((uint)(this.HistoBits - 2), 3);
703705
this.EncodeImageNoHuffman(
704706
histogramBgra,
@@ -714,7 +716,7 @@ private void EncodeImage(int width, int height, bool useCache, CrunchConfig conf
714716
// Store Huffman codes.
715717
// Find maximum number of symbols for the huffman tree-set.
716718
int maxTokens = 0;
717-
for (int i = 0; i < 5 * histogramImage.Count; i++)
719+
for (int i = 0; i < 5 * histogramImageSize; i++)
718720
{
719721
HuffmanTreeCode codes = huffmanCodes[i];
720722
if (maxTokens < codes.NumSymbols)
@@ -729,7 +731,7 @@ private void EncodeImage(int width, int height, bool useCache, CrunchConfig conf
729731
tokens[i] = new HuffmanTreeToken();
730732
}
731733

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

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,25 @@ 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.SaveAsWebp(@"C:\Users\james\Downloads\2763-fixed.webp", encoder);
514+
515+
image.DebugSave(provider);
516+
image.VerifyEncoder(provider, "webp", string.Empty, encoder);
517+
}
518+
500519
public static void RunEncodeLossy_WithPeakImage()
501520
{
502521
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
@@ -820,6 +820,7 @@ public static class Lossy
820820
public const string Issue2243 = "Webp/issues/Issue2243.webp";
821821
public const string Issue2257 = "Webp/issues/Issue2257.webp";
822822
public const string Issue2670 = "Webp/issues/Issue2670.webp";
823+
public const string Issue2763 = "Webp/issues/Issue2763.png";
823824
}
824825
}
825826

Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)