@Override public void run(){
  amIActive=true;
  String inputHeader=null;
  String outputHeader=null;
  int row, col;
  float progress=0;
  double maxPatchValue=0;
  int x, y;
  boolean blnFoundNeighbour;
  boolean blnIncludeDiagNeighbour=false;
  boolean blnTreatZerosAsBackground=false;
  int i;
  if (args.length <= 0) {
    showFeedback("Plugin parameters have not been set.");
    return;
  }
  for (i=0; i < args.length; i++) {
    if (i == 0) {
      inputHeader=args[i];
    }
 else     if (i == 1) {
      outputHeader=args[i];
    }
 else     if (i == 2) {
      blnIncludeDiagNeighbour=Boolean.parseBoolean(args[i]);
      if (!blnIncludeDiagNeighbour) {
        dX=new int[]{0,1,0,-1};
        dY=new int[]{-1,0,1,0};
      }
    }
 else     if (i == 3) {
      blnTreatZerosAsBackground=Boolean.parseBoolean(args[i]);
    }
  }
  if ((inputHeader == null) || (outputHeader == null)) {
    showFeedback("One or more of the input parameters have not been set properly.");
    return;
  }
  try {
    numScanCells=dY.length;
    image=new WhiteboxRaster(inputHeader,"r");
    int rows=image.getNumberRows();
    int cols=image.getNumberColumns();
    noData=image.getNoDataValue();
    double initialValue=-1;
    output=new WhiteboxRaster(outputHeader,"rw",inputHeader,WhiteboxRaster.DataType.FLOAT,initialValue);
    output.setDataScale(WhiteboxRaster.DataScale.CATEGORICAL);
    output.setPreferredPalette("qual.pal");
    if (blnTreatZerosAsBackground) {
      for (row=0; row < rows; row++) {
        for (col=0; col < cols; col++) {
          if (image.getValue(row,col) == 0) {
            output.setValue(row,col,0);
          }
        }
        if (cancelOp) {
          cancelOperation();
          return;
        }
        progress=(float)(100f * row / (rows - 1));
        updateProgress((int)progress);
      }
      if (output.getValue(0,0) == -1) {
        output.setValue(0,0,1);
      }
    }
 else {
      output.setValue(0,0,0);
    }
    double patchValue=0;
    double neighbourPatchValue=0;
    double newPatchValue=0;
    double imageValue=0;
    int loopNum=1;
    updateProgress("Loop " + loopNum + ":",0);
    for (row=0; row < rows; row++) {
      for (col=0; col < cols; col++) {
        imageValue=image.getValue(row,col);
        if (imageValue != noData) {
          patchValue=output.getValue(row,col);
          if (patchValue == initialValue) {
            blnFoundNeighbour=false;
            for (i=0; i < numScanCells; i++) {
              x=col + dX[i];
              y=row + dY[i];
              neighbourPatchValue=output.getValue(y,x);
              if (neighbourPatchValue != initialValue && image.getValue(y,x) == imageValue) {
                output.setValue(row,col,neighbourPatchValue);
                newPatchValue=neighbourPatchValue;
                blnFoundNeighbour=true;
                break;
              }
            }
            if (!blnFoundNeighbour) {
              maxPatchValue++;
              newPatchValue=maxPatchValue;
              output.setValue(row,col,newPatchValue);
            }
            depth=0;
            ScanConnectedCells(row,col,imageValue,initialValue,newPatchValue);
          }
        }
 else {
          output.setValue(row,col,noData);
        }
      }
      if (cancelOp) {
        cancelOperation();
        return;
      }
      progress=(float)(100f * row / (rows - 1));
      updateProgress((int)progress);
    }
    boolean somethingDone;
    double[] reclass=new double[(int)maxPatchValue + 1];
    do {
      loopNum++;
      updateProgress("Loop " + loopNum + ":",0);
      somethingDone=false;
      for (row=0; row < rows; row++) {
        for (col=0; col < cols; col++) {
          imageValue=image.getValue(row,col);
          if (imageValue != noData) {
            patchValue=output.getValue(row,col);
            for (i=0; i < numScanCells; i++) {
              x=col + dX[i];
              y=row + dY[i];
              neighbourPatchValue=output.getValue(y,x);
              if (neighbourPatchValue != patchValue && image.getValue(y,x) == imageValue) {
                somethingDone=true;
                if (patchValue < neighbourPatchValue) {
                  reclass[(int)neighbourPatchValue]=-1;
                  output.setValue(y,x,patchValue);
                  ScanConnectedCells(y,x,imageValue,neighbourPatchValue,patchValue);
                }
 else {
                  reclass[(int)patchValue]=-1;
                  output.setValue(row,col,neighbourPatchValue);
                  ScanConnectedCells(row,col,imageValue,patchValue,neighbourPatchValue);
                  patchValue=neighbourPatchValue;
                }
              }
            }
          }
        }
        if (cancelOp) {
          cancelOperation();
          return;
        }
        progress=(float)(100f * row / (rows - 1));
        updateProgress((int)progress);
      }
    }
 while (somethingDone);
    i=0;
    for (int a=0; a < maxPatchValue + 1; a++) {
      if (reclass[a] != -1) {
        reclass[a]=i;
        i++;
      }
    }
    loopNum++;
    updateProgress("Loop " + loopNum + ":",0);
    for (row=0; row < rows; row++) {
      for (col=0; col < cols; col++) {
        patchValue=output.getValue(row,col);
        if (patchValue != noData) {
          output.setValue(row,col,reclass[(int)patchValue]);
        }
      }
      if (cancelOp) {
        cancelOperation();
        return;
      }
      progress=(float)(100f * row / (rows - 1));
      updateProgress((int)progress);
    }
    output.addMetadataEntry("Created by the " + getDescriptiveName() + " tool.");
    output.addMetadataEntry("Created on " + new Date());
    image.close();
    output.close();
    returnData(outputHeader);
  }
 catch (  OutOfMemoryError oe) {
    myHost.showFeedback("An out-of-memory error has occurred during operation.");
  }
catch (  Exception e) {
    myHost.showFeedback("An error has occurred during operation. See log file for details.");
    myHost.logException("Error in " + getDescriptiveName(),e);
  }
 finally {
    updateProgress("Progress: ",0);
    amIActive=false;
    myHost.pluginComplete();
  }
}

