Browse Source

Adding extensions was easier than I though

qoi
LuisAlfredo92 3 years ago
parent
commit
c92a4ecb59
No known key found for this signature in database GPG Key ID: 13A8436905993B8F
  1. 102
      src/ImageSharp/Formats/ImageExtensions.Save.cs
  2. 135
      tests/ImageSharp.Tests/Formats/Qoi/ImageExtensionsTest.cs

102
src/ImageSharp/Formats/ImageExtensions.Save.cs

@ -9,6 +9,7 @@ using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Pbm;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Formats.Qoi;
using SixLabors.ImageSharp.Formats.Tga;
using SixLabors.ImageSharp.Formats.Webp;
using SixLabors.ImageSharp.Formats.Tiff;
@ -836,4 +837,105 @@ public static partial class ImageExtensions
encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(TiffFormat.Instance),
cancellationToken);
/// <summary>
/// Saves the image to the given stream with the Qoi format.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="path">The file path to save the image to.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the path is null.</exception>
public static void SaveAsQoi(this Image source, string path) => SaveAsQoi(source, path, default);
/// <summary>
/// Saves the image to the given stream with the Qoi format.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="path">The file path to save the image to.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the path is null.</exception>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static Task SaveAsQoiAsync(this Image source, string path) => SaveAsQoiAsync(source, path, default);
/// <summary>
/// Saves the image to the given stream with the Qoi format.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="path">The file path to save the image to.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the path is null.</exception>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static Task SaveAsQoiAsync(this Image source, string path, CancellationToken cancellationToken)
=> SaveAsQoiAsync(source, path, default, cancellationToken);
/// <summary>
/// Saves the image to the given stream with the Qoi format.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="path">The file path to save the image to.</param>
/// <param name="encoder">The encoder to save the image with.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the path is null.</exception>
public static void SaveAsQoi(this Image source, string path, QoiEncoder encoder) =>
source.Save(
path,
encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(QoiFormat.Instance));
/// <summary>
/// Saves the image to the given stream with the Qoi format.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="path">The file path to save the image to.</param>
/// <param name="encoder">The encoder to save the image with.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the path is null.</exception>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static Task SaveAsQoiAsync(this Image source, string path, QoiEncoder encoder, CancellationToken cancellationToken = default)
=> source.SaveAsync(
path,
encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(QoiFormat.Instance),
cancellationToken);
/// <summary>
/// Saves the image to the given stream with the Qoi format.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="stream">The stream to save the image to.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsQoi(this Image source, Stream stream)
=> SaveAsQoi(source, stream, default);
/// <summary>
/// Saves the image to the given stream with the Qoi format.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="stream">The stream to save the image to.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static Task SaveAsQoiAsync(this Image source, Stream stream, CancellationToken cancellationToken = default)
=> SaveAsQoiAsync(source, stream, default, cancellationToken);
/// <summary>
/// Saves the image to the given stream with the Qoi format.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="stream">The stream to save the image to.</param>
/// <param name="encoder">The encoder to save the image with.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SaveAsQoi(this Image source, Stream stream, QoiEncoder encoder)
=> source.Save(
stream,
encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(QoiFormat.Instance));
/// <summary>
/// Saves the image to the given stream with the Qoi format.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="stream">The stream to save the image to.</param>
/// <param name="encoder">The encoder to save the image with.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static Task SaveAsQoiAsync(this Image source, Stream stream, QoiEncoder encoder, CancellationToken cancellationToken = default)
=> source.SaveAsync(
stream,
encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(QoiFormat.Instance),
cancellationToken);
}

135
tests/ImageSharp.Tests/Formats/Qoi/ImageExtensionsTest.cs

@ -0,0 +1,135 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.Formats.Qoi;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Tests.Formats.Qoi;
public class ImageExtensionsTest
{
[Fact]
public void SaveAsQoi_Path()
{
string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest));
string file = Path.Combine(dir, "SaveAsQoi_Path.qoi");
using (Image<L8> image = new(10, 10))
{
image.SaveAsQoi(file);
}
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is QoiFormat);
}
[Fact]
public async Task SaveAsQoiAsync_Path()
{
string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest));
string file = Path.Combine(dir, "SaveAsQoiAsync_Path.qoi");
using (Image<L8> image = new(10, 10))
{
await image.SaveAsQoiAsync(file);
}
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is QoiFormat);
}
[Fact]
public void SaveAsQoi_Path_Encoder()
{
string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions));
string file = Path.Combine(dir, "SaveAsQoi_Path_Encoder.qoi");
using (Image<L8> image = new(10, 10))
{
image.SaveAsQoi(file, new QoiEncoder());
}
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is QoiFormat);
}
[Fact]
public async Task SaveAsQoiAsync_Path_Encoder()
{
string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions));
string file = Path.Combine(dir, "SaveAsQoiAsync_Path_Encoder.qoi");
using (Image<L8> image = new(10, 10))
{
await image.SaveAsQoiAsync(file, new QoiEncoder());
}
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is QoiFormat);
}
[Fact]
public void SaveAsQoi_Stream()
{
using MemoryStream memoryStream = new();
using (Image<L8> image = new(10, 10))
{
image.SaveAsQoi(memoryStream);
}
memoryStream.Position = 0;
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is QoiFormat);
}
[Fact]
public async Task SaveAsQoiAsync_StreamAsync()
{
using MemoryStream memoryStream = new();
using (Image<L8> image = new(10, 10))
{
await image.SaveAsQoiAsync(memoryStream);
}
memoryStream.Position = 0;
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is QoiFormat);
}
[Fact]
public void SaveAsQoi_Stream_Encoder()
{
using MemoryStream memoryStream = new();
using (Image<L8> image = new(10, 10))
{
image.SaveAsQoi(memoryStream, new QoiEncoder());
}
memoryStream.Position = 0;
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is QoiFormat);
}
[Fact]
public async Task SaveAsQoiAsync_Stream_Encoder()
{
using MemoryStream memoryStream = new();
using (Image<L8> image = new(10, 10))
{
await image.SaveAsQoiAsync(memoryStream, new QoiEncoder());
}
memoryStream.Position = 0;
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is QoiFormat);
}
}
Loading…
Cancel
Save