a :jg2 @sVddlmZddlZddlZddlZddlZddlmZddlm Z m Z m Z m Z ddl mZe dZervddlmZd Zd Zd d d ddZd ddddddZd ddddddZd ddddddZd ddddddZGdddZGddde j Zd d d!d"d#d$d%Zd&d'd(d)d*Ze ejeee ejd+e ejee !ejd,e"d-krRe#ej$d.kre%d/e&e'ej$dd0Z(ee(Z)e)j*d1D]4Z+e+\Z,Z-Z.e)_+e)/d2e,d3e-d3e.d4qe 'ej$dZ0e0/d5Wdn1s0Yej1d6kr2e2d5Wdn1sH0YdS)7) annotationsN)IO)Image ImageFilePngImagePluginfeatures) deprecateZjpg_2000)Jpeg2KImagePluginsicns IO[bytes]ztuple[bytes, int]fobjreturncCstd|tS)Nz>4sI)structunpackread HEADERSIZE)rr=/usr/local/lib/python3.9/site-packages/PIL/IcnsImagePlugin.py nextheader&srztuple[int, int]tuple[int, int, int]dict[str, Image.Image])r start_lengthsizercCsH|\}}|||d}|dkr0d}t|t||d|df|S)Nsz'Unknown signature, expecting 0x00000000)seekr SyntaxErrorread_32)rrrstartlengthsigmsgrrrread_32t*s  r#c Csp|\}}|||d|d|d|df}|d|d}||dkrp||}td||dddd}ntd|d}tdD]} g} |} | dkr|d} | sq| d} | d@r| d }|d} t|D]}| | qn| d}| ||| |8} | dkrqq| dkr:d t| d }t|td |d | dd dd}|j |j | qd|iS)zn Read a 32bit RGB icon resource. Seems to be either uncompressed or an RLE packbits-like scheme. rrRGBrawN}zError reading channel [z left]L) rrr frombuffernewrangeappendreprrjoinimZputband)rrrrr pixel_sizesizesqZindatar2Zband_ixdataZ bytesleftbyteZbyte_int blocksizeir"bandrrrr7s@          rc Csf|d}|||d|d|d|df}|d|d}td|||dddd}d|iS)Nrr$rr*r'A)rrr,r)rrrrr3r4r9rrrread_mkds   r;c Cs|\}}|||d}|dddkrT||t|}t|jd|iS|dddks||dddks||dkrtsd }t|||||}t |} t | }t|j|j dkr|d}d|iSd }t|dS) N r sPNG  RGBArsOQs s jP zQUnsupported icon subimage format (rebuild PIL with JPEG 2000 support to fix this)z Unsupported icon subimage format)rrrZ PngImageFilerZ_decompression_bomb_checkr enable_jpeg2k ValueErrorioBytesIOr ZJpeg2KImageFilemodeconvert) rrrrr r!r2r"Z jp2kstreamfrrrread_png_or_jpeg2000ps8            rEc@seZdZdefgdefgdefgdefgdefgdefdefdefgd efgd efgd efd efgd efdefdefgdefgdefdefdefgd ZdddddZddddZ dddd Z dd!d"d#d$Z d*d&d'd"d(d)Z d%S)+IcnsFileic10ic09ic14ic08ic13ic07sit32st8mksicp6ic12sih32sh8mksicp5sil32sl8mkic11sicp4sis32ss8mk) )rOr$)rOrOr)rPr$)rPrPr)r(r(r$)r(r(r)@rQr) rRr$)0rSr)rRrRr)rTr$)rTrTrr Noner cCsi|_||_t|\}}t|s,d}t|t}||krt|\}}|dkrXd}t||t7}|t8}||f|j|<||tj||7}q0dS)z@ fobj is a file-like object as an icns resource znot an icns filerzinvalid block headerN) dctrr_acceptrrrr@SEEK_CUR)selfrr!filesizer"r8r7rrr__init__s"  zIcnsFile.__init__zlist[tuple[int, int, int]]rcCsBg}|jD].\}}|D] \}}||jvr||qqq|SN)SIZESitemsrVr/)rYsizesrZfmtsfmtreaderrrr itersizess   zIcnsFile.itersizesrcCs |}|sd}t|t|S)NzNo 32bit icon resources found)rcrmax)rYr`r"rrrbestsizes zIcnsFile.bestsizer)rrcCsDi}|j|D]0\}}|j|}|dur|||j||q|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)r^rVgetupdater)rYrrVcoderbdescrrr dataforsizes  zIcnsFile.dataforsizeNz-tuple[int, int] | tuple[int, int, int] | None Image.ImagecCs|dur|}nt|dkr0|d|ddf}||}|d}|rL|S|d}z||dWnty|Yn0|S)Nr$rrr=r&r:)relenrjrfcopyZputalphaKeyError)rYrZchannelsr2rrrgetimages      zIcnsFile.getimage)N) __name__ __module__ __qualname__rEr#r;rr^r[rcrerjrorrrrrFs8 rFc@s`eZdZdZdZdZddddZeddd d Zej ddd d d ZddddddZ 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. ZICNSzMac OS icns resourcerUr\cCs\t|j|_d|_|j|jd<|j|_|jd|jd|jd|jdf|_dS)Nr=r`rr$r) rFfpicns_modercinfore best_sizerrYrrr_open s  zIcnsImageFile._openz&tuple[int, int] | tuple[int, int, int]cCs|jSr])_sizeryrrrrszIcnsImageFile.size)valuercCst|dkr2tddd||jdvr||_dSn`|jdD]T}|d|d|d|df}|d|d}|d|d|kr<||_dSq[r+z_save..Z append_imagesZpngr css|]}|dVqdS)rNr)rentryrrr qr+z_save..z>isTOC rrr$N)hasattrrZ encoderinforfsetvaluesresizer@rAsavegetvaluer_r/rrlwriteMAGICsumrpack)r2rtrr`Zprovided_imagesZ size_streamsrimagetempentriestypestreamZ file_lengthrrrr_saveIsR        rbytesbool)prefixrcCs|ddtkS)Nr)r)rrrrrWsrWz.icnsz image/icns__main__r$z)Syntax: python3 IcnsImagePlugin.py [file]rbr`zout--z.pngzout.pngwindows)3 __future__rr@osrsystypingrrrrrZ _deprecater Z check_codecr>r rrrr#rr;rErFrsrrWZ register_openrZregister_extensionZ register_saveZ register_mimerprlargvprintexitopenrtZimfrwrrrr}rr2platformZ startfilerrrrsL      - &iJ< "*