1 | |
---|
2 | import wx |
---|
3 | import os, stat |
---|
4 | |
---|
5 | #---------------------------------------------------------------------- |
---|
6 | |
---|
7 | PATH2DXT = "/Users/luc/Dev/FastDXT/bin/2dxt" |
---|
8 | |
---|
9 | WIDTH = 1920 |
---|
10 | HEIGHT = 1080 |
---|
11 | FORMAT = 1 |
---|
12 | #---------------------------------------------------------------------- |
---|
13 | |
---|
14 | def mylistdir(directory): |
---|
15 | """A specialized version of os.listdir() that ignores files that |
---|
16 | start with a leading period.""" |
---|
17 | filelist = os.listdir(directory) |
---|
18 | return [x for x in filelist |
---|
19 | if not (x.startswith('.'))] |
---|
20 | |
---|
21 | def Convert(fn): |
---|
22 | dn, bs = os.path.split(fn) |
---|
23 | file, ext = os.path.splitext(bs) |
---|
24 | |
---|
25 | if ext != ".rgb" and ext != ".rgba": |
---|
26 | frgb = "%s/%s.%s" % ( dn, file, "rgba" ) |
---|
27 | command = "convert %s -depth 8 RGBA:%s" % (fn, frgb) |
---|
28 | print command |
---|
29 | os.system( command ) |
---|
30 | |
---|
31 | fout = "%s/%s.%s" % ( dn, file, "dxt" ) |
---|
32 | command = "%s %d %d %d %s %s && /bin/rm -f %s" % (PATH2DXT, WIDTH, HEIGHT, FORMAT, frgb, fout, frgb) |
---|
33 | else: |
---|
34 | fout = "%s/%s.%s" % ( dn, file, "dxt" ) |
---|
35 | command = "%s %d %d %d %s %s" % (PATH2DXT, WIDTH, HEIGHT, FORMAT, fn, fout) |
---|
36 | |
---|
37 | print command |
---|
38 | os.system( command ) |
---|
39 | |
---|
40 | class MyFileDropTarget(wx.FileDropTarget): |
---|
41 | def __init__(self, window): |
---|
42 | wx.FileDropTarget.__init__(self) |
---|
43 | self.window = window |
---|
44 | |
---|
45 | def OnDropFiles(self, x, y, filenames): |
---|
46 | self.window.SetInsertionPointEnd() |
---|
47 | self.window.Clear() |
---|
48 | |
---|
49 | fn = list() |
---|
50 | |
---|
51 | if len(filenames) == 1: |
---|
52 | mode = os.stat(filenames[0])[stat.ST_MODE] |
---|
53 | if stat.S_ISDIR(mode): |
---|
54 | # It's a directory, recurse into it |
---|
55 | for f in mylistdir(filenames[0]): |
---|
56 | full = os.path.join(filenames[0], f) |
---|
57 | if stat.S_ISREG( os.stat(full)[stat.ST_MODE] ): |
---|
58 | fn.append(full) |
---|
59 | elif stat.S_ISREG(mode): |
---|
60 | # It's a file, call the callback function |
---|
61 | fn.append(filenames[0]) |
---|
62 | else: |
---|
63 | # Unknown file type, print a message |
---|
64 | print 'Skipping %s' % pathname |
---|
65 | else: |
---|
66 | for f in filenames: |
---|
67 | fn.append(f) |
---|
68 | |
---|
69 | fn.sort() |
---|
70 | max = len(fn) |
---|
71 | |
---|
72 | self.window.WriteText("\n%d file(s) dropped:\n" % len(fn)) |
---|
73 | |
---|
74 | dlg = wx.ProgressDialog("DXT Conversion", "Converting RAW files to DXT", |
---|
75 | maximum = max, |
---|
76 | parent=self.window, |
---|
77 | style = wx.PD_CAN_ABORT | wx.PD_APP_MODAL| wx.PD_ELAPSED_TIME| wx.PD_REMAINING_TIME) |
---|
78 | |
---|
79 | keepGoing = True |
---|
80 | count = 0 |
---|
81 | |
---|
82 | while keepGoing and count < max: |
---|
83 | self.window.WriteText(fn[count] + '\n') |
---|
84 | (keepGoing, skip) = dlg.Update(count, fn[count]) |
---|
85 | Convert(fn[count]) |
---|
86 | count += 1 |
---|
87 | |
---|
88 | dlg.Destroy() |
---|
89 | |
---|
90 | |
---|
91 | class FileDropPanel(wx.Panel): |
---|
92 | def __init__(self, parent): |
---|
93 | wx.Panel.__init__(self, parent, -1) |
---|
94 | |
---|
95 | sizer = wx.BoxSizer(wx.VERTICAL) |
---|
96 | |
---|
97 | # Format selection |
---|
98 | rb = wx.RadioBox(self, -1, "Select DXT format:", wx.DefaultPosition, wx.DefaultSize, |
---|
99 | ['DXT1', 'DXT5', 'DXT6 (5-YCoCg)'], 1, wx.RA_SPECIFY_ROWS) |
---|
100 | self.Bind(wx.EVT_RADIOBOX, self.OnFormat, rb) |
---|
101 | sizer.Add(rb, 0, wx.ALL, 5) |
---|
102 | |
---|
103 | # Width and height setting |
---|
104 | ssizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
105 | |
---|
106 | ssizer.Add(wx.StaticText(self, -1, "Width", style=wx.ALIGN_BOTTOM), 0, wx.ALL, 5) |
---|
107 | sw = wx.SpinCtrl(self, -1, "", wx.DefaultPosition, (100,-1)) |
---|
108 | sw.SetRange(1,65535) |
---|
109 | sw.SetValue(WIDTH) |
---|
110 | self.Bind(wx.EVT_SPINCTRL, self.OnWidth, sw) |
---|
111 | ssizer.Add(sw, 0, wx.ALL, 0) |
---|
112 | |
---|
113 | ssizer.Add(wx.StaticText(self, -1, "Height", style=wx.ALIGN_BOTTOM), 0, wx.ALL, 5) |
---|
114 | sh = wx.SpinCtrl(self, -1, "", wx.DefaultPosition, (100,-1)) |
---|
115 | sh.SetRange(1,65535) |
---|
116 | sh.SetValue(HEIGHT) |
---|
117 | self.Bind(wx.EVT_SPINCTRL, self.OnHeight, sh) |
---|
118 | ssizer.Add(sh, 0, wx.ALL, 0) |
---|
119 | |
---|
120 | sizer.Add(ssizer, 0, wx.ALL, 5) |
---|
121 | |
---|
122 | # Drop box |
---|
123 | sizer.Add( |
---|
124 | wx.StaticText(self, -1, " \nDrag some files here:"), |
---|
125 | 0, wx.EXPAND|wx.ALL, 5) |
---|
126 | |
---|
127 | self.text = wx.TextCtrl( |
---|
128 | self, -1, "\n\tImage files ...", |
---|
129 | style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY) |
---|
130 | |
---|
131 | dt = MyFileDropTarget(self) |
---|
132 | self.text.SetDropTarget(dt) |
---|
133 | sizer.Add(self.text, 1, wx.EXPAND) |
---|
134 | |
---|
135 | self.SetAutoLayout(True) |
---|
136 | self.SetSizer(sizer) |
---|
137 | |
---|
138 | def OnWidth(self, arg): |
---|
139 | global WIDTH |
---|
140 | WIDTH = arg.GetInt() |
---|
141 | def OnHeight(self, arg): |
---|
142 | global HEIGHT |
---|
143 | HEIGHT = arg.GetInt() |
---|
144 | def OnFormat(self, arg): |
---|
145 | global FORMAT |
---|
146 | f = arg.GetInt() |
---|
147 | if f == 0: |
---|
148 | FORMAT = 1 |
---|
149 | if f == 1: |
---|
150 | FORMAT = 5 |
---|
151 | if f == 2: |
---|
152 | FORMAT = 6 |
---|
153 | |
---|
154 | def WriteText(self, text): |
---|
155 | self.text.WriteText(text) |
---|
156 | |
---|
157 | def Clear(self): |
---|
158 | self.text.Clear() |
---|
159 | |
---|
160 | def SetInsertionPointEnd(self): |
---|
161 | self.text.SetInsertionPointEnd() |
---|
162 | |
---|
163 | |
---|
164 | #---------------------------------------------------------------------- |
---|
165 | |
---|
166 | class TestPanel(wx.Panel): |
---|
167 | def __init__(self, parent): |
---|
168 | wx.Panel.__init__(self, parent, -1) |
---|
169 | |
---|
170 | self.SetAutoLayout(True) |
---|
171 | outsideSizer = wx.BoxSizer(wx.VERTICAL) |
---|
172 | |
---|
173 | msg = "DXT Conversion Drag-And-Drop" |
---|
174 | text = wx.StaticText(self, -1, "", style=wx.ALIGN_CENTRE) |
---|
175 | text.SetFont(wx.Font(24, wx.SWISS, wx.NORMAL, wx.BOLD, False)) |
---|
176 | text.SetLabel(msg) |
---|
177 | |
---|
178 | w,h = text.GetTextExtent(msg) |
---|
179 | text.SetSize(wx.Size(w,h+1)) |
---|
180 | text.SetForegroundColour(wx.BLUE) |
---|
181 | outsideSizer.Add(text, 0, wx.EXPAND|wx.ALL, 5) |
---|
182 | outsideSizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND) |
---|
183 | |
---|
184 | inSizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
185 | inSizer.Add(FileDropPanel(self), 1, wx.EXPAND) |
---|
186 | |
---|
187 | outsideSizer.Add(inSizer, 1, wx.EXPAND) |
---|
188 | self.SetSizer(outsideSizer) |
---|
189 | |
---|
190 | |
---|
191 | #---------------------------------------------------------------------- |
---|
192 | class MainWindow(wx.Frame): |
---|
193 | """ This window displays the GUI Widgets. """ |
---|
194 | def __init__(self,parent,id,title): |
---|
195 | wx.Frame.__init__(self,parent,-4, title, size = (500,300), style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE) |
---|
196 | self.SetBackgroundColour(wx.WHITE) |
---|
197 | |
---|
198 | w = TestPanel(self) |
---|
199 | |
---|
200 | # Display the Window |
---|
201 | self.Show(True) |
---|
202 | |
---|
203 | |
---|
204 | class MyApp(wx.App): |
---|
205 | """ DXT Conversion """ |
---|
206 | def OnInit(self): |
---|
207 | """ Initialize the Application """ |
---|
208 | # Declare the Main Application Window |
---|
209 | frame = MainWindow(None, -1, "DXT Drag and Drop") |
---|
210 | |
---|
211 | # Show the Application as the top window |
---|
212 | self.SetTopWindow(frame) |
---|
213 | return True |
---|
214 | |
---|
215 | |
---|
216 | def runTest(frame, nb, log): |
---|
217 | win = TestPanel(nb, log) |
---|
218 | return win |
---|
219 | |
---|
220 | # Declare the Application and start the Main Loop |
---|
221 | app = MyApp(0) |
---|
222 | app.MainLoop() |
---|