<commit_before>﻿using System;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Kinect;
using Utility;

namespace Kinect3.DepthStream
{
	public partial class MainWindow
	{
		//todo: near vs far, skeleton on or off 

		private ImageSource _imageSource;
		const float MaxDepthDistance = 4095; // max value returned
		const float MinDepthDistance = 850; // min value returned
		const float MaxDepthDistanceOffset = MaxDepthDistance - MinDepthDistance;


		public ImageSource ImageSource
		{
			get { return _imageSource; }
			set
			{
				if (value.Equals(_imageSource)) return;
				_imageSource = value;
				PropertyChanged.Raise(() => ImageSource);
			}
		}

		private void Initialize()
		{
			if (_kinectSensor == null)
				return;
			_kinectSensor.AllFramesReady += KinectSensorAllFramesReady;
			_kinectSensor.DepthStream.Range = DepthRange.Near;
			_kinectSensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30);
			_kinectSensor.SkeletonStream.Enable();
			_kinectSensor.Start();
			_kinectSensor.SkeletonStream.EnableTrackingInNearRange = true;
			_kinectSensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated;
			Message = "Kinect connected";
		}

		void KinectSensorAllFramesReady(object sender, AllFramesReadyEventArgs e)
		{
			using (var frame = e.OpenDepthImageFrame())
			{
				if(frame==null)
					return;

				var pixels = GetColoredBytes(frame);
				if(ImageSource==null)
					ImageSource = new WriteableBitmap(frame.Width, frame.Height, 96, 96,
							PixelFormats.Bgr32, null);

				var stride = frame.Width*PixelFormats.Bgr32.BitsPerPixel/8;
				ImageSource = BitmapSource.Create(frame.Width, frame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);
			}
		}

		byte[] GetColoredBytes(DepthImageFrame frame)
		{
			var depthData = new short[frame.PixelDataLength];
			frame.CopyPixelDataTo(depthData);

			//4 (Red, Green, Blue, empty byte)
			var pixels = new byte[frame.Height * frame.Width * 4];

			const int blueIndex = 0;
			const int greenIndex = 1;
			const int redIndex = 2;

			//pick a RGB color based on distance
			for (int depthIndex = 0, colorIndex = 0;
				 depthIndex < depthData.Length && colorIndex < pixels.Length;
				 depthIndex++, colorIndex += 4)
			{
				var player = depthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask;
				var depth = depthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth;

				//.9M or 2.95'
				if (depth <= 900)
				{
					//we are very close
					pixels[colorIndex + blueIndex] = 255;
					pixels[colorIndex + greenIndex] = 0;
					pixels[colorIndex + redIndex] = 0;

				}
				// .9M - 2M or 2.95' - 6.56'
				else if (depth > 900 && depth < 2000)
				{
					//we are a bit further away
					pixels[colorIndex + blueIndex] = 0;
					pixels[colorIndex + greenIndex] = 255;
					pixels[colorIndex + redIndex] = 0;
				}
				// 2M+ or 6.56'+
				else if (depth > 2000)
				{
					//we are the farthest
					pixels[colorIndex + blueIndex] = 0;
					pixels[colorIndex + greenIndex] = 0;
					pixels[colorIndex + redIndex] = 255;
				}

				////equal coloring for monochromatic histogram
				//var intensity = CalculateIntensityFromDepth(depth);
				//pixels[colorIndex + blueIndex] = intensity;
				//pixels[colorIndex + greenIndex] = intensity;
				//pixels[colorIndex + redIndex] = intensity;

				//Color all players "gold"
				if (player > 0)
				{
					pixels[colorIndex + blueIndex] = Colors.Gold.B;
					pixels[colorIndex + greenIndex] = Colors.Gold.G;
					pixels[colorIndex + redIndex] = Colors.Gold.R;
				}

			}

			return pixels;
		}

		public static byte CalculateIntensityFromDepth(int distance)
		{
			//formula for calculating monochrome intensity for histogram
			return (byte)(255 - (255 * Math.Max(distance - MinDepthDistance, 0)
				 / (MaxDepthDistanceOffset)));
		}

	}
}<commit_msg>Depth sample done<commit_after>﻿using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Kinect;
using Utility;

namespace Kinect3.DepthStream
{
	public partial class MainWindow
	{
		private ImageSource _imageSource;

		public ImageSource ImageSource
		{
			get { return _imageSource; }
			set
			{
				if (value.Equals(_imageSource)) return;
				_imageSource = value;
				PropertyChanged.Raise(() => ImageSource);
			}
		}

		private void Initialize()
		{
			if (_kinectSensor == null)
				return;
			_kinectSensor.AllFramesReady += KinectSensorAllFramesReady;
			_kinectSensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30);
			//_kinectSensor.SkeletonStream.Enable();
			_kinectSensor.Start();
			Message = "Kinect connected";
		}

		void KinectSensorAllFramesReady(object sender, AllFramesReadyEventArgs e)
		{
			using (var frame = e.OpenDepthImageFrame())
			{
				if(frame==null)
					return;

				var pixels = GetColoredBytes(frame);
				if(ImageSource==null)
					ImageSource = new WriteableBitmap(frame.Width, frame.Height, 96, 96,
							PixelFormats.Bgr32, null);

				var stride = frame.Width*PixelFormats.Bgr32.BitsPerPixel/8;
				ImageSource = BitmapSource.Create(frame.Width, frame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);
			}
		}

		byte[] GetColoredBytes(DepthImageFrame frame)
		{
			var depthData = new short[frame.PixelDataLength];
			frame.CopyPixelDataTo(depthData);

			//4 (Red, Green, Blue, empty byte)
			var pixels = new byte[frame.Height * frame.Width * 4];

			const int blueIndex = 0;
			const int greenIndex = 1;
			const int redIndex = 2;

			//pick a RGB color based on distance
			for (int depthIndex = 0, colorIndex = 0;
				 depthIndex < depthData.Length && colorIndex < pixels.Length;
				 depthIndex++, colorIndex += 4)
			{
				var player = depthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask;
				var depth = depthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth;

				if (depth <= 900)
				{
					pixels[colorIndex + blueIndex] = 255;
					pixels[colorIndex + greenIndex] = 0;
					pixels[colorIndex + redIndex] = 0;

				}
				else if (depth > 900 && depth < 2000)
				{
					pixels[colorIndex + blueIndex] = 0;
					pixels[colorIndex + greenIndex] = 255;
					pixels[colorIndex + redIndex] = 0;
				}
				else if (depth > 2000)
				{
					pixels[colorIndex + blueIndex] = 0;
					pixels[colorIndex + greenIndex] = 0;
					pixels[colorIndex + redIndex] = 255;
				}

				if (player > 0)
				{
					pixels[colorIndex + blueIndex] = Colors.Gold.B;
					pixels[colorIndex + greenIndex] = Colors.Gold.G;
					pixels[colorIndex + redIndex] = Colors.Gold.R;
				}
			}
			return pixels;
		}
	}
}
{'fraction_non_alphanumeric': 0.1083969465648855, 'fraction_numerical': 0.02351145038167939, 'mean_word_length': 3.307034845496384, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4737242', 'n_tokens_mistral': 2749, 'n_tokens_neox': 2108, 'n_words': 492}