22// Licensed under the Apache License, Version 2.0.
33
44using System . IO ;
5+ using System . Threading ;
56using System . Threading . Tasks ;
67using SixLabors . ImageSharp . IO ;
78using SixLabors . ImageSharp . Memory ;
@@ -30,52 +31,25 @@ public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
3031 where TPixel : unmanaged, IPixel < TPixel >
3132 {
3233 var decoder = new GifDecoderCore ( configuration , this ) ;
33-
34- try
35- {
36- using var bufferedStream = new BufferedReadStream ( configuration , stream ) ;
37- return decoder . Decode < TPixel > ( bufferedStream ) ;
38- }
39- catch ( InvalidMemoryOperationException ex )
40- {
41- Size dims = decoder . Dimensions ;
42-
43- GifThrowHelper . ThrowInvalidImageContentException ( $ "Cannot decode image. Failed to allocate buffers for possibly degenerate dimensions: { dims . Width } x{ dims . Height } .", ex ) ;
44-
45- // Not reachable, as the previous statement will throw a exception.
46- return null ;
47- }
34+ return decoder . Decode < TPixel > ( configuration , stream ) ;
4835 }
4936
5037 /// <inheritdoc />
5138 public Image Decode ( Configuration configuration , Stream stream )
5239 => this . Decode < Rgba32 > ( configuration , stream ) ;
5340
5441 /// <inheritdoc/>
55- public async Task < Image < TPixel > > DecodeAsync < TPixel > ( Configuration configuration , Stream stream )
42+ public Task < Image < TPixel > > DecodeAsync < TPixel > ( Configuration configuration , Stream stream , CancellationToken cancellationToken )
5643 where TPixel : unmanaged, IPixel < TPixel >
5744 {
5845 var decoder = new GifDecoderCore ( configuration , this ) ;
59-
60- try
61- {
62- using var bufferedStream = new BufferedReadStream ( configuration , stream ) ;
63- return await decoder . DecodeAsync < TPixel > ( bufferedStream ) . ConfigureAwait ( false ) ;
64- }
65- catch ( InvalidMemoryOperationException ex )
66- {
67- Size dims = decoder . Dimensions ;
68-
69- GifThrowHelper . ThrowInvalidImageContentException ( $ "Cannot decode image. Failed to allocate buffers for possibly degenerate dimensions: { dims . Width } x{ dims . Height } .", ex ) ;
70-
71- // Not reachable, as the previous statement will throw a exception.
72- return null ;
73- }
46+ return decoder . DecodeAsync < TPixel > ( configuration , stream , cancellationToken ) ;
7447 }
7548
7649 /// <inheritdoc />
77- public async Task < Image > DecodeAsync ( Configuration configuration , Stream stream )
78- => await this . DecodeAsync < Rgba32 > ( configuration , stream ) . ConfigureAwait ( false ) ;
50+ public async Task < Image > DecodeAsync ( Configuration configuration , Stream stream , CancellationToken cancellationToken )
51+ => await this . DecodeAsync < Rgba32 > ( configuration , stream , cancellationToken )
52+ . ConfigureAwait ( false ) ;
7953
8054 /// <inheritdoc/>
8155 public IImageInfo Identify ( Configuration configuration , Stream stream )
@@ -85,18 +59,16 @@ public IImageInfo Identify(Configuration configuration, Stream stream)
8559 var decoder = new GifDecoderCore ( configuration , this ) ;
8660
8761 using var bufferedStream = new BufferedReadStream ( configuration , stream ) ;
88- return decoder . Identify ( bufferedStream ) ;
62+ return decoder . Identify ( bufferedStream , default ) ;
8963 }
9064
9165 /// <inheritdoc/>
92- public Task < IImageInfo > IdentifyAsync ( Configuration configuration , Stream stream )
66+ public Task < IImageInfo > IdentifyAsync ( Configuration configuration , Stream stream , CancellationToken cancellationToken )
9367 {
9468 Guard . NotNull ( stream , nameof ( stream ) ) ;
9569
9670 var decoder = new GifDecoderCore ( configuration , this ) ;
97-
98- using var bufferedStream = new BufferedReadStream ( configuration , stream ) ;
99- return decoder . IdentifyAsync ( bufferedStream ) ;
71+ return decoder . IdentifyAsync ( configuration , stream , cancellationToken ) ;
10072 }
10173 }
10274}
0 commit comments