/**
   * 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 ColorImage to scale.
   * @param roi Region of Interest of <code>image</code>.
   * @return <code>image</code>.
   */
  protected Image apply(ColorImage image, ROI roi) {
    GrayImage plane;
    int int_max = (int) float_max;
    int int_min = (int) float_min;
    float min = (float) image.min(roi);
    float max = (float) 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 = (float) plane.get(x, y);
          value = (((r - min) / (range) * (int_max - int_min)) + int_min);
          plane.set(x, y, (short) value);
        }
      }

      image.setPlane(i, plane);
    }

    return image;
  }
  /**
   * Writes a GrayImage to a file
   *
   * @param im the GrayImage
   */
  public void write(GrayImage im) throws IOException {

    X = im.X();
    Y = im.Y();

    // convert to byte size
    GrayImage tmpim = (GrayImage) im.copy();
    tmpim.byteSize();

    // write PGM in raw format
    writeRawPGMHeader(X, Y);
    for (int y = 0; y < Y; y++) {
      for (int x = 0; x < X; x++) {
        data.write((byte) tmpim.get(x, y));
      }
    }
  }
 /**
  * Scales the range of <code>image</code> to an arbitrary min/max.
  *
  * @param image GrayImage to scale.
  * @return <code>image</code>.
  */
 protected Image apply(GrayImage image) {
   return apply(image, new ROI(0, 0, image.X() - 1, image.Y() - 1));
 }