Java Image absoluteDifferenceImage(int[] rgb1, int[] rgb2, double scale)

Here you can find the source of absoluteDifferenceImage(int[] rgb1, int[] rgb2, double scale)

Description

Generate the absolute difference image between two images

License

Open Source License

Parameter

Parameter Description
rgb1 the first RGB array
rgb2 the second RGB array
scale how much to scale the result

Return

new rgb array

Declaration

public static int[] absoluteDifferenceImage(int[] rgb1, int[] rgb2, double scale) 

Method Source Code

//package com.java2s;
/*//from www.  j  a  va2 s .  c o m
 * ArikTools
 * Copyright (C) Arik Z.Lakritz, Peter Macko, and David K. Wittenberg
 * 
 * This file is part of ArikTools.
 *
 * ArikTools 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 3 of the License, or
 * (at your option) any later version.
 *
 * ArikTools 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 ArikTools.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.image.*;

import javax.swing.*;

public class Main {
    /**
     * Generate the absolute difference image between two images
     *
     * @param rgb1 the first RGB array
     * @param rgb2 the second RGB array
     * @param scale how much to scale the result
     * @return new rgb array
     */
    public static int[] absoluteDifferenceImage(int[] rgb1, int[] rgb2, double scale) {

        if (rgb1.length != rgb2.length)
            throw new RuntimeException("The two images need to have the same number of pixels");
        int[] out = new int[rgb1.length];

        for (int i = 0; i < rgb1.length; i++) {

            int c1 = rgb1[i];
            int r1 = (c1 >> 16) & 0xff;
            int g1 = (c1 >> 8) & 0xff;
            int b1 = (c1) & 0xff;

            int c2 = rgb2[i];
            int r2 = (c2 >> 16) & 0xff;
            int g2 = (c2 >> 8) & 0xff;
            int b2 = (c2) & 0xff;

            int r = Math.round(Math.max(Math.min(Math.round(Math.abs(r1 - r2) * scale), 255), 0));
            int g = Math.round(Math.max(Math.min(Math.round(Math.abs(g1 - g2) * scale), 255), 0));
            int b = Math.round(Math.max(Math.min(Math.round(Math.abs(b1 - b2) * scale), 255), 0));

            out[i] = (r << 16) | (g << 8) | b;
        }

        return out;
    }

    /**
     * Generate the absolute difference image between two images
     *
     * @param icon1 the first image icon
     * @param icon2 the second image icon
     * @param scale how much to scale the result
     * @return the difference buffered image
     */
    public static BufferedImage absoluteDifferenceImage(ImageIcon icon1, ImageIcon icon2, double scale) {
        return plotRGB(absoluteDifferenceImage(grabRGB(icon1), grabRGB(icon2), scale), icon1.getIconWidth(),
                icon1.getIconHeight());
    }

    /**
     * Create a buffered image from an RGB array
     *
     * @param rgb the RGB array
     * @param width the image width
     * @param height the image height
     * @return the buffered image
     */
    public static BufferedImage plotRGB(int rgb[], int width, int height) {

        BufferedImage b = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        for (int y = 0; y < height; y++) {
            int base = y * width;
            for (int x = 0; x < width; x++) {
                b.setRGB(x, y, rgb[base + x]);
            }
        }

        return b;
    }

    /**
     * Grabs a RGB values of the given image icon
     *
     * @param icon the image icon
     * @return an array of RGB values
     */
    public static int[] grabRGB(ImageIcon icon) {

        int width = icon.getIconWidth();
        int height = icon.getIconHeight();

        int[] rgb = new int[width * height];
        PixelGrabber pg = new PixelGrabber(icon.getImage(), 0, 0, width, height, rgb, 0, width);

        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted waiting for pixels!");
        }

        if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
            throw new RuntimeException("Image fetch aborted or errored");
        }

        for (int i = 0; i < rgb.length; i++)
            rgb[i] &= 0xffffff;

        return rgb;
    }
}

Related

  1. checkLosslessImageExt(Component parent, File f)
  2. configureMacOSApplication(String appName, String dockImagePath)
  3. CreateCopy(Image srcImg)
  4. createCursor(Image cursorImage, Point hotSpot, String name)