Skip to content

Commit c92a4ec

Browse files
committed
Adding extensions was easier than I though
1 parent 4a494c7 commit c92a4ec

File tree

2 files changed

+237
-0
lines changed

2 files changed

+237
-0
lines changed

src/ImageSharp/Formats/ImageExtensions.Save.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using SixLabors.ImageSharp.Formats.Jpeg;
1010
using SixLabors.ImageSharp.Formats.Pbm;
1111
using SixLabors.ImageSharp.Formats.Png;
12+
using SixLabors.ImageSharp.Formats.Qoi;
1213
using SixLabors.ImageSharp.Formats.Tga;
1314
using SixLabors.ImageSharp.Formats.Webp;
1415
using SixLabors.ImageSharp.Formats.Tiff;
@@ -836,4 +837,105 @@ public static Task SaveAsTiffAsync(this Image source, Stream stream, TiffEncoder
836837
encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(TiffFormat.Instance),
837838
cancellationToken);
838839

840+
/// <summary>
841+
/// Saves the image to the given stream with the Qoi format.
842+
/// </summary>
843+
/// <param name="source">The image this method extends.</param>
844+
/// <param name="path">The file path to save the image to.</param>
845+
/// <exception cref="System.ArgumentNullException">Thrown if the path is null.</exception>
846+
public static void SaveAsQoi(this Image source, string path) => SaveAsQoi(source, path, default);
847+
848+
/// <summary>
849+
/// Saves the image to the given stream with the Qoi format.
850+
/// </summary>
851+
/// <param name="source">The image this method extends.</param>
852+
/// <param name="path">The file path to save the image to.</param>
853+
/// <exception cref="System.ArgumentNullException">Thrown if the path is null.</exception>
854+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
855+
public static Task SaveAsQoiAsync(this Image source, string path) => SaveAsQoiAsync(source, path, default);
856+
857+
/// <summary>
858+
/// Saves the image to the given stream with the Qoi format.
859+
/// </summary>
860+
/// <param name="source">The image this method extends.</param>
861+
/// <param name="path">The file path to save the image to.</param>
862+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
863+
/// <exception cref="System.ArgumentNullException">Thrown if the path is null.</exception>
864+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
865+
public static Task SaveAsQoiAsync(this Image source, string path, CancellationToken cancellationToken)
866+
=> SaveAsQoiAsync(source, path, default, cancellationToken);
867+
868+
/// <summary>
869+
/// Saves the image to the given stream with the Qoi format.
870+
/// </summary>
871+
/// <param name="source">The image this method extends.</param>
872+
/// <param name="path">The file path to save the image to.</param>
873+
/// <param name="encoder">The encoder to save the image with.</param>
874+
/// <exception cref="System.ArgumentNullException">Thrown if the path is null.</exception>
875+
public static void SaveAsQoi(this Image source, string path, QoiEncoder encoder) =>
876+
source.Save(
877+
path,
878+
encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(QoiFormat.Instance));
879+
880+
/// <summary>
881+
/// Saves the image to the given stream with the Qoi format.
882+
/// </summary>
883+
/// <param name="source">The image this method extends.</param>
884+
/// <param name="path">The file path to save the image to.</param>
885+
/// <param name="encoder">The encoder to save the image with.</param>
886+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
887+
/// <exception cref="System.ArgumentNullException">Thrown if the path is null.</exception>
888+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
889+
public static Task SaveAsQoiAsync(this Image source, string path, QoiEncoder encoder, CancellationToken cancellationToken = default)
890+
=> source.SaveAsync(
891+
path,
892+
encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(QoiFormat.Instance),
893+
cancellationToken);
894+
895+
/// <summary>
896+
/// Saves the image to the given stream with the Qoi format.
897+
/// </summary>
898+
/// <param name="source">The image this method extends.</param>
899+
/// <param name="stream">The stream to save the image to.</param>
900+
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
901+
public static void SaveAsQoi(this Image source, Stream stream)
902+
=> SaveAsQoi(source, stream, default);
903+
904+
/// <summary>
905+
/// Saves the image to the given stream with the Qoi format.
906+
/// </summary>
907+
/// <param name="source">The image this method extends.</param>
908+
/// <param name="stream">The stream to save the image to.</param>
909+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
910+
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
911+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
912+
public static Task SaveAsQoiAsync(this Image source, Stream stream, CancellationToken cancellationToken = default)
913+
=> SaveAsQoiAsync(source, stream, default, cancellationToken);
914+
915+
/// <summary>
916+
/// Saves the image to the given stream with the Qoi format.
917+
/// </summary>
918+
/// <param name="source">The image this method extends.</param>
919+
/// <param name="stream">The stream to save the image to.</param>
920+
/// <param name="encoder">The encoder to save the image with.</param>
921+
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
922+
public static void SaveAsQoi(this Image source, Stream stream, QoiEncoder encoder)
923+
=> source.Save(
924+
stream,
925+
encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(QoiFormat.Instance));
926+
927+
/// <summary>
928+
/// Saves the image to the given stream with the Qoi format.
929+
/// </summary>
930+
/// <param name="source">The image this method extends.</param>
931+
/// <param name="stream">The stream to save the image to.</param>
932+
/// <param name="encoder">The encoder to save the image with.</param>
933+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
934+
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
935+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
936+
public static Task SaveAsQoiAsync(this Image source, Stream stream, QoiEncoder encoder, CancellationToken cancellationToken = default)
937+
=> source.SaveAsync(
938+
stream,
939+
encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(QoiFormat.Instance),
940+
cancellationToken);
839941
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using SixLabors.ImageSharp.Formats.Qoi;
5+
using SixLabors.ImageSharp.Formats;
6+
using SixLabors.ImageSharp.PixelFormats;
7+
8+
namespace SixLabors.ImageSharp.Tests.Formats.Qoi;
9+
10+
public class ImageExtensionsTest
11+
{
12+
[Fact]
13+
public void SaveAsQoi_Path()
14+
{
15+
string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest));
16+
string file = Path.Combine(dir, "SaveAsQoi_Path.qoi");
17+
18+
using (Image<L8> image = new(10, 10))
19+
{
20+
image.SaveAsQoi(file);
21+
}
22+
23+
IImageFormat format = Image.DetectFormat(file);
24+
Assert.True(format is QoiFormat);
25+
}
26+
27+
[Fact]
28+
public async Task SaveAsQoiAsync_Path()
29+
{
30+
string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest));
31+
string file = Path.Combine(dir, "SaveAsQoiAsync_Path.qoi");
32+
33+
using (Image<L8> image = new(10, 10))
34+
{
35+
await image.SaveAsQoiAsync(file);
36+
}
37+
38+
IImageFormat format = Image.DetectFormat(file);
39+
Assert.True(format is QoiFormat);
40+
}
41+
42+
[Fact]
43+
public void SaveAsQoi_Path_Encoder()
44+
{
45+
string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions));
46+
string file = Path.Combine(dir, "SaveAsQoi_Path_Encoder.qoi");
47+
48+
using (Image<L8> image = new(10, 10))
49+
{
50+
image.SaveAsQoi(file, new QoiEncoder());
51+
}
52+
53+
IImageFormat format = Image.DetectFormat(file);
54+
Assert.True(format is QoiFormat);
55+
}
56+
57+
[Fact]
58+
public async Task SaveAsQoiAsync_Path_Encoder()
59+
{
60+
string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions));
61+
string file = Path.Combine(dir, "SaveAsQoiAsync_Path_Encoder.qoi");
62+
63+
using (Image<L8> image = new(10, 10))
64+
{
65+
await image.SaveAsQoiAsync(file, new QoiEncoder());
66+
}
67+
68+
IImageFormat format = Image.DetectFormat(file);
69+
Assert.True(format is QoiFormat);
70+
}
71+
72+
[Fact]
73+
public void SaveAsQoi_Stream()
74+
{
75+
using MemoryStream memoryStream = new();
76+
77+
using (Image<L8> image = new(10, 10))
78+
{
79+
image.SaveAsQoi(memoryStream);
80+
}
81+
82+
memoryStream.Position = 0;
83+
84+
IImageFormat format = Image.DetectFormat(memoryStream);
85+
Assert.True(format is QoiFormat);
86+
}
87+
88+
[Fact]
89+
public async Task SaveAsQoiAsync_StreamAsync()
90+
{
91+
using MemoryStream memoryStream = new();
92+
93+
using (Image<L8> image = new(10, 10))
94+
{
95+
await image.SaveAsQoiAsync(memoryStream);
96+
}
97+
98+
memoryStream.Position = 0;
99+
100+
IImageFormat format = Image.DetectFormat(memoryStream);
101+
Assert.True(format is QoiFormat);
102+
}
103+
104+
[Fact]
105+
public void SaveAsQoi_Stream_Encoder()
106+
{
107+
using MemoryStream memoryStream = new();
108+
109+
using (Image<L8> image = new(10, 10))
110+
{
111+
image.SaveAsQoi(memoryStream, new QoiEncoder());
112+
}
113+
114+
memoryStream.Position = 0;
115+
116+
IImageFormat format = Image.DetectFormat(memoryStream);
117+
Assert.True(format is QoiFormat);
118+
}
119+
120+
[Fact]
121+
public async Task SaveAsQoiAsync_Stream_Encoder()
122+
{
123+
using MemoryStream memoryStream = new();
124+
125+
using (Image<L8> image = new(10, 10))
126+
{
127+
await image.SaveAsQoiAsync(memoryStream, new QoiEncoder());
128+
}
129+
130+
memoryStream.Position = 0;
131+
132+
IImageFormat format = Image.DetectFormat(memoryStream);
133+
Assert.True(format is QoiFormat);
134+
}
135+
}

0 commit comments

Comments
 (0)