def on_show_imagej(self):
        """Show the ImageJ user interface
        
        This method shows the ImageJ user interface when the user presses
        the Show ImageJ button.
        """
        logger.debug("Starting ImageJ UI")
        ui_service = ij2.get_ui_service(get_context())
        if ui_service is not None and not ui_service.isVisible():
            if cpprefs.get_headless():
                # Silence the auto-updater in the headless preferences
                #
                ij2.update_never_remind()

            ui_service.createUI()
        elif ui_service is not None:
            ui = ui_service.getDefaultUI()
            J.execute_runnable_in_main_thread(
                J.run_script(
                    """new java.lang.Runnable() {
                run: function() { 
                    ui.getApplicationFrame().setVisible(true); }}""",
                    dict(ui=ui),
                ),
                True,
            )
Example #2
0
def set_current_image(imagej_obj):
    '''Set the currently active window'''
    imagej_obj.show()
    image_window = imagej_obj.getWindow()
    J.execute_runnable_in_main_thread(J.run_script(
        """new java.lang.Runnable() {
        run:function() { Packages.ij.WindowManager.setCurrentWindow(w); }}
        """, dict(w=image_window)), synchronous=True)
Example #3
0
 def setActiveDisplay(self, display):
     '''Make this the active display'''
     # Note: has to be run in GUI thread on mac
     r = J.run_script(
         """new java.lang.Runnable() {
             run:function() { displayService.setActiveDisplay(display); }
         }
         """, dict(displayService=self.o, display=display.o))
     J.execute_runnable_in_main_thread(r, True)
Example #4
0
 def setActiveDisplay(self, display):
     '''Make this the active display'''
     # Note: has to be run in GUI thread on mac
     r = J.run_script(
         """new java.lang.Runnable() {
             run:function() { displayService.setActiveDisplay(display); }
         }
         """, dict(displayService=self.o, display=display.o))
     J.execute_runnable_in_main_thread(r, True)
Example #5
0
 def createUIS(self, arg):
     '''Create the ImageJ UI with a string argument'''
     r = J.run_script(
         """new java.lang.Runnable() {
             run:function() {
                 uiService.showUI(arg);
             }
         };
         """, dict(uiService=self.o, arg=arg))
     J.execute_runnable_in_main_thread(r, True)
Example #6
0
 def createUIS(self, arg):
     '''Create the ImageJ UI with a string argument'''
     r = J.run_script(
         """new java.lang.Runnable() {
             run:function() {
                 uiService.showUI(arg);
             }
         };
         """, dict(uiService=self.o, arg=arg))
     J.execute_runnable_in_main_thread(r, True)
Example #7
0
 def close(self):
     '''Close the display
     
     This is run in the UI thread with synchronization.
     '''
     r = J.run_script(
         """new java.lang.Runnable() {
         run: function() { display.close(); }
         };
         """, dict(display=self.o))
     J.execute_runnable_in_main_thread(r, True)
Example #8
0
def execute_macro(macro_text):
    '''Execute a macro in ImageJ
    
    macro_text - the macro program to be run
    '''
    interp = J.make_instance("ij/macro/Interpreter","()V");
    J.execute_runnable_in_main_thread(J.run_script(
        """new java.lang.Runnable() {
        run: function() {
            interp.run(macro_text);
        }}""", dict(interp=interp, macro_text=macro_text)), synchronous=True)
Example #9
0
 def close(self):
     '''Close the display
     
     This is run in the UI thread with synchronization.
     '''
     r = J.run_script(
         """new java.lang.Runnable() {
         run: function() { display.close(); }
         };
         """, dict(display=self.o))
     J.execute_runnable_in_main_thread(r, True)
Example #10
0
def set_temp_current_image(imagej_obj):
    '''Set the temporary current image for the UI thread'''
    script = """
    new java.lang.Runnable() {
        run: function() {
            Packages.ij.WindowManager.setTempCurrentImage(ip);
        }
    };
    """
    J.execute_runnable_in_main_thread(
        J.run_script(script, dict(ip = imagej_obj.o)), True)
