Esempio n. 1
0
        private void UpdatePropertyView(double x, double y, uint width, uint height, EventTypePluginData data)
        {
            Debug.Assert(data != null);

            bool doEvent = false;

            lock (this.lockObj)
            {
                if (this.selectedData != data)
                {
                    doEvent           = true;
                    this.selectedData = data;
                }

                data.selected2DPixelX     = 0;
                data.selected2DPixelY     = 0;
                data.selected2DPixelColor = Colors.Black;

                if (width == data.imageWidth)
                {
                    data.selected2DPixelX = (uint)x;
                }
                else
                {
                    data.selected2DPixelX = (uint)((x / width) * data.imageWidth);
                }

                if (height == data.imageHeight)
                {
                    data.selected2DPixelY = (uint)y;
                }
                else
                {
                    data.selected2DPixelY = (uint)((y / height) * data.imageHeight);
                }

                ColorPlugin.UpdateSelectedPixelValue(data);
            }

            this.RaisePropertyChanged("Selected2DPixelX");
            this.RaisePropertyChanged("Selected2DPixelY");
            this.RaisePropertyChanged("Selected2DPixelColor");

            if (doEvent)
            {
                this.RaisePropertyChanged("HasSelected2DPixelData");
            }
        }
Esempio n. 2
0
        private viz.Texture UpdateData(EventTypePluginData data)
        {
            Debug.Assert(data != null);

            viz.Texture value = null;

            bool doDataEvent    = false;
            bool doVisibleEvent = false;

            lock (this.lockObj)
            {
                bool updated = false;

                value = data.colorTexture;

                bool isSelectedData = (this.selectedData == data);

                data.visibleChanged = false;

                if (data.needsUpdate && (data.sharedColorFrame != null) && (data.sharedColorFrame.Buffer != IntPtr.Zero) && (data.colorTexture != null))
                {
                    try
                    {
                        if (data.doDecompress)
                        {
                            DateTime now = DateTime.Now;

                            if (now > this.nextDecompress)
                            {
                                data.timer.Stop();

                                doVisibleEvent    = isSelectedData && (!data.hasValidData || data.visibleChanged);
                                data.needsUpdate  = false;
                                data.hasValidData = true;

                                updated = true;

                                if ((data.decompressMemoryStreamBuffer == null) || (data.decompressMemoryStreamBuffer.Length < data.sharedColorFrame.Size))
                                {
                                    data.decompressMemoryStreamBuffer = new byte[data.sharedColorFrame.Size];
                                }

                                uint convertBufferSize = data.imageWidth * data.imageHeight * 3;

                                if ((data.convertColorBuffer == null) || (data.convertColorBuffer.Length < convertBufferSize))
                                {
                                    data.convertColorBuffer = new byte[convertBufferSize];
                                }

                                byte[] rgbBuffer = data.convertColorBuffer;

                                if (this.averageDecompressCount > 10)
                                {
                                    this.averageDecompressCount = 1;
                                    this.stopwatch.Reset();
                                }
                                else
                                {
                                    this.averageDecompressCount++;
                                }

                                this.stopwatch.Start();

                                Marshal.Copy(data.sharedColorFrame.Buffer, data.decompressMemoryStreamBuffer, 0, (int)data.sharedColorFrame.Size);

                                using (Stream imageStream = new MemoryStream(data.decompressMemoryStreamBuffer))
                                {
                                    // this has to run on the UI thread
                                    JpegBitmapDecoder decoder = new JpegBitmapDecoder(imageStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);

                                    BitmapSource bitmapSource = decoder.Frames[0];

                                    bitmapSource.CopyPixels(rgbBuffer, (int)(data.imageWidth * 3), 0);

                                    // expand bgr to rgba
                                    uint imageSize = data.imageWidth * data.imageHeight;

                                    for (uint index = 0; index < imageSize; ++index)
                                    {
                                        uint rgbaIndex = index * 4;
                                        uint bgrIndex  = index * 3;
                                        data.rgbaFrame[rgbaIndex]     = rgbBuffer[bgrIndex + 2];
                                        data.rgbaFrame[rgbaIndex + 1] = rgbBuffer[bgrIndex + 1];
                                        data.rgbaFrame[rgbaIndex + 2] = rgbBuffer[bgrIndex];
                                        data.rgbaFrame[rgbaIndex + 3] = 0xFF;
                                    }
                                }

                                this.stopwatch.Stop();

                                // color decompression takes a LONG time (sometimes upwards of 1/10 of  second), so throttle it

                                double averageDecompressTime = ((double)this.stopwatch.ElapsedMilliseconds) / this.averageDecompressCount;

                                double sinceLastDecompress = (now - this.lastDecompress).TotalMilliseconds;

                                if (sinceLastDecompress > 1000)
                                {
                                    this.nextDecompress = now + TimeSpan.FromMilliseconds(averageDecompressTime * 2);
                                }
                                else
                                {
                                    double minTime = Math.Max(sinceLastDecompress, averageDecompressTime * 2);

                                    this.nextDecompress = now + TimeSpan.FromMilliseconds(minTime);
                                }

                                this.lastDecompress = now;
                            }
                        }
                        else
                        {
                            data.timer.Stop();

                            doVisibleEvent    = isSelectedData && (!data.hasValidData || data.visibleChanged);
                            data.needsUpdate  = false;
                            data.hasValidData = true;
                            updated           = true;

                            unsafe
                            {
                                fixed(byte *p = data.rgbaFrame)
                                {
                                    GCHandle rgbaFrameHandle = GCHandle.Alloc(data.rgbaFrame, GCHandleType.Pinned);

                                    NativeMethods.ConvertYUYVToRGBA(
                                        data.sharedColorFrame.Buffer,
                                        data.sharedColorFrame.Size,
                                        rgbaFrameHandle.AddrOfPinnedObject(),
                                        (uint)data.rgbaFrame.Length);
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // TODO_LOG
                    }
                }

                if (updated)
                {
                    if (ColorPlugin.UpdateSelectedPixelValue(data))
                    {
                        doDataEvent = isSelectedData;
                    }

                    if ((data.colorTexture != null) && (data.rgbaFrame != null))
                    {
                        unsafe
                        {
                            fixed(byte *pFrame = data.rgbaFrame)
                            {
                                data.colorTexture.UpdateData(pFrame, (uint)data.rgbaFrame.Length);
                            }
                        }
                    }
                }
            }

            if (doVisibleEvent)
            {
                this.RaisePropertyChanged("HasSelected2DPixelData");
            }

            if (doDataEvent)
            {
                this.RaisePropertyChanged("Selected2DPixelColor");
            }

            return(value);
        }