-
Notifications
You must be signed in to change notification settings - Fork 4
/
DepthStreamManager.cs
90 lines (78 loc) · 3.2 KB
/
DepthStreamManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Kinect;
using Kinect.Toolbox.Record;
namespace Kinect.Toolbox
{
public class DepthStreamManager : Notifier, IStreamManager
{
byte[] depthFrame32;
public WriteableBitmap Bitmap { get; private set; }
public void Update(ReplayDepthImageFrame frame)
{
var pixelData = new short[frame.PixelDataLength];
frame.CopyPixelDataTo(pixelData);
if (depthFrame32 == null)
{
depthFrame32 = new byte[frame.Width * frame.Height * 4];
}
if (Bitmap == null)
{
Bitmap = new WriteableBitmap(frame.Width, frame.Height, 96, 96, PixelFormats.Bgra32, null);
}
ConvertDepthFrame(pixelData);
int stride = Bitmap.PixelWidth * Bitmap.Format.BitsPerPixel / 8;
Int32Rect dirtyRect = new Int32Rect(0, 0, Bitmap.PixelWidth, Bitmap.PixelHeight);
Bitmap.WritePixels(dirtyRect, depthFrame32, stride, 0);
RaisePropertyChanged(() => Bitmap);
}
void ConvertDepthFrame(short[] depthFrame16)
{
for (int i16 = 0, i32 = 0; i16 < depthFrame16.Length && i32 < depthFrame32.Length; i16 ++, i32 += 4)
{
int user = depthFrame16[i16] & 0x07;
int realDepth = (depthFrame16[i16] >> 3);
byte intensity = (byte)(255 - (255 * realDepth / 0x1fff));
depthFrame32[i32] = 0;
depthFrame32[i32 + 1] = 0;
depthFrame32[i32 + 2] = 0;
depthFrame32[i32 + 3] = 255;
switch (user)
{
case 0: // no one
depthFrame32[i32] = (byte)(intensity / 2);
depthFrame32[i32 + 1] = (byte)(intensity / 2);
depthFrame32[i32 + 2] = (byte)(intensity / 2);
break;
case 1:
depthFrame32[i32] = intensity;
break;
case 2:
depthFrame32[i32 + 1] = intensity;
break;
case 3:
depthFrame32[i32 + 2] = intensity;
break;
case 4:
depthFrame32[i32] = intensity;
depthFrame32[i32 + 1] = intensity;
break;
case 5:
depthFrame32[i32] = intensity;
depthFrame32[i32 + 2] = intensity;
break;
case 6:
depthFrame32[i32 + 1] = intensity;
depthFrame32[i32 + 2] = intensity;
break;
case 7:
depthFrame32[i32] = intensity;
depthFrame32[i32 + 1] = intensity;
depthFrame32[i32 + 2] = intensity;
break;
}
}
}
}
}