@@ -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 ( ) ;
0 commit comments