Jack Jansen | e6f49ec | 1993-01-27 11:39:37 +0000 | [diff] [blame] | 1 | import imageop |
| 2 | |
| 3 | error = 'imgconv.error' |
| 4 | |
| 5 | LOSSY = 1 |
| 6 | NOT_LOSSY = 0 |
| 7 | |
| 8 | def null(img, x, y): |
| 9 | return img |
| 10 | |
| 11 | def mono2grey(img, x, y): |
Jack Jansen | 1e0fdd8 | 1993-02-19 10:06:52 +0000 | [diff] [blame] | 12 | return imageop.mono2grey(img, x, y, 0, 255) |
Jack Jansen | e6f49ec | 1993-01-27 11:39:37 +0000 | [diff] [blame] | 13 | |
| 14 | converters = [ \ |
| 15 | ('grey', 'grey4', imageop.grey2grey4, LOSSY), \ |
| 16 | ('grey', 'grey2', imageop.dither2grey2, LOSSY), \ |
| 17 | ('grey', 'mono', imageop.dither2mono, LOSSY), \ |
| 18 | ('mono', 'grey', mono2grey, NOT_LOSSY), \ |
| 19 | ('grey2', 'grey', imageop.grey22grey, NOT_LOSSY), \ |
| 20 | ('grey4', 'grey', imageop.grey42grey, NOT_LOSSY), \ |
Jack Jansen | 171a55b | 1993-02-19 15:52:30 +0000 | [diff] [blame^] | 21 | ('rgb', 'rgb8', imageop.rgb2rgb8, LOSSY), \ |
| 22 | ('rgb8', 'rgb', imageop.rgb82rgb, NOT_LOSSY), \ |
| 23 | ('rgb', 'grey', imageop.rgb2grey, LOSSY), \ |
| 24 | ('grey', 'rgb', imageop.grey2rgb, NOT_LOSSY) \ |
Jack Jansen | e6f49ec | 1993-01-27 11:39:37 +0000 | [diff] [blame] | 25 | ] |
| 26 | |
Jack Jansen | 171a55b | 1993-02-19 15:52:30 +0000 | [diff] [blame^] | 27 | built = {} |
| 28 | |
Jack Jansen | e6f49ec | 1993-01-27 11:39:37 +0000 | [diff] [blame] | 29 | def addconverter(fcs, tcs, lossy, func): |
| 30 | for i in range(len(converters)): |
| 31 | ifcs, itcs, irtn, ilossy = converters[i] |
| 32 | if (fcs, tcs) == (ifcs, itcs): |
| 33 | converters[i] = (fcs, tcs, func, lossy) |
| 34 | return |
| 35 | converters.append((fcs,tcs,lossy,func)) |
| 36 | |
| 37 | def getconverter(fcs, tcs): |
Jack Jansen | 171a55b | 1993-02-19 15:52:30 +0000 | [diff] [blame^] | 38 | # |
| 39 | # If formats are the same return the dummy converter |
| 40 | # |
Jack Jansen | e6f49ec | 1993-01-27 11:39:37 +0000 | [diff] [blame] | 41 | if fcs == tcs: return null |
Jack Jansen | 171a55b | 1993-02-19 15:52:30 +0000 | [diff] [blame^] | 42 | # |
| 43 | # Otherwise, if we have a converter return that one |
| 44 | # |
Jack Jansen | e6f49ec | 1993-01-27 11:39:37 +0000 | [diff] [blame] | 45 | for ifcs, itcs, irtn, ilossy in converters: |
| 46 | if (fcs, tcs) == (ifcs, itcs): |
| 47 | return irtn |
Jack Jansen | 171a55b | 1993-02-19 15:52:30 +0000 | [diff] [blame^] | 48 | # |
| 49 | # Finally, we try to create a converter |
| 50 | # |
| 51 | if not built.has_key(fcs): |
| 52 | built[fcs] = enumerate_converters(fcs) |
| 53 | if not built[fcs].has_key(tcs): |
| 54 | raise error, 'Sorry, conversion from '+fcs+' to '+tcs+ \ |
| 55 | ' is not implemented' |
| 56 | if len(built[fcs][tcs]) == 3: |
| 57 | # |
| 58 | # Converter not instantiated yet |
| 59 | # |
| 60 | built[fcs][tcs].append(instantiate_converter(built[fcs][tcs])) |
| 61 | cf = built[fcs][tcs][3] |
| 62 | return cf |
| 63 | |
| 64 | def enumerate_converters(fcs): |
| 65 | cvs = {} |
| 66 | formats = [fcs] |
| 67 | steps = 0 |
| 68 | while 1: |
| 69 | workdone = 0 |
| 70 | for ifcs, itcs, irtn, ilossy in converters: |
| 71 | if ifcs == fcs: |
| 72 | template = [ilossy, 1, [irtn]] |
| 73 | elif cvs.has_key(ifcs): |
| 74 | template = cvs[ifcs][:] |
| 75 | template[2] = template[2][:] |
| 76 | if ilossy: |
| 77 | template[0] = ilossy |
| 78 | template[1] = template[1] + 1 |
| 79 | template[2].append(irtn) |
| 80 | else: |
| 81 | continue |
| 82 | if not cvs.has_key(itcs): |
| 83 | cvs[itcs] = template |
| 84 | workdone = 1 |
| 85 | else: |
| 86 | previous = cvs[itcs] |
| 87 | if template < previous: |
| 88 | cvs[itcs] = template |
| 89 | workdone = 1 |
| 90 | if not workdone: |
| 91 | break |
| 92 | steps = steps + 1 |
| 93 | if steps > len(converters): |
| 94 | print '------------------loop in emunerate_converters--------' |
| 95 | print 'CONVERTERS:' |
| 96 | print converters |
| 97 | print 'RESULTS:' |
| 98 | print cvs |
| 99 | raise error, 'Internal error - loop' |
| 100 | return cvs |
| 101 | |
| 102 | def instantiate_converter(args): |
| 103 | list = args[2] |
| 104 | cl = RtConverters().init(list) |
| 105 | args.append(cl.convert) |
| 106 | return args |
| 107 | |
| 108 | class RtConverters(): |
| 109 | def init(self, list): |
| 110 | self.list = list |
| 111 | return self |
| 112 | |
| 113 | def convert(self, img, w, h): |
| 114 | for cv in self.list: |
| 115 | img = cv(img, w, h) |
| 116 | return img |