Use py_resource module
[python/dscho.git] / Demo / sgi / video / DisplayVideoIn.py
blob6dbc7bcc384dc04c521fe41df8fc0bf4662e4055
1 # Live video input from display class.
3 import gl
4 import GL
6 # The live video input class.
7 # Only instantiate this if have_video is true!
9 class DisplayVideoIn:
11 # Initialize an instance. Arguments:
12 # vw, vh: size of the video window data to be captured.
13 # position defaults to 0, 0 but can be set later
14 def __init__(self, pktmax, vw, vh, type):
15 self.pktmax = pktmax
16 self.realwidth, self.realheight = vw, vh
17 if type <> 'rgb':
18 raise 'Incorrent video data type', type
19 self.type = type
20 self.width = vw
21 self.height = vh
23 # Open dummy window
25 gl.foreground()
26 gl.noport()
27 self.wid = gl.winopen('DisplayVideoIn')
29 self.x0 = 0
30 self.x1 = self.x0 + self.width - 1
31 self.y0 = 0
32 self.y1 = self.y0 + self.height - 1
33 # Compute # full lines per packet
34 self.lpp = pktmax / self.linewidth()
35 if self.lpp <= 0:
36 raise 'No lines in packet', self.linewidth()
37 self.pktsize = self.lpp*self.linewidth()
38 self.data = None
39 self.old_data = None
40 self.dataoffset = 0
41 self.lpos = 0
42 self.hints = 0
44 # Change the size of the video being displayed.
46 def resizevideo(self, vw, vh):
47 self.width = vw
48 self.height = vh
49 self.x1 = self.x0 + self.width - 1
50 self.y1 = self.y0 + self.height - 1
52 def positionvideo(self, x, y):
53 self.x0 = x
54 self.y0 = y
55 self.x1 = self.x0 + self.width - 1
56 self.y1 = self.y0 + self.height - 1
58 # Remove an instance.
59 # This turns off continuous capture.
61 def close(self):
62 gl.winclose(self.wid)
64 # Get the length in bytes of a video line
65 def linewidth(self):
66 return self.width*4
68 # Get the next video packet.
69 # This returns (lpos, data) where:
70 # - lpos is the line position
71 # - data is a piece of data
72 # The dimensions of data are:
73 # - pixel depth = 1 byte
74 # - scan line width = self.width (the vw argument to __init__())
75 # - number of scan lines = self.lpp (PKTMAX / vw)
77 def getnextpacket(self):
78 if not self.data or self.dataoffset >= len(self.data):
79 self.old_data = self.data
80 self.data = gl.readdisplay(self.x0, self.y0, \
81 self.x1, self.y1, self.hints)
82 self.dataoffset = 0
83 self.lpos = 0
84 data = self.data[self.dataoffset:self.dataoffset+self.pktsize]
85 while self.old_data and \
86 self.dataoffset+self.pktsize < len(self.data):
87 odata = self.old_data[self.dataoffset: \
88 self.dataoffset+self.pktsize]
89 if odata <> data:
90 break
91 print 'skip', self.lpos
92 self.lpos = self.lpos + self.lpp
93 self.dataoffset = self.dataoffset + self.pktsize
94 data = self.data[self.dataoffset:\
95 self.dataoffset+self.pktsize]
96 lpos = self.lpos
97 self.dataoffset = self.dataoffset + self.pktsize
98 self.lpos = self.lpos + self.lpp
99 return lpos, data