Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect PreferContiguousImageBuffers in decoders #1999

Merged
merged 4 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData)
this.pixelRowsPerStep = majorVerticalSamplingFactor * blockPixelHeight;

// pixel buffer for resulting image
this.pixelBuffer = allocator.Allocate2D<TPixel>(frame.PixelWidth, frame.PixelHeight);
this.pixelBuffer = allocator.Allocate2D<TPixel>(
frame.PixelWidth,
frame.PixelHeight,
this.configuration.PreferContiguousImageBuffers);
this.paddedProxyPixelRow = allocator.Allocate<TPixel>(frame.PixelWidth + 3);

// component processors from spectral to Rgba32
Expand Down
6 changes: 4 additions & 2 deletions src/ImageSharp/Image.Decode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ internal static Image<TPixel> CreateUninitialized<TPixel>(
ImageMetadata metadata)
where TPixel : unmanaged, IPixel<TPixel>
{
Buffer2D<TPixel> uninitializedMemoryBuffer =
configuration.MemoryAllocator.Allocate2D<TPixel>(width, height);
Buffer2D<TPixel> uninitializedMemoryBuffer = configuration.MemoryAllocator.Allocate2D<TPixel>(
width,
height,
configuration.PreferContiguousImageBuffers);
return new Image<TPixel>(configuration, uninitializedMemoryBuffer.FastMemoryGroup, width, height, metadata);
}

Expand Down
37 changes: 36 additions & 1 deletion tests/ImageSharp.Tests/Image/LargeImageIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// Licensed under the Apache License, Version 2.0.

using System;
using System.IO;
using Microsoft.DotNet.RemoteExecutor;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
Expand Down Expand Up @@ -32,12 +35,44 @@ static void RunTest()
Configuration configuration = Configuration.Default.Clone();
configuration.PreferContiguousImageBuffers = true;

using var image = new Image<Rgba32>(configuration, 8192, 4096);
using var image = new Image<Rgba32>(configuration, 2048, 2048);
Assert.True(image.DangerousTryGetSinglePixelMemory(out Memory<Rgba32> mem));
Assert.Equal(8192 * 4096, mem.Length);
antonfirsov marked this conversation as resolved.
Show resolved Hide resolved
}
}

[Theory]
[InlineData("bmp")]
[InlineData("png")]
[InlineData("jpeg")]
[InlineData("gif")]
[InlineData("tiff")]
[InlineData("webp")]
public void PreferContiguousImageBuffers_LoadImage_BufferIsContiguous(string formatOuter)
{
// Run remotely to avoid large allocation in the test process:
// RemoteExecutor.Invoke(RunTest).Dispose();
RunTest(formatOuter);

static void RunTest(string formatInner)
{
Configuration configuration = Configuration.Default.Clone();
configuration.PreferContiguousImageBuffers = true;
IImageEncoder encoder = configuration.ImageFormatsManager.FindEncoder(
configuration.ImageFormatsManager.FindFormatByFileExtension(formatInner));
string dir = TestEnvironment.CreateOutputDirectory(".Temp");
string path = Path.Combine(dir, $"{Guid.NewGuid().ToString()}.{formatInner}");
using (Image<Rgba32> temp = new(2048, 2048))
{
temp.Save(path, encoder);
}

using var image = Image.Load<Rgba32>(configuration, path);
File.Delete(path);
Assert.Equal(1, image.GetPixelMemoryGroup().Count);
}
}

[Theory]
[WithBasicTestPatternImages(width: 10, height: 10, PixelTypes.Rgba32)]
public void DangerousTryGetSinglePixelMemory_WhenImageTooLarge_ReturnsFalse(TestImageProvider<Rgba32> provider)
Expand Down