String showDialog() {
   if (defaultDir == null) defaultDir = Menus.getMacrosPath();
   OpenDialog od = new OpenDialog("Install Macros", defaultDir, fileName);
   String name = od.getFileName();
   if (name == null) return null;
   String dir = od.getDirectory();
   if (!(name.endsWith(".txt") || name.endsWith(".ijm"))) {
     IJ.showMessage("Macro Installer", "File name must end with \".txt\" or \".ijm\" .");
     return null;
   }
   fileName = name;
   defaultDir = dir;
   return dir + name;
 }
Esempio n. 2
0
    public void actionPerformed(ActionEvent e) {
      Button b = (Button) e.getSource();
      if (b == null) return;

      boolean imageThere = checkImage();

      if (imageThere) {
        if (b == originalB) {
          reset(imp, ip);
          filteredB.setEnabled(true);
        } else if (b == filteredB) {
          apply(imp, ip);
        } else if (b == sampleB) {
          reset(imp, ip);
          sample();
          apply(imp, ip);
        } else if (b == stackB) {
          applyStack();
        } else if (b == helpB) {
          IJ.showMessage(
              "Help",
              "Threshold Colour  v1.0\n \n"
                  + "Modification of Bob Dougherty's BandPass2 plugin by G.Landini to\n"
                  + "threshold 24 bit RGB images based on Hue, Saturation and Brightness\n"
                  + "or Red, Green and Blue components.\n \n"
                  + "Pass: Band-pass filter (anything within range is displayed).\n \n"
                  + "Stop: Band-reject filter (anything within range is NOT displayed).\n \n"
                  + "Original: Shows the original image and updates the buffer when\n"
                  + " switching to another image.\n \n"
                  + "Filtered: Shows the filtered image.\n \n"
                  + "Stack: Processes the rest of the slices in the stack (if any)\n"
                  + " using the current settings.\n \n"
                  + "Threshold: Shows the object/background in the foreground and\n"
                  + " background colours selected in the ImageJ toolbar.\n \n"
                  + "Invert: Swaps the fore/background colours.\n \n"
                  + "Sample: (experimental) Sets the ranges of the filters based on the\n"
                  + " pixel value componentd in a rectangular, user-defined, ROI.\n \n"
                  + "HSB RGB: Selects HSB or RGB space and resets all the filters.\n \n"
                  + "Note that the \'thresholded\' image is RGB, not 8 bit grey.");
        }
        updatePlot();
        updateLabels();
        imp.updateAndDraw();
      } else {
        IJ.beep();
        IJ.showStatus("No Image");
      }
      notify();
    }
 void install() {
   subMenus.clear();
   if (text != null) {
     Tokenizer tok = new Tokenizer();
     pgm = tok.tokenize(text);
   }
   if (macrosMenu != null) IJ.showStatus("");
   int[] code = pgm.getCode();
   Symbol[] symbolTable = pgm.getSymbolTable();
   int count = 0, token, nextToken, address;
   String name;
   Symbol symbol;
   shortcutsInUse = null;
   inUseCount = 0;
   nShortcuts = 0;
   toolCount = 0;
   macroStarts = new int[MAX_MACROS];
   macroNames = new String[MAX_MACROS];
   boolean isPluginsMacrosMenu = false;
   if (macrosMenu != null) {
     int itemCount = macrosMenu.getItemCount();
     isPluginsMacrosMenu = macrosMenu == Menus.getMacrosMenu();
     int baseCount = isPluginsMacrosMenu ? MACROS_MENU_COMMANDS : Editor.MACROS_MENU_ITEMS;
     if (itemCount > baseCount) {
       for (int i = itemCount - 1; i >= baseCount; i--) macrosMenu.remove(i);
     }
   }
   if (pgm.hasVars() && pgm.macroCount() > 0 && pgm.getGlobals() == null)
     new Interpreter().saveGlobals(pgm);
   ArrayList tools = new ArrayList();
   for (int i = 0; i < code.length; i++) {
     token = code[i] & TOK_MASK;
     if (token == MACRO) {
       nextToken = code[i + 1] & TOK_MASK;
       if (nextToken == STRING_CONSTANT) {
         if (count == MAX_MACROS) {
           if (isPluginsMacrosMenu)
             IJ.error("Macro Installer", "Macro sets are limited to " + MAX_MACROS + " macros.");
           break;
         }
         address = code[i + 1] >> TOK_SHIFT;
         symbol = symbolTable[address];
         name = symbol.str;
         macroStarts[count] = i + 2;
         macroNames[count] = name;
         if (name.indexOf('-') != -1
             && (name.indexOf("Tool") != -1 || name.indexOf("tool") != -1)) {
           tools.add(name);
           toolCount++;
         } else if (name.startsWith("AutoRun")) {
           if (autoRunCount == 0 && !openingStartupMacrosInEditor) {
             new MacroRunner(pgm, macroStarts[count], name, (String) null);
             if (name.equals("AutoRunAndHide")) autoRunAndHideCount++;
           }
           autoRunCount++;
           count--;
         } else if (name.equals("Popup Menu")) installPopupMenu(name, pgm);
         else if (!name.endsWith("Tool Selected")) {
           if (macrosMenu != null) {
             addShortcut(name);
             int pos = name.indexOf(">");
             boolean inSubMenu = name.startsWith("<") && (pos > 1);
             if (inSubMenu) {
               Menu parent = macrosMenu;
               Menu subMenu = null;
               String parentStr = name.substring(1, pos).trim();
               String childStr = name.substring(pos + 1).trim();
               MenuItem mnuItem = new MenuItem();
               mnuItem.setActionCommand(name);
               mnuItem.setLabel(childStr);
               for (int jj = 0; jj < subMenus.size(); jj++) {
                 String aName = subMenus.get(jj).getName();
                 if (aName.equals(parentStr)) subMenu = subMenus.get(jj);
               }
               if (subMenu == null) {
                 subMenu = new Menu(parentStr);
                 subMenu.setName(parentStr);
                 subMenu.addActionListener(this);
                 subMenus.add(subMenu);
                 parent.add(subMenu);
               }
               subMenu.add(mnuItem);
             } else macrosMenu.add(new MenuItem(name));
           }
         }
         // IJ.log(count+" "+name+" "+macroStarts[count]);
         count++;
       }
     } else if (token == EOF) break;
   }
   nMacros = count;
   if (toolCount > 0 && (isPluginsMacrosMenu || macrosMenu == null) && installTools) {
     Toolbar tb = Toolbar.getInstance();
     if (toolCount == 1) tb.addMacroTool((String) tools.get(0), this);
     else {
       for (int i = 0; i < tools.size(); i++) {
         String toolName = (String) tools.get(i);
         if (toolName.startsWith("Abort Macro or Plugin") && toolCount > 6)
           toolName = "Unused " + toolName;
         tb.addMacroTool(toolName, this, i);
       }
     }
     if (toolCount > 1 && Toolbar.getToolId() >= Toolbar.CUSTOM1) tb.setTool(Toolbar.RECTANGLE);
     tb.repaint();
   }
   if (macrosMenu != null) this.instance = this;
   if (shortcutsInUse != null && text != null)
     IJ.showMessage(
         "Install Macros",
         (inUseCount == 1 ? "This keyboard shortcut is" : "These keyboard shortcuts are")
             + " already in use:"
             + shortcutsInUse);
   if (nMacros == 0 && fileName != null) {
     if (text == null || text.length() == 0) return;
     int dotIndex = fileName.lastIndexOf('.');
     if (dotIndex > 0) anonymousName = fileName.substring(0, dotIndex);
     else anonymousName = fileName;
     if (macrosMenu != null) macrosMenu.add(new MenuItem(anonymousName));
     macroNames[0] = anonymousName;
     nMacros = 1;
   }
   String word = nMacros == 1 ? " macro" : " macros";
   if (isPluginsMacrosMenu) IJ.showStatus(nMacros + word + " installed");
 }
