view src/org/gfxtest/ImageDiffer/ImageComparator.java @ 809:ab44756e58f8 draft

Javadoc for updateBoundary and drawRectangleAroundDifferentPixels.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Mon, 21 Mar 2016 11:18:24 +0100
parents 70dddc9b0917
children 38e1f8d55fde
line wrap: on
line source

/*
  Java gfx-test framework

   Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016  Red Hat

This file is part of IcedTea.

IcedTea is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

IcedTea is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with IcedTea; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.
*/

package org.gfxtest.ImageDiffer;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;

/**
 * Comparation of two images according to given comparation parameters.
 *
 * @author Pavel Tisnovsky <ptisnovs@redhat.com>
 */
public class ImageComparator
{
    /**
     * Updates the boundary stored as two Points. Boundary specifies a
     * rectangular area and when new point is added, it could lie inside the
     * boundary (not change) or outside it (boundary needs to be enlarged to
     * contain the new point).
     * 
     * @param min - first point that specifies boundary of rectangular area
     * @param max - the second that specifies boundary of rectangular area
     * @param x - x-coordinate of new point to be added into rectangular area
     * @param y - y-coordinate of new point to be added into rectangular area
     */
    private void updateBoundary(Point min, Point max, int x, int y)
    {
        min.x = Math.min(min.x, x);
        min.y = Math.min(min.y, y);
        max.x = Math.max(max.x, x);
        max.y = Math.max(max.y, y);
    }

    /**
     * Draw rectangle around the area that contain different pixels.
     * 
     * @param configuration - data structure that contains border width around rectangular area
     * @param min - first point that specifies boundary of rectangular area
     * @param max - the second that specifies boundary of rectangular area
     * @param width - width of the area
     * @param height - height of the area
     * @param diffImage - image to draw to
     */
    private void drawRectangleAroundDifferentPixels(Configuration configuration, Point min, Point max, int width,
            int height, BufferedImage diffImage)
    {
        int x1 = Math.max(0, min.x - configuration.getBoundarySize());
        int y1 = Math.max(0, min.y - configuration.getBoundarySize());
        int x2 = Math.min(width - 1, max.x + configuration.getBoundarySize());
        int y2 = Math.min(height - 1, max.y + configuration.getBoundarySize());
        Graphics g = diffImage.createGraphics();
        g.setColor(configuration.getBoundaryColor());
        g.drawRect(x1, y1, x2 - x1, y2 - y1);
        g.dispose();
    }

    public ComparisonResult diffImages(String imageFileName, BufferedImage[] images, Configuration configuration)
    {
        Point min = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE);
        Point max = new Point(Integer.MIN_VALUE, Integer.MIN_VALUE);
        int width = Math.min(images[0].getWidth(), images[1].getWidth());
        int height = Math.min(images[0].getHeight(), images[1].getHeight());
        int totalPixels = 0;
        int differentPixels = 0;
        int smallDifferences = 0;
        int equalPixels = 0;
        BufferedImage diffImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int j = 0; j < height; j++)
        {
            for (int i = 0; i < width; i++)
            {
                totalPixels ++;
                int cmp = ImageUtils.compare(images[0].getRGB(i, j), images[1].getRGB(i, j));
                Color color = ImageUtils.rgb2grayscale(images[0].getRGB(i, j));
                if (cmp > configuration.getDifferentPixelsThreshold())
                {
                    differentPixels ++;
                    updateBoundary(min, max, i, j);
                    color = configuration.getDiffColorPerceptiblePixelDifference();
                }
                else if (cmp > 0)
                {
                    smallDifferences ++;
                    updateBoundary(min, max, i, j);
                    color = configuration.getDiffColorPixelValueUnderThreshold();
                }
                else
                {
                    equalPixels ++;
                }
                diffImage.setRGB(i, j, color.getRGB());
            }
        }
        boolean equalImages = min.x == Integer.MAX_VALUE;
        if (!equalImages)
        {
            drawRectangleAroundDifferentPixels(configuration, min, max, width, height, diffImage);
        }
        else
        {
            min = new Point(-1, -1);
            max = new Point(-1, -1);
        }
        return new ComparisonResult(imageFileName, equalImages, diffImage, min, max, width, height, totalPixels, differentPixels, smallDifferences, equalPixels);
    }

}