Java BufferedImage Operation stitchImages(BufferedImage[] images, int[] relX)

Here you can find the source of stitchImages(BufferedImage[] images, int[] relX)

Description

stitch Images

License

Open Source License

Declaration

public static BufferedImage stitchImages(BufferedImage[] images, int[] relX) 

Method Source Code

//package com.java2s;
/* VisNow// w ww  . ja va2 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 BufferedImage stitchImages(BufferedImage[] images, int[] relX) {
        if (images == null || images.length < 2) {
            return null;
        }

        int[] xMax = max(relX);
        int x0 = relX[0];
        int xmax = xMax[0];
        int xmaxIndex = xMax[1];
        int W = xmax + images[xmaxIndex].getWidth();
        int H = images[0].getHeight();
        BufferedImage out = new BufferedImage(W, H, images[0].getType());
        int[] relRelX = new int[relX.length];
        relRelX[0] = 0;

        for (int i = 1; i < relX.length; i++) {
            relRelX[i] = relX[i] - relX[i - 1];
            //System.out.println("relRelX["+i+"]="+relRelX[i]);
        }

        for (int i = 0; i < images.length; i++) {
            if (i == 0) {
                //dla 0 obrazka
                copyRGBcolumns(images[0], 0, relX[1], out, 0);
                fadeImages(images[0], images[1], out, relX[1], relX[0]);
            } else if (i == images.length - 1) {
                //dla ostatniego obrazka
                int tmp1 = relX[i - 1] + images[i - 1].getWidth();
                copyRGBcolumns(images[i], tmp1 - relX[i], W - tmp1, out, tmp1);
            } else {
                //dla innych obrazkow
                int tmp1 = relX[i - 1] + images[i - 1].getWidth();
                copyRGBcolumns(images[i], tmp1 - relX[i], relX[i + 1] - tmp1, out, tmp1);
                fadeImages(images[i], images[i + 1], out, relRelX[i + 1], relX[i]);
            }
        }
        return out;
    }

    public static int[] max(int[] arr) {
        int[] out = new int[2];
        out[0] = arr[0];
        out[1] = 0;

        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > out[0]) {
                out[0] = arr[i];
                out[1] = i;
            }
        }
        return out;
    }

    public static void copyRGBcolumns(BufferedImage sourceImg, int sourceStartX, int sourceWidth,
            BufferedImage targetImg, int targetStartX) {
        targetImg.setRGB(targetStartX, 0, sourceWidth, sourceImg.getHeight(),
                sourceImg.getRGB(sourceStartX, 0, sourceWidth, sourceImg.getHeight(), null, 0, sourceWidth), 0,
                sourceWidth);
    }

    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. setPixel(BufferedImage img, int x, int y, int c)
  2. setPoint(BufferedImage image, int x, int y, int factor, Color color)
  3. showColors(BufferedImage image, boolean alpha, boolean red, boolean green, boolean blue)
  4. shrink(BufferedImage source, double factor)
  5. smaller(final BufferedImage src, final int radius, final int opaqueLimit)
  6. switchAxes(BufferedImage img)
  7. symmetrifyY(BufferedImage image, boolean useFirstHalfImage, boolean flipVertical)
  8. testNeighbours(BufferedImage source, int x, int y, boolean alpha)
  9. texture2D(BufferedImage normalMap, int x, int y)