3
	jQcE+                 @   s  d dl mZmZmZ d dlmZ d dlZd dlZd dlZd dl	Z	d dl
Z
d dlZeejdZerld dl mZ dZdd Zd	d
 Zdd Zdd Zdd ZG dd deZG dd dejZdd Zejejedd  ejejd e
jdkrejeje ejejd e dkre!e
j"dk r6e#d e
j$  ee%e
j"d d Z&x6e&j'd! D ](Z(e(e&_(e&j)  e&j*Z*e*j+d"e(  qVW ej%e
j"d Z*e*j+d# e
jd$krej,d# dS )%    )Image	ImageFilePngImagePlugin)i8NZjp2klib_version)Jpeg2KImagePlugin   c             C   s   t jd| jtS )Nz>4sI)structZunpackread
HEADERSIZE)fobj r   5/usr/lib/python3/dist-packages/PIL/IcnsImagePlugin.py
nextheader"   s    r   c             C   sD   |\}}| j | | jd}|dkr,tdt| |d |d f|S )N   s       z'Unknown signature, expecting 0x00000000)seekr	   SyntaxErrorread_32)r   start_lengthsizestartlengthsigr   r   r   read_32t&   s    

r   c          	   C   sl  |\}}| j | |d |d  |d |d  f}|d |d  }||d krp| j|}tjd||dddd}ntjd|d}xtdD ]}	g }
|}x|dkr| jd}|sP t|}|d@ r|d	 }| jd}x4t|D ]}|
j| qW n|d }|
j| j| ||8 }|dkrP qW |dkr4td
| tjd|dj	|
dddd}|j
j|j
|	 qW d|iS )zn
    Read a 32bit RGB icon resource.  Seems to be either uncompressed or
    an RLE packbits-like scheme.
    r            RGBrawN   }   zError reading channel [%r left]L    )r   r	   r   
frombuffernewranger   appendr   joinimZputband)r   r   r   r   r   
pixel_sizesizesqZindatar'   Zband_ixdataZ	bytesleftZbyte	blocksizeibandr   r   r   r   0   sB    
 




r   c             C   sf   |d }| j | |d |d  |d |d  f}|d |d  }tjd|| j|dddd}d|iS )Nr   r   r   r    r   A)r   r   r"   r	   )r   r   r   r   r(   r)   r-   r   r   r   read_mk^   s    
 r/   c       	      C   s   |\}}| j | | jd}|d d dkrT| j | tj| }tj|j d|iS |d d dks||d d dks||dkrtstd	| j | | j|}t	j
|}tj|}tj|j |jdkr|jd}d|iS td
d S )N   r   s   PNG

RGBAr   s   OQs   

s      jP  

zQUnsupported icon subimage format (rebuild PIL with JPEG 2000 support to fix this)z Unsupported icon subimage format)r   r	   r   ZPngImageFiler   Z_decompression_bomb_checkr   enable_jpeg2k
ValueErrorioBytesIOr   ZJpeg2KImageFilemodeZconvert)	r   r   r   r   r   r   r'   Z
jp2kstreamfr   r   r   read_png_or_jpeg2000j   s,    









r8   c               @   s   e Zd Zd(defgd)defgd*defgd+defgd,d
efgd-defdefdefgd.defgd/defgd0defdefgd1defdefdefgd2defgd3defdefdefgiZdd Zdd  Z	d!d" Z
d#d$ Zd4d&d'Zd%S )5IcnsFile   r   s   ic10r   s   ic09   s   ic14s   ic08r   s   ic13s   ic07s   it32s   t8mk@   s   icp6    s   ic120   s   ih32s   h8mks   icp5s   il32s   l8mk   s   ic11s   icp4s   is32s   s8mkc             C   s   i  | _ }|| _t|\}}|dkr,tdt}xX||k rt|\}}|dkrVtd|t7 }|t8 }||f||< |j|d ||7 }q2W dS )z@
        fobj is a file-like object as an icns resource
        s   icnsznot an icns filer   zinvalid block headerr   N)dctr   r   r   r
   r   )selfr   r@   r   Zfilesizer,   r+   r   r   r   __init__   s    

zIcnsFile.__init__c             C   sH   g }x>| j j D ]0\}}x&|D ]\}}|| jkr|j| P qW qW |S )N)SIZESitemsr@   r%   )rA   sizesr   ZfmtsZfmtreaderr   r   r   	itersizes   s    


