Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Poker-sang committed Nov 1, 2023
1 parent b89bc54 commit 3940f02
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 190 deletions.
7 changes: 4 additions & 3 deletions src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,14 @@ public static long WriteAnimationFrame(Stream stream, WebpFrameData animation)
long position = stream.Position;
BinaryPrimitives.WriteUInt32BigEndian(buf, 0);
stream.Write(buf);
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, animation.X);
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, animation.Y);

// TODO: If we can clip the indexed frame for transparent bounds we can set properties here.
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, 0);
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, 0);
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, animation.Width - 1);
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, animation.Height - 1);
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, animation.Duration);

// TODO: If we can clip the indexed frame for transparent bounds we can set properties here.
byte flag = (byte)(((int)animation.BlendingMethod << 1) | (int)animation.DisposalMethod);
stream.WriteByte(flag);
return position;
Expand Down
113 changes: 57 additions & 56 deletions src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,10 @@ public WebpLosslessDecoder(Vp8LBitReader bitReader, MemoryAllocator memoryAlloca
public void Decode<TPixel>(Buffer2D<TPixel> pixels, int width, int height)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Vp8LDecoder decoder = new Vp8LDecoder(width, height, this.memoryAllocator))
{
this.DecodeImageStream(decoder, width, height, true);
this.DecodeImageData(decoder, decoder.Pixels.Memory.Span);
this.DecodePixelValues(decoder, pixels, width, height);
}
using Vp8LDecoder decoder = new(width, height, this.memoryAllocator);
this.DecodeImageStream(decoder, width, height, true);
this.DecodeImageData(decoder, decoder.Pixels.Memory.Span);
this.DecodePixelValues(decoder, pixels, width, height);
}

public IMemoryOwner<uint> DecodeImageStream(Vp8LDecoder decoder, int xSize, int ySize, bool isLevel0)
Expand Down Expand Up @@ -616,15 +614,12 @@ private void ReadHuffmanCodeLengths(Span<HuffmanCode> table, int[] codeLengthCod
private void ReadTransformation(int xSize, int ySize, Vp8LDecoder decoder)
{
Vp8LTransformType transformType = (Vp8LTransformType)this.bitReader.ReadValue(2);
Vp8LTransform transform = new Vp8LTransform(transformType, xSize, ySize);
Vp8LTransform transform = new(transformType, xSize, ySize);

// Each transform is allowed to be used only once.
foreach (Vp8LTransform decoderTransform in decoder.Transforms)
if (decoder.Transforms.Any(decoderTransform => decoderTransform.TransformType == transform.TransformType))
{
if (decoderTransform.TransformType == transform.TransformType)
{
WebpThrowHelper.ThrowImageFormatException("Each transform can only be present once");
}
WebpThrowHelper.ThrowImageFormatException("Each transform can only be present once");
}

switch (transformType)
Expand Down Expand Up @@ -744,61 +739,67 @@ public void DecodeAlphaData(AlphaDecoder dec)

this.bitReader.FillBitWindow();
int code = (int)this.ReadSymbol(htreeGroup[0].HTrees[HuffIndex.Green]);
if (code < WebpConstants.NumLiteralCodes)
switch (code)
{
// Literal
data[pos] = (byte)code;
++pos;
++col;

if (col >= width)
case < WebpConstants.NumLiteralCodes:
{
col = 0;
++row;
if (row <= lastRow && row % WebpConstants.NumArgbCacheRows == 0)
// Literal
data[pos] = (byte)code;
++pos;
++col;

if (col >= width)
{
dec.ExtractPalettedAlphaRows(row);
col = 0;
++row;
if (row <= lastRow && row % WebpConstants.NumArgbCacheRows == 0)
{
dec.ExtractPalettedAlphaRows(row);
}
}

break;
}

Check failure on line 762 in src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs

View workflow job for this annotation

GitHub Actions / Build (false, ubuntu-latest, net6.0, 6.0.x, -x64, false)

Check failure on line 762 in src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs

View workflow job for this annotation

GitHub Actions / Build (false, ubuntu-latest, net6.0, 6.0.x, -x64, false)

}
else if (code < lenCodeLimit)
{
// Backward reference
int lengthSym = code - WebpConstants.NumLiteralCodes;
int length = this.GetCopyLength(lengthSym);
int distSymbol = (int)this.ReadSymbol(htreeGroup[0].HTrees[HuffIndex.Dist]);
this.bitReader.FillBitWindow();
int distCode = this.GetCopyDistance(distSymbol);
int dist = PlaneCodeToDistance(width, distCode);
if (pos >= dist && end - pos >= length)
{
CopyBlock8B(data, pos, dist, length);
}
else
case < lenCodeLimit:
{
WebpThrowHelper.ThrowImageFormatException("error while decoding alpha data");
}
// Backward reference
int lengthSym = code - WebpConstants.NumLiteralCodes;
int length = this.GetCopyLength(lengthSym);
int distSymbol = (int)this.ReadSymbol(htreeGroup[0].HTrees[HuffIndex.Dist]);
this.bitReader.FillBitWindow();
int distCode = this.GetCopyDistance(distSymbol);
int dist = PlaneCodeToDistance(width, distCode);
if (pos >= dist && end - pos >= length)
{
CopyBlock8B(data, pos, dist, length);
}
else
{
WebpThrowHelper.ThrowImageFormatException("error while decoding alpha data");
}

pos += length;
col += length;
while (col >= width)
{
col -= width;
++row;
if (row <= lastRow && row % WebpConstants.NumArgbCacheRows == 0)
pos += length;
col += length;
while (col >= width)
{
dec.ExtractPalettedAlphaRows(row);
col -= width;
++row;
if (row <= lastRow && row % WebpConstants.NumArgbCacheRows == 0)
{
dec.ExtractPalettedAlphaRows(row);
}
}
}

if (pos < last && (col & mask) > 0)
{
htreeGroup = GetHTreeGroupForPos(hdr, col, row);
if (pos < last && (col & mask) > 0)
{
htreeGroup = GetHTreeGroupForPos(hdr, col, row);
}

break;
}

Check failure on line 799 in src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs

View workflow job for this annotation

GitHub Actions / Build (false, ubuntu-latest, net6.0, 6.0.x, -x64, false)

Check failure on line 799 in src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs

View workflow job for this annotation

GitHub Actions / Build (false, ubuntu-latest, net6.0, 6.0.x, -x64, false)

}
else
{
WebpThrowHelper.ThrowImageFormatException("bitstream error while parsing alpha data");
default:
WebpThrowHelper.ThrowImageFormatException("bitstream error while parsing alpha data");
break;
}

this.bitReader.Eos = this.bitReader.IsEndOfStream();
Expand Down
Loading

0 comments on commit 3940f02

Please sign in to comment.