ó
Č4ec           @   sK  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l m Z m Z m	 Z	 d  d l
 m Z e e j d  Z e r d  d l m Z n  d Z d   Z d   Z d	   Z d
   Z d   Z d e f d     YZ d e j f d     YZ d   Z e j e j e d    e j e j d  e j d k rne j e j e  e j e j d  n  e  d k rGe! e j"  d k  r”d GHe j#   n  e e$ e j" d d   Z% xB e% j& d D]3 Z' e' e% _' e% j(   e% j) Z) e) j* d e'  qĖWe j$ e j" d  Z) e) j* d  e j d k rGe j+ d  qGn  d S(   i’’’’N(   t   Imaget	   ImageFilet   PngImagePlugin(   t   i8t   jp2klib_version(   t   Jpeg2KImagePlugini   c         C   s   t  j d |  j t   S(   Ns   >4sI(   t   structt   unpackt   readt
   HEADERSIZE(   t   fobj(    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyt
   nextheader#   s    c         C   sa   | \ } } |  j  |  |  j d  } | d k rC t d   n  t |  | d | d f |  S(   Ni   s       s'   Unknown signature, expecting 0x00000000(   t   seekR   t   SyntaxErrort   read_32(   R
   t   start_lengtht   sizet   startt   lengtht   sig(    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyt   read_32t'   s    c      	   C   sš  | \ } } |  j  |  | d | d | d | d f } | d | d } | | d k r |  j |  } t j d | | d d d d  } nSt j d | d  } x;t d  D]-}	 g  }
 | } x¹ | d k r|  j d  } | sī Pn  t |  } | d @rD| d } |  j d  } xD t |  D] } |
 j |  q*Wn  | d } |
 j |  j |   | | 8} | d k rÉ PqÉ qÉ W| d k r”t	 d	 |   n  t j d
 | d j
 |
  d d
 d d  } | j j | j |	  q“ Wi | d 6S(   sn   
    Read a 32bit RGB icon resource.  Seems to be either uncompressed or
    an RLE packbits-like scheme.
    i    i   i   i   t   RGBt   rawi   i}   s   Error reading channel [%r left]t   Lt    N(   R   R   R    t
   frombuffert   newt   Nonet   rangeR   t   appendR   t   joint   imt   putband(   R
   R   R   R   R   t
   pixel_sizet   sizesqt   indataR   t   band_ixt   datat	   bytesleftt   bytet	   blocksizet   it   band(    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyR   1   s>    $$



*c         C   s   | d } |  j  |  | d | d | d | d f } | d | d } t j d | |  j |  d d d d  } i | d 6S(   Ni    i   i   R   R   t   A(   R   R    R   R   (   R
   R   R   R   R!   R"   R*   (    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyt   read_mk[   s    
$*c   	      C   s  | \ } } |  j  |  |  j d  } | d  d k r_ |  j  |  t j |   } i | d 6S| d  d k s | d  d k s | d k rt s  t d	   n  |  j  |  |  j |  } t j |  } t j	 |  } | j
 d k rū | j d  } n  i | d 6St d
   d  S(   Ni   i   s   PNG

t   RGBAi   s   ’O’Qs   

s      jP  

sQ   Unsupported icon subimage format (rebuild PIL with JPEG 2000 support to fix this)s    Unsupported icon subimage format(   R   R   R   t   PngImageFilet   enable_jpeg2kt
   ValueErrort   iot   BytesIOR   t   Jpeg2KImageFilet   modet   convert(	   R
   R   R   R   R   R   R   t
   jp2kstreamt   f(    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyt   read_png_or_jpeg2000e   s*    t   IcnsFilec           B   s=  e  Z i d  e f g d! 6d e f g d" 6d e f g d# 6d e f g d$ 6d e f g d% 6d
 e f d e f d e f g d& 6d e f g d' 6d e f g d( 6d e f d e f g d) 6d e f d e f d e f g d* 6d e f g d+ 6d e f d e f d e f g d, 6Z d   Z d   Z d   Z	 d   Z
 d- d   Z RS(.   t   ic10i   i   t   ic09i   t   ic14i   t   ic08t   ic13i   t   ic07t   it32t   t8mkt   icp6i@   t   ic12i    t   ih32t   h8mki0   t   icp5t   il32t   l8mkt   ic11i   t   icp4t   is32t   s8mkc         C   sĪ   i  |  _  } | |  _ t |  \ } } | d k rC t d   n  t } x~ | | k  rÉ t |  \ } } | d k r t d   n  | t 7} | t 8} | | f | | <| j | t j  | | 7} qL Wd S(   s@   
        fobj is a file-like object as an icns resource
        t   icnss   not an icns filei    s   invalid block headerN(   t   dctR
   R   R   R	   R   R1   t   SEEK_CUR(   t   selfR
   RN   R   t   filesizeR)   R(   (    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyt   __init__    s    	

c         C   sa   g  } xT |  j  j   D]C \ } } x4 | D], \ } } | |  j k r) | j |  Pq) q) Wq W| S(   N(   t   SIZESt   itemsRN   R   (   RP   t   sizesR   t   fmtst   fmtt   reader(    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyt	   itersizesµ   s    c         C   s+   |  j    } | s! t d   n  t |  S(   Ns   No 32bit icon resources found(   RY   R   t   max(   RP   RU   (    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyt   bestsize¾   s    c         C   se   i  } xX |  j  | D]I \ } } |  j j |  } | d k	 r | j | |  j | |   q q W| S(   sĀ   
        Get an icon resource as {channel: array}.  Note that
        the arrays are bottom-up like windows bitmaps and will likely
        need to be flipped or transposed in some way.
        N(   RS   RN   t   getR   t   updateR
   (   RP   R   RN   t   codeRX   t   desc(    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyt   dataforsizeÄ   s    #c         C   s“   | d  k r |  j   } n  t |  d k rG | d | d d f } n  |  j |  } | j d d   } | rr | S| j d  j   } y | j | d  Wn t k
 rÆ n X| S(   Ni   i    i   R-   R   R+   (   R   R[   t   lenR`   R\   t   copyt   putalphat   KeyError(   RP   R   t   channelsR   (    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyt   getimageŃ   s    (   i   i   i   (   i   i   i   (   i   i   i   (   i   i   i   (   i   i   i   (   i   i   i   (   i@   i@   i   (   i    i    i   (   i0   i0   i   (   i    i    i   (   i   i   i   (   i   i   i   N(   t   __name__t
   __module__R8   R   R,   R   RS   RR   RY   R[   R`   R   Rf   (    (    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyR9      s0   											t   IcnsImageFilec           B   sM   e  Z d  Z d Z d Z d   Z e d    Z e j d    Z d   Z	 RS(   s!  
    PIL image support for Mac OS .icns files.
    Chooses the best resolution, but will possibly load
    a different size image if you mutate the size attribute
    before calling 'load'.

    The info dictionary has a key 'sizes' that is a list
    of sizes that the icns file has.
    t   ICNSs   Mac OS icns resourcec         C   sz   t  |  j  |  _ d |  _ |  j j   |  j d <|  j j   |  _ |  j d |  j d |  j d |  j d f |  _ d  S(   NR-   RU   i    i   i   (	   R9   t   fpRM   R4   RY   t   infoR[   t	   best_sizeR   (   RP   (    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyt   _openö   s    	c         C   s   |  j  S(   N(   t   _size(   RP   (    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyR      s    c         C   s  | } | |  j  d k rE t |  d k rE | d | d d f } n  | |  j  d k rä t |  d k rä | d d k rä g  |  j  d D]* } | d | d | d | d f ^ q } | | k rä |  j  d | j |  } qä n  | |  j  d k rt d   n  | |  _ d  S(   NRU   i   i    i   i   s2   This is not one of the allowed sizes of this image(   Rl   Ra   t   indexR0   Ro   (   RP   t   valuet	   info_sizeR   t   simple_sizes(    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyR     s    %> c         C   sä   t  |  j  d k rW |  j |  _ |  j d |  j d |  j d |  j d f |  _ n  t j j |   |  j r |  j j |  j k r d  S|  j   |  j j |  j  } | j   | j |  _ | j	 |  _	 | j |  _ |  j
   d  S(   Ni   i    i   i   (   Ra   R   Rm   R    t   loadR   t   load_prepareRM   Rf   R4   t   load_end(   RP   R   (    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyRt     s    !

(
   Rg   Rh   t   __doc__t   formatt   format_descriptionRn   t   propertyR   t   setterRt   (    (    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyRi   č   s   		
c      
   C   s   t  | d  r | j   n  t j d  } d   |  j j d g   D } d } d } xž d d d d d	 g D]ē } d
 j | |  } t j	 j
 | | d  }	 | | k r¼ t j | |	  n4 | j | |  j | | f t j   }
 |
 j |	  t j	 j
 | | d  } | j | d |  j | d | d f t j   } | j |  | d } ql Wd d l m } m } m } d d d d | | g } t t j d   } | | d | d | } Wd QX| j j   | j   } t j |  | rü| | |   n  d S(   s„   
    Saves the image as a series of PNG files,
    that are then converted to a .icns file
    using the macOS command line utility 'iconutil'.

    macOS only.
    t   flushs   .iconsetc         S   s   i  |  ] } | | j   q S(    (   t   width(   t   .0R   (    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pys
   <dictcomp>=  s   	 t   append_imagesi   i    i   i   i   s
   icon_{}x{}s   .pngs   @2x.pngi   i’’’’(   t   Popent   PIPEt   CalledProcessErrort   iconutils   -cRM   s   -ot   wbt   stdoutt   stderrN(   t   hasattrR|   t   tempfilet   mkdtempt   encoderinfoR\   R   Rx   t   ost   pathR   t   shutilt   copyfilet   resizeR    t   LANCZOSt   savet
   subprocessR   R   R   t   opent   devnullR   t   closet   waitt   rmtree(   R   Rk   t   filenamet   iconsett   provided_imagest   last_wt   second_patht   wt   prefixt
   first_patht   im_wt   im_w2R   R   R   t   convert_cmdR   t   convert_proct   retcode(    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyt   _save0  s4    '3c         C   s   |  d  d k S(   Ni   RM   (    (   t   x(    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyt   <lambda>a  R   s   .icnst   darwins
   image/icnst   __main__i   s(   Syntax: python IcnsImagePlugin.py [file]i   t   rbRU   s   out-%s-%s-%s.pngs   out.pngt   windows(,   R1   R   R   R   t   sysR   t   PILR    R   R   t   PIL._binaryR   R   t   coreR/   R   R	   R   R   R   R,   R8   t   objectR9   Ri   R„   t   register_openRx   t   register_extensiont   platformt   register_savet   register_mimeRg   Ra   t   argvt   exitR   t   imfRl   R   Rt   R   R   t	   startfile(    (    (    s=   /usr/local/lib/python2.7/dist-packages/PIL/IcnsImagePlugin.pyt   <module>   sL   		
	*	
	eH	1	
	