Adding Color File Formats and JSON

pull/78/head
tatarize 2019-08-30 00:08:31 -07:00 zatwierdzone przez GitHub
rodzic 8898f14c43
commit 97efa90a26
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
11 zmienionych plików z 288 dodań i 19 usunięć

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

@ -3,6 +3,8 @@ import os.path
import pyembroidery.A100Reader as A100Reader
import pyembroidery.A10oReader as A10oReader
import pyembroidery.BroReader as BroReader
import pyembroidery.ColReader as ColReader
import pyembroidery.ColWriter as ColWriter
import pyembroidery.CsvReader as CsvReader
import pyembroidery.CsvWriter as CsvWriter
import pyembroidery.DatReader as DatReader
@ -10,6 +12,8 @@ import pyembroidery.DsbReader as DsbReader
import pyembroidery.DstReader as DstReader
import pyembroidery.DstWriter as DstWriter
import pyembroidery.DszReader as DszReader
import pyembroidery.EdrReader as EdrReader
import pyembroidery.EdrWriter as EdrWriter
import pyembroidery.EmdReader as EmdReader
import pyembroidery.ExpReader as ExpReader
import pyembroidery.ExpWriter as ExpWriter
@ -19,10 +23,14 @@ import pyembroidery.GcodeReader as GcodeReader
import pyembroidery.GcodeWriter as GcodeWriter
import pyembroidery.GtReader as GtReader
import pyembroidery.HusReader as HusReader
import pyembroidery.InfReader as InfReader
import pyembroidery.InfWriter as InfWriter
import pyembroidery.InbReader as InbReader
import pyembroidery.JefReader as JefReader
import pyembroidery.JefWriter as JefWriter
import pyembroidery.JpxReader as JpxReader
import pyembroidery.JsonWriter as JsonWriter
import pyembroidery.JsonReader as JsonReader
import pyembroidery.KsmReader as KsmReader
import pyembroidery.MaxReader as MaxReader
import pyembroidery.MitReader as MitReader
@ -471,6 +479,42 @@ def supported_formats():
"category": "embroidery",
"reader": HusReader
})
yield ({
"description": "Edr Color Format",
"extension": "edr",
"extensions": ("edr",),
"mimetype": "application/x-edr",
"category": "color",
"reader": EdrReader,
"writer": EdrWriter
})
yield ({
"description": "Col Color Format",
"extension": "col",
"extensions": ("col",),
"mimetype": "application/x-col",
"category": "color",
"reader": ColReader,
"writer": ColWriter
})
yield ({
"description": "Inf Color Format",
"extension": "inf",
"extensions": ("inf",),
"mimetype": "application/x-inf",
"category": "color",
"reader": InfReader,
"writer": InfWriter
})
yield ({
"description": "Json Export",
"extension": "json",
"extensions": ("json",),
"mimetype": "application/json",
"category": "debug",
"reader": JsonReader,
"writer": JsonWriter
})
def convert(filename_from, filename_to, settings=None):
@ -645,8 +689,23 @@ def write_embroidery(writer, pattern, stream, settings=None):
pattern = pattern.get_normalized_pattern(settings)
if is_str(stream):
with open(stream, "wb") as stream:
writer.write(pattern, stream, settings)
text_mode = False
try:
text_mode = writer.WRITE_FILE_IN_TEXT_MODE
except AttributeError:
pass
if text_mode:
try:
with open(stream, "w") as stream:
writer.write(pattern, stream, settings)
except IOError:
pass
else:
try:
with open(stream, "wb") as stream:
writer.write(pattern, stream, settings)
except IOError:
pass
else:
writer.write(pattern, stream, settings)

Wyświetl plik

@ -77,7 +77,6 @@ def vp3_read_colorblock(f, out, center_x, center_y):
bytescheck = f.read(3) # \x0A\xF6\x00
stitch_byte_length = block_end_position - f.tell()
stitch_bytes = read_signed(f, stitch_byte_length)
trimmed = False
i = 0
while i < len(stitch_bytes) - 1:
x = stitch_bytes[i]
@ -85,26 +84,20 @@ def vp3_read_colorblock(f, out, center_x, center_y):
i += 2
if (x & 0xFF) != 0x80:
out.stitch(x, y)
trimmed = False
continue
if y == 0x01:
x = signed16(stitch_bytes[i], stitch_bytes[i + 1])
i += 2
y = signed16(stitch_bytes[i], stitch_bytes[i + 1])
i += 2
if abs(x) > 255 or abs(y) > 255:
if not trimmed:
out.trim()
out.move(x, y)
trimmed = True
else:
out.stitch(x, y)
out.stitch(x, y)
i += 2
# Final element is typically 0x80 0x02, this is skipped regardless of its value.
elif y == 0x02:
pass # ends long stitch mode.
# This is only seen after 80 01 and should have been skipped. Has no known effect.
pass
elif y == 0x03:
if not trimmed:
out.trim()
trimmed = True
out.trim()
def vp3_read_thread(f):

Wyświetl plik

@ -226,8 +226,12 @@ def write_stitches_block(f, stitches, first_pos_x, first_pos_y):
flags = stitch[2] & COMMAND_MASK
alt = stitch[2] & FLAGS_MASK
if flags == END:
# This is a trim command. The machine does not autotrim.
# Consequently writers tend to add this explicit trim command.
f.write(b'\x80\x03')
break
elif flags == COLOR_CHANGE:
# Colorchange commands divided the pattern into colorblocks.
continue
elif flags == TRIM:
f.write(b'\x80\x03')
@ -237,11 +241,10 @@ def write_stitches_block(f, stitches, first_pos_x, first_pos_y):
elif flags == SEQUIN_EJECT:
continue
elif flags == STOP:
# Not sure what to do here.
# f.write(b'\x80\x04')
continue
elif flags == JUMP:
# Since VP3.Jump == VP3.Stitch, we combine jumps.
# VP3 has no jump commands. These are skipped.
# It moves to the relevant location without needing to block the needlebar.
continue
dx = int(x - last_x)
dy = int(y - last_y)
@ -256,5 +259,4 @@ def write_stitches_block(f, stitches, first_pos_x, first_pos_y):
write_int_16be(f, dx)
write_int_16be(f, dy)
f.write(b'\x80\x02')
# VSM gave ending stitches as 80 03 35 A5, so, 80 03 isn't strictly end.
vp3_patch_byte_offset(f, placeholder_distance_to_end_of_stitches_block_010)