U
    ­A›d{>  ã                   @   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   úK/home/sam/Atlas/atlas_env/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   -   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   3   s   r   c                 C   sŒ   t dtdtdi ¡ D ]X\}}|  |¡r| t|ƒd … } | |jkrt|› | › �d|j› d| › �ƒ ||    S qdt› d| › d�}t	|ƒ‚d S )	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 �]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   ©r   Ú	bytearrayÚrangeÚstructÚunpack_fromr+   Úextend)ÚdataÚalphaÚblocksÚretÚblockÚidxÚcolor0Úcolor1ÚbitsÚr0Úg0Úb0Úr1Úg1Úb1Újr*   ÚcontrolÚaÚrÚgÚbr   r   r   Údecode_dxt1L   sD    



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 �]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   r0   )r6   r8   r9   r:   r;   r>   r<   r=   Úcoder?   r@   rA   rB   rC   rD   rE   Úhighr*   Úalphacode_indexrG   Ú
color_coderH   rI   rJ   r   r   r   Údecode_dxt3ƒ   sH    




rV   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)
    rL   z<BBz<6Br   r   r-   r.   r(   é   r   r   rM   rN   rO   r   rP   é   r/   r0   )r6   r8   r9   r:   r;   Za0Za1r>   Z
alphacode1Z
alphacode2r<   r=   rR   r?   r@   rA   rB   rC   rD   rE   r*   rT   Z	alphacoderG   rU   rH   rI   rJ   r   r   r   Údecode_dxt5¸   s^    ,










rY   c                   @   s   e Zd ZdS )ÚBLPFormatErrorN)r	   r
   r   r   r   r   r   rZ   þ   s   rZ   c                 C   s   | d d… dkS )Nr.   ©ó   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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-   r[   zBad BLP magic ÚRGBAÚRGB©r   r   r   )ÚfpÚreadÚmagicÚseekÚosÚSEEK_CURr3   ÚunpackÚ_blp_alpha_depthÚ_sizeÚdecodeÚreprrZ   ÚmodeÚsizeÚtile)ÚselfÚdecoderr$   r   r   r   Ú_open  s    
zBlpImageFile._openN)r	   r
   r   Ú__doc__ÚformatÚformat_descriptionrv   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   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Ú_loadr3   ÚerrorÚOSError)rt   ÚbufferÚer$   r   r   r   ro   $  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 )	Nr.   ú<ira   r   rb   r-   ú<16Ié@   )Úfdri   r3   rl   Ú
_safe_readÚ_blp_compressionÚ_blp_encodingrm   Ú_blp_alpha_encodingrj   rk   rr   Ú
isinstanceÚBLP1DecoderÚ_blp_offsetsÚ_blp_lengths)rt   r   r   r   r|   -  s    
z _BLPBaseDecoder._read_blp_headerc                 C   s   t  | j|¡S )N)r   r†   r…   )rt   Úlengthr   r   r   r†   @  s    z_BLPBaseDecoder._safe_readc              	   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.   )r2   r3   rl   r†   r~   Úappend)rt   r9   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k
rL   Y q„Y nX || \}}}}|||f}	| jrx|	|f7 }	| 	|	¡ q|S )Nr   ú<Br   )
r1   r   r†   r�   r3   rl   rg   r~   rm   r5   )
rt   Úpaletter6   Ú_dataÚoffsetrJ   rI   rH   rG   Údr   r   r   Ú
_read_bgraM  s    


z_BLPBaseDecoder._read_bgraN)	r	   r
   r   Z	_pulls_fdro   r|   r†   r’   r˜   r   r   r   r   rz   !  s   	
rz   c                   @   s   e Zd Zdd„ Zdd„ ZdS )r‹   c                 C   s‚   | j tjkr|  ¡  nh| j dkrf| jdkrL|  ¡ }|  |¡}|  t|ƒ¡ q~d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_rawÚbytesrp   rZ   )rt   r”   r6   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r¬|jd \}}}}	||||	d dffg|_| d¡ ¡ \}
}}t
 d|||
f¡}|  | ¡ ¡ d S )Nr   )ÚJpegImageFilerN   r.   r   ZCMYKrd   )ZJpegImagePluginrœ   r3   rl   r†   rŒ   r…   Útellr�   r   r   Z_decompression_bomb_checkrr   rq   rs   ÚconvertÚsplitÚmergerš   Útobytes)rt   rœ   Zjpeg_header_sizeZjpeg_headerr6   Ú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@|  |¡}�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 }�qf�qTndt| jƒ› �}t|ƒ‚nd	t| jƒ› �}t|ƒ‚nd
t| jƒ› �}t|ƒ‚|  t|ƒ¡ d S )Nr   r   r   r.   r-   )r7   rL   zUnsupported alpha encoding zUnknown BLP encoding zUnknown BLP compression )r’   r…   ri   rŒ   r‡   rˆ   r   r   r˜   r   r1   r‰   r   r   rr   r2   rK   r†   Úboolrm   r   rV   r   rY   rp   rZ   rš   r›   )rt   r”   r6   ZlinesizeZybr—   r$   r   r   r   r}   ‚  s@     ÿ


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ó    rc   r�   r.   r   r�   )ÚimZ
getpaletter2   r3   Úpack)rt   r6   r”   r*   rH   rI   rJ   rG   r   r   r   Ú_write_palette³  s     zBLPEncoder._write_palettec           	   
   C   sš   |   ¡ }dt|ƒ }tjd|fdžŽ }| jj\}}|tjd|| fdžŽ 7 }||7 }t|ƒD ].}t|ƒD ] }|t d| j ||f¡¡7 }qhq\t|ƒd|fS )Né”   rƒ   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r“   r   )rª   r   r3   r©   r¨   rr   r2   Zgetpixel)	rt   ÚbufsizeZpalette_datar–   r6   Ú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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ÚBLP1r\   r]   r‚   r   ra   rc   r   rb   r(   r`   re   )rb   )rq   Ú
ValueErrorZencoderinfoÚgetÚwriter3   r©   r   r   r”   rr   r   Ú_save)r¨   rf   ÚfilenameZsave_allr$   rh   r   r   r   r·   Í  s    

"r·   z.blpr³   ZBLP2r`   )F)F)%rw   rj   r3   r"   r   Úior   Ú r   r   Z
_deprecater   r   r   r   r%   r+   rK   rV   rY   ÚNotImplementedErrorrZ   r^   r_   Z	PyDecoderrz   r‹   r¤   Z	PyEncoderr¦   r·   Zregister_openrx   Zregister_extensionZregister_decoderZregister_saveZregister_encoderr   r   r   r   Ú<module>   s8   
75F<$/
