Java BufferedImage Operation fadeImages(BufferedImage source1, BufferedImage source2, BufferedImage target, int relX, int targetX)

Here you can find the source of fadeImages(BufferedImage source1, BufferedImage source2, BufferedImage target, int relX, int targetX)

Description

fade Images

License

Open Source License

Declaration

public static void fadeImages(BufferedImage source1, BufferedImage source2, BufferedImage target, int relX,
            int targetX) 

Method Source Code

//package com.java2s;
/* VisNow//from w w  w .  j a  v a 2 s  .  co m
   Copyright (C) 2006-2013 University of Warsaw, ICM
    
This file is part of GNU Classpath.
    
GNU Classpath 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.
    
GNU Classpath 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 GNU Classpath; see the file COPYING.  If not, write to the 
University of Warsaw, Interdisciplinary Centre for Mathematical and 
Computational Modelling, Pawinskiego 5a, 02-106 Warsaw, Poland. 
    
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. */

import java.awt.image.*;

public class Main {
    public static void fadeImages(BufferedImage source1, BufferedImage source2, BufferedImage target, int relX,
            int targetX) {
        int pixel1, pixel2, newPixel;
        double f;
        int r1, g1, b1, r2, g2, b2;
        byte newR, newG, newB;
        ColorModel cm = source1.getColorModel();

        for (int x = relX; x < source1.getWidth(); x++) {
            f = linearF(x, relX, source1.getWidth());
            for (int y = 0; y < source1.getHeight(); y++) {
                pixel1 = source1.getRGB(x, y);
                pixel2 = source2.getRGB(x - relX, y);

                r1 = cm.getRed(pixel1);
                g1 = cm.getGreen(pixel1);
                b1 = cm.getBlue(pixel1);
                r2 = cm.getRed(pixel2);
                g2 = cm.getGreen(pixel2);
                b2 = cm.getBlue(pixel2);

                int tr = 10;

                if (r1 < tr && g1 < tr && b1 < tr) {
                    newPixel = pixel2;
                } else if (r2 < tr && g2 < tr && b2 < tr) {
                    newPixel = pixel1;
                } else {
                    newR = (byte) Math.round(((double) r1) * (1 - f) + ((double) r2) * f);
                    newG = (byte) Math.round(((double) g1) * (1 - f) + ((double) g2) * f);
                    newB = (byte) Math.round(((double) b1) * (1 - f) + ((double) b2) * f);
                    newPixel = (newR & 0xff) << 16 | (newG & 0xff) << 8 | (newB & 0xff) << 0;
                }
                target.setRGB(x + targetX, y, newPixel);
            }

        }
    }

    private static double linearF(int x, int x0, int x1) {
        //funkca ma zwracac wartosci z przedzialu (0,1) na przedziale (x0,x1)
        return ((double) (x - x0) / (double) (x1 - x0));
    }
}

Related

  1. duplicateImage(BufferedImage image)
  2. dye(BufferedImage image, Color color)
  3. ensureIntRGB(final BufferedImage img)
  4. ensureRGBAImage(BufferedImage img)
  5. fadeImageByShape(BufferedImage bimg, Shape arbshape, double alpha, int rule)
  6. fakeAOI(final BufferedImage pImage, final Rectangle pSourceRegion)
  7. findavg(BufferedImage bimg)
  8. findColor(BufferedImage image, int startX, int startY, int dirX, int dirY, int colorIndex)
  9. findDifference(BufferedImage img2, BufferedImage img1)