99
1010Error = 'VFile.Error' # Exception
1111
12+ MAXMAP = 4096 - 256
13+
1214def conv_grey (l ,x ,y ): return colorsys .yiq_to_rgb (l ,0 ,0 )
1315def conv_yiq (y ,i ,q ): return colorsys .yiq_to_rgb (y , (i - 0.5 )* 1.2 , q - 0.5 )
1416def conv_hls (l ,h ,s ): return colorsys .hls_to_rgb (h ,l ,s )
1517def conv_hsv (v ,h ,s ): return colorsys .hsv_to_rgb (h ,s ,v )
1618def conv_rgb (r ,g ,b ):
17- raise Error , 'Attempt to make RGB colormap'
19+ raise Error , 'Attempt to make RGB colormap'
1820
1921# Class VinFile represents a video file used for input.
2022#
@@ -35,6 +37,7 @@ def conv_rgb (r,g,b):
3537# format
3638#
3739# These writable data members provide additional parametrization:
40+ # magnify
3841# xorigin, yorigin
3942
4043class VinFile ():
@@ -43,7 +46,7 @@ class VinFile():
4346 # init() raises whatever open() raises if the file can't be opened.
4447
4548 def init (self , filename ):
46- if filename = '-' :
49+ if filename == '-' :
4750 return self .initfp (sys .stdin , filename )
4851 return self .initfp (open (filename , 'r' ), filename )
4952
@@ -55,11 +58,11 @@ def initfp(self, (fp, filename)):
5558 self .filename = filename
5659 #
5760 line = self .fp .readline ()
58- if line = 'CMIF video 1.0\n ' :
61+ if line == 'CMIF video 1.0\n ' :
5962 self .version = 1.0
60- elif line = 'CMIF video 2.0\n ' :
63+ elif line == 'CMIF video 2.0\n ' :
6164 self .version = 2.0
62- elif line = 'CMIF video 3.0\n ' :
65+ elif line == 'CMIF video 3.0\n ' :
6366 self .version = 3.0
6467 else :
6568 raise Error , self .filename + ': bad video format'
@@ -69,47 +72,50 @@ def initfp(self, (fp, filename)):
6972 self .chrompack = 0
7073 self .offset = 0
7174 self .format = 'grey'
72- elif self .version = 2.0 :
75+ elif self .version == 2.0 :
7376 line = self .fp .readline ()
7477 try :
7578 self .c0bits , self .c1bits , self .c2bits , \
7679 self .chrompack = eval (line [:- 1 ])
7780 if self .c1bits or self .c2bits :
78- self .format = 'yiq'
81+ self .format = 'yiq'
7982 else :
80- self .format = 'grey'
83+ self .format = 'grey'
8184 self .offset = 0
8285 except :
83- raise Error , self .filename + ': bad 2.0 color info'
84- elif self .version = 3.0 :
86+ raise Error , \
87+ self .filename + ': bad 2.0 color info'
88+ elif self .version == 3.0 :
8589 line = self .fp .readline ()
8690 try :
87- self .format , rest = eval (line [:- 1 ])
88- if self .format = 'rgb' :
89- pass
90- elif self .format = 'grey' :
91- self .offset = 0
92- self .c0bits = rest
93- self .c1bits = self .c2bits = \
91+ self .format , rest = eval (line [:- 1 ])
92+ if self .format = = 'rgb' :
93+ pass
94+ elif self .format = = 'grey' :
95+ self .offset = 0
96+ self .c0bits = rest
97+ self .c1bits = self .c2bits = \
9498 self .chrompack = 0
95- else :
96- self .c0bits ,self .c1bits ,self .c2bits ,\
97- self .chrompack ,self .offset = rest
99+ else :
100+ self .c0bits ,self .c1bits ,self .c2bits ,\
101+ self .chrompack ,self .offset = rest
98102 except :
99- raise Error , self .filename + ': bad 3.0 color info'
103+ raise Error , \
104+ self .filename + ': bad 3.0 color info'
100105
101106 try :
102- self .convcolor = eval ('conv_' + self .format )
107+ self .convcolor = eval ('conv_' + self .format )
103108 except :
104- raise Error , self .filename + ': unknown colorsys ' + self .format
109+ raise Error , \
110+ self .filename + ': unknown colorsys ' + self .format
105111 #
106112 line = self .fp .readline ()
107113 try :
108114 x = eval (line [:- 1 ])
109- if self .version > 1.0 or len (x ) = 3 :
115+ if self .version > 1.0 or len (x ) == 3 :
110116 self .width , self .height , self .packfactor = x
111- if self .packfactor = 0 :
112- self .format = 'rgb'
117+ if self .packfactor == 0 :
118+ self .format = 'rgb'
113119 else :
114120 sef .width , self .height = x
115121 self .packfactor = 2
@@ -153,11 +159,10 @@ def skipnextframe(self):
153159 def skipnextframedata (self , (size , chromsize )):
154160 # Note that this won't raise EOFError for a partial frame.
155161 try :
156- self .fp .seek (size + chromsize , 1 ) # Relatc1ve seek
162+ self .fp .seek (size + chromsize , 1 ) # Relative seek
157163 except :
158164 # Assume it's a pipe -- read the data to discard it
159- dummy = self .fp .read (size )
160- dummy = self .fp .read (chromsize )
165+ dummy = self .fp .read (size + chromsize )
161166
162167 def getnextframeheader (self ):
163168 line = self .fp .readline ()
@@ -169,11 +174,11 @@ def getnextframeheader(self):
169174 x = eval (line [:- 1 ])
170175 if type (x ) in (type (0 ), type (0.0 )):
171176 time = x
172- if pf = 0 :
177+ if pf == 0 :
173178 size = w * h * 4
174179 else :
175180 size = (w / pf ) * (h / pf )
176- elif len (x ) = 2 :
181+ elif len (x ) == 2 :
177182 time , size = x
178183 cp = self .chrompack
179184 if cp :
@@ -221,70 +226,80 @@ def showframe(self, (data, chromdata)):
221226
222227 def initcolormap (self ):
223228 self .colormapinited = 1
224- if self .format = 'rgb' :
225- gl .RGBmode ()
226- gl .gconfig ()
227- return
228- initcmap (self .convcolor , self .c0bits , self .c1bits , self .c2bits , self .chrompack , self .offset )
229+ if self .format == 'rgb' :
230+ gl .RGBmode ()
231+ gl .gconfig ()
232+ return
233+ gl .cmode ()
234+ gl .gconfig ()
235+ sys .stderr .write ('Initializing color map...' )
236+ initcmap (self .convcolor , self .c0bits , self .c1bits , \
237+ self .c2bits , self .chrompack , self .offset )
238+ sys .stderr .write (' Done.\n ' )
229239 if self .offset == 0 :
230- gl .color (0x800 )
231- self .mask = 0x7ff
240+ gl .color (0x800 )
241+ self .mask = 0x7ff
232242 else :
233- self .mask = 0xfff
243+ self .mask = 0xfff
234244 gl .clear ()
235245
236246
237247def initcmap (convcolor , c0bits , c1bits , c2bits , chrompack , offset ):
238248 if c0bits + c1bits + c2bits > 11 :
239249 raise Error , 'Sorry, 11 bits max'
240- import colorsys
241250 maxc0 = 1 << c0bits
242251 maxc1 = 1 << c1bits
243252 maxc2 = 1 << c2bits
244- if offset = 0 :
245- offset = 2048
246- rng = (offset , 4192 - 256 )
247- for i in range (rng ):
253+ if offset == 0 :
254+ offset = 2048
255+ for i in range (offset , MAXMAP ):
248256 gl .mapcolor (i , 0 , 255 , 0 )
249257 for c0 in range (maxc0 ):
250258 c0v = c0 / float (maxc0 - 1 )
251259 for c1 in range (maxc1 ):
252- if maxc1 = 1 :
260+ if maxc1 == 1 :
253261 c1v = 0
254262 else :
255263 c1v = c1 / float (maxc1 - 1 )
256264 for c2 in range (maxc2 ):
257- if maxc2 = 1 :
265+ if maxc2 == 1 :
258266 c2v = 0
259267 else :
260268 c2v = c2 / float (maxc2 - 1 )
261269 index = offset + c0 + \
262- (c1 << c0bits ) + (c2 << (c0bits + c1bits ))
270+ (c1 << c0bits ) + (c2 << (c0bits + c1bits ))
263271 rv , gv , bv = convcolor (c0v , c1v , c2v )
264272 r , g , b = \
265- int (rv * 255.0 ), int (gv * 255.0 ), int (bv * 255.0 )
266- if index < 4096 - 256 :
273+ int (rv * 255.0 ), int (gv * 255.0 ), int (bv * 255.0 )
274+ if index < MAXMAP :
267275 gl .mapcolor (index , r , g , b )
268276
269277
270278def test ():
271- import sys
279+ import sys , time
272280 filename = 'film.video'
273281 if sys .argv [1 :]: filename = sys .argv [1 ]
274282 vin = VinFile ().init (filename )
283+ print 'File: ' , filename
275284 print 'Version: ' , vin .version
276- print 'Size: ' , vin .width , 'x' , vin .height
277- print 'Pack: ' , vin .packfactor , vin .chrompack
278- print 'Bits: ' , vin .c0bits , vin .c1bits , vin .c2bits
279- print 'Format: ' , vin .format
280- print 'Offset: ' , vin .offset
285+ print 'Size: ' , vin .width , 'x' , vin .height
286+ print 'Pack: ' , vin .packfactor , '; chrom:' , vin .chrompack
287+ print 'Bits: ' , vin .c0bits , vin .c1bits , vin .c2bits
288+ print 'Format: ' , vin .format
289+ print 'Offset: ' , vin .offset
281290 gl .foreground ()
282291 gl .prefsize (vin .width , vin .height )
283- wid = gl .winopen ('VFile.test: ' + filename )
284- try :
285- while 1 :
286- t = vin .shownextframe ()
287- except EOFError :
288- pass
289- import time
290- time .sleep (5 )
292+ wid = gl .winopen (filename )
293+ vin .initcolormap ()
294+ t0 = time .millitimer ()
295+ while 1 :
296+ try :
297+ t , data , chromdata = vin .getnextframe ()
298+ except EOFError :
299+ break
300+ dt = t + t0 - time .millitimer ()
301+ if dt > 0 :
302+ time .millisleep (dt )
303+ vin .showframe (data , chromdata )
304+ print 'Done.'
305+ time .sleep (2 )
0 commit comments