home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Demo / sgi / video / LiveVideoOut.py < prev    next >
Text File  |  1993-12-17  |  4KB  |  131 lines

  1. # Live video output (display video on the screen, presumably from the net)
  2.  
  3. import gl
  4. from VFile import Displayer
  5.  
  6.  
  7. # Video output (displayer) class.
  8.  
  9. class LiveVideoOut:
  10.  
  11.     # 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.  
  15.     def __init__(self, wid, vw, vh, type):
  16.         ##print 'Init', wid, xywh
  17.         ##print 'video', vw, vw
  18.         self.vw = vw
  19.         self.vh = vh
  20.         self.disp = Displayer()
  21.         if not type in ('rgb', 'rgb8', 'grey', 'mono', 'grey2', \
  22.               'grey4'):
  23.             raise 'Incorrent live video output type', type
  24.         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)
  28.         self.disp.setinfo(info)
  29.         self.wid = wid
  30.         oldwid = gl.winget()
  31.         gl.winset(wid)
  32.         self.disp.initcolormap()
  33.         self.reshapewindow()
  34.         gl.winset(oldwid)
  35.  
  36.     # 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):
  40.         oldwid = gl.winget()
  41.         gl.winset(self.wid)
  42.         gl.reshapeviewport()
  43.         w, h = gl.getsize()
  44.         self.disp.xorigin = (w-self.vw)/2
  45.         self.disp.yorigin = (h-self.vh)/2
  46.         self.disp.clear()
  47.         gl.winset(oldwid)
  48.  
  49.     # 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.  
  57.     # Return the number of bytes in one video line
  58.     def linewidth(self):
  59.         if self.disp.format == 'rgb':
  60.             return self.vw*4
  61.         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.  
  70.     # 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.  
  76.     def putnextpacket(self, pos, data):
  77.         nline = len(data)/self.linewidth()
  78.         if nline*self.linewidth() <> len(data):
  79.             print 'Incorrect-sized video fragment ignored'
  80.             return
  81.         oldwid = gl.winget()
  82.         gl.winset(self.wid)
  83.         self.disp.showpartframe(data, None, (0, pos, self.vw, nline))
  84.         gl.winset(oldwid)
  85.  
  86.     # Call this to close the window.
  87.  
  88.     def close(self):
  89.         pass
  90.  
  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)
  102.         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. #
  110. class LiveVideoOutSlow(LiveVideoOut):
  111.  
  112.     # Reshapewindow - Realloc buffer.
  113.     # (is also called by __init__() indirectly)
  114.  
  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:]
  131.