
    e.                       d dl m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	m
Z
  e
j                  d      ZerddlmZ dZdZd	 Zd
 Zd Zd Zd Z G d d      Z G d dej                        Zd Zd Z ej2                  ej4                  ee        ej6                  ej4                  d        ej8                  ej4                  e        ej:                  ej4                  d       edk(  r eej@                        dk  r e!d        ejD                           e#ej@                  d   d      5 Z$ ee$      Z%e%jL                  d   D ]*  Z'e'x\  Z(Z)Z*e%_'        e%jW                  de( de) de* d       ,  ejF                  ej@                  d         5 Z,e,jW                  d       ddd       ejZ                  dk(  r ej\                  d       ddd       yy# 1 sw Y   4xY w# 1 sw Y   yxY w)     )annotationsN   )Image	ImageFilePngImagePluginfeaturesjpg_2000)Jpeg2KImagePlugins   icns   c                T    t        j                  d| j                  t                    S )Nz>4sI)structunpackread
HEADERSIZE)fobjs    5/usr/lib/python3/dist-packages/PIL/IcnsImagePlugin.py
nextheaderr   $   s    ==:!677    c                    |\  }}| j                  |       | j                  d      }|dk7  rd}t        |      t        | |dz   |dz
  f|      S )N   s       z'Unknown signature, expecting 0x00000000)seekr   SyntaxErrorread_32)r   start_lengthsizestartlengthsigmsgs          r   read_32tr    (   sY    "OUFIIe
))A,C
!!7#4%!)VaZ0$77r   c           
     B   |\  }}| j                  |       |d   |d   z  |d   |d   z  f}|d   |d   z  }||dz  k(  r0| j                  |      }t        j                  d||dddd      }d|iS t        j                  d|d      }t        d      D ]  }	g }
|}|dkD  r| j                  d      }|snx|d   }|dz  r8|d	z
  }| j                  d      }t        |      D ]  }|
j                  |        n%|dz   }|
j                  | j                  |             ||z  }|dk  rn|dkD  r|dk7  rd
t        |       d}t        |      t        j                  d|dj                  |
      dddd      }|j                  j                  |j                  |	       
 d|iS )zn
    Read a 32bit RGB icon resource.  Seems to be either uncompressed or
    an RLE packbits-like scheme.
    r      r      RGBrawN   }   zError reading channel [z left]Lr   )r   r   r   
frombuffernewrangeappendreprr   joinimputband)r   r   r   r   r   
pixel_sizesizesqindatar/   band_ixdata	bytesleftbyte	blocksizeir   bands                    r   r   r   3   s   
 #OUFIIeq'DG#T!WtAw%67J]Z]*F!6"eZq!L8 2;3 YYuj$/Qx 	,GDIa-yy|Aw$; $s
I99Q<D"9- *D)* !%qIKK		) 45Y&	> a-  A~/Y/@G!#&&##CSXXd^UCQRTUVDEEMM$''7+/	,0 2;r   c           	         |d   }| j                  |       |d   |d   z  |d   |d   z  f}|d   |d   z  }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   r1   r2   r:   s          r   read_mkr=   ^   s|    OEIIeq'DG#T!WtAw%67J]Z]*FCTYYv->sAqQD;r   c                z   |\  }}| j                  |       | j                  d      }|d d dk(  rI| j                  |       t        j                  |       }t	        j
                  |j                         d|iS |d d dk(  s|d d dk(  s|dk(  rt        sd	}t        |      | j                  |       | j                  |      }t        j                  |      }	t        j                  |	      }t	        j
                  |j                         |j                  dk7  r|j                  d      }d|iS d
}t        |      )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   PngImageFiler   _decompression_bomb_checkr   enable_jpeg2k
ValueErrorioBytesIOr
   Jpeg2KImageFilemodeconvert)
