B
    ñ¶I`68  ã               @   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e ejedd„ ¡ 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   ú6/tmp/pip-unpacked-wheel-wndzrte2/PIL/BlpImagePlugin.pyÚ
unpack_5650   s    r   Fc          	   C   sÖ  t | ƒd }tƒ tƒ tƒ tƒ f}�x®t|ƒD �] }|d }t d| |¡\}}}t|ƒ\}	}
}t|ƒ\}}}�x`tdƒD �]R}�xJtdƒD �]<}|d@ }|d? }d}|dkr¼|	|
|  }}}nØ|dkrÖ|||  }}}n¾|dk�rB||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ˆW qvW q,W |S )
zE
    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    



 r/   c          	   C   sæ  t | ƒd }tƒ tƒ tƒ tƒ f}�x¾t|ƒD �]°}|d }| ||d … }t d|¡}t d|d¡\}}t d|d¡\}t|ƒ\}	}
}t|ƒ\}}}�xFtdƒD �]8}d}�x,tdƒD �]}d| | d	 }|| }|ræd}|dL }nd
}|dM }|d9 }|d	d| |  ? d@ }|dk�r.|	|
|  }}}n’|dk�rJ|||  }}}nv|d	k�r†d	|	 | d }d	|
 | d }d	| | d }n:|dk�rÀd	| |	 d }d	| |
 d }d	| | d }||  ||||g¡ q¶W q W q,W |S )zE
    input: one "row" of data (i.e. will produce 4*width pixels)
    é   z<8Bz<HHr   z<Ié   r   Fr   Té   é   r   r   r   )r   r   r   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-   r.   r   r   r   Údecode_dxt3k   sH    




"r8   c          	   C   sÔ  t | ƒd }tƒ tƒ tƒ tƒ f}�x¬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|ƒ\}}}�xâtdƒD �]Ô}�xÌtdƒD �]¾}dd| |  }|dk�r2|	|? d@ }n0|dk�rR|	d? |d> d@ B }n||d ? d@ }|d
k�rr|}nt|dk�r‚|}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�r6|||  }}}nv|dk�rrd| | d }d| | d }d| | d }n:|dk�r¬d| | d }d| | d }d| | d }||  ||||g¡ �qW qòW q,W |S )zG
    input: one "row" of data (i.e. will produce 4 * width pixels)
    r0   z<BBz<6Br   r   r   r   r   é   r   r   z<HHz<Ir1   r   r2   é   r   )r   r   r   r   r   r   r   )r   r   r   r   r   Za0Za1r"   Z
alphacode1Z
alphacode2r    r!   r4   r#   r$   r%   r&   r'   r(   r)   r   r6   Z	alphacoder+   r7   r,   r-   r.   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   s   BLP1ÚBLP1ZRGBs   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   s   BLP1z<16Ié@   )r   ÚunpackrC   rD   Ú_blp_compressionÚ_blp_encodingrH   Ú_blp_alpha_encodingÚ	_blp_mipsÚ_sizerE   Ú_blp_subtypeÚ_blp_offsetsÚ_blp_lengths)rL   r   r   r   rF     s    
zBlpImageFile._read_blp_headerN)r=   r>   r?   Ú__doc__ÚformatÚformat_descriptionrN   rF   r   r   r   r   r@   ê   s
   r@   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú_BLPBaseDecoderTc          
   C   sb   y.| j  d¡ | j  d¡| _|  ¡  |  ¡  W n. tjk
r\ } ztdƒ|‚W d d }~X Y nX dS )Nr   r   zTruncated Blp file)r   r   )	ÚfdÚseekrD   rE   rF   Ú_loadr   ÚerrorÚOSError)rL   ÚbufferÚer   r   r   Údecode  s    z_BLPBaseDecoder.decodec          	   C   sf   g }x\t dƒD ]P}y t d| j d¡¡\}}}}W n tjk
rJ   P Y nX | ||||f¡ qW |S )Né   z<4Br   )r   r   rP   r]   rD   r`   Úappend)rL   r   r   r.   r-   r,   r+   r   r   r   Ú_read_palette!  s     z_BLPBaseDecoder._read_palettec             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   s   BLP1z<16IrO   )r   rP   r]   rD   rQ   rR   rH   rS   rT   rJ   rE   rV   rW   rX   )rL   r   r   r   rF   +  s    
z _BLPBaseDecoder._read_blp_headerN)r=   r>   r?   Z	_pulls_fdrd   rg   rF   r   r   r   r   r\     s   

r\   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kr´tƒ }|  ¡ }t| j | j	d ¡ƒ}xVyt
 d| d¡¡\}W n t
jk
r~   P Y nX || \}}}}| |||g¡ qNW |  t|ƒ¡ qÞtdt| jƒ› �ƒ‚ntdt| jƒ› �ƒ‚d S )Nr   )r   r   r   z<BzUnsupported BLP encoding zUnsupported BLP compression )rQ   ÚBLP_FORMAT_JPEGÚ_decode_jpeg_streamrR   r   rg   r   r]   rD   rX   r   rP   r`   r   Ú
set_as_rawÚbytesr<   rI   )	rL   r   ÚpaletteÚ_dataÚoffsetr.   r-   r,   r+   r   r   r   r_   ?  s&    



zBLP1Decoder._loadc             C   sœ   ddl m} t d| j d¡¡\}| j |¡}| j | jd | j ¡  ¡ | j | jd ¡}|| }t	|ƒ}||ƒ}t
 |j¡ |j| _|j| _|j| _d S )Nr   )ÚJpegImageFilez<Ir   )ZPIL.JpegImagePluginrp   r   rP   r]   rD   rW   ÚtellrX   r   r   Z_decompression_bomb_checkrJ   rK   rC   rG   )rL   rp   Zjpeg_header_sizeZjpeg_headerr   Úimager   r   r   rj   Z  s    zBLP1Decoder._decode_jpeg_streamN)r=   r>   r?   r_   rj   r   r   r   r   rh   >  s   rh   c               @   s   e Zd Zdd„ ZdS )ÚBLP2Decoderc             C   sL  |   ¡ }tƒ }| j | jd ¡ | jdk�r&| jtkr¨t| j 	| j
d ¡ƒ}xVyt d| 	d¡¡\}W n tjk
r~   P Y nX || \}}}}| |||f¡ qNW �q:| jtk�r| jtk�r&| jd d d d }	xLt| jd d d ƒD ]2}
x,t| j 	|	¡t| jƒd�D ]}||7 }�qW qîW nè| jtk�r�| jd d d d }	xÄt| jd d d ƒD ]*}
x"t| j 	|	¡ƒD ]}||7 }�qvW �q`W n~| jtk�rú| jd d d d }	xZt| jd d d ƒD ]*}
x"t| j 	|	¡ƒD ]}||7 }�qàW �qÊW ntd	t| jƒ› �ƒ‚ntd
t| jƒ› �ƒ‚ntdt| jƒ› �ƒ‚|  t|ƒ¡ d S )Nr   r   z<Br   r   r   )r   r0   zUnsupported alpha encoding zUnknown BLP encoding zUnknown BLP compression )rg   r   r]   r^   rW   rQ   rR   ÚBLP_ENCODING_UNCOMPRESSEDr   rD   rX   r   rP   r`   r   ÚBLP_ENCODING_DXTrS   ÚBLP_ALPHA_ENCODING_DXT1rJ   r   r/   ÚboolrH   ÚBLP_ALPHA_ENCODING_DXT3r8   ÚBLP_ALPHA_ENCODING_DXT5r;   r<   rI   rk   rl   )rL   rm   r   rn   ro   r.   r-   r,   r+   ZlinesizeZybÚdr   r   r   r_   k  sH    
zBLP2Decoder._loadN)r=   r>   r?   r_   r   r   r   r   rs   j  s   rs   c             C   s   | d d… dkS )Nr   )s   BLP1s   BLP2r   )Úpr   r   r   Ú<lambda>¢  s    r|   z.blprA   rB   )F)rY   r   Úior   Ú r   r   ri   rt   ru   Z"BLP_ENCODING_UNCOMPRESSED_RAW_BGRArv   rx   ry   r   r/   r8   r;   ÚNotImplementedErrorr<   r@   Z	PyDecoderr\   rh   rs   Zregister_openrZ   Zregister_extensionZregister_decoderr   r   r   r   Ú<module>   s0   
75F**,7