blob: 427d30a3c9327fcbc2378bbff0d8b08c5194e6df [file] [log] [blame]
Jack Jansenb05eaf11993-02-17 15:58:49 +00001# Live video input from display class.
2
3import gl
4import GL
5
6# The live video input class.
7# Only instantiate this if have_video is true!
8
9class DisplayVideoIn:
10
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
22 #
23 # Open dummy window
24 #
25 gl.foreground()
26 gl.noport()
27 self.wid = gl.winopen('DisplayVideoIn')
28
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
43 return self
44
45 # Change the size of the video being displayed.
46
47 def resizevideo(self, vw, vh):
48 self.width = vw
49 self.height = vh
50 self.x1 = self.x0 + self.width - 1
51 self.y1 = self.y0 + self.height - 1
52
53 def positionvideo(self, x, y):
54 self.x0 = x
55 self.y0 = y
56 self.x1 = self.x0 + self.width - 1
57 self.y1 = self.y0 + self.height - 1
58
59 # Remove an instance.
60 # This turns off continuous capture.
61
62 def close(self):
63 gl.winclose(self.wid)
64
65 # Get the length in bytes of a video line
66 def linewidth(self):
67 return self.width*4
68
69 # Get the next video packet.
70 # This returns (lpos, data) where:
71 # - lpos is the line position
72 # - data is a piece of data
73 # The dimensions of data are:
74 # - pixel depth = 1 byte
75 # - scan line width = self.width (the vw argument to init())
76 # - number of scan lines = self.lpp (PKTMAX / vw)
77
78 def getnextpacket(self):
79 if not self.data or self.dataoffset >= len(self.data):
80 self.old_data = self.data
81 self.data = gl.readdisplay(self.x0, self.y0, \
82 self.x1, self.y1, self.hints)
83 self.dataoffset = 0
84 self.lpos = 0
85 data = self.data[self.dataoffset:self.dataoffset+self.pktsize]
86 while self.old_data and \
87 self.dataoffset+self.pktsize < len(self.data):
88 odata = self.old_data[self.dataoffset: \
89 self.dataoffset+self.pktsize]
90 if odata <> data:
91 break
92 print 'skip', self.lpos
93 self.lpos = self.lpos + self.lpp
94 self.dataoffset = self.dataoffset + self.pktsize
95 data = self.data[self.dataoffset:\
96 self.dataoffset+self.pktsize]
97 lpos = self.lpos
98 self.dataoffset = self.dataoffset + self.pktsize
99 self.lpos = self.lpos + self.lpp
100 return lpos, data