Clarify portability and main program.
[python/dscho.git] / Demo / sgi / video / imgconv.py
blob8fc98b4abba182e7e4c2f50e8bd7f231fffe14a9
1 import imageop
3 error = 'imgconv.error'
5 LOSSY = 1
6 NOT_LOSSY = 0
8 def null(img, x, y):
9 return img
11 def mono2grey(img, x, y):
12 return imageop.mono2grey(img, x, y, 0, 255)
14 def grey2jpeggrey(img, x, y):
15 import jpeg
16 return jpeg.compress(img, x, y, 1)
18 def rgb2jpeg(img, x, y):
19 import jpeg
20 return jpeg.compress(img, x, y, 4)
22 def jpeggrey2grey(img, width, height):
23 import jpeg
24 data, width, height, bytesperpixel = jpeg.decompress(img)
25 if bytesperpixel <> 1: raise RuntimeError, 'not greyscale jpeg'
26 return data
28 def jpeg2rgb(img, width, height):
29 import jpeg
30 data, width, height, bytesperpixel = jpeg.decompress(img)
31 if bytesperpixel <> 4: raise RuntimeError, 'not rgb jpeg'
32 return data
34 converters = [ \
35 ('grey', 'grey4', imageop.grey2grey4, LOSSY), \
36 ('grey', 'grey2', imageop.dither2grey2, LOSSY), \
37 ('grey', 'mono', imageop.dither2mono, LOSSY), \
38 ('mono', 'grey', mono2grey, NOT_LOSSY), \
39 ('grey2', 'grey', imageop.grey22grey, NOT_LOSSY), \
40 ('grey4', 'grey', imageop.grey42grey, NOT_LOSSY), \
41 ('rgb', 'rgb8', imageop.rgb2rgb8, LOSSY), \
42 ('rgb8', 'rgb', imageop.rgb82rgb, NOT_LOSSY), \
43 ('rgb', 'grey', imageop.rgb2grey, LOSSY), \
44 ('grey', 'rgb', imageop.grey2rgb, NOT_LOSSY), \
45 ('jpeggrey','grey',jpeggrey2grey, NOT_LOSSY), \
46 ('grey', 'jpeggrey',grey2jpeggrey, LOSSY), \
47 ('jpeg', 'rgb', jpeg2rgb, NOT_LOSSY), \
48 ('rgb', 'jpeg', rgb2jpeg, LOSSY), \
51 built = {}
53 def addconverter(fcs, tcs, lossy, func):
54 for i in range(len(converters)):
55 ifcs, itcs, irtn, ilossy = converters[i]
56 if (fcs, tcs) == (ifcs, itcs):
57 converters[i] = (fcs, tcs, func, lossy)
58 return
59 converters.append((fcs,tcs,lossy,func))
61 def getconverter(fcs, tcs):
63 # If formats are the same return the dummy converter
65 if fcs == tcs: return null
67 # Otherwise, if we have a converter return that one
69 for ifcs, itcs, irtn, ilossy in converters:
70 if (fcs, tcs) == (ifcs, itcs):
71 return irtn
73 # Finally, we try to create a converter
75 if not built.has_key(fcs):
76 built[fcs] = enumerate_converters(fcs)
77 if not built[fcs].has_key(tcs):
78 raise error, 'Sorry, conversion from '+fcs+' to '+tcs+ \
79 ' is not implemented'
80 if len(built[fcs][tcs]) == 3:
82 # Converter not instantiated yet
84 built[fcs][tcs].append(instantiate_converter(built[fcs][tcs]))
85 cf = built[fcs][tcs][3]
86 return cf
88 def enumerate_converters(fcs):
89 cvs = {}
90 formats = [fcs]
91 steps = 0
92 while 1:
93 workdone = 0
94 for ifcs, itcs, irtn, ilossy in converters:
95 if ifcs == fcs:
96 template = [ilossy, 1, [irtn]]
97 elif cvs.has_key(ifcs):
98 template = cvs[ifcs][:]
99 template[2] = template[2][:]
100 if ilossy:
101 template[0] = ilossy
102 template[1] = template[1] + 1
103 template[2].append(irtn)
104 else:
105 continue
106 if not cvs.has_key(itcs):
107 cvs[itcs] = template
108 workdone = 1
109 else:
110 previous = cvs[itcs]
111 if template < previous:
112 cvs[itcs] = template
113 workdone = 1
114 if not workdone:
115 break
116 steps = steps + 1
117 if steps > len(converters):
118 print '------------------loop in emunerate_converters--------'
119 print 'CONVERTERS:'
120 print converters
121 print 'RESULTS:'
122 print cvs
123 raise error, 'Internal error - loop'
124 return cvs
126 def instantiate_converter(args):
127 list = args[2]
128 cl = RtConverters(list)
129 args.append(cl.convert)
130 return args
132 class RtConverters:
133 def __init__(self, list):
134 self.list = list
136 def convert(self, img, w, h):
137 for cv in self.list:
138 img = cv(img, w, h)
139 return img