Skip to content

Commit f24b02f

Browse files
authored
Merge pull request #1999 from SixLabors/af/contiguous-load
Respect PreferContiguousImageBuffers in decoders
2 parents a1da24a + d181b77 commit f24b02f

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData)
111111
this.pixelRowsPerStep = majorVerticalSamplingFactor * blockPixelHeight;
112112

113113
// pixel buffer for resulting image
114-
this.pixelBuffer = allocator.Allocate2D<TPixel>(frame.PixelWidth, frame.PixelHeight);
114+
this.pixelBuffer = allocator.Allocate2D<TPixel>(
115+
frame.PixelWidth,
116+
frame.PixelHeight,
117+
this.configuration.PreferContiguousImageBuffers);
115118
this.paddedProxyPixelRow = allocator.Allocate<TPixel>(frame.PixelWidth + 3);
116119

117120
// component processors from spectral to Rgba32

src/ImageSharp/Image.Decode.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ internal static Image<TPixel> CreateUninitialized<TPixel>(
3737
ImageMetadata metadata)
3838
where TPixel : unmanaged, IPixel<TPixel>
3939
{
40-
Buffer2D<TPixel> uninitializedMemoryBuffer =
41-
configuration.MemoryAllocator.Allocate2D<TPixel>(width, height);
40+
Buffer2D<TPixel> uninitializedMemoryBuffer = configuration.MemoryAllocator.Allocate2D<TPixel>(
41+
width,
42+
height,
43+
configuration.PreferContiguousImageBuffers);
4244
return new Image<TPixel>(configuration, uninitializedMemoryBuffer.FastMemoryGroup, width, height, metadata);
4345
}
4446

tests/ImageSharp.Tests/Image/LargeImageIntegrationTests.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
5+
using System.IO;
56
using Microsoft.DotNet.RemoteExecutor;
7+
using SixLabors.ImageSharp.Advanced;
8+
using SixLabors.ImageSharp.Formats;
69
using SixLabors.ImageSharp.Memory;
710
using SixLabors.ImageSharp.PixelFormats;
811
using SixLabors.ImageSharp.Processing;
@@ -32,9 +35,40 @@ static void RunTest()
3235
Configuration configuration = Configuration.Default.Clone();
3336
configuration.PreferContiguousImageBuffers = true;
3437

35-
using var image = new Image<Rgba32>(configuration, 8192, 4096);
38+
using var image = new Image<Rgba32>(configuration, 2048, 2048);
3639
Assert.True(image.DangerousTryGetSinglePixelMemory(out Memory<Rgba32> mem));
37-
Assert.Equal(8192 * 4096, mem.Length);
40+
Assert.Equal(2048 * 2048, mem.Length);
41+
}
42+
}
43+
44+
[Theory]
45+
[InlineData("bmp")]
46+
[InlineData("png")]
47+
[InlineData("jpeg")]
48+
[InlineData("gif")]
49+
[InlineData("tiff")]
50+
[InlineData("webp")]
51+
public void PreferContiguousImageBuffers_LoadImage_BufferIsContiguous(string formatOuter)
52+
{
53+
// Run remotely to avoid large allocation in the test process:
54+
RemoteExecutor.Invoke(RunTest, formatOuter).Dispose();
55+
56+
static void RunTest(string formatInner)
57+
{
58+
Configuration configuration = Configuration.Default.Clone();
59+
configuration.PreferContiguousImageBuffers = true;
60+
IImageEncoder encoder = configuration.ImageFormatsManager.FindEncoder(
61+
configuration.ImageFormatsManager.FindFormatByFileExtension(formatInner));
62+
string dir = TestEnvironment.CreateOutputDirectory(".Temp");
63+
string path = Path.Combine(dir, $"{Guid.NewGuid().ToString()}.{formatInner}");
64+
using (Image<Rgba32> temp = new(2048, 2048))
65+
{
66+
temp.Save(path, encoder);
67+
}
68+
69+
using var image = Image.Load<Rgba32>(configuration, path);
70+
File.Delete(path);
71+
Assert.Equal(1, image.GetPixelMemoryGroup().Count);
3872
}
3973
}
4074

0 commit comments

Comments
 (0)