Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ The facade covers what every format has in common. Each format package has a ric

</details>

### Streaming

`ImageCodecs.OpenRowReader(stream, format, options)` / `OpenRowReaderAsync(…)` decode a stream that need not seek (a request body, a pipe) a band of rows at a time, top to bottom; the rows are exactly `TryDecodeInto`'s. `ImageCodecs.CreateEncoder(format, stream)` writes a file to a stream about every 64 KiB as the encoder produces it; its frames take `WriteRowsAsync` and the session `CommitAsync`, which never write synchronously. `IsStreaming` says whether the codec really works as the data flows: when it is false (a format, variant or option that needs the whole image, such as an interlaced PNG, a progressive JPEG, a scaled decode or palette quantisation) the reader reads the stream whole and decodes it first, and the encoder writes the file at `Commit`, behind the same API.

```csharp
// An upload re-encoded as PNG, stream to stream: rows in, bytes out, no synchronous I/O.
await using IImageRowReader reader = await ImageCodecs.OpenRowReaderAsync(request.Body, PixelFormat.Rgba32);
await using StreamEncoderSession png = ImageCodecs.CreateEncoder(ImageFormat.Png, response.Body);
StreamFrameEncoder frame = png.AddFrame(reader.Width, reader.Height, PixelFormat.Rgba32);
byte[] band = new byte[reader.Width * 4 * 16];
for (int n; (n = await reader.ReadRowsAsync(band, 16)) > 0;)
await frame.WriteRowsAsync(band.AsMemory(0, n * reader.Width * 4), n);
await png.CommitAsync();
```
Upgrading from 1.x: [docs/migrating-to-2.0.md](docs/migrating-to-2.0.md).

## Formats
Expand Down
14 changes: 14 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ ImageCodecs.Register(HeifImageDecoder.Registration);

</details>

### 流式读写

`ImageCodecs.OpenRowReader(stream, format, options)` / `OpenRowReaderAsync(…)` 从不必可寻址的流(请求体、管道)自上而下逐个行带解码,行数据与 `TryDecodeInto` 完全一致。`ImageCodecs.CreateEncoder(format, stream)` 在编码器产出字节时大约每 64 KiB 写一次流;它的帧有 `WriteRowsAsync`,会话有 `CommitAsync`,二者都不做同步写入。`IsStreaming` 表示 codec 是否真的边读边解、边编边写:为 false 时(需要整幅图像的格式、变体或选项,比如隔行 PNG、渐进式 JPEG、缩放解码、调色板量化),读取器先读完整个流再解码,编码器在 `Commit` 时写出文件,API 不变。

```csharp
// 把上传的图片转成 PNG,流到流:行进,字节出,没有同步 I/O。
await using IImageRowReader reader = await ImageCodecs.OpenRowReaderAsync(request.Body, PixelFormat.Rgba32);
await using StreamEncoderSession png = ImageCodecs.CreateEncoder(ImageFormat.Png, response.Body);
StreamFrameEncoder frame = png.AddFrame(reader.Width, reader.Height, PixelFormat.Rgba32);
byte[] band = new byte[reader.Width * 4 * 16];
for (int n; (n = await reader.ReadRowsAsync(band, 16)) > 0;)
await frame.WriteRowsAsync(band.AsMemory(0, n * reader.Width * 4), n);
await png.CommitAsync();
```
从 1.x 升级:见 [docs/migrating-to-2.0.md](docs/migrating-to-2.0.md)。

## 支持的格式
Expand Down
41 changes: 32 additions & 9 deletions src/Sdcb.SimdCodecs/ImageCodecs.Encode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ public static partial class ImageCodecs
];
private static readonly Lock s_encoderLock = new();

/// <summary>The encoder registry as it stands (for tests, to put it back with <see cref="ExchangeEncoders"/>).</summary>
internal static ImageEncoderRegistration[] EncoderSnapshot => Volatile.Read(ref s_encoders);

