summaryrefslogtreecommitdiffstats
path: root/Demo/sgi/video/video2rgb.py
blob: bbfa6b823851d3b9c6fc744a61882c065c74843c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#! /usr/bin/env python

# Convert CMIF movie file(s) to a sequence of rgb images


# Help function

def help():
	print 'Usage: video2rgb [options] [file] ...'
	print
	print 'Options:'
	print '-q         : quiet, no informative messages'
	print '-m         : create monochrome (greyscale) image files'
	print '-f prefix  : create image files with names "prefix0000.rgb"'
	print 'file ...   : file(s) to convert; default film.video'


# Imported modules

import sys
sys.path.append('/ufs/jack/src/av/video') # Increase chance of finding VFile
import VFile
import time
import getopt
import string
import imgfile
import imgconv


# Global options

quiet = 0
prefix = 'film'
seqno = 0
mono = 0


# Main program -- mostly command line parsing

def main():
	global quiet, prefix, mono

	# Parse command line
	try:
		opts, args = getopt.getopt(sys.argv[1:], 'qmf:')
	except getopt.error, msg:
		sys.stdout = sys.stderr
		print 'Error:', msg, '\n'
		help()
		sys.exit(2)

	# Interpret options
	try:
		for opt, arg in opts:
			if opt == '-q': quiet = 1
			if opt == '-f': prefix = arg
			if opt == '-m': mono = 1
	except string.atoi_error:
		sys.stdout = sys.stderr
		print 'Option', opt, 'requires integer argument'
		sys.exit(2)

	# Process all files
	if not args: args = ['film.video']
	sts = 0
	for filename in args:
		sts = (process(filename) or sts)

	# Exit with proper exit status
	sys.exit(sts)


# Process one movie file

def process(filename):
	try:
		vin = VFile.VinFile(filename)
	except IOError, msg:
		sys.stderr.write(filename + ': I/O error: ' + `msg` + '\n')
		return 1
	except VFile.Error, msg:
		sys.stderr.write(msg + '\n')
		return 1
	except EOFError:
		sys.stderr.write(filename + ': EOF in video header\n')
		return 1

	if not quiet:
		vin.printinfo()
	
	width, height = int(vin.width), int(vin.height)

	try:
		if mono:
			cf = imgconv.getconverter(vin.format, 'grey')
		else:
			cf = imgconv.getconverter(vin.format, 'rgb')
	except imgconv.error:
		print 'Sorry, no converter available for type',vin.format
		return

	if mono:
		depth = 1
		bpp = 1
	else:
		depth = 3
		bpp = 4

	convert(vin, cf, width, height, depth, bpp, vin.packfactor)

def convert(vin, cf, width, height, depth, bpp, pf):
	global seqno

	if type(pf) == type(()):
		xpf, ypf = pf
	elif pf == 0:
		xpf = ypf = 1
	else:
		xpf = ypf = pf
	while 1:
		try:
			time, data, cdata = vin.getnextframe()
		except EOFError:
			return
		if cdata:
			print 'Film contains chromdata!'
			return
		data = cf(data, width/xpf, height/abs(ypf))
		if pf:
			data = applypackfactor(data, width, height, pf, bpp)
		s = `seqno`
		s = '0'*(4-len(s)) + s
		fname = prefix + s + '.rgb'
		seqno = seqno + 1
		if not quiet:
			print 'Writing',fname,'...'
		imgfile.write(fname, data, width, height, depth)
	
def applypackfactor(image, w, h, pf, bpp):
	import imageop
	if type(pf) == type(()):
		xpf, ypf = pf
	elif pf == 0:
		xpf = ypf = 1
	else:
		xpf = ypf = pf
	w1 = w/xpf
	h1 = h/abs(ypf)
	if ypf < 0:
		ypf = -ypf
		image = imageop.crop(image, bpp, w1, h1, 0, h1-1, w1-1, 0)
	return imageop.scale(image, bpp, w1, h1, w, h)
	
# Don't forget to call the main program

try:
	main()
except KeyboardInterrupt:
	print '[Interrupt]'