a :jg0@s*ddlmZddlZddlmZddlmZmZddlm Z m Z ddl m Z m Z mZmZddlmZdd lmZdd lmZdd lmZdd lmZd ZdddddddZdddddZGddde ZGdddZGdddejZe ej!eee "ej!ee #ej!d e $ej!d!dS)") annotationsN)BytesIO)ceillog)IO NamedTuple)BmpImagePluginImage ImageFilePngImagePlugin)i16le)i32le)o8)o16le)o32les Image.Image IO[bytes]z str | bytesNone)imfpfilenamereturnc Cs|t|jddk}|jdgd}g}|g|jdg}|j\}}tt|D]} | d|ksZ| d|ksZ| ddksZ| ddkrqZ|D]|} | j| krq|| |r tj | j d} | g} |D]<} | j| krqtj | j d} | | vr|| | | qqZq| }|j | t jjdd ||qZ|tt||t|d }|D]}|j\}}|t|dkr|nd|t|dkr|nd|rtj |j ddnd \} }|t||d |d |t| t}|rN||d| dkrZt d| }t||tdd| ddgn ||d|d|}|r|ddt|d|dd}t|}|t||t||}||||||}||qddS)NZ bitmap_formatbmpsizes))r)r) r)0r)@r)r )r!Z append_imagesrrr!)Z reducing_gapr)rrsZdibr1rawrr)r#rZpng )write_MAGICZ encoderinfogetsizesortedsetappendr ZSAVEmodecopyZ thumbnailr Z ResamplingZLANCZOSo16lentellrrsavenewr _save_Tileseekreado32)rrrrrframesZ provided_imswidthheightr-Z provided_imbitsZ bits_usedZother_imframeoffsetcolorsZimage_ioZand_maskZ image_bytesZ bytes_lencurrentrEr?nb_colorreservedplanesbppr-rBtuple[int, int]dimsquare color_depthN)__name__ __module__ __qualname____annotations__rErErErFrL|s rLc@sbeZdZdddddZdddd Zdd d d dddZdd d ddddZd ddddZdS)IcoFilerr)bufrc Cs|d}t|sd}t|||_g|_t|d|_t|jD]}|d}|dpXd}|dpdd}|d}t|d}t||||d t|dt|dt |d t |d ||f|||p|dkrt t |dpdd } |j | q@t |jd dd|_t |jdddd|_dS)zL Parse image from file-like object containing ico file data znot an ICO filerJrrr!rr(r'r)) r>r?rNrOrPrQr-rBrSrTrUcSs|jSN)rUxrErErFz"IcoFile.__init__..)keycSs|jSr^)rTr_rErErFrarbT)rcreverseN)r;rK SyntaxErrorr[entryi16Znb_itemsrangerLi32rrr0r.) selfr[smsgir>r?rNrQZ icon_headerrErErF__init__s:      zIcoFile.__init__zset[tuple[int, int]]rcCsdd|jDS)zI Get a set of all available icon sizes and color depths. cSsh|]}|j|jfqSrE)r>r?).0hrErErF rbz IcoFile.sizes..)rfrjrErErFrsz IcoFile.sizesFrRz int | boolrM)r-rQrcCs<t|jD],\}}||jkr |dus.||jkr |Sq dS)NFr) enumeraterfrSrU)rjr-rQrmrqrErErF getentryindexs zIcoFile.getentryindexrcCs||||S)z, Get an image from the icon )rAru)rjr-rQrErErFgetimageszIcoFile.getimage)idxrc Cs|j|}|j|j|jd}|j|j|ddtjkr`t|j}t |j nt |j}t |j |j dt |j ddf|_|jd\}}}}t|d|j |||jd<|jdkrF|j||j|j d|j ddd dd} ztd |j | d d } Wn$tyBtjr. https://code.google.com/archive/p/casadebender/wikis/Win32IconImagePlugin.wiki ZICOz Windows IconrrocCs8t|j|_|j|jd<|jjdj|_|dS)Nrr) rZricorinforfrSr-loadrsrErErF_openHs zIcoImageFile._openrRcCs|jSr^)ryrsrErErFr-NszIcoImageFile.size)valuercCs$||jdvrd}t|||_dS)Nrz2This is not one of the allowed sizes of this image)rrzry)rjrrlrErErFr-RszImage.core.PixelAccess | NonecCs|jdur$|jj|jkr$tj|S|j|j}||j|_|j|_|j rX|j |_ |j|jkrt d|j |j}t |jd}|j||<t||jd<|j|_dS)NzImage was not the expected sizer)Z_imrr-r rrrvr1_modepalettewarningswarnrulistrr/)rjrindexrrErErFrYs     zIcoImageFile.loadrM)posrcCsdSr^rE)rjrrErErF load_seekoszIcoImageFile.load_seekN) rVrWrX__doc__formatformat_descriptionrpropertyr-setterrrrErErErFr0srz.icoz image/x-icon)% __future__rriormathrrtypingrrr r r r _binaryr rgrrirrr3rr<r+r8rKrLrZrZ register_openrZ register_saveZregister_extensionZ register_mimerErErErFs*       M'I