pull/116/head
Tatarize 2021-03-04 18:06:08 -08:00
rodzic b887fd4610
commit 6208a25eb3
5 zmienionych plików z 155 dodań i 6 usunięć

Wyświetl plik

@ -3,6 +3,7 @@ from .ReadHelper import read_int_32le, signed8
def read_jef_stitches(f, out, settings=None):
color_index = 1
while True:
b = bytearray(f.read(2))
if len(b) != 2:
@ -22,7 +23,12 @@ def read_jef_stitches(f, out, settings=None):
out.move(x, y)
continue
if ctrl == 0x01:
out.color_change(0, 0)
if out.threadlist[color_index] is None:
out.stop(0, 0)
del out.threadlist[color_index]
else:
out.color_change(0, 0)
color_index += 1
continue
if ctrl == 0x10:
break
@ -54,7 +60,10 @@ def read(f, out, settings=None):
for i in range(0, count_colors):
index = abs(read_int_32le(f))
if index == 0:
out.add_thread(None)
out.add_thread(jef_threads[index % len(jef_threads)])
print(out.threadlist)
f.seek(stitch_offset, 0)
read_jef_stitches(f, out, settings)

Wyświetl plik

@ -30,7 +30,46 @@ def write(pattern, f, settings=None):
date_string = settings.get("date", date_string)
pattern.fix_color_count()
color_count = pattern.count_color_changes() + 1
# REMOVE BUG: color_count = pattern.count_threads(). #
# PATCH
jef_threads = get_thread_set()
last_index = None
last_thread = None
palette = []
color_toggled = False
color_count = 0 # Color and Stop count.
index_in_threadlist = 0
for stitch in pattern.stitches:
# Iterate all stitches.
flags = stitch[2] & COMMAND_MASK
if flags == COLOR_CHANGE or index_in_threadlist == 0:
# If color change *or* initial color unset.
thread = pattern.threadlist[index_in_threadlist]
index_in_threadlist += 1
color_count += 1
index_of_jefthread = thread.find_nearest_color_index(jef_threads)
if last_index == index_of_jefthread and last_thread != thread:
# Last thread and current thread pigeonhole to same jefcolor.
# We set that thread to None. And get the second closest color.
repeated_thread = jef_threads[index_of_jefthread]
repeated_index = index_of_jefthread
jef_threads[index_of_jefthread] = None
index_of_jefthread = thread.find_nearest_color_index(jef_threads)
jef_threads[repeated_index] = repeated_thread
palette.append(index_of_jefthread)
last_index = index_of_jefthread
last_thread = thread
color_toggled = False
if flags == STOP:
color_count += 1
color_toggled = not color_toggled
if color_toggled:
palette.append(0)
else:
palette.append(last_index)
# END PATCH
offsets = 0x74 + (color_count * 8)
write_int_32le(f, offsets)
write_int_32le(f, 0x14)
@ -48,7 +87,7 @@ def write(pattern, f, settings=None):
elif data == TRIM:
if trims:
point_count += 2 * command_count_max
elif data == COLOR_CHANGE:
elif data == COLOR_CHANGE or data == STOP: # PATCH: INCLUDE STOP.
point_count += 2
elif data == END:
break
@ -86,9 +125,12 @@ def write(pattern, f, settings=None):
y_hoop_edge = 1000 - half_height
write_hoop_edge_distance(f, x_hoop_edge, y_hoop_edge)
jef_threads = get_thread_set()
# REMOVE (We covered this in PATCH).
#jef_threads = get_thread_set()
#
#palette = build_nonrepeat_palette(jef_threads, pattern.threadlist)
# END REMOVE
palette = build_nonrepeat_palette(jef_threads, pattern.threadlist)
for t in palette:
write_int_32le(f, t)
@ -109,7 +151,7 @@ def write(pattern, f, settings=None):
write_int_8(f, dx)
write_int_8(f, -dy)
continue
elif data == COLOR_CHANGE:
elif data == COLOR_CHANGE or data == STOP: # PATCH INCLUDE STOP.
f.write(b"\x80\x01")
write_int_8(f, dx)
write_int_8(f, -dy)

Wyświetl plik

Wyświetl plik

