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

Representing a pointer to pixel data as byte[] is disadvantageous #1

Open
stakx opened this issue Jul 6, 2016 · 1 comment
Open

Comments

@stakx
Copy link
Owner

stakx commented Jul 6, 2016

Some methods have a "pointer to pixel data" parameter; for example:

  • IWICBitmapFrameEncode.WritePixels
  • IWICBitmapSource.CopyPixels
  • IWICImagingFactory.CreateBitmapFromMemory

These parameters are currently typed as byte[], which is not an ideal choice, for at least two reasons:

  1. Bitmaps can easily occupy a lot of memory. If a bitmap's complete pixel data is represented as a byte[] array, this will cause LOH (Large Object Heap) allocations. These should be avoided if possible.

    One solution would be to represent pointers to pixel data as IntPtr instead, and allocate buffers using Marshal.AllocCoTaskMem. Unmanaged memory blocks are invisible to the garbage collector, therefore there would be no LOH allocation; however some helper methods might be necessary to prevent unmanaged memory leaks.

  2. Some WIC methods that deal with pixel data allow passing a pointer and a "stride" value (which allows jumping to the next row in the pixel buffer). Since byte[] arrays cannot be "offset", there is no way to specify any other starting point than the very first pixel in the array (usually the top left).

    Again, the likely solution is to represent the pixel data buffer as IntPtr, and then possibly providing overloads for byte*, and/or byte[], and/or ArraySegment<byte>. Need to check what makes the most sense in practical scenarios.

@stakx
Copy link
Owner Author

stakx commented Dec 30, 2017

A nice solution might be to define the actual interface in terms of IntPtr, then provide extension methods on top of that that translate the raw memory pointer to a Span<byte>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant