Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3.1.x - Backport Webp: Fix Issue 2763/2692 #2768

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading