U
    M»{jî?  ã                   @  s®  d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZmZ G d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZdddœdd„Zd:ddddœdd„Zdddœdd„Zdddœdd„ZG d d!„ d!eƒZddd"œd#d$„ZG d%d&„ d&ejƒZG d'd(„ d(ejƒZG d)d*„ d*eƒZG d+d,„ d,eƒZG d-d.„ d.ejƒZd/d0d1d2d3œd4d5„Z e !ej"ee¡ e #ej"d6¡ e $d7e¡ e $d8e¡ e %ej"e ¡ e &d9e¡ dS );aÑ  
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                   @  s   e Zd ZdZdS )ÚFormatr   N)Ú__name__Ú
__module__Ú__qualname__ÚJPEG© r   r   úQ/var/www/html/bwave/python/venv/lib/python3.8/site-packages/PIL/BlpImagePlugin.pyr	   ,   s   r	   c                   @  s   e Zd ZdZdZdZdS )ÚEncodingr   é   é   N)r
   r   r   ÚUNCOMPRESSEDÚDXTZUNCOMPRESSED_RAW_BGRAr   r   r   r   r   0   s   r   c                   @  s   e Zd ZdZdZdZdS )ÚAlphaEncodingr   r   é   N)r
   r   r   ÚDXT1ÚDXT3ÚDXT5r   r   r   r   r   6   s   r   Úintztuple[int, int, int])ÚiÚreturnc                 C  s*   | d? d@ d> | d? d@ d> | d@ d> fS )Né   é   r   é   é?   r   r   )r   r   r   r   Ú
unpack_565<   s    r!   FÚbytesÚboolz1tuple[bytearray, bytearray, bytearray, bytearray])ÚdataÚalphar   c              	   C  sÄ  t | ƒd }tƒ tƒ tƒ tƒ f}t|ƒD �]”}|d }t d| |¡\}}}t|ƒ\}	}
}t|ƒ\}}}tdƒD �]L}tdƒD �]<}|d@ }|d? }d}|dkr°|	|
|  }}}nØ|dkrÊ|||  }}}n¾|dk�r6||k�rd|	 | d }d|
 | d }d| | d }n$|	| d }|
| d }|| d }nR|dk�rˆ||k�r|d| |	 d }d| |
 d }d| | d }nd	\}}}}|�r¦||  ||||g¡ q|||  |||g¡ q|qnq(|S )
úE
    input: one "row" of data (i.e. will produce 4*width pixels)
    é   z<HHIé   r   r   éÿ   r   r   )r   r   r   r   ©Ú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Úbr   r   r   Údecode_dxt1@   sD    



rD   )r$   r   c              	   C  sÔ  t | ƒd }tƒ tƒ tƒ tƒ f}t|ƒD �]¤}|d }| ||d … }t d|¡}t d|d¡\}}t d|d¡\}	t|ƒ\}
}}t|ƒ\}}}tdƒD �]2}d}tdƒD �]}d| | d	 }|| }|rÚd}|dL }nd
}|dM }|d9 }|	d	d| |  ? d@ }|dk�r"|
||  }}}n’|dk�r>|||  }}}nv|d	k�rzd	|
 | d }d	| | d }d	| | d }n:|dk�r´d	| |
 d }d	| | d }d	| | d }||  ||||g¡ qªq˜q(|S )r&   é   z<8Bú<HHr'   ú<Ié   r(   Fr   Té   é   r   r   r   r*   )r$   r1   r2   r3   r4   Úblockr7   r5   r6   Úcoder8   r9   r:   r;   r<   r=   r>   Úhighr   Úalphacode_indexr@   Ú
color_coderA   rB   rC   r   r   r   Údecode_dxt3y   sH    




rP   c              	   C  sÀ  t | ƒd }tƒ tƒ tƒ tƒ f}t|ƒD �]�}|d }| ||d … }t d|¡\}}t d|d¡}|d |d d> B |d d> B |d d	> B }	|d
 |d d> B }
t d|d¡\}}t d|d¡\}t|ƒ\}}}t|ƒ\}}}tdƒD �]Ì}tdƒD �]¼}dd| |  }|dk�r&|
|? d@ }n0|dk�rF|
d? |	d> d@ B }n|	|d ? d@ }|d
k�rf|}nt|dk�rv|}nd||k�ržd| | |d |  d }n<|dk�r®d
}n,|dk�r¾d}nd| | |d |  d }|dd| |  ? d@ }|d
k�r|||  }}}n’|dk�r*|||  }}}nv|dk�rfd| | d }d| | d }d| | d }n:|dk�r d| | d }d| | d }d| | d }||  ||||g¡ qøqêq(|S )zG
    input: one "row" of data (i.e. will produce 4 * width pixels)
    rE   z<BBz<6Br   r   r'   r(   r   é   r   r   rF   rG   rH   r   rI   é   r)   r*   )r$   r1   r2   r3   r4   rK   Za0Za1r7   Z
alphacode1Z
alphacode2r5   r6   rL   r8   r9   r:   r;   r<   r=   r>   r   rN   Z	alphacoder@   rO   rA   rB   rC   r   r   r   Údecode_dxt5®   s^    ,










rS   c                   @  s   e Zd ZdS )ÚBLPFormatErrorN)r
   r   r   r   r   r   r   rT   ô   s   rT   )Úprefixr   c                 C  s   | d d… dkS )Nr(   ©ó   BLP1ó   BLP2r   )rU   r   r   r   Ú_acceptø   s    rY   c                   @  s&   e Zd ZdZdZdZddœdd„ZdS )	ÚBlpImageFilez 
    Blizzard Mipmap Format
    ÚBLPzBlizzard Mipmap FormatÚNone©r   c                 C  s¼   | j  d¡| _| j  dtj¡ t d| j  d¡¡\| _| j  dtj¡ t d| j  d¡¡| _	| jdkrr| j 
¡ }nd	t| jƒ› �}t|ƒ‚| jr”d
nd| _|d| j d| jddffg| _d S )Nr(   r   ú<br   r   ú<IIr'   rV   zBad BLP magic ÚRGBAÚRGB©r   r   r   )ÚfpÚreadÚmagicÚseekÚosÚSEEK_CURr.   ÚunpackÚ_blp_alpha_depthÚ_sizeÚdecodeÚreprrT   Ú_modeÚsizeÚmodeÚtile)ÚselfÚdecoderÚmsgr   r   r   Ú_open  s    
zBlpImageFile._openN)r
   r   r   Ú__doc__ÚformatÚformat_descriptionru   r   r   r   r   rZ   ü   s   rZ   c                   @  sp   e Zd ZdZdddœdd„Zejddœd	d
„ƒZddœdd„Zdddœdd„Z	ddœdd„Z
dddœdd„ZdS )Ú_BLPBaseDecoderTr"   ztuple[int, int])Úbufferr   c              
   C  sL   z|   ¡  |  ¡  W n2 tjk
