blob: 72e3d5811e765310d74bc5c87db369dd4779c366 [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
Jack Jansen3b253711992-12-14 12:25:21 +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
20 self.disp = Displayer().init()
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)
35 return self
36
Guido van Rossumcfb6bb21992-09-24 16:03:56 +000037 # Call this in response to every REDRAW event for the window
38 # or if the window size has changed for other reasons.
39
40 def reshapewindow(self):
Guido van Rossumba066151992-09-22 17:23:17 +000041 oldwid = gl.winget()
42 gl.winset(self.wid)
Guido van Rossumba066151992-09-22 17:23:17 +000043 gl.reshapeviewport()
Guido van Rossumcfb6bb21992-09-24 16:03:56 +000044 w, h = gl.getsize()
45 self.disp.xorigin = (w-self.vw)/2
46 self.disp.yorigin = (h-self.vh)/2
Guido van Rossumba066151992-09-22 17:23:17 +000047 self.disp.clear()
48 gl.winset(oldwid)
49
Guido van Rossumcfb6bb21992-09-24 16:03:56 +000050 # Call this to change the size of the video images being displayed.
51 # Implies reshapewindow().
52
53 def resizevideo(self, vw, vh):
54 self.vw, self.vh = vw, vh
55 self.disp.setsize(vw, vh)
56 self.reshapewindow()
57
Jack Jansen2c490171993-01-27 11:38:03 +000058 # Return the number of bytes in one video line
59 def linewidth(self):
Jack Jansen1d6821f1993-02-17 15:54:32 +000060 if self.disp.format == 'rgb':
61 return self.vw*4
Jack Jansen2c490171993-01-27 11:38:03 +000062 if self.disp.format == 'mono':
63 return (self.vw+7)/8
64 elif self.disp.format == 'grey2':
65 return (self.vw+3)/4
66 elif self.disp.format == 'grey4':
67 return (self.vw+1)/2
68 else:
69 return self.vw
70
Guido van Rossumcfb6bb21992-09-24 16:03:56 +000071 # Call this to display the next video packet. Arguments:
72 # pos: line number where the packet begins
73 # data: image data of the packet
74 # (these correspond directly to the return values from
75 # LiveVideoIn.getnextpacket()).
76
Guido van Rossumba066151992-09-22 17:23:17 +000077 def putnextpacket(self, pos, data):
Jack Jansen2c490171993-01-27 11:38:03 +000078 nline = len(data)/self.linewidth()
79 if nline*self.linewidth() <> len(data):
80 print 'Incorrect-sized video fragment ignored'
81 return
Guido van Rossumba066151992-09-22 17:23:17 +000082 oldwid = gl.winget()
83 gl.winset(self.wid)
Guido van Rossumba066151992-09-22 17:23:17 +000084 self.disp.showpartframe(data, None, (0, pos, self.vw, nline))
85 gl.winset(oldwid)
86
Guido van Rossumcfb6bb21992-09-24 16:03:56 +000087 # Call this to close the window.
88
Guido van Rossumba066151992-09-22 17:23:17 +000089 def close(self):
Guido van Rossum7b47c791992-09-24 15:01:37 +000090 pass
Jack Jansen6bc8c7f1992-12-23 15:37:20 +000091
92 # Call this to set optional mirroring of video
93 def setmirror(self, mirrored):
94 f, w, h, pf, c0, c1, c2, o, cp = self.disp.getinfo()
95 if type(pf) == type(()):
96 xpf, ypf = pf
97 else:
98 xpf = ypf = pf
99 xpf = abs(xpf)
100 if mirrored:
101 xpf = -xpf
102 info = (f, w, h, (xpf, ypf), c0, c1, c2, o, cp)
Jack Jansen2c490171993-01-27 11:38:03 +0000103 self.disp.setinfo(info)
104 self.disp.initcolormap()
105 self.disp.clear()
106
107#
108# This derived class has one difference with the base class: the video is
109# not displayed until an entire image has been gotten
110#
111class LiveVideoOutSlow(LiveVideoOut):
112
113 # Reshapewindow - Realloc buffer.
114 # (is also called by init() indirectly)
115
116 def reshapewindow(self):
117 LiveVideoOut.reshapewindow(self)
118 self.buffer = '\0'*self.linewidth()*self.vh
119 self.isbuffered = []
120
121 # putnextpacket - buffer incoming data until a complete
122 # image has been received
123
124 def putnextpacket(self, pos, data):
125 if pos in self.isbuffered or pos == 0:
126 LiveVideoOut.putnextpacket(self, 0, self.buffer)
127 self.isbuffered = []
128 self.isbuffered.append(pos)
129 bpos = pos * self.linewidth()
130 epos = bpos + len(data)
131 self.buffer = self.buffer[:bpos] + data + self.buffer[epos:]