Skip to content

Commit 3940f02

Browse files
committed
fix
1 parent b89bc54 commit 3940f02

File tree

5 files changed

+181
-190
lines changed

5 files changed

+181
-190
lines changed

src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,14 @@ public static long WriteAnimationFrame(Stream stream, WebpFrameData animation)
257257
long position = stream.Position;
258258
BinaryPrimitives.WriteUInt32BigEndian(buf, 0);
259259
stream.Write(buf);
260-
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, animation.X);
261-
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, animation.Y);
260+
261+
// TODO: If we can clip the indexed frame for transparent bounds we can set properties here.
262+
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, 0);
263+
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, 0);
262264
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, animation.Width - 1);
263265
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, animation.Height - 1);
264266
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, animation.Duration);
265267

266-
// TODO: If we can clip the indexed frame for transparent bounds we can set properties here.
267268
byte flag = (byte)(((int)animation.BlendingMethod << 1) | (int)animation.DisposalMethod);
268269
stream.WriteByte(flag);
269270
return position;

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

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,10 @@ public WebpLosslessDecoder(Vp8LBitReader bitReader, MemoryAllocator memoryAlloca
9595
public void Decode<TPixel>(Buffer2D<TPixel> pixels, int width, int height)
9696
where TPixel : unmanaged, IPixel<TPixel>
9797
{
98-
using (Vp8LDecoder decoder = new Vp8LDecoder(width, height, this.memoryAllocator))
99-
{
100-
this.DecodeImageStream(decoder, width, height, true);
101-
this.DecodeImageData(decoder, decoder.Pixels.Memory.Span);
102-
this.DecodePixelValues(decoder, pixels, width, height);
103-
}
98+
using Vp8LDecoder decoder = new(width, height, this.memoryAllocator);
99+
this.DecodeImageStream(decoder, width, height, true);
100+
this.DecodeImageData(decoder, decoder.Pixels.Memory.Span);
101+
this.DecodePixelValues(decoder, pixels, width, height);
104102
}
105103

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

621619
// Each transform is allowed to be used only once.
622-
foreach (Vp8LTransform decoderTransform in decoder.Transforms)
620+
if (decoder.Transforms.Any(decoderTransform => decoderTransform.TransformType == transform.TransformType))
623621
{
624-
if (decoderTransform.TransformType == transform.TransformType)
625-
{
626-
WebpThrowHelper.ThrowImageFormatException("Each transform can only be present once");
627-
}
622+
WebpThrowHelper.ThrowImageFormatException("Each transform can only be present once");
628623
}
629624

630625
switch (transformType)
@@ -744,61 +739,67 @@ public void DecodeAlphaData(AlphaDecoder dec)
744739

745740
this.bitReader.FillBitWindow();
746741
int code = (int)this.ReadSymbol(htreeGroup[0].HTrees[HuffIndex.Green]);
747-
if (code < WebpConstants.NumLiteralCodes)
742+
switch (code)
748743
{
749-
// Literal
750-
data[pos] = (byte)code;
751-
++pos;
752-
++col;
753-
754-
if (col >= width)
744+
case < WebpConstants.NumLiteralCodes:
755745
{
756-
col = 0;
757-
++row;
758-
if (row <= lastRow && row % WebpConstants.NumArgbCacheRows == 0)
746+
// Literal
747+
data[pos] = (byte)code;
748+
++pos;
749+
++col;
750+
751+
if (col >= width)
759752
{
760-
dec.ExtractPalettedAlphaRows(row);
753+
col = 0;
754+
++row;
755+
if (row <= lastRow && row % WebpConstants.NumArgbCacheRows == 0)
756+
{
757+
dec.ExtractPalettedAlphaRows(row);
758+
}
761759
}
760+
761+
break;
762762
}
763-
}
764-
else if (code < lenCodeLimit)
765-
{
766-
// Backward reference
767-
int lengthSym = code - WebpConstants.NumLiteralCodes;
768-
int length = this.GetCopyLength(lengthSym);
769-
int distSymbol = (int)this.ReadSymbol(htreeGroup[0].HTrees[HuffIndex.Dist]);
770-
this.bitReader.FillBitWindow();
771-
int distCode = this.GetCopyDistance(distSymbol);
772-
int dist = PlaneCodeToDistance(width, distCode);
773-
if (pos >= dist && end - pos >= length)
774-
{
775-
CopyBlock8B(data, pos, dist, length);
776-
}
777-
else
763+
case < lenCodeLimit:
778764
{
779-
WebpThrowHelper.ThrowImageFormatException("error while decoding alpha data");
780-
}
765+
// Backward reference
766+
int lengthSym = code - WebpConstants.NumLiteralCodes;
767+
int length = this.GetCopyLength(lengthSym);
768+
int distSymbol = (int)this.ReadSymbol(htreeGroup[0].HTrees[HuffIndex.Dist]);
769+
this.bitReader.FillBitWindow();
770+
int distCode = this.GetCopyDistance(distSymbol);
771+
int dist = PlaneCodeToDistance(width, distCode);
772+
if (pos >= dist && end - pos >= length)
773+
{
774+
CopyBlock8B(data, pos, dist, length);
775+
}
776+
else
777+
{
778+
WebpThrowHelper.ThrowImageFormatException("error while decoding alpha data");
779+
}
781780

782-
pos += length;
783-
col += length;
784-
while (col >= width)
785-
{
786-
col -= width;
787-
++row;
788-
if (row <= lastRow && row % WebpConstants.NumArgbCacheRows == 0)
781+
pos += length;
782+
col += length;
783+
while (col >= width)
789784
{
790-
dec.ExtractPalettedAlphaRows(row);
785+
col -= width;
786+
++row;
787+
if (row <= lastRow && row % WebpConstants.NumArgbCacheRows == 0)
788+
{
789+
dec.ExtractPalettedAlphaRows(row);
790+
}
791791
}
792-
}
793792

794-
if (pos < last && (col & mask) > 0)
795-
{
796-
htreeGroup = GetHTreeGroupForPos(hdr, col, row);
793+
if (pos < last && (col & mask) > 0)
794+
{
795+
htreeGroup = GetHTreeGroupForPos(hdr, col, row);
796+
}
797+
798+
break;
797799
}
798-
}
799-
else
800-
{
801-
WebpThrowHelper.ThrowImageFormatException("bitstream error while parsing alpha data");
800+
default:
801+
WebpThrowHelper.ThrowImageFormatException("bitstream error while parsing alpha data");
802+
break;
802803
}
803804

804805
this.bitReader.Eos = this.bitReader.IsEndOfStream();

0 commit comments

Comments
 (0)