From b0f64a341c43ed93fc8374a2dccd5c145dd19dcc Mon Sep 17 00:00:00 2001 From: pekspro Date: Sat, 4 Jul 2020 17:23:12 +0200 Subject: [PATCH 01/11] Adds async and methods supporting paths in Bmp-ImageExtensions. --- src/ImageSharp/Formats/Bmp/ImageExtensions.cs | 72 +++++++++++++++++-- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/ImageExtensions.cs b/src/ImageSharp/Formats/Bmp/ImageExtensions.cs index 93e2b3fb1..8d97c8b46 100644 --- a/src/ImageSharp/Formats/Bmp/ImageExtensions.cs +++ b/src/ImageSharp/Formats/Bmp/ImageExtensions.cs @@ -1,8 +1,8 @@ -// Copyright (c) Six Labors. +// Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. using System.IO; - +using System.Threading.Tasks; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats.Bmp; @@ -13,13 +13,64 @@ namespace SixLabors.ImageSharp /// 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 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. + /// Thrown if the path is null. + /// A representing the asynchronous operation. + public static Task SaveAsBmpAsync(this Image source, string path, BmpEncoder encoder) => + source.SaveAsync( + 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 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. /// Thrown if the stream is null. - public static void SaveAsBmp(this Image source, Stream stream) => source.SaveAsBmp(stream, null); + /// A representing the asynchronous operation. + public static Task SaveAsBmpAsync(this Image source, Stream stream) => SaveAsBmpAsync(source, stream, null); /// /// Saves the image to the given stream with the bmp format. @@ -32,5 +83,18 @@ namespace SixLabors.ImageSharp 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. + /// Thrown if the stream is null. + /// A representing the asynchronous operation. + public static Task SaveAsBmpAsync(this Image source, Stream stream, BmpEncoder encoder) => + source.SaveAsync( + stream, + encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(BmpFormat.Instance)); } -} \ No newline at end of file +} From 8077c2bf7593916a3336b6589bc5b0fa1a693142 Mon Sep 17 00:00:00 2001 From: pekspro Date: Sat, 4 Jul 2020 17:23:30 +0200 Subject: [PATCH 02/11] Adds async and methods supporting paths in Gif-ImageExtensions. --- src/ImageSharp/Formats/Gif/ImageExtensions.cs | 78 +++++++++++++++++-- 1 file changed, 71 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/ImageExtensions.cs b/src/ImageSharp/Formats/Gif/ImageExtensions.cs index 7e762d68b..d262b056c 100644 --- a/src/ImageSharp/Formats/Gif/ImageExtensions.cs +++ b/src/ImageSharp/Formats/Gif/ImageExtensions.cs @@ -1,8 +1,8 @@ -// Copyright (c) Six Labors. +// Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. using System.IO; - +using System.Threading.Tasks; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats.Gif; @@ -14,23 +14,87 @@ namespace SixLabors.ImageSharp public static partial class ImageExtensions { /// - /// Saves the image to the given stream in the gif format. + /// 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 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. + /// Thrown if the path is null. + /// A representing the asynchronous operation. + public static Task SaveAsGifAsync(this Image source, string path, GifEncoder encoder) => + source.SaveAsync( + 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 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. /// Thrown if the stream is null. - public static void SaveAsGif(this Image source, Stream stream) => source.SaveAsGif(stream, null); + /// A representing the asynchronous operation. + public static Task SaveAsGifAsync(this Image source, Stream stream) => SaveAsGifAsync(source, stream, null); /// - /// Saves the image to the given stream in the gif format. + /// Saves the image to the given stream with the gif format. /// /// The image this method extends. /// The stream to save the image to. - /// The options for the encoder. + /// 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. + /// Thrown if the stream is null. + /// A representing the asynchronous operation. + public static Task SaveAsGifAsync(this Image source, Stream stream, GifEncoder encoder) => + source.SaveAsync( + stream, + encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance)); } -} \ No newline at end of file +} From fed16debcfc935834c726f3bd3ec2ee638b595d8 Mon Sep 17 00:00:00 2001 From: pekspro Date: Sat, 4 Jul 2020 17:23:55 +0200 Subject: [PATCH 03/11] Adds async and methods supporting paths in Jpeg-ImageExtensions. --- .../Formats/Jpeg/ImageExtensions.cs | 72 +++++++++++++++++-- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/ImageExtensions.cs b/src/ImageSharp/Formats/Jpeg/ImageExtensions.cs index ee47aa345..d6600b625 100644 --- a/src/ImageSharp/Formats/Jpeg/ImageExtensions.cs +++ b/src/ImageSharp/Formats/Jpeg/ImageExtensions.cs @@ -1,8 +1,8 @@ -// Copyright (c) Six Labors. +// Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. using System.IO; - +using System.Threading.Tasks; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats.Jpeg; @@ -13,6 +13,48 @@ namespace SixLabors.ImageSharp /// public static partial class ImageExtensions { + /// + /// 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 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. + /// Thrown if the path is null. + /// A representing the asynchronous operation. + public static Task SaveAsJpegAsync(this Image source, string path, JpegEncoder encoder) => + source.SaveAsync( + path, + encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance)); + /// /// Saves the image to the given stream with the jpeg format. /// @@ -26,11 +68,33 @@ namespace SixLabors.ImageSharp /// /// The image this method extends. /// The stream to save the image to. - /// The options for the encoder. + /// Thrown if the stream is null. + /// A representing the asynchronous operation. + public static Task SaveAsJpegAsync(this Image source, Stream stream) => SaveAsJpegAsync(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 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. + /// Thrown if the stream is null. + /// A representing the asynchronous operation. + public static Task SaveAsJpegAsync(this Image source, Stream stream, JpegEncoder encoder) => + source.SaveAsync( + stream, + encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance)); } -} \ No newline at end of file +} From 68ef124bcaf159fd766a7964e34f257dca24ac05 Mon Sep 17 00:00:00 2001 From: pekspro Date: Sat, 4 Jul 2020 17:24:18 +0200 Subject: [PATCH 04/11] Adds async and methods supporting paths in Png-ImageExtensions. --- src/ImageSharp/Formats/Png/ImageExtensions.cs | 72 +++++++++++++++++-- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Png/ImageExtensions.cs b/src/ImageSharp/Formats/Png/ImageExtensions.cs index 9188e43ad..e6a5265b2 100644 --- a/src/ImageSharp/Formats/Png/ImageExtensions.cs +++ b/src/ImageSharp/Formats/Png/ImageExtensions.cs @@ -1,8 +1,8 @@ -// Copyright (c) Six Labors. +// Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. using System.IO; - +using System.Threading.Tasks; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats.Png; @@ -13,6 +13,48 @@ namespace SixLabors.ImageSharp /// public static partial class ImageExtensions { + /// + /// 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 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. + /// Thrown if the path is null. + /// A representing the asynchronous operation. + public static Task SaveAsPngAsync(this Image source, string path, PngEncoder encoder) => + source.SaveAsync( + path, + encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance)); + /// /// Saves the image to the given stream with the png format. /// @@ -26,11 +68,33 @@ namespace SixLabors.ImageSharp /// /// The image this method extends. /// The stream to save the image to. - /// The options for the encoder. + /// Thrown if the stream is null. + /// A representing the asynchronous operation. + public static Task SaveAsPngAsync(this Image source, Stream stream) => SaveAsPngAsync(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 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. + /// Thrown if the stream is null. + /// A representing the asynchronous operation. + public static Task SaveAsPngAsync(this Image source, Stream stream, PngEncoder encoder) => + source.SaveAsync( + stream, + encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance)); } -} \ No newline at end of file +} From 480996548d81306d2adf69a617e4b9d70177ca8b Mon Sep 17 00:00:00 2001 From: pekspro Date: Sat, 4 Jul 2020 17:24:21 +0200 Subject: [PATCH 05/11] Adds async and methods supporting paths in Tga-ImageExtensions. --- src/ImageSharp/Formats/Tga/ImageExtensions.cs | 68 ++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Formats/Tga/ImageExtensions.cs b/src/ImageSharp/Formats/Tga/ImageExtensions.cs index 50e6c166a..f39738eae 100644 --- a/src/ImageSharp/Formats/Tga/ImageExtensions.cs +++ b/src/ImageSharp/Formats/Tga/ImageExtensions.cs @@ -2,7 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System.IO; - +using System.Threading.Tasks; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats.Tga; @@ -13,6 +13,48 @@ namespace SixLabors.ImageSharp /// public static partial class ImageExtensions { + /// + /// 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 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. + /// Thrown if the path is null. + /// A representing the asynchronous operation. + public static Task SaveAsTgaAsync(this Image source, string path, TgaEncoder encoder) => + source.SaveAsync( + path, + encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance)); + /// /// Saves the image to the given stream with the tga format. /// @@ -26,11 +68,33 @@ namespace SixLabors.ImageSharp /// /// The image this method extends. /// The stream to save the image to. - /// The options for the encoder. + /// Thrown if the stream is null. + /// A representing the asynchronous operation. + public static Task SaveAsTgaAsync(this Image source, Stream stream) => SaveAsTgaAsync(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 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. + /// Thrown if the stream is null. + /// A representing the asynchronous operation. + public static Task SaveAsTgaAsync(this Image source, Stream stream, TgaEncoder encoder) => + source.SaveAsync( + stream, + encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance)); } } From 458308a8f76f49ec8080121e36d3c41c18842e97 Mon Sep 17 00:00:00 2001 From: pekspro Date: Wed, 15 Jul 2020 10:39:30 +0200 Subject: [PATCH 06/11] Adds ImageExtensionsTest for bmp. --- .../Formats/Bmp/ImageExtensionsTest.cs | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs diff --git a/tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs new file mode 100644 index 000000000..e860c8855 --- /dev/null +++ b/tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs @@ -0,0 +1,152 @@ +using System.IO; +using System.Threading.Tasks; +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.Formats.Bmp; +using SixLabors.ImageSharp.PixelFormats; +using Xunit; + +namespace SixLabors.ImageSharp.Tests.Formats.Bmp +{ + public class ImageExtensionsTest + { + [Fact] + public void SaveAsBmp_Path() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); + string file = Path.Combine(dir, "SaveAsBmp_Path.bmp"); + + using (var image = new Image(10, 10)) + { + image.SaveAsBmp(file); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/bmp", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsBmpAsync_Path() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); + string file = Path.Combine(dir, "SaveAsBmpAsync_Path.bmp"); + + using (var image = new Image(10, 10)) + { + await image.SaveAsBmpAsync(file); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/bmp", mime.DefaultMimeType); + } + } + + [Fact] + public void SaveAsBmp_Path_Encoder() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); + string file = Path.Combine(dir, "SaveAsBmp_Path_Encoder.bmp"); + + using (var image = new Image(10, 10)) + { + image.SaveAsBmp(file, new BmpEncoder()); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/bmp", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsBmpAsync_Path_Encoder() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); + string file = Path.Combine(dir, "SaveAsBmpAsync_Path_Encoder.bmp"); + + using (var image = new Image(10, 10)) + { + await image.SaveAsBmpAsync(file, new BmpEncoder()); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/bmp", mime.DefaultMimeType); + } + } + + [Fact] + public void SaveAsBmp_Stream() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + image.SaveAsBmp(memoryStream); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/bmp", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsBmpAsync_StreamAsync() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + await image.SaveAsBmpAsync(memoryStream); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/bmp", mime.DefaultMimeType); + } + } + + [Fact] + public void SaveAsBmp_Stream_Encoder() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + image.SaveAsBmp(memoryStream, new BmpEncoder()); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/bmp", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsBmpAsync_Stream_Encoder() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + await image.SaveAsBmpAsync(memoryStream, new BmpEncoder()); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/bmp", mime.DefaultMimeType); + } + } + } +} From 31471d49bc21e8cc2aea2c453990dd2635653ed9 Mon Sep 17 00:00:00 2001 From: pekspro Date: Wed, 15 Jul 2020 10:43:18 +0200 Subject: [PATCH 07/11] Adds ImageExtensionsTest for gif. --- .../Formats/Gif/ImageExtensionsTest.cs | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs diff --git a/tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs new file mode 100644 index 000000000..0f3d37879 --- /dev/null +++ b/tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs @@ -0,0 +1,152 @@ +using System.IO; +using System.Threading.Tasks; +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.Formats.Gif; +using SixLabors.ImageSharp.PixelFormats; +using Xunit; + +namespace SixLabors.ImageSharp.Tests.Formats.Gif +{ + public class ImageExtensionsTest + { + [Fact] + public void SaveAsGif_Path() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); + string file = Path.Combine(dir, "SaveAsGif_Path.gif"); + + using (var image = new Image(10, 10)) + { + image.SaveAsGif(file); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/gif", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsGifAsync_Path() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); + string file = Path.Combine(dir, "SaveAsGifAsync_Path.gif"); + + using (var image = new Image(10, 10)) + { + await image.SaveAsGifAsync(file); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/gif", mime.DefaultMimeType); + } + } + + [Fact] + public void SaveAsGif_Path_Encoder() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); + string file = Path.Combine(dir, "SaveAsGif_Path_Encoder.gif"); + + using (var image = new Image(10, 10)) + { + image.SaveAsGif(file, new GifEncoder()); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/gif", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsGifAsync_Path_Encoder() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); + string file = Path.Combine(dir, "SaveAsGifAsync_Path_Encoder.gif"); + + using (var image = new Image(10, 10)) + { + await image.SaveAsGifAsync(file, new GifEncoder()); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/gif", mime.DefaultMimeType); + } + } + + [Fact] + public void SaveAsGif_Stream() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + image.SaveAsGif(memoryStream); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/gif", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsGifAsync_StreamAsync() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + await image.SaveAsGifAsync(memoryStream); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/gif", mime.DefaultMimeType); + } + } + + [Fact] + public void SaveAsGif_Stream_Encoder() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + image.SaveAsGif(memoryStream, new GifEncoder()); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/gif", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsGifAsync_Stream_Encoder() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + await image.SaveAsGifAsync(memoryStream, new GifEncoder()); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/gif", mime.DefaultMimeType); + } + } + } +} From fcd35a11b881feec7a901d505f21047b565edf3f Mon Sep 17 00:00:00 2001 From: pekspro Date: Wed, 15 Jul 2020 10:49:52 +0200 Subject: [PATCH 08/11] Adds ImageExtensionsTest for jpg. --- .../Formats/Jpg/ImageExtensionsTest.cs | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs diff --git a/tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs new file mode 100644 index 000000000..d77a2e340 --- /dev/null +++ b/tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs @@ -0,0 +1,152 @@ +using System.IO; +using System.Threading.Tasks; +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.Formats.Jpeg; +using SixLabors.ImageSharp.PixelFormats; +using Xunit; + +namespace SixLabors.ImageSharp.Tests.Formats.Jpg +{ + public class ImageExtensionsTest + { + [Fact] + public void SaveAsJpeg_Path() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); + string file = Path.Combine(dir, "SaveAsJpeg_Path.jpg"); + + using (var image = new Image(10, 10)) + { + image.SaveAsJpeg(file); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/jpeg", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsJpegAsync_Path() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); + string file = Path.Combine(dir, "SaveAsJpegAsync_Path.jpg"); + + using (var image = new Image(10, 10)) + { + await image.SaveAsJpegAsync(file); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/jpeg", mime.DefaultMimeType); + } + } + + [Fact] + public void SaveAsJpeg_Path_Encoder() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); + string file = Path.Combine(dir, "SaveAsJpeg_Path_Encoder.jpg"); + + using (var image = new Image(10, 10)) + { + image.SaveAsJpeg(file, new JpegEncoder()); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/jpeg", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsJpegAsync_Path_Encoder() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); + string file = Path.Combine(dir, "SaveAsJpegAsync_Path_Encoder.jpg"); + + using (var image = new Image(10, 10)) + { + await image.SaveAsJpegAsync(file, new JpegEncoder()); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/jpeg", mime.DefaultMimeType); + } + } + + [Fact] + public void SaveAsJpeg_Stream() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + image.SaveAsJpeg(memoryStream); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/jpeg", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsJpegAsync_StreamAsync() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + await image.SaveAsJpegAsync(memoryStream); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/jpeg", mime.DefaultMimeType); + } + } + + [Fact] + public void SaveAsJpeg_Stream_Encoder() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + image.SaveAsJpeg(memoryStream, new JpegEncoder()); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/jpeg", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsJpegAsync_Stream_Encoder() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + await image.SaveAsJpegAsync(memoryStream, new JpegEncoder()); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/jpeg", mime.DefaultMimeType); + } + } + } +} From 53554d2e3e1bc312de2b0c8b878b9dedc7ab7283 Mon Sep 17 00:00:00 2001 From: pekspro Date: Wed, 15 Jul 2020 10:53:51 +0200 Subject: [PATCH 09/11] Adds ImageExtensionsTest for png. --- .../Formats/Png/ImageExtensionsTest.cs | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs diff --git a/tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs new file mode 100644 index 000000000..9360d79eb --- /dev/null +++ b/tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs @@ -0,0 +1,152 @@ +using System.IO; +using System.Threading.Tasks; +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.Formats.Png; +using SixLabors.ImageSharp.PixelFormats; +using Xunit; + +namespace SixLabors.ImageSharp.Tests.Formats.Png +{ + public class ImageExtensionsTest + { + [Fact] + public void SaveAsPng_Path() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); + string file = Path.Combine(dir, "SaveAsPng_Path.png"); + + using (var image = new Image(10, 10)) + { + image.SaveAsPng(file); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/png", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsPngAsync_Path() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); + string file = Path.Combine(dir, "SaveAsPngAsync_Path.png"); + + using (var image = new Image(10, 10)) + { + await image.SaveAsPngAsync(file); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/png", mime.DefaultMimeType); + } + } + + [Fact] + public void SaveAsPng_Path_Encoder() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); + string file = Path.Combine(dir, "SaveAsPng_Path_Encoder.png"); + + using (var image = new Image(10, 10)) + { + image.SaveAsPng(file, new PngEncoder()); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/png", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsPngAsync_Path_Encoder() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); + string file = Path.Combine(dir, "SaveAsPngAsync_Path_Encoder.png"); + + using (var image = new Image(10, 10)) + { + await image.SaveAsPngAsync(file, new PngEncoder()); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/png", mime.DefaultMimeType); + } + } + + [Fact] + public void SaveAsPng_Stream() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + image.SaveAsPng(memoryStream); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/png", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsPngAsync_StreamAsync() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + await image.SaveAsPngAsync(memoryStream); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/png", mime.DefaultMimeType); + } + } + + [Fact] + public void SaveAsPng_Stream_Encoder() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + image.SaveAsPng(memoryStream, new PngEncoder()); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/png", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsPngAsync_Stream_Encoder() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + await image.SaveAsPngAsync(memoryStream, new PngEncoder()); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/png", mime.DefaultMimeType); + } + } + } +} From 626545aa8729a4d3b58e6d1b7c14cd0fe812f6d0 Mon Sep 17 00:00:00 2001 From: pekspro Date: Wed, 15 Jul 2020 10:59:23 +0200 Subject: [PATCH 10/11] Adds ImageExtensionsTest for tga. --- .../Formats/Tga/ImageExtensionsTest.cs | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs diff --git a/tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs new file mode 100644 index 000000000..6a4b846e2 --- /dev/null +++ b/tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs @@ -0,0 +1,152 @@ +using System.IO; +using System.Threading.Tasks; +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.Formats.Tga; +using SixLabors.ImageSharp.PixelFormats; +using Xunit; + +namespace SixLabors.ImageSharp.Tests.Formats.Tga +{ + public class ImageExtensionsTest + { + [Fact] + public void SaveAsTga_Path() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); + string file = Path.Combine(dir, "SaveAsTga_Path.tga"); + + using (var image = new Image(10, 10)) + { + image.SaveAsTga(file); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/tga", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsTgaAsync_Path() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); + string file = Path.Combine(dir, "SaveAsTgaAsync_Path.tga"); + + using (var image = new Image(10, 10)) + { + await image.SaveAsTgaAsync(file); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/tga", mime.DefaultMimeType); + } + } + + [Fact] + public void SaveAsTga_Path_Encoder() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); + string file = Path.Combine(dir, "SaveAsTga_Path_Encoder.tga"); + + using (var image = new Image(10, 10)) + { + image.SaveAsTga(file, new TgaEncoder()); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/tga", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsTgaAsync_Path_Encoder() + { + string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); + string file = Path.Combine(dir, "SaveAsTgaAsync_Path_Encoder.tga"); + + using (var image = new Image(10, 10)) + { + await image.SaveAsTgaAsync(file, new TgaEncoder()); + } + + using (Image.Load(file, out IImageFormat mime)) + { + Assert.Equal("image/tga", mime.DefaultMimeType); + } + } + + [Fact] + public void SaveAsTga_Stream() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + image.SaveAsTga(memoryStream); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/tga", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsTgaAsync_StreamAsync() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + await image.SaveAsTgaAsync(memoryStream); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/tga", mime.DefaultMimeType); + } + } + + [Fact] + public void SaveAsTga_Stream_Encoder() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + image.SaveAsTga(memoryStream, new TgaEncoder()); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/tga", mime.DefaultMimeType); + } + } + + [Fact] + public async Task SaveAsTgaAsync_Stream_Encoder() + { + using var memoryStream = new MemoryStream(); + + using (var image = new Image(10, 10)) + { + await image.SaveAsTgaAsync(memoryStream, new TgaEncoder()); + } + + memoryStream.Position = 0; + + using (Image.Load(memoryStream, out IImageFormat mime)) + { + Assert.Equal("image/tga", mime.DefaultMimeType); + } + } + } +} From f826f6d8417374a3851e2af56749c792be3a3cd3 Mon Sep 17 00:00:00 2001 From: pekspro Date: Wed, 15 Jul 2020 14:59:34 +0200 Subject: [PATCH 11/11] Adds file header on image extensions tests. --- tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs | 3 +++ tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs | 3 +++ tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs | 3 +++ tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs | 3 +++ tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs | 3 +++ 5 files changed, 15 insertions(+) diff --git a/tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs index e860c8855..5428ddbdc 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs @@ -1,3 +1,6 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + using System.IO; using System.Threading.Tasks; using SixLabors.ImageSharp.Formats; diff --git a/tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs index 0f3d37879..50b0fc6bf 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs @@ -1,3 +1,6 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + using System.IO; using System.Threading.Tasks; using SixLabors.ImageSharp.Formats; diff --git a/tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs index d77a2e340..9b67bcd1e 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs @@ -1,3 +1,6 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + using System.IO; using System.Threading.Tasks; using SixLabors.ImageSharp.Formats; diff --git a/tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs index 9360d79eb..f0493f176 100644 --- a/tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs @@ -1,3 +1,6 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + using System.IO; using System.Threading.Tasks; using SixLabors.ImageSharp.Formats; diff --git a/tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs index 6a4b846e2..3bde32b97 100644 --- a/tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs @@ -1,3 +1,6 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + using System.IO; using System.Threading.Tasks; using SixLabors.ImageSharp.Formats;