comparison lightsim/openglsim.py @ 466:f69ba5ac17c5

qt version of lightsim now works
author drewp@bigasterisk.com
date Mon, 01 Sep 2008 05:36:24 +0000
parents 60b49f6c2027
children 61164aaadbaa
comparison
equal deleted inserted replaced
465:62bea321edbf 466:f69ba5ac17c5
6 import numarray as num 6 import numarray as num
7 import Tkinter as tk 7 import Tkinter as tk
8 import Image 8 import Image
9 from louie import dispatcher 9 from louie import dispatcher
10 10
11 from PyQt4 import QtCore, QtOpenGL 11 from PyQt4.QtCore import QSize
12 from PyQt4.QtOpenGL import QGLWidget
12 from OpenGL.GL import * 13 from OpenGL.GL import *
13 14
14 def xxxdrawWithAlpha(imgString, w, h, alpha): 15 def xxxdrawWithAlpha(imgString, w, h, alpha):
15 # this one should be faster because GL does the alpha adjust, but 16 # this one should be faster because GL does the alpha adjust, but
16 # i don't think it works yet 17 # i don't think it works yet
19 20
20 glClear(GL_COLOR_BUFFER_BIT) 21 glClear(GL_COLOR_BUFFER_BIT)
21 #glBlendColor(1, 1, 1, mag) # needs ARB_imaging 22 #glBlendColor(1, 1, 1, mag) # needs ARB_imaging
22 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, imgString) 23 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, imgString)
23 24
24 class Surface(QtOpenGL.QGLWidget): 25 class Surface(QGLWidget):
25 """widget that adds multiple image files together with adjustable scales""" 26 """widget that adds multiple image files together with adjustable scales"""
26 def __init__(self, parent, filenames, width=512, height=270, 27 def __init__(self, parent, filenames, imgRescaleTo=None):
27 imgRescaleTo=None):
28 """ 28 """
29 imgRescaleTo can be a length of pixels to reduce all the input 29 imgRescaleTo can be a length of pixels to reduce all the input
30 images into. Try 64 for a low res drawing. 30 images into. Try 64 for a low res drawing.
31 """ 31 """
32 QtOpenGL.QGLWidget.__init__(self, parent) 32 QGLWidget.__init__(self, parent)
33 33
34 self.levels = {} # filename : brightness 34 self.levels = {} # filename : brightness
35 35
36 self.image = {} # filename : imgstr 36 self.image = {} # filename : imgstr
37 for filename in filenames: 37 for filename in filenames:
38 print "open", filename 38 print "open", filename
39 im = Image.open(filename) 39 im = Image.open(filename)
40 self.origImageSize = im.size[0], im.size[1]
40 if imgRescaleTo: 41 if imgRescaleTo:
41 im.thumbnail((imgRescaleTo, imgRescaleTo)) 42 im.thumbnail((imgRescaleTo, imgRescaleTo))
42 im = im.transpose(Image.FLIP_TOP_BOTTOM) 43 im = im.transpose(Image.FLIP_TOP_BOTTOM)
43 self.imageWidth = im.size[0] 44 self.imageWidth = im.size[0]
44 self.imageHeight = im.size[1] 45 self.imageHeight = im.size[1]
45 self.image[filename] = im.convert("RGBA").tostring() 46 self.image[filename] = im.convert("RGBA").tostring()
46 47
47 #self.set_centerpoint(0, 0, 0)
48
49 def initializeGL(self): 48 def initializeGL(self):
50 glDisable(GL_CULL_FACE) 49 glDisable(GL_CULL_FACE)
51 glShadeModel(GL_FLAT) 50 glShadeModel(GL_FLAT)
52 print 'GL_ARB_imaging', 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS) 51 print 'GL_ARB_imaging', 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS)
53 import OpenGL 52 import OpenGL
54 print OpenGL.__version__ 53 print OpenGL.__version__
55 54
56 # def minimumSizeHint(self): 55 def sizeHint(self):
57 # return QtCore.QSize(512, 512) 56 return QSize(*self.origImageSize)
58
59 # def sizeHint(self):
60 # return QtCore.QSize(512, 512)
61 57
62 def paintGL(self): 58 def paintGL(self):
63 """you set self.levels to dict and call tkRedraw""" 59 """you set self.levels to dict and call tkRedraw"""
64 assert 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS).split() 60 assert 'GL_ARB_imaging' in glGetString(GL_EXTENSIONS).split()
65 start = time.time() 61 start = time.time()
73 # glNewList(l,GL_COMPILE) 69 # glNewList(l,GL_COMPILE)
74 # glEndList() 70 # glEndList()
75 71
76 # drawing to glAccum might be good 72 # drawing to glAccum might be good
77 layerTimes = [] 73 layerTimes = []
74 layers = 0
78 for filename, mag in self.levels.items(): 75 for filename, mag in self.levels.items():
79 t = time.time() 76 t = time.time()
80 self.drawWithAlpha(self.image[filename], 77 layers += self.drawWithAlpha(self.image[filename],
81 self.imageWidth, self.imageHeight, mag) 78 self.imageWidth, self.imageHeight, mag)
82 layerTimes.append(time.time() - t) 79 layerTimes.append(time.time() - t)
80
81 dispatcher.send("status", key="visibleLayers", value=str(layers))
83 82
84 dispatcher.send("status", key="redraw", 83 dispatcher.send("status", key="redraw",
85 value="%.1fms" % ((time.time() - start) * 1000)) 84 value="%.1fms" % ((time.time() - start) * 1000))
86 85
87 86
88 def drawWithAlpha(self, imgString, w, h, alpha): 87 def drawWithAlpha(self, imgString, w, h, alpha):
89 """without opengl extensions""" 88 """without opengl extensions. Returns number of layers drawn"""
90 if alpha == 0: 89 if alpha == 0:
91 return 90 return 0
92 t = time.time() 91 t = time.time()
93 ar = num.reshape(num.fromstring(imgString, dtype='uint8'), 92 ar = num.reshape(num.fromstring(imgString, dtype='uint8'),
94 (w * h, 4)) 93 (w * h, 4))
95 #print " tonum", time.time() - t 94 #print " tonum", time.time() - t
96 if alpha != 1: 95 if alpha != 1:
107 106
108 glPixelZoom(self.width() / w, self.height() / h) 107 glPixelZoom(self.width() / w, self.height() / h)
109 glDrawPixels(w, h, 108 glDrawPixels(w, h,
110 GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring()) 109 GL_RGBA, GL_UNSIGNED_BYTE, ar.tostring())
111 #print " draw", time.time() - t 110 #print " draw", time.time() - t
111 return 1
112 112
113 def newLevels(self, event=None, levels=None): 113 def newLevels(self, event=None, levels=None):
114 if levels != self.levels: 114 if levels != self.levels:
115 self.levels = levels 115 self.levels = levels
116 self.updateGL() 116 self.updateGL()