Java BufferedImage Operation generateOutline(BufferedImage source, Color color, boolean alpha)

Here you can find the source of generateOutline(BufferedImage source, Color color, boolean alpha)

Description

generate Outline

License

Open Source License

Declaration

public static BufferedImage generateOutline(BufferedImage source,
            Color color, boolean alpha) 

Method Source Code

//package com.java2s;
/*/*from   w w  w . j a  v a  2  s. co  m*/
 * Collab canvas - Java framework for graphics software enabling offline and shared
 * painting
 * Copyright (C) 2012 Martin Indra <aktive@seznam.cz>
 *
 * This file is part of Collab canvas.
 *
 * Collab canvas 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.
 *
 * Collab canvas 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 Collab canvas.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Color;
import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage generateOutline(BufferedImage source,
            Color color, boolean alpha) {
        return generateOutline(source, color, color, alpha);
    }

    public static BufferedImage generateOutline(BufferedImage source,
            Color color1, Color color2, boolean alpha) {
        int w = source.getWidth();
        int h = source.getHeight();
        BufferedImage result = new BufferedImage(w, h,
                BufferedImage.TYPE_4BYTE_ABGR);

        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                if (!testNeighbour(source, x, y, alpha)) {
                    if (testNeighbours(source, x, y, alpha)) {
                        boolean a = ((x + y) % 10) <= 5;
                        result.setRGB(x, y,
                                a ? color1.getRGB() : color2.getRGB());
                    }
                }
            }
        }
        return result;
    }

    protected static boolean testNeighbour(BufferedImage source, int x,
            int y, boolean alpha) {
        if (x >= 0 && y >= 0 && x < source.getWidth()
                && y < source.getHeight()) {
            int gray = source.getRGB(x, y) & 0x000000ff;
            int alphaI = (source.getRGB(x, y) & 0xff000000) >>> 24;
            return (alpha && alphaI == 0) || (!alpha && gray == 255);
        }
        return true;
    }

    protected static boolean testNeighbours(BufferedImage source, int x,
            int y, boolean alpha) {
        boolean up = testNeighbour(source, x, y - 1, alpha);
        boolean down = testNeighbour(source, x, y + 1, alpha);
        boolean left = testNeighbour(source, x - 1, y, alpha);
        boolean right = testNeighbour(source, x + 1, y, alpha);
        return up || down || left || right;
    }
}

Related

  1. fuzzyEquals(BufferedImage a, BufferedImage b, int threshold)
  2. genBlackAndWhiteImage(BufferedImage image)
  3. generate(int w, int h, BufferedImage img)
  4. generateBufferedImageFromComponent(Component component)
  5. generateImageHash(BufferedImage image)
  6. gradientMask(BufferedImage img, float start, float stop)
  7. greyScale(BufferedImage image)
  8. grid(BufferedImage image, int between)
  9. height(BufferedImage image, Double measurementLatitude, Double measurementLongitude)