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

  1. # Live video input class.
  2. # Note that importing this module attempts to initialize video.
  3.  
  4.  
  5. # Check if video is available.
  6. # There are three reasons for failure here:
  7. # (1) this version of Python may not have the sv or imageop modules;
  8. # (2) this machine may not have a video board;
  9. # (3) initializing the video board may fail for another reason.
  10. # The global variable have_video is set to true iff we reall do have video.
  11.  
  12. try:
  13.     import sv
  14.     import SV
  15.     import imageop
  16.     try:
  17.         v = sv.OpenVideo()
  18.         have_video = 1
  19.     except sv.error:
  20.         have_video = 0
  21. except ImportError:
  22.     have_video = 0
  23.  
  24.  
  25. # The live video input class.
  26. # Only instantiate this if have_video is true!
  27.  
  28. class LiveVideoIn:
  29.  
  30.     # Initialize an instance.  Arguments:
  31.     # vw, vh: size of the video window data to be captured.
  32.     # For some reason, vw MUST be a multiple of 4.
  33.     # Note that the data has to be cropped unless vw and vh are
  34.     # just right for the video board (vw:vh == 4:3 and vh even).
  35.  
  36.     def __init__(self, pktmax, vw, vh, type):
  37.         if not have_video:
  38.             raise RuntimeError, 'no video available'
  39.         if vw % 4 != 0:
  40.             raise ValueError, 'vw must be a multiple of 4'
  41.         self.pktmax = pktmax
  42.         realvw = vh*SV.PAL_XMAX/SV.PAL_YMAX
  43.         if realvw < vw:
  44.             realvw = vw
  45.         self.realwidth, self.realheight = v.QuerySize(realvw, vh)
  46.         if not type in ('rgb8', 'grey', 'mono', 'grey2', 'grey4'):
  47.             raise 'Incorrent video data type', type
  48.         self.type = type
  49.         if type in ('grey', 'grey4', 'grey2', 'mono'):
  50.             v.SetParam([SV.COLOR, SV.MONO, SV.INPUT_BYPASS, 1])
  51.         else:
  52.             v.SetParam([SV.COLOR, SV.DEFAULT_COLOR, \
  53.                   SV.INPUT_BYPASS, 0])
  54.         # Initialize capture
  55.         (mode, self.realwidth, self.realheight, qsize, rate) = \
  56.             v.InitContinuousCapture(SV.RGB8_FRAMES, \
  57.                 self.realwidth, self.realheight, 1, 2)
  58.         self.width = vw
  59.         self.height = vh
  60.         self.x0 = (self.realwidth-self.width)/2
  61.         self.x1 = self.x0 + self.width - 1
  62.         self.y0 = (self.realheight-self.height)/2
  63.         self.y1 = self.y0 + self.height - 1
  64.         # Compute # full lines per packet
  65.         self.lpp = pktmax / self.linewidth()
  66.         self.pktsize = self.lpp*self.linewidth()
  67.         self.data = None
  68.         self.dataoffset = 0
  69.         self.lpos = 0
  70.         self.justright = (self.realwidth == self.width and \
  71.             self.realheight == self.height)
  72. ##        if not self.justright:
  73. ##            print 'Want:', self.width, 'x', self.height,
  74. ##            print '; grab:', self.realwidth, 'x', self.realheight
  75.  
  76.     # Change the size of the video being displayed.
  77.  
  78.     def resizevideo(self, vw, vh):
  79.         self.close()
  80.         self.__init__(self.pktmax, vw, vh, self.type)
  81.  
  82.     # Remove an instance.
  83.     # This turns off continuous capture.
  84.  
  85.     def close(self):
  86.         v.EndContinuousCapture()
  87.  
  88.     # Get the length in bytes of a video line
  89.     def linewidth(self):
  90.         if self.type == 'mono':
  91.             return (self.width+7)/8
  92.         elif self.type == 'grey2':
  93.             return (self.width+3)/4
  94.         elif self.type == 'grey4':
  95.             return (self.width+1)/2
  96.         else:
  97.             return self.width
  98.  
  99.     # Get the next video packet.
  100.     # This returns (lpos, data) where:
  101.     # - lpos is the line position
  102.     # - data is a piece of data
  103.     # The dimensions of data are:
  104.     # - pixel depth = 1 byte
  105.     # - scan line width = self.width (the vw argument to __init__())
  106.     # - number of scan lines = self.lpp (PKTMAX / vw)
  107.  
  108.     def getnextpacket(self):
  109.         if not self.data or self.dataoffset >= len(self.data):
  110.             try:
  111.                 cd, id = v.GetCaptureData()
  112.             except sv.error:
  113.                 return None
  114.             data = cd.InterleaveFields(1)
  115.             cd.UnlockCaptureData()
  116.             if self.justright:
  117.                 self.data = data
  118.             else:
  119.                 self.data = imageop.crop(data, 1, \
  120.                       self.realwidth, \
  121.                       self.realheight, \
  122.                       self.x0, self.y0, \
  123.                       self.x1, self.y1)
  124.             self.lpos = 0
  125.             self.dataoffset = 0
  126.             if self.type == 'mono':
  127.                 self.data = imageop.dither2mono(self.data, \
  128.                       self.width, self.height)
  129.             elif self.type == 'grey2':
  130.                 self.data = imageop.dither2grey2(self.data, \
  131.                       self.width, self.height)
  132.             elif self.type == 'grey4':
  133.                 self.data = imageop.grey2grey4(self.data, \
  134.                       self.width, self.height)
  135.         data = self.data[self.dataoffset:self.dataoffset+self.pktsize]
  136.         lpos = self.lpos
  137.         self.dataoffset = self.dataoffset + self.pktsize
  138.         self.lpos = self.lpos + self.lpp
  139.         return lpos, data
  140.