Class CShowZStackPos { //CShowZStackPos(Object self) Result("\n " + self.ScriptObjectGetClassName() + " object 0x"+self.ScriptObjectGetID().Hex()+" created.") //~CShowZStackPos(Object self) Result("\n " + self.ScriptObjectGetClassName() + " object 0x"+self.ScriptObjectGetID().Hex()+" destroyed.") ImageDisplay disp Component textAnno Number KeyListenerID Number ListenerID Number bShowCenter, bShowCalibrated string fStr // Event Handling Methods ///////////////////////////////////////////////////////////////////////////// void ToggleDisplay( object self ) { if ( !textAnno.ComponentIsValid() ) { // Create the annotation and define its format and position textAnno = NewTextAnnotation(10,10,"", 12) textAnno.ComponentSetForeGroundColor(1,0.8,0) } // Add or remove annotation from display if ( !textAnno.ComponentGetParentComponent().ComponentIsValid() ) disp.ComponentAddChildAtEnd( textAnno ) else textAnno.ComponentRemoveFromParent() } void UpdateTextAnnotation( object self ) { string msg image img := disp.ImageDisplayGetImage() if ( 3 == img.ImageGetNumDimensions() ) { number start, end string unit number origin,scale img.ImageGetDimensionCalibration(2,origin,scale,unit,0) disp.ImageDisplayGetDisplayedLayers( start, end ) if ( bShowCalibrated ) { start = origin + start * scale end = origin + end * scale } if ( start == end ) msg = Format( start, fStr ) else if ( bShowCenter ) msg = "(" + Format((start+end)/2, fStr ) + " +/-" + Format((end-start)/2, fStr ) + ")" else msg = "(" + Format(start, fStr) + " -" + Format(end, fStr) + ")" if ( bShowCalibrated ) msg += " [" + unit + "]" } textAnno.TextAnnotationSetText( msg ) } void OnSlicePropChanged( object self, Number disp_flags, ImageDisplay disp, Number flags1, Number flags2, object slice_id_beg, object slice_id_end ) { self.UpdateTextAnnotation() } // Convenience KEYs ///////////////////////////////////////////////////////////////////////////// Number KeyListenAction(Object self, ImageDisplay disp, Object keydesc ) { number b_keyhandled = 0 If ( keydesc.MatchesKeyDescriptor("q") ) { disp.ImageDisplayRemoveEventListener( ListenerID ) disp.ImageDisplayRemoveKeyHandler( KeyListenerID ) textAnno.ComponentRemoveFromParent() OKDialog( "Stopped Display Listeners" ) b_keyhandled = 1 } else If ( keydesc.MatchesKeyDescriptor("t") ) { self.ToggleDisplay() b_keyhandled = 1 } else If ( keydesc.MatchesKeyDescriptor("u") ) { bShowCalibrated = !bShowCalibrated self.UpdateTextAnnotation() b_keyhandled = 1 } else If ( keydesc.MatchesKeyDescriptor("c") ) { bShowCenter = !bShowCenter self.UpdateTextAnnotation() b_keyhandled = 1 } return b_keyhandled; } Object Launch( object self, image Img ) { if ( !img.ImageIsValid() ) Throw( "Invalid input image." ) disp = img.ImageGetImageDisplay(0) if ( 3 != img.ImageGetNumDimensions() ) Throw( "This script only supports 3D images." ) bShowCenter = 1 // Show Center-Value for ranges bShowCalibrated = 1 // Use Image calibration fStr = "%4.0f" // Defines number formatting // Register DisplayListener to catch when it updates ListenerID = disp.ImageDisplayAddEventListener( self, "slice_property_changed:OnSlicePropChanged" ) // Register KeyListener to provide mechanism to stopp script KeyListenerID = disp.ImageDisplayAddKeyHandler( self, "KeyListenAction" ) // Initial display self.ToggleDisplay() self.UpdateTextAnnotation() string msg = "Added Display Listner." msg += "\nPress to stop." msg += "\nPress to toggle display" msg += "\nPress to toggle calibration" msg += "\nPress to toggle center display" OKDialog( msg ) return self } } //Main call image fImg GetFrontImage(fImg) Alloc(CShowZStackPos).Launch(fImg)