@@ -82,38 +82,48 @@ public WebpDecoderCore(Configuration configuration, IWebpDecoderOptions options)
8282 public Image < TPixel > Decode < TPixel > ( BufferedReadStream stream , CancellationToken cancellationToken )
8383 where TPixel : unmanaged, IPixel < TPixel >
8484 {
85- this . Metadata = new ImageMetadata ( ) ;
86- this . currentStream = stream ;
85+ Image < TPixel > image = null ;
86+ try
87+ {
8788
88- uint fileSize = this . ReadImageHeader ( ) ;
89+ this . Metadata = new ImageMetadata ( ) ;
90+ this . currentStream = stream ;
8991
90- using ( this . webImageInfo = this . ReadVp8Info ( ) )
91- {
92- if ( this . webImageInfo . Features is { Animation : true } )
93- {
94- WebpThrowHelper . ThrowNotSupportedException ( "Animations are not supported" ) ;
95- }
92+ uint fileSize = this . ReadImageHeader ( ) ;
9693
97- var image = new Image < TPixel > ( this . Configuration , ( int ) this . webImageInfo . Width , ( int ) this . webImageInfo . Height , this . Metadata ) ;
98- Buffer2D < TPixel > pixels = image . GetRootFramePixelBuffer ( ) ;
99- if ( this . webImageInfo . IsLossless )
94+ using ( this . webImageInfo = this . ReadVp8Info ( ) )
10095 {
101- var losslessDecoder = new WebpLosslessDecoder ( this . webImageInfo . Vp8LBitReader , this . memoryAllocator , this . Configuration ) ;
102- losslessDecoder . Decode ( pixels , image . Width , image . Height ) ;
103- }
104- else
105- {
106- var lossyDecoder = new WebpLossyDecoder ( this . webImageInfo . Vp8BitReader , this . memoryAllocator , this . Configuration ) ;
107- lossyDecoder . Decode ( pixels , image . Width , image . Height , this . webImageInfo ) ;
108- }
96+ if ( this . webImageInfo . Features is { Animation : true } )
97+ {
98+ WebpThrowHelper . ThrowNotSupportedException ( "Animations are not supported" ) ;
99+ }
109100
110- // There can be optional chunks after the image data, like EXIF and XMP.
111- if ( this . webImageInfo . Features != null )
112- {
113- this . ParseOptionalChunks ( this . webImageInfo . Features ) ;
114- }
101+ image = new Image < TPixel > ( this . Configuration , ( int ) this . webImageInfo . Width , ( int ) this . webImageInfo . Height , this . Metadata ) ;
102+ Buffer2D < TPixel > pixels = image . GetRootFramePixelBuffer ( ) ;
103+ if ( this . webImageInfo . IsLossless )
104+ {
105+ var losslessDecoder = new WebpLosslessDecoder ( this . webImageInfo . Vp8LBitReader , this . memoryAllocator , this . Configuration ) ;
106+ losslessDecoder . Decode ( pixels , image . Width , image . Height ) ;
107+ }
108+ else
109+ {
110+ var lossyDecoder = new WebpLossyDecoder ( this . webImageInfo . Vp8BitReader , this . memoryAllocator , this . Configuration ) ;
111+ lossyDecoder . Decode ( pixels , image . Width , image . Height , this . webImageInfo ) ;
112+ }
115113
116- return image ;
114+ // There can be optional chunks after the image data, like EXIF and XMP.
115+ if ( this . webImageInfo . Features != null )
116+ {
117+ this . ParseOptionalChunks ( this . webImageInfo . Features ) ;
118+ }
119+
120+ return image ;
121+ }
122+ }
123+ catch
124+ {
125+ image ? . Dispose ( ) ;
126+ throw ;
117127 }
118128 }
119129
0 commit comments