/**
   * Scales the range of <code>image</code> to an arbitrary min/max in a Region of Interest. The
   * range used for all planes is the global max, min of the image.
   *
   * @param image RealColorImage to scale.
   * @param roi Region of Interest of <code>image</code>.
   * @return <code>image</code>.
   */
  protected Image apply(RealColorImage image, ROI roi) {
    RealGrayImage plane;

    float min = image.min(roi);
    float max = image.max(roi);
    float r = 0;
    float range = max - min;
    float value = 0;
    if (range == 0) range = 1; // to avoid divide by zero problems

    for (int i = 0; i < 3; i++) {
      plane = image.plane(i);

      for (int y = roi.uy(); y <= roi.ly(); y++) {
        for (int x = roi.ux(); x <= roi.lx(); x++) {
          r = plane.get(x, y);
          value = (((r - min) / (range) * (float_max - float_min)) + float_min);
          plane.set(x, y, value);
        }
      }

      image.setPlane(i, plane);
    }

    return image;
  }
  /**
   * Scales the range of <code>image</code> to an arbitrary min/max in a Region of Interest.
   *
   * @param image RealGrayImage to scale.
   * @param roi Region of Interest of <code>image</code>.
   * @return <code>image</code>.
   */
  protected Image apply(RealGrayImage image, ROI roi) {
    float min = image.min(roi);
    float max = image.max(roi);

    float r = 0;
    float range = max - min;

    if (range == 0)
      range = 1; // to avoid divide by zero (r-min will be zero so value will always be float_min)

    float value = 0;
    for (int y = roi.uy(); y <= roi.ly(); y++) {
      for (int x = roi.ux(); x <= roi.lx(); x++) {
        r = (float) image.get(x, y);
        value = ((float) ((r - min) / (range)) * (float_max - float_min)) + float_min;
        image.set(x, y, (float) value);
      }
    }
    return image;
  }
  /**
   * Scales the range of <code>image</code> to an arbitrary min/max in a Region of Interest.
   *
   * @param image GrayImage to scale.
   * @param roi Region of Interest of <code>image</code>
   * @return <code>image</code>.
   */
  protected Image apply(GrayImage image, ROI roi) {
    int int_max = (int) float_max;
    int int_min = (int) float_min;

    float min = image.min(roi);
    float max = image.max(roi);
    float r = 0;
    float range = max - min;

    if (range == 0) range = 1; // to avoid divide by zero problems

    float value = 0;
    for (int y = roi.uy(); y <= roi.ly(); y++) {
      for (int x = roi.ux(); x <= roi.lx(); x++) {
        r = (float) image.get(x, y);
        value = (((r - min) / (range) * (int_max - int_min)) + int_min);
        image.set(x, y, (short) value);
      }
    }
    return image;
  }