#============================================================================== # # Project: SharpImage # Module: AddLabel.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 relevent class import Script from Script import * class AddLabelScript(ScriptObject): # ------------------------------------------------------------------------- Name = "AddLabel" Help = """Displays a form allowing the addition of a coloured label to the input image.""" Parameters = """(string) Label = the name search pattern for the label image. (\"Label\")""" Renderer = None Input = None Default = None Label = String( "Label" ) Form = None # ------------------------------------------------------------------------- def Run(self): """ The entry-point for this script. """ self.Initialise() # NOTE: DoWork() will be invoked when Continue is clicked. # NOTE: Finalise() will be invoked when FinishedWork() is called. def Initialise(self): """ Initialise the environment for running this script. """ """ NOTE: This function is invoked on the main UI thread. """ ScriptObject.Initialise(self) self.Renderer = self.ParentApplication.CurrentRenderer self.Input = self.Renderer.Inputs[0] if (self.Default != None): self.Label = self.Default self.Label = self.GetOpenImageMatchingPattern( self.Label ) if (self.Label == None): raise FileNotFoundException( "Could not find Label." ) self.Form = siFormAddLabel( self.ParentApplication, self.Input.PixelType ) self.Form.Continue += self.Continue self.Form.Cancel += self.Cancel self.ParentApplication.AddTool( self.Form ) self.ParentApplication.SetApplicationAsReady(0) def DoWork(self): """ Perform the main functions of the script. """ """ NOTE: This function is invoked on the main UI thread. """ # Setup LUT if (String.Compare( self.Form.LabelInfo.Name, "Single Color" ) == 0): lut = siGdiLookupTable( ) lut.SetTableRange( self.Label, 256 ) value = self.Form.LabelInfo.Value.ValueAsD color = self.Form.LabelInfo.Color lut.SetTableToSingleColor( value, color ) elif (String.Compare( self.Form.LabelInfo.Name, "Random Colors" ) == 0): lut = siGdiLookupTable( ) lut.SetTableRange( self.Label, 256 ) lut.SetTableToPsuedoRandomColor( self.Form.LabelInfo.Alpha / 255.0 ) # Add label to Renderer and refresh self.Renderer.AddInputAsLabel( self.Label, lut ) self.Renderer.Repaint() self.Renderer.Focus() # Tell user what we did self.WriteLineToConsole( "Added " + self.Form.LabelInfo.Name + " label" ) self.WriteLineToConsole( "Input=" + self.Input.Name ) self.WriteLineToConsole( "Label=" + self.Label.Name ) # Clean up self.Finalise() def Finalise(self): """ Finalise the environment after running this script. """ """ NOTE: This function is invoked on the main UI thread. """ self.UnregisterEventHandlers() self.ParentApplication.SetApplicationAsReady(0) self.Handlers["ScriptFinalised"]() def Continue(self, sender, args): """ Allow processing to continue. """ self.DoWork() def Cancel(self, sender, args): """ Cancel the script. """ self.Finalise() def UnregisterEventHandlers(self): """ Unregister all the event handlers. """ if (self.Form != None): self.Form.Continue -= self.Continue self.Form.Cancel -= self.Cancel self.Form = None