Example #11
0
def show_imagej():
    '''Show the ImageJ user interface'''
    r = J.run_script("""new java.lang.Runnable() {
        run: function() {
            var imageJ = Packages.ij.IJ.getInstance();
            if (imageJ == null) {
                imageJ = Packages.ij.ImageJ();
            }
            imageJ.setVisible(true);
            imageJ.toFront();
        }
    };""", class_loader=get_user_loader())
    J.execute_runnable_in_main_thread(r, True)
Example #12
0
 def createUI(self):
     '''Create the ImageJ UI'''
     #
     # This has to be done via a future in order
     # for CP to run the Mac event loop.
     #
     r = J.run_script(
         """new java.lang.Runnable() {
             run:function() {
                 uiService.showUI();
             }
         };
         """, dict(uiService=self.o))
     J.execute_runnable_in_main_thread(r, True)
Example #13
0
 def createUI(self):
     '''Create the ImageJ UI'''
     #
     # This has to be done via a future in order
     # for CP to run the Mac event loop.
     #
     r = J.run_script(
         """new java.lang.Runnable() {
             run:function() {
                 uiService.showUI();
             }
         };
         """, dict(uiService=self.o))
     J.execute_runnable_in_main_thread(r, True)
Example #14
0
 def inject_image(self, pixels, name=''):
     '''inject an image into ImageJ for processing'''
     ij_processor = ijiproc.make_image_processor(
         (pixels * 255.0).astype('float32'))
     script = """
     new java.lang.Runnable() {
         run: function() {
             var imp = Packages.ij.ImagePlus(name, ij_processor);
             imp.show();
             Packages.ij.WindowManager.setCurrentWindow(imp.getWindow());
         }};"""
     r = J.run_script(script, bindings_in = {
         "name":name,
         "ij_processor": ij_processor})
     J.execute_runnable_in_main_thread(r, True)
Example #15
0
def select_overlay(display, overlay, select=True):
    '''Select or deselect an overlay
    
    display - the overlay's display
    
    overlay - the overlay to select
    '''
    for view in J.get_collection_wrapper(display, fn_wrapper = wrap_data_view):
        if J.call(overlay, "equals", "(Ljava/lang/Object;)Z", view.getData()):
            J.execute_runnable_in_main_thread(J.run_script(
                """new java.lang.Runnable() {
                    run: function() { view.setSelected(select);}
                   }""", dict(view = view.o, select=select)), synchronous=True)
            break
    else:
        logger.info("Failed to select overlay")
Example #16
0
def select_overlay(display, overlay, select=True):
    '''Select or deselect an overlay
    
    display - the overlay's display
    
    overlay - the overlay to select
    '''
    for view in J.get_collection_wrapper(display, fn_wrapper=wrap_data_view):
        if J.call(overlay, "equals", "(Ljava/lang/Object;)Z", view.getData()):
            J.execute_runnable_in_main_thread(J.run_script(
                """new java.lang.Runnable() {
                    run: function() { view.setSelected(select);}
                   }""", dict(view=view.o, select=select)),
                                              synchronous=True)
            break
    else:
        logger.info("Failed to select overlay")
Example #17
0
def set_current_image(imagej_obj):
    '''Set the currently active window
    
    imagej_obj - an ImagePlus to become the current image
    '''
    J.execute_runnable_in_main_thread(J.run_script(
        """new java.lang.Runnable() {
            run:function() {
                var w = imp.getWindow();
                if (w == null) {
                    imp.show();
                } else {
                    Packages.ij.WindowManager.setCurrentWindow(w);
                }
            }
        }
        """, dict(imp=imagej_obj.o)), synchronous=True)
Example #18
0
 def show(self, message = None):
     '''Show the window associated with this image
     
     message - optional message to display
     '''
     if message is None:
         script, bindings_in = """
         new java.lang.Runnable() {
             run: function() { o.show(); }
         };""", dict(o=self.o)
     else:
         script, bindings_in = """
         new java.lang.Runnable() {
             run: function() { o.show(message); }
         };""", dict(o=self.o, message=message)
     J.execute_runnable_in_main_thread(J.run_script(
         script, bindings_in = bindings_in), True)
