/** Sets a rectangle inside the image with the specified value. */
  public static void fillRectangle(
      ImageFloat32 img, float value, int x0, int y0, int width, int height) {
    int x1 = x0 + width;
    int y1 = y0 + height;

    for (int y = y0; y < y1; y++) {
      for (int x = x0; x < x1; x++) {
        if (img.isInBounds(x, y)) img.set(x, y, value);
      }
    }
  }
  private void shiftCopy(int offX, int offY, ImageFloat32 src, ImageFloat32 dst) {
    for (int y = 0; y < src.height; y++) {
      for (int x = 0; x < src.width; x++) {
        int xx = x + offX;
        int yy = y + offY;

        if (xx >= 0 && xx < src.width && yy >= 0 && yy < src.height) {
          dst.set(xx, yy, src.get(x, y));
        }
      }
    }
  }
 public static void set(ImageSingleBand img, int x, int y, double value) {
   if (ImageInteger.class.isAssignableFrom(img.getClass())) {
     ((ImageInteger) img).set(x, y, (int) value);
   } else if (img instanceof ImageFloat32) {
     ((ImageFloat32) img).set(x, y, (float) value);
   } else if (img instanceof ImageFloat64) {
     ((ImageFloat64) img).set(x, y, value);
   } else {
     throw new IllegalArgumentException(
         "Unknown or incompatible image type: " + img.getClass().getSimpleName());
   }
 }
  public static void process(ImageFloat32 input, ImageFloat32 output, int radius, float storage[]) {
    int w = 2 * radius + 1;
    if (storage == null) {
      storage = new float[w * w];
    } else if (storage.length < w * w) {
      throw new IllegalArgumentException("'storage' must be at least of length " + (w * w));
    }

    for (int y = 0; y < radius; y++) {
      int minI = y - radius;
      int maxI = y + radius + 1;
      if (minI < 0) minI = 0;
      if (maxI > input.height) maxI = input.height;

      for (int x = 0; x < input.width; x++) {
        int minJ = x - radius;
        int maxJ = x + radius + 1;

        // bound it ot be inside the image
        if (minJ < 0) minJ = 0;
        if (maxJ > input.width) maxJ = input.width;

        int index = 0;

        for (int i = minI; i < maxI; i++) {
          for (int j = minJ; j < maxJ; j++) {
            storage[index++] = input.get(j, i);
          }
        }

        // use quick select to avoid sorting the whole list
        float median = QuickSelectArray.select(storage, index / 2, index);
        output.set(x, y, median);
      }
    }

    for (int y = input.height - radius; y < input.height; y++) {
      int minI = y - radius;
      int maxI = y + radius + 1;
      if (minI < 0) minI = 0;
      if (maxI > input.height) maxI = input.height;

      for (int x = 0; x < input.width; x++) {
        int minJ = x - radius;
        int maxJ = x + radius + 1;

        // bound it ot be inside the image
        if (minJ < 0) minJ = 0;
        if (maxJ > input.width) maxJ = input.width;

        int index = 0;

        for (int i = minI; i < maxI; i++) {
          for (int j = minJ; j < maxJ; j++) {
            storage[index++] = input.get(j, i);
          }
        }

        // use quick select to avoid sorting the whole list
        float median = QuickSelectArray.select(storage, index / 2, index);
        output.set(x, y, median);
      }
    }

    for (int y = radius; y < input.height - radius; y++) {
      int minI = y - radius;
      int maxI = y + radius + 1;
      for (int x = 0; x < radius; x++) {
        int minJ = x - radius;
        int maxJ = x + radius + 1;

        // bound it ot be inside the image
        if (minJ < 0) minJ = 0;
        if (maxJ > input.width) maxJ = input.width;

        int index = 0;

        for (int i = minI; i < maxI; i++) {
          for (int j = minJ; j < maxJ; j++) {
            storage[index++] = input.get(j, i);
          }
        }

        // use quick select to avoid sorting the whole list
        float median = QuickSelectArray.select(storage, index / 2, index);
        output.set(x, y, median);
      }
    }

    for (int y = radius; y < input.height - radius; y++) {
      int minI = y - radius;
      int maxI = y + radius + 1;
      for (int x = input.width - radius; x < input.width; x++) {
        int minJ = x - radius;
        int maxJ = x + radius + 1;

        // bound it ot be inside the image
        if (minJ < 0) minJ = 0;
        if (maxJ > input.width) maxJ = input.width;

        int index = 0;

        for (int i = minI; i < maxI; i++) {
          for (int j = minJ; j < maxJ; j++) {
            storage[index++] = input.get(j, i);
          }
        }

        // use quick select to avoid sorting the whole list
        float median = QuickSelectArray.select(storage, index / 2, index);
        output.set(x, y, median);
      }
    }
  }