cpython/Demo/sgi/video/VFile.py

306 lines
7.6 KiB
Python
Raw Normal View History

1991-12-03 12:50:00 -04:00
# VFile -- two classes for Video Files.
# VinFile -- for video input.
# VoutFile -- for video output.
import sys
import gl
import GL
import colorsys
1991-12-03 12:50:00 -04:00
Error = 'VFile.Error' # Exception
1992-02-11 10:45:43 -04:00
MAXMAP = 4096 - 256
def conv_grey(l,x,y): return colorsys.yiq_to_rgb(l,0,0)
def conv_yiq (y,i,q): return colorsys.yiq_to_rgb(y, (i-0.5)*1.2, q-0.5)
def conv_hls (l,h,s): return colorsys.hls_to_rgb(h,l,s)
def conv_hsv (v,h,s): return colorsys.hsv_to_rgb(h,s,v)
def conv_rgb (r,g,b):
1992-02-11 10:45:43 -04:00
raise Error, 'Attempt to make RGB colormap'
1991-12-03 12:50:00 -04:00
# Class VinFile represents a video file used for input.
#
# It has the following methods:
# init(filename)
# initfp(fp, filename)
# rewind()
# getframe()
# skipframe()
#
# The following read-only data members provide public information:
# version
# filename
# width, height
# packfactor
# c0bits, c1bits, c2bits, chrompack
# offset
# format
1991-12-03 12:50:00 -04:00
#
# These writable data members provide additional parametrization:
1992-02-11 10:45:43 -04:00
# magnify
1991-12-03 12:50:00 -04:00
# xorigin, yorigin
class VinFile():
# init() and initfp() raise Error if the header is bad.
# init() raises whatever open() raises if the file can't be opened.
def init(self, filename):
1992-02-11 10:45:43 -04:00
if filename == '-':
1991-12-03 12:50:00 -04:00
return self.initfp(sys.stdin, filename)
return self.initfp(open(filename, 'r'), filename)
def initfp(self, (fp, filename)):
self.colormapinited = 0
self.magnify = 1.0
self.xorigin = self.yorigin = 0
self.fp = fp
self.filename = filename
#
line = self.fp.readline()
1992-02-11 10:45:43 -04:00
if line == 'CMIF video 1.0\n':
1991-12-03 12:50:00 -04:00
self.version = 1.0
1992-02-11 10:45:43 -04:00
elif line == 'CMIF video 2.0\n':
1991-12-03 12:50:00 -04:00
self.version = 2.0
1992-02-11 10:45:43 -04:00
elif line == 'CMIF video 3.0\n':
self.version = 3.0
1991-12-03 12:50:00 -04:00
else:
raise Error, self.filename + ': bad video format'
#
if self.version < 2.0:
self.c0bits, self.c1bits, self.c2bits = 8, 0, 0
1991-12-03 12:50:00 -04:00
self.chrompack = 0
self.offset = 0
self.format = 'grey'
1992-02-11 10:45:43 -04:00
elif self.version == 2.0:
1991-12-03 12:50:00 -04:00
line = self.fp.readline()
try:
self.c0bits, self.c1bits, self.c2bits, \
1991-12-03 12:50:00 -04:00
self.chrompack = eval(line[:-1])
if self.c1bits or self.c2bits:
1992-02-11 10:45:43 -04:00
self.format = 'yiq'
else:
1992-02-11 10:45:43 -04:00
self.format = 'grey'
self.offset = 0
except:
1992-02-11 10:45:43 -04:00
raise Error, \
self.filename + ': bad 2.0 color info'
elif self.version == 3.0:
line = self.fp.readline()
try:
1992-02-11 10:45:43 -04:00
self.format, rest = eval(line[:-1])
if self.format == 'rgb':
pass
elif self.format == 'grey':
self.offset = 0
self.c0bits = rest
self.c1bits = self.c2bits = \
self.chrompack = 0
1992-02-11 10:45:43 -04:00
else:
self.c0bits,self.c1bits,self.c2bits,\
self.chrompack,self.offset = rest
1991-12-03 12:50:00 -04:00
except:
1992-02-11 10:45:43 -04:00
raise Error, \
self.filename + ': bad 3.0 color info'
try:
1992-02-11 10:45:43 -04:00
self.convcolor = eval('conv_'+self.format)
except:
1992-02-11 10:45:43 -04:00
raise Error, \
self.filename + ': unknown colorsys ' + self.format
1991-12-03 12:50:00 -04:00
#
line = self.fp.readline()
try:
x = eval(line[:-1])
1992-02-11 10:45:43 -04:00
if self.version > 1.0 or len(x) == 3:
1991-12-03 12:50:00 -04:00
self.width, self.height, self.packfactor = x
1992-02-11 10:45:43 -04:00
if self.packfactor == 0:
self.format = 'rgb'
1991-12-03 12:50:00 -04:00
else:
sef.width, self.height = x
self.packfactor = 2
except:
raise Error, self.filename + ': bad (w,h,pf) info'
#
return self
# rewind() raises Error if the header is bad (which can only
# happen if the file was written to since opened).
def rewind(self):
self.fp.seek(0)
x = self.initfp(self.fp, self.filename)
# getnextframe() raises EOFError (built-in) if there is no next frame,
# or if the next frame is broken.
# So to getnextframeheader(), getnextframedata() and skipnextframe().
def getnextframe(self):
time, size, chromsize = self.getnextframeheader()
data, chromdata = self.getnextframedata(size, chromsize)
return time, data, chromdata
def getnextframedata(self, (size, chromsize)):
data = self.fp.read(size)
if len(data) <> size: raise EOFError
if chromsize:
chromdata = self.fp.read(chromsize)
if len(chromdata) <> chromsize: raise EOFError
else:
chromdata = None
#
return data, chromdata
def skipnextframe(self):
time, size, chromsize = self.getnextframeheader()
self.skipnextframedata(size, chromsize)
return time
def skipnextframedata(self, (size, chromsize)):
# Note that this won't raise EOFError for a partial frame.
try:
1992-02-11 10:45:43 -04:00
self.fp.seek(size + chromsize, 1) # Relative seek
1991-12-03 12:50:00 -04:00
except:
# Assume it's a pipe -- read the data to discard it
1992-02-11 10:45:43 -04:00
dummy = self.fp.read(size + chromsize)
1991-12-03 12:50:00 -04:00
def getnextframeheader(self):
line = self.fp.readline()
if not line:
raise EOFError
#
w, h, pf = self.width, self.height, self.packfactor
try:
x = eval(line[:-1])
if type(x) in (type(0), type(0.0)):
time = x
1992-02-11 10:45:43 -04:00
if pf == 0:
1991-12-03 12:50:00 -04:00
size = w * h * 4
else:
size = (w/pf) * (h/pf)
1992-02-11 10:45:43 -04:00
elif len(x) == 2:
1991-12-03 12:50:00 -04:00
time, size = x
cp = self.chrompack
if cp:
cw = (w + cp - 1) / cp
ch = (h + cp - 1) / cp
chromsize = 2 * cw * ch
else:
chromsize = 0
else:
time, size, chromsize = x
except:
raise Error, self.filename + ': bad frame header'
return time, size, chromsize
def shownextframe(self):
time, data, chromdata = self.getnextframe()
self.showframe(data, chromdata)
return time
def showframe(self, (data, chromdata)):
w, h, pf = self.width, self.height, self.packfactor
if not self.colormapinited:
self.initcolormap()
1991-12-03 12:50:00 -04:00
factor = self.magnify
if pf: factor = factor * pf
if chromdata:
cp = self.chrompack
cw = (w+cp-1)/cp
ch = (h+cp-1)/cp
gl.rectzoom(factor*cp, factor*cp)
gl.pixmode(GL.PM_SIZE, 16)
gl.writemask(self.mask - ((1 << self.c0bits) - 1))
1991-12-03 12:50:00 -04:00
gl.lrectwrite(self.xorigin, self.yorigin, \
self.xorigin + cw - 1, self.yorigin + ch - 1, \
chromdata)
#
if pf:
gl.writemask((1 << self.c0bits) - 1)
1991-12-03 12:50:00 -04:00
gl.pixmode(GL.PM_SIZE, 8)
gl.rectzoom(factor, factor)
w = w/pf
h = h/pf
gl.lrectwrite(self.xorigin, self.yorigin, \
self.xorigin + w - 1, self.yorigin + h - 1, data)
def initcolormap(self):
self.colormapinited = 1
1992-02-11 10:45:43 -04:00
if self.format == 'rgb':
gl.RGBmode()
gl.gconfig()
return
gl.cmode()
gl.gconfig()
sys.stderr.write('Initializing color map...')
initcmap(self.convcolor, self.c0bits, self.c1bits, \
self.c2bits, self.chrompack, self.offset)
sys.stderr.write(' Done.\n')
if self.offset == 0:
1992-02-11 10:45:43 -04:00
gl.color(0x800)
self.mask = 0x7ff
else:
1992-02-11 10:45:43 -04:00
self.mask = 0xfff
gl.clear()
1991-12-03 12:50:00 -04:00
def initcmap(convcolor, c0bits, c1bits, c2bits, chrompack, offset):
if c0bits+c1bits+c2bits > 11:
1991-12-03 12:50:00 -04:00
raise Error, 'Sorry, 11 bits max'
maxc0 = 1 << c0bits
maxc1 = 1 << c1bits
maxc2 = 1 << c2bits
1992-02-11 10:45:43 -04:00
if offset == 0:
offset = 2048
for i in range(offset, MAXMAP):
1991-12-03 12:50:00 -04:00
gl.mapcolor(i, 0, 255, 0)
for c0 in range(maxc0):
c0v = c0/float(maxc0-1)
for c1 in range(maxc1):
1992-02-11 10:45:43 -04:00
if maxc1 == 1:
c1v = 0
1991-12-03 12:50:00 -04:00
else:
c1v = c1/float(maxc1-1)
for c2 in range(maxc2):
1992-02-11 10:45:43 -04:00
if maxc2 == 1:
c2v = 0
1991-12-03 12:50:00 -04:00
else:
c2v = c2/float(maxc2-1)
index = offset + c0 + \
1992-02-11 10:45:43 -04:00
(c1<<c0bits) + (c2 << (c0bits+c1bits))
rv, gv, bv = convcolor(c0v, c1v, c2v)
1991-12-03 12:50:00 -04:00
r, g, b = \
1992-02-11 10:45:43 -04:00
int(rv*255.0), int(gv*255.0), int(bv*255.0)
if index < MAXMAP:
1991-12-03 12:50:00 -04:00
gl.mapcolor(index, r, g, b)
def test():
1992-02-11 10:45:43 -04:00
import sys, time
1991-12-03 12:50:00 -04:00
filename = 'film.video'
if sys.argv[1:]: filename = sys.argv[1]
vin = VinFile().init(filename)
1992-02-11 10:45:43 -04:00
print 'File: ', filename
print 'Version: ', vin.version
1992-02-11 10:45:43 -04:00
print 'Size: ', vin.width, 'x', vin.height
print 'Pack: ', vin.packfactor, '; chrom:', vin.chrompack
print 'Bits: ', vin.c0bits, vin.c1bits, vin.c2bits
print 'Format: ', vin.format
print 'Offset: ', vin.offset
1991-12-03 12:50:00 -04:00
gl.foreground()
gl.prefsize(vin.width, vin.height)
1992-02-11 10:45:43 -04:00
wid = gl.winopen(filename)
vin.initcolormap()
t0 = time.millitimer()
while 1:
try:
t, data, chromdata = vin.getnextframe()
except EOFError:
break
dt = t + t0 - time.millitimer()
if dt > 0:
time.millisleep(dt)
vin.showframe(data, chromdata)
print 'Done.'
time.sleep(2)