@@ -95,12 +95,10 @@ public WebpLosslessDecoder(Vp8LBitReader bitReader, MemoryAllocator memoryAlloca
95
95
public void Decode < TPixel > ( Buffer2D < TPixel > pixels , int width , int height )
96
96
where TPixel : unmanaged, IPixel < TPixel >
97
97
{
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 ) ;
104
102
}
105
103
106
104
public IMemoryOwner < uint > DecodeImageStream ( Vp8LDecoder decoder , int xSize , int ySize , bool isLevel0 )
@@ -616,15 +614,12 @@ private void ReadHuffmanCodeLengths(Span<HuffmanCode> table, int[] codeLengthCod
616
614
private void ReadTransformation ( int xSize , int ySize , Vp8LDecoder decoder )
617
615
{
618
616
Vp8LTransformType transformType = ( Vp8LTransformType ) this . bitReader . ReadValue ( 2 ) ;
619
- Vp8LTransform transform = new Vp8LTransform ( transformType , xSize , ySize ) ;
617
+ Vp8LTransform transform = new ( transformType , xSize , ySize ) ;
620
618
621
619
// 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 ) )
623
621
{
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" ) ;
628
623
}
629
624
630
625
switch ( transformType )
@@ -744,61 +739,69 @@ public void DecodeAlphaData(AlphaDecoder dec)
744
739
745
740
this . bitReader . FillBitWindow ( ) ;
746
741
int code = ( int ) this . ReadSymbol ( htreeGroup [ 0 ] . HTrees [ HuffIndex . Green ] ) ;
747
- if ( code < WebpConstants . NumLiteralCodes )
742
+ switch ( code )
748
743
{
749
- // Literal
750
- data [ pos ] = ( byte ) code ;
751
- ++ pos ;
752
- ++ col ;
753
-
754
- if ( col >= width )
744
+ case < WebpConstants . NumLiteralCodes :
755
745
{
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 )
759
752
{
760
- dec . ExtractPalettedAlphaRows ( row ) ;
753
+ col = 0 ;
754
+ ++ row ;
755
+ if ( row <= lastRow && row % WebpConstants . NumArgbCacheRows == 0 )
756
+ {
757
+ dec . ExtractPalettedAlphaRows ( row ) ;
758
+ }
761
759
}
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 ;
780
762
}
781
763
782
- pos += length ;
783
- col += length ;
784
- while ( col >= width )
764
+ case < lenCodeLimit :
785
765
{
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 )
789
774
{
790
- dec . ExtractPalettedAlphaRows ( row ) ;
775
+ CopyBlock8B ( data , pos , dist , length ) ;
776
+ }
777
+ else
778
+ {
779
+ WebpThrowHelper . ThrowImageFormatException ( "error while decoding alpha data" ) ;
791
780
}
792
- }
793
781
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 ;
797
800
}
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 ;
802
805
}
803
806
804
807
this . bitReader . Eos = this . bitReader . IsEndOfStream ( ) ;
0 commit comments