@ -59,6 +59,7 @@ class Turtle:
}
evaluate_lsystem(initial, rules, 3) # 6
def get_big_pattern():
pattern = EmbPattern()
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "red")
@ -131,6 +132,78 @@ def get_shift_pattern():
return pattern
def get_shift_stop_pattern():
pattern = EmbPattern()
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "red")
pattern.add_command(MATRIX_TRANSLATE, 25, 25)
pattern.add_command(MATRIX_ROTATE, 22.5)
pattern.add_command(STOP)
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "red")
pattern.add_command(MATRIX_TRANSLATE, 25, 25)
pattern.add_command(MATRIX_ROTATE, 22.5)
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "green")
pattern.add_command(MATRIX_TRANSLATE, 25, 25)
pattern.add_command(MATRIX_ROTATE, 22.5)
pattern.add_command(STOP)
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "green")
pattern.add_command(MATRIX_TRANSLATE, 25, 25)
pattern.add_command(MATRIX_ROTATE, 22.5)
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "green")
pattern.add_command(MATRIX_TRANSLATE, 25, 25)
pattern.add_command(MATRIX_ROTATE, 22.5)
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "ivory")
pattern.add_command(MATRIX_TRANSLATE, 25, 25)
pattern.add_command(MATRIX_ROTATE, 22.5)
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "ivory")
pattern.add_command(MATRIX_TRANSLATE, 25, 25)
pattern.add_command(MATRIX_ROTATE, 22.5)
pattern.add_command(STOP)
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "ivory")
pattern.add_command(MATRIX_TRANSLATE, 25, 25)
pattern.add_command(MATRIX_ROTATE, 22.5)
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "olive")
pattern.add_command(MATRIX_TRANSLATE, 25, 25)
pattern.add_command(MATRIX_ROTATE, 22.5)
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "olive")
pattern.add_command(MATRIX_TRANSLATE, 25, 25)
pattern.add_command(MATRIX_ROTATE, 22.5)
pattern.add_command(STOP)
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "olive")
pattern.add_command(MATRIX_TRANSLATE, 25, 25)
pattern.add_command(MATRIX_ROTATE, 22.5)
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "olive")
pattern.add_command(MATRIX_TRANSLATE, 25, 25)
pattern.add_command(MATRIX_ROTATE, 22.5)
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "violet")
pattern.add_command(MATRIX_TRANSLATE, 25, 25)
pattern.add_command(MATRIX_ROTATE, 22.5)
pattern.add_command(STOP)
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "white")
pattern.add_command(MATRIX_TRANSLATE, 25, 25)
pattern.add_command(MATRIX_ROTATE, 22.5)
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "salmon")
pattern.add_command(MATRIX_TRANSLATE, 25, 25)
pattern.add_command(MATRIX_ROTATE, 22.5)
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "lime")
return pattern
def get_simple_stop():
pattern = EmbPattern()
pattern += (0,0)
pattern += (0, 100)
pattern += (100, 100)
pattern += (100, 0)
pattern += (0, 0)
pattern.stop()
pattern += (0, 0)
pattern += (0, 100)
pattern += (100, 100)
pattern += (100, 0)
pattern += (0, 0)
return pattern
def get_simple_pattern():
pattern = EmbPattern()
pattern.add_block([(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)], "red")

Wyświetl plik

@ -168,4 +168,29 @@ class TestConverts(unittest.TestCase):
self.position_equals(t_pattern.stitches, 0, -1)
print("jef->xxx: ", t_pattern.stitches)
self.addCleanup(os.remove, file1)
self.addCleanup(os.remove, file2)
def test_jef_stop_write(self):
file2 = "stop2.jef"
write_jef(get_simple_stop(), file2)
s_pattern = read_jef(file2)
self.assertEqual(s_pattern.count_stitch_commands(STOP), 1)
self.assertEqual(s_pattern.count_color_changes(), 0)
file1 = "stop.jef"
pattern = get_shift_stop_pattern()
write_jef(pattern, file1)
f_pattern = read_jef(file1)
self.assertIsNotNone(f_pattern)
with open(file1, "rb") as f:
f.seek(0x18)
colors = f.read(1)
self.assertEqual(ord(colors), f_pattern.count_color_changes() + f_pattern.count_stitch_commands(STOP) + 1)
self.assertEqual(f_pattern.count_stitch_commands(COLOR_CHANGE), 12)
self.assertEqual(f_pattern.count_stitch_commands(STITCH), 16 * 5)
self.assertEqual(f_pattern.count_stitch_commands(STOP), 6)
self.position_equals(f_pattern.stitches, 0, -1)
self.addCleanup(os.remove, file1)
self.addCleanup(os.remove, file2)