Esempio n. 4
0
    void sample() {
      byte[] hsSource, ssSource, bsSource;
      // ImageProcessor ip2;
      // ip2 = imp.getProcessor();
      Rectangle myroi = ip.getRoi();
      int swidth = myroi.width;
      int sheight = myroi.height;
      int sy = myroi.y;
      int sx = myroi.x;
      if (swidth == width && sheight == height) {
        IJ.showMessage("Select a rectangular ROI");
        IJ.beep();
        return;
      }

      IJ.run("Select None");

      int snumPixels = swidth * sheight;

      hsSource = new byte[snumPixels];
      ssSource = new byte[snumPixels];
      bsSource = new byte[snumPixels];

      int[] pixs = new int[snumPixels];
      int[] bin = new int[256];

      int counter = 0, pi = 0, rangePassH = 0, rangeStopH = 0, rangePassL = 0, rangeStopL = 0, i, j;

      for (i = sy; i < sy + sheight; i++) {
        for (j = sx; j < sx + swidth; j++) {
          pixs[counter++] = ip.getPixel(j, i);
        }
      }

      // Get hsb or rgb from roi.
      ColorProcessor cp2 = new ColorProcessor(swidth, sheight, pixs);

      int iminhue = 256, imaxhue = -1, iminsat = 256, imaxsat = -1, iminbri = 256, imaxbri = -1;
      int iminred = 256, imaxred = -1, imingre = 256, imaxgre = -1, iminblu = 256, imaxblu = -1;

      if (isRGB) cp2.getRGB(hsSource, ssSource, bsSource);
      else cp2.getHSB(hsSource, ssSource, bsSource);

      for (i = 0; i < snumPixels; i++) {
        bin[hsSource[i] & 255] = 1;
        if ((hsSource[i] & 255) > imaxhue) imaxhue = (hsSource[i] & 255);
        if ((hsSource[i] & 255) < iminhue) iminhue = (hsSource[i] & 255);
        if ((ssSource[i] & 255) > imaxsat) imaxsat = (ssSource[i] & 255);
        if ((ssSource[i] & 255) < iminsat) iminsat = (ssSource[i] & 255);
        if ((bsSource[i] & 255) > imaxbri) imaxbri = (bsSource[i] & 255);
        if ((bsSource[i] & 255) < iminbri) iminbri = (bsSource[i] & 255);
        // IJ.showMessage("h:"+minhue+"H:"+maxhue+"s:"+minsat+"S:"+maxsat+"b:"+minbri+"B:"+maxbri);
      }

      if (!isRGB) { // get pass or stop filter whichever has a narrower range
        for (i = 0; i < 256; i++) {
          if (bin[i] > 0) {
            rangePassL = i;
            break;
          }
        }
        for (i = 255; i >= 0; i--) {
          if (bin[i] > 0) {
            rangePassH = i;
            break;
          }
        }
        for (i = 0; i < 256; i++) {
          if (bin[i] == 0) {
            rangeStopL = i;
            break;
          }
        }
        for (i = 255; i >= 0; i--) {
          if (bin[i] == 0) {
            rangeStopH = i;
            break;
          }
        }
        if ((rangePassH - rangePassL) < (rangeStopH - rangeStopL)) {
          bandPassH.setState(true);
          bandStopH.setState(false);
          iminhue = rangePassL;
          imaxhue = rangePassH;
        } else {
          bandPassH.setState(false);
          bandStopH.setState(true);
          iminhue = rangeStopL;
          imaxhue = rangeStopH;
        }
      } else {
        bandPassH.setState(true);
        bandStopH.setState(false);
      }

      adjustMinHue(iminhue);
      minSlider.setValue(iminhue);
      adjustMaxHue(imaxhue);
      maxSlider.setValue(imaxhue);
      adjustMinSat(iminsat);
      minSlider2.setValue(iminsat);
      adjustMaxSat(imaxsat);
      maxSlider2.setValue(imaxsat);
      adjustMinBri(iminbri);
      minSlider3.setValue(iminbri);
      adjustMaxBri(imaxbri);
      maxSlider3.setValue(imaxbri);
      originalB.setEnabled(true);
      // IJ.showStatus("done");
    }