Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package img2step;
- import java.awt.Color;
- import java.awt.Point;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.PrintStream;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import javax.imageio.ImageIO;
- public class Main
- {
- public static class NullOutputStream extends OutputStream
- {
- public StringBuilder o;
- @Override
- public void write(int b) throws IOException
- {
- if(o == null) o = new StringBuilder();
- o.append((char)b);
- }
- }
- public static PrintStream out;
- public static void main(String[] args) throws IOException
- {
- int ystart = 25; //120;
- NullOutputStream nos = new NullOutputStream();
- out = new PrintStream(nos);//System.out;
- BufferedImage bi = ImageIO.read(new File("F:/img2step/steps3.png"));
- List<Color> colors;
- colors = getColors(bi);
- //colors.forEach(System.out::println);
- int count = 1;
- for(Color c : colors)
- {
- if(c.equals(Color.white)) continue;
- //System.out.println("get color : " + c);
- //getColor(c);
- //out.print("r");
- List<Point> ps = getPositions(bi, c);
- stripExtra(ps);
- List<List<Point>> lines = toLines(ps);
- for(List<Point> line : lines)
- {
- boolean lower = false;
- for(Point p : line)
- {
- lower = !lower;
- out.print("rx" + p.x*5 + "y" + ((bi.getHeight()-p.y)*5+ystart) + "!");
- getColor(c);
- // if(lines.indexOf(line) != lines.size()-1 && !lower)
- // getColor(c);
- if(count%3 ==0)
- out.println();
- count++;
- // if(!lower)
- // out.print("p");
- }
- //out.println();
- }
- //ps.forEach(p -> System.out.println(colors.indexOf(c) + " | " + p));
- }
- String s = nos.o.toString();
- System.out.println(s);
- //Arrays.stream(s.split("(?<=\\G(x\\d+y\\d+){3})")).forEach(System.out::println);
- }
- public static void getColor(Color c)
- {
- //System.out.println(c);
- if(c.equals(new Color(0,0,0)))
- out.print("clw");
- if(c.equals(new Color(25,37,58)))
- out.print("nlwclw");
- if(c.equals(new Color(44,78,126)))
- out.print("nlw");
- if(c.equals(new Color(136,56,28)))
- out.print("vlw");
- if(c.equals(new Color(204,127,43)))
- out.print("blwvlw");
- if(c.equals(new Color(105,138,156)))
- out.print("blwnlw");
- if(c.equals(new Color(204,197,174)))
- out.print("blw");
- // if(c.equals(new Color(255,255,255)))
- // out.print("vlw");
- }
- public static List<Color> getColors(BufferedImage in)
- {
- List<Color> colors = new ArrayList<>();
- for(int x = 0; x < in.getWidth(); x++)
- {
- for(int y = 0; y < in.getHeight(); y++)
- {
- Color t = new Color(in.getRGB(x, y));
- if(!colors.contains(t))
- {
- colors.add(t);
- }
- }
- }
- return colors;
- }
- public static List<Point> getPositions(BufferedImage bi, Color c)
- {
- List<Point> points = new ArrayList<Point>();
- for(int y = 0; y < bi.getHeight(); y++)
- {
- for(int x = 0; x < bi.getWidth(); x++)
- {
- if(c.equals(new Color(bi.getRGB(x, y))))
- {
- points.add(new Point(x, y));
- }
- }
- }
- return points;
- }
- public static void stripExtra(List<Point> points)
- {
- Iterator<Point> i = points.iterator();
- List<Point> toRemove = new ArrayList<>();
- List<Point> toAdd = new ArrayList<>();
- while(i.hasNext())
- {
- Point p = i.next();
- if(points.contains(new Point(p.x-1, p.y)) && points.contains(new Point(p.x+1, p.y)))
- {
- //System.out.println("remove " + p);
- //toRemove.add(p);
- }
- if(!points.contains(new Point(p.x-1, p.y)) && !points.contains(new Point(p.x+1, p.y)))
- {
- //System.out.println("add " + p);
- //toAdd.add(p);
- }
- }
- points.removeAll(toRemove);
- points.addAll(toAdd);
- points.sort((o1, o2) ->
- {
- if(o1.y == o2.y)
- {
- return o1.x - o2.x;
- }
- return o1.y - o2.y;
- });
- //System.out.println(points);
- }
- public static List<List<Point>> toLines(List<Point> ps)
- {
- List<List<Point>> out = new ArrayList<>();
- out.add(new ArrayList<Point>());
- out.get(0).add(ps.get(0));
- for(int i = 1; i < ps.size(); i++)
- {
- if(out.get(out.size()-1).get(out.get(out.size()-1).size()-1).y != ps.get(i).y)
- {
- // System.out.println(out.get(out.size()-1).get(out.get(out.size()-1).size()-1).y);
- // System.out.println(ps.get(i).y);
- out.add(new ArrayList<Point>());
- }
- out.get(out.size()-1).add(ps.get(i));
- }
- return out;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement