home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2153 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  9.1 KB  |  263 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import Image
  5. import _imagingmath
  6. VERBOSE = 0
  7.  
  8. def _isconstant(v):
  9.     if not isinstance(v, type(0)):
  10.         pass
  11.     return isinstance(v, type(0))
  12.  
  13.  
  14. class _Operand:
  15.     
  16.     def __init__(self, im):
  17.         self.im = im
  18.  
  19.     
  20.     def _Operand__fixup(self, im1):
  21.         if isinstance(im1, _Operand):
  22.             if im1.im.mode in ('1', 'L'):
  23.                 return im1.im.convert('I')
  24.             if im1.im.mode in ('I', 'F'):
  25.                 return im1.im
  26.             raise ValueError, 'unsupported mode: %s' % im1.im.mode
  27.         isinstance(im1, _Operand)
  28.         if _isconstant(im1) and self.im.mode in ('1', 'L', 'I'):
  29.             return Image.new('I', self.im.size, im1)
  30.         return Image.new('F', self.im.size, im1)
  31.  
  32.     
  33.     def apply(self, op, im1, im2 = None, mode = None):
  34.         im1 = self._Operand__fixup(im1)
  35.         if im2 is None:
  36.             if not mode:
  37.                 pass
  38.             out = Image.new(im1.mode, im1.size, None)
  39.             im1.load()
  40.             
  41.             try:
  42.                 op = getattr(_imagingmath, op + '_' + im1.mode)
  43.             except AttributeError:
  44.                 raise TypeError, "bad operand type for '%s'" % op
  45.  
  46.             _imagingmath.unop(op, out.im.id, im1.im.id)
  47.         else:
  48.             im2 = self._Operand__fixup(im2)
  49.             if im1.mode != im2.mode:
  50.                 if im1.mode != 'F':
  51.                     im1 = im1.convert('F')
  52.                 
  53.                 if im2.mode != 'F':
  54.                     im2 = im2.convert('F')
  55.                 
  56.                 if im1.mode != im2.mode:
  57.                     raise ValueError, 'mode mismatch'
  58.                 im1.mode != im2.mode
  59.             
  60.             if im1.size != im2.size:
  61.                 size = (min(im1.size[0], im2.size[0]), min(im1.size[1], im2.size[1]))
  62.                 if im1.size != size:
  63.                     im1 = im1.crop((0, 0) + size)
  64.                 
  65.                 if im2.size != size:
  66.                     im2 = im2.crop((0, 0) + size)
  67.                 
  68.                 if not mode:
  69.                     pass
  70.                 out = Image.new(im1.mode, size, None)
  71.             elif not mode:
  72.                 pass
  73.             out = Image.new(im1.mode, im1.size, None)
  74.             im1.load()
  75.             im2.load()
  76.             
  77.             try:
  78.                 op = getattr(_imagingmath, op + '_' + im1.mode)
  79.             except AttributeError:
  80.                 raise TypeError, "bad operand type for '%s'" % op
  81.  
  82.             _imagingmath.binop(op, out.im.id, im1.im.id, im2.im.id)
  83.         return _Operand(out)
  84.  
  85.     
  86.     def __nonzero__(self):
  87.         return self.im.getbbox() is not None
  88.  
  89.     
  90.     def __abs__(self):
  91.         return self.apply('abs', self)
  92.  
  93.     
  94.     def __pos__(self):
  95.         return self
  96.  
  97.     
  98.     def __neg__(self):
  99.         return self.apply('neg', self)
  100.  
  101.     
  102.     def __add__(self, other):
  103.         return self.apply('add', self, other)
  104.  
  105.     
  106.     def __radd__(self, other):
  107.         return self.apply('add', other, self)
  108.  
  109.     
  110.     def __sub__(self, other):
  111.         return self.apply('sub', self, other)
  112.  
  113.     
  114.     def __rsub__(self, other):
  115.         return self.apply('sub', other, self)
  116.  
  117.     
  118.     def __mul__(self, other):
  119.         return self.apply('mul', self, other)
  120.  
  121.     
  122.     def __rmul__(self, other):
  123.         return self.apply('mul', other, self)
  124.  
  125.     
  126.     def __div__(self, other):
  127.         return self.apply('div', self, other)
  128.  
  129.     
  130.     def __rdiv__(self, other):
  131.         return self.apply('div', other, self)
  132.  
  133.     
  134.     def __mod__(self, other):
  135.         return self.apply('mod', self, other)
  136.  
  137.     
  138.     def __rmod__(self, other):
  139.         return self.apply('mod', other, self)
  140.  
  141.     
  142.     def __pow__(self, other):
  143.         return self.apply('pow', self, other)
  144.  
  145.     
  146.     def __rpow__(self, other):
  147.         return self.apply('pow', other, self)
  148.  
  149.     
  150.     def __invert__(self):
  151.         return self.apply('invert', self)
  152.  
  153.     
  154.     def __and__(self, other):
  155.         return self.apply('and', self, other)
  156.  
  157.     
  158.     def __rand__(self, other):
  159.         return self.apply('and', other, self)
  160.  
  161.     
  162.     def __or__(self, other):
  163.         return self.apply('or', self, other)
  164.  
  165.     
  166.     def __ror__(self, other):
  167.         return self.apply('or', other, self)
  168.  
  169.     
  170.     def __xor__(self, other):
  171.         return self.apply('xor', self, other)
  172.  
  173.     
  174.     def __rxor__(self, other):
  175.         return self.apply('xor', other, self)
  176.  
  177.     
  178.     def __lshift__(self, other):
  179.         return self.apply('lshift', self, other)
  180.  
  181.     
  182.     def __rshift__(self, other):
  183.         return self.apply('rshift', self, other)
  184.  
  185.     
  186.     def __eq__(self, other):
  187.         return self.apply('eq', self, other)
  188.  
  189.     
  190.     def __ne__(self, other):
  191.         return self.apply('ne', self, other)
  192.  
  193.     
  194.     def __lt__(self, other):
  195.         return self.apply('lt', self, other)
  196.  
  197.     
  198.     def __le__(self, other):
  199.         return self.apply('le', self, other)
  200.  
  201.     
  202.     def __gt__(self, other):
  203.         return self.apply('gt', self, other)
  204.  
  205.     
  206.     def __ge__(self, other):
  207.         return self.apply('ge', self, other)
  208.  
  209.  
  210.  
  211. def imagemath_int(self):
  212.     return _Operand(self.im.convert('I'))
  213.  
  214.  
  215. def imagemath_float(self):
  216.     return _Operand(self.im.convert('F'))
  217.  
  218.  
  219. def imagemath_equal(self, other):
  220.     return self.apply('eq', self, other, mode = 'I')
  221.  
  222.  
  223. def imagemath_notequal(self, other):
  224.     return self.apply('ne', self, other, mode = 'I')
  225.  
  226.  
  227. def imagemath_min(self, other):
  228.     return self.apply('min', self, other)
  229.  
  230.  
  231. def imagemath_max(self, other):
  232.     return self.apply('max', self, other)
  233.  
  234.  
  235. def imagemath_convert(self, mode):
  236.     return _Operand(self.im.convert(mode))
  237.  
  238. ops = { }
  239. for k, v in globals().items():
  240.     if k[:10] == 'imagemath_':
  241.         ops[k[10:]] = v
  242.         continue
  243.  
  244.  
  245. def eval(expression, _dict = { }, **kw):
  246.     args = ops.copy()
  247.     args.update(_dict)
  248.     args.update(kw)
  249.     for k, v in args.items():
  250.         if hasattr(v, 'im'):
  251.             args[k] = _Operand(v)
  252.             continue
  253.     
  254.     import __builtin__
  255.     out = __builtin__.eval(expression, args)
  256.     
  257.     try:
  258.         return out.im
  259.     except AttributeError:
  260.         return out
  261.  
  262.  
  263.