22// Licensed under the Apache License, Version 2.0.
33
44using System ;
5- using System . Linq ;
6- using System . Runtime . InteropServices ;
7-
5+ using System . Collections . Generic ;
6+ using System . IO ;
7+ using System . Text ;
8+ using System . Threading . Tasks ;
9+ using SixLabors . ImageSharp . Formats ;
810using SixLabors . ImageSharp . Memory ;
911using SixLabors . ImageSharp . PixelFormats ;
1012
@@ -15,15 +17,66 @@ namespace SixLabors.ImageSharp.Advanced
1517 /// </summary>
1618 public static class AdvancedImageExtensions
1719 {
20+ /// <summary>
21+ /// For a given file path find the best encoder to use via its extension.
22+ /// </summary>
23+ /// <param name="source">The source image.</param>
24+ /// <param name="filePath">The target file path to save the image to.</param>
25+ /// <returns>The matching encoder.</returns>
26+ public static IImageEncoder DetectEncoder ( this Image source , string filePath )
27+ {
28+ Guard . NotNull ( filePath , nameof ( filePath ) ) ;
29+
30+ string ext = Path . GetExtension ( filePath ) ;
31+ IImageFormat format = source . GetConfiguration ( ) . ImageFormatsManager . FindFormatByFileExtension ( ext ) ;
32+ if ( format is null )
33+ {
34+ var sb = new StringBuilder ( ) ;
35+ sb . AppendLine ( $ "No encoder was found for extension '{ ext } '. Registered encoders include:") ;
36+ foreach ( IImageFormat fmt in source . GetConfiguration ( ) . ImageFormats )
37+ {
38+ sb . AppendFormat ( " - {0} : {1}{2}" , fmt . Name , string . Join ( ", " , fmt . FileExtensions ) , Environment . NewLine ) ;
39+ }
40+
41+ throw new NotSupportedException ( sb . ToString ( ) ) ;
42+ }
43+
44+ IImageEncoder encoder = source . GetConfiguration ( ) . ImageFormatsManager . FindEncoder ( format ) ;
45+
46+ if ( encoder is null )
47+ {
48+ var sb = new StringBuilder ( ) ;
49+ sb . AppendLine ( $ "No encoder was found for extension '{ ext } ' using image format '{ format . Name } '. Registered encoders include:") ;
50+ foreach ( KeyValuePair < IImageFormat , IImageEncoder > enc in source . GetConfiguration ( ) . ImageFormatsManager . ImageEncoders )
51+ {
52+ sb . AppendFormat ( " - {0} : {1}{2}" , enc . Key , enc . Value . GetType ( ) . Name , Environment . NewLine ) ;
53+ }
54+
55+ throw new NotSupportedException ( sb . ToString ( ) ) ;
56+ }
57+
58+ return encoder ;
59+ }
60+
1861 /// <summary>
1962 /// Accepts a <see cref="IImageVisitor"/> to implement a double-dispatch pattern in order to
2063 /// apply pixel-specific operations on non-generic <see cref="Image"/> instances
2164 /// </summary>
22- /// <param name="source">The source.</param>
23- /// <param name="visitor">The visitor.</param>
65+ /// <param name="source">The source image .</param>
66+ /// <param name="visitor">The image visitor.</param>
2467 public static void AcceptVisitor ( this Image source , IImageVisitor visitor )
2568 => source . Accept ( visitor ) ;
2669
70+ /// <summary>
71+ /// Accepts a <see cref="IImageVisitor"/> to implement a double-dispatch pattern in order to
72+ /// apply pixel-specific operations on non-generic <see cref="Image"/> instances
73+ /// </summary>
74+ /// <param name="source">The source image.</param>
75+ /// <param name="visitor">The image visitor.</param>
76+ /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
77+ public static Task AcceptVisitorAsync ( this Image source , IImageVisitorAsync visitor )
78+ => source . AcceptAsync ( visitor ) ;
79+
2780 /// <summary>
2881 /// Gets the configuration for the image.
2982 /// </summary>
0 commit comments