ó
X–Tc           @   sn   d  Z  d d l Z d d l Z d d l Z d „  Z d d d „ Z d „  Z d „  Z d	 e f d
 „  ƒ  YZ	 d S(   sO   A module for manipulating Images, which are specially wrapped Pygame
surfaces.
iÿÿÿÿNc         C   s6   t  j t |  d ƒ t |  d ƒ f t  j d ƒ j ƒ  S(   sM   
    Internal method for creating a new Spyral-compliant Pygame surface.
    i    i   i    (   t   pygamet   Surfacet   intt   SRCALPHAt   convert_alpha(   t   size(    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt   _new_spyral_surface	   s    t   righti    c         C   s½  | d k r· t  t j t j t |  ƒ ƒ ƒ ƒ } d } xzt |  ƒ D]m \ } } | | d k rx d } | | 7} d } n | | j 7} t | | j ƒ } |	 j	 | | | f f ƒ qC Wnü | d k rØ t
 j d d ƒ }
 n t
 j d d ƒ }
 | d k rt |  ƒ n  t | ƒ t t  t f k r4| g t |  ƒ } n t | ƒ } | j	 d ƒ t
 j d d ƒ } g  }	 xK t |  | ƒ D]: \ } } |	 j	 | | f ƒ | |
 | j | | f } quWt |	 ƒ S(	   sß  
    A function that returns a new Image from a list of images by
    placing them next to each other.

    :param images: A list of images to lay out.
    :type images: List of :class:`Image <spyral.Image>`
    :param str orientation: Either 'left', 'right', 'above', 'below', or
                            'square' (square images will be placed in a grid
                            shape, like a chess board).
    :param padding: The padding between each image. Can be specified as a
                    scalar number (for constant padding between all images)
                    or a list (for different paddings between each image).
    :type padding: int or a list of ints.
    :returns: A new :class:`Image <spyral.Image>`
    t   squarei    t   leftR   i   t   above(   s   lefts   right(   s   leftR
   (   R   t   matht   ceilt   sqrtt   lent	   enumeratet   widtht   maxt   heightt   appendt   spyralt   Vec2Dt   reversedt   typet   floatt   longt   listt   zipR   t   from_conglomerate(   t   imagest   orientationt   paddingt   lengtht
   max_heightt   indext   imaget   xt   yt   sequencet   selectort   base(    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt   from_sequence   s4    $
	 c         C   s¡   d \ } } xF |  D]> \ } \ } } t  | | | j ƒ } t  | | | j ƒ } q Wt d | | f ƒ } x0 |  D]( \ } \ } } | j | | | f ƒ qq W| S(   s   
    A function that generates a new image from a sequence of
    (image, position) pairs. These images will be placed onto a singe image
    large enough to hold all of them. More explicit and less convenient than
    :func:`from_seqeuence <spyral.image.from_sequence>`.

    :param sequence: A list of (image, position) pairs, where the positions
                     are :class:`Vec2D <spyral.Vec2D>` s.
    :type sequence: List of image, position pairs.
    :returns: A new :class:`Image <spyral.Image>`
    i    R   (   i    i    (   R   R   R   t   Imaget
   draw_image(   R&   R   R   R#   R$   R%   t   new(    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyR   B   s    c         C   sE  t  j | ƒ } | d } | d } |  j d } t | d ƒ } t | d ƒ } |  j } t  j d | d ƒ }  |  j }	 | j t j d | ƒ ƒ }
 | j t j d | f | ƒ ƒ } | j t j d d | f | ƒ ƒ } | j t j | d f | ƒ ƒ } | j t j | | f | ƒ ƒ } | j t j | d | f | ƒ ƒ } | j t j d | d f | ƒ ƒ } | j t j d | | f | ƒ ƒ } | j t j d | d | f | ƒ ƒ } |	 j	 |
 d ƒ |	 j	 | | | d f ƒ |	 j	 | d | | f ƒ |	 j	 | | | ƒ xR t
 | | | | | ƒ D]6 } |	 j	 | d | f ƒ |	 j	 | | | | f ƒ q&W|	 j	 | d | | | f ƒ |	 j	 | | | | | | f ƒ xR t
 | | | | | ƒ D]6 } |	 j	 | | d f ƒ |	 j	 | | | | f ƒ q»W|	 j	 | | | | d f ƒ |	 j	 | | | | | | f ƒ x] t
 | | | | | ƒ D]A } x8 t
 | | | | | ƒ D] } |	 j	 | | | f ƒ qqWqPWx@ t
 | | | | | ƒ D]$ } |	 j	 | | | | | f ƒ q°Wx@ t
 | | | | | ƒ D]$ } |	 j	 | | | | | f ƒ qóW|	 j	 | | | | | | | f ƒ |  S(	   sß  
    Creates a new image by dividing the given image into a 3x3 grid, and stretching
    the sides and center while leaving the corners the same size. This is ideal
    for buttons and other rectangular shapes.

    :param image: The image to stretch.
    :type image: :class:`Image <spyral.Image>`
    :param size: The new (width, height) of this image.
    :type size: :class:`Vec2D <spyral.Vec2D>`
    :returns: A new :class:`Image <spyral.Image>` similar to the old one.
    i    i   i   R   i   (   i   i   (   i    i    (   i    i    (   R   R   R   R   t   _surfR*   t
   subsurfaceR    t   Rectt   blitt   range(   R#   R   t   bst   bwt   bht   pst   pwt   pht   surft   st   topleftR	   t
   bottomleftt   topt   midt   bottomt   toprightR   t   bottomrightR%   R$   (    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt   render_nine_sliceW   sR    

		!%!!%%%)!"!"!!!"!"&R*   c           B   s  e  Z d  Z d d d „ Z d „  Z e e ƒ Z d „  Z e e ƒ Z	 d „  Z
 e e
 ƒ Z d „  Z d d d d „ Z d	 e d
 „ Z d d d „ Z d d d d „ Z d d „ Z d d d d „ Z d d d „ Z d „  Z d „  Z e e d „ Z d „  Z d d „ Z d d „ Z RS(   sü  
    The image is the basic drawable item in spyral. They can be created
    either by loading from common file formats, or by creating a new
    image and using some of the draw methods. Images are not drawn on
    their own, they are placed as the *image* attribute on Sprites to
    be drawn.

    Almost all of the methods of an Image instance return the Image itself,
    enabling commands to be chained in a
    `fluent interface <http://en.wikipedia.org/wiki/Fluent_interface>`_.

    :param size: If size is passed, creates a new blank image of that size to
                 draw on. If you do not specify a size, you *must* pass in a
                 filename.
    :type size: :class:`Vec2D <spyral.Vec2D>`
    :param str filename:  If filename is set, the file with that name is loaded.
                          The appendix has a list of the 
                          :ref:`valid image formats<ref.image_formats>`. If you do
                          not specify a filename, you *must* pass in a size.

    c         C   s¦   | d  k	 r' | d  k	 r' t d ƒ ‚ n  | d  k rN | d  k rN t d ƒ ‚ n  | d  k	 ru t | ƒ |  _ d  |  _ n$ t j j | ƒ j ƒ  |  _ | |  _ d |  _	 d  S(   Nss   Must specify exactly one of size and filename. See http://platipy.org/en/latest/spyral_docs.html#spyral.image.Imagei   (
   t   Nonet
   ValueErrorR   R-   t   _nameR    R#   t   loadR   t   _version(   t   selft   filenameR   (    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt   __init__¯   s    	c         C   s   |  j  j ƒ  S(   N(   R-   t	   get_width(   RG   (    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt
   _get_width½   s    c         C   s   |  j  j ƒ  S(   N(   R-   t
   get_height(   RG   (    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt   _get_heightÃ   s    c         C   s   t  j |  j j ƒ  ƒ S(   N(   R   R   R-   t   get_size(   RG   (    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt	   _get_sizeÉ   s    c         C   s9   |  j  j | ƒ |  j d 7_ t j j j |  j  ƒ |  S(   s  
        Fills the entire image with the specified color.

        :param color: a three-tuple of RGB values ranging from 0-255. Example:
                      (255, 128, 0) is orange.
        :type color: a three-tuple of ints.
        :returns: This image.
        i   (   R-   t   fillRF   R   t   utilt   scale_surfacet   clear(   RG   t   color(    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyRP   Ð   s    	i    R:   c         C   sš   | d k r t j | ƒ } n t j | | ƒ } |  j | | j ƒ } t j j |  j | | j	 | | j f | ƒ |  j
 d 7_
 t j j j |  j ƒ |  S(   sÝ  
        Draws a rectangle on this image.

        :param color: a three-tuple of RGB values ranging from 0-255. Example:
                      (255, 128, 0) is orange.
        :type color: a three-tuple of ints.
        :param position: The starting position of the rect (top-left corner). If
                         position is a Rect, then size should be `None`.
        :type position: :class:`Vec2D <spyral.Vec2D>` or
                        :class:`Rect <spyral.Rect>`
        :param size: The size of the rectangle; should not be given if position
                     is a rect.
        :type size: :class:`Vec2D <spyral.Vec2D>`
        :param int border_width: The width of the border to draw. If it is 0,
                                 the rectangle is filled with the color
                                 specified.
        :param str anchor: The anchor parameter is an
                           :ref:`anchor position <ref.anchors>`.
        :returns: This image.
        i   N(   RB   R   R/   t   _calculate_offsetR   R    t   drawt   rectR-   t   posRF   RQ   RR   RS   (   RG   RT   t   positionR   t   border_widtht   anchorRW   t   offset(    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt	   draw_rectÞ   s    i   c         C   ss   | d k r+ t  j j |  j | | | ƒ n t  j j |  j | | | | ƒ |  j d 7_ t j j j	 |  j ƒ |  S(   s  
        Draws a series of connected lines on a image, with the
        vertices specified by points. This does not draw any sort of
        end caps on lines.

        :param color: a three-tuple of RGB values ranging from 0-255. Example:
                      (255, 128, 0) is orange.
        :type color: a three-tuple of ints.
        :param points: A list of points that will be connected, one to another.
        :type points: A list of :class:`Vec2D <spyral.Vec2D>` s.
        :param int width: The width of the lines.
        :param bool closed: If closed is True, the first and last point will be
                            connected. If closed is True and width is 0, the
                            shape will be filled.
        :returns: This image.
        i   (
   R    RV   t   aalinesR-   t   linesRF   R   RQ   RR   RS   (   RG   RT   t   pointsR   t   closed(    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt
   draw_linesÿ   s    c         C   sa   |  j  | ƒ } t j j |  j | | | j ƒ  | | ƒ |  j d 7_ t j j	 j
 |  j ƒ |  S(   s•  
        Draws a circle on this image.

        :param color: a three-tuple of RGB values ranging from 0-255. Example:
                      (255, 128, 0) is orange.
        :type color: a three-tuple of ints.
        :param position: The center of this circle
        :type position: :class:`Vec2D <spyral.Vec2D>`
        :param int radius: The radius of this circle
        :param int width: The width of the circle. If it is 0, the circle is
                          filled with the color specified.
        :param str anchor: The anchor parameter is an
                           :ref:`anchor position <ref.anchors>`.
        :returns: This image.
        i   (   RU   R    RV   t   circleR-   t   floorRF   R   RQ   RR   RS   (   RG   RT   RY   t   radiusR   R[   R\   (    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt   draw_circle  s    
c         C   sš   | d k r t j | ƒ } n t j | | ƒ } |  j | | j ƒ } t j j |  j | | j	 | | j f | ƒ |  j
 d 7_
 t j j j |  j ƒ |  S(   s¬  
        Draws an ellipse on this image.

        :param color: a three-tuple of RGB values ranging from 0-255. Example:
                      (255, 128, 0) is orange.
        :type color: a three-tuple of ints.
        :param position: The starting position of the ellipse (top-left corner).
                         If position is a Rect, then size should be `None`.
        :type position: :class:`Vec2D <spyral.Vec2D>` or
                        :class:`Rect <spyral.Rect>`
        :param size: The size of the ellipse; should not be given if position is
                     a rect.
        :type size: :class:`Vec2D <spyral.Vec2D>`
        :param int border_width: The width of the ellipse. If it is 0, the
                          ellipse is filled with the color specified.
        :param str anchor: The anchor parameter is an
                           :ref:`anchor position <ref.anchors>`.
        :returns: This image.
        i   N(   RB   R   R/   RU   R   R    RV   t   ellipseR-   RX   RF   RQ   RR   RS   (   RG   RT   RY   R   RZ   R[   RW   R\   (    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt   draw_ellipse/  s    c         C   sO   |  j  | ƒ } |  j j | | | ƒ |  j d 7_ t j j j |  j ƒ |  S(   sÙ  
        Draws a point on this image.

        :param color: a three-tuple of RGB values ranging from 0-255. Example:
                      (255, 128, 0) is orange.
        :type color: a three-tuple of ints.
        :param position: The position of this point.
        :type position: :class:`Vec2D <spyral.Vec2D>`
        :param str anchor: The anchor parameter is an
                           :ref:`anchor position <ref.anchors>`.
        :returns: This image.
        i   (   RU   R-   t   set_atRF   R   RQ   RR   RS   (   RG   RT   RY   R[   R\   (    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt
   draw_pointO  s
    c   
      C   s    | d k r t j | ƒ } n t j | | ƒ } |  j | | j ƒ }	 t j j |  j | | j	 |	 | j f | | | ƒ |  j
 d 7_
 t j j j |  j ƒ |  S(   sK  
        Draws an elliptical arc on this image.

        :param color: a three-tuple of RGB values ranging from 0-255. Example:
                      (255, 128, 0) is orange.
        :type color: a three-tuple of ints.
        :param float start_angle: The starting angle, in radians, of the arc.
        :param float end_angle: The ending angle, in radians, of the arc.
        :param position: The starting position of the ellipse (top-left corner).
                         If position is a Rect, then size should be `None`.
        :type position: :class:`Vec2D <spyral.Vec2D>` or
                        :class:`Rect <spyral.Rect>`
        :param size: The size of the ellipse; should not be given if position is
                     a rect.
        :type size: :class:`Vec2D <spyral.Vec2D>`
        :param int border_width: The width of the ellipse. If it is 0, the
                          ellipse is filled with the color specified.
        :param str anchor: The anchor parameter is an
                           :ref:`anchor position <ref.anchors>`.
        :returns: This image.
        i   N(   RB   R   R/   RU   R   R    RV   t   arcR-   RX   RF   RQ   RR   RS   (
   RG   RT   t   start_anglet	   end_angleRY   R   RZ   R[   RW   R\   (    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt   draw_arcb  s    %c         C   s^   |  j  | | j j ƒ  ƒ } |  j j | j | | ƒ |  j d 7_ t j j j |  j ƒ |  S(   s§  
        Draws another image over this one.

        :param image: The image to overlay on top of this one.
        :type image: :class:`Image <spyral.Image>`
        :param position: The position of this image.
        :type position: :class:`Vec2D <spyral.Vec2D>`
        :param str anchor: The anchor parameter is an
                           :ref:`anchor position <ref.anchors>`.
        :returns: This image.
        i   (	   RU   R-   RN   R0   RF   R   RQ   RR   RS   (   RG   R#   RY   R[   R\   (    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyR+   „  s
    c         C   s4   t  j j |  j | ƒ j ƒ  |  _ |  j d 7_ |  S(   si  
        Rotates the image by angle degrees clockwise. This may change the image
        dimensions if the angle is not a multiple of 90.

        Successive rotations degrate image quality. Save a copy of the
        original if you plan to do many rotations.

        :param float angle: The number of degrees to rotate.
        :returns: This image.
        i   (   R    t	   transformt   rotateR-   R   RF   (   RG   t   angle(    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyRp   –  s    !c         C   s4   t  j j |  j | ƒ j ƒ  |  _ |  j d 7_ |  S(   s¼   
        Scales the image to the destination size.

        :param size: The new size of the image.
        :type size: :class:`Vec2D <spyral.Vec2D>`
        :returns: This image.
        i   (   R    Ro   t   smoothscaleR-   R   RF   (   RG   R   (    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt   scale¥  s    c         C   s7   |  j  d 7_  t j j |  j | | ƒ j ƒ  |  _ |  S(   sÕ   
        Flips the image horizontally, vertically, or both.

        :param bool flip_x: whether to flip horizontally.
        :param bool flip_y: whether to flip vertically.
        :returns: This image.
        i   (   RF   R    Ro   t   flipR-   R   (   RG   t   flip_xt   flip_y(    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyRt   ²  s    c         C   s%   t  j  |  ƒ } |  j j  ƒ  | _ | S(   s‰   
        Returns a copy of this image that can be changed while preserving the
        original.

        :returns: A new image.
        (   t   copyR-   (   RG   R,   (    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyRw   ¿  s    c         C   sz   | d k r t j | ƒ } n t j | | ƒ } t | ƒ } | j |  j d | j | j f ƒ | |  _ |  j d 7_ |  S(   sQ  
        Removes the edges of an image, keeping the internal rectangle specified
        by position and size.

        :param position: The upperleft corner of the internal rectangle that
                         will be preserved.
        :type position: a :class:`Vec2D <spyral.Vec2D>` or a
                        :class:`Rect <spyral.Rect>`.
        :param size: The size of the internal rectangle to preserve. If a Rect
                     was passed in for position, this should be None.
        :type size: :class:`Vec2D <spyral.Vec2D>` or None.
        :returns: This image.
        i    i   N(   i    i    (	   RB   R   R/   R   R0   R-   RX   R   RF   (   RG   RY   R   RW   R,   (    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt   cropÊ  s    "	c         C   s†  |  j  j ƒ  \ } } | \ } } | d k r= t j d d ƒ S| d k r] t j | | d ƒ S| d k r t j | | d d ƒ S| d k r¡ t j d | | ƒ S| d k rÅ t j | | | | ƒ S| d k rí t j | | d | | ƒ S| d	 k rt j d | | d ƒ S| d
 k r9t j | | | | d ƒ S| d k ret j | | d | | d ƒ St j | ƒ t j | | ƒ Sd S(   sô  
        Internal method for calculating the offset associated with an
        anchor type.

        :param anchor_type: A string indicating the position of the anchor,
                            taken from :ref:`anchor position <ref.anchors>`. A
                            numerical offset can also be specified.
        :type anchor_type: str or a :class:`Vec2D <spyral.Vec2D>`.
        :param size: The size of the region to offset in.
        :type size: :class:`Vec2D <spyral.Vec2D>`.
        R:   i    R?   t   midtopg       @R;   R@   t	   midbottomt   midleftt   midrightt   centerN(   R-   RN   R   R   (   RG   t   anchor_typeR   t   wt   ht   w2t   h2(    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyRU   â  s*     N(   i    i    (   i    i    (   t   __name__t
   __module__t   __doc__RB   RI   RK   t   propertyR   RM   R   RO   R   RP   R]   t   FalseRb   Rf   Rh   Rj   Rn   R+   Rp   Rs   t   TrueRt   Rw   Rx   RU   (    (    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyR*   ˜   s0   				 !			(
   R…   R    R   Rw   R   R)   R   RA   t   objectR*   (    (    (    s0   /usr/lib/python2.7/site-packages/spyral/image.pyt   <module>   s   	1		A