protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         lock (this.lockObj)
         {
             RawIrPlugin.DisposeData(this.monitorData, true);
             RawIrPlugin.DisposeData(this.inspectionData, true);
         }
     }
 }
        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.selected2DPixelIntensity = 0;
                data.selected2DPixelDepth     = 0;

                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);
                }

                RawIrPlugin.UpdateSelectedPixelValue(data);
            }

            this.RaisePropertyChanged("Selected2DPixelX");
            this.RaisePropertyChanged("Selected2DPixelY");
            this.RaisePropertyChanged("Selected2DPixelIrIntensity");
            this.RaisePropertyChanged("Selected2DPixelDepth");

            if (doEvent)
            {
                this.RaisePropertyChanged("HasSelected2DPixelData");
            }
        }
        public void UninitializeRender(EventType eventType)
        {
            switch (eventType)
            {
            case EventType.Monitor:
                Debug.Assert(this.monitorData != null);
                RawIrPlugin.DisposeData(this.monitorData, false);
                break;

            case EventType.Inspection:
                Debug.Assert(this.inspectionData != null);
                RawIrPlugin.DisposeData(this.inspectionData, false);
                break;
            }
        }
        public void InitializeRender(EventType eventType, viz.Context context)
        {
            switch (eventType)
            {
            case EventType.Monitor:
                Debug.Assert(this.monitorData != null);
                break;

            case EventType.Inspection:
                Debug.Assert(this.inspectionData != null);

                if (this.inspectionData.sharedRawFrame != null)
                {
                    EventTypePluginData data = this.inspectionData;

                    RawIrPlugin.CreateTextures(this.inspectionData, context);
                    DepthIrEngine depthIrEngine = this.pluginService.DepthIrEngine;

                    if ((depthIrEngine != null) && (data.depthFrame != null) && (data.irFrame != null))
                    {
                        unsafe
                        {
                            fixed(ushort *pDepthFrame = &data.depthFrame[0], pIrFrame = &data.irFrame[0])
                            {
                                depthIrEngine.HandleEvent(data.sharedRawFrame.Buffer, data.sharedRawFrame.Size, pDepthFrame, pIrFrame, data.imageWidth, data.imageHeight);

                                uint imageDataSize = data.imageWidth * data.imageHeight * sizeof(ushort);

                                if (data.rawIrTexture != null)
                                {
                                    data.rawIrTexture.UpdateData((byte *)pIrFrame, imageDataSize);
                                }

                                if (data.depthMap != null)
                                {
                                    data.depthMap.UpdateData(pDepthFrame, imageDataSize);
                                }
                            }
                        }
                    }
                }
                break;
            }
        }
        public void Render3D(EventType eventType, IPluginViewSettings pluginViewSettings, viz.Context context, viz.Texture texture)
        {
            lock (this.lockObj)
            {
                nui.Registration registration = null;
                if (this.pluginService != null)
                {
                    registration = pluginService.GetRegistration(eventType);
                }
                switch (eventType)
                {
                case EventType.Monitor:
                    RawIrPlugin.Render3D(pluginViewSettings, texture, this.monitorData, registration);
                    break;

                case EventType.Inspection:
                    RawIrPlugin.Render3D(pluginViewSettings, texture, this.inspectionData, registration);
                    break;
                }
            }
        }
        private viz.Texture GetTexture(IPluginViewSettings pluginViewSettings, EventTypePluginData data)
        {
            Debug.Assert(data != null);

            viz.Texture value = null;

            lock (this.lockObj)
            {
                RawIrPlugin2DViewSettings raw2DSettingsViewSettings = pluginViewSettings as RawIrPlugin2DViewSettings;
                if (raw2DSettingsViewSettings != null)
                {
                    RawIrPlugin.UpdateData(raw2DSettingsViewSettings, data);

                    if (raw2DSettingsViewSettings.ViewType == RawIrPlugin2DViewSettings.RawIr2DViewType.Ir)
                    {
                        value = data.irTexture;
                    }
                    else
                    {
                        value = data.depthTexture;
                    }
                }
                else
                {
                    RawIrPlugin3DViewSettings raw3DSettingsViewSettings = pluginViewSettings as RawIrPlugin3DViewSettings;
                    if (raw3DSettingsViewSettings != null)
                    {
                        if (raw3DSettingsViewSettings.ViewType == RawIrPlugin3DViewSettings.RawIr3DViewType.Ir)
                        {
                            value = data.irTexture;
                        }
                    }
                }
            }

            return(value);
        }
        private static void Render3D(IPluginViewSettings pluginViewSettings, viz.Texture texture, EventTypePluginData data, nui.Registration registration)
        {
            Debug.Assert(data != null);

            RawIrPlugin3DViewSettings rawPlugin3DViewSettings = pluginViewSettings as RawIrPlugin3DViewSettings;

            if (rawPlugin3DViewSettings != null)
            {
                RawIrPlugin.UpdateData(rawPlugin3DViewSettings, texture, data);

                bool doColor     = false;
                bool doBodyIndex = false;

                if (texture != null)
                {
                    uint textureWidth  = texture.GetWidth();
                    uint textureHeight = texture.GetHeight();

                    doColor = (textureWidth == nui.Constants.STREAM_COLOR_WIDTH) &&
                              (textureHeight == nui.Constants.STREAM_COLOR_HEIGHT);

                    doBodyIndex = (texture.GetTextureFormat() == viz.TextureFormat.B8G8R8A8_UNORM) &&
                                  (textureWidth == nui.Constants.STREAM_IR_WIDTH) &&
                                  (textureHeight == nui.Constants.STREAM_IR_HEIGHT);
                }

                if ((!rawPlugin3DViewSettings.IsSupplyingSurface && (rawPlugin3DViewSettings.ViewType == RawIrPlugin3DViewSettings.RawIr3DViewType.Ir)) ||
                    (rawPlugin3DViewSettings.IsSupplyingSurface && doBodyIndex) ||
                    (!rawPlugin3DViewSettings.IsSupplyingSurface && rawPlugin3DViewSettings.ViewType == RawIrPlugin3DViewSettings.RawIr3DViewType.SurfaceNormal))
                {
                    if (data.depthMap != null)
                    {
                        // special case for body index
                        viz.Effect effect = new viz.Effect()
                        {
                            Direction      = new viz.Vector(0.5f, 0.3f, 1.5f, 0),
                            Ambient        = new viz.Vector(0.0f, 0.0f, 0.0f, 1.0f),
                            Diffuse        = new viz.Vector(0.5f, 0.5f, 0.5f, 1.0f),
                            Specular       = new viz.Vector(1.0f, 1.0f, 1.0f, 1.0f),
                            Power          = 25.0f,
                            EnableLighting = true,
                            EnableTexture  = false,
                        };

                        if ((rawPlugin3DViewSettings.IsSupplyingSurface && doBodyIndex))
                        {
                            if (data.depthMap != null)
                            {
                                data.depthMap.SetMode(viz.DepthVertexMode.SurfaceWithNormal, viz.DepthRampMode.None);
                                effect.EnableTexture = true;
                            }
                        }

                        if (!rawPlugin3DViewSettings.IsSupplyingSurface && (rawPlugin3DViewSettings.ViewType != RawIrPlugin3DViewSettings.RawIr3DViewType.Ir))
                        {
                            texture = null;
                        }

                        data.depthMap.Render(effect, texture);
                    }
                }
                else
                {
                    if (rawPlugin3DViewSettings.IsSupplyingSurface && doColor)
                    {
                        // special case for color
                        if ((registration != null) && (data.depthMap != null) && (data.uvTable != null) && (data.sharedRawFrame != null))
                        {
                            data.depthMap.SetMode(viz.DepthVertexMode.SurfaceWithUV, viz.DepthRampMode.None);

                            IntPtr ptr = data.sharedRawFrame.Buffer;

                            if (ptr != IntPtr.Zero)
                            {
                                registration.Process(ptr, data.uvTable.Buffer);
                            }

                            data.depthMap.UpdateUVTable(data.uvTable.Buffer, data.uvTable.Size);
                        }
                    }

                    if (data.depthMap != null)
                    {
                        viz.Effect effect = new viz.Effect()
                        {
                            EnableTexture = texture != null,
                        };

                        data.depthMap.Render(effect, texture);
                    }
                }
            }
        }
        private void HandleEvent(EventType eventType, KStudioEvent eventObj, EventTypePluginData data)
        {
            Debug.Assert(data != null);

            if (eventObj != null)
            {
                bool doDataEvent    = false;
                bool doVisibleEvent = false;

                if (eventObj.EventStreamDataTypeId == KStudioEventStreamDataTypeIds.RawIr)
                {
                    lock (this.lockObj)
                    {
                        bool isSelectedData = (this.selectedData == data);

                        HGlobalBuffer newSharedFrame = eventObj.GetRetainableEventDataBuffer();
                        doVisibleEvent      = isSelectedData && (newSharedFrame == null) != (data.sharedRawFrame == null);
                        data.sharedRawFrame = newSharedFrame;

                        uint newWidth  = nui.Constants.STREAM_IR_WIDTH;
                        uint newHeight = nui.Constants.STREAM_IR_HEIGHT;

                        if ((data.rawIrTexture == null) || (newWidth != data.imageWidth) || (newHeight != data.imageHeight))
                        {
                            data.imageWidth  = newWidth;
                            data.imageHeight = newHeight;

                            viz.Context context = null;

                            if (this.pluginService != null)
                            {
                                context = this.pluginService.GetContext(eventType);
                            }

                            RawIrPlugin.CreateTextures(data, context);
                        }

                        DepthIrEngine depthIrEngine = this.pluginService.DepthIrEngine;

                        if ((depthIrEngine != null) && (data.depthFrame != null) && (data.irFrame != null))
                        {
                            unsafe
                            {
                                fixed(ushort *pDepthFrame = &data.depthFrame[0], pIrFrame = &data.irFrame[0])
                                {
                                    depthIrEngine.HandleEvent(data.sharedRawFrame.Buffer, data.sharedRawFrame.Size, pDepthFrame, pIrFrame, data.imageWidth, data.imageHeight);

                                    uint imageDataSize = data.imageWidth * data.imageHeight * sizeof(ushort);

                                    if (data.rawIrTexture != null)
                                    {
                                        data.rawIrTexture.UpdateData((byte *)pIrFrame, imageDataSize);
                                    }

                                    if (data.depthMap != null)
                                    {
                                        data.depthMap.UpdateData(pDepthFrame, imageDataSize);
                                    }
                                }
                            }
                        }

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

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

                    if (doDataEvent)
                    {
                        this.RaisePropertyChanged("Selected2DPixelIrIntensity");
                        this.RaisePropertyChanged("Selected2DPixelDepth");
                    }
                }
            }
        }