title: High performance red-filter on Java BufferedImage
body: <p>Let's say I have a <code>BufferedImage</code> with <code>ARGB</code> channels. I can turn this image into a grayscale image simply by doing</p>
  
  <pre><code>BufferedImage copy = new BufferedImage(image.getWidth(), image.getHeight(),  
          BufferedImage.TYPE_BYTE_GRAY);  
  Graphics g = copy.getGraphics().create();  
  g.drawImage(image, 0, 0, null);  
  g.dispose();
  </code></pre>
  
  <p>There are a couple of other methods to do grayscale conversion I'm aware of, but this one works good for my program. I can also (and do) enhance the contrast of the image by doing this:</p>
  
  <pre><code>RescaleOp op;
  op = new RescaleOp(1.0f, darken, null);
  op.filter(copy, copy);
  op = new RescaleOp(brighten, 0.0f, null);
  op.filter(copy, copy);
  </code></pre>
  
  <p>But there's a problem. Sometimes there are slightly dark-red parts of my image that I need to isolate, which are close to slightly bright regions, that is, regions with a high red value (such as yellow and purple). I need to isolate these red regions. How can I do this efficiently?</p>
  
  <p>Manually, I would like something like</p>
  
  <pre><code>for each pixel p in original
      new.p = grayscale(Math.max(Math.abs(p.red - p.green), Math.abs(p.red - p.blue)))
  </code></pre>
  
  <p>Can I do this more efficiently using built-in filters or the like? I'm not looking for an exact filter - just something to help me on the way of isolating these red areas a bit. This kind of code makes me think there's an efficient way: this is for producing lower-quality grayscale images, but it is very fast.</p>
  
  <pre><code>ImageFilter filter = new GrayFilter(true, 50);  
  ImageProducer producer = new FilteredImageSource(colorImage.getSource(), filter);  
  Image mage = this.createImage(producer);  
  </code></pre>
  
  <p>Thanks for any help and suggestions!</p>