cpython/Demo/sgi/video/vcopy.py

109 lines
2.0 KiB
Python
Raw Normal View History

1991-11-04 11:54:22 -04:00
# Copy a video file, interactively, frame-by-frame.
import sys
import getopt
from gl import *
from DEVICE import *
def loadframe(f, w, h, pf):
line = f.readline()
1992-02-11 10:50:22 -04:00
if not line or line == '\n':
1991-11-04 11:54:22 -04:00
raise EOFError
x = eval(line[:-1])
1992-02-11 10:50:22 -04:00
if type(x) == type(0):
1991-11-04 11:54:22 -04:00
if pf: size = w*h*4
else: size = w*h*pf
else:
time, size = x
data = f.read(size)
if len(data) <> size:
raise EOFError
if pf:
windata = unpackrect(w/pf, h/pf, 1, data)
rectzoom(pf, pf)
lrectwrite(0, 0, w/pf-1, h/pf-1, windata)
else:
lrectwrite(0, 0, w-1, h-1, data)
return time, data
def report(time, iframe):
print 'Frame', iframe, ': t =', time
def usage():
sys.write('usage: vcopy infile outfile\n')
sys.exit(2)
def help():
print 'Command summary:'
print 'n get next image from input'
print 'w write current image to output'
def main():
opts, args = getopt.getopt(sys.argv[1:], '')
if len(args) <> 2:
usage()
[ifile, ofile] = args
ifp = open(ifile, 'r')
ofp = open(ofile, 'w')
#
line = ifp.readline()
1992-02-11 10:50:22 -04:00
if line[:4] == 'CMIF':
1991-11-04 11:54:22 -04:00
line = ifp.readline()
x = eval(line[:-1])
1992-02-11 10:50:22 -04:00
if len(x) == 3:
1991-11-04 11:54:22 -04:00
w, h, pf = x
else:
w, h = x
pf = 2
if pf:
w = (w/pf)*pf
h = (h/pf)*pf
#
ofp.write('CMIF video 1.0\n')
ofp.write(`w, h, pf` + '\n')
#
foreground()
prefsize(w, h)
wid = winopen(ifile + ' -> ' + ofile)
RGBmode()
gconfig()
qdevice(KEYBD)
qdevice(ESCKEY)
qdevice(WINQUIT)
qdevice(WINSHUT)
#
help()
#
time, data = loadframe(ifp, w, h, pf)
iframe = 1
report(time, iframe)
#
while 1:
dev, val = qread()
if dev in (ESCKEY, WINQUIT, WINSHUT):
break
1992-02-11 10:50:22 -04:00
if dev == REDRAW:
1991-11-04 11:54:22 -04:00
reshapeviewport()
1992-02-11 10:50:22 -04:00
elif dev == KEYBD:
1991-11-04 11:54:22 -04:00
c = chr(val)
1992-02-11 10:50:22 -04:00
if c == 'n':
1991-11-04 11:54:22 -04:00
try:
time, data = loadframe(ifp, w, h, pf)
iframe = iframe+1
report(time, iframe)
except EOFError:
print 'EOF'
ringbell()
1992-02-11 10:50:22 -04:00
elif c == 'w':
1991-11-04 11:54:22 -04:00
ofp.write(`time, len(data)` + '\n')
ofp.write(data)
print 'Frame', iframe, 'written.'
else:
print 'Character', `c`, 'ignored'
1992-02-11 10:50:22 -04:00
elif dev == INPUTCHANGE:
1991-11-04 11:54:22 -04:00
pass
else:
print '(dev, val) =', (dev, val)
main()