@ Override public void run ( ) { amIActive = true ; String inputHeader = null ; String outputHeader = null ; int row , col ; float progress = 0 ; double maxPatchValue = 0 ; int x , y ; boolean blnFoundNeighbour ; boolean blnIncludeDiagNeighbour = false ; boolean blnTreatZerosAsBackground = false ; int i ; if ( args . length <= 0 ) { showFeedback ( "Plugin parameters have not been set." ) ; return ; } for ( i = 0 ; i < args . length ; i ++ ) { if ( i == 0 ) { inputHeader = args [ i ] ; } else if ( i == 1 ) { outputHeader = args [ i ] ; } else if ( i == 2 ) { blnIncludeDiagNeighbour = Boolean . parseBoolean ( args [ i ] ) ; if ( ! blnIncludeDiagNeighbour ) { dX = new int [ ] { 0 , 1 , 0 , - 1 } ; dY = new int [ ] { - 1 , 0 , 1 , 0 } ; } } else if ( i == 3 ) { blnTreatZerosAsBackground = Boolean . parseBoolean ( args [ i ] ) ; } } if ( ( inputHeader == null ) || ( outputHeader == null ) ) { showFeedback ( "One or more of the input parameters have not been set properly." ) ; return ; } try { numScanCells = dY . length ; image = new WhiteboxRaster ( inputHeader , "r" ) ; int rows = image . getNumberRows ( ) ; int cols = image . getNumberColumns ( ) ; noData = image . getNoDataValue ( ) ; double initialValue = - 1 ; output = new WhiteboxRaster ( outputHeader , "rw" , inputHeader , WhiteboxRaster . DataType . FLOAT , initialValue ) ; output . setDataScale ( WhiteboxRaster . DataScale . CATEGORICAL ) ; output . setPreferredPalette ( "qual.pal" ) ; if ( blnTreatZerosAsBackground ) { for ( row = 0 ; row < rows ; row ++ ) { for ( col = 0 ; col < cols ; col ++ ) { if ( image . getValue ( row , col ) == 0 ) { output . setValue ( row , col , 0 ) ; } } if ( cancelOp ) { cancelOperation ( ) ; return ; } progress = ( float ) ( 100f * row / ( rows - 1 ) ) ; updateProgress ( ( int ) progress ) ; } if ( output . getValue ( 0 , 0 ) == - 1 ) { output . setValue ( 0 , 0 , 1 ) ; } } else { output . setValue ( 0 , 0 , 0 ) ; } double patchValue = 0 ; double neighbourPatchValue = 0 ; double newPatchValue = 0 ; double imageValue = 0 ; int loopNum = 1 ; updateProgress ( "Loop " + loopNum + ":" , 0 ) ; for ( row = 0 ; row < rows ; row ++ ) { for ( col = 0 ; col < cols ; col ++ ) { imageValue = image . getValue ( row , col ) ; if ( imageValue != noData ) { patchValue = output . getValue ( row , col ) ; if ( patchValue == initialValue ) { blnFoundNeighbour = false ; for ( i = 0 ; i < numScanCells ; i ++ ) { x = col + dX [ i ] ; y = row + dY [ i ] ; neighbourPatchValue = output . getValue ( y , x ) ; if ( neighbourPatchValue != initialValue && image . getValue ( y , x ) == imageValue ) { output . setValue ( row , col , neighbourPatchValue ) ; newPatchValue = neighbourPatchValue ; blnFoundNeighbour = true ; break ; } } if ( ! blnFoundNeighbour ) { maxPatchValue ++ ; newPatchValue = maxPatchValue ; output . setValue ( row , col , newPatchValue ) ; } depth = 0 ; ScanConnectedCells ( row , col , imageValue , initialValue , newPatchValue ) ; } } else { output . setValue ( row , col , noData ) ; } } if ( cancelOp ) { cancelOperation ( ) ; return ; } progress = ( float ) ( 100f * row / ( rows - 1 ) ) ; updateProgress ( ( int ) progress ) ; } boolean somethingDone ; double [ ] reclass = new double [ ( int ) maxPatchValue + 1 ] ; do { loopNum ++ ; updateProgress ( "Loop " + loopNum + ":" , 0 ) ; somethingDone = false ; for ( row = 0 ; row < rows ; row ++ ) { for ( col = 0 ; col < cols ; col ++ ) { imageValue = image . getValue ( row , col ) ; if ( imageValue != noData ) { patchValue = output . getValue ( row , col ) ; for ( i = 0 ; i < numScanCells ; i ++ ) { x = col + dX [ i ] ; y = row + dY [ i ] ; neighbourPatchValue = output . getValue ( y , x ) ; if ( neighbourPatchValue != patchValue && image . getValue ( y , x ) == imageValue ) { somethingDone = true ; if ( patchValue < neighbourPatchValue ) { reclass [ ( int ) neighbourPatchValue ] = - 1 ; output . setValue ( y , x , patchValue ) ; ScanConnectedCells ( y , x , imageValue , neighbourPatchValue , patchValue ) ; } else { reclass [ ( int ) patchValue ] = - 1 ; output . setValue ( row , col , neighbourPatchValue ) ; ScanConnectedCells ( row , col , imageValue , patchValue , neighbourPatchValue ) ; patchValue = neighbourPatchValue ; } } } } } if ( cancelOp ) { cancelOperation ( ) ; return ; } progress = ( float ) ( 100f * row / ( rows - 1 ) ) ; updateProgress ( ( int ) progress ) ; } } while ( somethingDone ) ; i = 0 ; for ( int a = 0 ; a < maxPatchValue + 1 ; a ++ ) { if ( reclass [ a ] != - 1 ) { reclass [ a ] = i ; i ++ ; } } loopNum ++ ; updateProgress ( "Loop " + loopNum + ":" , 0 ) ; for ( row = 0 ; row < rows ; row ++ ) { for ( col = 0 ; col < cols ; col ++ ) { patchValue = output . getValue ( row , col ) ; if ( patchValue != noData ) { output . setValue ( row , col , reclass [ ( int ) patchValue ] ) ; } } if ( cancelOp ) { cancelOperation ( ) ; return ; } progress = ( float ) ( 100f * row / ( rows - 1 ) ) ; updateProgress ( ( int ) progress ) ; } output . addMetadataEntry ( "Created by the " + getDescriptiveName ( ) + " tool." ) ; output . addMetadataEntry ( "Created on " + new Date ( ) ) ; image . close ( ) ; output . close ( ) ; returnData ( outputHeader ) ; } catch ( OutOfMemoryError oe ) { myHost . showFeedback ( "An out-of-memory error has occurred during operation." ) ; } catch ( Exception e ) { myHost . showFeedback ( "An error has occurred during operation. See log file for details." ) ; myHost . logException ( "Error in " + getDescriptiveName ( ) , e ) ; } finally { updateProgress ( "Progress: " , 0 ) ; amIActive = false ; myHost . pluginComplete ( ) ; } }
used to execute this plugin tool .
