#============================================================================== # # Project: SharpImage # Module: SlicePipelineScript.py # Language: IronPython # Author: Dan Mueller # Date: $Date: 2007-07-06 10:57:00 +1000 (Fri, 06 Jul 2007) $ # Revision: $Revision: 2 $ # # Copyright (c) Queensland University of Technology (QUT) 2007. # All rights reserved. # # This software is distributed WITHOUT ANY WARRANTY; without even # the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR # PURPOSE. See the above copyright notices for more information. # #============================================================================== # Import the base script class import ImageToImageScript from ImageToImageScript import * class SlicePipelineScriptObject(ImageToImageScriptObject): Pipeline = None Form = None Variables = None def AddVariablesToForm(self): """ Add the required variables to the form. Subclasses must override this function. Example: self.Form.AddVariable( "Name", default, min, max, inc ) """ def CreatePipeline(self, isSlice): """ Set self.Pipeline to an instance of an itkPipeline. If isSlice is True the events are watched, otherwise events are NOT watched. Subclasses must override this function. """ def ApplyPipeline(self, isSlice): """ Apply self.Pipeline by setting the input, setting the filter parameters (stored in self.Variables), updating the pipeline, getting the output, and disconnecting the inputs and outputs. If isSlice is True the pipeline is being applied to the current slice only, otherwise the pipeline is being applied to whole image (eg. a 2D or 3D image). Subclasses must override this function. """ def GetRenderer(self): return self.ParentApplication.CurrentRenderer def GetInputAsSlice(self): return self.Renderer.InputsAsSlice[0] def GetInput(self): return self.Renderer.Inputs[0] def Run(self): """ The entry-point for this script. """ try: self.Initialise() self.ParentApplication.SetApplicationAsReady( 0 ) except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) ScriptObject.Finalise( self ) def Initialise(self): """ Initialise the environment for running this script. """ """ NOTE: This function is invoked on the main UI thread. """ try: ThreadedScriptObject.Initialise( self ) # Get the current image slice self.Renderer = self.GetRenderer( ) self.Input = self.GetInputAsSlice( ) self.Output = itkImage.New( self.Input ) # Create pipeline self.CreatePipeline( True ) self.Pipeline.SetInput( self.Input ) # Create Form self.Form = siFormVariableSlider( self.Input ) self.AddVariablesToForm( ) self.Form.Initialise( ) self.Form.VariableChanged += self.VariableChangedHandler self.Form.Continue += self.ContinueHandler self.Form.Cancel += self.CancelHandler self.ParentApplication.AddTool( self.Form ) self.Form.Reset() # Watch Renderer for slice changed event self.Renderer.SliceChanged += self.SliceChangedHandler except Exception, ex: self.HandleException( ex ) self.FinishedWork( False ) ScriptObject.Finalise( self ) def VariableChangedHandler(self, name, variables): """ Handle the self.Form.VariableChanged event. """ self.Variables = variables self.ApplyPipeline( True ) self.ShowOutputAsCurrentSlice( ) def SliceChangedHandler(self, renderer, e): """ Handle the Renderer.SliceChanged event. """ self.Input = self.GetInputAsSlice( ) self.ApplyPipeline( True ) self.ShowOutputAsCurrentSlice( ) def ContinueHandler(self, sender, args): """ Apply the pipeline to whole image (not the slice), and displays the output. """ self.DoWork( ) def CancelHandler(self, sender, args): """ Cancel the script. """ self.StartedWork() self.WriteLineToConsole("Script cancelled by user.") self.Renderer.InitialiseInputs() self.Renderer.Repaint() self.Renderer.Focus() self.UnregisterEventHandlers() self.Form.Close() self.FinishedWork( True ) def ShowOutputAsCurrentSlice(self): """ Show self.Output as the current slice in the Renderer. """ self.Renderer.InputsAsSlice[0] = self.Output self.Renderer.InitialiseInputsAsSlice( ) self.Renderer.Repaint( ) def ShowOutputAsImage(self): """ Show self.Output as an image in the current or a new Renderer. """ if (self.Form.ApplyInPlace): # Show in current Renderer self.Renderer.Inputs[0] = self.Output self.Renderer.InitialiseInputs( ) self.Renderer.Repaint( ) self.Renderer.Focus( ) else: # Show in new Renderer self.Renderer.InitialiseInputs( ) self.Renderer.Repaint( ) self.ParentApplication.ShowImageInNewRenderer( self.Output ) def Finalise(self): """ Finalise the environment after running this script. """ """ NOTE: This function is invoked on the main UI thread. """ # Unregister event handlers and close form self.UnregisterEventHandlers() self.Form.Close() # Clean up self.DisposeOfObject( self.Pipeline ) self.DisposeOfObject( self.Form ) self.ParentApplication.SetApplicationAsReady( 0 ) # Call base Finalise ScriptObject.Finalise( self ) def UnregisterEventHandlers(self): """ Unregister all the event handlers. """ if (self.Renderer != None): self.Renderer.SliceChanged -= self.SliceChangedHandler if (self.Form != None): self.Form.VariableChanged -= self.VariableChangedHandler self.Form.Continue -= self.ContinueHandler self.Form.Cancel -= self.CancelHandler def ThreadedDoWork(self): """ Apply the pipeline to the full image on a background thread. """ try: # Prepare for work self.StartedWork( ) self.Input = self.GetInput( ) self.Output = itkImage.New( self.Input ) self.WriteInputName( ) # Update pipeline self.CreatePipeline( False ) self.Pipeline.SetInput( self.Input ) self.ApplyPipeline( False ) self.ShowOutputAsImage( ) self.WriteOutputName( ) # Signal we are finished self.FinishedWork( True ) except Exception, ex: self.WriteLineToConsole( ex.ToString() ) self.HandleException( ex ) self.FinishedWork( False ) self.Finalise( )