Java BufferedImage Operation fuzzyEquals(BufferedImage a, BufferedImage b, int threshold)

Here you can find the source of fuzzyEquals(BufferedImage a, BufferedImage b, int threshold)

Description

fuzzy Equals

License

Open Source License

Declaration

public static boolean fuzzyEquals(BufferedImage a, BufferedImage b, int threshold) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.image.BufferedImage;

public class Main {
    public static boolean fuzzyEquals(BufferedImage a, BufferedImage b, int threshold) {
        int aw = a.getWidth();
        int ah = a.getHeight();
        int bw = b.getWidth();
        int bh = b.getHeight();
        if ((aw != bw) || (ah != bh))
            return false;
        for (int y = 0; y < ah; y++)
            for (int x = 0; x < bw; x++) {
                if (!fuzzyColorMatch(a.getRGB(x, y), b.getRGB(x, y), threshold)) {
                    return false;
                }/*from   ww  w  .  j a va2s.c o m*/
            }
        return true;
    }

    public static boolean fuzzyColorMatch(int color1, int color2, int threshold) {
        if (color1 == color2)
            return true;
        int difference = Math.abs((color1 & 0xFF) - (color2 & 0xFF));
        if (difference > threshold)
            return false;
        difference += Math.abs((color1 >> 8 & 0xFF) - (color2 >> 8 & 0xFF));
        if (difference > threshold)
            return false;
        difference += Math.abs((color1 >> 16 & 0xFF) - (color2 >> 16 & 0xFF));
        if (difference > threshold)
            return false;
        difference += Math.abs((color1 >> 24 & 0xFF) - (color2 >> 24 & 0xFF));
        return difference <= threshold;
    }
}

Related

  1. findTranslation(AffineTransform at, BufferedImage bi)
  2. fixImage(BufferedImage img, String ext)
  3. floatBufferToGrayBufferedImage(FloatBuffer floatBuffer, BufferedImage bi)
  4. floodFill(BufferedImage img, int startX, int startY, Color targetColor, Color replacementColor)
  5. fuzzyCompare(final BufferedImage img1, final BufferedImage img2, final double colorTolerance, final double pixelTolerance, final int fuzzyBlockDimension)
  6. genBlackAndWhiteImage(BufferedImage image)
  7. generate(int w, int h, BufferedImage img)
  8. generateBufferedImageFromComponent(Component component)
  9. generateImageHash(BufferedImage image)