r   r   r   r   r   r   r/   r   
jp2kstreamfs
             r   read_png_or_jpeg2000rL   h   s(   "OUFIIe
))B-C
2Aw,,		%((.''0|BQ&&r7))996  S/!		%YYv&
JJz"..q1''077fF#B|0or   c                      e Zd Zdefgdefgdefgdefgdefgdefdefdefgd	efgd
efgdefdefgdefdefdefgdefgdefdefdefgdZd Zd Z	d Z
d ZddZy)IcnsFile   ic10   ic09   ic14   ic08   ic13   ic07s   it32s   t8mks   icp6   ic12s   ih32s   h8mks   icp5s   il32s   l8mk   ic11s   icp4s   is32s   s8mk))   rW   r"   )rW   rW   r   )   rX   r"   )rX   rX   r   )r&   r&   r"   )r&   r&   r   )@   rY   r   )    rZ   r"   )0   r[   r   )rZ   rZ   r   )   r\   r"   )r\   r\   r   c                P   i x| _         }|| _        t        |      \  }}t        |      sd}t	        |      t
        }||k  ret        |      \  }}|dk  rd}t	        |      |t
        z  }|t
        z  }||f||<   |j                  |t        j                         ||z  }||k  rdyy)z@
        fobj is a file-like object as an icns resource
        znot an icns filer   zinvalid block headerN)	dctr   r   _acceptr   r   r   rE   SEEK_CUR)selfr   r^   r   filesizer   r9   r8   s           r   __init__zIcnsFile.__init__   s    
 3	"4(Xs|$Cc""(l'-NCA~,!#&&OA#I9~CHIIi-NA (lr   c                    g }| j                   j                         D ]0  \  }}|D ]&  \  }}|| j                  v s|j                  |        0 2 |S N)SIZESitemsr^   r,   )ra   sizesr   fmtsfmtreaders         r   	itersizeszIcnsFile.itersizes   s[    ****, 	JD$# V$((?LL&	
 r   c                V    | j                         }|sd}t        |      t        |      S )NzNo 32bit icon resources found)rl   r   max)ra   rh   r   s      r   bestsizezIcnsFile.bestsize   s+     1Cc""5zr   c                    i }| j                   |   D ]F  \  }}| j                  j                  |      }|$|j                   || j                  ||             H |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.
        )rf   r^   getupdater   )ra   r   r^   coderk   descs         r   dataforsizezIcnsFile.dataforsize   s]      JJt, 	:LD&88<<%D

6$))T489	: 
r   Nc                2   || j                         }t        |      dk(  r|d   |d   df}| j                  |      }|j                  dd       }|r|S |j                  d      j	                         }	 |j                  |d          |S # t        $ r Y |S w xY w)Nr"   r   r   r@   r$   r<   )ro   lenru   rq   copyputalphaKeyError)ra   r   channelsr/   s       r   getimagezIcnsFile.getimage   s    <==?Dt9>GT!Wa(D##D)\\&$'I\\% %%'	KK& 	  			s   3B	 		BBre   )__name__
__module____qualname__rL   r    r=   r   rf   rc   rl   ro   ru   r|    r   r   rN   rN      s     "678 "678 "678 "678 "678*+hg

  456 456(7G*<=*+gg

  456*+gg
)E6.r   rN   c                  X    e Zd ZdZdZdZd Zed        Zej                  d        Zd Z
y)	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.
    ICNSzMac OS icns resourcec                X   t        | j                        | _        d| _        | j                  j	                         | j
                  d<   | j                  j                         | _        | j                  d   | j                  d   z  | j                  d   | j                  d   z  f| _        y )Nr@   rh   r   r"   r   )	rN   fpicns_moderl   inforo   	best_sizer   ra   s    r   _openzIcnsImageFile._open   s    TWW%	
!YY002		'++-NN1q 11NN1q 11
	r   c                    | j                   S re   )_sizer   s    r   r   zIcnsImageFile.size	  s    zzr   c                   |}|| j                   d   vrt        |      dk(  r|d   |d   df}|| j                   d   vrkt        |      dk(  r]|d   dk(  rU| j                   d   D cg c]  }|d   |d   z  |d   |d   z  f }}||v r!| j                   d   |j                  |         }|| j                   d   vrd}t        |      || _        y c c}w )Nrh   r"   r   r   r#   z2This is not one of the allowed sizes of this image)r   rw   indexrD   r   )ra   value	info_sizer   simple_sizesr   s         r   r   zIcnsImageFile.size  s    	DIIg..3y>Q3F"1y|Q7ITYYw//I!#!! DH99WCU;?a47"DGd1g$56L  $ IIg.|/A/A%/HI	DIIg..FCS/!
