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

  1. # Live video input from display class.
  2.  
  3. import gl
  4. import GL
  5.  
  6. # The live video input class.
  7. # Only instantiate this if have_video is true!
  8.  
  9. class 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.  
  44.     # Change the size of the video being displayed.
  45.  
  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
  51.  
  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
  57.  
  58.     # Remove an instance.
  59.     # This turns off continuous capture.
  60.  
  61.     def close(self):
  62.         gl.winclose(self.wid)
  63.  
  64.     # Get the length in bytes of a video line
  65.     def linewidth(self):
  66.         return self.width*4
  67.  
  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)
  76.  
  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
  100.