Add Image<T>.LoadPixelData variant that allows reusing an image object #1472
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
@fredlllll we have an internal pooling mechanism implemented by the type However, based on the memory timeline you posted something might got wrong in your app, but without code/more info I can't advise. A lot depends on image sizes + what operations you do. But first, I suggest to rethink your code based on the article above. |
Beta Was this translation helpful? Give feedback.
-
I've run into a similar form of this while experimenting with using ImageSharp in a Unity project. The idea was to use ImageSharp to load external images, as Unity just exposes a blocking main-thread-only static Texture2D LoadImage<T>(Configuration configuration, Stream input)
where T : unmanaged, IPixel<T>
{
// Identify the image without loading it so we can create the Texture2D with an appropriate size.
IImageInfo info = Image.Identify(configuration, input);
var texture = new Texture2D(info.Width, info.Height, GetTextureFormat<T>(), mipChain: false);
// Unity allows access to the pixel data using GetPixelData<T>, which can
// then be wrapped into a Memory<T> instance using a custom MemoryManager.
NativeArray<T> pixels = texture.GetPixelData<T>(mipLevel: 0);
// There doesn't appear to be a way to load the pixels into the image without needing to
// allocate a new one and then copying the pixels across, which is a slow operation (omitted here).
var image = Image.WrapMemory(configuration, pixels.AsMemory(), info.Width, info.Height, info.Metadata);
return texture;
} As seen above, I have no control over the allocation of the memory for the pixel data and am forced to pay the price of a copy via |
Beta Was this translation helpful? Give feedback.
@fredlllll we have an internal pooling mechanism implemented by the type
ArrayPoolMemoryAllocator
, so normally there is no benefit from bothering with manual reusing/pooling strategies, all you need is to make sure you properly dispose all images being created. Note that the Image's buffer is not the only one being created during processing, there are temporary buffers created+destroyed behind the scenes, so proper pooling is a must for getting this right, you cant achieve it by just reusing the image instance. And again -- pooling is provided out of the box by the library. See the following article for more details:https://docs.sixlabors.com/articles/imagesharp/memorymanagement.html
How…