rF } zd}t|ƒ|‚W 5 d }~X Y nX dS )NzTruncated BLP file)éÿÿÿÿr   )Ú_read_blp_headerÚ_loadr.   ÚerrorÚOSError)rr   rz   Úert   r   r   r   rl     s    z_BLPBaseDecoder.decoder\   r]   c                 C  s   d S ©Nr   ©rr   r   r   r   r}   #  s    z_BLPBaseDecoder._loadc                 C  sò   | j d k	st‚| j  d¡ t d|  d¡¡\| _t d|  d¡¡\| _t d|  d¡¡\| _t d|  d¡¡\| _	| j  dt
j¡ t d|  d¡¡| _t| tƒrÆt d|  d¡¡\| _| j  dt
j¡ t d|  d¡¡| _t d|  d¡¡| _d S )	Nr(   ú<ir^   r   r_   r'   ú<16Ié@   )ÚfdÚAssertionErrorrf   r.   ri   Ú
_safe_readÚ_blp_compressionÚ_blp_encodingrj   Ú_blp_alpha_encodingrg   rh   ro   Ú
isinstanceÚBLP1DecoderÚ_blp_offsetsÚ_blp_lengthsr‚   r   r   r   r|   '  s    
z _BLPBaseDecoder._read_blp_headerr   )Úlengthr   c                 C  s   t  | j|¡S r�   )r   rˆ   r†   )rr   r�   r   r   r   rˆ   ;  s    z_BLPBaseDecoder._safe_readzlist[tuple[int, int, int, int]]c              	   C  sd   g }t dƒD ]R}zt d|  d¡¡\}}}}W n tjk
rJ   Y  q`Y nX | ||||f¡ q|S )Né   ú<4Br(   )r-   r.   ri   rˆ   r~   Úappend)rr   r2   r   rC   rB   rA   r@   r   r   r   Ú_read_palette>  s    z_BLPBaseDecoder._read_paletter,   )Úpaletter   c           
      C  sˆ   t ƒ }t|  | jd ¡ƒ}zt d| d¡¡\}W n tjk
rL   Y q„Y nX || \}}}}|||f}	| jrx|	|f7 }	| 	|	¡ q|S )Nr   ú<Br   )
r,   r   rˆ   r�   r.   ri   rd   r~   rj   r0   )
rr   r•   r$   Ú_dataÚoffsetrC   rB   rA   r@   Údr   r   r   Ú
_read_bgraH  s    


z_BLPBaseDecoder._read_bgraN)r
   r   r   Z	_pulls_fdrl   ÚabcÚabstractmethodr}   r|   rˆ   r”   rš   r   r   r   r   ry     s   	
ry   c                   @  s(   e Zd Zddœdd„Zddœdd„ZdS )r�   r\   r]   c                 C  s~   | j tjkr|  ¡  nd| j dkrb| jdkrH|  ¡ }|  |¡}|  |¡ qzdt| jƒ› �}t	|ƒ‚ndt| jƒ› �}t	|ƒ‚d S )Nr   )r(   r   zUnsupported BLP encoding zUnsupported BLP compression )
r‰   r	   r   Ú_decode_jpeg_streamrŠ   r”   rš   Ú
set_as_rawrm   rT   )rr   r•   r$   rt   r   r   r   r}   Y  s    




zBLP1Decoder._loadc                 C  sî   ddl m} t d|  d¡¡\}|  |¡}| jd k	s8t‚|  | jd | j ¡  ¡ |  | j	d ¡}|| }|t
|ƒƒ}t |j¡ |jdkr¶|jd \}}}}	||||	d dffg|_| d¡ ¡ \}
}}t d|||
f¡}|  | ¡ ¡ d S )Nr   )ÚJpegImageFilerG   r(   r   ZCMYKra   )ZJpegImagePluginrŸ   r.   ri   rˆ   r†   r‡   rŽ   Útellr�   r   r   Z_decompression_bomb_checkro   rp   rq   ÚconvertÚsplitÚmergerž   Útobytes)rr   rŸ   Zjpeg_header_sizeZjpeg_headerr$   ÚimageZdecoder_nameZextentsr˜   ÚargsrA   rB   rC   Zreversed_imager   r   r   r�   i  s    

zBLP1Decoder._decode_jpeg_streamN)r
   r   r   r}   r�   r   r   r   r   r�   X  s   r�   c                   @  s   e Zd Zddœdd„ZdS )ÚBLP2Decoderr\   r]   c                 C  sæ  |   ¡ }| jd k	st‚| j | jd ¡ | jdk�rÀ| jtjkrN|  	|¡}�qØ| jtj
k�r¦tƒ }| jtjkrÈ| jd d d d }t| jd d d ƒD ]*}t|  |¡t| jƒd�D ]}||7 }q¶qšnÜ| jtjk�r*| jd d d d }t| jd d d ƒD ]$}t|  |¡ƒD ]}||7 }�q�qnz| jtjk�rŒ| jd d d d }t| jd d d ƒD ]$}t|  |¡ƒD ]}||7 }�qv�qdndt| jƒ› �}t|ƒ‚nd	t| jƒ› �}t|ƒ‚nd
t| jƒ› �}t|ƒ‚|  |¡ d S )Nr   r   r   r(   r'   )r%   rE   zUnsupported alpha encoding zUnknown BLP encoding zUnknown BLP compression )r”   r†   r‡   rf   rŽ   r‰   rŠ   r   r   rš   r   r,   r‹   r   r   ro   r-   rD   rˆ   r#   rj   r   rP   r   rS   rm   rT   rž   )rr   r•   r$   ZlinesizeZybr™   rt   r   r   r   r}   }  sB     ÿ


zBLP2Decoder._loadN)r
   r   r   r}   r   r   r   r   r§   |  s   r§   c                   @  s.   e Zd ZdZddœdd„Zdddœd	d
„ZdS )Ú
BLPEncoderTr"   r]   c              	   C  s†   d}| j d k	st‚| j  dd¡}tt|ƒd ƒD ]:}||d |d d … \}}}}|t d||||¡7 }q0t|ƒdk r‚|d7 }ql|S )Nó    r`   r(   r   r’   i   s       )Úimr‡   Z
getpaletter-   r+   r.   Úpack)rr   r$   r•   r   rA   rB   rC   r@   r   r   r   Ú_write_palette¯  s     
zBLPEncoder._write_paletter   ztuple[int, int, bytes])Úbufsizer   c           	   
   C  s¨   |   ¡ }dt|ƒ }tjd|fdžŽ }| jd k	s4t‚| jj\}}|tjd|| fdžŽ 7 }||7 }t|ƒD ].}t|ƒD ] }|t d| j ||f¡¡7 }qvqjt|ƒd|fS )Né”   r„   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r–   r   )	r¬   r+   r.   r«   rª   r‡   ro   r-   Zgetpixel)	rr   r­   Zpalette_datar˜   r$   ÚwÚhÚyÚxr   r   r   Úencodeº  s     zBLPEncoder.encodeN)r
   r   r   Z
