public BufferedImage thresholdTransform(BufferedImage image,double threshold, int grayValue){
    if(image == null){
      return null;
    }

    int width = image.getWidth();
    int height = image.getHeight();

    //calculate color as RGB integer
    int color = (grayValue << 16) | (grayValue << 8) | grayValue;

    BufferedImage result = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
    for(int h=0;h<height;h++){
      for(int w=0;w<width;w++){

        int pixel = image.getRGB(w,h) & 0xff;
        if(pixel >= threshold){
          //set to background color
          result.setRGB(w,h,color);
        }else{
          result.setRGB(w,h,0);
          //splash 1.5 pixels
          if(w>1 && w<width-1 && h>1 && h<height-1){
            result.setRGB(w-1,h,0);
            result.setRGB(w,h-1,0);
            result.setRGB(w+1,h,0);
            result.setRGB(w,h+1,0);
            result.setRGB(w-1,h-1,0);
            result.setRGB(w-1,h+1,0);
            result.setRGB(w+1,h-1,0);
            result.setRGB(w+1,h+1,0);
            result.setRGB(w+2,h,0);
            result.setRGB(w-2,h,0);
            result.setRGB(w,h-2,0);
            result.setRGB(w,h+2,0);
          }
        }
      }
    }
    return result;
  }