o �J�h�B�@s�dZddlmZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z mZGd d �d e�ZGd d �d e�ZGd d�de�Zd=dd�Z d>d?dd�Zd@dd�Zd@dd �ZGd!d"�d"e�ZdAd$d%�ZGd&d'�d'ej�ZGd(d)�d)ej�ZGd*d+�d+e�ZGd,d-�d-e�ZGd.d/�d/ej�ZdBd7d8�Z e �!ej"ee�e �#ej"d9�e �$d:e�e �$d;e�e �%ej"e �e �&d<e�dS)Ca� Blizzard Mipmap Format (.blp) Jerome Leclanche <jerome@leclan.ch> The contents of this file are hereby released in the public domain (CC0) Full text of the CC0 license: https://creativecommons.org/publicdomain/zero/1.0/ BLP1 files, used mostly in Warcraft III, are not fully supported. All types of BLP2 files used in World of Warcraft are supported. The BLP file structure consists of a header, up to 16 mipmaps of the texture Texture sizes must be powers of two, though the two dimensions do not have to be equal; 512x256 is valid, but 512x200 is not. The first mipmap (mipmap #0) is the full size image; each subsequent mipmap halves both dimensions. The final mipmap should be 1x1. BLP files come in many different flavours: * JPEG-compressed (type == 0) - only supported for BLP1. * RAW images (type == 1, encoding == 1). Each mipmap is stored as an array of 8-bit values, one per pixel, left to right, top to bottom. Each value is an index to the palette. * DXT-compressed (type == 1, encoding == 2): - DXT1 compression is used if alpha_encoding == 0. - An additional alpha bit is used if alpha_depth == 1. - DXT3 compression is used if alpha_encoding == 1. - DXT5 compression is used if alpha_encoding == 7. �)� annotationsN)�IntEnum)�BytesIO)�IO�)�Image� ImageFilec@seZdZdZdS)�FormatrN)�__name__� __module__� __qualname__�JPEG�rr�PC:\pinokio\api\whisper-webui.git\app\env\lib\site-packages\PIL\BlpImagePlugin.pyr ,sr c@�eZdZdZdZdZdS)�Encodingr��N)r r r � UNCOMPRESSED�DXTZUNCOMPRESSED_RAW_BGRArrrrr0�rc@r)� AlphaEncodingrr�N)r r r �DXT1�DXT3�DXT5rrrrr6rr�i�int�return�tuple[int, int, int]cCs*|d?d@d>|d?d@d>|d@d>fS)N� �r��?rr)rrrr� unpack_565<s*r$F�data�bytes�alpha�bool�1tuple[bytearray, bytearray, bytearray, bytearray]c Cs�t|�d}t�t�t�t�f}t|�D]�}|d}t�d||�\}}}t|�\} } } t|�\} } }td�D]�}td�D]�}|d@}|d?}d}|dkrU| | | }}}nh|dkrb| | |}}}n[|dkr�||kr�d| | d}d| | d}d| |d}n:| | d}| | d}| |d}n'|dkr�||kr�d| | d}d| | d}d|| d}nd \}}}}|r�||�||||g�q<||�|||g�q<q6q|S) �E input: one "row" of data (i.e. will produce 4*width pixels) �z<HHI�rr��rr)rrrr��len� bytearray�range�struct� unpack_fromr$�extend)r%r'�blocks�ret� block_index�idx�color0�color1�bits�r0�g0�b0�r1�g1�b1�jr�control�a�r�g�brrr� decode_dxt1@sH       ��"rHc Cs�t|�d}t�t�t�t�f}t|�D]�}|d}|||d�}t�d|�}t�d|d�\}}t�d|d�\} t|�\} } } t|�\} }}td�D]�}d}td�D]�}d||d }||}|rjd}|dL}nd }|d M}|d 9}| d d||?d @}|dkr�| | | }}}nF|dkr�| ||}}}n9|d kr�d | | d }d | |d }d | |d }n|d kr�d | | d }d || d }d || d }||�||||g�qSqKq|S)r*�z<8B�<HHr+�<I� r,FrT��rrrr.)r%r5r6r7r8�blockr;r9r:�coder<r=r>r?r@rArB�highr�alphacode_indexrD� color_coderErFrGrrr� decode_dxt3ysL      ��rTc Cs�t|�d}t�t�t�t�f}t|�D�]>}|d}|||d�}t�d|�\}}t�d|d�}|d|dd>B|dd>B|dd >B} |d |d d>B} t�d |d�\} } t�d |d�\} t| �\}}}t| �\}}}td�D]�}td�D]�}dd||}|dkr�| |?d@}n|dkr�| d?| d >d@B}n| |d?d@}|d kr�|}n6|d kr�|}n/||kr�d|||d |d}n|dkr�d }n|dkr�d}nd|||d |d}| dd||?d@}|d kr�|||}}}nI|d k�r |||}}}n;|dk�r)d||d}d||d}d||d}n|dk�rFd||d}d||d}d||d}||�||||g�q{quq|S)zG input: one "row" of data (i.e. will produce 4 * width pixels) rIz<BBz<6Brrr+r,r"�rrrJrKrLrrM�r-r.)r%r5r6r7r8rO�a0�a1r;Z alphacode1Z alphacode2r9r:rPr<r=r>r?r@rArBrrRZ alphacoderDrSrErFrGrrr� decode_dxt5�sb ,     ��*rYc@s eZdZdS)�BLPFormatErrorN)r r r rrrrrZ�srZ�prefixcCs|dd�dvS)Nr,��BLP1�BLP2r)r[rrr�_accept�sr_c@s"eZdZdZdZdZd dd�ZdS) � BlpImageFilez Blizzard Mipmap Format �BLPzBlizzard Mipmap Formatr�NonecCs�|j�d�|_|j�dtj�t�d|j�d��\|_|j�dtj�t�d|j�d��|_ |jdvr9|j� �}n d t |j���}t |��|jrJd nd |_ t�|d |jd |jd df�g|_dS)Nr,r"�<brr�<IIr+r\zBad BLP magic �RGBA�RGB�rrr)�fp�read�magic�seek�os�SEEK_CURr2�unpack�_blp_alpha_depth�_size�decode�reprrZ�_moder�_Tile�size�mode�tile)�self�decoder�msgrrr�_opens  &zBlpImageFile._openN�rrb)r r r �__doc__�format�format_descriptionr{rrrrr`�s r`c@sReZdZdZddd�Zejdd d ��Zdd d �Zddd�Z ddd�Z ddd�Z dS)�_BLPBaseDecoderT�buffer�$bytes | Image.SupportsArrayInterfacer�tuple[int, int]c Cs@z |��|��WdStjy}zd}t|�|�d}~ww)NzTruncated BLP file)�����r)�_read_blp_header�_loadr2�error�OSError)rxr��erzrrrrqs � ��z_BLPBaseDecoder.decoderbcCsdS�Nr�rxrrrr�#sz_BLPBaseDecoder._loadcCs�|jdusJ�|j�d�t�d|�d��\|_t�d|�d��\|_t�d|�d��\|_t�d|�d��\|_|j�dt j �t�d|�d��|_ t |t �rct�d|�d��\|_|j�dt j �t�d|�d��|_t�d|�d��|_dS) Nr,�<ircrrdr+�<16I�@)�fdrkr2rn� _safe_read�_blp_compression� _blp_encodingro�_blp_alpha_encodingrlrmru� isinstance� BLP1Decoder� _blp_offsets� _blp_lengthsr�rrrr�'s  z _BLPBaseDecoder._read_blp_header�lengthrr&cCs|jdusJ�t�|j|�Sr�)r�rr�)rxr�rrrr�;sz_BLPBaseDecoder._safe_read�list[tuple[int, int, int, int]]c Cs`g}td�D]'}zt�d|�d��\}}}}Wn tjy#Y|Sw|�||||f�q|S)N��<4Br,)r1r2rnr�r��append)rxr6rrGrFrErDrrr� _read_palette?s �z_BLPBaseDecoder._read_palette�paletter0c Cs�t�}t|�|jd��} z t�d|�d��\}Wn tjy%Y|Sw||\}}}}|||f} |jr;| |f7} |� | �q)NrT�<Br) r0rr�r�r2rnrir�ror4) rxr�r%�_data�offsetrGrFrErD�drrr� _read_bgraIs�   �z_BLPBaseDecoder._read_bgraN)r�r�rr�r|)r�rrr&)rr�)r�r�rr0) r r r � _pulls_fdrq�abc�abstractmethodr�r�r�r�r�rrrrr�s      r�c@s eZdZddd�Zddd�ZdS) r�rrbcCs||jtjkr |��dS|jdkr2|jdvr&|��}|�|�}|�|�dSdt|j���}t |��dt|j���}t |��)Nr)r,r"zUnsupported BLP encoding zUnsupported BLP compression ) r�r r �_decode_jpeg_streamr�r�r�� set_as_rawrrrZ)rxr�r%rzrrrr�Zs     zBLP1Decoder._loadcCsddlm}t�d|�d��\}|�|�}|jdusJ�|�|jd|j���|�|jd�}||}|t |��}t � |j �|j dkrd|jd\}}}} t| t�sVJ�t�|||| ddf�g|_|�d���\} } } t �d| | | f�} |�| ���dS)Nr)� JpegImageFilerKr,r�CMYKrf)�JpegImagePluginr�r2rnr�r�r��tellr�rr�_decompression_bomb_checkrurvrwr��tuplerrt�convert�split�merger��tobytes)rxr�Zjpeg_header_sizeZ jpeg_headerr%�image� decoder_name�extentsr��argsrErFrGZreversed_imagerrrr�js"     �zBLP1Decoder._decode_jpeg_streamNr|)r r r r�r�rrrrr�Ys r�c@seZdZddd�ZdS)� BLP2DecoderrrbcCs�|��}|jdus J�|j�|jd�|jdkr�|jtjkr%|�|�}n�|jtj kr�t �}|j t j kra|jdddd}t|jddd�D]}t|�|�t|j�d�D]}||7}qXqJn�|j t jkr�|jdddd}t|jddd�D]}t|�|��D]}||7}q�q}nR|j t jkr�|jdddd}t|jddd�D]}t|�|��D]}||7}q�q�n$dt|j ���}t|��d t|j���}t|��d t|j���}t|��|�|�dS) Nrrrr,r+)r'rIzUnsupported alpha encoding zUnknown BLP encoding zUnknown BLP compression )r�r�rkr�r�r�rrr�rr0r�rrrur1rHr�r(rorrTrrYrrrZr�)rxr�r%ZlinesizeZybr�rzrrrr��sL      � ��  ��  ��zBLP2Decoder._loadNr|)r r r r�rrrrr��sr�c@s$eZdZdZd dd�Zd d d �Zd S)� BLPEncoderTrr&c Cs�d}|jdus J�|j�dd�}tt|�d�D]}||d|dd�\}}}}|t�d||||�7}qt|�dkrF|d7}t|�dks<|S)N�rer,rr�is)�im� getpaletter1r/r2�pack)rxr%r�rrErFrGrDrrr�_write_palette�s   �zBLPEncoder._write_palette�bufsizer�tuple[int, int, bytes]c Cs�|��}dt|�}tjd|gd�R�}|jdusJ�|jj\}}|tjd||gd�R�7}||7}t|�D]}t|�D]}|t�d|j�||f��7}q=q7t|�d|fS)N�r�)rrrrrrrrrrrrrrrr�r)r�r/r2r�r�rur1�getpixel) rxr�� palette_datar�r%�w�h�y�xrrr�encode�s    �zBLPEncoder.encodeN)rr&)r�rrr�)r r r � _pushes_fdr�r�rrrrr��s  r�r�� Image.Imagerh� IO[bytes]�filename� str | bytesrbc Cs |jdkr d}t|��|j�d�dkrdnd}|�|�|jdus#J�|�t�dd��|�t�d tj ��|�t�d |jjd krCdnd ��|�t�d d ��|�t�d d ��|�tjd g|j �R��|dkr||�t�dd ��|�t�dd ��t � ||t � dd|j d |j�g�dS)N�PzUnsupported BLP image modeZ blp_version�BLP1r]r^r�rrcrerrdr"rarg)rv� ValueError� encoderinfo�get�writer�r2r�rrrur�_savert)r�rhr�rzrjrrrr��s  "(r�z.blpr�ZBLP2ra)rrrr)F)r%r&r'r(rr))r%r&rr))r[r&rr()r�r�rhr�r�r�rrb)'r}� __future__rr�rlr2�enumr�ior�typingr�rrr rrr$rHrTrY�NotImplementedErrorrZr_r`� PyDecoderr�r�r�� PyEncoderr�r�� register_openr~�register_extension�register_decoder� register_save�register_encoderrrrr�<module>s>      � 9 5F B'0 !  
Memory