-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from EmbroidePy/tatarize-color+json
- Loading branch information
Showing
16 changed files
with
473 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from .EmbThread import * | ||
|
||
READ_FILE_IN_TEXT_MODE = True | ||
|
||
|
||
def read(f, out, settings=None): | ||
count = int(f.readline()) | ||
for i in range(0,count): | ||
line = f.readline() | ||
splits = line.split(',') | ||
thread = EmbThread() | ||
thread.catalog_number = splits[0] | ||
thread.set_color(int(splits[1]), int(splits[2]), int(splits[3])) | ||
out.add_thread(thread) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from .WriteHelper import write_string_utf8 | ||
|
||
ENCODE = False | ||
|
||
|
||
def write(pattern, f, settings=None): | ||
write_string_utf8(f, "%d\r\n" % len(pattern.threadlist)) | ||
index = 0 | ||
for thread in pattern.threadlist: | ||
write_string_utf8(f, "%d,%d,%d,%d\r\n" % ( | ||
index, | ||
thread.get_red(), | ||
thread.get_green(), | ||
thread.get_blue())) | ||
index += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from .EmbThread import EmbThread | ||
from .ReadHelper import read_int_8 | ||
|
||
|
||
def read(f, out, settings=None): | ||
while True: | ||
red = read_int_8(f) | ||
green = read_int_8(f) | ||
blue = read_int_8(f) | ||
if blue is None: | ||
return | ||
f.seek(1, 1) | ||
thread = EmbThread() | ||
thread.set_color(red, green, blue) | ||
out.add_thread(thread) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from .WriteHelper import write_int_8 | ||
|
||
ENCODE = False | ||
|
||
|
||
def write(pattern, f, settings=None): | ||
if len(pattern.threadlist) > 0: | ||
for thread in pattern.threadlist: | ||
write_int_8(f, thread.get_red()) | ||
write_int_8(f, thread.get_green()) | ||
write_int_8(f, thread.get_blue()) | ||
write_int_8(f, 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from .EmbThread import EmbThread | ||
from .ReadHelper import read_int_32be, read_int_16be | ||
|
||
|
||
def read(f, out, settings=None): | ||
u0 = read_int_32be(f) | ||
u1 = read_int_32be(f) | ||
u2 = read_int_32be(f) | ||
number_of_colors = read_int_32be(f) | ||
for j in range(0, number_of_colors): | ||
length = read_int_16be(f) - 2 # 2 bytes of the length. | ||
byte_data = bytearray(f.read(length)) | ||
if len(byte_data) != length: | ||
break | ||
red = byte_data[2] | ||
green = byte_data[3] | ||
blue = byte_data[4] | ||
thread = EmbThread() | ||
thread.set_color(red, green, blue) | ||
byte_data = byte_data[7:] | ||
for j in range(0, len(byte_data)): | ||
b = byte_data[j] | ||
if b == 0: | ||
thread.description = byte_data[:j].decode('utf8') | ||
byte_data = byte_data[j+1:] | ||
break | ||
for j in range(0, len(byte_data)): | ||
b = byte_data[j] | ||
if b == 0: | ||
thread.chart = byte_data[:j].decode('utf8') | ||
break | ||
out.add_thread(thread) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from .WriteHelper import write_int_8, write_int_32be, write_int_16be, write_string_utf8 | ||
|
||
ENCODE = False | ||
|
||
|
||
def patch_byte_offset(stream, offset): | ||
current_pos = stream.tell() | ||
stream.seek(offset, 0) # Absolute position seek. | ||
position = current_pos - offset - 4 # 4 bytes int32 | ||
write_int_32be(stream, position) | ||
stream.seek(current_pos, 0) # Absolute position seek. | ||
|
||
|
||
def write(pattern, f, settings=None): | ||
write_int_32be(f, 1) | ||
write_int_32be(f, 8) | ||
placeholder = f.tell() | ||
write_int_32be(f, 0) # Placeholder. | ||
write_int_32be(f, len(pattern.threadlist)) | ||
index = 0 | ||
for thread in pattern.threadlist: | ||
details = thread.description | ||
if details is None: | ||
details = "Unknown" | ||
chart = thread.chart | ||
if chart is None: | ||
chart = "Unknown" | ||
write_int_16be(f, 11 + len(details) + len(chart)) # 2 + 2 + 1 + 1 + 1 + 2 + d + 1 + c + 1 = 11 + d + c | ||
write_int_16be(f, index) # record index | ||
index += 1 | ||
write_int_8(f, thread.get_red()) | ||
write_int_8(f, thread.get_green()) | ||
write_int_8(f, thread.get_blue()) | ||
write_int_16be(f, index) # needle number | ||
write_string_utf8(f, details) | ||
write_int_8(f, 0) | ||
write_string_utf8(f, chart) | ||
write_int_8(f, 0) | ||
patch_byte_offset(f, placeholder) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from .EmbFunctions import * | ||
from .EmbThread import EmbThread | ||
|
||
|
||
def decoded_command(command_dict,name): | ||
split = name.split(' ') | ||
command = command_dict[split[0]] | ||
for sp in split[1:]: | ||
if sp[0] == "n": | ||
needle = int(sp[1:]) | ||
command |= (needle + 1) << 16 | ||
if sp[0] == "o": | ||
order = int(sp[1:]) | ||
command |= (order + 1) << 24 | ||
if sp[0] == "t": | ||
thread = int(sp[1:]) | ||
command |= (thread + 1) << 8 | ||
return command | ||
|
||
|
||
def read(f, out, settings=None): | ||
import json | ||
json_object = json.load(f) | ||
command_dict = get_command_dictionary() | ||
stitches = json_object['stitches'] | ||
extras = json_object['extras'] | ||
threadlist = json_object['threadlist'] | ||
for t in threadlist: | ||
color = t["color"] | ||
thread = EmbThread(color) | ||
thread.description = t["description"] | ||
thread.catalog_number = t["catalog_number"] | ||
thread.details = t["details"] | ||
thread.brand = t["brand"] | ||
thread.chart = t["chart"] | ||
thread.weight = t["weight"] | ||
out.add_thread(thread) | ||
for s in stitches: | ||
out.stitches.append( | ||
[ | ||
s[0], | ||
s[1], | ||
decoded_command(command_dict, s[2]) | ||
] | ||
) | ||
out.extras.update(extras) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from .EmbFunctions import * | ||
|
||
ENCODE = False | ||
WRITE_FILE_IN_TEXT_MODE = True | ||
|
||
|
||
def decoded_name(names, data): | ||
command = decode_embroidery_command(data) | ||
try: | ||
name = names[command[0]] | ||
if command[1] is not None: | ||
name = name + " t" + str(command[1]) | ||
if command[2] is not None: | ||
name = name + " n" + str(command[2]) | ||
if command[3] is not None: | ||
name = name + " o" + str(command[3]) | ||
except (IndexError, KeyError): | ||
name = "UNKNOWN " + str(data) | ||
return name | ||
|
||
|
||
def write(pattern, f, settings=None): | ||
import json | ||
names = get_common_name_dictionary() | ||
|
||
json_normal = { | ||
"threadlist": [ | ||
{ | ||
"color": thread.color, | ||
"description": thread.description, | ||
"catalog_number": thread.catalog_number, | ||
"details": thread.details, | ||
"brand": thread.brand, | ||
"chart": thread.chart, | ||
"weight": thread.weight | ||
} | ||
for thread in pattern.threadlist | ||
], | ||
"stitches": [[s[0], s[1], str(decoded_name(names, s[2]))] for s in pattern.stitches], | ||
"extras": pattern.extras | ||
} | ||
json.dump(json_normal, f, indent=4) |
Oops, something went wrong.