Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package normalfill;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import javax.imageio.ImageIO;
- /**
- *
- * @author maik
- */
- public class Normalfill {
- public static void main(String[] args) throws Exception {
- File f = new File("/tmp/test.png");
- BufferedImage bi = ImageIO.read(f);
- boolean finished;
- do {
- finished = true;
- for (int x = 0; x < bi.getWidth(); ++x) {
- for (int y = 0; y < bi.getHeight(); ++y) {
- int pixel = bi.getRGB(x, y) & 0xFFFFFF;
- if (pixel == 0) {
- int fillcolor = getNextNeighbour(bi, x, y);
- if (fillcolor != 0) {
- bi.setRGB(x, y, fillcolor);
- finished = false;
- }
- }
- }
- }
- } while (!finished);
- ImageIO.write(bi, "png", new File("/tmp/test2.png"));
- }
- private static int getNextNeighbour(BufferedImage bi, int x, int y) {
- for (int xoff = -1; xoff < 2; xoff++) {
- int xpos = x + xoff;
- if (xpos >= 0 && xpos < bi.getWidth()) {
- for (int yoff = -1; yoff < 2; yoff++) {
- if (Math.abs(xoff) + Math.abs(yoff) == 1) {
- int ypos = y + yoff;
- if (ypos >= 0 && ypos < bi.getHeight()) {
- int pixel = bi.getRGB(xpos, ypos);
- if ((pixel & 0xFFFFFF) != 0) {
- return pixel;
- }
- }
- }
- }
- }
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement