From 0d6aa322a9f3aa26d608889792353dd3c1a00aaf Mon Sep 17 00:00:00 2001 From: Collin Alpert Date: Mon, 19 Sep 2022 07:11:46 +0200 Subject: [PATCH 1/3] Replace ImageExtensions.Save.tt with Source Generator --- ImageSharp.sln | 7 + .../ImageExtensionsSaveGenerator.cs | 164 ++++ .../ImageSharp.Generators.csproj | 19 + .../Formats/ImageExtensions.Save.cs | 839 ------------------ .../Formats/ImageExtensions.Save.tt | 147 --- src/ImageSharp/ImageSharp.csproj | 13 +- 6 files changed, 194 insertions(+), 995 deletions(-) create mode 100644 src/ImageSharp.Generators/ImageExtensionsSaveGenerator.cs create mode 100644 src/ImageSharp.Generators/ImageSharp.Generators.csproj delete mode 100644 src/ImageSharp/Formats/ImageExtensions.Save.cs delete mode 100644 src/ImageSharp/Formats/ImageExtensions.Save.tt diff --git a/ImageSharp.sln b/ImageSharp.sln index 3ea3160a79..4d60a8edfb 100644 --- a/ImageSharp.sln +++ b/ImageSharp.sln @@ -647,6 +647,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tga", "Tga", "{5DFC394F-136 tests\Images\Input\Tga\targa_8bit_rle.tga = tests\Images\Input\Tga\targa_8bit_rle.tga EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageSharp.Generators", "src\ImageSharp.Generators\ImageSharp.Generators.csproj", "{4743B273-473A-4D20-BE25-EC3D0B38E396}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -669,6 +671,10 @@ Global {FC527290-2F22-432C-B77B-6E815726B02C}.Debug|Any CPU.Build.0 = Debug|Any CPU {FC527290-2F22-432C-B77B-6E815726B02C}.Release|Any CPU.ActiveCfg = Release|Any CPU {FC527290-2F22-432C-B77B-6E815726B02C}.Release|Any CPU.Build.0 = Release|Any CPU + {4743B273-473A-4D20-BE25-EC3D0B38E396}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4743B273-473A-4D20-BE25-EC3D0B38E396}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4743B273-473A-4D20-BE25-EC3D0B38E396}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4743B273-473A-4D20-BE25-EC3D0B38E396}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -699,6 +705,7 @@ Global {FC527290-2F22-432C-B77B-6E815726B02C} = {56801022-D71A-4FBE-BC5B-CBA08E2284EC} {670DD46C-82E9-499A-B2D2-00A802ED0141} = {E1C42A6F-913B-4A7B-B1A8-2BB62843B254} {5DFC394F-136F-4B76-9BCA-3BA786515EFC} = {9DA226A1-8656-49A8-A58A-A8B5C081AD66} + {4743B273-473A-4D20-BE25-EC3D0B38E396} = {815C0625-CD3D-440F-9F80-2D83856AB7AE} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {5F8B9D1F-CD8B-4CC5-8216-D531E25BD795} diff --git a/src/ImageSharp.Generators/ImageExtensionsSaveGenerator.cs b/src/ImageSharp.Generators/ImageExtensionsSaveGenerator.cs new file mode 100644 index 0000000000..4f22c6be86 --- /dev/null +++ b/src/ImageSharp.Generators/ImageExtensionsSaveGenerator.cs @@ -0,0 +1,164 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Text; + +namespace ImageSharp.Generators; + +[Generator(LanguageNames.CSharp)] +internal class ImageExtensionsSaveGenerator : IIncrementalGenerator +{ + private const string FileHeader = @"// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +// "; + + private static readonly string[] ImageFormats = + { + "Bmp", + "Gif", + "Jpeg", + "Pbm", + "Png", + "Tga", + "Webp", + "Tiff" + }; + + public void Initialize(IncrementalGeneratorInitializationContext context) => context.RegisterPostInitializationOutput(GenerateExtensionsMethods); + + private static void GenerateExtensionsMethods(IncrementalGeneratorPostInitializationContext ctx) + { + StringBuilder stringBuilder = new(FileHeader); + stringBuilder.AppendLine(); + stringBuilder.AppendLine("using SixLabors.ImageSharp.Advanced;"); + foreach (string format in ImageFormats) + { + stringBuilder.Append("using SixLabors.ImageSharp.Formats.").Append(format).AppendLine(";"); + } + + stringBuilder.AppendLine(); + stringBuilder.AppendLine("namespace SixLabors.ImageSharp;"); + stringBuilder.AppendLine(); + stringBuilder.AppendLine(@"/// +/// Extension methods for the type. +/// +public static partial class ImageExtensions +{"); + + ctx.CancellationToken.ThrowIfCancellationRequested(); + + foreach (string format in ImageFormats) + { + stringBuilder.AppendLine($@" + /// + /// Saves the image to the given stream with the {format} format. + /// + /// The image this method extends. + /// The file path to save the image to. + /// Thrown if the path is null. + public static void SaveAs{format}(this Image source, string path) => SaveAs{format}(source, path, null); + + /// + /// Saves the image to the given stream with the {format} format. + /// + /// The image this method extends. + /// The file path to save the image to. + /// Thrown if the path is null. + /// A representing the asynchronous operation. + public static Task SaveAs{format}Async(this Image source, string path) => SaveAs{format}Async(source, path, null); + + /// + /// Saves the image to the given stream with the {format} format. + /// + /// The image this method extends. + /// The file path to save the image to. + /// The token to monitor for cancellation requests. + /// Thrown if the path is null. + /// A representing the asynchronous operation. + public static Task SaveAs{format}Async(this Image source, string path, CancellationToken cancellationToken) + => SaveAs{format}Async(source, path, null, cancellationToken); + + /// + /// Saves the image to the given stream with the {format} format. + /// + /// The image this method extends. + /// The file path to save the image to. + /// The encoder to save the image with. + /// Thrown if the path is null. + public static void SaveAs{format}(this Image source, string path, {format}Encoder encoder) => + source.Save( + path, + encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder({format}Format.Instance)); + + /// + /// Saves the image to the given stream with the {format} format. + /// + /// The image this method extends. + /// The file path to save the image to. + /// The encoder to save the image with. + /// The token to monitor for cancellation requests. + /// Thrown if the path is null. + /// A representing the asynchronous operation. + public static Task SaveAs{format}Async(this Image source, string path, {format}Encoder encoder, CancellationToken cancellationToken = default) => + source.SaveAsync( + path, + encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder({format}Format.Instance), + cancellationToken); + + /// + /// Saves the image to the given stream with the {format} format. + /// + /// The image this method extends. + /// The stream to save the image to. + /// Thrown if the stream is null. + public static void SaveAs{format}(this Image source, Stream stream) + => SaveAs{format}(source, stream, null); + + /// + /// Saves the image to the given stream with the {format} format. + /// + /// The image this method extends. + /// The stream to save the image to. + /// The token to monitor for cancellation requests. + /// Thrown if the stream is null. + /// A representing the asynchronous operation. + public static Task SaveAs{format}Async(this Image source, Stream stream, CancellationToken cancellationToken = default) + => SaveAs{format}Async(source, stream, null, cancellationToken); + + /// + /// Saves the image to the given stream with the {format} format. + /// + /// The image this method extends. + /// The stream to save the image to. + /// The encoder to save the image with. + /// Thrown if the stream is null. + public static void SaveAs{format}(this Image source, Stream stream, {format}Encoder encoder) + => source.Save( + stream, + encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder({format}Format.Instance)); + + /// + /// Saves the image to the given stream with the {format} format. + /// + /// The image this method extends. + /// The stream to save the image to. + /// The encoder to save the image with. + /// The token to monitor for cancellation requests. + /// Thrown if the stream is null. + /// A representing the asynchronous operation. + public static Task SaveAs{format}Async(this Image source, Stream stream, {format}Encoder encoder, CancellationToken cancellationToken = default) => + source.SaveAsync( + stream, + encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder({format}Format.Instance), + cancellationToken);"); + } + + stringBuilder.Append("}"); + + SourceText sourceText = SourceText.From(stringBuilder.ToString(), Encoding.UTF8); + ctx.AddSource("ImageExtensions.Save.g.cs", sourceText); + } +} diff --git a/src/ImageSharp.Generators/ImageSharp.Generators.csproj b/src/ImageSharp.Generators/ImageSharp.Generators.csproj new file mode 100644 index 0000000000..a17be03056 --- /dev/null +++ b/src/ImageSharp.Generators/ImageSharp.Generators.csproj @@ -0,0 +1,19 @@ + + + + netstandard2.0 + enable + enable + + 10 + + + + + + + + + + + diff --git a/src/ImageSharp/Formats/ImageExtensions.Save.cs b/src/ImageSharp/Formats/ImageExtensions.Save.cs deleted file mode 100644 index f8763b72fd..0000000000 --- a/src/ImageSharp/Formats/ImageExtensions.Save.cs +++ /dev/null @@ -1,839 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Six Labors Split License. - -// -using SixLabors.ImageSharp.Advanced; - -using SixLabors.ImageSharp.Formats.Bmp; -using SixLabors.ImageSharp.Formats.Gif; -using SixLabors.ImageSharp.Formats.Jpeg; -using SixLabors.ImageSharp.Formats.Pbm; -using SixLabors.ImageSharp.Formats.Png; -using SixLabors.ImageSharp.Formats.Tga; -using SixLabors.ImageSharp.Formats.Webp; -using SixLabors.ImageSharp.Formats.Tiff; - -namespace SixLabors.ImageSharp; - -/// -/// Extension methods for the type. -/// -public static partial class ImageExtensions -{ - /// - /// Saves the image to the given stream with the Bmp format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - public static void SaveAsBmp(this Image source, string path) => SaveAsBmp(source, path, null); - - /// - /// Saves the image to the given stream with the Bmp format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsBmpAsync(this Image source, string path) => SaveAsBmpAsync(source, path, null); - - /// - /// Saves the image to the given stream with the Bmp format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsBmpAsync(this Image source, string path, CancellationToken cancellationToken) - => SaveAsBmpAsync(source, path, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Bmp format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// Thrown if the path is null. - public static void SaveAsBmp(this Image source, string path, BmpEncoder encoder) => - source.Save( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(BmpFormat.Instance)); - - /// - /// Saves the image to the given stream with the Bmp format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsBmpAsync(this Image source, string path, BmpEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(BmpFormat.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the Bmp format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// Thrown if the stream is null. - public static void SaveAsBmp(this Image source, Stream stream) - => SaveAsBmp(source, stream, null); - - /// - /// Saves the image to the given stream with the Bmp format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsBmpAsync(this Image source, Stream stream, CancellationToken cancellationToken = default) - => SaveAsBmpAsync(source, stream, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Bmp format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// Thrown if the stream is null. - public static void SaveAsBmp(this Image source, Stream stream, BmpEncoder encoder) - => source.Save( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(BmpFormat.Instance)); - - /// - /// Saves the image to the given stream with the Bmp format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsBmpAsync(this Image source, Stream stream, BmpEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(BmpFormat.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the Gif format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - public static void SaveAsGif(this Image source, string path) => SaveAsGif(source, path, null); - - /// - /// Saves the image to the given stream with the Gif format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsGifAsync(this Image source, string path) => SaveAsGifAsync(source, path, null); - - /// - /// Saves the image to the given stream with the Gif format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsGifAsync(this Image source, string path, CancellationToken cancellationToken) - => SaveAsGifAsync(source, path, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Gif format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// Thrown if the path is null. - public static void SaveAsGif(this Image source, string path, GifEncoder encoder) => - source.Save( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance)); - - /// - /// Saves the image to the given stream with the Gif format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsGifAsync(this Image source, string path, GifEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the Gif format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// Thrown if the stream is null. - public static void SaveAsGif(this Image source, Stream stream) - => SaveAsGif(source, stream, null); - - /// - /// Saves the image to the given stream with the Gif format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsGifAsync(this Image source, Stream stream, CancellationToken cancellationToken = default) - => SaveAsGifAsync(source, stream, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Gif format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// Thrown if the stream is null. - public static void SaveAsGif(this Image source, Stream stream, GifEncoder encoder) - => source.Save( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance)); - - /// - /// Saves the image to the given stream with the Gif format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsGifAsync(this Image source, Stream stream, GifEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the Jpeg format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - public static void SaveAsJpeg(this Image source, string path) => SaveAsJpeg(source, path, null); - - /// - /// Saves the image to the given stream with the Jpeg format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsJpegAsync(this Image source, string path) => SaveAsJpegAsync(source, path, null); - - /// - /// Saves the image to the given stream with the Jpeg format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsJpegAsync(this Image source, string path, CancellationToken cancellationToken) - => SaveAsJpegAsync(source, path, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Jpeg format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// Thrown if the path is null. - public static void SaveAsJpeg(this Image source, string path, JpegEncoder encoder) => - source.Save( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance)); - - /// - /// Saves the image to the given stream with the Jpeg format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsJpegAsync(this Image source, string path, JpegEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the Jpeg format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// Thrown if the stream is null. - public static void SaveAsJpeg(this Image source, Stream stream) - => SaveAsJpeg(source, stream, null); - - /// - /// Saves the image to the given stream with the Jpeg format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsJpegAsync(this Image source, Stream stream, CancellationToken cancellationToken = default) - => SaveAsJpegAsync(source, stream, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Jpeg format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// Thrown if the stream is null. - public static void SaveAsJpeg(this Image source, Stream stream, JpegEncoder encoder) - => source.Save( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance)); - - /// - /// Saves the image to the given stream with the Jpeg format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsJpegAsync(this Image source, Stream stream, JpegEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the Pbm format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - public static void SaveAsPbm(this Image source, string path) => SaveAsPbm(source, path, null); - - /// - /// Saves the image to the given stream with the Pbm format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsPbmAsync(this Image source, string path) => SaveAsPbmAsync(source, path, null); - - /// - /// Saves the image to the given stream with the Pbm format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsPbmAsync(this Image source, string path, CancellationToken cancellationToken) - => SaveAsPbmAsync(source, path, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Pbm format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// Thrown if the path is null. - public static void SaveAsPbm(this Image source, string path, PbmEncoder encoder) => - source.Save( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PbmFormat.Instance)); - - /// - /// Saves the image to the given stream with the Pbm format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsPbmAsync(this Image source, string path, PbmEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PbmFormat.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the Pbm format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// Thrown if the stream is null. - public static void SaveAsPbm(this Image source, Stream stream) - => SaveAsPbm(source, stream, null); - - /// - /// Saves the image to the given stream with the Pbm format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsPbmAsync(this Image source, Stream stream, CancellationToken cancellationToken = default) - => SaveAsPbmAsync(source, stream, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Pbm format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// Thrown if the stream is null. - public static void SaveAsPbm(this Image source, Stream stream, PbmEncoder encoder) - => source.Save( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PbmFormat.Instance)); - - /// - /// Saves the image to the given stream with the Pbm format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsPbmAsync(this Image source, Stream stream, PbmEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PbmFormat.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the Png format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - public static void SaveAsPng(this Image source, string path) => SaveAsPng(source, path, null); - - /// - /// Saves the image to the given stream with the Png format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsPngAsync(this Image source, string path) => SaveAsPngAsync(source, path, null); - - /// - /// Saves the image to the given stream with the Png format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsPngAsync(this Image source, string path, CancellationToken cancellationToken) - => SaveAsPngAsync(source, path, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Png format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// Thrown if the path is null. - public static void SaveAsPng(this Image source, string path, PngEncoder encoder) => - source.Save( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance)); - - /// - /// Saves the image to the given stream with the Png format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsPngAsync(this Image source, string path, PngEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the Png format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// Thrown if the stream is null. - public static void SaveAsPng(this Image source, Stream stream) - => SaveAsPng(source, stream, null); - - /// - /// Saves the image to the given stream with the Png format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsPngAsync(this Image source, Stream stream, CancellationToken cancellationToken = default) - => SaveAsPngAsync(source, stream, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Png format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// Thrown if the stream is null. - public static void SaveAsPng(this Image source, Stream stream, PngEncoder encoder) - => source.Save( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance)); - - /// - /// Saves the image to the given stream with the Png format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsPngAsync(this Image source, Stream stream, PngEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the Tga format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - public static void SaveAsTga(this Image source, string path) => SaveAsTga(source, path, null); - - /// - /// Saves the image to the given stream with the Tga format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsTgaAsync(this Image source, string path) => SaveAsTgaAsync(source, path, null); - - /// - /// Saves the image to the given stream with the Tga format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsTgaAsync(this Image source, string path, CancellationToken cancellationToken) - => SaveAsTgaAsync(source, path, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Tga format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// Thrown if the path is null. - public static void SaveAsTga(this Image source, string path, TgaEncoder encoder) => - source.Save( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance)); - - /// - /// Saves the image to the given stream with the Tga format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsTgaAsync(this Image source, string path, TgaEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the Tga format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// Thrown if the stream is null. - public static void SaveAsTga(this Image source, Stream stream) - => SaveAsTga(source, stream, null); - - /// - /// Saves the image to the given stream with the Tga format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsTgaAsync(this Image source, Stream stream, CancellationToken cancellationToken = default) - => SaveAsTgaAsync(source, stream, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Tga format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// Thrown if the stream is null. - public static void SaveAsTga(this Image source, Stream stream, TgaEncoder encoder) - => source.Save( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance)); - - /// - /// Saves the image to the given stream with the Tga format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsTgaAsync(this Image source, Stream stream, TgaEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the Webp format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - public static void SaveAsWebp(this Image source, string path) => SaveAsWebp(source, path, null); - - /// - /// Saves the image to the given stream with the Webp format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsWebpAsync(this Image source, string path) => SaveAsWebpAsync(source, path, null); - - /// - /// Saves the image to the given stream with the Webp format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsWebpAsync(this Image source, string path, CancellationToken cancellationToken) - => SaveAsWebpAsync(source, path, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Webp format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// Thrown if the path is null. - public static void SaveAsWebp(this Image source, string path, WebpEncoder encoder) => - source.Save( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(WebpFormat.Instance)); - - /// - /// Saves the image to the given stream with the Webp format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsWebpAsync(this Image source, string path, WebpEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(WebpFormat.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the Webp format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// Thrown if the stream is null. - public static void SaveAsWebp(this Image source, Stream stream) - => SaveAsWebp(source, stream, null); - - /// - /// Saves the image to the given stream with the Webp format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsWebpAsync(this Image source, Stream stream, CancellationToken cancellationToken = default) - => SaveAsWebpAsync(source, stream, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Webp format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// Thrown if the stream is null. - public static void SaveAsWebp(this Image source, Stream stream, WebpEncoder encoder) - => source.Save( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(WebpFormat.Instance)); - - /// - /// Saves the image to the given stream with the Webp format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsWebpAsync(this Image source, Stream stream, WebpEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(WebpFormat.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the Tiff format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - public static void SaveAsTiff(this Image source, string path) => SaveAsTiff(source, path, null); - - /// - /// Saves the image to the given stream with the Tiff format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsTiffAsync(this Image source, string path) => SaveAsTiffAsync(source, path, null); - - /// - /// Saves the image to the given stream with the Tiff format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsTiffAsync(this Image source, string path, CancellationToken cancellationToken) - => SaveAsTiffAsync(source, path, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Tiff format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// Thrown if the path is null. - public static void SaveAsTiff(this Image source, string path, TiffEncoder encoder) => - source.Save( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TiffFormat.Instance)); - - /// - /// Saves the image to the given stream with the Tiff format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAsTiffAsync(this Image source, string path, TiffEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TiffFormat.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the Tiff format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// Thrown if the stream is null. - public static void SaveAsTiff(this Image source, Stream stream) - => SaveAsTiff(source, stream, null); - - /// - /// Saves the image to the given stream with the Tiff format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsTiffAsync(this Image source, Stream stream, CancellationToken cancellationToken = default) - => SaveAsTiffAsync(source, stream, null, cancellationToken); - - /// - /// Saves the image to the given stream with the Tiff format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// Thrown if the stream is null. - public static void SaveAsTiff(this Image source, Stream stream, TiffEncoder encoder) - => source.Save( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TiffFormat.Instance)); - - /// - /// Saves the image to the given stream with the Tiff format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAsTiffAsync(this Image source, Stream stream, TiffEncoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TiffFormat.Instance), - cancellationToken); - -} diff --git a/src/ImageSharp/Formats/ImageExtensions.Save.tt b/src/ImageSharp/Formats/ImageExtensions.Save.tt deleted file mode 100644 index 9ca3a0f223..0000000000 --- a/src/ImageSharp/Formats/ImageExtensions.Save.tt +++ /dev/null @@ -1,147 +0,0 @@ -<#@ template language="C#" #> -<#@ import namespace="System.Text" #> -<#@ import namespace="System.Collections.Generic" #> -// Copyright (c) Six Labors. -// Licensed under the Six Labors Split License. - -// -using SixLabors.ImageSharp.Advanced; - -<# - var formats = new []{ - "Bmp", - "Gif", - "Jpeg", - "Pbm", - "Png", - "Tga", - "Webp", - "Tiff", - }; - - foreach (string fmt in formats) - { -#> -using SixLabors.ImageSharp.Formats.<#= fmt #>; -<# - - } -#> - -namespace SixLabors.ImageSharp; - -/// -/// Extension methods for the type. -/// -public static partial class ImageExtensions -{ -<# - foreach (string fmt in formats) - { -#> - /// - /// Saves the image to the given stream with the <#= fmt #> format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - public static void SaveAs<#= fmt #>(this Image source, string path) => SaveAs<#= fmt #>(source, path, null); - - /// - /// Saves the image to the given stream with the <#= fmt #> format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAs<#= fmt #>Async(this Image source, string path) => SaveAs<#= fmt #>Async(source, path, null); - - /// - /// Saves the image to the given stream with the <#= fmt #> format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAs<#= fmt #>Async(this Image source, string path, CancellationToken cancellationToken) - => SaveAs<#= fmt #>Async(source, path, null, cancellationToken); - - /// - /// Saves the image to the given stream with the <#= fmt #> format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// Thrown if the path is null. - public static void SaveAs<#= fmt #>(this Image source, string path, <#= fmt #>Encoder encoder) => - source.Save( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(<#= fmt #>Format.Instance)); - - /// - /// Saves the image to the given stream with the <#= fmt #> format. - /// - /// The image this method extends. - /// The file path to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the path is null. - /// A representing the asynchronous operation. - public static Task SaveAs<#= fmt #>Async(this Image source, string path, <#= fmt #>Encoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(<#= fmt #>Format.Instance), - cancellationToken); - - /// - /// Saves the image to the given stream with the <#= fmt #> format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// Thrown if the stream is null. - public static void SaveAs<#= fmt #>(this Image source, Stream stream) - => SaveAs<#= fmt #>(source, stream, null); - - /// - /// Saves the image to the given stream with the <#= fmt #> format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAs<#= fmt #>Async(this Image source, Stream stream, CancellationToken cancellationToken = default) - => SaveAs<#= fmt #>Async(source, stream, null, cancellationToken); - - /// - /// Saves the image to the given stream with the <#= fmt #> format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// Thrown if the stream is null. - public static void SaveAs<#= fmt #>(this Image source, Stream stream, <#= fmt #>Encoder encoder) - => source.Save( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(<#= fmt #>Format.Instance)); - - /// - /// Saves the image to the given stream with the <#= fmt #> format. - /// - /// The image this method extends. - /// The stream to save the image to. - /// The encoder to save the image with. - /// The token to monitor for cancellation requests. - /// Thrown if the stream is null. - /// A representing the asynchronous operation. - public static Task SaveAs<#= fmt #>Async(this Image source, Stream stream, <#= fmt #>Encoder encoder, CancellationToken cancellationToken = default) => - source.SaveAsync( - stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(<#= fmt #>Format.Instance), - cancellationToken); - -<# -} -#> -} diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 41ca9b9fa9..1bc73adb1c 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -41,6 +41,10 @@ + + + + True @@ -132,11 +136,6 @@ True PorterDuffFunctions.Generated.tt - - True - True - ImageExtensions.Save.tt - @@ -212,10 +211,6 @@ DefaultPixelBlenders.Generated.cs TextTemplatingFileGenerator - - TextTemplatingFileGenerator - ImageExtensions.Save.cs - From 464090fa5ba384bb758b8c93f599f6194b2c1cce Mon Sep 17 00:00:00 2001 From: Collin Alpert Date: Tue, 20 Sep 2022 11:50:43 +0200 Subject: [PATCH 2/3] Multi target SG. --- src/ImageSharp.Generators/ImageExtensionsSaveGenerator.cs | 7 ++++--- src/ImageSharp.Generators/ImageSharp.Generators.csproj | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp.Generators/ImageExtensionsSaveGenerator.cs b/src/ImageSharp.Generators/ImageExtensionsSaveGenerator.cs index 4f22c6be86..a67d82945f 100644 --- a/src/ImageSharp.Generators/ImageExtensionsSaveGenerator.cs +++ b/src/ImageSharp.Generators/ImageExtensionsSaveGenerator.cs @@ -52,7 +52,7 @@ public static partial class ImageExtensions foreach (string format in ImageFormats) { - stringBuilder.AppendLine($@" + string methods = $@" /// /// Saves the image to the given stream with the {format} format. /// @@ -153,10 +153,11 @@ public static partial class ImageExtensions source.SaveAsync( stream, encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder({format}Format.Instance), - cancellationToken);"); + cancellationToken);"; + stringBuilder.AppendLine(methods); } - stringBuilder.Append("}"); + stringBuilder.Append('}'); SourceText sourceText = SourceText.From(stringBuilder.ToString(), Encoding.UTF8); ctx.AddSource("ImageExtensions.Save.g.cs", sourceText); diff --git a/src/ImageSharp.Generators/ImageSharp.Generators.csproj b/src/ImageSharp.Generators/ImageSharp.Generators.csproj index a17be03056..82d0b946a0 100644 --- a/src/ImageSharp.Generators/ImageSharp.Generators.csproj +++ b/src/ImageSharp.Generators/ImageSharp.Generators.csproj @@ -1,7 +1,7 @@ - netstandard2.0 + netstandard2.0;net6.0 enable enable From 06c144a1bd80d0836de506f8835d2ce01d012d66 Mon Sep 17 00:00:00 2001 From: Collin Alpert Date: Tue, 20 Sep 2022 14:20:59 +0200 Subject: [PATCH 3/3] More multi targeting. --- src/ImageSharp.Generators/ImageSharp.Generators.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp.Generators/ImageSharp.Generators.csproj b/src/ImageSharp.Generators/ImageSharp.Generators.csproj index 82d0b946a0..44529aef8a 100644 --- a/src/ImageSharp.Generators/ImageSharp.Generators.csproj +++ b/src/ImageSharp.Generators/ImageSharp.Generators.csproj @@ -1,7 +1,7 @@ - netstandard2.0;net6.0 + netstandard2.0;net6.0;net7.0 enable enable