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