o
    pèÝc{>  ã                   @   sn  d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	 ddl
mZ G dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZdd„ Zdd„ Zd-dd„Zdd„ Zdd„ ZG dd„ deƒZ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-d'd(„Ze ej ee¡ e !ej d)¡ e "d*e¡ e "d+e¡ e #ej e¡ e $d,e¡ 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.
é    N)ÚIntEnum)ÚBytesIOé   )ÚImageÚ	ImageFile)Ú	deprecatec                   @   s   e Zd ZdZdS )ÚFormatr   N)Ú__name__Ú
__module__Ú__qualname__ÚJPEG© r   r   ú2lib/python3.10/site-packages/PIL/BlpImagePlugin.pyr   )   s    r   c                   @   ó   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   -   ó    r   c                   @   r   )ÚAlphaEncodingr   r   é   N)r	   r
   r   ÚDXT1ÚDXT3ÚDXT5r   r   r   r   r   3   r   r   c                 C   sˆ   t dtdtdi ¡ D ],\}}|  |¡r6| t|ƒd … } | |jv r6t|› | › d|j› d| › ƒ ||    S q
dt› d| › d}t	|ƒ‚)	NZBLP_FORMAT_ZBLP_ENCODING_ZBLP_ALPHA_ENCODING_é
   Ú.zmodule 'z' has no attribute 'ú')
r   r   r   ÚitemsÚ
startswithÚlenÚ__members__r   r	   ÚAttributeError)ÚnameÚenumÚprefixÚmsgr   r   r   Ú__getattr__9   s   ý
ü

 €r'   c                 C   s*   | d? d@ d> | d? d@ d> | d@ d> fS )Né   é   r   é   é?   r   r   )Úir   r   r   Ú
unpack_565H   s   *r-   Fc              	   C   s´  t | ƒd }tƒ tƒ tƒ tƒ f}t|ƒD ]Ã}|d }t d| |¡\}}}t|ƒ\}	}
}t|ƒ\}}}tdƒD ] }tdƒD ]™}|d@ }|d? }d}|dkrU|	|
|}}}nh|dkrb|||}}}n[|dkr–||krƒd|	 | d }d|
 | d }d| | d }n:|	| d }|
| d }|| d }n'|dkr½||kr·d| |	 d }d| |
 d }d| | d }nd	\}}}}|rË||  ||||g¡ q<||  |||g¡ q<q6q|S )
úE
    input: one "row" of data (i.e. will produce 4*width pixels)
    é   z<HHIé   r   r   éÿ   r   r   )r   r   r   r   ©r    Ú	bytearrayÚrangeÚstructZunpack_fromr-   Úextend)ÚdataÚalphaÚblocksÚretÚblockÚidxÚcolor0Úcolor1ÚbitsÚr0Úg0Úb0Úr1Úg1Úb1Újr,   ZcontrolÚaÚrÚgÚbr   r   r   Údecode_dxt1L   sH   áÿ"rK   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 ]”}d}tdƒD ]‹}d| | d	 }|| }|rjd}|dL }nd
}|dM }|d9 }|d	d| |  ? d@ }|dkr|	|
|}}}nF|dkrš|||}}}n9|d	kr·d	|	 | d }d	|
 | d }d	| | d }n|dkrÓd	| |	 d }d	| |
 d }d	| | d }||  ||||g¡ qSqKq|S )r.   é   z<8Bú<HHr/   ú<Ié   r0   Fr   Té   é   r   r   r   r2   )r7   r9   r:   r;   r<   r?   r=   r>   Úcoder@   rA   rB   rC   rD   rE   rF   Zhighr,   Úalphacode_indexrG   Ú
color_coderH   rI   rJ   r   r   r   Údecode_dxt3ƒ   sL   
æþrU   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@ }n|dkrŸ|	d? |d> d@ B }n||d ? d@ }|d
kr®|}n6|dkrµ|}n/||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ý|||}}}nI|dkr|||}}}n;|dkr)d| | d }d| | d }d| | d }n|dkrFd| | d }d| | d }d| | d }||  ||||g¡ q{quq|S )zG
    input: one "row" of data (i.e. will produce 4 * width pixels)
    rL   z<BBz<6Br   r   r/   r0   r*   é   r   r   rM   rN   rO   r   rP   é   r1   r2   )r7   r9   r:   r;   r<   Za0Za1r?   Z
alphacode1Z
alphacode2r=   r>   rR   r@   rA   rB   rC   rD   rE   rF   r,   rS   Z	alphacoderG   rT   rH   rI   rJ   r   r   r   Údecode_dxt5¸   sb   ,


Ùÿ*rX   c                   @   s   e Zd ZdS )ÚBLPFormatErrorN)r	   r
   r   r   r   r   r   rY   þ   s    rY   c                 C   s   | d d… dv S )Nr0   ©ó   BLP1ó   BLP2r   )r%   r   r   r   Ú_accept  s   r]   c                   @   s    e Zd ZdZdZdZdd„ ZdS )ÚBlpImageFilez 
    Blizzard Mipmap Format
    ÚBLPzBlizzard Mipmap Formatc                 C   s¼   | j  d¡| _| j  dtj¡ t d| j  d¡¡\| _| j  dtj¡ t d| j  d¡¡| _	| jdv r9| j 
¡ }nd	t| jƒ› }t|ƒ‚| jrJd
nd| _|d| j d| jddffg| _d S )Nr0   r*   ú<br   r   ú<IIr/   rZ   zBad BLP magic ÚRGBAÚRGB©r   r   r   )ÚfpÚreadÚmagicÚseekÚosÚSEEK_CURr5   ÚunpackÚ_blp_alpha_depthÚ_sizeÚdecodeÚreprrY   ÚmodeÚsizeÚtile)ÚselfÚdecoderr&   r   r   r   Ú_open  s   
"zBlpImageFile._openN)r	   r
   r   Ú__doc__ÚformatZformat_descriptionru   r   r   r   r   r^     s
    r^   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )Ú_BLPBaseDecoderTc              
   C   s@   z|   ¡  |  ¡  W dS  tjy } zd}t|ƒ|‚d }~ww )NzTruncated BLP file)éÿÿÿÿr   )Ú_read_blp_headerÚ_loadr5   ÚerrorÚOSError)rs   ÚbufferÚer&   r   r   r   rn   $  s   
ý
€þz_BLPBaseDecoder.decodec                 C   sä   | 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 )	Nr0   ú<ir`   r   ra   r/   ú<16Ié@   )Úfdrh   r5   rk   Ú
_safe_readÚ_blp_compressionÚ_blp_encodingrl   Ú_blp_alpha_encodingri   rj   rq   Ú
isinstanceÚBLP1DecoderÚ_blp_offsetsÚ_blp_lengths)rs   r   r   r   rz   -  s   
z _BLPBaseDecoder._read_blp_headerc                 C   s   t  | j|¡S )N)r   r„   rƒ   )rs   Úlengthr   r   r   r„   @  s   z_BLPBaseDecoder._safe_readc              	   C   s`   g }t dƒD ]'}zt d|  d¡¡\}}}}W n tjy#   Y  |S w | ||||f¡ q|S )Né   ú<4Br0   )r4   r5   rk   r„   r|   Úappend)rs   r:   r,   rJ   rI   rH   rG   r   r   r   Ú_read_paletteC  s   ýz_BLPBaseDecoder._read_palettec           
      C   s‚   t ƒ }t|  | jd ¡ƒ}	 zt d| d¡¡\}W n tjy%   Y |S w || \}}}}|||f}	| jr;|	|f7 }	| 	|	¡ q)Nr   Tú<Br   )
r3   r   r„   r‹   r5   rk   rf   r|   rl   r6   )
rs   Úpaletter7   Ú_dataÚoffsetrJ   rI   rH   rG   Údr   r   r   Ú
_read_bgraM  s   ù


÷z_BLPBaseDecoder._read_bgraN)	r	   r
   r   Z	_pulls_fdrn   rz   r„   r   r–   r   r   r   r   rx   !  s    	
rx   c                   @   s   e Zd Zdd„ Zdd„ ZdS )r‰   c                 C   s€   | j tjkr|  ¡  d S | j dkr4| jdv r(|  ¡ }|  |¡}|  t|ƒ¡ d S dt	| jƒ› }t
|ƒ‚dt	| jƒ› }t
|ƒ‚)Nr   )r0   r*   zUnsupported BLP encoding zUnsupported BLP compression )r…   r   r   Ú_decode_jpeg_streamr†   r   r–   Ú
set_as_rawÚbytesro   rY   )rs   r’   r7   r&   r   r   r   r{   ^  s   


zBLP1Decoder._loadc                 C   sä   ddl m} t d|  d¡¡\}|  |¡}|  | jd | j ¡  ¡ |  | jd ¡}|| }t	|ƒ}||ƒ}t
 |j¡ |jdkrV|jd \}}}}	||||	d dffg|_| d¡ ¡ \}
}}t
 d|||
