How to effectively color pixels in a BufferedImage?
<p>I'm using the following piece of code to iterate over all pixels in an image and draw a red 1x1 square over the pixels that are within a certain RGB-tolerance. I guess there is a more efficient way to do this? Any ideas appreciated. (<code>bi</code> is a <code>BufferedImage</code> and <code>g2</code> is a <code>Graphics2D</code> with its color set to <code>Color.RED</code>).</p>

<pre><code>    Color targetColor = new Color(selectedRGB);

    for (int x = 0; x &lt; bi.getWidth(); x++) {
        for (int y = 0; y &lt; bi.getHeight(); y++) {
            Color pixelColor = new Color(bi.getRGB(x, y));
            if (withinTolerance(pixelColor, targetColor)) {
                g2.drawRect(x, y, 1, 1);
            }
        }
    }

private boolean withinTolerance(Color pixelColor, Color targetColor) {
    int pixelRed = pixelColor.getRed();
    int pixelGreen = pixelColor.getGreen();
    int pixelBlue = pixelColor.getBlue();

    int targetRed = targetColor.getRed();
    int targetGreen = targetColor.getGreen();
    int targetBlue = targetColor.getBlue();

    return (((pixelRed &gt;= targetRed - tolRed) &amp;&amp; (pixelRed &lt;= targetRed + tolRed)) &amp;&amp;
            ((pixelGreen &gt;= targetGreen - tolGreen) &amp;&amp; (pixelGreen &lt;= targetGreen + tolGreen)) &amp;&amp;
            ((pixelBlue &gt;= targetBlue - tolBlue) &amp;&amp; (pixelBlue &lt;= targetBlue + tolBlue)));
}
</code></pre>
