blob: 07407a80fc4e0e704cc7f14b03f46aaac56b1781 [file] [log] [blame]
Jack Janseneeec33f1993-02-17 15:52:56 +00001#! /ufs/guido/bin/sgi/python
2
Guido van Rossum245be3a1993-02-18 18:09:18 +00003# Convert CMIF movie file(s) to a sequence of rgb images
Jack Janseneeec33f1993-02-17 15:52:56 +00004
5
6# Help function
7
8def help():
Guido van Rossum245be3a1993-02-18 18:09:18 +00009 print 'Usage: video2rgb [options] [file] ...'
Jack Janseneeec33f1993-02-17 15:52:56 +000010 print
11 print 'Options:'
12 print '-q : quiet, no informative messages'
13 print '-m : create monochrome (greyscale) image files'
Guido van Rossum245be3a1993-02-18 18:09:18 +000014 print '-f prefix : create image files with names "prefix0000.rgb"'
15 print 'file ... : file(s) to convert; default film.video'
Jack Janseneeec33f1993-02-17 15:52:56 +000016
17
18# Imported modules
19
20import sys
21sys.path.append('/ufs/guido/src/video') # Increase chance of finding VFile
22import VFile
23import time
24import getopt
25import string
26import imgfile
27import imgconv
28
29
30# Global options
31
32quiet = 0
33prefix = 'film'
34seqno = 0
35mono = 0
36
37
38# Main program -- mostly command line parsing
39
40def main():
41 global quiet, prefix, mono
42
43 # Parse command line
44 try:
45 opts, args = getopt.getopt(sys.argv[1:], 'qmf:')
46 except getopt.error, msg:
47 sys.stdout = sys.stderr
48 print 'Error:', msg, '\n'
49 help()
50 sys.exit(2)
51
52 # Interpret options
53 try:
54 for opt, arg in opts:
55 if opt == '-q': quiet = 1
56 if opt == '-f': prefix = arg
57 if opt == '-m': mono = 1
58 except string.atoi_error:
59 sys.stdout = sys.stderr
60 print 'Option', opt, 'requires integer argument'
61 sys.exit(2)
62
63 # Process all files
64 if not args: args = ['film.video']
65 sts = 0
66 for filename in args:
67 sts = (process(filename) or sts)
68
69 # Exit with proper exit status
70 sys.exit(sts)
71
72
73# Process one movie file
74
75def process(filename):
76 try:
77 vin = VFile.VinFile().init(filename)
78 except IOError, msg:
79 sys.stderr.write(filename + ': I/O error: ' + `msg` + '\n')
80 return 1
81 except VFile.Error, msg:
82 sys.stderr.write(msg + '\n')
83 return 1
84 except EOFError:
85 sys.stderr.write(filename + ': EOF in video header\n')
86 return 1
87
88 if not quiet:
89 vin.printinfo()
90
91 width, height = int(vin.width), int(vin.height)
92
93 try:
94 if mono:
95 cf = imgconv.getconverter(vin.format, 'grey')
96 else:
97 cf = imgconv.getconverter(vin.format, 'rgb')
98 except imgconv.error:
99 print 'Sorry, no converter available for type',vin.format
100 return
101
102 if mono:
103 depth = 1
104 else:
105 depth = 3
106
107 pf = testpackfactor(vin.packfactor)
108 if pf == None:
109 print 'Sorry, packfactor not supported:', vin.packfactor
110 convert(vin, cf, width, height, depth, pf)
111
112def convert(vin, cf, width, height, depth, pf):
113 global seqno
114
115 while 1:
116 try:
117 time, data, cdata = vin.getnextframe()
118 except EOFError:
119 return
120 if cdata:
121 print 'Film contains chromdata!'
122 return
123 if pf:
124 data = applypackfactor(data, width, height, pf)
125 s = `seqno`
126 s = '0'*(4-len(s)) + s
127 fname = prefix + s + '.rgb'
128 seqno = seqno + 1
129 if not quiet:
130 print 'Writing',fname,'...'
131 imgfile.write(fname, data, width, height, depth)
132
133def testpackfactor(pf):
134 if type(pf) == type(()):
135 xpf, ypf = pf
136 else:
137 if pf in (0, 1):
138 return 0
139 return None
140 if xpf <> 1:
141 return None
142 return pf
143
144def applypackfactor(image, w, h, pf):
145 if type(pf) == type(()):
146 xpf, ypf = pf
147 else:
148 xpf = ypf = 1
149 rows = []
150 for i in range(0, (w*h+ypf-1)/abs(ypf), w):
151 rows.append(image[i:i+w])
152 if ypf < 0:
153 rows.reverse()
154 ypf = -ypf
155 image = ''
156 for i in range(0, h):
157 image = image + rows[i/ypf]
158 return image
159
160# Don't forget to call the main program
161
162try:
163 main()
164except KeyboardInterrupt:
165 print '[Interrupt]'