/// <summary> /// this method sourced from: /// http://groups.google.com/group/openni-dev/browse_thread/thread/f3d82e8f2de0a59f/19cb38853f9d2672?lnk=gst&q=draw+rgb+image#19cb38853f9d2672 /// </summary> unsafe byte[] GetImageBytes() { byte[] bmpBytes = null; var imageMD = new ImageMetaData(); this.image.GetMetaData(imageMD); lock (this) { int byteCount = imageMD.XRes * imageMD.YRes * 3; // 3-byte color representation - RGB if (bmpBytes == null || bmpBytes.Length != byteCount) bmpBytes = new byte[byteCount]; fixed(byte *texturePointer = &bmpBytes[0]) { RGB24Pixel *pImage = (RGB24Pixel *)this.image.GetImageMapPtr().ToPointer(); int pointerWalker = 0; for (int y = 0; y < imageMD.YRes; ++y) { for (int x = 0; x < imageMD.XRes; ++x, ++pImage, pointerWalker += 3) { texturePointer[pointerWalker] = pImage->nRed; texturePointer[pointerWalker + 1] = pImage->nGreen; texturePointer[pointerWalker + 2] = pImage->nBlue; } } } } return(bmpBytes); }
//private unsafe void ReaderThread() public unsafe void GetData(object sender, DoWorkEventArgs e) { DepthMetaData depthMD = new DepthMetaData(); ImageMetaData imageMD = new ImageMetaData(); //SceneMetaData sceneMD = new SceneMetaData(); //while (this.ShouldRun) while (!this.CancellationPending) { //if (this.CancellationPending) //this.ShouldRun = false; try { //this.context.WaitOneUpdateAll(this.depth); this.context.WaitAndUpdateAll(); } catch (Exception) { } // if no device connected if (this.depth == null || this.image == null) { return; } this.depth.GetMetaData(depthMD); this.image.GetMetaData(imageMD); // NOTE: interesting to note if this is the same as GetUserPixels //MapData<ushort> sceneMap = this.scene.GetLabelMap(); CalcHist(depthMD); lock (this) { Rectangle rect = new Rectangle(0, 0, this.bitmap.Width, this.bitmap.Height); //BitmapData data = this.bitmap.LockBits(rect, ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); BitmapData data = this.bitmap.LockBits(rect, ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); RGB24Pixel *pImage = (RGB24Pixel *)this.image.GetImageMapPtr().ToPointer(); ushort * pDepth = (ushort *)this.depth.GetDepthMapPtr().ToPointer(); ushort * pLabels = (ushort *)this.userGenerator.GetUserPixels(0).SceneMapPtr.ToPointer(); // set pixels for (int y = 0; y < depthMD.YRes; ++y) { byte *pDest = (byte *)data.Scan0.ToPointer() + y * data.Stride; //for (int x = 0; x < depthMD.XRes; ++x, ++pDepth, ++pLabels, ++pImage, pDest += 3) for (int x = 0; x < depthMD.XRes; ++x, ++pDepth, ++pLabels, ++pImage, pDest += 4) { //pDest[0] = pDest[1] = pDest[2] = 0; pDest[0] = pDest[1] = pDest[2] = pDest[3] = 0; ushort label = *pLabels; //------------------------------------------------- // Grab the pixel according to mode //------------------------------------------------- if (textureMode == TextureMode.DEPTH) { byte pixel = (byte)this.histogram[*pDepth]; pDest[0] = 0; pDest[1] = pixel; pDest[2] = pixel; pDest[3] = 255; // alpha } else if (textureMode == TextureMode.DEPTH_RGB_USER) { if (*pLabels != 0) { Color labelColor = Color.White; if (label != 0) { labelColor = colors[label % ncolors]; } /* * byte pixel = (byte)this.histogram[*pDepth]; * pDest[0] = (byte)(pixel * (labelColor.B / 256.0)); * pDest[1] = (byte)(pixel * (labelColor.G / 256.0)); * pDest[2] = (byte)(pixel * (labelColor.R / 256.0)); */ // grab the rgb pixel for the user // translate from RGB to BGR (windows format) pDest[0] = pImage->nBlue; pDest[1] = pImage->nGreen; pDest[2] = pImage->nRed; pDest[3] = 255; // alpha } else { byte pixel = (byte)this.histogram[*pDepth]; pDest[0] = 0; pDest[1] = pixel; pDest[2] = pixel; pDest[3] = 255; // alpha } } else if (textureMode == TextureMode.RGB) { pDest[0] = pImage->nBlue; pDest[1] = pImage->nGreen; pDest[2] = pImage->nRed; pDest[3] = 255; // alpha } else if (textureMode == TextureMode.RGB_USER_ONLY) { if (*pLabels != 0) { /* * Color labelColor = Color.White; * if (label != 0) * { * labelColor = colors[label % ncolors]; * } */ // grab the rgb pixel for the user // translate from RGB to BGR (windows format) pDest[0] = pImage->nBlue; pDest[1] = pImage->nGreen; pDest[2] = pImage->nRed; pDest[3] = 255; // alpha } else { // set all other pixels to be transparent pDest[0] = 0; pDest[1] = 0; pDest[2] = 0; pDest[3] = 0; // alpha } } /* * if (*pLabels != 0) * { * Color labelColor = Color.White; * if (label != 0) * { * labelColor = colors[label % ncolors]; * } * * //byte pixel = (byte)this.histogram[*pDepth]; * //pDest[0] = (byte)(pixel * (labelColor.B / 256.0)); * //pDest[1] = (byte)(pixel * (labelColor.G / 256.0)); * //pDest[2] = (byte)(pixel * (labelColor.R / 256.0)); * // grab the rgb pixel for the user * // translate from RGB to BGR (windows format) * pDest[0] = pImage->nBlue; * pDest[1] = pImage->nGreen; * pDest[2] = pImage->nRed; * } * else * { * byte pixel = (byte)this.histogram[*pDepth]; * pDest[0] = 0; * pDest[1] = pixel; * pDest[2] = pixel; * } */ } } this.bitmap.UnlockBits(data); Graphics g = Graphics.FromImage(this.bitmap); uint[] users = this.userGenerator.GetUsers(); foreach (uint user in users) { if (this.shouldPrintID) { Point3D com = this.userGenerator.GetCoM(user); com = this.depth.ConvertRealWorldToProjective(com); string label = ""; g.DrawString(label, new Font("Arial", 6), new SolidBrush(anticolors[user % ncolors]), com.X, com.Y); } } } // locked copy this.KinectBitmap.Dispose(); this.KinectBitmap = (Bitmap)this.bitmap.Clone(); // makes all black pixels transparent //if(textureMode == TextureMode.RGB_USER_ONLY) // this.KinectBitmap.MakeTransparent( Color.Black ); // will signal up-stream to come get the bitmap this.ReportProgress(1); /* * if (_frame == 30) * { * //Debug.WriteLine("\t\tNow: " + DateTime.Now.ToString()); * // will signal to come get the bitmap * this.ReportProgress(_frame); * //if (OnKinectTextureReady != null) * // OnKinectTextureReady(new EarthmineEventArgs(null, null, null)); * * _frame = 0; * } * else * _frame++; */ } } // reader-thread
private unsafe void ReaderThread() { DepthMetaData depthMD = new DepthMetaData(); ImageMetaData imageMD = new ImageMetaData(); //SceneMetaData sceneMD = new SceneMetaData(); while (this.shouldRun) { try { //this.context.WaitOneUpdateAll(this.depth); this.context.WaitAndUpdateAll(); } catch (Exception) { } this.depth.GetMetaData(depthMD); this.image.GetMetaData(imageMD); // NOTE: interesting to note if this is the same as GetUserPixels //MapData<ushort> sceneMap = this.scene.GetLabelMap(); CalcHist(depthMD); lock (this) { Rectangle rect = new Rectangle(0, 0, this.bitmap.Width, this.bitmap.Height); BitmapData data = this.bitmap.LockBits(rect, ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); RGB24Pixel *pImage = (RGB24Pixel *)this.image.GetImageMapPtr().ToPointer(); ushort * pDepth = (ushort *)this.depth.GetDepthMapPtr().ToPointer(); ushort * pLabels = (ushort *)this.userGenerator.GetUserPixels(0).SceneMapPtr.ToPointer(); // set pixels for (int y = 0; y < depthMD.YRes; ++y) { byte *pDest = (byte *)data.Scan0.ToPointer() + y * data.Stride; for (int x = 0; x < depthMD.XRes; ++x, ++pDepth, ++pLabels, ++pImage, pDest += 3) { pDest[0] = pDest[1] = pDest[2] = 0; ushort label = *pLabels; if (*pLabels != 0) { Color labelColor = Color.White; if (label != 0) { labelColor = colors[label % ncolors]; } /* * byte pixel = (byte)this.histogram[*pDepth]; * pDest[0] = (byte)(pixel * (labelColor.B / 256.0)); * pDest[1] = (byte)(pixel * (labelColor.G / 256.0)); * pDest[2] = (byte)(pixel * (labelColor.R / 256.0)); */ // grab the rgb pixel for the user // translate from RGB to BGR (windows format) pDest[0] = pImage->nBlue; pDest[1] = pImage->nGreen; pDest[2] = pImage->nRed; } else { byte pixel = (byte)this.histogram[*pDepth]; pDest[0] = 0; pDest[1] = pixel; pDest[2] = pixel; } } } this.bitmap.UnlockBits(data); Graphics g = Graphics.FromImage(this.bitmap); uint[] users = this.userGenerator.GetUsers(); foreach (uint user in users) { if (this.shouldPrintID) { Point3D com = this.userGenerator.GetCoM(user); com = this.depth.ConvertRealWorldToProjective(com); string label = ""; g.DrawString(label, new Font("Arial", 6), new SolidBrush(anticolors[user % ncolors]), com.X, com.Y); } } } this.Invalidate(); } }