Example #19
0
def execute_macro(macro_text):
    """Execute a macro in ImageJ
    
    macro_text - the macro program to be run
    """
    script = """
    new java.lang.Runnable() {
        run: function() {
            importClass(Packages.ij.IJ, Packages.ij.ImageJ);
            importClass(Packages.ij.macro.Interpreter);
            var imagej = IJ.getInstance();
            var interpreter = Interpreter();
            if (imagej == null) {
                imagej = ImageJ();
            }
            imagej.setVisible(true);
            interpreter.run(macro);
        }
    };"""
    runnable = J.run_script(script, bindings_in={"macro": macro_text}, class_loader=get_user_loader())
    J.execute_runnable_in_main_thread(runnable, True)
Example #20
0
    def on_show_imagej(self):
        '''Show the ImageJ user interface
        
        This method shows the ImageJ user interface when the user presses
        the Show ImageJ button.
        '''
        logger.debug("Starting ImageJ UI")
        ui_service = ij2.get_ui_service(get_context())
        if ui_service is not None and not ui_service.isVisible():
            if cpprefs.get_headless():
                # Silence the auto-updater in the headless preferences
                #
                ij2.update_never_remind()

            ui_service.createUI()
        elif ui_service is not None:
            ui = ui_service.getDefaultUI()
            J.execute_runnable_in_main_thread(
                J.run_script(
                    """new java.lang.Runnable() {
                run: function() { 
                    ui.getApplicationFrame().setVisible(true); }}""",
                    dict(ui=ui)), True)
Example #21
0
def execute_command(command, options=None):
    """Execute the named command within ImageJ"""
    r = J.run_script(
        """
    new java.lang.Runnable() {
        run: function() { 
            importClass(Packages.ij.IJ, Packages.ij.ImageJ);
            var imagej = IJ.getInstance();
            if (imagej == null) {
                imagej = ImageJ();
            }
            imagej.setVisible(true);
            if (options==null) {
                IJ.run(command);
            } else {
                IJ.run(command, options);
            }
        }
    };""",
        bindings_in={"command": command, "options": options},
        class_loader=get_user_loader(),
    )
    J.execute_runnable_in_main_thread(r, True)
Example #22
0
 def test_06_02_execute_synch_main(self):
     J.execute_runnable_in_main_thread(J.run_script(
         "new java.lang.Runnable() { run:function() {}};"), True)
Example #23
0
 def hide(self):
     '''Hide the ImageWindow associated with this image'''
     J.execute_runnable_in_main_thread(J.run_script(
         """new java.lang.Runnable() { run: function() { o.hide(); }};""",
         dict(o=self.o)), True)
Example #24
0
 def hide(self):
     J.execute_runnable_in_main_thread(J.run_script("""
     new java.lang.Runnable() {
     run: function() { o.hide(); }}""", dict(o=self.o)), synchronous=True)
