Program: 22773
Project: 22773
Buggy source:
/** 
 * Use simple point relaxation to warp 2D source into 2D target.  Source is only modified at nonzero locations in the mask
 * @param source input 2D image to be relaxed
 * @param target target 2D image
 * @param mask 2D image mask
 * @param binaryThreshold binarization threshold
 * @return warped source image and warping error
 */
public static WarpingResults simplePointWarp2d(final ImageProcessor source,final ImageProcessor target,final ImageProcessor mask,double binaryThreshold){
  if (binaryThreshold < 0 || binaryThreshold > 1)   binaryThreshold=0.5;
  final ImagePlus targetReal=new ImagePlus("String_Node_Str",target.duplicate());
  final ImagePlus targetBin=new ImagePlus("String_Node_Str",target.duplicate());
  final ImagePlus sourceReal=new ImagePlus("String_Node_Str",source.duplicate());
  final ImagePlus maskReal=(null != mask) ? new ImagePlus("String_Node_Str",mask.duplicate().convertToFloat()) : null;
  final int width=target.getWidth();
  final int height=target.getHeight();
  IJ.run(targetReal,"String_Node_Str","String_Node_Str" + (width + 2) + "String_Node_Str"+ (height + 2)+ "String_Node_Str");
  IJ.run(targetBin,"String_Node_Str","String_Node_Str" + (width + 2) + "String_Node_Str"+ (height + 2)+ "String_Node_Str");
  IJ.run(sourceReal,"String_Node_Str","String_Node_Str" + (width + 2) + "String_Node_Str"+ (height + 2)+ "String_Node_Str");
  if (null != maskReal)   IJ.run(maskReal,"String_Node_Str","String_Node_Str" + (width + 2) + "String_Node_Str"+ (height + 2)+ "String_Node_Str");
  final float[] sourceRealPix=(float[])sourceReal.getProcessor().getPixels();
  for (int i=0; i < sourceRealPix.length; i++)   if (sourceRealPix[i] > 0)   sourceRealPix[i]=1.0f;
  final float[] targetBinPix=(float[])targetBin.getProcessor().getPixels();
  for (int i=0; i < targetBinPix.length; i++)   targetBinPix[i]=(targetBinPix[i] > binaryThreshold) ? 1.0f : 0.0f;
  double diff=Double.MIN_VALUE;
  double diff_before=0;
  while (true) {
    ImageProcessor missclass_points_image=sourceReal.getProcessor().duplicate();
    missclass_points_image.copyBits(targetBin.getProcessor(),0,0,Blitter.DIFFERENCE);
    diff_before=diff;
    float pixels[]=(float[])missclass_points_image.getPixels();
    float mask_pixels[]=(null != maskReal) ? (float[])maskReal.getProcessor().getPixels() : new float[pixels.length];
    if (null == maskReal)     Arrays.fill(mask_pixels,1f);
    diff=0;
    for (int k=0; k < pixels.length; k++)     if (pixels[k] != 0 && mask_pixels[k] != 0)     diff++;
    if (diff == diff_before || diff == 0)     break;
    final ArrayList<Point3f> mismatches=new ArrayList<Point3f>();
    final float[] realTargetPix=(float[])targetReal.getProcessor().getPixels();
    for (int x=1; x < width + 1; x++)     for (int y=1; y < height + 1; y++) {
      if (pixels[x + y * (width + 2)] != 0 && mask_pixels[x + y * (width + 2)] != 0)       mismatches.add(new Point3f(x,y,(float)Math.abs(realTargetPix[x + y * (width + 2)] - binaryThreshold)));
    }
    Collections.sort(mismatches,new Comparator<Point3f>(){
      public int compare(      Point3f o1,      Point3f o2){
        return (int)((o2.z - o1.z) * 10000);
      }
    }
);
    for (    final Point3f p : mismatches) {
      final int x=(int)p.x;
      final int y=(int)p.y;
      double[] val=new double[]{sourceRealPix[(x - 1) + (y - 1) * (width + 2)],sourceRealPix[(x) + (y - 1) * (width + 2)],sourceRealPix[(x + 1) + (y - 1) * (width + 2)],sourceRealPix[(x - 1) + (y) * (width + 2)],sourceRealPix[(x) + (y) * (width + 2)],sourceRealPix[(x + 1) + (y) * (width + 2)],sourceRealPix[(x - 1) + (y + 1) * (width + 2)],sourceRealPix[(x) + (y + 1) * (width + 2)],sourceRealPix[(x + 1) + (y + 1) * (width + 2)]};
      final double pix=val[4];
      final ImagePlus patch=new ImagePlus("String_Node_Str",new FloatProcessor(3,3,val));
      if (simple2D(patch,4)) {
        sourceRealPix[x + y * (width + 2)]=pix > 0.0 ? 0.0f : 1.0f;
      }
    }
  }
  IJ.run(sourceReal,"String_Node_Str","String_Node_Str" + width + "String_Node_Str"+ height+ "String_Node_Str");
  WarpingResults result=new WarpingResults();
  result.warpedSource=sourceReal;
  result.warpingError=diff / (width * height);
  return result;
}

