At the release of 1.0.1.
[python/dscho.git] / Demo / sgi / video / VGrabber.py
blob242ebf2231f112f00b35b7cf39efe5fa94dc841e
1 # Class to grab frames from a window.
2 # (This has fewer user-settable parameters than Displayer.)
3 # It is the caller's responsibility to initialize the window and to
4 # ensure that it is current when using grabframe()
6 import gl, GL
7 import VFile
8 import GET
9 from VFile import Error
11 class VGrabber(VFile.VideoParams):
13 # XXX The constructor of VideoParams is just fine, for now
15 # Grab a frame.
16 # Return (data, chromdata) just like getnextframe().
18 def grabframe(self):
19 grabber = choose_grabber(self.format)
20 return grabber(self.width, self.height, self.packfactor)
23 # Choose one of the grabber functions below based upon a color system name
25 def choose_grabber(format):
26 try:
27 return eval('grab_' + format)
28 except:
29 raise Error, 'Unknown color system: ' + `format`
32 # Routines to grab data, per color system (only a few really supported).
33 # (These functions are used via eval with a constructed argument!)
35 def grab_rgb(w, h, pf):
36 if gl.getdisplaymode() <> GET.DMRGB:
37 raise Error, 'Sorry, can only grab rgb in single-buf rgbmode'
38 if pf <> (1, 1):
39 raise Error, 'Sorry, only grab rgb with packfactor (1,1)'
40 return gl.lrectread(0, 0, w-1, h-1), None
42 def grab_rgb8(w, h, pf):
43 if gl.getdisplaymode() <> GET.DMRGB:
44 raise Error, 'Sorry, can only grab rgb8 in single-buf rgbmode'
45 if pf <> (1, 1):
46 raise Error, 'Sorry, can only grab rgb8 with packfactor (1,1)'
47 if not VFile.is_entry_indigo():
48 raise Error, 'Sorry, can only grab rgb8 on entry level Indigo'
49 # XXX Dirty Dirty here.
50 # XXX Set buffer to cmap mode, grab image and set it back.
51 gl.cmode()
52 gl.gconfig()
53 gl.pixmode(GL.PM_SIZE, 8)
54 data = gl.lrectread(0, 0, w-1, h-1)
55 data = data[:w*h] # BUG FIX for python lrectread
56 gl.RGBmode()
57 gl.gconfig()
58 gl.pixmode(GL.PM_SIZE, 32)
59 return data, None
61 def grab_grey(w, h, pf):
62 raise Error, 'Sorry, grabbing grey not implemented'
64 def grab_yiq(w, h, pf):
65 raise Error, 'Sorry, grabbing yiq not implemented'
67 def grab_hls(w, h, pf):
68 raise Error, 'Sorry, grabbing hls not implemented'
70 def grab_hsv(w, h, pf):
71 raise Error, 'Sorry, grabbing hsv not implemented'
73 def grab_jpeg(w, h, pf):
74 data, dummy = grab_rgb(w, h, pf)
75 import jpeg
76 data = jpeg.compress(data, w, h, 4)
77 return data, None
79 def grab_jpeggrey(w, h, pf):
80 raise Error, 'sorry, grabbing jpeggrey not implemented'