diff lightsim/openglsim.py @ 353:941cfe1e1691

lightsim now reads levels from dmxserver
author drewp@bigasterisk.com
date Mon, 11 Jun 2007 00:53:16 +0000
parents a08882a05d29
children 60b49f6c2027
line wrap: on
line diff
--- a/lightsim/openglsim.py	Sun Jun 10 17:25:23 2007 +0000
+++ b/lightsim/openglsim.py	Mon Jun 11 00:53:16 2007 +0000
@@ -6,7 +6,7 @@
 import numarray as num
 import Tkinter as tk
 import Image
-
+from louie import dispatcher
 try:
     from OpenGL import Tk as Togl
     from OpenGL.GL import *
@@ -27,20 +27,24 @@
     glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, imgString)
 
 class Surface(Togl.Opengl):
-    width = 512
-    height = 270
-    imgRescaleTo = 100
-    def __init__(self, master, filenames):
-        Togl.Opengl.__init__(self, master=master, width = self.width,
-                             height = self.height, double = True, depth = 0)
+    """widget that adds multiple image files together with adjustable scales"""
+    def __init__(self, master, filenames, width=512, height=270,
+                 imgRescaleTo=None):
+        """
+        imgRescaleTo can be a length of pixels to reduce all the input
+        images into. Try 64 for a low res drawing.
+        """
+        Togl.Opengl.__init__(self, master=master, width=width,
+                             height=height, double=True, depth=0)
+        self.width, self.height = width, height
 
         self.levels = {} # filename : brightness
   
         self.image = {} # filename : imgstr
         for filename in filenames:
             im = Image.open(filename)
-            if self.imgRescaleTo:
-                im.thumbnail((self.imgRescaleTo, self.imgRescaleTo))
+            if imgRescaleTo:
+                im.thumbnail((imgRescaleTo, imgRescaleTo))
             im = im.transpose(Image.FLIP_TOP_BOTTOM)
             self.imageWidth = im.size[0]
             self.imageHeight = im.size[1]
@@ -57,9 +61,35 @@
         self.bind("<Configure>", self.configure)
 
     def configure(self, ev):
-#        import pdb; pdb.set_trace()
         self.width, self.height = ev.width, ev.height
   
+    def redraw(self, event=None):
+        """you set self.levels to dict and call tkRedraw"""
+        assert 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS).split()
+        start = time.time()
+        
+        glClearColor(0.0, 0.0, 0.0, 0)
+        glClear( GL_COLOR_BUFFER_BIT |GL_ACCUM_BUFFER_BIT)
+        glEnable(GL_BLEND)
+        glBlendFunc(GL_SRC_ALPHA, GL_ONE) # add
+        
+#        l=glGenLists(1)
+#        glNewList(l,GL_COMPILE)
+#        glEndList()
+
+        # drawing to glAccum might be good
+        layerTimes = []
+        for filename, mag in self.levels.items():
+            #print "pic %s at %f" % (filename, mag)
+            t = time.time()
+            self.drawWithAlpha(self.image[filename],
+                               self.imageWidth, self.imageHeight, mag)
+            layerTimes.append(time.time() - t)
+
+        dispatcher.send("status", key="redraw",
+                        value="%.1fms" % ((time.time() - start) * 1000))
+        
+
     def drawWithAlpha(self, imgString, w, h, alpha):
         """without opengl extensions"""
         if alpha == 0:
@@ -72,32 +102,23 @@
             ar[:,3] *= alpha
 
         #print "  scl", time.time() - t
+
+        # this might be a good way to scale the color channels too,
+        # but the blend might take two steps. Anyway,
+        # GL_CONSTANT_COLOR seems not to work, so i'm not exploring
+        # this right now.
+        #glBlendFunc(GL_CONSTANT_COLOR, GL_ONE)
+        #glBlendColor(.8, .5, .5, .5)
+        
         glPixelZoom(self.width / w, self.height / h)
         glDrawPixels(w, h,
                      GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring())
         #print "  draw", time.time() - t
 
-    def redraw(self, event=None):
-        """you set self.levels to dict and call tkRedraw"""
-        assert 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS).split()
-    
-        glClearColor(0.0, 0.0, 0.0, 0)
-        glClear( GL_COLOR_BUFFER_BIT |GL_ACCUM_BUFFER_BIT)
-        glEnable(GL_BLEND)
-        glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA)
-#        l=glGenLists(1)
-#        glNewList(l,GL_COMPILE)
-#        glEndList()
-
-        # drawing to glAccum might be good
-        for filename, mag in self.levels.items():
-            #print "pic %s at %f" % (filename, mag)
-            self.drawWithAlpha(self.image[filename],
-                               self.imageWidth, self.imageHeight, mag)
-
     def newLevels(self, event=None, levels=None):
-        self.levels = levels
-        self.tkRedraw()
+        if levels != self.levels:
+            self.levels = levels
+            self.tkRedraw()
   
 def main():
     root = tk.Frame()