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
45 changes: 40 additions & 5 deletions src/ImageSharp/IO/ChunkedMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,32 @@ public override void Write(ReadOnlySpan<byte> buffer)
remaining = this.memoryChunkBuffer.Length - this.position;
}

if (this.position > this.length)
{
// Seeking does not expose bytes or allocate storage. A later write exposes the skipped range,
// which must contain zeros rather than pooled data. Clear only that gap, not each new chunk.
long gap = this.position - this.length;

// Locate the previous end after expansion. Clearing the gap also advances the cursor to the write position.
this.SetPosition(this.length);

while (gap > 0)
{
MemoryChunk chunk = this.memoryChunkBuffer[this.bufferIndex];
int bytesToClear = (int)Math.Min(gap, chunk.Length - this.chunkIndex);
chunk.Buffer.Slice(this.chunkIndex, bytesToClear).Clear();

gap -= bytesToClear;
this.position += bytesToClear;
this.chunkIndex += bytesToClear;
if (this.chunkIndex == chunk.Length)
{
this.chunkIndex = 0;
this.bufferIndex++;
}
}
}

int bytesToWrite = count;
int bytesWritten = 0;
while (bytesToWrite > 0 && this.bufferIndex != this.memoryChunkBuffer.Length)
Expand Down Expand Up @@ -231,7 +257,11 @@ public override void Write(ReadOnlySpan<byte> buffer)
}

this.position += bytesWritten;
this.length += bytesWritten;

// Encoders such as TIFF and BMP seek back to patch offsets that were written earlier.
// Overwriting existing bytes must not extend the stream, so the length only grows
// when the write ends beyond the current end of the data.
this.length = Math.Max(this.length, this.position);
}

/// <summary>
Expand Down Expand Up @@ -330,6 +360,10 @@ protected override void Dispose(bool disposing)
}
}

/// <summary>
/// Moves the logical position without allocating storage or changing the stream length.
/// </summary>
/// <param name="value">The nonnegative byte offset from the start of the stream.</param>
private void SetPosition(long value)
{
long newPosition = value;
Expand All @@ -344,11 +378,12 @@ private void SetPosition(long value)
int currentChunkIndex = 0;
long offset = newPosition;

// If the new position is greater than the length of the stream, set the position to the end of the stream
if (offset > 0 && offset >= this.memoryChunkBuffer.Length)
// At the capacity boundary, the next byte belongs to the next chunk. Positions beyond capacity have
// no allocated chunk yet. Write expands storage, then traverses the gap from the previous stream end.
if (offset >= this.memoryChunkBuffer.Length)
{
this.bufferIndex = this.memoryChunkBuffer.ChunkCount - 1;
this.chunkIndex = this.memoryChunkBuffer[this.bufferIndex].Length - 1;
this.bufferIndex = this.memoryChunkBuffer.ChunkCount;
this.chunkIndex = 0;
return;
}

Expand Down
27 changes: 27 additions & 0 deletions tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,33 @@ public void TiffEncoder_WritesIfdOffsetAtWordBoundary()
Assert.Equal(imageBytes[5], expectedIfdOffsetBytes[1]);
}

/// <summary>
/// Checks that backpatching multiple frame directories produces identical seekable and buffered output.
/// </summary>
[Fact]
public async Task TiffEncoder_ToNonSeekableStream_MatchesSeekableStream()
{
// arrange
// Each frame adds an IFD offset that is patched after the frame data is written.
using Image<Rgba32> image = new(32, 24, Color.Red.ToPixel<Rgba32>());
image.Frames.CreateFrame(Color.Blue.ToPixel<Rgba32>());
TiffEncoder encoder = new();
using MemoryStream seekable = new();
image.Save(seekable, encoder);

using MemoryStream syncTarget = new();
using MemoryStream asyncTarget = new();

// act
image.Save(new NonSeekableStream(syncTarget), encoder);
await image.SaveAsync(new NonSeekableStream(asyncTarget), encoder, TestContext.Current.CancellationToken);

// assert
byte[] expected = seekable.ToArray();
Assert.Equal(expected, syncTarget.ToArray());
Assert.Equal(expected, asyncTarget.ToArray());
}

[Theory]
[WithFile(RgbUncompressed, PixelTypes.Rgba32, TiffCompression.CcittGroup3Fax, TiffCompression.CcittGroup3Fax)]
[WithFile(RgbUncompressed, PixelTypes.Rgba32, TiffCompression.CcittGroup4Fax, TiffCompression.CcittGroup4Fax)]
Expand Down
74 changes: 74 additions & 0 deletions tests/ImageSharp.Tests/IO/ChunkedMemoryStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests.Memory;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;

namespace SixLabors.ImageSharp.Tests.IO;
Expand Down Expand Up @@ -224,6 +225,79 @@ public void MemoryStream_WriteToSpanTests(int length)
}
}

