changes by Barry, e.g. font lock & email addresses
[python/dscho.git] / Demo / sgi / video / imgconv.py
blob291fdc8125deb3a2ff47155b5177cbf8055b79a5
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 cl, CL
30 import jpeg
31 data, width, height, bytesperpixel = jpeg.decompress(img)
32 if bytesperpixel <> 4: raise RuntimeError, 'not rgb jpeg'
33 return data
35 converters = [ \
36 ('grey', 'grey4', imageop.grey2grey4, LOSSY), \
37 ('grey', 'grey2', imageop.dither2grey2, LOSSY), \
38 ('grey', 'mono', imageop.dither2mono, LOSSY), \
39 ('mono', 'grey', mono2grey, NOT_LOSSY), \
40 ('grey2', 'grey', imageop.grey22grey, NOT_LOSSY), \
41 ('grey4', 'grey', imageop.grey42grey, NOT_LOSSY), \
42 ('rgb', 'rgb8', imageop.rgb2rgb8, LOSSY), \
43 ('rgb8', 'rgb', imageop.rgb82rgb, NOT_LOSSY), \
44 ('rgb', 'grey', imageop.rgb2grey, LOSSY), \
45 ('grey', 'rgb', imageop.grey2rgb, NOT_LOSSY), \
46 ('jpeggrey','grey',jpeggrey2grey, NOT_LOSSY), \
47 ('grey', 'jpeggrey',grey2jpeggrey, LOSSY), \
48 ('jpeg', 'rgb', jpeg2rgb, NOT_LOSSY), \
49 ('rgb', 'jpeg', rgb2jpeg, LOSSY), \
52 built = {}
54 def addconverter(fcs, tcs, lossy, func):
55 for i in range(len(converters)):
56 ifcs, itcs, irtn, ilossy = converters[i]
57 if (fcs, tcs) == (ifcs, itcs):
58 converters[i] = (fcs, tcs, func, lossy)
59 return
60 converters.append((fcs,tcs,lossy,func))
62 def getconverter(fcs, tcs):
64 # If formats are the same return the dummy converter
66 if fcs == tcs: return null
68 # Otherwise, if we have a converter return that one
70 for ifcs, itcs, irtn, ilossy in converters:
71 if (fcs, tcs) == (ifcs, itcs):
72 return irtn
74 # Finally, we try to create a converter
76 if not built.has_key(fcs):
77 built[fcs] = enumerate_converters(fcs)
78 if not built[fcs].has_key(tcs):
79 raise error, 'Sorry, conversion from '+fcs+' to '+tcs+ \
80 ' is not implemented'
81 if len(built[fcs][tcs]) == 3:
83 # Converter not instantiated yet
85 built[fcs][tcs].append(instantiate_converter(built[fcs][tcs]))
86 cf = built[fcs][tcs][3]
87 return cf
89 def enumerate_converters(fcs):
90 cvs = {}
91 formats = [fcs]
92 steps = 0
93 while 1:
94 workdone = 0
95 for ifcs, itcs, irtn, ilossy in converters:
96 if ifcs == fcs:
97 template = [ilossy, 1, [irtn]]
98 elif cvs.has_key(ifcs):
99 template = cvs[ifcs][:]
100 template[2] = template[2][:]
101 if ilossy:
102 template[0] = ilossy
103 template[1] = template[1] + 1
104 template[2].append(irtn)
105 else:
106 continue
107 if not cvs.has_key(itcs):
108 cvs[itcs] = template
109 workdone = 1
110 else:
111 previous = cvs[itcs]
112 if template < previous:
113 cvs[itcs] = template
114 workdone = 1
115 if not workdone:
116 break
117 steps = steps + 1
118 if steps > len(converters):
119 print '------------------loop in emunerate_converters--------'
120 print 'CONVERTERS:'
121 print converters
122 print 'RESULTS:'
123 print cvs
124 raise error, 'Internal error - loop'
125 return cvs
127 def instantiate_converter(args):
128 list = args[2]
129 cl = RtConverters(list)
130 args.append(cl.convert)
131 return args
133 class RtConverters:
134 def __init__(self, list):
135 self.list = list
137 def convert(self, img, w, h):
138 for cv in self.list:
139 img = cv(img, w, h)
140 return img