Skip to content

Commit

Permalink
Merge pull request #2768 from SixLabors/backport/v3-fix-2763
Browse files Browse the repository at this point in the history
v3.1.x - Backport Webp: Fix Issue 2763/2692
  • Loading branch information
JimBobSquarePants authored Jul 10, 2024
2 parents b496109 + 59298c8 commit 1d4a287
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 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 @@ -699,6 +699,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 @@ -714,7 +716,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 @@ -729,7 +731,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
4 changes: 0 additions & 4 deletions src/ImageSharp/Formats/Webp/WebpChunkParsingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,6 @@ public static WebpImageInfo ReadVp8XHeader(BufferedReadStream stream, Span<byte>

// 3 reserved bytes should follow which are supposed to be zero.
stream.Read(buffer, 0, 3);
if (buffer[0] != 0 || buffer[1] != 0 || buffer[2] != 0)
{
WebpThrowHelper.ThrowImageFormatException("reserved bytes should be zero");
}

// 3 bytes for the width.
uint width = ReadUInt24LittleEndian(stream, buffer) + 1;
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 @@ -820,6 +820,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 1d4a287

Please sign in to comment.