55import sys
66import gl
77import GL
8+ import colorsys
89
910Error = 'VFile.Error' # Exception
1011
12+ def conv_grey (l ,x ,y ): return colorsys .yiq_to_rgb (l ,0 ,0 )
13+ def conv_yiq (y ,i ,q ): return colorsys .yiq_to_rgb (y , (i - 0.5 )* 1.2 , q - 0.5 )
14+ def conv_hls (l ,h ,s ): return colorsys .hls_to_rgb (h ,l ,s )
15+ def conv_hsv (v ,h ,s ): return colorsys .hsv_to_rgb (h ,s ,v )
16+ def conv_rgb (r ,g ,b ):
17+ raise Error , 'Attempt to make RGB colormap'
1118
1219# Class VinFile represents a video file used for input.
1320#
2330# filename
2431# width, height
2532# packfactor
26- # ybits, ibits, qbits, chrompack
33+ # c0bits, c1bits, c2bits, chrompack
34+ # offset
35+ # format
2736#
2837# These writable data members provide additional parametrization:
2938# xorigin, yorigin
@@ -50,25 +59,57 @@ def initfp(self, (fp, filename)):
5059 self .version = 1.0
5160 elif line = 'CMIF video 2.0\n ' :
5261 self .version = 2.0
62+ elif line = 'CMIF video 3.0\n ' :
63+ self .version = 3.0
5364 else :
5465 raise Error , self .filename + ': bad video format'
5566 #
5667 if self .version < 2.0 :
57- self .ybits , self .ibits , self .qbits = 8 , 0 , 0
68+ self .c0bits , self .c1bits , self .c2bits = 8 , 0 , 0
5869 self .chrompack = 0
59- else :
70+ self .offset = 0
71+ self .format = 'grey'
72+ elif self .version = 2.0 :
6073 line = self .fp .readline ()
6174 try :
62- self .ybits , self .ibits , self .qbits , \
75+ self .c0bits , self .c1bits , self .c2bits , \
6376 self .chrompack = eval (line [:- 1 ])
77+ if self .c1bits or self .c2bits :
78+ self .format = 'yiq'
79+ else :
80+ self .format = 'grey'
81+ self .offset = 0
82+ except :
83+ raise Error , self .filename + ': bad 2.0 color info'
84+ elif self .version = 3.0 :
85+ line = self .fp .readline ()
86+ 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 = \
94+ self .chrompack = 0
95+ else :
96+ self .c0bits ,self .c1bits ,self .c2bits ,\
97+ self .chrompack ,self .offset = rest
6498 except :
65- raise Error , self .filename + ': bad color info'
99+ raise Error , self .filename + ': bad 3.0 color info'
100+
101+ try :
102+ self .convcolor = eval ('conv_' + self .format )
103+ except :
104+ raise Error , self .filename + ': unknown colorsys ' + self .format
66105 #
67106 line = self .fp .readline ()
68107 try :
69108 x = eval (line [:- 1 ])
70109 if self .version > 1.0 or len (x ) = 3 :
71110 self .width , self .height , self .packfactor = x
111+ if self .packfactor = 0 :
112+ self .format = 'rgb'
72113 else :
73114 sef .width , self .height = x
74115 self .packfactor = 2
@@ -112,7 +153,7 @@ def skipnextframe(self):
112153 def skipnextframedata (self , (size , chromsize )):
113154 # Note that this won't raise EOFError for a partial frame.
114155 try :
115- self .fp .seek (size + chromsize , 1 ) # Relative seek
156+ self .fp .seek (size + chromsize , 1 ) # Relatc1ve seek
116157 except :
117158 # Assume it's a pipe -- read the data to discard it
118159 dummy = self .fp .read (size )
@@ -154,6 +195,8 @@ def shownextframe(self):
154195
155196 def showframe (self , (data , chromdata )):
156197 w , h , pf = self .width , self .height , self .packfactor
198+ if not self .colormapinited :
199+ self .initcolormap ()
157200 factor = self .magnify
158201 if pf : factor = factor * pf
159202 if chromdata :
@@ -162,15 +205,13 @@ def showframe(self, (data, chromdata)):
162205 ch = (h + cp - 1 )/ cp
163206 gl .rectzoom (factor * cp , factor * cp )
164207 gl .pixmode (GL .PM_SIZE , 16 )
165- gl .writemask (0x7ff - ((1 << self .ybits ) - 1 ))
208+ gl .writemask (self . mask - ((1 << self .c0bits ) - 1 ))
166209 gl .lrectwrite (self .xorigin , self .yorigin , \
167210 self .xorigin + cw - 1 , self .yorigin + ch - 1 , \
168211 chromdata )
169212 #
170213 if pf :
171- if not self .colormapinited :
172- self .initcolormap ()
173- gl .writemask ((1 << self .ybits ) - 1 )
214+ gl .writemask ((1 << self .c0bits ) - 1 )
174215 gl .pixmode (GL .PM_SIZE , 8 )
175216 gl .rectzoom (factor , factor )
176217 w = w / pf
@@ -179,36 +220,47 @@ def showframe(self, (data, chromdata)):
179220 self .xorigin + w - 1 , self .yorigin + h - 1 , data )
180221
181222 def initcolormap (self ):
182- initcmap (self .ybits , self .ibits , self .qbits , self .chrompack )
183- gl .color (0x800 )
184- gl .clear ()
185223 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 .offset == 0 :
230+ gl .color (0x800 )
231+ self .mask = 0x7ff
232+ else :
233+ self .mask = 0xfff
234+ gl .clear ()
186235
187236
188- def initcmap (ybits , ibits , qbits , chrompack ):
189- if ybits + ibits + qbits > 11 :
237+ def initcmap (convcolor , c0bits , c1bits , c2bits , chrompack , offset ):
238+ if c0bits + c1bits + c2bits > 11 :
190239 raise Error , 'Sorry, 11 bits max'
191240 import colorsys
192- maxy = 1 << ybits
193- maxi = 1 << ibits
194- maxq = 1 << qbits
195- for i in range (2048 , 4096 - 256 ):
241+ maxc0 = 1 << c0bits
242+ maxc1 = 1 << c1bits
243+ maxc2 = 1 << c2bits
244+ if offset = 0 :
245+ offset = 2048
246+ rng = (offset , 4192 - 256 )
247+ for i in range (rng ):
196248 gl .mapcolor (i , 0 , 255 , 0 )
197- for y in range (maxy ):
198- yv = y / float (maxy - 1 )
199- for i in range (maxi ):
200- if maxi = 1 :
201- iv = 0
249+ for c0 in range (maxc0 ):
250+ c0v = c0 / float (maxc0 - 1 )
251+ for c1 in range (maxc1 ):
252+ if maxc1 = 1 :
253+ c1v = 0
202254 else :
203- iv = ( i / float (maxi - 1 )) - 0.5
204- for q in range (maxq ):
205- if maxq = 1 :
206- qv = 0
255+ c1v = c1 / float (maxc1 - 1 )
256+ for c2 in range (maxc2 ):
257+ if maxc2 = 1 :
258+ c2v = 0
207259 else :
208- qv = ( q / float (maxq - 1 )) - 0.5
209- index = 2048 + y + \
210- (i << ybits ) + (q << (ybits + ibits ))
211- rv , gv , bv = colorsys . yiq_to_rgb ( yv , iv , qv )
260+ c2v = c2 / float (maxc2 - 1 )
261+ index = offset + c0 + \
262+ (c1 << c0bits ) + (c2 << (c0bits + c1bits ))
263+ rv , gv , bv = convcolor ( c0v , c1v , c2v )
212264 r , g , b = \
213265 int (rv * 255.0 ), int (gv * 255.0 ), int (bv * 255.0 )
214266 if index < 4096 - 256 :
@@ -220,6 +272,12 @@ def test():
220272 filename = 'film.video'
221273 if sys .argv [1 :]: filename = sys .argv [1 ]
222274 vin = VinFile ().init (filename )
275+ 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
223281 gl .foreground ()
224282 gl .prefsize (vin .width , vin .height )
225283 wid = gl .winopen ('VFile.test: ' + filename )
@@ -228,3 +286,5 @@ def test():
228286 t = vin .shownextframe ()
229287 except EOFError :
230288 pass
289+ import time
290+ time .sleep (5 )
0 commit comments