mirror of https://github.com/SixLabors/ImageSharp
committed by
GitHub
66 changed files with 1936 additions and 936 deletions
@ -1,100 +0,0 @@ |
|||
// 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; |
|||
|
|||
namespace SixLabors.ImageSharp |
|||
{ |
|||
/// <summary>
|
|||
/// Extension methods for the <see cref="Image"/> type.
|
|||
/// </summary>
|
|||
public static partial class ImageExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the gif 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 SaveAsGif(this Image source, string path) => SaveAsGif(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the gif 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 SaveAsGifAsync(this Image source, string path) => SaveAsGifAsync(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the gif 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 SaveAsGif(this Image source, string path, GifEncoder encoder) => |
|||
source.Save( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the gif 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static Task SaveAsGifAsync(this Image source, string path, GifEncoder encoder) => |
|||
source.SaveAsync( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the gif 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 SaveAsGif(this Image source, Stream stream) => SaveAsGif(source, stream, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the gif 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static Task SaveAsGifAsync(this Image source, Stream stream) => SaveAsGifAsync(source, stream, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the gif 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 SaveAsGif(this Image source, Stream stream, GifEncoder encoder) => |
|||
source.Save( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the gif 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static Task SaveAsGifAsync(this Image source, Stream stream, GifEncoder encoder) => |
|||
source.SaveAsync( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance)); |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System.IO; |
|||
using System.Threading; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats |
|||
{ |
|||
/// <summary>
|
|||
/// Abstraction for shared internals for ***DecoderCore implementations to be used with <see cref="ImageEncoderUtilities"/>.
|
|||
/// </summary>
|
|||
internal interface IImageEncoderInternals |
|||
{ |
|||
/// <summary>
|
|||
/// Encodes the image.
|
|||
/// </summary>
|
|||
/// <param name="image">The image.</param>
|
|||
/// <param name="stream">The stream.</param>
|
|||
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
|
|||
/// <typeparam name="TPixel">The pixel type.</typeparam>
|
|||
void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken) |
|||
where TPixel : unmanaged, IPixel<TPixel>; |
|||
} |
|||
} |
|||
@ -0,0 +1,61 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using System.IO; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using SixLabors.ImageSharp.Advanced; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats |
|||
{ |
|||
internal static class ImageEncoderUtilities |
|||
{ |
|||
public static async Task EncodeAsync<TPixel>( |
|||
this IImageEncoderInternals encoder, |
|||
Image<TPixel> image, |
|||
Stream stream, |
|||
CancellationToken cancellationToken) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
Configuration configuration = image.GetConfiguration(); |
|||
if (stream.CanSeek) |
|||
{ |
|||
await DoEncodeAsync(stream).ConfigureAwait(false); |
|||
} |
|||
else |
|||
{ |
|||
using var ms = new MemoryStream(); |
|||
await DoEncodeAsync(ms); |
|||
ms.Position = 0; |
|||
await ms.CopyToAsync(stream, configuration.StreamProcessingBufferSize, cancellationToken) |
|||
.ConfigureAwait(false); |
|||
} |
|||
|
|||
Task DoEncodeAsync(Stream innerStream) |
|||
{ |
|||
try |
|||
{ |
|||
encoder.Encode(image, innerStream, cancellationToken); |
|||
return Task.CompletedTask; |
|||
} |
|||
catch (OperationCanceledException) |
|||
{ |
|||
return Task.FromCanceled(cancellationToken); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
return Task.FromException(ex); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public static void Encode<TPixel>( |
|||
this IImageEncoderInternals encoder, |
|||
Image<TPixel> image, |
|||
Stream stream) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
=> encoder.Encode(image, stream, default); |
|||
} |
|||
} |
|||
@ -0,0 +1,539 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
// <auto-generated />
|
|||
using System.IO; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using SixLabors.ImageSharp.Advanced; |
|||
|
|||
using SixLabors.ImageSharp.Formats.Bmp; |
|||
using SixLabors.ImageSharp.Formats.Gif; |
|||
using SixLabors.ImageSharp.Formats.Jpeg; |
|||
using SixLabors.ImageSharp.Formats.Png; |
|||
using SixLabors.ImageSharp.Formats.Tga; |
|||
|
|||
namespace SixLabors.ImageSharp |
|||
{ |
|||
/// <summary>
|
|||
/// Extension methods for the <see cref="Image"/> type.
|
|||
/// </summary>
|
|||
public static partial class ImageExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Bmp 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 SaveAsBmp(this Image source, string path) => SaveAsBmp(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Bmp 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 SaveAsBmpAsync(this Image source, string path) => SaveAsBmpAsync(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Bmp 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 SaveAsBmpAsync(this Image source, string path, CancellationToken cancellationToken) |
|||
=> SaveAsBmpAsync(source, path, null, cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Bmp 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 SaveAsBmp(this Image source, string path, BmpEncoder encoder) => |
|||
source.Save( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(BmpFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Bmp 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 SaveAsBmpAsync(this Image source, string path, BmpEncoder encoder, CancellationToken cancellationToken = default) => |
|||
source.SaveAsync( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(BmpFormat.Instance), |
|||
cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Bmp 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 SaveAsBmp(this Image source, Stream stream) |
|||
=> SaveAsBmp(source, stream, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Bmp 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 SaveAsBmpAsync(this Image source, Stream stream, CancellationToken cancellationToken = default) |
|||
=> SaveAsBmpAsync(source, stream, null, cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Bmp 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static void SaveAsBmp(this Image source, Stream stream, BmpEncoder encoder) |
|||
=> source.Save( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(BmpFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Bmp 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 SaveAsBmpAsync(this Image source, Stream stream, BmpEncoder encoder, CancellationToken cancellationToken = default) => |
|||
source.SaveAsync( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(BmpFormat.Instance), |
|||
cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Gif 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 SaveAsGif(this Image source, string path) => SaveAsGif(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Gif 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 SaveAsGifAsync(this Image source, string path) => SaveAsGifAsync(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Gif 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 SaveAsGifAsync(this Image source, string path, CancellationToken cancellationToken) |
|||
=> SaveAsGifAsync(source, path, null, cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Gif 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 SaveAsGif(this Image source, string path, GifEncoder encoder) => |
|||
source.Save( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Gif 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 SaveAsGifAsync(this Image source, string path, GifEncoder encoder, CancellationToken cancellationToken = default) => |
|||
source.SaveAsync( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance), |
|||
cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Gif 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 SaveAsGif(this Image source, Stream stream) |
|||
=> SaveAsGif(source, stream, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Gif 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 SaveAsGifAsync(this Image source, Stream stream, CancellationToken cancellationToken = default) |
|||
=> SaveAsGifAsync(source, stream, null, cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Gif 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static void SaveAsGif(this Image source, Stream stream, GifEncoder encoder) |
|||
=> source.Save( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Gif 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 SaveAsGifAsync(this Image source, Stream stream, GifEncoder encoder, CancellationToken cancellationToken = default) => |
|||
source.SaveAsync( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance), |
|||
cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Jpeg 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 SaveAsJpeg(this Image source, string path) => SaveAsJpeg(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Jpeg 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 SaveAsJpegAsync(this Image source, string path) => SaveAsJpegAsync(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Jpeg 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 SaveAsJpegAsync(this Image source, string path, CancellationToken cancellationToken) |
|||
=> SaveAsJpegAsync(source, path, null, cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Jpeg 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 SaveAsJpeg(this Image source, string path, JpegEncoder encoder) => |
|||
source.Save( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Jpeg 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 SaveAsJpegAsync(this Image source, string path, JpegEncoder encoder, CancellationToken cancellationToken = default) => |
|||
source.SaveAsync( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance), |
|||
cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Jpeg 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 SaveAsJpeg(this Image source, Stream stream) |
|||
=> SaveAsJpeg(source, stream, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Jpeg 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 SaveAsJpegAsync(this Image source, Stream stream, CancellationToken cancellationToken = default) |
|||
=> SaveAsJpegAsync(source, stream, null, cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Jpeg 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static void SaveAsJpeg(this Image source, Stream stream, JpegEncoder encoder) |
|||
=> source.Save( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Jpeg 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 SaveAsJpegAsync(this Image source, Stream stream, JpegEncoder encoder, CancellationToken cancellationToken = default) => |
|||
source.SaveAsync( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance), |
|||
cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Png 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 SaveAsPng(this Image source, string path) => SaveAsPng(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Png 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 SaveAsPngAsync(this Image source, string path) => SaveAsPngAsync(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Png 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 SaveAsPngAsync(this Image source, string path, CancellationToken cancellationToken) |
|||
=> SaveAsPngAsync(source, path, null, cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Png 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 SaveAsPng(this Image source, string path, PngEncoder encoder) => |
|||
source.Save( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Png 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 SaveAsPngAsync(this Image source, string path, PngEncoder encoder, CancellationToken cancellationToken = default) => |
|||
source.SaveAsync( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance), |
|||
cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Png 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 SaveAsPng(this Image source, Stream stream) |
|||
=> SaveAsPng(source, stream, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Png 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 SaveAsPngAsync(this Image source, Stream stream, CancellationToken cancellationToken = default) |
|||
=> SaveAsPngAsync(source, stream, null, cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Png 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static void SaveAsPng(this Image source, Stream stream, PngEncoder encoder) |
|||
=> source.Save( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Png 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 SaveAsPngAsync(this Image source, Stream stream, PngEncoder encoder, CancellationToken cancellationToken = default) => |
|||
source.SaveAsync( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance), |
|||
cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Tga 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 SaveAsTga(this Image source, string path) => SaveAsTga(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Tga 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 SaveAsTgaAsync(this Image source, string path) => SaveAsTgaAsync(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Tga 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 SaveAsTgaAsync(this Image source, string path, CancellationToken cancellationToken) |
|||
=> SaveAsTgaAsync(source, path, null, cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Tga 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 SaveAsTga(this Image source, string path, TgaEncoder encoder) => |
|||
source.Save( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Tga 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 SaveAsTgaAsync(this Image source, string path, TgaEncoder encoder, CancellationToken cancellationToken = default) => |
|||
source.SaveAsync( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance), |
|||
cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Tga 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 SaveAsTga(this Image source, Stream stream) |
|||
=> SaveAsTga(source, stream, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Tga 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 SaveAsTgaAsync(this Image source, Stream stream, CancellationToken cancellationToken = default) |
|||
=> SaveAsTgaAsync(source, stream, null, cancellationToken); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Tga 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static void SaveAsTga(this Image source, Stream stream, TgaEncoder encoder) |
|||
=> source.Save( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the Tga 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 SaveAsTgaAsync(this Image source, Stream stream, TgaEncoder encoder, CancellationToken cancellationToken = default) => |
|||
source.SaveAsync( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance), |
|||
cancellationToken); |
|||
|
|||
} |
|||
} |
|||
@ -1,100 +0,0 @@ |
|||
// 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; |
|||
|
|||
namespace SixLabors.ImageSharp |
|||
{ |
|||
/// <summary>
|
|||
/// Extension methods for the <see cref="Image"/> type.
|
|||
/// </summary>
|
|||
public static partial class ImageExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the jpeg 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 SaveAsJpeg(this Image source, string path) => SaveAsJpeg(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the jpeg 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 SaveAsJpegAsync(this Image source, string path) => SaveAsJpegAsync(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the jpeg 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 SaveAsJpeg(this Image source, string path, JpegEncoder encoder) => |
|||
source.Save( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the jpeg 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static Task SaveAsJpegAsync(this Image source, string path, JpegEncoder encoder) => |
|||
source.SaveAsync( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the jpeg 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 SaveAsJpeg(this Image source, Stream stream) => SaveAsJpeg(source, stream, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the jpeg 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static Task SaveAsJpegAsync(this Image source, Stream stream) => SaveAsJpegAsync(source, stream, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the jpeg 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 SaveAsJpeg(this Image source, Stream stream, JpegEncoder encoder) => |
|||
source.Save( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the jpeg 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static Task SaveAsJpegAsync(this Image source, Stream stream, JpegEncoder encoder) => |
|||
source.SaveAsync( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance)); |
|||
} |
|||
} |
|||
@ -1,100 +0,0 @@ |
|||
// 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; |
|||
|
|||
namespace SixLabors.ImageSharp |
|||
{ |
|||
/// <summary>
|
|||
/// Extension methods for the <see cref="Image"/> type.
|
|||
/// </summary>
|
|||
public static partial class ImageExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the png 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 SaveAsPng(this Image source, string path) => SaveAsPng(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the png 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 SaveAsPngAsync(this Image source, string path) => SaveAsPngAsync(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the png 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 SaveAsPng(this Image source, string path, PngEncoder encoder) => |
|||
source.Save( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the png 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static Task SaveAsPngAsync(this Image source, string path, PngEncoder encoder) => |
|||
source.SaveAsync( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the png 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 SaveAsPng(this Image source, Stream stream) => SaveAsPng(source, stream, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the png 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static Task SaveAsPngAsync(this Image source, Stream stream) => SaveAsPngAsync(source, stream, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the png 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 SaveAsPng(this Image source, Stream stream, PngEncoder encoder) => |
|||
source.Save( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the png 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static Task SaveAsPngAsync(this Image source, Stream stream, PngEncoder encoder) => |
|||
source.SaveAsync( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance)); |
|||
} |
|||
} |
|||
@ -1,100 +0,0 @@ |
|||
// 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.Tga; |
|||
|
|||
namespace SixLabors.ImageSharp |
|||
{ |
|||
/// <summary>
|
|||
/// Extension methods for the <see cref="Image"/> type.
|
|||
/// </summary>
|
|||
public static partial class ImageExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the tga 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 SaveAsTga(this Image source, string path) => SaveAsTga(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the tga 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 SaveAsTgaAsync(this Image source, string path) => SaveAsTgaAsync(source, path, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the tga 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 SaveAsTga(this Image source, string path, TgaEncoder encoder) => |
|||
source.Save( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the tga 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static Task SaveAsTgaAsync(this Image source, string path, TgaEncoder encoder) => |
|||
source.SaveAsync( |
|||
path, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the tga 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 SaveAsTga(this Image source, Stream stream) => SaveAsTga(source, stream, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the tga 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static Task SaveAsTgaAsync(this Image source, Stream stream) => SaveAsTgaAsync(source, stream, null); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the tga 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 SaveAsTga(this Image source, Stream stream, TgaEncoder encoder) => |
|||
source.Save( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance)); |
|||
|
|||
/// <summary>
|
|||
/// Saves the image to the given stream with the tga 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>
|
|||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
|||
public static Task SaveAsTgaAsync(this Image source, Stream stream, TgaEncoder encoder) => |
|||
source.SaveAsync( |
|||
stream, |
|||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance)); |
|||
} |
|||
} |
|||
@ -0,0 +1,130 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using System.IO; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using Xunit; |
|||
|
|||
namespace SixLabors.ImageSharp.Tests |
|||
{ |
|||
public partial class ImageTests |
|||
{ |
|||
public class Decode_Cancellation : ImageLoadTestBase |
|||
{ |
|||
private bool isTestStreamSeekable; |
|||
private readonly SemaphoreSlim notifyWaitPositionReachedSemaphore = new SemaphoreSlim(0); |
|||
private readonly SemaphoreSlim continueSemaphore = new SemaphoreSlim(0); |
|||
private readonly CancellationTokenSource cts = new CancellationTokenSource(); |
|||
|
|||
public Decode_Cancellation() |
|||
{ |
|||
this.TopLevelConfiguration.StreamProcessingBufferSize = 128; |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(false)] |
|||
[InlineData(true)] |
|||
public async Task LoadAsync_Specific_Stream(bool isInputStreamSeekable) |
|||
{ |
|||
this.isTestStreamSeekable = isInputStreamSeekable; |
|||
_ = Task.Factory.StartNew(this.DoCancel, TaskCreationOptions.LongRunning); |
|||
|
|||
await Assert.ThrowsAsync<TaskCanceledException>(() => Image.LoadAsync<Rgb24>(this.TopLevelConfiguration, this.DataStream, this.cts.Token)); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(false)] |
|||
[InlineData(true)] |
|||
public async Task LoadAsync_Agnostic_Stream(bool isInputStreamSeekable) |
|||
{ |
|||
this.isTestStreamSeekable = isInputStreamSeekable; |
|||
_ = Task.Factory.StartNew(this.DoCancel, TaskCreationOptions.LongRunning); |
|||
|
|||
await Assert.ThrowsAsync<TaskCanceledException>(() => Image.LoadAsync(this.TopLevelConfiguration, this.DataStream, this.cts.Token)); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task LoadAsync_Agnostic_Path() |
|||
{ |
|||
this.isTestStreamSeekable = true; |
|||
_ = Task.Factory.StartNew(this.DoCancel, TaskCreationOptions.LongRunning); |
|||
|
|||
await Assert.ThrowsAsync<TaskCanceledException>(() => Image.LoadAsync(this.TopLevelConfiguration, this.MockFilePath, this.cts.Token)); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task LoadAsync_Specific_Path() |
|||
{ |
|||
this.isTestStreamSeekable = true; |
|||
_ = Task.Factory.StartNew(this.DoCancel, TaskCreationOptions.LongRunning); |
|||
|
|||
await Assert.ThrowsAsync<TaskCanceledException>(() => Image.LoadAsync<Rgb24>(this.TopLevelConfiguration, this.MockFilePath, this.cts.Token)); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(false)] |
|||
[InlineData(true)] |
|||
public async Task IdentifyAsync_Stream(bool isInputStreamSeekable) |
|||
{ |
|||
this.isTestStreamSeekable = isInputStreamSeekable; |
|||
_ = Task.Factory.StartNew(this.DoCancel, TaskCreationOptions.LongRunning); |
|||
|
|||
await Assert.ThrowsAsync<TaskCanceledException>(() => Image.IdentifyAsync(this.TopLevelConfiguration, this.DataStream, this.cts.Token)); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task IdentifyAsync_CustomConfiguration_Path() |
|||
{ |
|||
this.isTestStreamSeekable = true; |
|||
_ = Task.Factory.StartNew(this.DoCancel, TaskCreationOptions.LongRunning); |
|||
|
|||
await Assert.ThrowsAsync<TaskCanceledException>(() => Image.IdentifyAsync(this.TopLevelConfiguration, this.MockFilePath, this.cts.Token)); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(false)] |
|||
[InlineData(true)] |
|||
public async Task IdentifyWithFormatAsync_CustomConfiguration_Stream(bool isInputStreamSeekable) |
|||
{ |
|||
this.isTestStreamSeekable = isInputStreamSeekable; |
|||
_ = Task.Factory.StartNew(this.DoCancel, TaskCreationOptions.LongRunning); |
|||
|
|||
await Assert.ThrowsAsync<TaskCanceledException>(() => Image.IdentifyWithFormatAsync(this.TopLevelConfiguration, this.DataStream, this.cts.Token)); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task IdentifyWithFormatAsync_CustomConfiguration_Path() |
|||
{ |
|||
this.isTestStreamSeekable = true; |
|||
_ = Task.Factory.StartNew(this.DoCancel, TaskCreationOptions.LongRunning); |
|||
|
|||
await Assert.ThrowsAsync<TaskCanceledException>(() => Image.IdentifyWithFormatAsync(this.TopLevelConfiguration, this.MockFilePath, this.cts.Token)); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task IdentifyWithFormatAsync_DefaultConfiguration_Stream() |
|||
{ |
|||
_ = Task.Factory.StartNew(this.DoCancel, TaskCreationOptions.LongRunning); |
|||
|
|||
await Assert.ThrowsAsync<TaskCanceledException>(() => Image.IdentifyWithFormatAsync(this.DataStream, this.cts.Token)); |
|||
} |
|||
|
|||
private async Task DoCancel() |
|||
{ |
|||
// wait until we reach the middle of the steam
|
|||
await this.notifyWaitPositionReachedSemaphore.WaitAsync(); |
|||
|
|||
// set the cancellation
|
|||
this.cts.Cancel(); |
|||
|
|||
// continue processing the stream
|
|||
this.continueSemaphore.Release(); |
|||
} |
|||
|
|||
protected override Stream CreateStream() => this.TestFormat.CreateAsyncSamaphoreStream(this.notifyWaitPositionReachedSemaphore, this.continueSemaphore, this.isTestStreamSeekable); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,68 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using System.IO; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace SixLabors.ImageSharp.Tests.TestUtilities |
|||
{ |
|||
internal class SemaphoreReadMemoryStream : MemoryStream |
|||
{ |
|||
private readonly SemaphoreSlim continueSemaphore; |
|||
private readonly SemaphoreSlim notifyWaitPositionReachedSemaphore; |
|||
private int pauseDone; |
|||
private readonly long waitPosition; |
|||
|
|||
public SemaphoreReadMemoryStream( |
|||
byte[] buffer, |
|||
long waitPosition, |
|||
SemaphoreSlim notifyWaitPositionReachedSemaphore, |
|||
SemaphoreSlim continueSemaphore) |
|||
: base(buffer) |
|||
{ |
|||
this.continueSemaphore = continueSemaphore; |
|||
this.notifyWaitPositionReachedSemaphore = notifyWaitPositionReachedSemaphore; |
|||
this.waitPosition = waitPosition; |
|||
} |
|||
|
|||
public override int Read(byte[] buffer, int offset, int count) |
|||
{ |
|||
int read = base.Read(buffer, offset, count); |
|||
if (this.Position > this.waitPosition && this.TryPause()) |
|||
{ |
|||
this.notifyWaitPositionReachedSemaphore.Release(); |
|||
this.continueSemaphore.Wait(); |
|||
} |
|||
|
|||
return read; |
|||
} |
|||
|
|||
private bool TryPause() => Interlocked.CompareExchange(ref this.pauseDone, 1, 0) == 0; |
|||
|
|||
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
|||
{ |
|||
int read = await base.ReadAsync(buffer, offset, count, cancellationToken); |
|||
if (this.Position > this.waitPosition && this.TryPause()) |
|||
{ |
|||
this.notifyWaitPositionReachedSemaphore.Release(); |
|||
await this.continueSemaphore.WaitAsync(); |
|||
} |
|||
|
|||
return read; |
|||
} |
|||
|
|||
public override int ReadByte() |
|||
{ |
|||
if (this.Position + 1 > this.waitPosition && this.TryPause()) |
|||
{ |
|||
this.notifyWaitPositionReachedSemaphore.Release(); |
|||
this.continueSemaphore.Wait(); |
|||
} |
|||
|
|||
int result = base.ReadByte(); |
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,90 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System.IO; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using SixLabors.ImageSharp.Tests.TestUtilities; |
|||
using Xunit; |
|||
|
|||
namespace SixLabors.ImageSharp.Tests |
|||
{ |
|||
public class SemaphoreReadMemoryStreamTests |
|||
{ |
|||
private readonly SemaphoreSlim continueSemaphore = new SemaphoreSlim(0); |
|||
private readonly SemaphoreSlim notifyWaitPositionReachedSemaphore = new SemaphoreSlim(0); |
|||
private readonly byte[] buffer = new byte[128]; |
|||
|
|||
[Fact] |
|||
public void Read_BeforeWaitLimit_ShouldFinish() |
|||
{ |
|||
using Stream stream = this.CreateTestStream(); |
|||
int read = stream.Read(this.buffer); |
|||
Assert.Equal(this.buffer.Length, read); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task ReadAsync_BeforeWaitLimit_ShouldFinish() |
|||
{ |
|||
using Stream stream = this.CreateTestStream(); |
|||
int read = await stream.ReadAsync(this.buffer, 0, this.buffer.Length); |
|||
Assert.Equal(this.buffer.Length, read); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Read_AfterWaitLimit_ShouldPause() |
|||
{ |
|||
using Stream stream = this.CreateTestStream(); |
|||
stream.Read(this.buffer); |
|||
Assert.Equal(0, this.notifyWaitPositionReachedSemaphore.CurrentCount); |
|||
|
|||
Task readTask = Task.Factory.StartNew( |
|||
() => |
|||
{ |
|||
stream.Read(this.buffer); |
|||
stream.Read(this.buffer); |
|||
stream.Read(this.buffer); |
|||
stream.Read(this.buffer); |
|||
stream.Read(this.buffer); |
|||
}, TaskCreationOptions.LongRunning); |
|||
|
|||
await Task.Delay(5); |
|||
Assert.False(readTask.IsCompleted); |
|||
await this.notifyWaitPositionReachedSemaphore.WaitAsync(); |
|||
await Task.Delay(5); |
|||
Assert.False(readTask.IsCompleted); |
|||
this.continueSemaphore.Release(); |
|||
await readTask; |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task ReadAsync_AfterWaitLimit_ShouldPause() |
|||
{ |
|||
using Stream stream = this.CreateTestStream(); |
|||
await stream.ReadAsync(this.buffer, 0, this.buffer.Length); |
|||
|
|||
Task readTask = Task.Factory.StartNew( |
|||
async () => |
|||
{ |
|||
await stream.ReadAsync(this.buffer, 0, this.buffer.Length); |
|||
await stream.ReadAsync(this.buffer, 0, this.buffer.Length); |
|||
await stream.ReadAsync(this.buffer, 0, this.buffer.Length); |
|||
await stream.ReadAsync(this.buffer, 0, this.buffer.Length); |
|||
await stream.ReadAsync(this.buffer, 0, this.buffer.Length); |
|||
}, TaskCreationOptions.LongRunning); |
|||
await Task.Delay(5); |
|||
Assert.False(readTask.IsCompleted); |
|||
await this.notifyWaitPositionReachedSemaphore.WaitAsync(); |
|||
await Task.Delay(5); |
|||
Assert.False(readTask.IsCompleted); |
|||
this.continueSemaphore.Release(); |
|||
await readTask; |
|||
} |
|||
|
|||
private Stream CreateTestStream(int size = 1024, int waitAfterPosition = 256) |
|||
{ |
|||
byte[] buffer = new byte[size]; |
|||
return new SemaphoreReadMemoryStream(buffer, waitAfterPosition, this.notifyWaitPositionReachedSemaphore, this.continueSemaphore); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue