a
    çÝEbé8  ã                   @   sü   d Z ddlZddlmZ ddlmZmZ dZdZdZ	dZ
dZdZdZd	d
„ Zd!dd„Zdd„ Zdd„ ZG dd„ deƒ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dd„ Ze ejee¡ e ejd¡ e d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)ÚBytesIOé   )ÚImageÚ	ImageFileé   é   é   c                 C   s*   | d? d@ d> | d? d@ d> | d@ d> fS )Né   é   r   é   é?   r   © )Úir   r   úR/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/PIL/BlpImagePlugin.pyÚ
unpack_5650   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   ©ÚlenÚ	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_dxt14   sD    



r1   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   r   r   r    r!   r$   r"   r#   Úcoder%   r&   r'   r(   r)   r*   r+   Úhighr   Úalphacode_indexr-   Ú
color_coder.   r/   r0   r   r   r   Údecode_dxt3k   sH    




r<   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)
    r2   z<BBz<6Br   r   r   r   r   é   r   r   r3   r4   r5   r   r6   é   r   r   )r   r   r   r    r!   Za0Za1r$   Z
alphacode1Z
alphacode2r"   r#   r8   r%   r&   r'   r(   r)   r*   r+   r   r:   Z	alphacoder-   r;   r.   r/   r0   r   r   r   Údecode_dxt5    s^    ,










r?   c                   @   s   e Zd ZdS )ÚBLPFormatErrorN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r@   æ   s   r@   c                   @   s(   e Zd ZdZdZdZdd„ Zdd„ ZdS )	ÚBlpImageFilez 
    Blizzard Mipmap Format
    ZBLPzBlizzard Mipmap Formatc                 C   s‚   | j  d¡| _|  ¡  | jdkr,d}d| _n4| jdkrLd}| jrDdnd| _ntdt| jƒ› �ƒ‚|d	| j d
| jd
dffg| _	d S )Nr   ó   BLP1ÚBLP1ÚRGBó   BLP2ÚBLP2ZRGBAzBad BLP magic ©r   r   r   r   )
ÚfpÚreadÚmagicÚ_read_blp_headerÚmodeÚ_blp_alpha_depthr@   ÚreprÚsizeÚtile)ÚselfÚdecoderr   r   r   Ú_openò   s    

zBlpImageFile._openc                 C   sø   t  d| j d¡¡\| _t  d| j d¡¡\| _t  d| j d¡¡\| _t  d| j d¡¡\| _t  d| j d¡¡\| _t  d| j d¡¡| _	| j
dkrÈt  d| j d¡¡\| _t  d| j d¡¡\| _t  d| j d	¡¡| _t  d| j d	¡¡| _d S ©
Nz<ir   z<br   z<IIr   rE   z<16Ié@   )r   ÚunpackrK   rL   Ú_blp_compressionÚ_blp_encodingrP   Ú_blp_alpha_encodingÚ	_blp_mipsÚ_sizerM   Ú_blp_subtypeÚ_blp_offsetsÚ_blp_lengths©rT   r   r   r   rN     s    
zBlpImageFile._read_blp_headerN)rA   rB   rC   Ú__doc__ÚformatÚformat_descriptionrV   rN   r   r   r   r   rD   ê   s
   rD   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Ú_BLPBaseDecoderTc              
   C   sd   z.| j  d¡ | j  d¡| _|  ¡  |  ¡  W n0 tjy^ } ztdƒ|‚W Y d }~n
