From d3f1d2bdf2516176922f615a0439a313f82f7329 Mon Sep 17 00:00:00 2001 From: tatarize Date: Sun, 25 Jul 2021 07:36:00 -0700 Subject: [PATCH 1/6] Zhs Corrections, Implementation --- pyembroidery/EmbPattern.py | 28 +++++++++++++++++--------- pyembroidery/ZhsReader.py | 41 +++++++++++++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/pyembroidery/EmbPattern.py b/pyembroidery/EmbPattern.py index 9f0d079..f27bd01 100644 --- a/pyembroidery/EmbPattern.py +++ b/pyembroidery/EmbPattern.py @@ -66,7 +66,7 @@ import pyembroidery.Vp3Writer as Vp3Writer import pyembroidery.XxxReader as XxxReader import pyembroidery.XxxWriter as XxxWriter -# import pyembroidery.ZhsReader as ZhsReader +import pyembroidery.ZhsReader as ZhsReader import pyembroidery.ZxyReader as ZxyReader from .EmbEncoder import Transcoder as Normalizer @@ -923,7 +923,7 @@ class EmbPattern: ) yield ( { - "description": "Melco Embroidery Format", + "description": "Melco Expanded Embroidery Format", "extension": "exp", "extensions": ("exp",), "mimetype": "application/x-exp", @@ -932,6 +932,16 @@ class EmbPattern: "writer": ExpWriter, } ) + # yield ( + # { + # "description": "Melco Condensed Embroidery Format", + # "extension": "cnd", + # "extensions": ("cnd",), + # "mimetype": "application/x-cnd", + # "category": "embroidery", + # "reader": CndReader, + # } + # ) yield ( { "description": "Tajima Embroidery Format", @@ -1317,13 +1327,13 @@ class EmbPattern: "reader": StcReader, } ) - # yield ({ - # "description": "Zeng Hsing Embroidery Format", - # "extension": "zhs", - # "mimetype": "application/x-zhs", - # "category": "embroidery", - # "reader": ZhsReader - # }) + yield ({ + "description": "Zeng Hsing Embroidery Format", + "extension": "zhs", + "mimetype": "application/x-zhs", + "category": "embroidery", + "reader": ZhsReader + }) yield ( { "description": "ZSK TC Embroidery Format", diff --git a/pyembroidery/ZhsReader.py b/pyembroidery/ZhsReader.py index 53c38ed..f927d6e 100644 --- a/pyembroidery/ZhsReader.py +++ b/pyembroidery/ZhsReader.py @@ -3,6 +3,7 @@ from .ReadHelper import read_int_32le, signed8 def read_zhs_stitches(f, out): count = 0 + while True: count += 1 b = bytearray(f.read(3)) @@ -10,14 +11,44 @@ def read_zhs_stitches(f, out): break ctrl = b[0] if ctrl == 0x10: - pass - x = signed8(b[1]) - y = signed8(b[2]) + continue + i = (b[1] << 8) + b[2] + x = 0 + x |= (i & 0b00000001_00000000) >> 8 + x |= (i & 0b00000000_00000010) >> 0 + x |= (i & 0b00000100_00000000) >> 8 + x |= (i & 0b00000000_00001000) >> 0 + x |= (i & 0b00010000_00000000) >> 8 + x |= (i & 0b00000000_00100000) >> 0 + x |= (i & 0b01000000_00000000) >> 8 + x |= (i & 0b00000000_10000000) >> 0 + if x >= 127: + x = -256 + x + if x >= 63: + x += 1 + if x <= -63: + x -= 1 + y = 0 + y |= (i & 0b00000000_00000001) >> 0 + y |= (i & 0b00000010_00000000) >> 8 + y |= (i & 0b00000000_00000100) >> 0 + y |= (i & 0b00001000_00000000) >> 8 + y |= (i & 0b00000000_00010000) >> 0 + y |= (i & 0b00100000_00000000) >> 8 + y |= (i & 0b00000000_01000000) >> 0 + y |= (i & 0b10000000_00000000) >> 8 + + if y >= 127: + y = -256 + y + if y >= 63: + y += 1 + if y <= -63: + y -= 1 if ctrl == 0x02: - out.stitch(x, y) + out.stitch(x, -y) continue if ctrl == 0x01: - out.move(x, y) + out.move(x,-y) continue if ctrl == 0x04: out.color_change() From 81a780f2ee8d88231e07ded0efb4649d8a991c85 Mon Sep 17 00:00:00 2001 From: tatarize Date: Sun, 25 Jul 2021 09:06:01 -0700 Subject: [PATCH 2/6] Zhs Reader Improvements --- pyembroidery/ZhsReader.py | 63 +++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/pyembroidery/ZhsReader.py b/pyembroidery/ZhsReader.py index f927d6e..f7e836f 100644 --- a/pyembroidery/ZhsReader.py +++ b/pyembroidery/ZhsReader.py @@ -4,6 +4,8 @@ from .ReadHelper import read_int_32le, signed8 def read_zhs_stitches(f, out): count = 0 + xx = 0 + yy = 0 while True: count += 1 b = bytearray(f.read(3)) @@ -11,32 +13,35 @@ def read_zhs_stitches(f, out): break ctrl = b[0] if ctrl == 0x10: + # Checksum continue - i = (b[1] << 8) + b[2] + # x x = 0 - x |= (i & 0b00000001_00000000) >> 8 - x |= (i & 0b00000000_00000010) >> 0 - x |= (i & 0b00000100_00000000) >> 8 - x |= (i & 0b00000000_00001000) >> 0 - x |= (i & 0b00010000_00000000) >> 8 - x |= (i & 0b00000000_00100000) >> 0 - x |= (i & 0b01000000_00000000) >> 8 - x |= (i & 0b00000000_10000000) >> 0 + x += b[1] & 0b00000001 + x += b[2] & 0b00000010 + x += b[1] & 0b00000100 + x += b[2] & 0b00001000 + x += b[1] & 0b00010000 + x += b[2] & 0b00100000 + x += b[1] & 0b01000000 + x += b[2] & 0b10000000 if x >= 127: x = -256 + x if x >= 63: x += 1 if x <= -63: x -= 1 + + # y y = 0 - y |= (i & 0b00000000_00000001) >> 0 - y |= (i & 0b00000010_00000000) >> 8 - y |= (i & 0b00000000_00000100) >> 0 - y |= (i & 0b00001000_00000000) >> 8 - y |= (i & 0b00000000_00010000) >> 0 - y |= (i & 0b00100000_00000000) >> 8 - y |= (i & 0b00000000_01000000) >> 0 - y |= (i & 0b10000000_00000000) >> 8 + y += b[2] & 0b00000001 + y += b[1] & 0b00000010 + y += b[2] & 0b00000100 + y += b[1] & 0b00001000 + y += b[2] & 0b00010000 + y += b[1] & 0b00100000 + y += b[2] & 0b01000000 + y += b[1] & 0b10000000 if y >= 127: y = -256 + y @@ -44,16 +49,28 @@ def read_zhs_stitches(f, out): y += 1 if y <= -63: y -= 1 - if ctrl == 0x02: - out.stitch(x, -y) + + xx += x + yy += y + if ctrl == 0x41: + # Still unmapped. + pass + elif ctrl == 0x02: + out.stitch(xx, -yy) + xx = 0 + yy = 0 continue - if ctrl == 0x01: - out.move(x,-y) + elif ctrl == 0x01: + out.move(xx, -yy) + xx = 0 + yy = 0 continue - if ctrl == 0x04: + elif ctrl == 0x04: + xx = 0 + yy = 0 out.color_change() continue - if ctrl == 0x80: + elif ctrl == 0x80: break out.end() From ec5bf0c46f394484877994145c26318caf24993e Mon Sep 17 00:00:00 2001 From: tatarize Date: Sun, 25 Jul 2021 17:15:01 -0700 Subject: [PATCH 3/6] ZhsColors --- pyembroidery/ZhsReader.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/pyembroidery/ZhsReader.py b/pyembroidery/ZhsReader.py index f7e836f..b44bf49 100644 --- a/pyembroidery/ZhsReader.py +++ b/pyembroidery/ZhsReader.py @@ -1,4 +1,4 @@ -from .ReadHelper import read_int_32le, signed8 +from .ReadHelper import read_int_32le, read_int_16le, read_int_8, read_int_24be def read_zhs_stitches(f, out): @@ -75,8 +75,36 @@ def read_zhs_stitches(f, out): out.end() +def read_zhz_header(f, out): + color_count = read_int_8(f) + for i in range(color_count): + out.add_thread(read_int_24be(f)) + length = read_int_16le(f) + b = bytearray(f.read(length)) + thread_data = b.decode('utf8') + threads = thread_data.split("&$") + try: + for i, data in enumerate(threads[1:]): + thread = out.threadlist[i] + parts = data.split("&#") + try: + if len(parts[0]): + thread.chart = parts[0] + if len(parts[1]): + thread.description = parts[1] + if len(parts[2]) > 3: + thread.catalog_number = parts[2][:-2] + except IndexError: + pass + except IndexError: + pass + + def read(f, out, settings=None): f.seek(0x0F, 0) stitch_start_position = read_int_32le(f) + header_start_position = read_int_32le(f) + f.seek(header_start_position, 0) + read_zhz_header(f, out) f.seek(stitch_start_position, 0) read_zhs_stitches(f, out) From 3d032cc43b24fce0addd0c8a6c0b485ee6ffad32 Mon Sep 17 00:00:00 2001 From: tatarize Date: Sun, 25 Jul 2021 23:09:46 -0700 Subject: [PATCH 4/6] Edge Condition Signed Value Corrects a mangling bug. --- pyembroidery/ZhsReader.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pyembroidery/ZhsReader.py b/pyembroidery/ZhsReader.py index b44bf49..042d7d2 100644 --- a/pyembroidery/ZhsReader.py +++ b/pyembroidery/ZhsReader.py @@ -1,4 +1,4 @@ -from .ReadHelper import read_int_32le, read_int_16le, read_int_8, read_int_24be +from .ReadHelper import read_int_32le, read_int_16le, read_int_8, read_int_24be, signed8 def read_zhs_stitches(f, out): @@ -25,8 +25,7 @@ def read_zhs_stitches(f, out): x += b[2] & 0b00100000 x += b[1] & 0b01000000 x += b[2] & 0b10000000 - if x >= 127: - x = -256 + x + x = signed8(x) if x >= 63: x += 1 if x <= -63: @@ -43,8 +42,7 @@ def read_zhs_stitches(f, out): y += b[2] & 0b01000000 y += b[1] & 0b10000000 - if y >= 127: - y = -256 + y + y = signed8(y) if y >= 63: y += 1 if y <= -63: @@ -61,6 +59,7 @@ def read_zhs_stitches(f, out): yy = 0 continue elif ctrl == 0x01: + print(xx,yy,"move") out.move(xx, -yy) xx = 0 yy = 0 From 43447a749e6ba5fd82e2ba105bbb3ef4c3521edf Mon Sep 17 00:00:00 2001 From: tatarize Date: Sun, 25 Jul 2021 23:17:10 -0700 Subject: [PATCH 5/6] Version Bump --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 0ca7fca..5f7a2d6 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r") as fh: setuptools.setup( name="pyembroidery", - version="1.4.29", + version="1.4.30", author="Tatarize", author_email="tatarize@gmail.com", description="Embroidery IO library", From 7cf247e8a6c62923ec8f0afae6138b8c6caeeb40 Mon Sep 17 00:00:00 2001 From: tatarize Date: Sun, 25 Jul 2021 23:26:50 -0700 Subject: [PATCH 6/6] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 22d723e..b4f124a 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,7 @@ Pyembroidery will read: * .tbf * .u01 * .xxx +* .zhs * .zxy * .gcode