Support for CMIF video 3.0 files (more color systems).
This commit is contained in:
parent
8f72bbb2c8
commit
444339d463
|
@ -5,9 +5,16 @@
|
||||||
import sys
|
import sys
|
||||||
import gl
|
import gl
|
||||||
import GL
|
import GL
|
||||||
|
import colorsys
|
||||||
|
|
||||||
Error = 'VFile.Error' # Exception
|
Error = 'VFile.Error' # Exception
|
||||||
|
|
||||||
|
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):
|
||||||
|
raise Error, 'Attempt to make RGB colormap'
|
||||||
|
|
||||||
# Class VinFile represents a video file used for input.
|
# Class VinFile represents a video file used for input.
|
||||||
#
|
#
|
||||||
|
@ -23,7 +30,9 @@ Error = 'VFile.Error' # Exception
|
||||||
# filename
|
# filename
|
||||||
# width, height
|
# width, height
|
||||||
# packfactor
|
# packfactor
|
||||||
# ybits, ibits, qbits, chrompack
|
# c0bits, c1bits, c2bits, chrompack
|
||||||
|
# offset
|
||||||
|
# format
|
||||||
#
|
#
|
||||||
# These writable data members provide additional parametrization:
|
# These writable data members provide additional parametrization:
|
||||||
# xorigin, yorigin
|
# xorigin, yorigin
|
||||||
|
@ -50,25 +59,57 @@ class VinFile():
|
||||||
self.version = 1.0
|
self.version = 1.0
|
||||||
elif line = 'CMIF video 2.0\n':
|
elif line = 'CMIF video 2.0\n':
|
||||||
self.version = 2.0
|
self.version = 2.0
|
||||||
|
elif line = 'CMIF video 3.0\n':
|
||||||
|
self.version = 3.0
|
||||||
else:
|
else:
|
||||||
raise Error, self.filename + ': bad video format'
|
raise Error, self.filename + ': bad video format'
|
||||||
#
|
#
|
||||||
if self.version < 2.0:
|
if self.version < 2.0:
|
||||||
self.ybits, self.ibits, self.qbits = 8, 0, 0
|
self.c0bits, self.c1bits, self.c2bits = 8, 0, 0
|
||||||
self.chrompack = 0
|
self.chrompack = 0
|
||||||
else:
|
self.offset = 0
|
||||||
|
self.format = 'grey'
|
||||||
|
elif self.version = 2.0:
|
||||||
line = self.fp.readline()
|
line = self.fp.readline()
|
||||||
try:
|
try:
|
||||||
self.ybits, self.ibits, self.qbits, \
|
self.c0bits, self.c1bits, self.c2bits, \
|
||||||
self.chrompack = eval(line[:-1])
|
self.chrompack = eval(line[:-1])
|
||||||
|
if self.c1bits or self.c2bits:
|
||||||
|
self.format = 'yiq'
|
||||||
|
else:
|
||||||
|
self.format = 'grey'
|
||||||
|
self.offset = 0
|
||||||
except:
|
except:
|
||||||
raise Error, self.filename + ': bad color info'
|
raise Error, self.filename + ': bad 2.0 color info'
|
||||||
|
elif self.version = 3.0:
|
||||||
|
line = self.fp.readline()
|
||||||
|
try:
|
||||||
|
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
|
||||||
|
else:
|
||||||
|
self.c0bits,self.c1bits,self.c2bits,\
|
||||||
|
self.chrompack,self.offset = rest
|
||||||
|
except:
|
||||||
|
raise Error, self.filename + ': bad 3.0 color info'
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.convcolor = eval('conv_'+self.format)
|
||||||
|
except:
|
||||||
|
raise Error, self.filename + ': unknown colorsys ' + self.format
|
||||||
#
|
#
|
||||||
line = self.fp.readline()
|
line = self.fp.readline()
|
||||||
try:
|
try:
|
||||||
x = eval(line[:-1])
|
x = eval(line[:-1])
|
||||||
if self.version > 1.0 or len(x) = 3:
|
if self.version > 1.0 or len(x) = 3:
|
||||||
self.width, self.height, self.packfactor = x
|
self.width, self.height, self.packfactor = x
|
||||||
|
if self.packfactor = 0:
|
||||||
|
self.format = 'rgb'
|
||||||
else:
|
else:
|
||||||
sef.width, self.height = x
|
sef.width, self.height = x
|
||||||
self.packfactor = 2
|
self.packfactor = 2
|
||||||
|
@ -112,7 +153,7 @@ class VinFile():
|
||||||
def skipnextframedata(self, (size, chromsize)):
|
def skipnextframedata(self, (size, chromsize)):
|
||||||
# Note that this won't raise EOFError for a partial frame.
|
# Note that this won't raise EOFError for a partial frame.
|
||||||
try:
|
try:
|
||||||
self.fp.seek(size + chromsize, 1) # Relative seek
|
self.fp.seek(size + chromsize, 1) # Relatc1ve seek
|
||||||
except:
|
except:
|
||||||
# Assume it's a pipe -- read the data to discard it
|
# Assume it's a pipe -- read the data to discard it
|
||||||
dummy = self.fp.read(size)
|
dummy = self.fp.read(size)
|
||||||
|
@ -154,6 +195,8 @@ class VinFile():
|
||||||
|
|
||||||
def showframe(self, (data, chromdata)):
|
def showframe(self, (data, chromdata)):
|
||||||
w, h, pf = self.width, self.height, self.packfactor
|
w, h, pf = self.width, self.height, self.packfactor
|
||||||
|
if not self.colormapinited:
|
||||||
|
self.initcolormap()
|
||||||
factor = self.magnify
|
factor = self.magnify
|
||||||
if pf: factor = factor * pf
|
if pf: factor = factor * pf
|
||||||
if chromdata:
|
if chromdata:
|
||||||
|
@ -162,15 +205,13 @@ class VinFile():
|
||||||
ch = (h+cp-1)/cp
|
ch = (h+cp-1)/cp
|
||||||
gl.rectzoom(factor*cp, factor*cp)
|
gl.rectzoom(factor*cp, factor*cp)
|
||||||
gl.pixmode(GL.PM_SIZE, 16)
|
gl.pixmode(GL.PM_SIZE, 16)
|
||||||
gl.writemask(0x7ff - ((1 << self.ybits) - 1))
|
gl.writemask(self.mask - ((1 << self.c0bits) - 1))
|
||||||
gl.lrectwrite(self.xorigin, self.yorigin, \
|
gl.lrectwrite(self.xorigin, self.yorigin, \
|
||||||
self.xorigin + cw - 1, self.yorigin + ch - 1, \
|
self.xorigin + cw - 1, self.yorigin + ch - 1, \
|
||||||
chromdata)
|
chromdata)
|
||||||
#
|
#
|
||||||
if pf:
|
if pf:
|
||||||
if not self.colormapinited:
|
gl.writemask((1 << self.c0bits) - 1)
|
||||||
self.initcolormap()
|
|
||||||
gl.writemask((1 << self.ybits) - 1)
|
|
||||||
gl.pixmode(GL.PM_SIZE, 8)
|
gl.pixmode(GL.PM_SIZE, 8)
|
||||||
gl.rectzoom(factor, factor)
|
gl.rectzoom(factor, factor)
|
||||||
w = w/pf
|
w = w/pf
|
||||||
|
@ -179,36 +220,47 @@ class VinFile():
|
||||||
self.xorigin + w - 1, self.yorigin + h - 1, data)
|
self.xorigin + w - 1, self.yorigin + h - 1, data)
|
||||||
|
|
||||||
def initcolormap(self):
|
def initcolormap(self):
|
||||||
initcmap(self.ybits, self.ibits, self.qbits, self.chrompack)
|
|
||||||
gl.color(0x800)
|
|
||||||
gl.clear()
|
|
||||||
self.colormapinited = 1
|
self.colormapinited = 1
|
||||||
|
if self.format = 'rgb':
|
||||||
|
gl.RGBmode()
|
||||||
|
gl.gconfig()
|
||||||
|
return
|
||||||
|
initcmap(self.convcolor, self.c0bits, self.c1bits, self.c2bits, self.chrompack, self.offset)
|
||||||
|
if self.offset == 0:
|
||||||
|
gl.color(0x800)
|
||||||
|
self.mask = 0x7ff
|
||||||
|
else:
|
||||||
|
self.mask = 0xfff
|
||||||
|
gl.clear()
|
||||||
|
|
||||||
|
|
||||||
def initcmap(ybits, ibits, qbits, chrompack):
|
def initcmap(convcolor, c0bits, c1bits, c2bits, chrompack, offset):
|
||||||
if ybits+ibits+qbits > 11:
|
if c0bits+c1bits+c2bits > 11:
|
||||||
raise Error, 'Sorry, 11 bits max'
|
raise Error, 'Sorry, 11 bits max'
|
||||||
import colorsys
|
import colorsys
|
||||||
maxy = 1 << ybits
|
maxc0 = 1 << c0bits
|
||||||
maxi = 1 << ibits
|
maxc1 = 1 << c1bits
|
||||||
maxq = 1 << qbits
|
maxc2 = 1 << c2bits
|
||||||
for i in range(2048, 4096-256):
|
if offset = 0:
|
||||||
|
offset = 2048
|
||||||
|
rng = (offset, 4192-256)
|
||||||
|
for i in range(rng):
|
||||||
gl.mapcolor(i, 0, 255, 0)
|
gl.mapcolor(i, 0, 255, 0)
|
||||||
for y in range(maxy):
|
for c0 in range(maxc0):
|
||||||
yv = y/float(maxy-1)
|
c0v = c0/float(maxc0-1)
|
||||||
for i in range(maxi):
|
for c1 in range(maxc1):
|
||||||
if maxi = 1:
|
if maxc1 = 1:
|
||||||
iv = 0
|
c1v = 0
|
||||||
else:
|
else:
|
||||||
iv = (i/float(maxi-1))-0.5
|
c1v = c1/float(maxc1-1)
|
||||||
for q in range(maxq):
|
for c2 in range(maxc2):
|
||||||
if maxq = 1:
|
if maxc2 = 1:
|
||||||
qv = 0
|
c2v = 0
|
||||||
else:
|
else:
|
||||||
qv = (q/float(maxq-1))-0.5
|
c2v = c2/float(maxc2-1)
|
||||||
index = 2048 + y + \
|
index = offset + c0 + \
|
||||||
(i << ybits) + (q << (ybits+ibits))
|
(c1 << c0bits) + (c2 << (c0bits+c1bits))
|
||||||
rv, gv, bv = colorsys.yiq_to_rgb(yv, iv, qv)
|
rv, gv, bv = convcolor(c0v, c1v, c2v)
|
||||||
r, g, b = \
|
r, g, b = \
|
||||||
int(rv*255.0), int(gv*255.0), int(bv*255.0)
|
int(rv*255.0), int(gv*255.0), int(bv*255.0)
|
||||||
if index < 4096 - 256:
|
if index < 4096 - 256:
|
||||||
|
@ -220,6 +272,12 @@ def test():
|
||||||
filename = 'film.video'
|
filename = 'film.video'
|
||||||
if sys.argv[1:]: filename = sys.argv[1]
|
if sys.argv[1:]: filename = sys.argv[1]
|
||||||
vin = VinFile().init(filename)
|
vin = VinFile().init(filename)
|
||||||
|
print 'Version: ', vin.version
|
||||||
|
print 'Size: ', vin.width, 'x', vin.height
|
||||||
|
print 'Pack: ', vin.packfactor, vin.chrompack
|
||||||
|
print 'Bits: ', vin.c0bits, vin.c1bits, vin.c2bits
|
||||||
|
print 'Format: ', vin.format
|
||||||
|
print 'Offset: ', vin.offset
|
||||||
gl.foreground()
|
gl.foreground()
|
||||||
gl.prefsize(vin.width, vin.height)
|
gl.prefsize(vin.width, vin.height)
|
||||||
wid = gl.winopen('VFile.test: ' + filename)
|
wid = gl.winopen('VFile.test: ' + filename)
|
||||||
|
@ -228,3 +286,5 @@ def test():
|
||||||
t = vin.shownextframe()
|
t = vin.shownextframe()
|
||||||
except EOFError:
|
except EOFError:
|
||||||
pass
|
pass
|
||||||
|
import time
|
||||||
|
time.sleep(5)
|
||||||
|
|
Loading…
Reference in New Issue