f¡}|  | ¡ ¡ d S )Nr   )ÚJpegImageFilerN   r0   r   ZCMYKrc   )ZJpegImagePluginrš   r5   rk   r„   rŠ   rƒ   Útellr‹   r   r   Z_decompression_bomb_checkrq   rp   rr   ZconvertÚsplitZmerger˜   Útobytes)rs   rš   Zjpeg_header_sizeZjpeg_headerr7   ZimageZdecoder_nameZextentsr”   ÚargsrH   rI   rJ   r   r   r   r—   n  s   

zBLP1Decoder._decode_jpeg_streamN)r	   r
   r   r{   r—   r   r   r   r   r‰   ]  s    r‰   c                   @   s   e Zd Zdd„ ZdS )ÚBLP2Decoderc                 C   sÆ  |   ¡ }| j | jd ¡ | jdkrÎ| jtjkr|  |¡}n¼| jtj	krÂt
ƒ }| jtjkrZ| jd d d d }t| jd d d ƒD ]}t|  |¡t| jƒdD ]}||7 }qQqCn€| jtjkrˆ| jd d d d }t| jd d d ƒD ]}t|  |¡ƒD ]}||7 }qqvnR| jtjkr¶| jd d d d }t| jd d d ƒD ]}t|  |¡ƒD ]}||7 }q­q¤n$dt| jƒ› }t|ƒ‚d	t| jƒ› }t|ƒ‚d
t| jƒ› }t|ƒ‚|  t|ƒ¡ d S )Nr   r   r   r0   r/   )r8   rL   zUnsupported alpha encoding zUnknown BLP encoding zUnknown BLP compression )r   rƒ   rh   rŠ   r…   r†   r   r   r–   r   r3   r‡   r   r   rq   r4   rK   r„   Úboolrl   r   rU   r   rX   ro   rY   r˜   r™   )rs   r’   r7   ZlinesizeZybr•   r&   r   r   r   r{   ‚  sJ   

ÿ
ýÿ
ÿÿ
ÿÿzBLP2Decoder._loadN)r	   r
   r   r{   r   r   r   r   rŸ     s    rŸ   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú
BLPEncoderTc              	   C   sZ   d}| j  dd¡}tdƒD ]}||d |d d … \}}}}|t d||||¡7 }q|S )Nó    rb   r   r0   r   rŽ   )ÚimZ
getpaletter4   r5   Úpack)rs   r7   r’   r,   rH   rI   rJ   rG   r   r   r   Ú_write_palette³  s    zBLPEncoder._write_palettec           	   
   C   sž   |   ¡ }dt|ƒ }tjd|gd¢R Ž }| jj\}}|tjd|| gd¢R Ž 7 }||7 }t|ƒD ]}t|ƒD ]}|t d| j ||f¡¡7 }q6q0t|ƒd|fS )Né”   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r‘   r   )r¥   r    r5   r¤   r£   rq   r4   Zgetpixel)	rs   ÚbufsizeZpalette_datar”   r7   Ú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¡   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r<dnd¡¡ | t d	d¡¡ | t d	d¡¡ | tjdg| j
¢R Ž ¡ |dkru| t dd¡¡ | t dd¡¡ t | |dd| j
 d| j fg¡ d S )NÚPzUnsupported BLP image modeZblp_versionÚBLP1r[   r\   r€   r   r`   rb   r   ra   r*   r_   rd   )rp   Ú
ValueErrorZencoderinfoÚgetÚwriter5   r¤   r   r   r’   rq   r   Ú_save)r£   re   ÚfilenameZsave_allr&   rg   r   r   r   r²   Í  s   

"$r²   z.blpr®   ZBLP2r_   )F)%rv   ri   r5   r$   r   Úior   Ú r   r   Z
_deprecater   r   r   r   r'   r-   rK   rU   rX   ÚNotImplementedErrorrY   r]   r^   Z	PyDecoderrx   r‰   rŸ   Z	PyEncoderr¡   r²   Zregister_openrw   Zregister_extensionZregister_decoderZregister_saveZregister_encoderr   r   r   r   Ú<module>   s:    
75F<$/
