Skip to content

Commit bc11bdd

Browse files
authored
Merge pull request #31 from EmbroidePy/pec-correction
PEC Corrections for Initial Command
2 parents 80b7a6c + 12fe3a6 commit bc11bdd

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ public void readPec() throws IOException {
3434
skip(0x1D0 - colorChanges);
3535
int stitch_block_end = readInt24LE() - 5 + tell();
3636
//The end of this value is 5 into the sttichblock.
37-
// 3 bytes, '0x31, 0xff, 0xf0, 6 2-byte shorts. 15 total.
38-
skip(0x0F);
37+
38+
// 3 bytes, '0x31, 0xff, 0xf0, 4 2-byte shorts. 11 total.
39+
skip(0x0B);
3940
readPecStitches();
4041
seek(stitch_block_end);
4142
int byte_size = pec_graphic_byte_stride * pec_graphic_icon_height;
@@ -109,7 +110,6 @@ public void process_pec_table(byte[] color_bytes) {
109110

110111
public void map_pec_colors(byte[] color_bytes) {
111112
int color_count = color_bytes.length;
112-
int color_index = 0;
113113
if (pattern.threadlist.isEmpty()) {
114114
//if the threadlist is empty, we are reading a file without header threads.
115115
process_pec_colors(color_bytes);

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

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class PecWriter extends EmbWriter {
1717

1818
static final int PEC_ICON_WIDTH = 48;
1919
static final int PEC_ICON_HEIGHT = 38;
20+
21+
static final boolean GROUP_LONG = false;
2022

2123
public PecWriter() {
2224
super();
@@ -132,9 +134,6 @@ void write_pec_block() throws IOException {
132134
writeInt16LE((short) 0x1E0);
133135
writeInt16LE((short) 0x1B0);
134136

135-
writeInt16BE((0x9000 | -Math.round(pattern.getMinX())));
136-
writeInt16BE((0x9000 | -Math.round(pattern.getMinY())));
137-
138137
pec_encode();
139138

140139
int stitch_block_length = tell() - stitch_block_start_position;
@@ -173,19 +172,28 @@ public static int encode_long_form(int value) {
173172
return value;
174173
}
175174

176-
public static int flagJump(int longForm) {
177-
return longForm | (JUMP_CODE << 8);
175+
private void write_value(int value) throws IOException {
176+
write_value(value, false, 0);
178177
}
179-
180-
public static int flagTrim(int longForm) {
181-
return longForm | (TRIM_CODE << 8);
178+
179+
private void write_value(int value, boolean force_long, int flag) throws IOException {
180+
if (!force_long && -64 < value && value < 63) {
181+
writeInt8(value & MASK_07_BIT);
182+
}
183+
else {
184+
value &= 0b00001111_11111111;
185+
value |= 0b10000000_00000000;
186+
value |= (flag << 8);
187+
writeInt16BE(value);
188+
}
182189
}
183-
190+
184191
private void pec_encode() throws IOException {
185192
boolean color_two = true;
186193
Points stitches = pattern.getStitches();
187194
int dx, dy;
188195
boolean jumping = false;
196+
boolean init = true;
189197
double xx = 0, yy = 0;
190198
for (int i = 0, ie = stitches.size(); i < ie; i++) {
191199
int data = stitches.getData(i) & COMMAND_MASK;
@@ -199,40 +207,44 @@ private void pec_encode() throws IOException {
199207
case STITCH:
200208
if (jumping) {
201209
if ((dx != 0) && (dy != 0)) {
202-
writeInt8((byte) 0x00);
203-
writeInt8((byte) 0x00);
210+
write_value(0);
211+
write_value(0);
204212
}
205213
jumping = false;
206214
}
207-
if (dx < 63 && dx > -64 && dy < 63 && dy > -64) {
208-
writeInt8(dx & MASK_07_BIT);
209-
writeInt8(dy & MASK_07_BIT);
210-
} else {
211-
dx = encode_long_form(dx);
212-
dy = encode_long_form(dy);
213-
writeInt16BE(dx);
214-
writeInt16BE(dy);
215+
if (GROUP_LONG && dx < 63 && dx > -64 && dy < 63 && dy > -64) {
216+
write_value(dx, true, 0);
217+
write_value(dy, true, 0);
218+
}
219+
else {
220+
write_value(dx);
221+
write_value(dy);
215222
}
223+
init = false;
216224
continue;
217225
case JUMP:
218226
jumping = true;
219-
dx = encode_long_form(dx);
220-
dx = flagTrim(dx);
221-
dy = encode_long_form(dy);
222-
dy = flagTrim(dy);
223-
writeInt16BE(dx);
224-
writeInt16BE(dy);
227+
if (init) {
228+
write_value(dx, true, JUMP_CODE);
229+
write_value(dy, true, JUMP_CODE);
230+
}
231+
else {
232+
write_value(dx, true, TRIM_CODE);
233+
write_value(dy, true, TRIM_CODE);
234+
}
235+
init = false;
225236
continue;
226237
case COLOR_CHANGE:
227238
if (jumping) {
228-
writeInt8((byte) 0x00);
229-
writeInt8((byte) 0x00);
239+
write_value(0);
240+
write_value(0);
230241
jumping = false;
231242
}
232243
writeInt8(0xfe);
233244
writeInt8(0xb0);
234245
writeInt8((color_two) ? 2 : 1);
235246
color_two = !color_two;
247+
init = false;
236248
continue;
237249
case TRIM:
238250
continue;

0 commit comments

Comments
 (0)