Loading images as contiguous memory #1258
-
I've been using ImageSharp for quite some time to load images in a graphics library I've been developing. ImageSharp has been a great cross-platform solution for decoding images in different formats and getting a contiguous span of data I can pass to OpenGL and load into a texture. However, in a recent update, the image.GetPixelSpan() function was removed. First, I tried using image.GetPixelRowSpan(0) and that seemed to work, and looking at this commit from ImageSharp.Textures this seemed to be the way to go. But then I saw the image.TryGetSinglePixelSpan() function and sure enough, in my test project that loads a single texture I can get a Span with the entire image as desired. Unfortunately for me, the syntax of this function clearly implies that the memory of images can no longer be guaranteed to be contiguous, which is something I need. Is there a good solution to this? It would sure be nice to have a way to ensure an Image's memory is contiguous and I imagine I'm not the only one having this issue. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Discontiguous buffers have been introduced in #1109 to enable handling very large images. The size of a single contiguous buffer is limited by Unfortunately, at the moment there's no other way than to use the full constructor of |
Beta Was this translation helpful? Give feedback.
Discontiguous buffers have been introduced in #1109 to enable handling very large images. The size of a single contiguous buffer is limited by
ArrayPoolMemoryAllocator.BufferCapacityInBytes
. It's current (arbitrary) default value isint.MaxValue/4
which means that anImage<Rgba32>
below the size of ~134 megapixels (~ 12k x 11k
pixels) will always fit into a single buffer with the default setup. However, we may decide to lower this value, so if you want to make sure your images fit into a single buffer, I'd suggest to create a customArrayPoolMemoryAllocator
instance, and assign it toConfiguration.Default.MemoryAllocator
(or to the same property on your customConfiguration
instance).Unf…