11// Copyright (c) Six Labors.
22// Licensed under the Six Labors Split License.
3- #nullable disable
43
54using SixLabors . ImageSharp . IO ;
65using SixLabors . ImageSharp . PixelFormats ;
@@ -17,49 +16,85 @@ public abstract class ImageDecoder : IImageDecoder
1716 /// <inheritdoc/>
1817 public Image < TPixel > Decode < TPixel > ( DecoderOptions options , Stream stream )
1918 where TPixel : unmanaged, IPixel < TPixel >
20- => WithSeekableStream (
21- options ,
22- stream ,
23- s => this . Decode < TPixel > ( options , s , default ) ) ;
19+ {
20+ Image < TPixel > image = WithSeekableStream (
21+ options ,
22+ stream ,
23+ s => this . Decode < TPixel > ( options , s , default ) ) ;
24+
25+ this . SetDecoderFormat ( options . Configuration , image ) ;
26+
27+ return image ;
28+ }
2429
2530 /// <inheritdoc/>
2631 public Image Decode ( DecoderOptions options , Stream stream )
27- => WithSeekableStream (
28- options ,
29- stream ,
30- s => this . Decode ( options , s , default ) ) ;
32+ {
33+ Image image = WithSeekableStream (
34+ options ,
35+ stream ,
36+ s => this . Decode ( options , s , default ) ) ;
37+
38+ this . SetDecoderFormat ( options . Configuration , image ) ;
39+
40+ return image ;
41+ }
3142
3243 /// <inheritdoc/>
33- public Task < Image < TPixel > > DecodeAsync < TPixel > ( DecoderOptions options , Stream stream , CancellationToken cancellationToken = default )
44+ public async Task < Image < TPixel > > DecodeAsync < TPixel > ( DecoderOptions options , Stream stream , CancellationToken cancellationToken = default )
3445 where TPixel : unmanaged, IPixel < TPixel >
35- => WithSeekableMemoryStreamAsync (
36- options ,
37- stream ,
38- ( s , ct ) => this . Decode < TPixel > ( options , s , ct ) ,
39- cancellationToken ) ;
46+ {
47+ Image < TPixel > image = await WithSeekableMemoryStreamAsync (
48+ options ,
49+ stream ,
50+ ( s , ct ) => this . Decode < TPixel > ( options , s , ct ) ,
51+ cancellationToken ) . ConfigureAwait ( false ) ;
52+
53+ this . SetDecoderFormat ( options . Configuration , image ) ;
54+
55+ return image ;
56+ }
4057
4158 /// <inheritdoc/>
42- public Task < Image > DecodeAsync ( DecoderOptions options , Stream stream , CancellationToken cancellationToken = default )
43- => WithSeekableMemoryStreamAsync (
59+ public async Task < Image > DecodeAsync ( DecoderOptions options , Stream stream , CancellationToken cancellationToken = default )
60+ {
61+ Image image = await WithSeekableMemoryStreamAsync (
4462 options ,
4563 stream ,
4664 ( s , ct ) => this . Decode ( options , s , ct ) ,
47- cancellationToken ) ;
65+ cancellationToken ) . ConfigureAwait ( false ) ;
66+
67+ this . SetDecoderFormat ( options . Configuration , image ) ;
68+
69+ return image ;
70+ }
4871
4972 /// <inheritdoc/>
50- public IImageInfo Identify ( DecoderOptions options , Stream stream )
51- => WithSeekableStream (
52- options ,
53- stream ,
54- s => this . Identify ( options , s , default ) ) ;
73+ public ImageInfo Identify ( DecoderOptions options , Stream stream )
74+ {
75+ ImageInfo info = WithSeekableStream (
76+ options ,
77+ stream ,
78+ s => this . Identify ( options , s , default ) ) ;
79+
80+ this . SetDecoderFormat ( options . Configuration , info ) ;
81+
82+ return info ;
83+ }
5584
5685 /// <inheritdoc/>
57- public Task < IImageInfo > IdentifyAsync ( DecoderOptions options , Stream stream , CancellationToken cancellationToken = default )
58- => WithSeekableMemoryStreamAsync (
59- options ,
60- stream ,
61- ( s , ct ) => this . Identify ( options , s , ct ) ,
62- cancellationToken ) ;
86+ public async Task < ImageInfo > IdentifyAsync ( DecoderOptions options , Stream stream , CancellationToken cancellationToken = default )
87+ {
88+ ImageInfo info = await WithSeekableMemoryStreamAsync (
89+ options ,
90+ stream ,
91+ ( s , ct ) => this . Identify ( options , s , ct ) ,
92+ cancellationToken ) . ConfigureAwait ( false ) ;
93+
94+ this . SetDecoderFormat ( options . Configuration , info ) ;
95+
96+ return info ;
97+ }
6398
6499 /// <summary>
65100 /// Decodes the image from the specified stream to an <see cref="Image{TPixel}" /> of a specific pixel type.
@@ -98,9 +133,9 @@ protected abstract Image<TPixel> Decode<TPixel>(DecoderOptions options, Stream s
98133 /// <param name="options">The general decoder options.</param>
99134 /// <param name="stream">The <see cref="Stream"/> containing image data.</param>
100135 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
101- /// <returns>The <see cref="IImageInfo "/> object.</returns>
136+ /// <returns>The <see cref="ImageInfo "/> object.</returns>
102137 /// <exception cref="ImageFormatException">Thrown if the encoded image contains errors.</exception>
103- protected abstract IImageInfo Identify ( DecoderOptions options , Stream stream , CancellationToken cancellationToken ) ;
138+ protected abstract ImageInfo Identify ( DecoderOptions options , Stream stream , CancellationToken cancellationToken ) ;
104139
105140 /// <summary>
106141 /// Performs a scaling operation against the decoded image. If the target size is not set, or the image size
@@ -114,7 +149,7 @@ protected static void ScaleToTargetSize(DecoderOptions options, Image image)
114149 {
115150 ResizeOptions resizeOptions = new ( )
116151 {
117- Size = options . TargetSize . Value ,
152+ Size = options . TargetSize ! . Value ,
118153 Sampler = options . Sampler ,
119154 Mode = ResizeMode . Max
120155 } ;
@@ -137,7 +172,7 @@ private static bool ShouldResize(DecoderOptions options, Image image)
137172 }
138173
139174 Size targetSize = options . TargetSize . Value ;
140- Size currentSize = image . Size ( ) ;
175+ Size currentSize = image . Size ;
141176 return currentSize . Width != targetSize . Width && currentSize . Height != targetSize . Height ;
142177 }
143178
@@ -252,4 +287,20 @@ private static async Task<T> CopyToMemoryStreamAndActionAsync<T>(
252287 memoryStream . Position = 0 ;
253288 return await action ( memoryStream , position , cancellationToken ) . ConfigureAwait ( false ) ;
254289 }
290+
291+ internal void SetDecoderFormat ( Configuration configuration , Image image )
292+ {
293+ if ( configuration . ImageFormatsManager . TryFindFormatByDecoder ( this , out IImageFormat ? format ) )
294+ {
295+ image . Metadata . DecodedImageFormat = format ;
296+ }
297+ }
298+
299+ internal void SetDecoderFormat ( Configuration configuration , ImageInfo info )
300+ {
301+ if ( configuration . ImageFormatsManager . TryFindFormatByDecoder ( this , out IImageFormat ? format ) )
302+ {
303+ info . Metadata . DecodedImageFormat = format ;
304+ }
305+ }
255306}
0 commit comments