Java BufferedImage Operation smaller(final BufferedImage src, final int radius, final int opaqueLimit)

Here you can find the source of smaller(final BufferedImage src, final int radius, final int opaqueLimit)

Description

smaller

License

Open Source License

Declaration

private static BufferedImage smaller(final BufferedImage src, final int radius, final int opaqueLimit) 

Method Source Code

//package com.java2s;
/*//from w  w w .ja  va 2 s .  c  o m
 *   Copyright (C) 2012 Markus Niedermann
 *   
 *   This program 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
 *   any later version.
 *   
 *   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.image.BufferedImage;

public class Main {
    private static BufferedImage smaller(final BufferedImage src, final int radius, final int opaqueLimit) {

        BufferedImage ret = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB);
        for (int y = 0; y < src.getHeight(); y++) {
            for (int x = 0; x < src.getWidth(); x++) {
                if (((src.getRGB(x, y) >> 24) & 0xff) > opaqueLimit
                        && checkRadius(src, x, y, opaqueLimit, radius)) {
                    ret.setRGB(x, y, src.getRGB(x, y));
                }

            }
        }
        return ret;
    }

    private static boolean checkRadius(final BufferedImage src, final int x, final int y, final int opaqueLimit,
            final int radius) {
        int minX = Math.max(x - radius, 0);
        int maxX = Math.min(src.getWidth(), x + radius);

        int minY = Math.max(y - radius, 0);
        int maxY = Math.min(src.getHeight(), y + radius);

        for (int curY = minY; curY < maxY; curY++) {
            for (int curX = minX; curX < maxX; curX++) {
                if (getDistance(curX, curY, x, y, radius) <= 1
                        && ((src.getRGB(curX, curY) >> 24) & 0xff) <= opaqueLimit) {
                    return false;
                }
            }
        }
        return true;
    }

    private static double getDistance(final int curX, final int curY, final int x, final int y, final int radius) {
        return Math.sqrt((curX - x) * (curX - x) + (curY - y) * (curY - y)) / radius;
    }
}

Related

  1. setLuminance(BufferedImage image, int x, int y, float value)
  2. setPixel(BufferedImage img, int x, int y, int c)
  3. setPoint(BufferedImage image, int x, int y, int factor, Color color)
  4. showColors(BufferedImage image, boolean alpha, boolean red, boolean green, boolean blue)
  5. shrink(BufferedImage source, double factor)
  6. stitchImages(BufferedImage[] images, int[] relX)
  7. switchAxes(BufferedImage img)
  8. symmetrifyY(BufferedImage image, boolean useFirstHalfImage, boolean flipVertical)
  9. testNeighbours(BufferedImage source, int x, int y, boolean alpha)