-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathColorStreamManager.cs
More file actions
72 lines (56 loc) · 2.36 KB
/
ColorStreamManager.cs
File metadata and controls
72 lines (56 loc) · 2.36 KB
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
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Kinect;
using System;
using Kinect.Toolbox.Record;
namespace Kinect.Toolbox
{
public class ColorStreamManager : Notifier, IStreamManager
{
public WriteableBitmap Bitmap { get; private set; }
int[] yuvTemp;
static double Clamp(double value)
{
return Math.Max(0, Math.Min(value, 255));
}
static int ConvertFromYUV(byte y, byte u, byte v)
{
byte b = (byte)Clamp(1.164 * (y - 16) + 2.018 * (u - 128));
byte g = (byte)Clamp(1.164 * (y - 16) - 0.813 * (v - 128) - 0.391 * (u - 128));
byte r = (byte)Clamp(1.164 * (y - 16) + 1.596 * (v - 128));
return (r << 16) + (g << 8) + b;
}
public void Update(ReplayColorImageFrame frame)
{
var pixelData = new byte[frame.PixelDataLength];
frame.CopyPixelDataTo(pixelData);
if (Bitmap == null)
{
Bitmap = new WriteableBitmap(frame.Width, frame.Height,
96, 96, PixelFormats.Bgr32, null);
}
int stride = Bitmap.PixelWidth * Bitmap.Format.BitsPerPixel / 8;
Int32Rect dirtyRect = new Int32Rect(0, 0, Bitmap.PixelWidth, Bitmap.PixelHeight);
if (frame.Format == ColorImageFormat.RawYuvResolution640x480Fps15)
{
if (yuvTemp == null)
yuvTemp = new int[frame.Width * frame.Height];
int current = 0;
for (int uyvyIndex = 0; uyvyIndex < pixelData.Length; uyvyIndex += 4)
{
byte u = pixelData[uyvyIndex];
byte y1 = pixelData[uyvyIndex + 1];
byte v = pixelData[uyvyIndex + 2];
byte y2 = pixelData[uyvyIndex + 3];
yuvTemp[current++] = ConvertFromYUV(y1, u, v);
yuvTemp[current++] = ConvertFromYUV(y2, u, v);
}
Bitmap.WritePixels(dirtyRect, yuvTemp, stride, 0);
}
else
Bitmap.WritePixels(dirtyRect, pixelData, stride, 0);
RaisePropertyChanged(() => Bitmap);
}
}
}