/// <summary>
/// Checks overwrites within a chunk, across chunks, and beyond the previous end of the stream.
/// </summary>
/// <param name="length">The initial stream length.</param>
/// <param name="position">The offset to overwrite.</param>
[Theory]
[InlineData(16, 6)]
[InlineData(DefaultSmallChunkSize + 16, DefaultSmallChunkSize - 2)]
[InlineData(16, 14)]
public void MemoryStream_OverwriteAfterSeek_MatchesMemoryStream(int length, int position)
{
using ChunkedMemoryStream ms = new(this.allocator);
using MemoryStream expected = new();
byte[] head = this.CreateTestBuffer(length);
ms.Write(head, 0, head.Length);
expected.Write(head, 0, head.Length);

// Patch bytes inside the written data, as encoders do for offsets that are only known later.
byte[] patch = [1, 2, 3, 4];
ms.Position = position;
expected.Position = position;
ms.Write(patch, 0, patch.Length);
expected.Write(patch, 0, patch.Length);

Assert.Equal(expected.Position, ms.Position);
Assert.Equal(expected.Length, ms.Length);
Assert.Equal(expected.ToArray(), ms.ToArray());
}

/// <summary>
/// Checks seek gaps before filling the reserved header, so dirty allocator bytes cannot go undetected.
/// </summary>
/// <param name="length">The initial stream length.</param>
/// <param name="gap">The number of bytes to skip before writing.</param>
/// <param name="count">The number of tail bytes to write.</param>
[Theory]
[InlineData(0, 16, 4)]
[InlineData(16, 16, 4)]
[InlineData(DefaultSmallChunkSize, 0, 4)]
[InlineData(DefaultSmallChunkSize - 8, 16, 4)]
[InlineData(16, DefaultSmallChunkSize * 2, 4)]
[InlineData(0, DefaultSmallChunkSize, 0)]
public void MemoryStream_WriteAfterSeekPastEnd_WritesAtPosition(int length, int gap, int count)
{
using ChunkedMemoryStream ms = new(new TestMemoryAllocator());
using MemoryStream expected = new();
byte[] head = this.CreateTestBuffer(length);
ms.Write(head, 0, head.Length);
expected.Write(head, 0, head.Length);

// Leave a gap after the data, as encoders do for a header that is written last.
byte[] tail = [1, 2, 3, 4];
ms.Seek(gap, SeekOrigin.Current);
expected.Seek(gap, SeekOrigin.Current);
ms.Write(tail, 0, count);
expected.Write(tail, 0, count);

// MemoryStream exposes zeros in the gap. The dirty allocator makes missing initialization deterministic.
Assert.Equal(expected.Position, ms.Position);
Assert.Equal(expected.Length, ms.Length);
Assert.Equal(expected.ToArray(), ms.ToArray());

byte[] header = this.CreateTestBuffer(gap);
ms.Position = length;
expected.Position = length;
ms.Write(header, 0, header.Length);
expected.Write(header, 0, header.Length);

Assert.Equal(expected.Position, ms.Position);
Assert.Equal(expected.Length, ms.Length);
Assert.Equal(expected.ToArray(), ms.ToArray());
}

[Fact]
public void MemoryStream_WriteByteTests()
{
Expand Down
51 changes: 51 additions & 0 deletions tests/ImageSharp.Tests/Image/ImageTests.Save.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Six Labors Split License.

using Moq;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
Expand Down Expand Up @@ -57,6 +58,56 @@ public void SetEncoding()
Assert.True(format is PngFormat);
}

/// <summary>
/// Checks that synchronous and asynchronous buffering preserve the bytes written by each encoder.
/// </summary>
/// <param name="filename">The filename used to select the encoder.</param>
[Theory]
[InlineData("test.ani")]
[InlineData("test.bmp")]
[InlineData("test.cur")]
[InlineData("test.exr")]
[InlineData("test.gif")]
[InlineData("test.heif")]
[InlineData("test.ico")]
[InlineData("test.jpg")]
[InlineData("test.pbm")]
[InlineData("test.png")]
[InlineData("test.qoi")]
[InlineData("test.tga")]
[InlineData("test.tiff")]
[InlineData("test.webp")]
public async Task ToNonSeekableStream_SyncAndAsync_MatchSeekableStream(string filename)
{
using Image<Rgba32> image = new(32, 24);
image.ProcessPixelRows(accessor =>
{
for (int y = 0; y < accessor.Height; y++)
{
Span<Rgba32> row = accessor.GetRowSpan(y);
for (int x = 0; x < row.Length; x++)
{
row[x] = new Rgba32((byte)(x * 8), (byte)(y * 10), (byte)((x + y) * 4));
}
}
});

IImageEncoder encoder = image.DetectEncoder(filename);

using MemoryStream seekable = new();
image.Save(seekable, encoder);

using MemoryStream syncTarget = new();
image.Save(new NonSeekableStream(syncTarget), encoder);

using MemoryStream asyncTarget = new();
await image.SaveAsync(new NonSeekableStream(asyncTarget), encoder, TestContext.Current.CancellationToken);

byte[] expected = seekable.ToArray();
Assert.Equal(expected, syncTarget.ToArray());
Assert.Equal(expected, asyncTarget.ToArray());
}

[Fact]
public void ThrowsWhenDisposed()
{
Expand Down
Loading