Fixed version (ground truth):
/** 
 * Use simple point relaxation to warp 2D source into 2D target.  Source is only modified at nonzero locations in the mask
 * @param source input 2D image to be relaxed
 * @param target target 2D image
 * @param mask 2D image mask
 * @param binaryThreshold binarization threshold
 * @return warped source image and warping error
 */
public static WarpingResults simplePointWarp2d(final ImageProcessor source,final ImageProcessor target,final ImageProcessor mask,double binaryThreshold){
  if (binaryThreshold < 0 || binaryThreshold > 1)   binaryThreshold=0.5;
  final ImagePlus targetReal;
  final ImagePlus targetBin;
  final ImagePlus sourceReal;
  final ImagePlus maskReal;
  final int width=target.getWidth();
  final int height=target.getHeight();
  ImageProcessor ip=target.createProcessor(width + 2,height + 2);
  ip.insert(target,1,1);
  targetReal=new ImagePlus("String_Node_Str",ip.duplicate());
  targetBin=new ImagePlus("String_Node_Str",ip.duplicate());
  ip=target.createProcessor(width + 2,height + 2);
  ip.insert(source,1,1);
  sourceReal=new ImagePlus("String_Node_Str",ip.duplicate());
  if (null != mask) {
    ip=target.createProcessor(width + 2,height + 2);
    ip.insert(mask,1,1);
    maskReal=new ImagePlus("String_Node_Str",ip.duplicate());
  }
 else {
    maskReal=null;
  }
  final float[] sourceRealPix=(float[])sourceReal.getProcessor().getPixels();
  for (int i=0; i < sourceRealPix.length; i++)   if (sourceRealPix[i] > 0)   sourceRealPix[i]=1.0f;
  final float[] targetBinPix=(float[])targetBin.getProcessor().getPixels();
  for (int i=0; i < targetBinPix.length; i++)   targetBinPix[i]=(targetBinPix[i] > binaryThreshold) ? 1.0f : 0.0f;
  double diff=Double.MIN_VALUE;
  double diff_before=0;
  while (true) {
    ImageProcessor missclass_points_image=sourceReal.getProcessor().duplicate();
    missclass_points_image.copyBits(targetBin.getProcessor(),0,0,Blitter.DIFFERENCE);
    diff_before=diff;
    float pixels[]=(float[])missclass_points_image.getPixels();
    float mask_pixels[]=(null != maskReal) ? (float[])maskReal.getProcessor().getPixels() : new float[pixels.length];
    if (null == maskReal)     Arrays.fill(mask_pixels,1f);
    diff=0;
    for (int k=0; k < pixels.length; k++)     if (pixels[k] != 0 && mask_pixels[k] != 0)     diff++;
    if (diff == diff_before || diff == 0)     break;
    final ArrayList<Point3f> mismatches=new ArrayList<Point3f>();
    final float[] realTargetPix=(float[])targetReal.getProcessor().getPixels();
    for (int x=1; x < width + 1; x++)     for (int y=1; y < height + 1; y++) {
      if (pixels[x + y * (width + 2)] != 0 && mask_pixels[x + y * (width + 2)] != 0)       mismatches.add(new Point3f(x,y,(float)Math.abs(realTargetPix[x + y * (width + 2)] - binaryThreshold)));
    }
    Collections.sort(mismatches,new Comparator<Point3f>(){
      public int compare(      Point3f o1,      Point3f o2){
        return (int)((o2.z - o1.z) * 10000);
      }
    }
);
    for (    final Point3f p : mismatches) {
      final int x=(int)p.x;
      final int y=(int)p.y;
      double[] val=new double[]{sourceRealPix[(x - 1) + (y - 1) * (width + 2)],sourceRealPix[(x) + (y - 1) * (width + 2)],sourceRealPix[(x + 1) + (y - 1) * (width + 2)],sourceRealPix[(x - 1) + (y) * (width + 2)],sourceRealPix[(x) + (y) * (width + 2)],sourceRealPix[(x + 1) + (y) * (width + 2)],sourceRealPix[(x - 1) + (y + 1) * (width + 2)],sourceRealPix[(x) + (y + 1) * (width + 2)],sourceRealPix[(x + 1) + (y + 1) * (width + 2)]};
      final double pix=val[4];
      final ImagePlus patch=new ImagePlus("String_Node_Str",new FloatProcessor(3,3,val));
      if (simple2D(patch,4)) {
        sourceRealPix[x + y * (width + 2)]=pix > 0.0 ? 0.0f : 1.0f;
      }
    }
  }
  ip=source.createProcessor(width,height);
  ip.insert(sourceReal.getProcessor(),-1,-1);
  sourceReal.setProcessor(ip.duplicate());
  WarpingResults result=new WarpingResults();
  result.warpedSource=sourceReal;
  result.warpingError=diff / (width * height);
  return result;
}