Java BufferedImage to Shape imageToShapeOld(BufferedImage image)

Here you can find the source of imageToShapeOld(BufferedImage image)

Description

image To Shape Old

License

Open Source License

Declaration

public static Shape imageToShapeOld(BufferedImage image) 

Method Source Code

//package com.java2s;
/*//w w w. ja v  a2 s .co m
 *  Copyright (C) 2010-2016 JPEXS, All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 */

import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import java.util.BitSet;

import java.util.HashMap;

import java.util.Map;

public class Main {
    private static final Map<BitSet, Area> shapeCache = new HashMap<>();

    public static Shape imageToShapeOld(BufferedImage image) {
        Area area = new Area();
        Rectangle rectangle = new Rectangle();
        int y1, y2;
        int width = image.getWidth();
        int height = image.getHeight();

        int[] imgData;
        int type = image.getType();
        if (type == BufferedImage.TYPE_INT_ARGB_PRE
                || type == BufferedImage.TYPE_INT_RGB) {
            imgData = ((DataBufferInt) image.getRaster().getDataBuffer())
                    .getData();
        } else {
            imgData = image.getRGB(0, 0, width, height, null, 0, width);
        }

        BitSet bs = new BitSet(width * height);
        bs.set(type);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int idx = width * y + x;
                if ((imgData[idx] >>> 24) > 0) {
                    bs.set(idx);
                }
            }
        }

        if (shapeCache.containsKey(bs)) {
            return shapeCache.get(bs);
        }

        for (int x = 0; x < width; x++) {
            y1 = Integer.MAX_VALUE;
            y2 = -1;
            for (int y = 0; y < height; y++) {
                int rgb = imgData[width * y + x];
                rgb = rgb >>> 24;
                if (rgb > 0) {
                    if (y1 == Integer.MAX_VALUE) {
                        y1 = y;
                        y2 = y;
                    }
                    if (y > (y2 + 1)) {
                        rectangle.setBounds(x, y1, 1, y2 - y1 + 1);
                        area.add(new Area(rectangle));
                        y1 = y;
                    }
                    y2 = y;
                }
            }
            if ((y2 - y1) >= 0) {
                rectangle.setBounds(x, y1, 1, y2 - y1 + 1);
                area.add(new Area(rectangle));
            }
        }

        shapeCache.put(bs, area);
        return area;
    }
}

Related

  1. imageToShape(BufferedImage image)
  2. imageToShape(BufferedImage image)