6

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?
Community
  • 1
  • 1
Tomas
  • 61
  • 2

2 Answers2

0

Camera api for native code is the way to go: http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj571202

you'll get the data directly in your c++ WinRT component code. see an example here : http://library.developer.nokia.com/Community/Wiki/Getting_started_with_the_Camera_APIs_for_native_code

user1968335
  • 276
  • 2
  • 8
  • Your suggestion works only when capturing new images, not for photos or otherwise stored images. – Tomas Aug 19 '13 at 07:53
  • i wonder if you could find a solution for this. I have a byte[] which i assigned to the native component's buffer and thus processed in native. Now, I am stuck at how to bind this to a WriteableBitmap. A similar copying of all bytes to a int[] would work probably but with performance degradation. and i guess there is not a reinterpret_cast thing in .net. – bahti Dec 28 '13 at 20:37
0

You can use an IRandomAccessStream (or it's concrete class InMemoryRandomAccessStream) to exchange raw data between WinRT components. Depending on what you want to achieve, it can be a good option to avoid copy.

Vincent
  • 3,656
  • 1
  • 23
  • 32