Will now generate converters that go thru intermedeate formats
This commit is contained in:
parent
4fada9c8e0
commit
171a55bf56
|
@ -18,8 +18,14 @@ converters = [ \
|
|||
('mono', 'grey', mono2grey, NOT_LOSSY), \
|
||||
('grey2', 'grey', imageop.grey22grey, NOT_LOSSY), \
|
||||
('grey4', 'grey', imageop.grey42grey, NOT_LOSSY), \
|
||||
('rgb', 'rgb8', imageop.rgb2rgb8, LOSSY), \
|
||||
('rgb8', 'rgb', imageop.rgb82rgb, NOT_LOSSY), \
|
||||
('rgb', 'grey', imageop.rgb2grey, LOSSY), \
|
||||
('grey', 'rgb', imageop.grey2rgb, NOT_LOSSY) \
|
||||
]
|
||||
|
||||
built = {}
|
||||
|
||||
def addconverter(fcs, tcs, lossy, func):
|
||||
for i in range(len(converters)):
|
||||
ifcs, itcs, irtn, ilossy = converters[i]
|
||||
|
@ -29,10 +35,82 @@ def addconverter(fcs, tcs, lossy, func):
|
|||
converters.append((fcs,tcs,lossy,func))
|
||||
|
||||
def getconverter(fcs, tcs):
|
||||
#
|
||||
# If formats are the same return the dummy converter
|
||||
#
|
||||
if fcs == tcs: return null
|
||||
|
||||
#
|
||||
# Otherwise, if we have a converter return that one
|
||||
#
|
||||
for ifcs, itcs, irtn, ilossy in converters:
|
||||
if (fcs, tcs) == (ifcs, itcs):
|
||||
return irtn
|
||||
raise error, 'Sorry, that conversion is not implemented'
|
||||
#
|
||||
# Finally, we try to create a converter
|
||||
#
|
||||
if not built.has_key(fcs):
|
||||
built[fcs] = enumerate_converters(fcs)
|
||||
if not built[fcs].has_key(tcs):
|
||||
raise error, 'Sorry, conversion from '+fcs+' to '+tcs+ \
|
||||
' is not implemented'
|
||||
if len(built[fcs][tcs]) == 3:
|
||||
#
|
||||
# Converter not instantiated yet
|
||||
#
|
||||
built[fcs][tcs].append(instantiate_converter(built[fcs][tcs]))
|
||||
cf = built[fcs][tcs][3]
|
||||
return cf
|
||||
|
||||
def enumerate_converters(fcs):
|
||||
cvs = {}
|
||||
formats = [fcs]
|
||||
steps = 0
|
||||
while 1:
|
||||
workdone = 0
|
||||
for ifcs, itcs, irtn, ilossy in converters:
|
||||
if ifcs == fcs:
|
||||
template = [ilossy, 1, [irtn]]
|
||||
elif cvs.has_key(ifcs):
|
||||
template = cvs[ifcs][:]
|
||||
template[2] = template[2][:]
|
||||
if ilossy:
|
||||
template[0] = ilossy
|
||||
template[1] = template[1] + 1
|
||||
template[2].append(irtn)
|
||||
else:
|
||||
continue
|
||||
if not cvs.has_key(itcs):
|
||||
cvs[itcs] = template
|
||||
workdone = 1
|
||||
else:
|
||||
previous = cvs[itcs]
|
||||
if template < previous:
|
||||
cvs[itcs] = template
|
||||
workdone = 1
|
||||
if not workdone:
|
||||
break
|
||||
steps = steps + 1
|
||||
if steps > len(converters):
|
||||
print '------------------loop in emunerate_converters--------'
|
||||
print 'CONVERTERS:'
|
||||
print converters
|
||||
print 'RESULTS:'
|
||||
print cvs
|
||||
raise error, 'Internal error - loop'
|
||||
return cvs
|
||||
|
||||
def instantiate_converter(args):
|
||||
list = args[2]
|
||||
cl = RtConverters().init(list)
|
||||
args.append(cl.convert)
|
||||
return args
|
||||
|
||||
class RtConverters():
|
||||
def init(self, list):
|
||||
self.list = list
|
||||
return self
|
||||
|
||||
def convert(self, img, w, h):
|
||||
for cv in self.list:
|
||||
img = cv(img, w, h)
|
||||
return img
|
||||
|
|
Loading…
Reference in New Issue