Skip to content

Commit b4e1b7f

Browse files
committed
fix
1 parent b89bc54 commit b4e1b7f

File tree

7 files changed

+188
-189
lines changed

7 files changed

+188
-189
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ public static long WriteAnimationFrame(Stream stream, WebpFrameData animation)
263263
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, animation.Height - 1);
264264
WebpChunkParsingUtils.WriteUInt24LittleEndian(stream, animation.Duration);
265265

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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,12 @@ public void Encode<TPixel>(ImageFrame<TPixel> frame, Stream stream, bool hasAnim
306306
if (hasAnimation)
307307
{
308308
WebpFrameMetadata frameMetadata = frame.Metadata.GetWebpMetadata();
309+
310+
// TODO: If we can clip the indexed frame for transparent bounds we can set properties here.
309311
prevPosition = BitWriterBase.WriteAnimationFrame(stream, new WebpFrameData
310312
{
313+
X = 0,
314+
Y = 0,
311315
Width = (uint)frame.Width,
312316
Height = (uint)frame.Height,
313317
Duration = frameMetadata.FrameDelay,

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

Lines changed: 59 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,69 @@ 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
}
762-
}
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
778-
{
779-
WebpThrowHelper.ThrowImageFormatException("error while decoding alpha data");
760+
761+
break;
780762
}
781763

782-
pos += length;
783-
col += length;
784-
while (col >= width)
764+
case < lenCodeLimit:
785765
{
786-
col -= width;
787-
++row;
788-
if (row <= lastRow && row % WebpConstants.NumArgbCacheRows == 0)
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)
789774
{
790-
dec.ExtractPalettedAlphaRows(row);
775+
CopyBlock8B(data, pos, dist, length);
776+
}
777+
else
778+
{
779+
WebpThrowHelper.ThrowImageFormatException("error while decoding alpha data");
791780
}
792-
}
793781

794-
if (pos < last && (col & mask) > 0)
795-
{
796-
htreeGroup = GetHTreeGroupForPos(hdr, col, row);
782+
pos += length;
783+
col += length;
784+
while (col >= width)
785+
{
786+
col -= width;
787+
++row;
788+
if (row <= lastRow && row % WebpConstants.NumArgbCacheRows == 0)
789+
{
790+
dec.ExtractPalettedAlphaRows(row);
791+
}
792+
}
793+
794+
if (pos < last && (col & mask) > 0)
795+
{
796+
htreeGroup = GetHTreeGroupForPos(hdr, col, row);
797+
}
798+
799+
break;
797800
}
798-
}
799-
else
800-
{
801-
WebpThrowHelper.ThrowImageFormatException("bitstream error while parsing alpha data");
801+
802+
default:
803+
WebpThrowHelper.ThrowImageFormatException("bitstream error while parsing alpha data");
804+
break;
802805
}
803806

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

src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,12 @@ private void Encode<TPixel>(ImageFrame<TPixel> frame, Stream stream, bool hasAni
476476
if (hasAnimation)
477477
{
478478
WebpFrameMetadata frameMetadata = frame.Metadata.GetWebpMetadata();
479+
480+
// TODO: If we can clip the indexed frame for transparent bounds we can set properties here.
479481
prevPosition = BitWriterBase.WriteAnimationFrame(stream, new WebpFrameData
480482
{
483+
X = 0,
484+
Y = 0,
481485
Width = (uint)frame.Width,
482486
Height = (uint)frame.Height,
483487
Duration = frameMetadata.FrameDelay,

0 commit comments

Comments
 (0)