³ò
r©kIc           @   s¿   d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k l Z e d d „ Z	 e d d d „ Z
 d d „ Z d	 „  Z d
 „  Z d „  Z d d d d e d „ Z d „  Z d S(   s'   image.py - Various image manipulations.iÿÿÿÿN(   t   prefsi    c         C   st  | d j  o
 d } n | d j  o
 d } n t  | d ƒ } t  | d ƒ } | d j o | | } } n |  i ƒ  } |  i ƒ  } | oŒ | | j o | | j or |  i ƒ  oa t d o+ |  i | | t i i d d d	 d
 ƒ }  q|  i | | t i i d d d d ƒ }  qþné t	 | ƒ | t	 | ƒ | j o! t
 t  | | | d ƒ ƒ } n t
 t  | | | d ƒ ƒ } |  i ƒ  oa t d o+ |  i | | t i i d d d	 d
 ƒ }  qþ|  i | | t i i d d d d ƒ }  n |  i | | t i i ƒ }  | d j o |  i t i i ƒ }  nM | d j o |  i t i i ƒ }  n' | d j o |  i t i i ƒ }  n |  S(   sÙ  Scale (and return) a pixbuf so that it fits in a rectangle with
    dimensions <width> x <height>. A negative <width> or <height>
    means an unbounded dimension - both cannot be negative.

    If <rotation> is 90, 180 or 270 we rotate <src> first so that the
    rotated pixbuf is fitted in the rectangle.

    Unless <scale_up> is True we don't stretch images smaller than the
    given rectangle.

    If <src> has an alpha channel it gets a checkboard background.
    i    i'  i   iZ   i  s#   checkered bg for transparent imagesiÿ   i   iwww i™™™ i   iÿÿÿ i´   (   iZ   i  (   t   maxt	   get_widtht
   get_heightt   get_has_alphaR    t   composite_color_simplet   gtkt   gdkt   INTERP_TILESt   floatt   intt   scale_simplet   rotate_simplet   PIXBUF_ROTATE_CLOCKWISEt   PIXBUF_ROTATE_UPSIDEDOWNt   PIXBUF_ROTATE_COUNTERCLOCKWISE(   t   srct   widtht   heightt   scale_upt   rotationt	   src_widtht
   src_height(    (    sE   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/image.pyt   fit_in_rectangle   sD    

"#!!c         C   sÌ  | d j  o
 d } n | d j  o
 d } n | d 8} t  | d ƒ } t  | d ƒ } |  i ƒ  } |  i ƒ  } | i ƒ  }	 | i ƒ  }
 | d j o | | } } n | d j o |
 |	 }	 }
 n | |	 } t  | | | d ƒ } t  |	 | | d ƒ } t | t | t | ƒ | t | ƒ ƒ ƒ } t |	 t | t |
 ƒ | t |	 ƒ ƒ ƒ } | | j  o | | | 7} n  | | j o | | | 7} n t |  t | ƒ | | | ƒ t | t | ƒ | | | ƒ f S(	   sÚ  Scale two pixbufs so that they fit together (side-by-side) into a
    rectangle with dimensions <width> x <height>, with a 2 px gap.
    If one pixbuf does not use all of its allotted space, the other one
    is given it, so that the pixbufs are not necessarily scaled to the
    same percentage.

    The pixbufs are rotated according to the angles in <rotation1> and
    <rotation2> before they are scaled.

    See fit_in_rectangle() for more info on the parameters.
    i    i'  i   i   iZ   i  (   iZ   i  (   iZ   i  (   R   R   R   t   roundt   minR	   R   R
   (   t   src1t   src2R   R   R   t	   rotation1t	   rotation2t
   src1_widtht   src1_heightt
   src2_widtht   src2_heightt   total_widtht   alloc_width_src1t   alloc_width_src2t   needed_width_src1t   needed_width_src2(    (    sE   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/image.pyt   fit_2_in_rectangleI   s<    



''	iÿ   c         C   s|   t  i i t  i i t d |  i ƒ  | d |  i ƒ  | d ƒ } | i | ƒ |  i d d |  i ƒ  |  i ƒ  | | | ƒ | S(   sV   Return a pixbuf from <pixbuf> with a <thickness> px border of
    <colour> added.
    i   i   i    (	   R   R   t   Pixbuft   COLORSPACE_RGBt   TrueR   R   t   fillt	   copy_area(   t   pixbuft	   thicknesst   colourt   canvas(    (    sE   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/image.pyt
   add_border|   s    c      	   C   s  |  i  ƒ  } |  i ƒ  } t i i t i i t d | d ƒ } t i i t i i t d | d ƒ } t i i t i i t d d | ƒ } t i i t i i t d d | ƒ } |  i d d | d | d d ƒ |  i d | d | d | d d ƒ |  i d d d | | d d ƒ |  i | d d d | | d d ƒ h  } xr | | | | f D]^ } t | ƒ }	 xI |	 i	 |	 i
 d |	 i
 d ƒ D]& \ }
 } | i | d ƒ |
 | | <qyWqEWd } d } x7 | i ƒ  D]) \ } }
 |
 | j o |
 } | } qÀqÀWg  } | D] } | | d qø~ S(   s  Return the most commonly occurring pixel value along the four edges
    of <pixbuf>. The return value is a sequence, (r, g, b), with 16 bit
    values.

    Note: This could be done more cleanly with subpixbuf(), but that
    doesn't work as expected together with get_pixels().
    i   i   i    i  N(   R   R   R   R   R(   R)   R*   R,   t   pixbuf_to_pilt	   getcolorst   sizet
   setdefaultt   Nonet	   iteritems(   R-   R   R   t   top_edget   bottom_edget	   left_edget
   right_edget   colour_countt   edget   imt   countR/   t	   max_countt   most_common_colourt   _[1]t   val(    (    sE   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/image.pyt   get_most_common_edge_colour‰   s2    $$$$## " " c      
   C   sj   |  i  ƒ  } |  i d j } t i i | t i i | d |  i d |  i d | o d p d |  i d ƒ S(   s-   Return a pixbuf created from the PIL <image>.t   RGBAi   i    i   i   i   (   t   tostringt   modeR   R   t   pixbuf_new_from_dataR)   R4   (   t   imaget   imagestrt   IS_RGBA(    (    sE   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/image.pyt   pil_to_pixbufª   s
    c         C   si   |  i  ƒ  |  i ƒ  f } |  i ƒ  } |  i ƒ  } |  i ƒ  o d p d } t i | | | d | | d ƒ S(   s)   Return a PIL image created from <pixbuf>.RE   t   RGBt   rawi   (   R   R   t   get_rowstridet
   get_pixelsR   t   Imaget
   frombuffer(   R-   t
   dimensionst   stridet   pixelsRG   (    (    sE   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/image.pyR2   ³   s
    g      ð?c         C   sê   t  |  ƒ } | d j o t i | ƒ i | ƒ } n | o) | i d j o t i | d d ƒ} n* | d j o t i | ƒ i | ƒ } n | d j o t i | ƒ i | ƒ } n | d j o t i	 | ƒ i | ƒ } n t
 | ƒ S(   sD  Return a modified pixbuf from <pixbuf> where the enhancement operations
    corresponding to each argument has been performed. A value of 1.0 means
    no change. If <autocontrast> is True it overrides the <contrast> value,
    but only if the image mode is supported by ImageOps.autocontrast (i.e.
    it is L or RGB.)
    g      ð?t   LRM   t   cutoffgš™™™™™¹?(   RV   s   RGB(   R2   t   ImageEnhancet
   Brightnesst   enhanceRG   t   ImageOpst   autocontrastt   Contrastt   Colort	   SharpnessRL   (   R-   t
   brightnesst   contrastt
   saturationt	   sharpnessR\   R>   (    (    sE   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/image.pyRZ   ¼   s    c         C   sR   |  i  d ƒ } | d j o d Sn+ | d j o d Sn | d j o d Sn d S(	   sÇ  Return the implied rotation of the pixbuf, as given by the pixbuf's
    orientation option (the value of which is based on EXIF data etc.).

    The implied rotation is the angle (in degrees) that the raw pixbuf should
    be rotated in order to be displayed "correctly". E.g. a photograph taken
    by a camera that is held sideways might store this fact in its EXIF data,       and the pixbuf loader will set the orientation option correspondingly.
    t   orientationt   3i´   t   6iZ   t   8i  i    (   t
   get_option(   R-   Rd   (    (    sE   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/image.pyt   get_implied_rotationÒ   s    (   t   __doc__R   RQ   RX   R[   t	   ImageStatt   preferencesR    t   FalseR   R'   R1   RD   RL   R2   RZ   Ri   (    (    (    sE   /home/sork/Pontus/Programmering/Python/Comix/comix-4.0.4/src/image.pys   <module>   s    =2	!					