title: How to replace color with another color in BufferedImage
body: <p>So I have an image file that has a volcano on it. Everything else is 0xFFFF00FF (opaque magenta). I want to replace every pixel that contains that color with 0 (transparent). So far my method looks like this:</p>
  
  <pre><code>public static BufferedImage replace(BufferedImage image, int target, int preferred) {
      int width = image.getWidth();
      int height = image.getHeight();
      BufferedImage newImage = new BufferedImage(width, height, image.getType());
      int color;
  
      for (int i = 0; i &lt; width; i++) {
          for (int j = 0; j &lt; height; j++) {
              color = image.getRGB(i, j);
              if (color == target) {
                  newImage.setRGB(i, j, preferred);
              }
              else {
                  newImage.setRGB(i, j, color);
              }
          }
      }
  
      return newImage;
  }
  </code></pre>
  
  <p>This works fine but seems VERY slow. I have seen someone do this another way, but I have no idea what was going on. If someone knows a better way to do this, I would very much like to hear it.</p>