Attachment 'lutctrl.py'

Download

   1 #Simple LUT Dialog version 20071011. Use at your own risk!
   2 #Code put in the public domain, for someone else could find a use for it.
   3 
   4 import wx
   5 import wx.lib.newevent
   6 
   7 from lutdata import LutData
   8 
   9 LutEvent, EVT_LUT = wx.lib.newevent.NewEvent()
  10 
  11 class LutPanel(wx.Panel,LutData):
  12     def __init__(self, parent,lut_index=0):
  13         wx.Panel.__init__(self, parent, -1,size=(10,24),style=wx.SUNKEN_BORDER)
  14         LutData.__init__(self)
  15 
  16         if lut_index >= self.get_count():
  17             lut_index = 0
  18 
  19         self.SetLutIndex(lut_index,redraw=0)
  20 
  21         self.box = wx.BoxSizer(wx.VERTICAL)
  22         self.sb=wx.StaticBitmap(self, -1)
  23         self.box.Add(self.sb,1,wx.EXPAND)
  24         self.Bind(wx.EVT_SIZE, self.OnSize)
  25 
  26         self.box.Layout()
  27         self.SetSizer(self.box)
  28 
  29     def OnSize(self,event):
  30         self.Redraw()
  31 
  32     def Redraw(self):
  33         w,h=self.GetClientSize()
  34         if self.img is None or w<=0 or h<=0:
  35             return
  36 
  37         self.sb.SetToolTip(wx.ToolTip(self._names[self.lut_index]))
  38         self.sb.SetBitmap(self.img.Scale(w,h).ConvertToBitmap())
  39         self.Refresh()
  40 
  41     def GetLutCount(self):
  42         return len(self._names)
  43 
  44     def GetLutIndex(self):
  45         return self.lut_index
  46 
  47     def GetLutNames(self):
  48         return self._names
  49 
  50     def GetLutName(self):
  51         return self.get_name(self.lut_index)
  52 
  53     #returns an interleaved "RGBRGBRGB..." string, 768 bytes long.
  54     def GetLut(self):
  55         return self.get_lut(self.lut_index)
  56 
  57     #returns 3 strings R,G,B 256 bytes long.
  58     #These are easy to convert to numarray, Numeric or numpy...
  59     #
  60     #r,g,b = event.GetLutRGB()
  61     #r_array = Numeric.array(r)
  62     #g_array = Numeric.array(r)
  63     #b_array = Numeric.array(r)
  64     def GetLutRGB(self):
  65         return self.get_rgb(self.lut_index)
  66 
  67     #select a LUT to display
  68     def SetLutIndex(self,lut_index,redraw=1):
  69         self.lut_index=lut_index
  70 
  71         s=self.get_lut(lut_index)
  72 
  73         image = wx.EmptyImage(256,1)
  74         image.SetData(s)
  75 
  76         self.img=image
  77 
  78         if redraw:
  79             self.Redraw()
  80 
  81 class LutChoice(wx.Panel):
  82     def __init__(self, parent,lut_index=0,height=20):
  83         wx.Panel.__init__(self, parent, -1)
  84 
  85         self.box = wx.BoxSizer(wx.HORIZONTAL)
  86         self.lp=LutPanel(self,lut_index)
  87         self.lut_index=lut_index
  88 
  89         self.ch=wx.Choice(self, -1, size=wx.Size(110, -1), choices = self.lp.GetLutNames())
  90         self.ch.SetSelection(lut_index)
  91 
  92         self.box.Add(self.ch,0)
  93         self.box.Add(self.lp,1,wx.EXPAND)
  94         self.box.Layout()
  95         self.SetSizer(self.box)
  96 
  97         self.Bind(wx.EVT_CHOICE,self.OnChoice,self.ch)
  98 
  99         self.GetLutCount=self.lp.GetLutCount
 100         self.GetLutIndex=self.lp.GetLutIndex
 101         self.GetLutRGB=self.lp.GetLutRGB
 102         self.GetLutName=self.lp.GetLutName
 103 
 104     def SetLutIndex(self,lut_index):
 105         self.SetLutIndex=self.lp.SetLutIndex(lut_index,redraw=1)
 106         self.ch.SetSelection(lut_index)
 107 
 108     def OnChoice(self, event):
 109         lut_index=self.ch.GetSelection()
 110         if lut_index!=self.lut_index:
 111             self.lut_index=lut_index
 112             self.lp.SetLutIndex(lut_index,redraw=1)
 113 
 114             lut_event = LutEvent(
 115                     GetLutCount=self.GetLutCount,
 116                     GetLutIndex=self.GetLutIndex,
 117                     GetLutRGB=self.GetLutRGB,
 118                     GetLutName=self.GetLutName)
 119             wx.PostEvent(self, lut_event)
 120 
 121 class LutSpin(wx.Panel):
 122     def __init__(self, parent,lut_index=0,height=20):
 123         wx.Panel.__init__(self, parent, -1)
 124 
 125         self.box = wx.BoxSizer(wx.HORIZONTAL)
 126         self.lp=LutPanel(self,lut_index)
 127         self.lp.SetToolTipString(self.lp.GetLutName())
 128 
 129         self.lut_index=lut_index
 130 
 131         self.spin = wx.SpinButton(self, -1, size=(height*.75, height), style=wx.SP_VERTICAL)
 132         self.spin.SetRange(0, self.lp.GetLutCount()-1)
 133         self.spin.SetValue(lut_index)
 134 
 135         self.box.Add(self.lp,1,wx.EXPAND)
 136         self.box.Add(self.spin,0)
 137         self.box.Layout()
 138         self.SetSizer(self.box)
 139 
 140         self.Bind(wx.EVT_SPIN, self.OnSpin, self.spin)
 141 
 142         self.GetLutCount=self.lp.GetLutCount
 143         self.GetLutIndex=self.lp.GetLutIndex
 144         self.GetLutRGB=self.lp.GetLutRGB
 145         self.GetLutName=self.lp.GetLutName
 146 
 147     def SetLutIndex(self,lut_index):
 148         self.SetLutIndex=self.lp.SetLutIndex(lut_index,redraw=1)
 149         self.spin.SetValue(lut_index)
 150 
 151     def OnSpin(self, event):
 152         lut_index=event.GetPosition()
 153         if lut_index!=self.lut_index:
 154             self.lut_index=lut_index
 155             self.lp.SetLutIndex(lut_index,redraw=1)
 156             self.lp.SetToolTipString(self.lp.GetLutName())
 157 
 158             lut_event = LutEvent(
 159                     GetLutCount=self.GetLutCount,
 160                     GetLutIndex=self.GetLutIndex,
 161                     GetLutRGB=self.GetLutRGB,
 162                     GetLutName=self.GetLutName)
 163             wx.PostEvent(self, lut_event)
 164 
 165 if __name__ == "__main__":
 166 
 167     class MyFrame(wx.Frame):
 168         def __init__(self):
 169             wx.Frame.__init__(self, None, -1, "Lut Control", size=(350,120))
 170             self.CreateStatusBar()
 171 
 172             p = wx.Panel(self,-1)
 173             cl=LutChoice(p,0)
 174             sl=LutSpin(p,28)
 175             pl=LutPanel(p,13)
 176 
 177             box = wx.BoxSizer(wx.VERTICAL)
 178             box.Add(cl,0,wx.EXPAND|wx.ALL,2)
 179             box.Add(sl,0,wx.EXPAND|wx.ALL,2)
 180             box.Add(pl,1,wx.EXPAND|wx.ALL,2)
 181             box.Layout()
 182             p.SetSizer(box)
 183 
 184             cl.Bind(EVT_LUT, self.OnLut)
 185             sl.Bind(EVT_LUT, self.OnLut)
 186 
 187             self.pl = pl
 188 
 189         def OnLut(self,event):
 190             s = "LUT %d of %d is '%s'" % (
 191                 event.GetLutIndex(),
 192                 event.GetLutCount(),
 193                 event.GetLutName())
 194 
 195             #The event.GetLUT() and event.GetLUTRGB() are also available.
 196             #They return and RGB string (interleaved) or separate R,G,B strings
 197             #These are easy to convert to numarray, Numeric or numpy...
 198             #
 199             #r,g,b = event.GetLutRGB()
 200             #r_array = Numeric.array(r)
 201             #g_array = Numeric.array(r)
 202             #b_array = Numeric.array(r)
 203 
 204             self.SetStatusText(s)
 205             self.pl.SetLutIndex(event.GetLutIndex())
 206 
 207     app = wx.PySimpleApp()
 208     f = MyFrame()
 209     f.Show()
 210     app.MainLoop()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-10-04 09:16:03, 6.4 KB) [[attachment:lutctrl.py]]
  • [get | view] (2009-10-04 09:16:03, 27.7 KB) [[attachment:lutdata.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.