Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. package normalfill;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import javax.imageio.ImageIO;
  6.  
  7. /**
  8. *
  9. * @author maik
  10. */
  11. public class Normalfill {
  12.  
  13. public static void main(String[] args) throws Exception {
  14.  
  15. File f = new File("/tmp/test.png");
  16.  
  17. BufferedImage bi = ImageIO.read(f);
  18.  
  19. boolean finished;
  20.  
  21. do {
  22. finished = true;
  23. for (int x = 0; x < bi.getWidth(); ++x) {
  24. for (int y = 0; y < bi.getHeight(); ++y) {
  25. int pixel = bi.getRGB(x, y) & 0xFFFFFF;
  26. if (pixel == 0) {
  27. int fillcolor = getNextNeighbour(bi, x, y);
  28. if (fillcolor != 0) {
  29. bi.setRGB(x, y, fillcolor);
  30. finished = false;
  31. }
  32. }
  33. }
  34. }
  35. } while (!finished);
  36.  
  37.  
  38. ImageIO.write(bi, "png", new File("/tmp/test2.png"));
  39. }
  40.  
  41. private static int getNextNeighbour(BufferedImage bi, int x, int y) {
  42.  
  43. for (int xoff = -1; xoff < 2; xoff++) {
  44. int xpos = x + xoff;
  45. if (xpos >= 0 && xpos < bi.getWidth()) {
  46. for (int yoff = -1; yoff < 2; yoff++) {
  47. if (Math.abs(xoff) + Math.abs(yoff) == 1) {
  48. int ypos = y + yoff;
  49. if (ypos >= 0 && ypos < bi.getHeight()) {
  50. int pixel = bi.getRGB(xpos, ypos);
  51. if ((pixel & 0xFFFFFF) != 0) {
  52. return pixel;
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59.  
  60. return 0;
  61.  
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement