Bug on closing ImagePlus without save dialog after adding ROI in javascript?

javascript
point-roi
save
Tags: #<Tag:0x00007fa30b9295b8> #<Tag:0x00007fa30b9292e8> #<Tag:0x00007fa30b928f50>

#1

I encountered a weird issue while writing a script in javascript that doesn’t allow me to close an ImagePlus without the save dialog popping up, despite having set the ‘changes’ field to false. Here is a minimal set of code lines that reproduce the phenomenon:

importClass(Packages.ij.ImagePlus);
importClass(Packages.ij.gui.PointRoi);
importClass(Packages.ij.process.FloatPolygon);
importClass(Packages.ij.gui.NewImage);

var image = NewImage.createFloatImage("Untitled", 1000, 1000, 1, NewImage.FILL_BLACK);

var roi = new PointRoi(new FloatPolygon([0],[0]));
roi = roi.addPoint(1, 1);
roi = roi.addPoint(2, 2);

image.setRoi(roi, true);
image.show();
image.changes = false;
image.close();

Note: the second point addition is required to trigger the issue. I’m on Fiji in MacOs latest version of all software.


#2

Nice catch, @lollopus, thanks for reporting!

I suppose you see that behavior because of the following lines in ImageWindow.java:

You can work around the issue by adding the following line before adding the ROI to the image:

roi.promptBeforeDeleting(false);

This method was apparently introduced recently with this commit:

but the reason why it was added is unclear to me.


I’d consider the behavior a bug, as it behaves inconsistently depending on how often roi.addPoint() was called.

@Wayne do you agree?


#3

Hi and thanks for the effective workaround! I’m glad I could help a little by spotting this one… cheers, Lorenzo


#4

This 1.52c regression is fixed in the latest ImageJ daily build (1.52e25). The “Save changes?” dialog is now only displayed when closing an image that has a point selection with more than 10 points added while the image is showing. The dialog is displayed to prevent work from being lost when using the multi-point tool as a counter.

The source code changes can be viewed at


#5

Thanks for the credits Wayne… Just a question: In my example above I used 3 points, but my actual script dynamically generates a much larger multi point selection based on the interpolation of freelines drawn by the user. Will I be able to avoid the save dialog when programmatically closing the image by setting img.changes=false or will I need to keep using the workaround suggested by Imagejan? Thanks L


#6

You will not need the workarounds if you upgrade to the daily build. This example creates a selection with 5000 points:

  image = IJ.createImage("Untitled", "32-bit", 1000, 1000, 1);
  ran = new Random()
  roi = new PointRoi(new FloatPolygon([0],[0]));
  for (i=0; i<5000; i++) {
     x = ran.nextDouble()*1000;
     y = ran.nextDouble()*1000;
     roi.addPoint(x, y);
  }
  image.setRoi(roi);
  image.show();
  IJ.wait(4000);
  image.close();

#7

Excellent, thanks again!


#8

The latest daily build (1.52e27) adds a no-argument constructor to the PointRoi class, making it easier to create multi-point selections. It also adds a size() method, which is equivalent to getPolygon().npoints. The following example creates a multi-point selection with 5000 points. The previous example actually created a 5001 point selection due to the point added by the constructor.

  size = 700;
  npoints = 5000;
  ran = new Random()
  points = new PointRoi();
  points.setPointType(2);
  points.setSize(2);
  points.setStrokeColor(Color.magenta);
  for (i=0; i<npoints; i++) {
     x = size/2+ran.nextGaussian()*size/7;
     y = size/2+ran.nextGaussian()*size/7;
     points.addPoint(x, y);
  }
  img = IJ.createImage("tmp", "16-bit", size, size, 1);
  img.setRoi(points);
  img.show();
  print("points: "+points.size());