I'm trying to find an efficient way to pass an image data buffer to a Windows Runtime Component on Windows Phone 8, minimizing the number of times the buffer data needs to be copied. A similar, but more general, question has been asked before:
See Passing images from Windows (Phone) Runtime Components (c++/cx) to Native c++ Algorithms
Background
An MSDN article on the topic can be found here. It suggests using a WriteableBitmap
that can expose the underlying pixel buffer as an IBuffer
object, which allows the native component to manipulate the data in-place without copying the buffer first.
See How to get access to WriteableBitmap.PixelBuffer pixels with C++?
However, the Windows.UI.Xaml.Media.Imaging
namespace that the WriteableBitmap
is part of is not available for Windows Phone 8, only Windows 8. Instead one can use System.Windows.Media.Imaging.WriteableBitmap
, but it only gives access to the image pixel data in the form of an int[]. One way of converting this to an IBuffer
is:
using System.Windows.Media.Imaging;
using System.Runtime.InteropServices.WindowsRuntime;
private static IBuffer AsBuffer(WriteableBitmap bitmap)
{
int[] p = bitmap.Pixels;
int len = p.Length * 4;
byte[] arr = new byte[len];
Buffer.BlockCopy(p, 0, arr, 0, len);
return arr.AsBuffer(); // Part of System.Runtime.InteropServices.WindowsRuntime
}
but it involves unnecessary copying of the buffer.
On the other hand, passing byte arrays of data has its disadvantages according to this MSDN article. Basically, in the Windows Runtime, parameters are either for input or for output, never both, which means that some copying is necessary in the end.
Sought answer
- How do I efficiently pass the image data through the Windows Runtime layer?
- What RT classes are suitable when passing images and when passing video streams? Simple arrays or more complex buffers?