Java BufferedImage Operation autoPanImage(BufferedImage img, Color bgcolor)

Here you can find the source of autoPanImage(BufferedImage img, Color bgcolor)

Description

auto Pan Image

License

Open Source License

Declaration

public static BufferedImage autoPanImage(BufferedImage img, Color bgcolor) 

Method Source Code


//package com.java2s;
/* VisNow/*from w  ww .jav a  2  s  .c  o  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.*;

import java.awt.image.*;

public class Main {
    public static BufferedImage autoPanImage(BufferedImage img, Color bgcolor) {
        BufferedImage out = null;
        if (img == null) {
            return null;
        }

        int bgcolorInt = bgcolor.getRGB();
        int w = img.getWidth();
        int h = img.getHeight();
        int up = 0;
        int down = h - 1;
        int left = 0;
        int right = w - 1;
        int tmp;

        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h / 2; y++) {
                tmp = img.getRGB(x, y);
                if (tmp == bgcolorInt) {
                    if (y > up) {
                        up = y;
                    }
                } else {
                    break;
                }
            }

            for (int y = h - 1; y > h / 2; y--) {
                tmp = img.getRGB(x, y);
                if (tmp == bgcolorInt) {
                    if (y < down) {
                        down = y;
                    }
                } else {
                    break;
                }
            }

        }

        for (int y = up; y < down; y++) {
            for (int x = 0; x < w / 2; x++) {
                tmp = img.getRGB(x, y);
                if (tmp == bgcolorInt) {
                    if (x > left) {
                        left = x;
                    }
                } else {
                    break;
                }
            }

            for (int x = w - 1; x > w / 2; x--) {
                tmp = img.getRGB(x, y);
                if (tmp == bgcolorInt) {
                    if (x < right) {
                        right = x;
                    }
                } else {
                    break;
                }
            }

        }

        System.out.println("up=" + up);
        System.out.println("down=" + down);
        System.out.println("left=" + left);
        System.out.println("right=" + right);

        out = img.getSubimage(left, up, right - left + 1, down - up + 1);
        return out;
    }
}

Related

  1. applyGrayscaleMaskToAlpha(BufferedImage image, BufferedImage mask)
  2. applyMask(BufferedImage img, Color keyColor)
  3. applyMaskImage(BufferedImage src, int x, int y, int w, int h)
  4. applyShadow(BufferedImage image)
  5. areBufferedImagesEqual(BufferedImage img1, BufferedImage img2)
  6. binarize(BufferedImage image)
  7. binarize(BufferedImage original)
  8. binary(BufferedImage src)
  9. blackAndWhiteCleaning(BufferedImage image)