_pushes_fdr¬   r³   r   r   r   r   r¨   ¬  s   r¨   zImage.Imagez	IO[bytes]zstr | bytesr\   )rª   rc   Úfilenamer   c                 C  s
  | j dkrd}t|ƒ‚| j d¡dkr*dnd}| |¡ | t dd¡¡ | t d	tj¡¡ | t d	| j	j d
krxdnd¡¡ | t d	d¡¡ | t d	d¡¡ | tjd| j
žŽ ¡ |dkræ| t dd¡¡ | t dd¡¡ t | |dd| j
 d| j fg¡ d S )NÚPzUnsupported BLP image modeZblp_versionÚBLP1rW   rX   rƒ   r   r^   r`   r   r_   r   r[   rb   )r_   )rp   Ú
ValueErrorZencoderinfoÚgetÚwriter.   r«   r   r   r•   ro   r   Ú_save)rª   rc   r´   rt   re   r   r   r   rº   Í  s    

"rº   z.blpr¶   ZBLP2r[   )F)'rv   Ú
__future__r   r›   rg   r.   Úenumr   Úior   Útypingr   Ú r   r   r	   r   r   r!   rD   rP   rS   ÚNotImplementedErrorrT   rY   rZ   Z	PyDecoderry   r�   r§   Z	PyEncoderr¨   rº   Zregister_openrw   Zregister_extensionZregister_decoderZregister_saveZregister_encoderr   r   r   r   Ú<module>   s<    ÿ95FA$0!