U
    &^'                     @   s   d dl Z d dlZd dlmZ d dlmZmZ ddlmZm	Z	m
Z
mZ ddlmZmZmZ dZdd	 Zd
d ZG dd dZG dd de
j
Ze	ejee e	eje e	ejd e	ejd dS )    N)BytesIO)ceillog   )BmpImagePluginImage	ImageFilePngImagePlugin)i8i16lei32les      c              
      s  | t | jddddddddg}| j\ t fd	d
|}t|}| tdt	| |
 t	|d  }|D ]}|\ | tddk rnd | td dk r nd | d | d | d | tdd t }|  }|j|tjd d ||d |d | }t	|}	| td|	 | td| |
 }
|| | | ||	 }||
 qzd S )Nsizes)   r   )   r   )    r   )0   r   )@   r   )   r   )   r   c                    s8   | d ks0| d  ks0| d dks0| d dkr4dS dS )Nr   r   r   FT xheightwidthr   6/tmp/pip-install-a1j0c_p1/Pillow/PIL/IcoImagePlugin.py<lambda>/   s    



z_save.<locals>.<lambda>z<Hr   Br   r       s     r   )Zreducing_gapZpngz<I)write_MAGICZencoderinfogetsizefilterlist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   _save'   sF    








r3   c                 C   s   | d d t kS )N   )r    )prefixr   r   r   _acceptR   s    r6   c                   @   s8   e Zd Zdd Zdd ZdddZddd	Zd
d ZdS )IcoFilec                 C   sj  | d}t|std|| _g | _t|dd | _t| jD ]}| d}t|d t|d t|d t|d	 t|dd t|dd t	|d
d t	|dd d}dD ]}|| sd||< q|d p|d dkrt
t|d dpd|d< |d |d f|d< |d |d  |d< | j| qBt| jdd d| _t| jdd d| _| j  dS )zL
        Parse image from file-like object containing ico file data
           znot an ICO filer4   Nr   r   r               )r   r   nb_colorreservedZplanesbppr"   r0   r   r   r   r?   r=   color_depthr   r   dimsquarec                 S   s   | d S )NrA   r   r   r   r   r   r          z"IcoFile.__init__.<locals>.<lambda>)keyc                 S   s   | d S )NrC   r   r   r   r   r   r      rD   )r,   r6   SyntaxErrorbufentryi16Znb_itemsranger
   i32r   r   appendsortedreverse)selfrG   siZicon_headerjr   r   r   __init__W   s@    






	zIcoFile.__init__c                 C   s   dd | j D S )zJ
        Get a list of all available icon sizes and color depths.
        c                 S   s   h | ]}|d  |d fqS r@   r   ).0hr   r   r   	<setcomp>   s     z IcoFile.sizes.<locals>.<setcomp>)rH   rO   r   r   r   r      s    zIcoFile.sizesFc                 C   s@   t | jD ]0\}}||d kr
|dks2||d kr
|  S q
dS )NrB   FrA   r   )	enumeraterH   )rO   r"   r?   rQ   rU   r   r   r   getentryindex   s     
zIcoFile.getentryindexc                 C   s   |  | ||S )z,
        Get an image from the icon
        )framerY   )rO   r"   r?   r   r   r   getimage   s    zIcoFile.getimagec              
   C   s  | j | }| j|d  | jd}| j|d  |dd tjkrXt| j}nt| j}t	
|j |jd t|jd d f|_|jd \}}}}|d|j ||f|jd< |d }	d}
tj D ]\}}|	|d kr|}
 qqd|
krB| j| | j|jd |jd  d	 d
dd	 }t	d|j|dd}n|jd }|d dkrp|d|jd d  7 }|t|jd |jd  |
d   }t||jd  d }| j| | j|}t	d|j|ddt|d df}|d}|| |S )z-
        Get an image from frame idx
        r0   r;   Nr   r   r9   )r   r   r   r4   r:   Lraw)r\   r   g       @1z1;Ir^   ZRGBA)rH   rG   r+   r,   r	   r    ZPngImageFiler   ZDibImageFiler   Z_decompression_bomb_checkr"   int_sizeZtileZBIT2MODEitemsZ
frombufferconvertZputalpha)rO   idxheaderdatar-   deoamoder?   kvZalpha_bytesmaskwZand_mask_offsettotal_bytesZ	mask_datar   r   r   rZ      sX    

*	
$

zIcoFile.frameN)F)F)__name__
__module____qualname__rS   r   rY   r[   rZ   r   r   r   r   r7   V   s
   :

r7   c                   @   sJ   e Zd ZdZdZdZdd Zedd Zej	dd Zd	d
 Z
dd ZdS )IcoImageFilea  
    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.

    When saving, PNG compression is used. Support for this was only added in
    Windows Vista.

    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   s:   t | j| _| j | jd< | jjd d | _|   d S )Nr   r   rB   )r7   r.   icor   inforH   r"   loadrW   r   r   r   _open  s    zIcoImageFile._openc                 C   s   | j S N)ra   rW   r   r   r   r"     s    zIcoImageFile.sizec                 C   s    || j d krtd|| _d S )Nr   z2This is not one of the allowed sizes of this image)rv   
ValueErrorra   )rO   valuer   r   r   r"     s    c                 C   s   | j r| j j| jkrd S | j| j}|  |j | _ |j| _|j| jkrtd | j| j}t	| j
d }|j||< t|| j
d< |j| _d S )NzImage was not the expected sizer   )r-   r"   ru   r[   rw   rk   warningswarnrY   r$   rv   set)rO   r-   indexr   r   r   r   rw   #  s    

zIcoImageFile.loadc                 C   s   d S ry   r   rW   r   r   r   	load_seek6  s    zIcoImageFile.load_seekN)rq   rr   rs   __doc__formatformat_descriptionrx   propertyr"   setterrw   r   r   r   r   r   rt      s   

rt   z.icozimage/x-icon)r%   r|   ior   mathr   r    r   r   r   r	   _binaryr
   r   rI   r   rK   r    r3   r6   r7   rt   Zregister_openr   Zregister_saveZregister_extensionZregister_mimer   r   r   r   <module>   s   + 'D