Commit 88cb9879 authored by nextime's avatar nextime

Add option to enable dithering

parent ba27a8ba
...@@ -420,11 +420,12 @@ def parse_layer_name(txt): ...@@ -420,11 +420,12 @@ def parse_layer_name(txt):
(field, value) = arg.split("=") (field, value) = arg.split("=")
except: except:
raise ValueError("Invalid argument in layer '%s'" % layerName) raise ValueError("Invalid argument in layer '%s'" % layerName)
if (field == "feed" or field == "ppm"): if (field == "feed" or field == "ppm" or field="dither"):
try: try:
value = float(value) value = float(value)
except: except:
raise ValueError("Invalid layer name '%s'" % value) raise ValueError("Invalid layer name '%s'" % value)
params[field] = value params[field] = value
logger.write("%s == %s" % (field, value)) logger.write("%s == %s" % (field, value))
...@@ -951,7 +952,7 @@ class Gcode_tools(inkex.Effect): ...@@ -951,7 +952,7 @@ class Gcode_tools(inkex.Effect):
self.skipped = 0 self.skipped = 0
def compile_paths(parent, node, trans, laserPower=100): def compile_paths(parent, node, trans, laserPower=100, altdither=False):
# Apply the object transform, along with the parent transformation # Apply the object transform, along with the parent transformation
mat = node.get('transform', None) mat = node.get('transform', None)
path = {} path = {}
...@@ -1019,6 +1020,7 @@ class Gcode_tools(inkex.Effect): ...@@ -1019,6 +1020,7 @@ class Gcode_tools(inkex.Effect):
#Get the image size #Get the image size
imageDataWidth, imageDataheight = img.size imageDataWidth, imageDataheight = img.size
if altdither:
# dithering image and compile the pixels # dithering image and compile the pixels
inkex.errormsg("start dithering.") inkex.errormsg("start dithering.")
palette=list(set(img.convert("P", palette=Image.ADAPTIVE, colors=laserPower-1).getpalette())) palette=list(set(img.convert("P", palette=Image.ADAPTIVE, colors=laserPower-1).getpalette()))
...@@ -1040,11 +1042,10 @@ class Gcode_tools(inkex.Effect): ...@@ -1040,11 +1042,10 @@ class Gcode_tools(inkex.Effect):
pass pass
pixels = [[pixel[r, l] for r in xrange(imageDataWidth)] for l in xrange(imageDataheight)] pixels = [[pixel[r, l] for r in xrange(imageDataWidth)] for l in xrange(imageDataheight)]
else:
#Compile the pixels. #Compile the pixels.
#pixels = list(img.getdata()) pixels = list(img.getdata())
#pixels = [pixels[i * (imageDataWidth):(i + 1) * (imageDataWidth)] for i in xrange(imageDataheight)] pixels = [pixels[i * (imageDataWidth):(i + 1) * (imageDataWidth)] for i in xrange(imageDataheight)]
path['type'] = "raster" path['type'] = "raster"
path['width'] = imageDataWidth path['width'] = imageDataWidth
...@@ -1191,6 +1192,7 @@ class Gcode_tools(inkex.Effect): ...@@ -1191,6 +1192,7 @@ class Gcode_tools(inkex.Effect):
# Check if the layer specifies an alternative (from the default) feed rate # Check if the layer specifies an alternative (from the default) feed rate
altfeed = layerParams.get("feed", self.options.feed) altfeed = layerParams.get("feed", self.options.feed)
altppm = layerParams.get("ppm", None) altppm = layerParams.get("ppm", None)
altdither = layerParams.get("dither", False)
logger.write("layer %s" % layerName) logger.write("layer %s" % layerName)
if (layerParams): if (layerParams):
...@@ -1224,12 +1226,12 @@ class Gcode_tools(inkex.Effect): ...@@ -1224,12 +1226,12 @@ class Gcode_tools(inkex.Effect):
selected.remove(node) selected.remove(node)
try: try:
newPath = compile_paths(self, node, trans, laserPower).copy(); newPath = compile_paths(self, node, trans, laserPower, altdither).copy();
pathList.append(newPath) pathList.append(newPath)
inkex.errormsg("Built gcode for "+str(node.get("id"))+" - will be cut as %s." % (newPath['type']) ) inkex.errormsg("Built gcode for "+str(node.get("id"))+" - will be cut as %s." % (newPath['type']) )
except: except:
messageOnce = True messageOnce = True
for objectData in compile_paths(self, node, trans, laserPower): for objectData in compile_paths(self, node, trans, laserPower, altdither):
#if (messageOnce): #if (messageOnce):
inkex.errormsg("Built gcode for group "+str(node.get("id"))+", item %s - will be cut as %s." % (objectData['id'], objectData['type']) ) inkex.errormsg("Built gcode for group "+str(node.get("id"))+", item %s - will be cut as %s." % (objectData['id'], objectData['type']) )
#messageOnce = False #messageOnce = False
...@@ -1294,12 +1296,12 @@ class Gcode_tools(inkex.Effect): ...@@ -1294,12 +1296,12 @@ class Gcode_tools(inkex.Effect):
trans = simpletransform.parseTransform("") trans = simpletransform.parseTransform("")
for node in selected: for node in selected:
try: try:
newPath = compile_paths(self, node, trans, laserPower).copy(); newPath = compile_paths(self, node, trans, laserPower, altdither).copy();
pathList.append(newPath) pathList.append(newPath)
inkex.errormsg("Built gcode for "+str(node.get("id"))+" - will be cut as %s." % (newPath['type']) ) inkex.errormsg("Built gcode for "+str(node.get("id"))+" - will be cut as %s." % (newPath['type']) )
except: except:
messageOnce = True messageOnce = True
for objectData in compile_paths(self, node, trans, laserPower): for objectData in compile_paths(self, node, trans, laserPower, altdither):
#if (messageOnce): #if (messageOnce):
inkex.errormsg("Built gcode for group "+str(node.get("id"))+", item %s - will be cut as %s." % (objectData['id'], objectData['type']) ) inkex.errormsg("Built gcode for group "+str(node.get("id"))+", item %s - will be cut as %s." % (objectData['id'], objectData['type']) )
#messageOnce = False #messageOnce = False
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment