blob: 6ee1846d41b53556750a8a350a1947d35c598935 [file] [log] [blame]
Guido van Rossumba066151992-09-22 17:23:17 +00001# Live video output (display video on the screen, presumably from the net)
2
3import gl
4from VFile import Displayer
5
6
7# Video output (displayer) class.
8
9class LiveVideoOut:
10
Guido van Rossumcfb6bb21992-09-24 16:03:56 +000011 # Call this to initialize things. Arguments:
12 # wid: the window id where the video is to be displayed (centered)
13 # vw, vh: size of the video image to be displayed
14
Guido van Rossum21a3ff91993-12-17 15:11:41 +000015 def __init__(self, wid, vw, vh, type):
Guido van Rossumba066151992-09-22 17:23:17 +000016 ##print 'Init', wid, xywh
17 ##print 'video', vw, vw
18 self.vw = vw
19 self.vh = vh
Guido van Rossum21a3ff91993-12-17 15:11:41 +000020 self.disp = Displayer()
Jack Jansen1d6821f1993-02-17 15:54:32 +000021 if not type in ('rgb', 'rgb8', 'grey', 'mono', 'grey2', \
22 'grey4'):
Jack Jansen6bc8c7f1992-12-23 15:37:20 +000023 raise 'Incorrent live video output type', type
Jack Jansen1d6821f1993-02-17 15:54:32 +000024 if type == 'rgb':
25 info = (type, vw, vh, 0, 32, 0, 0, 0, 0)
26 else:
27 info = (type, vw, vh, 1, 8, 0, 0, 0, 0)
Guido van Rossumba066151992-09-22 17:23:17 +000028 self.disp.setinfo(info)
29 self.wid = wid
30 oldwid = gl.winget()
31 gl.winset(wid)
32 self.disp.initcolormap()
Guido van Rossumcfb6bb21992-09-24 16:03:56 +000033 self.reshapewindow()
Guido van Rossumba066151992-09-22 17:23:17 +000034 gl.winset(oldwid)
Guido van Rossumba066151992-09-22 17:23:17 +000035
Guido van Rossumcfb6bb21992-09-24 16:03:56 +000036 # Call this in response to every REDRAW event for the window
37 # or if the window size has changed for other reasons.
38
39 def reshapewindow(self):
Guido van Rossumba066151992-09-22 17:23:17 +000040 oldwid = gl.winget()
41 gl.winset(self.wid)
Guido van Rossumba066151992-09-22 17:23:17 +000042 gl.reshapeviewport()
Guido van Rossumcfb6bb21992-09-24 16:03:56 +000043 w, h = gl.getsize()
44 self.disp.xorigin = (w-self.vw)/2
45 self.disp.yorigin = (h-self.vh)/2
Guido van Rossumba066151992-09-22 17:23:17 +000046 self.disp.clear()
47 gl.winset(oldwid)
48
Guido van Rossumcfb6bb21992-09-24 16:03:56 +000049 # Call this to change the size of the video images being displayed.
50 # Implies reshapewindow().
51
52 def resizevideo(self, vw, vh):
53 self.vw, self.vh = vw, vh
54 self.disp.setsize(vw, vh)
55 self.reshapewindow()
56
Jack Jansen2c490171993-01-27 11:38:03 +000057 # Return the number of bytes in one video line
58 def linewidth(self):
Jack Jansen1d6821f1993-02-17 15:54:32 +000059 if self.disp.format == 'rgb':
60 return self.vw*4
Jack Jansen2c490171993-01-27 11:38:03 +000061 if self.disp.format == 'mono':
62 return (self.vw+7)/8
63 elif self.disp.format == 'grey2':
64 return (self.vw+3)/4
65 elif self.disp.format == 'grey4':
66 return (self.vw+1)/2
67 else:
68 return self.vw
69
Guido van Rossumcfb6bb21992-09-24 16:03:56 +000070 # Call this to display the next video packet. Arguments:
71 # pos: line number where the packet begins
72 # data: image data of the packet
73 # (these correspond directly to the return values from
74 # LiveVideoIn.getnextpacket()).
75
Guido van Rossumba066151992-09-22 17:23:17 +000076 def putnextpacket(self, pos, data):
Jack Jansen2c490171993-01-27 11:38:03 +000077 nline = len(data)/self.linewidth()
78 if nline*self.linewidth() <> len(data):
79 print 'Incorrect-sized video fragment ignored'
80 return
Guido van Rossumba066151992-09-22 17:23:17 +000081 oldwid = gl.winget()
82 gl.winset(self.wid)
Guido van Rossumba066151992-09-22 17:23:17 +000083 self.disp.showpartframe(data, None, (0, pos, self.vw, nline))
84 gl.winset(oldwid)
85
Guido van Rossumcfb6bb21992-09-24 16:03:56 +000086 # Call this to close the window.
87
Guido van Rossumba066151992-09-22 17:23:17 +000088 def close(self):
Guido van Rossum7b47c791992-09-24 15:01:37 +000089 pass
Jack Jansen6bc8c7f1992-12-23 15:37:20 +000090
91 # Call this to set optional mirroring of video
92 def setmirror(self, mirrored):
93 f, w, h, pf, c0, c1, c2, o, cp = self.disp.getinfo()
94 if type(pf) == type(()):
95 xpf, ypf = pf
96 else:
97 xpf = ypf = pf
98 xpf = abs(xpf)
99 if mirrored:
100 xpf = -xpf
101 info = (f, w, h, (xpf, ypf), c0, c1, c2, o, cp)
Jack Jansen2c490171993-01-27 11:38:03 +0000102 self.disp.setinfo(info)
103 self.disp.initcolormap()
104 self.disp.clear()
105
106#
107# This derived class has one difference with the base class: the video is
108# not displayed until an entire image has been gotten
109#
110class LiveVideoOutSlow(LiveVideoOut):
111
112 # Reshapewindow - Realloc buffer.
Guido van Rossum21a3ff91993-12-17 15:11:41 +0000113 # (is also called by __init__() indirectly)
Jack Jansen2c490171993-01-27 11:38:03 +0000114
115 def reshapewindow(self):
116 LiveVideoOut.reshapewindow(self)
117 self.buffer = '\0'*self.linewidth()*self.vh
118 self.isbuffered = []
119
120 # putnextpacket - buffer incoming data until a complete
121 # image has been received
122
123 def putnextpacket(self, pos, data):
124 if pos in self.isbuffered or pos == 0:
125 LiveVideoOut.putnextpacket(self, 0, self.buffer)
126 self.isbuffered = []
127 self.isbuffered.append(pos)
128 bpos = pos * self.linewidth()
129 epos = bpos + len(data)
130 self.buffer = self.buffer[:bpos] + data + self.buffer[epos:]