-
Notifications
You must be signed in to change notification settings - Fork 11
/
vlight.py
executable file
·222 lines (168 loc) · 5.53 KB
/
vlight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python
# Copyright (C) 2013, Gabriele Facciolo <gfacciol@gmail.com>
## {{{ http://code.activestate.com/recipes/325391/ (r1)
import sys
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
from PIL import Image
name = 'viewer (press spacebar/backspace to navigate)'
# global variables for the mouse
x0=0; y0=0; w0=0; h0=0; b0state='';
# global variables for the image size
ix,iy=100,100
# and for the relative displacement
dx,dy=0,0
imageData=0
I1='' # preview
def loadImage(imageName):
im = Image.open(imageName)
try:
ix, iy, image = im.size[0], im.size[1], im.convert("RGBA").tostring("raw", "RGBA", 0, -1)
except SystemError:
ix, iy, image = im.size[0], im.size[1], im.convert("RGBA").tostring("raw", "RGBX", 0, -1)
return (image,ix,iy)
def display( ):
"""Render scene geometry"""
glClear(GL_COLOR_BUFFER_BIT);
# glDisable( GL_LIGHTING) # context lights by default
# this is the effective size of the current window
# ideally winx,winy should be equal to ix,iy BUT if the
# image is larger than the screen glutReshapeWindow(ix,iy)
# will fail and winx,winy will be truncated to the size of the screen
winx=glutGet(GLUT_WINDOW_WIDTH)
winy=glutGet(GLUT_WINDOW_HEIGHT)
# setup camera
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, winx, winy, 0, -1, 1);
glEnable (GL_TEXTURE_2D); #/* enable texture mapping */
glBindTexture (GL_TEXTURE_2D, 13); #/* bind to our texture, has id of 13 */
global dx,dy,ix,iy
glBegin( GL_QUADS );
glColor3f(1.0, 0.0, 0.0);
glTexCoord2d(0.0,0.0); glVertex3d(0 -dx,iy-dy,0);
glTexCoord2d(1.0,0.0); glVertex3d(ix-dx,iy-dy,0);
glTexCoord2d(1.0,1.0); glVertex3d(ix-dx,0 -dy,0);
glTexCoord2d(0.0,1.0); glVertex3d(0 -dx,0 -dy,0);
glEnd();
glDisable (GL_TEXTURE_2D); #/* disable texture mapping */
# show region
global x0,y0,w0,h0,b0state
if(b0state=='pressed'):
glEnable (GL_BLEND)
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin( GL_QUADS );
glColor4f(1.0, 0.0, 0.0,.6);
glVertex3d(x0 ,y0 ,-0.1);
glColor4f(0.0, 1.0, 1.0,.3);
glVertex3d(x0+w0,y0 ,-0.1);
glColor4f(0.0, 0.0, 1.0,.6);
glVertex3d(x0+w0,y0+h0,-0.1);
glColor4f(0.0, 1.0, 0.0,.3);
glVertex3d(x0 ,y0+h0,-0.1);
glEnd();
glDisable (GL_BLEND)
glFlush()
glutSwapBuffers()
def setupTexture(imageData, ix,iy):
"""texture environment setup"""
glBindTexture(GL_TEXTURE_2D, 13)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
# glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
# glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0,
GL_RGBA, GL_UNSIGNED_BYTE, imageData
)
def mouseMotionPass(x,y):
global dx,dy
title='p:%s,%s' % (x+dx,y+dy)
glutSetWindowTitle(title)
def mouseMotion(x,y):
global x0,y0,w0,h0,b0state
global dx,dy
w0,h0 = x-x0,y-y0
title='p:%s,%s [+%s+%s %sx%s]' % (x+dx,y+dy,x0+dx,y0+dy,w0,h0)
glutSetWindowTitle(title)
glutPostRedisplay()
def mouseButtons(button, state, x,y):
global x0,y0,w0,h0,b0state
if button==GLUT_LEFT_BUTTON and state==GLUT_DOWN:
x0,y0=x,y
b0state='pressed'
elif button==GLUT_LEFT_BUTTON and state==GLUT_UP:
w0,h0 = x-x0,y-y0
b0state='released'
print x0+dx, y0+dy, x+dx, y+dy
# letters and numbers
def keyboard(key, x, y):
if key=='q':
exit(0)
# print ord(key)
# handle spece/backspace
global I1, cidx
if key==' ': # space
cidx = (cidx+1) % len(I1)
if ord(key)==127: # backspace
cidx = (cidx-1) % len(I1)
# read the image
global ix,iy,imageData
(imageData,ix,iy) = loadImage(I1[cidx])
glutReshapeWindow(ix,iy)
# setup texture
setupTexture(imageData,ix,iy)
glutPostRedisplay()
# handle arrow keys
def keyboard2(key, x, y):
global dx,dy
winx=glutGet(GLUT_WINDOW_WIDTH)
winy=glutGet(GLUT_WINDOW_HEIGHT)
if key==102:
dx=dx+int(winx/16)
elif key==101:
dy=dy-int(winy/16)
elif key==100:
dx=dx-int(winx/16)
elif key==103:
dy=dy+int(winy/16)
# print key
glutPostRedisplay()
def main():
global I1, cidx
# verify input
if len(sys.argv) > 1:
I1 = sys.argv[1:]
cidx=0
else:
print "Incorrect syntax, use:"
print " > %s img1 img2 ..." % sys.argv[0]
sys.exit(1)
# globals
global ix,iy,imageData
#init opengl
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH)
glutInitWindowSize(ix,iy)
glutCreateWindow(name)
glutDisplayFunc(display)
# glutReshapeFunc (glutResize);
# glutIdleFunc(display)
glutMouseFunc(mouseButtons)
glutKeyboardFunc(keyboard)
glutSpecialFunc(keyboard2)
glutPassiveMotionFunc(mouseMotionPass);
glutMotionFunc(mouseMotion);
# read the image
(imageData,ix,iy) = loadImage(I1[cidx])
glutReshapeWindow(ix,iy)
# setup texture
setupTexture(imageData,ix,iy)
glutMainLoop()
if __name__ == '__main__': main()
## end of http://code.activestate.com/recipes/325391/ }}}