Ejemplo n.º 1
0
  public static BufferedImage toGrayScale(BufferedImage image) {
    BufferedImage result =
        new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    Raster raster = image.getData();
    WritableRaster grayRaster = result.getRaster();
    int height = raster.getHeight();
    int sample = 0;

    for (int x = 0; x < raster.getWidth(); ++x) {
      for (int y = 0; y < height; ++y) {

        sample = raster.getSample(x, y, 0) + raster.getSample(x, y, 1) + raster.getSample(x, y, 2);

        grayRaster.setSample(x, y, 0, (int) ((float) sample / 3.0));
      }
    }
    return result;
  }