Advertisement
Guest User

img2step

a guest
Apr 16th, 2017
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None | 0 0
  1. package img2step;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Point;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.io.OutputStream;
  9. import java.io.PrintStream;
  10. import java.io.PrintWriter;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.HashMap;
  14. import java.util.Iterator;
  15. import java.util.List;
  16. import java.util.Map;
  17.  
  18. import javax.imageio.ImageIO;
  19.  
  20. public class Main
  21. {
  22.     public static class NullOutputStream extends OutputStream
  23.     {
  24.         public StringBuilder o;
  25.        
  26.         @Override
  27.         public void write(int b) throws IOException
  28.         {
  29.             if(o == null) o = new StringBuilder();
  30.             o.append((char)b);
  31.         }
  32.     }
  33.    
  34.     public static PrintStream out;
  35.    
  36.     public static void main(String[] args) throws IOException
  37.     {
  38.         int ystart = 25; //120;
  39.         NullOutputStream nos = new NullOutputStream();
  40.         out = new PrintStream(nos);//System.out;
  41.         BufferedImage bi = ImageIO.read(new File("F:/img2step/steps3.png"));
  42.         List<Color> colors;
  43.         colors = getColors(bi);
  44.         //colors.forEach(System.out::println);
  45.         int count = 1;
  46.         for(Color c : colors)
  47.         {
  48.             if(c.equals(Color.white)) continue;
  49.             //System.out.println("get color : " + c);
  50.             //getColor(c);
  51.             //out.print("r");
  52.             List<Point> ps = getPositions(bi, c);
  53.             stripExtra(ps);
  54.             List<List<Point>> lines = toLines(ps);
  55.             for(List<Point> line : lines)
  56.             {
  57.                 boolean lower = false;
  58.                
  59.                 for(Point p : line)
  60.                 {
  61.                    
  62.                     lower = !lower;
  63.                     out.print("rx" + p.x*5 + "y" + ((bi.getHeight()-p.y)*5+ystart) + "!");
  64.                     getColor(c);
  65. //                  if(lines.indexOf(line) != lines.size()-1 && !lower)
  66. //                      getColor(c);
  67.                     if(count%3 ==0)
  68.                         out.println();
  69.                     count++;
  70. //                  if(!lower)
  71. //                      out.print("p");
  72.                 }
  73.                 //out.println();
  74.             }
  75.             //ps.forEach(p -> System.out.println(colors.indexOf(c) + " | " + p));
  76.            
  77.         }
  78.         String s = nos.o.toString();
  79.         System.out.println(s);
  80.         //Arrays.stream(s.split("(?<=\\G(x\\d+y\\d+){3})")).forEach(System.out::println);
  81.     }
  82.    
  83.     public static void getColor(Color c)
  84.     {
  85.         //System.out.println(c);
  86.         if(c.equals(new Color(0,0,0)))
  87.             out.print("clw");
  88.         if(c.equals(new Color(25,37,58)))
  89.             out.print("nlwclw");
  90.         if(c.equals(new Color(44,78,126)))
  91.             out.print("nlw");
  92.         if(c.equals(new Color(136,56,28)))
  93.             out.print("vlw");
  94.         if(c.equals(new Color(204,127,43)))
  95.             out.print("blwvlw");
  96.         if(c.equals(new Color(105,138,156)))
  97.             out.print("blwnlw");
  98.         if(c.equals(new Color(204,197,174)))
  99.             out.print("blw");
  100. //      if(c.equals(new Color(255,255,255)))
  101. //          out.print("vlw");
  102.     }
  103.    
  104.    
  105.     public static List<Color> getColors(BufferedImage in)
  106.     {
  107.         List<Color> colors = new ArrayList<>();
  108.         for(int x = 0; x < in.getWidth(); x++)
  109.         {
  110.             for(int y = 0; y < in.getHeight(); y++)
  111.             {
  112.                 Color t = new Color(in.getRGB(x, y));
  113.                 if(!colors.contains(t))
  114.                 {
  115.                     colors.add(t);
  116.                 }
  117.             }
  118.         }
  119.         return colors;
  120.     }
  121.    
  122.     public static List<Point> getPositions(BufferedImage bi, Color c)
  123.     {
  124.         List<Point> points = new ArrayList<Point>();
  125.         for(int y = 0; y < bi.getHeight(); y++)
  126.         {
  127.             for(int x = 0; x < bi.getWidth(); x++)
  128.             {
  129.                 if(c.equals(new Color(bi.getRGB(x, y))))
  130.                 {
  131.                     points.add(new Point(x, y));
  132.                 }
  133.             }
  134.         }
  135.         return points;
  136.     }
  137.    
  138.     public static void stripExtra(List<Point> points)
  139.     {
  140.         Iterator<Point> i = points.iterator();
  141.         List<Point> toRemove = new ArrayList<>();
  142.         List<Point> toAdd = new ArrayList<>();
  143.         while(i.hasNext())
  144.         {
  145.             Point p = i.next();
  146.             if(points.contains(new Point(p.x-1, p.y)) && points.contains(new Point(p.x+1, p.y)))
  147.             {
  148.                 //System.out.println("remove " + p);
  149.                 //toRemove.add(p);
  150.             }
  151.             if(!points.contains(new Point(p.x-1, p.y)) && !points.contains(new Point(p.x+1, p.y)))
  152.             {
  153.                 //System.out.println("add " + p);
  154.                 //toAdd.add(p);
  155.             }
  156.         }
  157.         points.removeAll(toRemove);
  158.         points.addAll(toAdd);
  159.         points.sort((o1, o2) ->
  160.         {
  161.             if(o1.y == o2.y)
  162.             {
  163.                 return o1.x - o2.x;
  164.             }
  165.             return o1.y - o2.y;
  166.         });
  167.         //System.out.println(points);
  168.     }
  169.    
  170.     public static List<List<Point>> toLines(List<Point> ps)
  171.     {
  172.         List<List<Point>> out = new ArrayList<>();
  173.         out.add(new ArrayList<Point>());
  174.         out.get(0).add(ps.get(0));
  175.         for(int i = 1; i < ps.size(); i++)
  176.         {
  177.             if(out.get(out.size()-1).get(out.get(out.size()-1).size()-1).y != ps.get(i).y)
  178.             {
  179. //              System.out.println(out.get(out.size()-1).get(out.get(out.size()-1).size()-1).y);
  180. //              System.out.println(ps.get(i).y);
  181.                 out.add(new ArrayList<Point>());
  182.             }
  183.             out.get(out.size()-1).add(ps.get(i));
  184.         }
  185.         return out;
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement