Java BufferedImage Compare compare(BufferedImage p1, BufferedImage p2)

Here you can find the source of compare(BufferedImage p1, BufferedImage p2)

Description

compare

License

Apache License

Declaration

public static boolean compare(BufferedImage p1, BufferedImage p2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.image.BufferedImage;
import java.awt.image.Raster;

import java.util.*;

public class Main {
    public static int compare(byte[][] im1, byte[][] im2) {
        int ret = 0;
        for (int y = 0; y < im1.length; y++) {
            for (int x = 0; x < im1[0].length / 3; x++) {
                if (im1[y][x * 3 + 0] != im2[y][x * 3 + 0] || im1[y][x * 3 + 1] != im2[y][x * 3 + 1]
                        || im1[y][x * 3 + 2] != im2[y][x * 3 + 2])
                    ++ret;//from www  .  j a  v a2s.  c  om
            }
        }
        return ret;
    }

    public static boolean compare(BufferedImage p1, BufferedImage p2) {
        if (p1 == null) {
            return p2 == null;
        } else if (p2 == null) {
            return false;
        }

        Raster r1 = p1.getRaster();
        Raster r2 = p2.getRaster();

        return compare(r1, r2);
    }

    public static boolean compare(Raster r1, Raster r2) {
        if (r1.getWidth() != r2.getWidth() || r1.getHeight() != r2.getHeight()) {
            return false;
        }

        int[] b1 = new int[4];
        int[] b2 = new int[4];

        for (int y = 0; y < r1.getHeight(); y++) {
            for (int x = 0; x < r1.getWidth(); x++) {
                r1.getPixel(x, y, b1);
                r2.getPixel(x, y, b2);
                if (!Arrays.equals(b1, b2)) {
                    return false;
                }
            }
        }
        return true;
    }
}

Related

  1. compare(BufferedImage a, BufferedImage b)
  2. compareBufferedImages(BufferedImage bufferedImage1, BufferedImage bufferedImage2)
  3. compareImage(BufferedImage biA, BufferedImage biB)
  4. compareImage(BufferedImage img1, BufferedImage img2)
  5. compareImage(BufferedImage img1, BufferedImage img2)