Skip to content

Commit 78d84cb

Browse files
authored
Merge pull request #23 from EmbroidePy/0.1.16
Jef Stop Extended
2 parents a94acdc + ca681f4 commit 78d84cb

File tree

2 files changed

+72
-12
lines changed

2 files changed

+72
-12
lines changed

core/src/main/java/org/embroideryio/embroideryio/JefReader.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public class JefReader extends EmbReader {
77

88
public void read_jef_stitches() throws IOException {
9+
int color_index = 1;
910
byte[] b = new byte[2];
1011
while (true) {
1112
if (readFully(b) != b.length) break;
@@ -29,7 +30,15 @@ public void read_jef_stitches() throws IOException {
2930
}
3031
continue;
3132
case 0x01:
32-
pattern.color_change();
33+
34+
if (pattern.threadlist.get(color_index) == null) {
35+
pattern.stop();
36+
pattern.threadlist.remove(color_index);
37+
}
38+
else {
39+
pattern.color_change();
40+
color_index += 1;
41+
}
3342
continue;
3443
case 0x10:
3544
break;
@@ -48,7 +57,13 @@ public void read() throws IOException {
4857
skip(88);
4958
for (int i = 0; i < count_colors; i++) {
5059
int index = Math.abs(readInt32LE());
51-
pattern.add(jefThread[index % jefThread.length]);
60+
if (index == 0) {
61+
// If we have color 0. Go ahead and set that to None.
62+
pattern.threadlist.add(null);
63+
}
64+
else {
65+
pattern.add(jefThread[index % jefThread.length]);
66+
}
5267
}
5368
seek(stitch_offset);
5469
read_jef_stitches();

core/src/main/java/org/embroideryio/embroideryio/JefWriter.java

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import java.io.IOException;
66
import java.text.SimpleDateFormat;
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.Collection;
710
import java.util.Date;
811

912
public 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

Comments
 (0)