zIcnsFile.itersizesc             C   s   | j  }|stdt|S )NzNo 32bit icon resources found)rG   r   max)rA   rE   r   r   r   bestsize   s    zIcnsFile.bestsizec             C   sH   i }x>| j | D ]0\}}| jj|}|dk	r|j|| j|| qW |S )z
        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)rC   r@   getupdater   )rA   r   r@   coderF   Zdescr   r   r   dataforsize   s    zIcnsFile.dataforsizeNc             C   s   |d kr| j  }t|dkr.|d |d df}| j|}|jdd }|rL|S |jdj }y|j|d  W n tk
r   Y nX |S )Nr   r   r   r1   r   r.   )rI   lenrM   rJ   copyZputalphaKeyError)rA   r   Zchannelsr'   r   r   r   getimage   s    
zIcnsFile.getimage)r:   r:   r   )r:   r:   r   )r;   r;   r   )r;   r;   r   )r   r   r   )r   r   r   )r<   r<   r   )r=   r=   r   )r>   r>   r   )r=   r=   r   )r?   r?   r   )r?   r?   r   )N)__name__
__module____qualname__r8   r   r/   r   rC   rB   rG   rI   rM   rQ   r   r   r   r   r9      sH   	r9   c               @   s(   e Zd ZdZdZdZdd Zdd ZdS )	IcnsImageFilea!  
    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.
    ZICNSzMac OS icns resourcec             C   sb   t | j| _d| _| jj | _| jd | jd  | jd | jd  f| _| jj | jd< d| _	d S )Nr1   r   r   r   rE    )rV   )
r9   fpicnsr6   rI   	best_sizer   rG   infotile)rA   r   r   r   _open  s    zIcnsImageFile._openc             C   s   t | jdkr@| j| _| jd | jd  | jd | jd  f| _tjj|  | jsVd S | j  | jj| j}|j  |j	| _	|j
| _
|j| _d | _d | _f | _| j  d S )Nr   r   r   r   )rN   r   rY   r   loadr[   Zload_preparerX   rQ   r'   r6   rW   Zload_end)rA   r'   r   r   r   r]     s"    zIcnsImageFile.loadN)rR   rS   rT   __doc__formatZformat_descriptionr\   r]   r   r   r   r   rU      s
   	
rU   c             C   sb  t |dr|j  tjd}dd | jjdg D }d}xdD ]}dj||}tjj	||d }||krvt
j|	| n$|j|| j||ftj}
|
j| tjj	||d }	|j|d | j|d |d ftj}|j|	 |d }q>W ddlm}m}m} dddd||g}ttjd}||||d}W dQ R X |jj  |j }t
j| |r^|||dS )z
    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.
    flushz.iconsetc             S   s   i | ]}||j qS r   )width).0r'   r   r   r   
<dictcomp>;  s    z_save.<locals>.<dictcomp>Zappend_imagesNr?   r=   r   r;      z
icon_{}x{}z.pngz@2x.pngr   r   )PopenPIPECalledProcessErrorZiconutilz-crX   z-owb)stdoutstderr)r?   r=   r   r;   rd   )hasattrr`   tempfileZmkdtempZencoderinforJ   r_   ospathr&   shutilZcopyfileZresizer   ZLANCZOSsave
subprocessre   rf   rg   opendevnullri   closewaitZrmtree)r'   rW   filenameZiconsetZprovided_imagesZlast_wwprefixZ
first_pathZsecond_pathZim_wZim_w2re   rf   rg   Zconvert_cmdrs   Zconvert_procZretcoder   r   r   _save.  s4    



&


ry   c             C   s   | d d dkS )Nr   s   icnsr   )xr   r   r   <lambda>`  s    r{   z.icnsdarwinz
image/icns__main__r   z(Syntax: python IcnsImagePlugin.py [file]r   rbrE   zout-%s-%s-%s.pngzout.pngZwindows)-ZPILr   r   r   ZPIL._binaryr   r4   rm   ro   r   sysrl   rk   Zcorer2   r   r
   r   r   r   r/   r8   objectr9   rU   ry   Zregister_openr_   Zregister_extensionplatformZregister_saveZregister_mimerR   rN   argvprintexitrr   ZimfrZ   r   r]   r'   rp   Z	startfiler   r   r   r   <module>   sN   
.w11



