Class around PixMap objects that allows more python-like access. By Joe Strout.
[python/dscho.git] / Demo / sgi / video / vcopy.py
blobd32bc1f8019f22eedd63b7752e821fcbdae834ec
1 # Copy a video file, interactively, frame-by-frame.
3 import sys
4 import getopt
5 from gl import *
6 from DEVICE import *
7 import VFile
8 import string
9 import imageop
11 def report(time, iframe):
12 print 'Frame', iframe, ': t =', time
14 def usage():
15 sys.stderr.write('usage: vcopy [-t type] [-m treshold] [-a] infile outfile\n')
16 sys.stderr.write('-t Convert to other type\n')
17 sys.stderr.write('-a Automatic\n')
18 sys.stderr.write('-m Convert grey to mono with treshold\n')
19 sys.stderr.write('-d Convert grey to mono with dithering\n')
20 sys.exit(2)
22 def help():
23 print 'Command summary:'
24 print 'n get next image from input'
25 print 'w write current image to output'
27 def main():
28 foreground()
29 opts, args = getopt.getopt(sys.argv[1:], 't:am:d')
30 if len(args) <> 2:
31 usage()
32 [ifile, ofile] = args
33 print 'open film ', ifile
34 ifilm = VFile.VinFile().init(ifile)
35 print 'open output ', ofile
36 ofilm = VFile.VoutFile().init(ofile)
38 ofilm.setinfo(ifilm.getinfo())
40 use_grabber = 0
41 continuous = 0
42 tomono = 0
43 tomonodither = 0
44 for o, a in opts:
45 if o == '-t':
46 ofilm.format = a
47 use_grabber = 1
48 if o == '-a':
49 continuous = 1
50 if o == '-m':
51 if ifilm.format <> 'grey':
52 print '-m only supported for greyscale'
53 sys.exit(1)
54 tomono = 1
55 treshold = string.atoi(a)
56 ofilm.format = 'mono'
57 if o == '-d':
58 if ifilm.format <> 'grey':
59 print '-m only supported for greyscale'
60 sys.exit(1)
61 tomonodither = 1
62 ofilm.format = 'mono'
64 ofilm.writeheader()
66 prefsize(ifilm.width, ifilm.height)
67 w = winopen(ifile)
68 qdevice(KEYBD)
69 qdevice(ESCKEY)
70 qdevice(WINQUIT)
71 qdevice(WINSHUT)
72 print 'qdevice calls done'
74 help()
76 time, data, cdata = ifilm.getnextframe()
77 ifilm.showframe(data, cdata)
78 iframe = 1
79 report(time, iframe)
81 while 1:
82 if continuous:
83 dev = KEYBD
84 else:
85 dev, val = qread()
86 if dev in (ESCKEY, WINQUIT, WINSHUT):
87 break
88 if dev == REDRAW:
89 reshapeviewport()
90 elif dev == KEYBD:
91 if continuous:
92 c = '0'
93 else:
94 c = chr(val)
95 #XXX Debug
96 if c == 'R':
97 c3i(255,0,0)
98 clear()
99 if c == 'G':
100 c3i(0,255,0)
101 clear()
102 if c == 'B':
103 c3i(0,0,255)
104 clear()
105 if c == 'w' or continuous:
106 if use_grabber:
107 data, cdata = ofilm.grabframe()
108 if tomono:
109 data = imageop.grey2mono(data, \
110 ifilm.width, ifilm.height, \
111 treshold)
112 if tomonodither:
113 data = imageop.dither2mono(data, \
114 ifilm.width, ifilm.height)
115 ofilm.writeframe(time, data, cdata)
116 print 'Frame', iframe, 'written.'
117 if c == 'n' or continuous:
118 try:
119 time,data,cdata = ifilm.getnextframe()
120 ifilm.showframe(data, cdata)
121 iframe = iframe+1
122 report(time, iframe)
123 except EOFError:
124 print 'EOF'
125 if continuous:
126 break
127 ringbell()
128 elif dev == INPUTCHANGE:
129 pass
130 else:
131 print '(dev, val) =', (dev, val)
132 ofilm.close()
134 main()