javascriptfor-looprhinoimagejimagej-macro

Strange Javascript loop behavior in ImageJ macro (Rhino)


The loop

   //imageRows = 6;
    print("imageRows: " + imageRows);
    for (var gridY = 1 ; gridY < imageRows + 1 ; gridY++)
    {
        print("imageRows: " + imageRows + " gridY: " + gridY + " gridY < imageRows + 1: " + (gridY < imageRows + 1));
    } 

gives output

imageRows: 6
imageRows: 6 gridY: 1 gridY < imageRows + 1: true
imageRows: 6 gridY: 2 gridY < imageRows + 1: true
imageRows: 6 gridY: 3 gridY < imageRows + 1: true
imageRows: 6 gridY: 4 gridY < imageRows + 1: true
imageRows: 6 gridY: 5 gridY < imageRows + 1: true
imageRows: 6 gridY: 6 gridY < imageRows + 1: true
imageRows: 6 gridY: 7 gridY < imageRows + 1: true
imageRows: 6 gridY: 8 gridY < imageRows + 1: true
imageRows: 6 gridY: 9 gridY < imageRows + 1: true
    .....
imageRows: 6 gridY: 59 gridY < imageRows + 1: true
imageRows: 6 gridY: 60 gridY < imageRows + 1: true

However, uncommenting imageRows = 6;

    imageRows = 6;
    print("imageRows: " + imageRows);
    for (var gridY = 1 ; gridY < imageRows + 1 ; gridY++)
    {
        print("imageRows: " + imageRows + " gridY: " + gridY + " gridY < imageRows + 1: " + (gridY < imageRows + 1));
    }

gives the expected:

imageRows: 6
imageRows: 6 gridY: 1 gridY < imageRows + 1: true
imageRows: 6 gridY: 2 gridY < imageRows + 1: true
imageRows: 6 gridY: 3 gridY < imageRows + 1: true
imageRows: 6 gridY: 4 gridY < imageRows + 1: true
imageRows: 6 gridY: 5 gridY < imageRows + 1: true
imageRows: 6 gridY: 6 gridY < imageRows + 1: true

ImageJ uses the Rhino engine to run Javascript macros.

NOTE: The above loop is nested inside another loop. But for debugging purposes, I commented out all other lines in the outer loop.

Edit: For what it's worth, I x'd out rest of the outer loop to:

numImages = 1;
for (var imageNumber = 1 ; imageNumber < numImages + 1 ; imageNumber++)
{

    imageRows = 6;
    print("imageRows: " + imageRows);
    for (var gridY = 1 ; gridY < imageRows + 1 ; gridY++)
    {
        print("imageRows: " + imageRows + " gridY: " + gridY + " gridY < imageRows + 1: " + (gridY < imageRows + 1));
    } 

} 

and exactly same behavior.


Solution

  • Steven Lacks led me in the right direction. imageRows was not indeed a valid number. I was reading it in from a file:

    importClass(Packages.ij.IJ);
    ....
    var gridConfigurationRawData = IJ.openAsString(folder + IMAGE_GRID_CONFIGURATION_FILENAME); 
    var arrayOfData = gridConfigurationRawData.split("\n");
    ....
    var imageRows = dataLine[1];
    

    When I checked the type:

    print("typeof imageRows: " + (typeof imageRows));
    

    I got object.

    When I changed to:

    var imageRows = parseInt(dataLine[1]);
    

    it worked.

    Writing Javascript macros in ImageJ is surprisingly tricky.