ó
È4ec           @   sœ   d  d l  Z  d d l m Z m Z m Z m Z d e f d „  ƒ  YZ d „  Z d „  Z	 d „  Z
 d	 d
 „ Z d	 d „ Z d d „ Z d	 d „ Z d „  Z d S(   iÿÿÿÿNi   (   t   GimpGradientFilet   GimpPaletteFilet
   ImageColort   PaletteFilet   ImagePalettec           B   sS   e  Z d  Z d d	 d d „ Z d „  Z d „  Z d „  Z e Z d „  Z	 d „  Z
 RS(
   ss  
    Color palette for palette mapped images

    :param mode: The mode to use for the Palette. See:
        :ref:`concept-modes`. Defaults to "RGB"
    :param palette: An optional palette. If given, it must be a bytearray,
        an array or a list of ints between 0-255 and of length ``size``
        times the number of colors in ``mode``. The list must be aligned
        by channel (All R values must be contiguous in the list before G
        and B values.) Defaults to 0 through 255 per channel.
    :param size: An optional palette size. If given, it cannot be equal to
        or greater than 256. Defaults to 0.
    t   RGBi    c         C   s®   | |  _  d  |  _ | p4 t t d ƒ ƒ t |  j  ƒ |  _ i  |  _ d  |  _ | d k rz t |  j  ƒ d t |  j ƒ k s› | d k rª | t |  j ƒ k rª t	 d ƒ ‚ n  d  S(   Ni   i    s   wrong palette size(
   t   modet   Nonet   rawmodet	   bytearrayt   ranget   lent   palettet   colorst   dirtyt
   ValueError(   t   selfR   R   t   size(    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyt   __init__'   s    		(		.!c         C   sb   t  ƒ  } |  j | _ |  j | _ |  j d  k	 r@ |  j | _ n  |  j j ƒ  | _ |  j | _ | S(   N(   R   R   R   R   R   R   t   copyR   (   R   t   new(    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyR   2   s    	c         C   s0   |  j  r |  j  |  j f S|  j d |  j ƒ  f S(   s£   
        Get palette contents in format suitable for the low-level
        ``im.putpalette`` primitive.

        .. warning:: This method is experimental.
        s   ;L(   R   R   R   t   tobytes(   R   (    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyt   getdata>   s    	c         C   si   |  j  r t d ƒ ‚ n  t |  j t ƒ r1 |  j St j d |  j ƒ } t | d ƒ r_ | j ƒ  S| j ƒ  S(   sU   Convert palette to bytes.

        .. warning:: This method is experimental.
        s!   palette contains raw palette datat   BR   (	   R   R   t
   isinstanceR   t   bytest   arrayt   hasattrR   t   tostring(   R   t   arr(    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyR   I   s    	
c         C   s  |  j  r t d ƒ ‚ n  t | t ƒ rð y |  j | SWq t k
 rì t |  j t ƒ rm t |  j ƒ |  _ n  t	 |  j ƒ } | d k r— t d ƒ ‚ n  | |  j | <| d |  j | <| d |  j | d <| d |  j | d <d |  _
 | SXn t d | ƒ ‚ d	 S(
   sg   Given an rgb tuple, allocate palette entry.

        .. warning:: This method is experimental.
        s!   palette contains raw palette datai   s$   cannot allocate more than 256 colorsi    i   i   i   s   unknown color specifier: %rN(   R   R   R   t   tupleR   t   KeyErrorR   R   R	   R   R   (   R   t   colort   index(    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyt   getcolorZ   s$    		c         C   s  |  j  r t d ƒ ‚ n  t | t ƒ r9 t | d ƒ } n  | j d ƒ | j d |  j ƒ x§ t d ƒ D]™ } | j d | ƒ xr t | t |  j ƒ | d t |  j ƒ ƒ D]C } y | j d |  j	 | ƒ Wq¬ t
 k
 rî | j d	 ƒ q¬ Xq¬ W| j d
 ƒ qg W| j ƒ  d S(   sV   Save palette to text file.

        .. warning:: This method is experimental.
        s!   palette contains raw palette datat   ws
   # Palette
s   # Mode: %s
i   s   %di   s    %ds    0s   
N(   R   R   R   t   strt   opent   writeR   R
   R   R   t
   IndexErrort   close(   R   t   fpt   it   j(    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyt   savet   s    	4N(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R"   R,   (    (    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyR      s   				c         C   s(   t  ƒ  } |  | _ | | _ d | _ | S(   Ni   (   R   R   R   R   (   R   t   dataR   (    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyt   rawŽ   s
    				c         C   sK   g  } |  d k rA x2 t  d ƒ D] } | j | | d ƒ q Wn t ‚ | S(   Ni    i   iÿ   (   R
   t   appendt   NotImplementedError(   t   blackt   whitet   lutR*   (    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyt   make_linear_lutš   s    c         C   sD   g  } x7 t  d ƒ D]) } | j t | d |  d d ƒ ƒ q W| S(   Ni   g     ào@g      à?(   R
   R2   t   int(   t   expR6   R*   (    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyt   make_gamma_lut¤   s    'R   c         C   s3   t  t d ƒ ƒ } | j ƒ  t |  | t |  ƒ ƒ S(   Ni   (   t   listR
   t   reverseR   R   (   R   R   (    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyt   negative«   s    
c         C   sZ   d d l  m } g  } x4 t d t |  ƒ ƒ D] } | j | d d ƒ ƒ q- Wt |  | ƒ S(   Niÿÿÿÿ(   t   randinti   i    iÿ   (   t   randomR>   R
   R   R2   R   (   R   R>   R   R*   (    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyR?   ±   s
    s   #fff0c0c         C   sZ   t  j |  ƒ \ } } } t d | ƒ } t d | ƒ } t d | ƒ } t d | | | ƒ S(   Ni    R   (   R   t   getrgbR7   R   (   R5   t   rt   gt   b(    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyt   sepiaº   s
    c         C   s#   t  |  t t d ƒ ƒ t |  ƒ ƒ S(   Ni   (   R   R;   R
   R   (   R   (    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyt   wedgeÂ   s    c         C   s’   t  |  d ƒ } } xs t j t j t j g D]M } y- | j d ƒ | | ƒ j ƒ  } | r] Pn  Wq+ t t f k
 rw q+ Xq+ Wt d ƒ ‚ Wd  QX| S(   Nt   rbi    s   cannot load palette(	   R%   R   R    R   t   seekt
   getpalettet   SyntaxErrorR   t   IOError(   t   filenameR)   t   paletteHandlerR6   (    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyt   loadÆ   s    (   R   t    R    R   R   R   t   objectR   R1   R7   R:   R=   R?   RD   RE   RM   (    (    (    s:   /usr/local/lib/python2.7/dist-packages/PIL/ImagePalette.pyt   <module>   s   "v		
		