s   %Cc                n   t        | j                        dk(  rT| j                  | _        | j                  d   | j                  d   z  | j                  d   | j                  d   z  f| _        t        j                  j	                  |       }| j
                  %| j
                  j                  | j                  k(  r|S | j                          | j                  j                  | j                        }|j	                         }|j
                  | _        |j                  | _
        |j                  | _        |S )Nr#   r   r"   r   )rw   r   r   r   loadr/   load_preparer   r|   rH   r   )ra   pxr/   s      r   r   zIcnsImageFile.load!  s    tyy>Q!YYDNq!DNN1$55q!DNN1$55DI
 [[d#77477<<499#<IYY/ WWY%%WW
GG		r   N)r}   r~   r   __doc__formatformat_descriptionr   propertyr   setterr   r   r   r   r   r      sJ     F/
   
[[ &r   r   c           	        t        |d      r|j                          ddddddddd}| j                  j                  d	g       D  ci c]  } | j                  |  }} i }t        |j                               D ]W  }||v r||   n j                  ||f      }t        j                         }|j                  |d
       |j                         ||<   Y g }	|j                         D ]/  \  }
}||   }|	j                  |
t        t        |      z   |d       1 |j!                  t"               t        }|t        dt        |	      z  z   z  }|t%        d |	D              z  }|j!                  t'        j(                  d|             |j!                  d       |j!                  t'        j(                  dt        t        |	      t        z  z                |	D ]>  }|j!                  |d          |j!                  t'        j(                  d|d                @ |	D ]R  }|j!                  |d          |j!                  t'        j(                  d|d                |j!                  |d          T t        |d      r|j                          yyc c} w )za
    Saves the image as a series of PNG files,
    that are then combined into a .icns file.
    flushr&   rX   rW   i   rZ   rY   )rT   rR   rP   rO   rV   rU   rS   rQ   append_imagespng)typer   streamr   c              3  &   K   | ]	  }|d      yw)r   Nr   ).0entrys     r   	<genexpr>z_save.<locals>.<genexpr>e  s     :uV}:s   z>is   TOC r   r   r   N)hasattrr   encoderinforq   widthsetvaluesresizerE   rF   savegetvaluerg   r,   r   rw   writeMAGICsumr   pack)r/   r   filenamerh   provided_imagessize_streamsr   imagetempentriesr   r   file_lengthr   s                 r   _saver   ;  sS   
 r7

 	E /1nn.@.@RT.UVrxx|VOVLELLN# 	- & D!D$<( 	 zz|

4!]]_T	- Gkkm 

dd#:F#;vN	

 HHUOK:CL 000K3:':::KHHV[[{+, HHWHHV[[zCL:,EEFG 3
v
T5=123
  "
v
T5=12
x!"
 r7

 Q Ws   Jc                    | d d t         k(  S )Nr   )r   )prefixs    r   r_   r_   y  s    "1:r   z.icnsz
image/icns__main__r"   z)Syntax: python3 IcnsImagePlugin.py [file]rbrh   zout--z.pngzout.pngwindows)/
__future__r   rE   osr   sys r   r   r   r   check_codecrC   r
   r   r   r   r    r   r=   rL   rN   r   r   r_   register_openr   register_extensionregister_saveregister_mimer}   rw   argvprintexitopenr   imfr   r   r   heightscaler   r/   platform	startfiler   r   r   <module>r      s  & # 	 	  
 8 8$$$Z0#
88(VD` `NGI'' GT;|   M((- A   --w 7   M((% 0   M((, 7z
388}q9:
	chhqk4	  $BBHHW% 	:D.22 E6538HHtE7!F81UG489	: UZZ$ 	GGI	<<9$BLL#$ $ 	 	$ $s%   3A$G(G))G(G%	!G((G1