d }~0 0 dS )Nr   r   zTruncated Blp filerJ   )	ÚfdÚseekrL   rM   rN   Ú_loadr   ÚerrorÚOSError)rT   ÚbufferÚer   r   r   Údecode  s     z_BLPBaseDecoder.decodec                 C   s   t  | j|¡S )N)r   Ú
_safe_readrg   )rT   Úlengthr   r   r   ro   !  s    z_BLPBaseDecoder._safe_readc              	   C   sb   g }t dƒD ]P}zt d|  d¡¡\}}}}W n tjyH   Y  q^Y n0 | ||||f¡ q|S )Né   z<4Br   )r   r   rY   ro   rj   Úappend)rT   r   r   r0   r/   r.   r-   r   r   r   Ú_read_palette$  s    z_BLPBaseDecoder._read_palettec                 C   sä   t  d|  d¡¡\| _t  d|  d¡¡\| _t  d|  d¡¡\| _t  d|  d¡¡\| _t  d|  d¡¡\| _t  d|  d¡¡| _| j	dkr¸t  d|  d¡¡\| _t  d|  d¡¡\| _
t  d|  d	¡¡| _t  d|  d	¡¡| _d S rW   )r   rY   ro   rZ   r[   rP   r\   r]   rR   rM   r_   r`   ra   rb   r   r   r   rN   .  s    
z _BLPBaseDecoder._read_blp_headerN)rA   rB   rC   Z	_pulls_fdrn   ro   rs   rN   r   r   r   r   rf     s
   

rf   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚBLP1Decoderc           	      C   sÜ   | j tkr|  ¡  nÄ| j dkrÄ| jdv r®tƒ }|  ¡ }t|  | jd ¡ƒ}zt	 
d| d¡¡\}W n t	jyz   Y qžY n0 || \}}}}| |||g¡ qJ|  t|ƒ¡ qØtdt| jƒ› �ƒ‚ntdt| jƒ› �ƒ‚d S )Nr   )r   r   r   ú<BzUnsupported BLP encoding zUnsupported BLP compression )rZ   ÚBLP_FORMAT_JPEGÚ_decode_jpeg_streamr[   r   rs   r   ro   ra   r   rY   rL   rj   r   Ú
set_as_rawÚbytesr@   rQ   )	rT   r   ÚpaletteÚ_dataÚoffsetr0   r/   r.   r-   r   r   r   ri   B  s(    




ÿÿzBLP1Decoder._loadc                 C   s”   ddl m} t d|  d¡¡\}|  |¡}|  | jd | j ¡  ¡ |  | jd ¡}|| }t	|ƒ}||ƒ}t
 |j¡ |j| _|j| _|j| _d S )Nr   )ÚJpegImageFiler4   r   )ZPIL.JpegImagePluginr}   r   rY   ro   r`   rg   Útellra   r   r   Z_decompression_bomb_checkrR   rS   rK   rO   )rT   r}   Zjpeg_header_sizeZjpeg_headerr   Úimager   r   r   rw   ]  s    
zBLP1Decoder._decode_jpeg_streamN)rA   rB   rC   ri   rw   r   r   r   r   rt   A  s   rt   c                   @   s   e Zd Zdd„ ZdS )ÚBLP2Decoderc                 C   s(  |   ¡ }tƒ }| j | jd ¡ | jdk�r| jtkr¢t|  	| j
d ¡ƒ}zt d| d¡¡\}W n tjyz   Y qžY n0 || \}}}}| |||f¡ qJ�q| jtk�rì| jtk�r| jd d d d }	t| jd d d ƒD ],}
t|  	|	¡t| jƒd�D ]}||7 }�qqænÔ| jtk�rv| jd d d d }	t| jd d d ƒD ]$}
t|  	|	¡ƒD ]}||7 }�q`�qNnt| jtk�rÖ| jd d d d }	t| jd d d ƒD ]$}
t|  	|	¡ƒD ]}||7 }�qÀ�q®ntd	t| jƒ› �ƒ‚ntd
t| jƒ› �ƒ‚ntdt| jƒ› �ƒ‚|  t|ƒ¡ d S )Nr   r   ru   r   r   r   )r   r2   zUnsupported alpha encoding zUnknown BLP encoding zUnknown BLP compression ) rs   r   rg   rh   r`   rZ   r[   ÚBLP_ENCODING_UNCOMPRESSEDr   ro   ra   r   rY   rL   rj   r   ÚBLP_ENCODING_DXTr\   ÚBLP_ALPHA_ENCODING_DXT1rR   r   r1   ÚboolrP   ÚBLP_ALPHA_ENCODING_DXT3r<   ÚBLP_ALPHA_ENCODING_DXT5r?   r@   rQ   rx   ry   )rT   rz   r   r{   r|   r0   r/   r.   r-   ZlinesizeZybÚdr   r   r   ri   n  sL    

ÿ
ÿÿzBLP2Decoder._loadN)rA   rB   rC   ri   r   r   r   r   r€   m  s   r€   c                 C   s   | d d… dv S )Nr   )rE   rH   r   )Úprefixr   r   r   Ú_accept¤  s    r‰   z.blprF   rI   )F)rc   r   Úior   Ú r   r   rv   r�   r‚   Z"BLP_ENCODING_UNCOMPRESSED_RAW_BGRArƒ   r…   r†   r   r1   r<   r?   ÚNotImplementedErrorr@   rD   Z	PyDecoderrf   rt   r€   r‰   Zregister_openrd   Zregister_extensionZregister_decoderr   r   r   r   Ú<module>   s0   
75F*-,7