I have a video stream coming in from the Kinect. It's data is packed in a 32bit BGRX format. I would like to move this data directly into a Texture2d but I cant find a matching SurfaceFormat. The closest thing I can find is SurfaceFormat.Color which looks to be 32bit RGBX.
Assuming that there is not a compatible format. What is the quickest way to convert and push t开发者_如何学JAVAhe data to a texture 2d
I was thinking something like this would be good, but it seems to slow down the framerate:
Edit: I changed the algorithm a bit and it seems to be running decently now.
void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)
{
byte x;
for (int i = 0; i < e.ImageFrame.Image.Bits.Length; i += 4)
{
x = e.ImageFrame.Image.Bits[i];
e.ImageFrame.Image.Bits[i] = e.ImageFrame.Image.Bits[i + 2];
e.ImageFrame.Image.Bits[i + 2] = x;
}
canvas.SetData(e.ImageFrame.Image.Bits);
}
This is the fastest thing I have been able to come up with:
void nui_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)
{
byte[] bits = e.ImageFrame.Image.Bits;
for (int i = 0; i < bits.Length; i += 4)
{
frameBuffer[i] = bits[i + 2];
frameBuffer[i + 1] = bits[i + 1];
frameBuffer[i + 2] = bits[i];
}
canvas.SetData(frameBuffer);
}
Without the kinect involved at all I get a framerate of 4200
Moving the bytes directly to the texture2d without correcting the format difference yield: 3100
If you give your self a local variable with e.ImageFrame.Image.Bits and copy the bytes into a preallocated buffer fixing the bytes as you go I get: 2700
The algorithm i have listed in my original question where you swap the bits in place and then send to texture2d yield about 750
So to recap changing to this algorithm bumped the framerate to 2700 up from 750
精彩评论