0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Aimport java.awt.Color;
0N/Aimport java.awt.image.BufferedImage;
0N/A
0N/Apublic class ImageComparator {
0N/A double accuracy;
0N/A int errorCounter = 0;
0N/A double maxError = 0f;
0N/A
0N/A int rMask = 0x00FF0000;
0N/A int gMask = 0x0000FF00;
0N/A int bMask = 0x000000FF;
0N/A
0N/A int rShift = 16;
0N/A int gShift = 8;
0N/A int bShift = 0;
0N/A
0N/A public ImageComparator() {
0N/A accuracy = 0;
0N/A }
0N/A
0N/A public ImageComparator(double accuracy) {
0N/A this.accuracy = accuracy;
0N/A }
0N/A
0N/A public ImageComparator(double accuracy, int rBits, int gBits, int bBits) {
0N/A this.accuracy = accuracy;
0N/A rShift += (8 - rBits);
0N/A gShift += (8 - gBits);
0N/A bShift += (8 - bBits);
0N/A }
0N/A
0N/A public boolean compare(int c1, int c2) {
0N/A int d1 = Math.abs(((c1&bMask)>>bShift) - ((c2&bMask)>>bShift));
0N/A int d2 = Math.abs(((c1&gMask)>>gShift) - ((c2&gMask)>>gShift));
0N/A int d3 = Math.abs(((c1&rMask)>>rShift) - ((c2&rMask)>>rShift));
0N/A if (d1 < d2) d1 = d2;
0N/A if (d1 < d3) d1 = d3;
0N/A if (d1 >= accuracy) {
0N/A errorCounter++;
0N/A if (d1 > maxError) maxError = d1;
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A public boolean compare(double r1, double g1, double b1,
0N/A double r2, double g2, double b2)
0N/A {
0N/A double d1 = Math.abs(r1 - r2);
0N/A double d2 = Math.abs(g1 - g2);
0N/A double d3 = Math.abs(b1 - b2);
0N/A if (d1 < d2) d1 = d2;
0N/A if (d1 < d3) d1 = d3;
0N/A if (d1 >= accuracy) {
0N/A errorCounter++;
0N/A if (d1 > maxError) maxError = d1;
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A public boolean compare(Color c1, Color c2) {
0N/A return compare(c1.getRed(), c1.getGreen(), c1.getBlue(),
0N/A c2.getRed(), c2.getGreen(), c2.getBlue());
0N/A }
0N/A
0N/A public boolean compare(BufferedImage img1, BufferedImage img2) {
0N/A boolean result = true;
0N/A if (img1.getWidth() != img2.getWidth() ||
0N/A img1.getHeight() != img2.getHeight()) {
0N/A throw new IllegalArgumentException(
0N/A "Images have different width or height");
0N/A }
0N/A for (int i = 0; i < img1.getWidth(); i++) {
0N/A for (int j = 0; j < img1.getHeight(); j++) {
0N/A boolean cmp = compare(img1.getRGB(i,j), img2.getRGB(i,j));
0N/A result = cmp && result;
0N/A }
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A public void resetStat() {
0N/A errorCounter = 0;
0N/A maxError = 0;
0N/A }
0N/A
0N/A public String getStat() {
0N/A return "Accuracy " + accuracy + ". Errors " + errorCounter +
0N/A ". Max error " + maxError;
0N/A }
0N/A
0N/A boolean compare(BufferedImage dst, BufferedImage gldImage, int x0, int y0,
0N/A int dx, int dy)
0N/A {
0N/A int width = gldImage.getWidth();
0N/A int height = gldImage.getHeight();
0N/A
0N/A if (x0 < 0) x0 = 0;
0N/A if (x0 > width - dx) x0 = width - dx;
0N/A if (y0 < 0) y0 = 0;
0N/A if (y0 > height - dy) y0 = height - dy;
0N/A
0N/A int c = 0;
0N/A
0N/A boolean result = true;
0N/A for (int i = x0; i < x0 + dx; i++) {
0N/A for (int j = y0; j < y0 + dy; j++) {
0N/A boolean cmp = compare(dst.getRGB(i-x0,j-y0),
0N/A gldImage.getRGB(i,j));
0N/A result = cmp && result;
0N/A }
0N/A }
0N/A return result;
0N/A }
0N/A}