Example #25
0
    def execute_advanced_command(self, workspace, command, d):
        '''Execute an advanced command

        command - name of the command
        d - dictionary to be used to find settings
        '''
        wants_display = workspace.frame is not None
        if wants_display:
            workspace.display_data.input_images = input_images = []
            workspace.display_data.output_images = output_images = []
        key = command.get_unicode_value()
        if bioformats.USE_IJ2:
            node = command.get_selected_leaf()
            module_info = node[2]
            module = IJ2.wrap_module(module_info.createModule())
            context = self.get_context()
            display_service = IJ2.get_display_service(context)
                
            display_dictionary = {}
            for setting, module_item in d[key]:
                field_type = module_item.getType()
                if isinstance(setting, cps.ImageNameProvider):
                    continue
                if field_type == IJ2.FT_BOOL:
                    value = J.make_instance("java/lang/Boolean",
                                            "(Z)V", setting.value)
                elif field_type == IJ2.FT_INTEGER:
                    value = J.make_instance("java/lang/Integer",
                                            "(I)V", setting.value)
                elif field_type == IJ2.FT_FLOAT:
                    value = J.make_instance("java/lang/Double",
                                            "(D)V", setting.value)
                elif field_type == IJ2.FT_STRING:
                    value = setting.value
                elif field_type == IJ2.FT_COLOR:
                    value = IJ2.make_color_rgb_from_html(setting.value)
                elif field_type == IJ2.FT_IMAGE:
                    image_name = setting.value
                    image = workspace.image_set.get_image(image_name)
                    dataset = IJ2.create_dataset(image.pixel_data,
                                                 setting.value)
                    display = display_service.createDisplay(dataset)
                    if image.has_mask:
                        overlay = IJ2.create_overlay(image.mask)
                        display.displayOverlay(overlay)
                    value = display
                    display_dictionary[module_item.getName()] = display
                    if wants_display:
                        input_images.append((image_name, image.pixel_data))
                module.setInput(module_item.getName(), value)
            module_service = IJ2.get_module_service(context)
            module_service.run(module)
            for setting, module_item in d[key]:
                if isinstance(setting, cps.ImageNameProvider):
                    name = module_item.getName()
                    output_name = setting.value
                    if display_dictionary.has_key(name):
                        display = display_dictionary[name]
                    else:
                        display = IJ2.wrap_display(module.getOutput(name))
                    ds = display_service.getActiveDataset(display)
                    pixel_data = ds.get_pixel_data()
                    image = cpi.Image(pixel_data)
                    workspace.image_set.add(output_name, image)
                    if wants_display:
                        output_images.append((output_name, pixel_data))
            for display in display_dictionary.values():
                panel = IJ2.wrap_display_panel(display.getDisplayPanel())
                panel.close()
        else:
            from imagej.imageplus import make_imageplus_from_processor
            from imagej.imageplus import get_imageplus_wrapper
            from imagej.imageprocessor import make_image_processor
            from imagej.imageprocessor import get_image
            
            command = command.value
            settings = d[command]
            classname = self.get_cached_commands()[command]
            plugin = M.get_plugin(classname)
            fp_in = P.get_input_fields_and_parameters(plugin)
            result = []
            image_set = workspace.image_set
            assert isinstance(image_set, cpi.ImageSet)
            for (field, parameter), setting in zip(fp_in, settings[:len(fp_in)]):
                field_type = P.get_field_type(field)
                label = parameter.label() or ""
                if field_type == P.FT_IMAGE:
                    image_name = setting.value
                    image = workspace.image_set.get_image(image_name,
                                                          must_be_grayscale = True)
                    pixel_data = (image.pixel_data * 255.0).astype(np.float32)
                    if wants_display:
                        input_images.append((image_name, pixel_data / 255.0))
                    processor = make_image_processor(pixel_data)
                    image_plus = make_imageplus_from_processor(image_name, processor)
                    field.set(plugin, image_plus)
                    del image_plus
                    del processor
                elif field_type == P.FT_INTEGER:
                    field.setInt(plugin, setting.value)
                elif field_type == P.FT_FLOAT:
                    field.setFloat(plugin, setting.value)
                elif field_type == P.FT_BOOL:
                    field.setBoolean(plugin, setting.value)
                else:
                    field.set(plugin, setting.value)
            #
            # There are two ways to run this:
            # * Batch - just call plugin.run()
            # * Interactive - use PlugInFunctions.runInteractively
            #
            if self.pause_before_proceeding:
                J.execute_runnable_in_main_thread(J.run_script(
                    """new java.lang.Runnable() { run:function() {
                        importClass(Packages.imagej.plugin.PlugInFunctions);
                        PlugInFunctions.runInteractively(plugin);
                    }};""", dict(plugin=plugin)), True)
            else:
                J.execute_runnable_in_main_thread(plugin, True)
            setting_idx = len(fp_in)
            fp_out = P.get_output_fields_and_parameters(plugin)
            for field, parameter in fp_out:
                field_type = P.get_field_type(field)
                if field_type == P.FT_IMAGE:
                    image_name = settings[setting_idx].value
                    setting_idx += 1
                    image_plus = get_imageplus_wrapper(field.get(plugin))
                    processor = image_plus.getProcessor()
                    pixel_data = get_image(processor).astype(np.float32) / 255.0
                    if wants_display:
                        output_images.append((image_name, pixel_data))
                    image = cpi.Image(pixel_data)
                    image_set.add(image_name, image)
Example #26
0
 def hide(self):
     J.execute_runnable_in_main_thread(J.run_script(
         """
     new java.lang.Runnable() {
     run: function() { o.hide(); }}""", dict(o=self.o)),
                                       synchronous=True)
Example #27
0
 def test_06_02_execute_synch_main(self):
     J.execute_runnable_in_main_thread(
         J.run_script("new java.lang.Runnable() { run:function() {}};"),
         True)