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 | |
Guido van Rossum | a557b0f | 1993-02-23 17:08:53 +0000 | [diff] [blame] | 14 | def grey2jpeggrey(img, x, y): |
| 15 | import jpeg |
| 16 | return jpeg.compress(img, x, y, 1) |
| 17 | |
| 18 | def rgb2jpeg(img, x, y): |
| 19 | import jpeg |
| 20 | return jpeg.compress(img, x, y, 4) |
| 21 | |
| 22 | def jpeggrey2grey(img, width, height): |
| 23 | import jpeg |
Guido van Rossum | be6d77a | 1993-02-25 00:17:22 +0000 | [diff] [blame] | 24 | data, width, height, bytesperpixel = jpeg.decompress(img) |
Guido van Rossum | 5e044b7 | 1993-02-25 14:20:13 +0000 | [diff] [blame] | 25 | if bytesperpixel <> 1: raise RuntimeError, 'not greyscale jpeg' |
Guido van Rossum | a557b0f | 1993-02-23 17:08:53 +0000 | [diff] [blame] | 26 | return data |
| 27 | |
| 28 | def jpeg2rgb(img, width, height): |
| 29 | import cl, CL |
| 30 | import jpeg |
Guido van Rossum | be6d77a | 1993-02-25 00:17:22 +0000 | [diff] [blame] | 31 | data, width, height, bytesperpixel = jpeg.decompress(img) |
Guido van Rossum | a557b0f | 1993-02-23 17:08:53 +0000 | [diff] [blame] | 32 | if bytesperpixel <> 4: raise RuntimeError, 'not rgb jpeg' |
| 33 | return data |
| 34 | |
Jack Jansen | e6f49ec | 1993-01-27 11:39:37 +0000 | [diff] [blame] | 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), \ |
Jack Jansen | 171a55b | 1993-02-19 15:52:30 +0000 | [diff] [blame] | 42 | ('rgb', 'rgb8', imageop.rgb2rgb8, LOSSY), \ |
| 43 | ('rgb8', 'rgb', imageop.rgb82rgb, NOT_LOSSY), \ |
| 44 | ('rgb', 'grey', imageop.rgb2grey, LOSSY), \ |
Guido van Rossum | a557b0f | 1993-02-23 17:08:53 +0000 | [diff] [blame] | 45 | ('grey', 'rgb', imageop.grey2rgb, NOT_LOSSY), \ |
| 46 | ('jpeggrey','grey',jpeggrey2grey, NOT_LOSSY), \ |
Guido van Rossum | be6d77a | 1993-02-25 00:17:22 +0000 | [diff] [blame] | 47 | ('grey', 'jpeggrey',grey2jpeggrey, LOSSY), \ |
Guido van Rossum | a557b0f | 1993-02-23 17:08:53 +0000 | [diff] [blame] | 48 | ('jpeg', 'rgb', jpeg2rgb, NOT_LOSSY), \ |
Guido van Rossum | be6d77a | 1993-02-25 00:17:22 +0000 | [diff] [blame] | 49 | ('rgb', 'jpeg', rgb2jpeg, LOSSY), \ |
Jack Jansen | e6f49ec | 1993-01-27 11:39:37 +0000 | [diff] [blame] | 50 | ] |
| 51 | |
Jack Jansen | 171a55b | 1993-02-19 15:52:30 +0000 | [diff] [blame] | 52 | built = {} |
| 53 | |
Jack Jansen | e6f49ec | 1993-01-27 11:39:37 +0000 | [diff] [blame] | 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)) |
| 61 | |
| 62 | def getconverter(fcs, tcs): |
Jack Jansen | 171a55b | 1993-02-19 15:52:30 +0000 | [diff] [blame] | 63 | # |
| 64 | # If formats are the same return the dummy converter |
| 65 | # |
Jack Jansen | e6f49ec | 1993-01-27 11:39:37 +0000 | [diff] [blame] | 66 | if fcs == tcs: return null |
Jack Jansen | 171a55b | 1993-02-19 15:52:30 +0000 | [diff] [blame] | 67 | # |
| 68 | # Otherwise, if we have a converter return that one |
| 69 | # |
Jack Jansen | e6f49ec | 1993-01-27 11:39:37 +0000 | [diff] [blame] | 70 | for ifcs, itcs, irtn, ilossy in converters: |
| 71 | if (fcs, tcs) == (ifcs, itcs): |
| 72 | return irtn |
Jack Jansen | 171a55b | 1993-02-19 15:52:30 +0000 | [diff] [blame] | 73 | # |
| 74 | # Finally, we try to create a converter |
| 75 | # |
| 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: |
| 82 | # |
| 83 | # Converter not instantiated yet |
| 84 | # |
| 85 | built[fcs][tcs].append(instantiate_converter(built[fcs][tcs])) |
| 86 | cf = built[fcs][tcs][3] |
| 87 | return cf |
| 88 | |
| 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 |
| 126 | |
| 127 | def instantiate_converter(args): |
| 128 | list = args[2] |
Guido van Rossum | 21a3ff9 | 1993-12-17 15:11:41 +0000 | [diff] [blame] | 129 | cl = RtConverters(list) |
Jack Jansen | 171a55b | 1993-02-19 15:52:30 +0000 | [diff] [blame] | 130 | args.append(cl.convert) |
| 131 | return args |
| 132 | |
Jack Jansen | 66338ec | 1993-05-25 10:40:23 +0000 | [diff] [blame] | 133 | class RtConverters: |
Guido van Rossum | 21a3ff9 | 1993-12-17 15:11:41 +0000 | [diff] [blame] | 134 | def __init__(self, list): |
Jack Jansen | 171a55b | 1993-02-19 15:52:30 +0000 | [diff] [blame] | 135 | self.list = list |
Jack Jansen | 171a55b | 1993-02-19 15:52:30 +0000 | [diff] [blame] | 136 | |
| 137 | def convert(self, img, w, h): |
| 138 | for cv in self.list: |
| 139 | img = cv(img, w, h) |
| 140 | return img |