3 error
= 'imgconv.error'
11 def mono2grey(img
, x
, y
):
12 return imageop
.mono2grey(img
, x
, y
, 0, 255)
14 def grey2jpeggrey(img
, x
, y
):
16 return jpeg
.compress(img
, x
, y
, 1)
18 def rgb2jpeg(img
, x
, y
):
20 return jpeg
.compress(img
, x
, y
, 4)
22 def jpeggrey2grey(img
, width
, height
):
24 data
, width
, height
, bytesperpixel
= jpeg
.decompress(img
)
25 if bytesperpixel
<> 1: raise RuntimeError, 'not greyscale jpeg'
28 def jpeg2rgb(img
, width
, height
):
30 data
, width
, height
, bytesperpixel
= jpeg
.decompress(img
)
31 if bytesperpixel
<> 4: raise RuntimeError, 'not rgb jpeg'
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
), \
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
)
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
):
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
+ \
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]
88 def enumerate_converters(fcs
):
94 for ifcs
, itcs
, irtn
, ilossy
in converters
:
96 template
= [ilossy
, 1, [irtn
]]
97 elif cvs
.has_key(ifcs
):
98 template
= cvs
[ifcs
][:]
99 template
[2] = template
[2][:]
102 template
[1] = template
[1] + 1
103 template
[2].append(irtn
)
106 if not cvs
.has_key(itcs
):
111 if template
< previous
:
117 if steps
> len(converters
):
118 print '------------------loop in emunerate_converters--------'
123 raise error
, 'Internal error - loop'
126 def instantiate_converter(args
):
128 cl
= RtConverters(list)
129 args
.append(cl
.convert
)
133 def __init__(self
, list):
136 def convert(self
, img
, w
, h
):