mirror of https://github.com/SixLabors/ImageSharp
20 changed files with 139 additions and 137 deletions
@ -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,43 @@ |
|||||
|
// 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.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) |
||||
|
{ |
||||
|
encoder.Encode(image, stream, cancellationToken); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
using var ms = new MemoryStream(); |
||||
|
encoder.Encode(image, ms, cancellationToken); |
||||
|
ms.Position = 0; |
||||
|
await ms.CopyToAsync(stream, configuration.StreamProcessingBufferSize, cancellationToken) |
||||
|
.ConfigureAwait(false); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static void Encode<TPixel>( |
||||
|
this IImageEncoderInternals encoder, |
||||
|
Image<TPixel> image, |
||||
|
Stream stream) |
||||
|
where TPixel : unmanaged, IPixel<TPixel> |
||||
|
=> encoder.Encode(image, stream, default); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue