3
V^Ì7  ã               @   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jejedd„ ƒ ejejdƒ ejdeƒ ej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   ú4/tmp/pip-build-zn8x4q5s/pillow/PIL/BlpImagePlugin.pyÚ
unpack_5650   s    r   Fc             C   sÖ  t | ƒd }tƒ tƒ tƒ tƒ f}�x®t|ƒD �] }|d }tj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²|| j||||gƒ qˆ|| j|||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jd|ƒ}tjd|dƒ\}}tj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 }|| j||||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jd|ƒ\}}tjd|dƒ}|d |d d> B |d d> B |d d	> B }|d
 |d d> B }	tjd|dƒ\}
}tj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 }|| j||||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 jdƒ| _| jƒ  | jdkr,d}d| _n.| jdkrLd}| jrDdnd| _ntd| j ƒ‚|d| j d	| jd	d
ffg| _d S )Nr   s   BLP1ÚBLP1ZRGBs   BLP2ÚBLP2ZRGBAzBad BLP magic %rr   r   )r   r   )	ÚfpÚreadÚmagicÚ_read_blp_headerÚmodeÚ_blp_alpha_depthr<   ÚsizeÚtile)ÚselfÚdecoderr   r   r   Ú_openò   s    

zBlpImageFile._openc             C   sø   t jd| jjdƒƒ\| _t jd| jjdƒƒ\| _t jd| jjdƒƒ\| _t jd| jjdƒƒ\| _t jd| jjdƒƒ\| _t jd| jjdƒƒ| _	| j
dkrÈt jd| jjdƒƒ\| _t jd| jjdƒƒ\| _t jd| jjd
ƒƒ| _t jd| jjdƒƒ| _d S )Nz<ir   z<br   z<IIr   s   BLP1z<16Ir0   é@   rN   )r   ÚunpackrC   rD   Ú_blp_compressionÚ_blp_encodingrH   Ú_blp_alpha_encodingÚ	_blp_mipsÚ_sizerE   Ú_blp_subtypeÚ_blp_offsetsÚ_blp_lengths)rK   r   r   r   rF     s    
zBlpImageFile._read_blp_headerN)r=   r>   r?   Ú__doc__ÚformatÚformat_descriptionrM   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   sR   y.| j jdƒ | j jdƒ| _| jƒ  | jƒ  W n tjk
rL   tdƒ‚Y nX dS )Nr   r   zTruncated Blp file)r   r   )	ÚfdÚseekrD   rE   rF   Ú_loadr   ÚerrorÚOSError)rK   Úbufferr   r   r   Údecode  s    z_BLPBaseDecoder.decodec             C   sf   g }x\t dƒD ]P}y tjd| jjdƒƒ\}}}}W n tjk
rJ   P Y nX |j||||fƒ qW |S )Né   z<4Br   )r   r   rO   r\   rD   r_   Úappend)rK   r   r   r.   r-   r,   r+   r   r   r   Ú_read_palette!  s     z_BLPBaseDecoder._read_palettec             C   sø   t jd| jjdƒƒ\| _t jd| jjdƒƒ\| _t jd| jjdƒƒ\| _t jd| jjdƒƒ\| _t jd| jjdƒƒ\| _t jd| jjdƒƒ| _	| j
dkrÈt jd| jjdƒƒ\| _t jd| jjdƒƒ\| _t jd| jjd
ƒƒ| _t jd| jjdƒƒ| _d S )Nz<ir   z<br   z<IIr   s   BLP1z<16Ir0   rN   rN   )r   rO   r\   rD   rP   rQ   rH   rR   rS   rI   rE   rU   rV   rW   )rK   r   r   r   rF   +  s    
z _BLPBaseDecoder._read_blp_headerN)r=   r>   r?   Z	_pulls_fdrb   re   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| jƒ  n¾| j dkrÄ| jdkr´tƒ }| jƒ }t| jj| j	d ƒƒ}xVyt
jd|jdƒƒ\}W n t
jk
r~   P Y nX || \}}}}|j|||gƒ qNW | jt|ƒƒ qÒtd| j ƒ‚ntd| j ƒ‚d S )	Nr   r   r   r   z<BzUnsupported BLP encoding %rzUnsupported BLP compression %r)r   r   )rP   ÚBLP_FORMAT_JPEGÚ_decode_jpeg_streamrQ   r   re   r   r\   rD   rW   r   rO   r_   r   Ú
set_as_rawÚbytesr<   )	rK   r   ÚpaletteÚ_dataÚoffsetr.   r-   r,   r+   r   r   r   r^   ?  s&    



zBLP1Decoder._loadc             C   s�   ddl m} tjd| jjdƒƒ\}| jj|ƒ}| jj| jd | jjƒ  ƒ | jj| jd ƒ}|| }t	|ƒ}||ƒ}|j
| _
|j| _|j| _d S )Nr   )ÚJpegImageFilez<Ir   )ZPIL.JpegImagePluginrn   r   rO   r\   rD   rV   ÚtellrW   r   rJ   rC   rG   )rK   rn   Zjpeg_header_sizeZjpeg_headerr   Úimager   r   r   rh   Z  s    zBLP1Decoder._decode_jpeg_streamN)r=   r>   r?   r^   rh   r   r   r   r   rf   >  s   rf   c               @   s   e Zd Zdd„ ZdS )ÚBLP2Decoderc             C   s:  | j ƒ }tƒ }| jj| jd ƒ | jdk�r| jtkr¨t| jj	| j
d ƒƒ}xVytjd|j	dƒƒ\}W n tjk
r~   P Y nX || \}}}}|j|||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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j	|	ƒƒD ]}||7 }�qvW �q`W nx| jtk�rú| jd d d d }	xTt| jd d d ƒD ]*}
x"t| jj	|	ƒƒD ]}||7 }�qàW �qÊW ntd	| j ƒ‚ntd
| j ƒ‚ntd| j ƒ‚| jt|ƒƒ d S )Nr   r   z<Br   r   r   )r   r0   zUnsupported alpha encoding %rzUnknown BLP encoding %rzUnknown BLP compression %r)re   r   r\   r]   rV   rP   rQ   ÚBLP_ENCODING_UNCOMPRESSEDr   rD   rW   r   rO   r_   r   ÚBLP_ENCODING_DXTrR   ÚBLP_ALPHA_ENCODING_DXT1rI   r   r/   ÚboolrH   ÚBLP_ALPHA_ENCODING_DXT3r8   ÚBLP_ALPHA_ENCODING_DXT5r;   r<   ri   rj   )rK   rk   r   rl   rm   r.   r-   r,   r+   ZlinesizeZybÚdr   r   r   r^   j  sF    
zBLP2Decoder._loadN)r=   r>   r?   r^   r   r   r   r   rq   i  s   rq   c             C   s   | d d… dkS )Nr   ó   BLP1ó   BLP2)ry   rz   r   )Úpr   r   r   Ú<lambda>Ÿ  s    r|   z.blprA   rB   )F)rX   r   Úior   Ú r   r   rg   rr   rs   Z"BLP_ENCODING_UNCOMPRESSED_RAW_BGRArt   rv   rw   r   r/   r8   r;   ÚNotImplementedErrorr<   r@   Z	PyDecoderr[   rf   rq   Zregister_openrY   Zregister_extensionZregister_decoderr   r   r   r   Ú<module>   s0   
75F**+5