1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
6 from Cura.util import mesh
8 def supportedExtensions():
9 return ['.bmp', '.jpg', '.jpeg', '.png']
12 return ';'.join(map(lambda s: '*' + s, supportedExtensions()))
14 class convertImageDialog(wx.Dialog):
15 def __init__(self, parent, filename):
16 super(convertImageDialog, self).__init__(None, title="Convert image...")
17 wx.EVT_CLOSE(self, self.OnClose)
19 self.filename = filename
21 image = wx.Image(filename)
22 w, h = image.GetWidth() - 1, image.GetHeight() - 1
23 self.aspectRatio = float(w) / float(h)
26 self.SetSizer(wx.BoxSizer())
27 self.GetSizer().Add(p, 1, flag=wx.EXPAND)
29 s = wx.GridBagSizer(2, 2)
31 s.Add(wx.StaticText(p, -1, _('Height (mm)')), pos=(0, 0), flag=wx.LEFT|wx.TOP|wx.RIGHT, border=5)
32 self.heightInput = wx.TextCtrl(p, -1, '10.0')
33 s.Add(self.heightInput, pos=(0, 1), flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND, border=5)
35 s.Add(wx.StaticText(p, -1, _('Width (mm)')), pos=(1, 0), flag=wx.LEFT|wx.TOP|wx.RIGHT, border=5)
36 self.widthInput = wx.TextCtrl(p, -1, str(w * 0.3))
37 s.Add(self.widthInput, pos=(1, 1), flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND, border=5)
39 s.Add(wx.StaticText(p, -1, _('Depth (mm)')), pos=(2, 0), flag=wx.LEFT|wx.TOP|wx.RIGHT, border=5)
40 self.depthInput = wx.TextCtrl(p, -1, str(h * 0.3))
41 s.Add(self.depthInput, pos=(2, 1), flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND, border=5)
43 options = ['Darker is higher', 'Lighter is higher']
44 self.invertInput = wx.ComboBox(p, -1, options[0], choices=options, style=wx.CB_DROPDOWN|wx.CB_READONLY)
45 s.Add(self.invertInput, pos=(3, 1), flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND, border=5)
47 options = ['No smoothing', 'Light smoothing', 'Heavy smoothing']
48 self.smoothInput = wx.ComboBox(p, -1, options[0], choices=options, style=wx.CB_DROPDOWN|wx.CB_READONLY)
49 s.Add(self.smoothInput, pos=(4, 1), flag=wx.LEFT|wx.TOP|wx.RIGHT|wx.EXPAND, border=5)
51 self.okButton = wx.Button(p, -1, 'Ok')
52 s.Add(self.okButton, pos=(5, 1), flag=wx.ALL, border=5)
54 self.okButton.Bind(wx.EVT_BUTTON, self.OnOkClick)
55 self.widthInput.Bind(wx.EVT_TEXT, self.OnWidthEnter)
56 self.depthInput.Bind(wx.EVT_TEXT, self.OnDepthEnter)
64 def OnOkClick(self, e):
66 height = float(self.heightInput.GetValue())
67 width = float(self.widthInput.GetValue())
68 blur = self.smoothInput.GetSelection()
70 invert = self.invertInput.GetSelection() == 0
72 obj = convertImage(self.filename, height, width, blur, invert)
73 self.parent._scene.add(obj)
74 self.parent._scene.centerAll()
75 self.parent.sceneUpdated()
77 def OnWidthEnter(self, e):
79 w = float(self.widthInput.GetValue())
82 h = w / self.aspectRatio
83 self.depthInput.SetValue(str(h))
85 def OnDepthEnter(self, e):
87 h = float(self.depthInput.GetValue())
90 w = h * self.aspectRatio
91 self.widthInput.SetValue(str(w))
93 def convertImage(filename, height=20.0, width=100.0, blur=0, invert=False, baseHeight=1.0):
94 image = wx.Image(filename)
95 image.ConvertToGreyscale()
96 if image.GetHeight() > 512:
97 image.Rescale(image.GetWidth() * 512 / image.GetHeight(), 512, wx.IMAGE_QUALITY_HIGH)
98 if image.GetWidth() > 512:
99 image.Rescale(512, image.GetHeight() * 512 / image.GetWidth(), wx.IMAGE_QUALITY_HIGH)
101 image = image.Blur(blur)
102 z = numpy.fromstring(image.GetData(), numpy.uint8)
103 z = numpy.array(z[::3], numpy.float32) #Only get the R values (as we are grayscale), and convert to float values
104 pMin, pMax = numpy.min(z), numpy.max(z)
109 z = ((z - pMin) * height / (pMax - pMin)) + baseHeight
111 w, h = image.GetWidth(), image.GetHeight()
112 scale = width / (image.GetWidth() - 1)
114 y, x = numpy.mgrid[0:h,0:w]
115 x = numpy.array(x, numpy.float32, copy=False) * scale
116 y = numpy.array(y, numpy.float32, copy=False) *-scale
117 v0 = numpy.concatenate((x.reshape((n, 1)), y.reshape((n, 1)), z.reshape((n, 1))), 1)
118 v0 = v0.reshape((h, w, 3))
124 obj = mesh.printableObject(filename)
126 m._prepareFaceCount((w-1) * (h-1) * 2 + 2 + (w-1)*4 + (h-1)*4)
127 m.vertexes = numpy.array(numpy.concatenate((v1,v3,v2,v2,v3,v4), 2).reshape(((w-1) * (h-1) * 6, 3)), numpy.float32, copy=False)
128 m.vertexes = numpy.concatenate((m.vertexes, numpy.zeros(((2+(w-1)*4+(h-1)*4)*3, 3), numpy.float32)))
129 m.vertexCount = (w-1) * (h-1) * 6
132 m._addFace(0,0,0, x,0,0, 0,y,0)
133 m._addFace(x,y,0, 0,y,0, x,0,0)
134 for n in xrange(0, w-1):
137 m._addFace(x+scale,0,0, x,0,0, x,0,z[n])
138 m._addFace(x+scale,0,0, x,0,z[n], x+scale,0,z[n+1])
139 m._addFace(x+scale,y,0, x,y,z[i], x,y,0)
140 m._addFace(x+scale,y,0, x+scale,y,z[i+1], x,y,z[i])
143 for n in xrange(0, h-1):
146 m._addFace(0,y-scale,0, 0,y,z[n*w], 0,y,0)
147 m._addFace(0,y-scale,0, 0,y-scale,z[n*w+w], 0,y,z[n*w])
148 m._addFace(x,y-scale,0, x,y,0, x,y,z[i])
149 m._addFace(x,y-scale,0, x,y,z[i], x,y-scale,z[i+w])
150 obj._postProcessAfterLoad()