
 X$                 @   s  d  d l  Z  d  d l m Z d  d l m Z m Z m Z m Z m Z d  d l	 m
 Z
 m Z d Z e j Z e j Z e j Z d Z d d   Z d	 d
   Z Gd d   d e  Z Gd d   d e j  Z e j e j e e  e j e j e  e j e j d  d S)    N)BytesIO)Image	ImageFileBmpImagePluginPngImagePlugin_binary)logceilz0.1s      c          
      s  | j  t  |  j j d d d d d d d d g  } |  j \    t    f d	 d
   |  | j  t j d t |    | j	   t |  d } xX| D]P} | \    | j  t j d    | j  t j d     | j  d  | j  d  | j  d  | j  t j d d   t
   } |  j   } | j | t j  | j | d  | j d  | j   } t |  }	 | j  t j d |	   | j  t j d |   | j	   }
 | j |  | j  |  | |	 } | j |
  q Wd  S)Nsizes          0   @         c                sH   |  d  k s@ |  d   k s@ |  d d k s@ |  d d k rD d Sd S)Nr      r   FT )x)heightwidthr   O/home/ubuntu/projects/ifolica/lib/python3.4/site-packages/PIL/IcoImagePlugin.py<lambda>1   s    z_save.<locals>.<lambda>z<HBs    s     Zpngr   z<I)r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )write_MAGICZencoderinfogetsizefilterstructpacklentellr   copyZ	thumbnailr   ZLANCZOSsaveseekread)imfpfilenamer
   offsetr   Zimage_iotmpZimage_bytesZ	bytes_lencurrentr   )r   r   r   _save+   s>    	
r-   c             C   s   |  d  d  t  k S)N   )r   )prefixr   r   r   _acceptN   s    r0   c               @   sC   e  Z d  Z d d   Z d d   Z d d d  Z d d	   Z d
 S)IcoFilec             C   s  | j  d  } t |  s* t d   n  | |  _ g  |  _ t | d d   |  _ xot |  j  D]^} | j  d  } i t | d  d 6t | d  d	 6t | d
  d 6t | d  d 6t | d d   d 6t | d d   d 6t	 | d d   d 6t	 | d d   d 6} x% d D] } | | s'd | | <q'q'W| d p~| d d k r{t
 t | d d
   p~d | d <| d | d	 f | d <| d | d	 | d <|  j j |  qe Wt |  j d d d   |  _ t |  j d d d   |  _ |  j j   d S)zL
        Parse image from file-like object containing ico file data
           znot an ICO filer.   Nr   r   r   r   r      Znb_color   reservedZplanesbpp   r      r*      color_depthdimsquarekeyc             S   s   |  d S)Nr:   r   )r   r   r   r   r      s    z"IcoFile.__init__.<locals>.<lambda>c             S   s   |  d S)Nr<   r   )r   r   r   r   r      s    )zwidthzheight)r&   r0   SyntaxErrorbufentryi16Znb_itemsrangei8i32r	   r   appendsortedreverse)selfr?   siZicon_headerjr   r   r   __init__S   s>    		

zIcoFile.__init__c             C   s   t  d d   |  j D  S)zJ
        Get a list of all available icon sizes and color depths.
        c             s   s#   |  ] } | d  | d f Vq d S)r   r   Nr   ).0hr   r   r   	<genexpr>   s    z IcoFile.sizes.<locals>.<genexpr>)setr@   )rH   r   r   r   r
      s    zIcoFile.sizesFc             C   sf   xV t  |  j  D]E \ } } | | d k r | d k sH | | d k r |  j |  Sq W|  j d  S)z,
        Get an image from the icon
        r;   Fr:   r   )	enumerater@   frame)rH   r   r6   rJ   rN   r   r   r   getimage   s    ,zIcoFile.getimagec          	   C   s  |  j  | } |  j j | d  |  j j d  } |  j j | d  | d d  t j k ru t j |  j  } nt j |  j  } | j	 d t
 | j	 d d  f | _	 | j d \ } } } } | d | j	 | | f | j d <| d }	 d }
 x8 t j j   D]' } |	 t j | d k r| }
 PqqWd |
 k r|  j j |  |  j j | j	 d | j	 d d  d	 d d  } t j d
 | j	 | d d  } n | j	 d } | d d k r| d | j	 d d 7} n  | t
 | j	 d | j	 d |
 d  } t
 | | j	 d d  } |  j j |  |  j j |  } t j d | j	 | d d t
 | d  d f  } | j d  } | j |  | S)z-
        Get an image from frame idx
        r*   r7   Nr   r   r3   r   r.   r4   Lrawg       @1z1;IZRGBA)r   r   )rT   r   rW   rW   )r@   r?   r%   r&   r   r   ZPngImageFiler   ZDibImageFiler   intZtileZBIT2MODEkeysr   Z
frombufferconvertZputalpha)rH   idxheaderdatar'   deoamoder6   kZalpha_bytesmaskwZand_mask_offsettotal_bytesZmaskDatar   r   r   rR      sT    ' 
5zIcoFile.frameN)__name__
__module____qualname__rL   r
   rS   rR   r   r   r   r   r1   R   s   7	r1   c               @   sF   e  Z d  Z d Z d Z d Z d d   Z d d   Z d d	   Z d
 S)IcoImageFilea9  
    PIL read-only image support for Microsoft Windows .ico files.

    By default the largest resolution image in the file will be loaded. This
    can be changed by altering the 'size' attribute before calling 'load'.

    The info dictionary has a key 'sizes' that is a list of the sizes available
    in the icon file.

    Handles classic, XP and Vista icon formats.

    This plugin is a refactored version of Win32IconImagePlugin by Bryan Davis
    <casadebender@gmail.com>.
    https://code.google.com/archive/p/casadebender/wikis/Win32IconImagePlugin.wiki
    ZICOzWindows Iconc             C   sM   t  |  j  |  _ |  j j   |  j d <|  j j d d |  _ |  j   d  S)Nr
   r   r;   )r1   r(   icor
   infor@   r   load)rH   r   r   r   _open  s    zIcoImageFile._openc             C   sG   |  j  j |  j  } | j   | j |  _ | j |  _ | j |  _ d  S)N)rk   rS   r   rm   r'   rb   )rH   r'   r   r   r   rm   
  s
    
zIcoImageFile.loadc             C   s   d  S)Nr   )rH   r   r   r   	load_seek  s    zIcoImageFile.load_seekN)	rg   rh   ri   __doc__formatZformat_descriptionrn   rm   ro   r   r   r   r   rj      s   rj   z.ico)r   ior   ZPILr   r   r   r   r   mathr   r	   __version__rC   Zi16lerA   Zi32lerD   r   r-   r0   objectr1   rj   Zregister_openrq   Zregister_saveZregister_extensionr   r   r   r   <module>   s   (			#(