@@ -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