44
55import java .io .IOException ;
66import java .text .SimpleDateFormat ;
7+ import java .util .ArrayList ;
8+ import java .util .Arrays ;
9+ import java .util .Collection ;
710import java .util .Date ;
811
912public class JefWriter extends EmbWriter {
@@ -25,24 +28,67 @@ public JefWriter() {
2528 @ Override
2629 public void write () throws IOException {
2730 boolean trims = getBoolean ("trims" , true );
28-
31+
2932 Date date = new Date ();
3033 String date_string = new SimpleDateFormat ("yyyyMMddHHmmss" ).format (date );
3134 date_string = (String ) get ("date" , date_string );
32-
35+
3336 Points stitches = pattern .getStitches ();
3437
3538 pattern .fixColorCount ();
36- int color_count = pattern .getThreadlist ().size ();
37-
39+
40+ EmbThreadJef [] jef_threads = EmbThreadJef .getThreadSet ();
41+ int last_index = -1 ;
42+ EmbThread last_thread = null ;
43+ ArrayList <Integer > palette = new ArrayList <>();
44+
45+ boolean color_toggled = false ;
46+ int color_count = 0 ; // Color and Stop count.
47+ int index_in_threadlist = 0 ;
48+ stitches = pattern .getStitches ();
49+ for (int i = 0 , s = stitches .size (); i < s ; i ++) {
50+ //Iterate all stitches.
51+ int flags = stitches .getData (i ) & COMMAND_MASK ;
52+ if ((flags == COLOR_CHANGE ) || (index_in_threadlist == 0 )) {
53+ // If color change *or* initial color unset.
54+ EmbThread thread = pattern .threadlist .get (index_in_threadlist );
55+ index_in_threadlist += 1 ;
56+ color_count += 1 ;
57+ int index_of_jefthread = EmbThread .findNearestColorIndex (thread .getColor (), Arrays .asList (jef_threads ));
58+ if ((last_index == index_of_jefthread ) && (last_thread != thread )) {
59+ //Last thread and current thread pigeonhole to same jefcolor.
60+ //We set that thread to None. And get the second closest color.
61+ EmbThreadJef repeated_thread = jef_threads [index_of_jefthread ];
62+ int repeated_index = index_of_jefthread ;
63+ jef_threads [index_of_jefthread ] = null ;
64+ index_of_jefthread = EmbThread .findNearestColorIndex (thread .getColor (), Arrays .asList (jef_threads ));
65+ jef_threads [repeated_index ] = repeated_thread ;
66+ }
67+ palette .add (index_of_jefthread );
68+ last_index = index_of_jefthread ;
69+ last_thread = thread ;
70+ color_toggled = false ;
71+ }
72+ if (flags == STOP ) {
73+ color_count += 1 ;
74+ color_toggled = !color_toggled ;
75+ if (color_toggled ) {
76+ palette .add (0 );
77+ }
78+ else {
79+ palette .add (last_index );
80+ }
81+ }
82+ }
83+
3884 int offsets = 0x74 + (color_count * 8 );
3985 writeInt32LE (offsets );
4086 writeInt32LE (0x14 );
4187 write (date_string .getBytes ());
4288 writeInt8 (0x00 );
4389 writeInt8 (0x00 );
4490 writeInt32LE (color_count );
45-
91+
4692 int command_count = 1 ; // 1 command for END;
4793 for (int i = 0 , ie = stitches .size (); i < ie ; i ++) {
4894 int data = stitches .getData (i ) & COMMAND_MASK ;
@@ -68,7 +114,7 @@ public void write() throws IOException {
68114 break ;
69115 }
70116 writeInt32LE (command_count );
71-
117+
72118 float [] bounds = pattern .getBounds ();
73119 int design_width = (int ) (bounds [2 ] - bounds [0 ]);
74120 int design_height = (int ) (bounds [3 ] - bounds [1 ]);
@@ -103,11 +149,10 @@ public void write() throws IOException {
103149 y_hoop_edge = 1000 - half_height ;
104150 write_hoop_edge_distance (x_hoop_edge , y_hoop_edge );
105151
106- EmbThread [] threadSet = EmbThreadJef .getThreadSet ();
107- for (EmbThread thread : pattern .threadlist ) {
108- int thread_index = EmbThread .findNearestIndex (thread .color , threadSet );
109- writeInt32LE (thread_index );
152+ for (Integer t : palette ) {
153+ writeInt32LE (t );
110154 }
155+
111156 for (int i = 0 ; i < color_count ; i ++) {
112157 writeInt32LE (0x0D );
113158 }
0 commit comments