/// <summary>Replaces the encoder registry and returns the one it replaced (for tests, as <see cref="ExchangeRegistry"/>).</summary>
internal static ImageEncoderRegistration[] ExchangeEncoders(ImageEncoderRegistration[] encoders)
{
lock (s_encoderLock)
{
var previous = s_encoders;
Volatile.Write(ref s_encoders, encoders);
return previous;
}
}

/// <summary>
/// Enables encoding to a format the facade does not encode out of the box, or replaces the encoder for one
/// (<c>ImageCodecs.RegisterEncoder(MyEncoder.Registration)</c>). PNG, JPEG, TIFF, WebP, GIF, BMP, ICO, QOI and Netpbm
Expand Down Expand Up @@ -83,6 +97,12 @@ public static IImageEncoderSession CreateEncoder(ImageFormat format, IBufferWrit
/// 8-bit-only format receives them narrowed, <c>v &gt;&gt; 8</c>), its <see cref="IDecodedImage.ColorEncoding"/> and
/// its <see cref="IDecodedImage.Metadata"/> (Exif, XMP, ICC), so a transcode keeps the capture time, GPS position and
/// colour profile. Float images cannot be encoded this way (no integer projection).
/// <para>
/// The file is written to the stream about every 64 KiB as the encoder produces it, synchronously; the stream is not
/// flushed. An encode that fails or is cancelled may leave the first part of the file in the stream. To write it
/// asynchronously, or row band by row band, use
/// <see cref="CreateEncoder(ImageFormat, Stream, EncodeOptions?, bool)"/>.
/// </para>
/// </summary>
/// <exception cref="NotSupportedException">No registered encoder writes <paramref name="format"/>, or the image has
/// float samples.</exception>
Expand All @@ -92,13 +112,13 @@ public static void Encode(IDecodedImage image, ImageFormat format, Stream destin
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(destination);
var buffer = new ArrayBufferWriter<byte>();
Encode(image, format, buffer, options, cancellationToken);
destination.Write(buffer.WrittenSpan);
using var writer = new StreamBufferWriter(destination);
Encode(image, format, writer, options, cancellationToken);
writer.Flush();
}

/// <summary><see cref="Encode(IDecodedImage, ImageFormat, Stream, EncodeOptions?, CancellationToken)"/> into a buffer
/// writer (for a <c>Stream</c> with no copy: <c>PipeWriter.Create(stream)</c>).</summary>
/// writer (a <c>PipeWriter</c>, an <c>ArrayBufferWriter</c>).</summary>
public static void Encode(IDecodedImage image, ImageFormat format, IBufferWriter<byte> destination, EncodeOptions? options = null,
CancellationToken cancellationToken = default)
{
Expand Down Expand Up @@ -130,7 +150,7 @@ public static byte[] Encode(ReadOnlySpan<byte> pixels, int width, int height, Pi

/// <summary>
/// <see cref="Encode(ReadOnlySpan{byte}, int, int, PixelFormat, ImageFormat, int, EncodeOptions?, ImageMetadata?, CancellationToken)"/>
/// writing the file into <paramref name="destination"/> (for a <c>Stream</c> with no copy: <c>PipeWriter.Create(stream)</c>).
/// writing the file into <paramref name="destination"/> (a <c>PipeWriter</c>, an <c>ArrayBufferWriter</c>).
/// </summary>
/// <exception cref="NotSupportedException">No registered encoder writes <paramref name="format"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="options"/> is of a type <paramref name="format"/>'s encoder does not take.</exception>
Expand All @@ -146,7 +166,10 @@ public static void Encode(ReadOnlySpan<byte> pixels, int width, int height, Pixe

/// <summary>
/// <see cref="Encode(ReadOnlySpan{byte}, int, int, PixelFormat, ImageFormat, int, EncodeOptions?, ImageMetadata?, CancellationToken)"/>
/// writing the file to <paramref name="destination"/> (buffered, then written in one call).
/// writing the file to <paramref name="destination"/>, about every 64 KiB as the encoder produces it, synchronously;
/// the stream is not flushed. An encode that fails or is cancelled may leave the first part of the file in the stream.
/// To write it asynchronously, or row band by row band, use
/// <see cref="CreateEncoder(ImageFormat, Stream, EncodeOptions?, bool)"/>.
/// </summary>
/// <exception cref="NotSupportedException">No registered encoder writes <paramref name="format"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="options"/> is of a type <paramref name="format"/>'s encoder does not take.</exception>
Expand All @@ -155,9 +178,9 @@ public static void Encode(ReadOnlySpan<byte> pixels, int width, int height, Pixe
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(destination);
var buffer = new ArrayBufferWriter<byte>();
Encode(pixels, width, height, pixelFormat, format, buffer, stride, options, metadata, cancellationToken);
destination.Write(buffer.WrittenSpan);
using var writer = new StreamBufferWriter(destination);
Encode(pixels, width, height, pixelFormat, format, writer, stride, options, metadata, cancellationToken);
writer.Flush();
}

private static ImageEncoderRegistration? FindEncoder(ImageFormat format)
Expand Down
190 changes: 190 additions & 0 deletions src/Sdcb.SimdCodecs/ImageCodecs.Streaming.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
using System.IO;
using Sdcb.SimdCodecs.Abstractions;
using Sdcb.SimdCodecs.Abstractions.CodecSupport;

namespace Sdcb.SimdCodecs;

public static partial class ImageCodecs
{
/// <summary>
/// The leading bytes <see cref="OpenRowReader"/> sniffs: the longest fixed signature a codec reads. An ISO-BMFF
/// <c>ftyp</c> box (HEIF, AVIF) lists brands to its end, so for one the sniff takes the whole box, up to
/// <see cref="MaxSniffLength"/>. The sniff always sees exactly that many bytes (fewer only for a shorter stream),
/// whatever the stream's reads return, so the codec chosen never depends on how the bytes arrive.
/// </summary>
internal const int SniffLength = 64;

/// <summary>The most bytes the sniff reads for an <c>ftyp</c> box (a thousand brands).</summary>
internal const int MaxSniffLength = 4096;

/// <summary>
/// Opens a reader that decodes <paramref name="stream"/> a band of rows at a time, top to bottom, as the stream
/// arrives: a request body, a network or pipe stream, a file too large to read whole. The stream need not seek. The
/// codec is chosen as <see cref="DetectFormat"/> chooses it, from the first 64 bytes (a leading ISO-BMFF <c>ftyp</c> box whole, up to 4 KiB). A codec that decodes as its input
/// arrives (<see cref="IImageRowReader.IsStreaming"/> true) holds a few rows and a small input buffer, and reads the
/// stream no further than the rows asked for need; any other codec, variant or option (an interlaced PNG, a
/// progressive JPEG, a scaled decode) reads the stream to its end and decodes it first, behind the same API
/// (<see cref="IImageRowReader.IsStreaming"/> false).
/// <para>
/// The rows are exactly those <see cref="TryDecodeInto"/> writes for the same bytes, <paramref name="format"/> and
/// <paramref name="options"/>. The header is read before this returns, so <see cref="IImageRowReader.Width"/> and
/// <see cref="IImageRowReader.Height"/> size the destination. A gain-map JPEG reads as the plain JPEG it carries (its SDR
/// base, which is <see cref="TryDecodeInto"/>'s output for it too): the gain map lies past the sniffed bytes. Reads
/// the stream synchronously; <see cref="OpenRowReaderAsync"/> never does. Dispose the reader when done.
/// </para>
/// </summary>
/// <param name="stream">The image, from its current position; it need not seek.</param>
/// <param name="format">The layout of the rows.</param>
/// <param name="options">Scale, region and orientation; null for the whole image at full size, as stored. The pool is ignored.</param>
/// <param name="leaveOpen">Keep <paramref name="stream"/> open when the reader is disposed (or when this throws).</param>
/// <param name="cancellationToken">Checked while the header (or, for a buffered decode, the whole stream) is read.</param>
/// <exception cref="InvalidDataException">No registered codec decodes the stream (the message names
/// <see cref="DetectFormat"/>'s answer), or its header is malformed or truncated.</exception>
/// <exception cref="NotSupportedException">The image has no projection to <paramref name="format"/> (float samples, an
/// HDR transfer; see <see cref="Decode(ReadOnlySpan{byte}, PixelFormat, DecodeOptions?, CancellationToken)"/>).</exception>
/// <exception cref="ArgumentException"><paramref name="stream"/> cannot be read, or
/// <see cref="DecodeOptions.Region"/> does not lie inside the image.</exception>
/// <exception cref="OperationCanceledException"><paramref name="cancellationToken"/> was cancelled.</exception>
public static IImageRowReader OpenRowReader(Stream stream, PixelFormat format, DecodeOptions? options = null,
bool leaveOpen = false, CancellationToken cancellationToken = default)
{
var input = OpenInput(stream, leaveOpen);
StreamingRowReader? reader = null;
try
{
input.Fill(SniffLength);
if (FtypLength(input.Buffered) is var ftyp and > SniffLength)
input.Fill(ftyp);
reader = CreateRowReader(input, format, options);
reader.Initialize(cancellationToken);
return reader;
}
catch
{
if (reader is not null) reader.Dispose();
else input.Dispose();
throw;
}
}

/// <summary>
/// <see cref="OpenRowReader"/>, reading the stream asynchronously only: for a stream that forbids synchronous reads (an
/// ASP.NET Core request body). Read the rows with <see cref="IImageRowReader.ReadRowsAsync"/>.
/// </summary>
/// <inheritdoc cref="OpenRowReader" path="/param"/>
/// <inheritdoc cref="OpenRowReader" path="/exception"/>
public static async ValueTask<IImageRowReader> OpenRowReaderAsync(Stream stream, PixelFormat format,
DecodeOptions? options = null, bool leaveOpen = false, CancellationToken cancellationToken = default)
{
var input = OpenInput(stream, leaveOpen);
StreamingRowReader? reader = null;
try
{
await input.FillAsync(SniffLength, cancellationToken).ConfigureAwait(false);
if (FtypLength(input.Buffered) is var ftyp and > SniffLength)
await input.FillAsync(ftyp, cancellationToken).ConfigureAwait(false);
reader = CreateRowReader(input, format, options);
await reader.InitializeAsync(cancellationToken).ConfigureAwait(false);
return reader;
}
catch
{
if (reader is not null) await reader.DisposeAsync().ConfigureAwait(false);
else input.Dispose();
throw;
}
}

private static StreamInput OpenInput(Stream stream, bool leaveOpen)
{
ArgumentNullException.ThrowIfNull(stream);
return new StreamInput(stream, leaveOpen);
}

// The bytes to sniff: SniffLength, or a leading ftyp box whole (capped at MaxSniffLength).
private static int FtypLength(ReadOnlySpan<byte> header) =>
header.Length >= 8 && header[4..8].SequenceEqual("ftyp"u8)
? (int)Math.Clamp(System.Buffers.Binary.BinaryPrimitives.ReadUInt32BigEndian(header), SniffLength, MaxSniffLength)
: SniffLength;

// The reader for the codec that claims the sniffed bytes: its streaming decoder, or the whole-input fallback.
private static StreamingRowReader CreateRowReader(StreamInput input, PixelFormat format, DecodeOptions? options)
{
var buffered = input.Buffered;
var header = buffered[..Math.Min(buffered.Length, FtypLength(buffered))];
var entry = Find(header) ?? throw new InvalidDataException(NoCodecMessage(header));
if (entry.CreateRowReader is { } create)
return create(input, format, options);
return new BufferedRowReader(input, format, options,
(ReadOnlySpan<byte> data, out ImageInfo info) => ReadInfoOrThrow(entry, data, out info),
(data, destination, stride, pixelFormat, decodeOptions, cancellationToken) =>
DecodeIntoOrThrow(entry, data, destination, stride, pixelFormat, decodeOptions, cancellationToken));
}

// The buffered reader's header read, failing as Decode does: with the codec's reason when it has one.
private static bool ReadInfoOrThrow(ImageDecoderRegistration entry, ReadOnlySpan<byte> data, out ImageInfo info)
{
if (entry.TryReadInfo(data, out info))
return true;
throw DecodeFailure(entry, data, null, CancellationToken.None, $"The {FormatName(entry.Format)} header cannot be read.")!;
}

// The buffered reader's decode, failing as Decode(data, format) does: a corrupt file with the codec's reason, an image
// without an integer projection with NotSupportedException.
private static bool DecodeIntoOrThrow(ImageDecoderRegistration entry, ReadOnlySpan<byte> data, Span<byte> destination,
int stride, PixelFormat format, DecodeOptions? options, CancellationToken cancellationToken)
{
if (entry.TryDecodeInto(data, destination, stride, format, options, cancellationToken))
return true;
if (DecodeFailure(entry, data, options, cancellationToken, null) is { } failure)
throw failure;
throw entry.TryReadInfo(data, out var info)
? NoProjection(entry.Format, info, format)
: new InvalidDataException($"The {FormatName(entry.Format)} file cannot be decoded.");
}

/// <summary>
/// Opens an encoding session for <paramref name="format"/> writing to <paramref name="output"/>: the session of
/// <see cref="CreateEncoder(ImageFormat, System.Buffers.IBufferWriter{byte}, EncodeOptions?)"/>, its bytes written to
/// the stream about every 64 KiB as the encoder produces them. An encoder that encodes rows as they come
/// (<see cref="StreamEncoderSession.IsStreaming"/> true) so writes the file while its rows are still arriving, holding
/// a band of rows and about a chunk of output; one that needs the whole image (palette quantisation, WebP lossless)
/// writes it at <see cref="StreamEncoderSession.Commit"/>. The bytes are those the buffer-writer session writes. The
/// synchronous members write a chunk as soon as it fills; the asynchronous ones hold what one call produces (the whole
/// file, for an encoder that is not streaming) until the call ends, then write it.
/// <para>
/// <see cref="StreamFrameEncoder.WriteRows"/> and <see cref="StreamEncoderSession.Commit"/> write the stream
/// synchronously; <see cref="StreamFrameEncoder.WriteRowsAsync"/> and <see cref="StreamEncoderSession.CommitAsync"/>
/// only asynchronously (an ASP.NET Core response body). Commit flushes the stream. Disposing a session that was not
/// committed leaves the bytes already written in the stream: an incomplete file.
/// </para>
/// </summary>
/// <param name="format">The format to write.</param>
/// <param name="output">Where the file is written, from its current position.</param>
/// <param name="options">The format's options; null for its defaults.</param>
/// <param name="leaveOpen">Keep <paramref name="output"/> open when the session is disposed (or when this throws for
/// the options).</param>
/// <exception cref="NotSupportedException">No registered encoder writes <paramref name="format"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="output"/> cannot be written, or <paramref name="options"/> is
/// not null and not an options type <paramref name="format"/>'s encoder takes.</exception>
public static StreamEncoderSession CreateEncoder(ImageFormat format, Stream output, EncodeOptions? options = null,
bool leaveOpen = false)
{
ArgumentNullException.ThrowIfNull(output);
if (!output.CanWrite)
throw new ArgumentException("The stream cannot be written.", nameof(output));
var encoder = RequireEncoder(format);
var writer = new StreamBufferWriter(output);
try
{
return new StreamEncoderSession(encoder.Create(writer, options), writer, output, leaveOpen);
}
catch
{
writer.Dispose();
if (!leaveOpen)
output.Dispose();
throw;
}
}
}
Loading