/**
   * Once all the necessary variables are set, call the Gaussian Blur algorithm based on what type
   * of image this is and whether or not there is a separate destination image.
   */
  protected void callAlgorithm() {

    try {

      centerDistanceAlgo =
          new PlugInAlgorithmCenterDistance2(
              image,
              blueMin,
              redMin,
              redFraction,
              mergingDistance,
              greenMin,
              greenFraction,
              greenRegionNumber,
              twoGreenLevels,
              blueBoundaryFraction,
              blueSmooth,
              interpolationDivisor);

      // This is very important. Adding this object as a listener allows
      // the algorithm to
      // notify this object when it has completed or failed. See algorithm
      // performed event.
      // This is made possible by implementing AlgorithmedPerformed
      // interface
      centerDistanceAlgo.addListener(this);
      createProgressBar(image.getImageName(), " ...", centerDistanceAlgo);

      setVisible(false); // Hide dialog

      if (isRunInSeparateThread()) {

        // Start the thread as a low priority because we wish to still
        // have user interface work fast.
        if (centerDistanceAlgo.startMethod(Thread.MIN_PRIORITY) == false) {
          MipavUtil.displayError("A thread is already running on this object");
        }
      } else {
        centerDistanceAlgo.run();
      }
    } catch (OutOfMemoryError x) {
      MipavUtil.displayError("Center Distance: unable to allocate enough memory");

      return;
    }
  } // end callAlgorithm()
  /** Loads the default settings from Preferences to set up the dialog. */
  public void legacyLoadDefaults() {
    String defaultsString = Preferences.getDialogDefaults(getDialogName());

    if ((defaultsString != null) && (newImage != null)) {

      try {
        StringTokenizer st = new StringTokenizer(defaultsString, ",");

        textSearchWindowSide.setText("" + MipavUtil.getInt(st));
        textSimilarityWindowSide.setText("" + MipavUtil.getInt(st));
        textNoiseStandardDeviation.setText("" + MipavUtil.getFloat(st));
        textDegree.setText("" + MipavUtil.getFloat(st));
        doRician = MipavUtil.getBoolean(st);
        doRicianCheckBox.setSelected(doRician);
        textDegree.setEnabled(doRician);
        labelDegree.setEnabled(doRician);
        image25DCheckBox.setSelected(MipavUtil.getBoolean(st));

        if (MipavUtil.getBoolean(st)) {
          newImage.setSelected(true);
        } else {
          replaceImage.setSelected(true);
        }

      } catch (Exception ex) {

        // since there was a problem parsing the defaults string, start over with the original
        // defaults
        Preferences.debug("Resetting defaults for dialog: " + getDialogName());
        Preferences.removeProperty(getDialogName());
      }
    }
  }
  /**
   * This method is required if the AlgorithmPerformed interface is implemented. It is called by the
   * algorithms when it has completed or failed to to complete, so that the dialog can be display
   * the result image and/or clean up.
   *
   * @param algorithm Algorithm that caused the event.
   */
  public void algorithmPerformed(AlgorithmBase algorithm) {

    if (algorithm instanceof AlgorithmNonlocalMeansFilter) {
      image.clearMask();

      if ((nlMeansFilterAlgo.isCompleted() == true) && (resultImage != null)) {

        updateFileInfo(image, resultImage);
        resultImage.clearMask();

        // The algorithm has completed and produced a new image to be displayed.
        try {
          new ViewJFrameImage(resultImage, null, new Dimension(610, 200));
        } catch (OutOfMemoryError error) {
          MipavUtil.displayError("Out of memory: unable to open new frame");
        }
      } else if (resultImage == null) {

        // These next lines set the titles in all frames where the source image is displayed to
        // image name so as to indicate that the image is now unlocked!
        // The image frames are enabled and then registed to the userinterface.
        Vector<ViewImageUpdateInterface> imageFrames = image.getImageFrameVector();

        for (int i = 0; i < imageFrames.size(); i++) {
          ((Frame) (imageFrames.elementAt(i))).setTitle(titles[i]);
          ((Frame) (imageFrames.elementAt(i))).setEnabled(true);

          if (((Frame) (imageFrames.elementAt(i))) != parentFrame) {
            userInterface.registerFrame((Frame) (imageFrames.elementAt(i)));
          }
        }

        if (parentFrame != null) {
          userInterface.registerFrame(parentFrame);
        }

        image.notifyImageDisplayListeners(null, true);
      } else if (resultImage != null) {

        // algorithm failed but result image still has garbage
        resultImage.disposeLocal(); // clean up memory
        resultImage = null;
      }
    }

    if (algorithm.isCompleted()) {
      insertScriptLine();
    }
    // save the completion status for later
    setComplete(algorithm.isCompleted());

    nlMeansFilterAlgo.finalize();
    nlMeansFilterAlgo = null;
    dispose();
  }
  /**
   * Creates new dialog for distances within a cell from the geometric center using a plugin.
   *
   * @param theParentFrame Parent frame.
   * @param im Source image.
   */
  public PlugInDialogCenterDistance2(Frame theParentFrame, ModelImage im) {
    super(theParentFrame, false);

    if (!im.isColorImage()) {
      MipavUtil.displayError("Source Image must be Color");
      dispose();

      return;
    }

    image = im;
    init();
  }
  /** Calls the algorithm. */
  protected void callAlgorithm() {

    try {
      resultImage =
          new ModelImage(imageA.getType(), imageA.getExtents(), (imageA.getImageName() + "_isn"));
      resultImage.copyFileTypeInfo(imageA);

      // Make algorithm
      isnAlgo = new PlugInAlgorithmISN(resultImage, imageA);

      // This is very important. Adding this object as a listener allows the algorithm to
      // notify this object when it has completed of failed. See algorithm performed event.
      // This is made possible by implementing AlgorithmedPerformed interface
      isnAlgo.addListener(this);

      createProgressBar(imageA.getImageName(), " ...", isnAlgo);

      // Hide dialog
      setVisible(false);

      if (isRunInSeparateThread()) {

        // Start the thread as a low priority because we wish to still have user interface work
        // fast.
        if (isnAlgo.startMethod(Thread.MIN_PRIORITY) == false) {
          MipavUtil.displayError("A thread is already running on this object");
        }
      } else {
        isnAlgo.run();
      }
    } catch (OutOfMemoryError x) {
      System.gc();
      MipavUtil.displayError("AlgorithmAbsoluteValue: unable to allocate enough memory");

      return;
    }
  }
  /**
   * Use the GUI results to set up the variables needed to run the algorithm.
   *
   * @return <code>true</code> if parameters set successfully, <code>false</code> otherwise.
   */
  private boolean setVariables() {
    String tmpStr;

    System.gc();

    if (replaceImage.isSelected()) {
      displayLoc = REPLACE;
    } else if (newImage.isSelected()) {
      displayLoc = NEW;
    }

    tmpStr = textSearchWindowSide.getText();

    if (testParameter(tmpStr, 5, 101)) {
      searchWindowSide = Integer.valueOf(tmpStr).intValue();
    } else {
      MipavUtil.displayError("Search window side must be between 5 and 101");
      textSearchWindowSide.requestFocus();
      textSearchWindowSide.selectAll();

      return false;
    }

    if ((searchWindowSide % 2) == 0) {
      MipavUtil.displayError("Search window side must be an odd number");
      textSearchWindowSide.requestFocus();
      textSearchWindowSide.selectAll();
      return false;
    }

    tmpStr = textSimilarityWindowSide.getText();

    if (testParameter(tmpStr, 3, 99)) {
      similarityWindowSide = Integer.valueOf(tmpStr).intValue();
    } else {
      MipavUtil.displayError("Similarity window side must be between 3 and 99");
      textSimilarityWindowSide.requestFocus();
      textSimilarityWindowSide.selectAll();

      return false;
    }

    if ((similarityWindowSide % 2) == 0) {
      MipavUtil.displayError("Similarity window side must be an odd number");
      textSimilarityWindowSide.requestFocus();
      textSimilarityWindowSide.selectAll();
      return false;
    }

    if (similarityWindowSide >= searchWindowSide) {
      MipavUtil.displayError("Similarity window side must be less than search window side");
      textSimilarityWindowSide.requestFocus();
      textSimilarityWindowSide.selectAll();
      return false;
    }

    tmpStr = textNoiseStandardDeviation.getText();

    if (testParameter(tmpStr, 0.001, 1000.0)) {
      noiseStandardDeviation = Float.valueOf(tmpStr).floatValue();
    } else {
      MipavUtil.displayError("Radius must be between 0.001 and 1000.0");
      textNoiseStandardDeviation.requestFocus();
      textNoiseStandardDeviation.selectAll();

      return false;
    }

    doRician = doRicianCheckBox.isSelected();

    if (doRician) {
      tmpStr = textDegree.getText();
      if (testParameter(tmpStr, 1.0, 10.0)) {
        degreeOfFiltering = Float.valueOf(tmpStr).floatValue();
      } else {
        MipavUtil.displayError("Degree of filtering must be between 1.0 and 10.0");
        textDegree.requestFocus();
        textDegree.selectAll();
      }
    }

    if (image.getNDims() > 2) {
      image25D = image25DCheckBox.isSelected();
    }

    return true;
  }
  /**
   * Once all the necessary variables are set, call the Nonlocal Means filter algorithm based on
   * what type of image this is and whether or not there is a separate destination image.
   */
  protected void callAlgorithm() {
    String name = makeImageName(image.getImageName(), "_NonlocalMeans");
    int[] destExtents;

    if (image.getNDims() == 2) { // source image is 2D
      destExtents = new int[2];
      destExtents[0] = image.getExtents()[0]; // X dim
      destExtents[1] = image.getExtents()[1]; // Y dim
    } else {
      destExtents = new int[3];
      destExtents[0] = image.getExtents()[0];
      destExtents[1] = image.getExtents()[1];
      destExtents[2] = image.getExtents()[2];
    }

    if (displayLoc == NEW) {

      try {

        // Make result image of float type
        if (image.isColorImage()) {
          resultImage = new ModelImage(ModelImage.ARGB, destExtents, name);
        } else {
          resultImage = new ModelImage(ModelImage.FLOAT, destExtents, name);
        }

        // resultImage = (ModelImage)image.clone();
        // resultImage.setImageName(name);
        // Make algorithm
        nlMeansFilterAlgo =
            new AlgorithmNonlocalMeansFilter(
                resultImage,
                image,
                searchWindowSide,
                similarityWindowSide,
                noiseStandardDeviation,
                degreeOfFiltering,
                doRician,
                image25D);

        // This is very important. Adding this object as a listener allows the algorithm to
        // notify this object when it has completed of failed. See algorithm performed event.
        // This is made possible by implementing AlgorithmedPerformed interface
        nlMeansFilterAlgo.addListener(this);
        createProgressBar(image.getImageName(), nlMeansFilterAlgo);

        // Hide dialog
        setVisible(false);

        if (isRunInSeparateThread()) {

          // Start the thread as a low priority because we wish to still have user interface work
          // fast
          if (nlMeansFilterAlgo.startMethod(Thread.MIN_PRIORITY) == false) {
            MipavUtil.displayError("A thread is already running on this object");
          }
        } else {
          nlMeansFilterAlgo.run();
        }
      } catch (OutOfMemoryError x) {
        MipavUtil.displayError("Dialog Nonlocal Means Filter: unable to allocate enough memory");

        if (resultImage != null) {
          resultImage.disposeLocal(); // Clean up memory of result image
          resultImage = null;
        }

        return;
      }
    } else {

      try {

        // No need to make new image space because the user has choosen to replace the source image
        // Make the algorithm class
        nlMeansFilterAlgo =
            new AlgorithmNonlocalMeansFilter(
                null,
                image,
                searchWindowSide,
                similarityWindowSide,
                noiseStandardDeviation,
                degreeOfFiltering,
                doRician,
                image25D);

        // This is very important. Adding this object as a listener allows the algorithm to
        // notify this object when it has completed of failed. See algorithm performed event.
        // This is made possible by implementing AlgorithmedPerformed interface
        nlMeansFilterAlgo.addListener(this);
        createProgressBar(image.getImageName(), nlMeansFilterAlgo);

        // Hide the dialog since the algorithm is about to run.
        setVisible(false);

        // These next lines set the titles in all frames where the source image is displayed to
        // "locked - " image name so as to indicate that the image is now read/write locked!
        // The image frames are disabled and then unregisted from the userinterface until the
        // algorithm has completed.
        Vector<ViewImageUpdateInterface> imageFrames = image.getImageFrameVector();
        titles = new String[imageFrames.size()];

        for (int i = 0; i < imageFrames.size(); i++) {
          titles[i] = ((Frame) (imageFrames.elementAt(i))).getTitle();
          ((Frame) (imageFrames.elementAt(i))).setTitle("Locked: " + titles[i]);
          ((Frame) (imageFrames.elementAt(i))).setEnabled(false);
          userInterface.unregisterFrame((Frame) (imageFrames.elementAt(i)));
        }

        if (isRunInSeparateThread()) {

          // Start the thread as a low priority because we wish to still have user interface work
          // fast
          if (nlMeansFilterAlgo.startMethod(Thread.MIN_PRIORITY) == false) {
            MipavUtil.displayError("A thread is already running on this object");
          }
        } else {
          nlMeansFilterAlgo.run();
        }
      } catch (OutOfMemoryError x) {
        MipavUtil.displayError("Dialog Nonlocal Means Filter: unable to allocate enough memory");

        return;
      }
    }
  }
  /**
   * Use the GUI results to set up the variables needed to run the algorithm.
   *
   * @return <code>true</code> if parameters set successfully, <code>false</code> otherwise.
   */
  private boolean setVariables() {
    String tmpStr;
    int i;

    int totLength = image.getExtents()[0];

    for (i = 1; i < image.getNDims(); i++) {
      totLength *= image.getExtents()[i];
    }

    tmpStr = greenMergingText.getText();
    mergingDistance = Float.parseFloat(tmpStr);
    if (mergingDistance < 0.0f) {
      MipavUtil.displayError("Merging distance cannot be less than 0");
      greenMergingText.requestFocus();
      greenMergingText.selectAll();
      return false;
    }

    tmpStr = redMinText.getText();
    redMin = Integer.parseInt(tmpStr);

    if (redMin < 1) {
      MipavUtil.displayError("red minimum must be at least 1");
      redMinText.requestFocus();
      redMinText.selectAll();

      return false;
    } else if (redMin > totLength) {
      MipavUtil.displayError("red minimum must not exceed " + totLength);
      redMinText.requestFocus();
      redMinText.selectAll();

      return false;
    }

    tmpStr = redFractionText.getText();
    redFraction = Float.parseFloat(tmpStr);

    if (redFraction <= 0.0f) {
      MipavUtil.displayError("red fraction must be greater than zero");
      redFractionText.requestFocus();
      redFractionText.selectAll();

      return false;
    } else if (redFraction > 1.0f) {
      MipavUtil.displayError("red fraction must not exceed one");
      redFractionText.requestFocus();
      redFractionText.selectAll();

      return false;
    }

    tmpStr = greenMinText.getText();
    greenMin = Integer.parseInt(tmpStr);

    if (greenMin < 1) {
      MipavUtil.displayError("green minimum must be at least 1");
      greenMinText.requestFocus();
      greenMinText.selectAll();

      return false;
    } else if (greenMin > totLength) {
      MipavUtil.displayError("green minimum must not exceed " + totLength);
      greenMinText.requestFocus();
      greenMinText.selectAll();

      return false;
    }

    tmpStr = greenFractionText.getText();
    greenFraction = Float.parseFloat(tmpStr);

    if (greenFraction <= 0.0f) {
      MipavUtil.displayError("green fraction must be greater than zero");
      greenFractionText.requestFocus();
      greenFractionText.selectAll();

      return false;
    } else if (greenFraction > 1.0f) {
      MipavUtil.displayError("green fraction must not exceed one");
      greenFractionText.requestFocus();
      greenFractionText.selectAll();

      return false;
    }

    tmpStr = blueMinText.getText();
    blueMin = Integer.parseInt(tmpStr);
    if (blueMin <= 0) {
      MipavUtil.displayError("Number of blue pixels must be greater than 0");
      blueMinText.requestFocus();
      blueMinText.selectAll();
      return false;
    } else if (blueMin > totLength) {
      MipavUtil.displayError("blue minimum must not exceed " + totLength);
      blueMinText.requestFocus();
      blueMinText.selectAll();

      return false;
    }

    if (oneButton.isSelected()) {
      greenRegionNumber = 1;
    } else if (twoButton.isSelected()) {
      greenRegionNumber = 2;
    } else if (threeButton.isSelected()) {
      greenRegionNumber = 3;
    } else {
      greenRegionNumber = 4;
    }

    twoGreenLevels = twoBox.isSelected();

    tmpStr = blueValueText.getText();
    blueBoundaryFraction = Float.parseFloat(tmpStr);
    if (blueBoundaryFraction < 0.0f) {
      MipavUtil.displayError("Blue boundary fraction cannot be less than 0.0");
      blueValueText.requestFocus();
      blueValueText.selectAll();
      return false;
    } else if (blueBoundaryFraction > 1.0f) {
      MipavUtil.displayError("Blue boundary value cannot be greater than 1.0");
      blueValueText.requestFocus();
      blueValueText.selectAll();
      return false;
    }

    blueSmooth = blueSmoothBox.isSelected();

    tmpStr = interpolationText.getText();
    interpolationDivisor = Float.parseFloat(tmpStr);
    if (interpolationDivisor <= 1.0f) {
      MipavUtil.displayError("Interpolation divisor must be greater than 1");
      interpolationText.requestFocus();
      interpolationText.selectAll();
      return false;
    }

    return true;
  } // end setVariables()
  /**
   * Use the GUI results to set up the variables needed to run the algorithm.
   *
   * @return <code>true</code> if parameters set successfully, <code>false</code> otherwise.
   */
  private boolean setVariables() {
    String tmpStr;

    tmpStr = textXDim.getText();
    try {
      subXDim = Integer.parseInt(tmpStr);
    } catch (NumberFormatException e) {
      MipavUtil.displayError("New XDIM string is not a valid integer");
      textXDim.requestFocus();
      textXDim.selectAll();

      return false;
    }
    if (subXDim < 3) {
      MipavUtil.displayError("New XDIM must be at least 3");
      textXDim.requestFocus();
      textXDim.selectAll();

      return false;
    } else if (subXDim > image.getExtents()[0]) {
      MipavUtil.displayError("New XDIM cannot exceed " + image.getExtents()[0]);
      textXDim.requestFocus();
      textXDim.selectAll();

      return false;
    }

    tmpStr = textYDim.getText();
    try {
      subYDim = Integer.parseInt(tmpStr);
    } catch (NumberFormatException e) {
      MipavUtil.displayError("New YDIM string is not a valid integer");
      textYDim.requestFocus();
      textYDim.selectAll();

      return false;
    }
    if (subYDim < 3) {
      MipavUtil.displayError("New YDIM must be at least 3");
      textYDim.requestFocus();
      textYDim.selectAll();

      return false;
    } else if (subYDim > image.getExtents()[1]) {
      MipavUtil.displayError("New YDIM cannot exceed " + image.getExtents()[1]);
      textYDim.requestFocus();
      textYDim.selectAll();

      return false;
    }

    tmpStr = textNumberImages.getText();
    try {
      numberOfImagesInMosaic = Integer.parseInt(tmpStr);
    } catch (NumberFormatException e) {
      MipavUtil.displayError("New numberOfImagesInMosaic string is not a valid integer");
      textNumberImages.requestFocus();
      textNumberImages.selectAll();

      return false;
    }
    if (numberOfImagesInMosaic < 1) {
      MipavUtil.displayError("New numberOfImagesInMosaic must be at least 1");
      textNumberImages.requestFocus();
      textNumberImages.selectAll();

      return false;
    } else if (numberOfImagesInMosaic > (subXDim * subYDim)) {
      MipavUtil.displayError("New numberOfImagesInMosaic cannot exceed (newXDim) * (newYDim)");
      textNumberImages.requestFocus();
      textNumberImages.selectAll();

      return false;
    }

    displayLoc = NEW;

    return true;
  }
  /**
   * Once all the necessary variables are set, call the Concat algorithm based on what type of image
   * this is and whether or not there is a separate destination image.
   */
  protected void callAlgorithm() {
    int destExtents[] = new int[3];
    ModelImage destImage = null;

    destExtents[0] = subXDim;
    destExtents[1] = subYDim;
    destExtents[2] = numberOfImagesInMosaic;

    destImage =
        new ModelImage(
            image.getType(), destExtents, makeImageName(image.getImageName(), "_mosaic_to_slices"));

    try {

      // Make algorithm
      mathAlgo = new AlgorithmMosaicToSlices(image, destImage);

      // This is very important. Adding this object as a listener allows the algorithm to
      // notify this object when it has completed of failed. See algorithm performed event.
      // This is made possible by implementing AlgorithmedPerformed interface
      mathAlgo.addListener(this);

      createProgressBar(image.getImageName(), mathAlgo);

      // Hide dialog
      setVisible(false);

      if (displayLoc == REPLACE) {

        // These next lines set the titles in all frames where the source image is displayed to
        // "locked - " image name so as to indicate that the image is now read/write locked!
        // The image frames are disabled and then unregisted from the userinterface until the
        // algorithm has completed.
        Vector<ViewImageUpdateInterface> imageFrames = image.getImageFrameVector();
        titles = new String[imageFrames.size()];

        for (int i = 0; i < imageFrames.size(); i++) {
          titles[i] = ((Frame) (imageFrames.elementAt(i))).getTitle();
          ((Frame) (imageFrames.elementAt(i))).setTitle("Locked: " + titles[i]);
          ((Frame) (imageFrames.elementAt(i))).setEnabled(false);
          userInterface.unregisterFrame((Frame) (imageFrames.elementAt(i)));
        }
      }

      if (isRunInSeparateThread()) {

        // Start the thread as a low priority because we wish to still have user interface work
        // fast.
        if (mathAlgo.startMethod(Thread.MIN_PRIORITY) == false) {
          MipavUtil.displayError("A thread is already running on this object");
        }

      } else {

        mathAlgo.run();
      }
    } catch (OutOfMemoryError x) {
      System.gc();
      MipavUtil.displayError("Dialog Concatenation: unable to allocate enough memory");

      return;
    }
  }
  /**
   * This method is required if the AlgorithmPerformed interface is implemented. It is called by the
   * algorithms when it has completed or failed to to complete, so that the dialog can be display
   * the result image and/or clean up.
   *
   * @param algorithm Algorithm that caused the event.
   */
  public void algorithmPerformed(AlgorithmBase algorithm) {
    ViewJFrameImage imageFrame = null;

    if (algorithm instanceof AlgorithmMosaicToSlices) {

      if ((mathAlgo.isCompleted() == true) && (mathAlgo.getResultImage() != null)) {

        // The algorithm has completed and produced a new image to be displayed.
        if (displayLoc == NEW) {

          try {
            resultImage = mathAlgo.getResultImage();

            new ViewJFrameImage(resultImage, null, new Dimension(610, 200));
          } catch (OutOfMemoryError error) {
            System.gc();
            MipavUtil.displayError("Out of memory: unable to open new frame");
          }
        } else {

          // These next lines set the titles in all frames where the source image is displayed to
          // image name so as to indicate that the image is now unlocked!
          // The image frames are enabled and then registed to the userinterface.
          resultImage = mathAlgo.getResultImage();

          Vector<ViewImageUpdateInterface> imageFrames = image.getImageFrameVector();

          for (int i = 0; i < imageFrames.size(); i++) {
            ((Frame) (imageFrames.elementAt(i))).setTitle(titles[i]);
            ((Frame) (imageFrames.elementAt(i))).setEnabled(true);

            if ((((Frame) (imageFrames.elementAt(i))) != parentFrame) && (parentFrame != null)) {
              userInterface.registerFrame((Frame) (imageFrames.elementAt(i)));
            }
          }

          Point pt;

          if (parentFrame != null) {
            pt = ((ViewJFrameBase) parentFrame).getLocation();
          } else {
            pt =
                new Point(
                    Toolkit.getDefaultToolkit().getScreenSize().width / 2,
                    Toolkit.getDefaultToolkit().getScreenSize().height / 2);
          }

          imageFrame = new ViewJFrameImage(resultImage, null, new Dimension(pt.x, pt.y));

          if (parentFrame != null) {
            ((ViewJFrameBase) parentFrame).close();
          } else {
            ((ViewJFrameBase) image.getParentFrame()).close();
          }

          // Not so sure about this.
          if (image.getLightBoxFrame() != null) {

            try {
              pt = image.getLightBoxFrame().getLocation();
              image.getLightBoxFrame().close();
              new ViewJFrameLightBox(
                  imageFrame,
                  "LightBox",
                  resultImage,
                  imageFrame.getComponentImage().getLUTa(),
                  imageFrame.getComponentImage().getImageB(),
                  imageFrame.getComponentImage().getLUTb(),
                  imageFrame.getComponentImage().getResolutionX(),
                  imageFrame.getComponentImage().getResolutionY(),
                  new Dimension(pt.x, pt.y),
                  imageFrame.getControls(),
                  imageFrame.getVOIManager());
            } catch (OutOfMemoryError error) {
              MipavUtil.displayError("Out of memory: unable to open new frame");
            }
          }
        }
      } else if (resultImage == null) {

        // These next lines set the titles in all frames where the source image is displayed to
        // image name so as to indicate that the image is now unlocked!
        // The image frames are enabled and then registered to the userinterface.
        /*Vector imageFrames = imageA.getImageFrameVector();

        for (int i = 0; i < imageFrames.size(); i++) {
            ((Frame) (imageFrames.elementAt(i))).setTitle(titles[i]);
            ((Frame) (imageFrames.elementAt(i))).setEnabled(true);

            if (((Frame) (imageFrames.elementAt(i))) != parentFrame) {
                userInterface.registerFrame((Frame) (imageFrames.elementAt(i)));

            }
        }*/

        if (parentFrame != null) {
          userInterface.registerFrame(parentFrame);
        }

        image.notifyImageDisplayListeners(null, true);
      } else if (resultImage != null) {

        // algorithm failed but result image still has garbage
        resultImage.disposeLocal(); // clean up memory
        System.gc();
      }
    }

    if (algorithm.isCompleted()) {
      insertScriptLine();
    }

    mathAlgo.finalize();
    mathAlgo = null;
    dispose();
  }