U
    &^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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-install-a1j0c_p1/Pillow/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	 }|| }|rd}|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rd	| |	 d }d	| |
 d }d	| | d }|| ||||g qqq(|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rd| | |d |  d }n<|dkrd
}n,|dkrd}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rd| | d }d| | d }d| | d }|| ||||g qqq(|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| _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      BLP1BLP1ZRGB   BLP2BLP2ZRGBAzBad BLP magic %rr   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 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rt 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   unpackrJ   rK   _blp_compression_blp_encodingrO   _blp_alpha_encoding	_blp_mips_sizerL   _blp_subtype_blp_offsets_blp_lengthsrR   r   r   r   rM     s    
zBlpImageFile._read_blp_headerN)rA   rB   rC   __doc__formatformat_descriptionrT   rM   r   r   r   r   rD      s
   rD   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_BLPBaseDecoderTc                 C   sR   z.| j d | j d| _|   |   W n tjk
rL   tdY nX dS )Nr   r   zTruncated Blp filerI   )	fdseekrK   rL   rM   _loadr   errorOSError)rR   bufferr   r   r   decode  s    z_BLPBaseDecoder.decodec              	   C   sf   g }t dD ]T}z td| jd\}}}}W n tjk
rL   Y  qbY nX |||||f q|S )N   z<4Br   )r   r   rW   re   rK   rh   append)rR   r   r   r0   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rt d| jd\| _t d| jd\| _t d| jd	| _t d| jd	| _d S rU   )r   rW   re   rK   rX   rY   rO   rZ   r[   rP   rL   r]   r^   r_   r`   r   r   r   rM   +  s    
z _BLPBaseDecoder._read_blp_headerN)rA   rB   rC   Z	_pulls_fdrk   rn   rM   r   r   r   r   rd     s   

rd   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rt }|  }t| j| j	d }zt
d|d\}W n t
jk
r~   Y qY nX || \}}}}||||g qL| t| qtd| j ntd| j d S )Nr   )r   r   r   <BzUnsupported BLP encoding %rzUnsupported BLP compression %r)rX   BLP_FORMAT_JPEG_decode_jpeg_streamrY   r   rn   r   re   rK   r_   r   rW   rh   r   
set_as_rawbytesr@   )	rR   r   palette_dataoffsetr0   r/   r.   r-   r   r   r   rg   ?  s(    




zBLP1Decoder._loadc                 C   s   ddl m} td| jd\}| j|}| j| jd | j   | j| jd }|| }t	|}||}|j
| _
|j| _|j| _d S )Nr   )JpegImageFiler4   r   )ZPIL.JpegImagePluginrx   r   rW   re   rK   r^   tellr_   r   rQ   rJ   rN   )rR   rx   Zjpeg_header_sizeZjpeg_headerr   imager   r   r   rr   Z  s    zBLP1Decoder._decode_jpeg_streamN)rA   rB   rC   rg   rr   r   r   r   r   ro   >  s   ro   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rt| j	| j
d }ztd|	d\}W n tjk
r~   Y qY nX || \}}}}||||f qLq| jtkr| jtkr| jd d d d }	t| jd d d D ].}
t| j	|	t| jdD ]}||7 }qqn| jtkr~| jd d d d }	t| jd d d D ]&}
t| j	|	D ]}||7 }qhqTnp| jtkr| jd d d d }	t| jd d d D ]&}
t| j	|	D ]}||7 }qʐqntd	| j ntd
| j ntd| j | t| d S )Nr   r   rp   r   r   r   )r   r2   zUnsupported alpha encoding %rzUnknown BLP encoding %rzUnknown BLP compression %r)rn   r   re   rf   r^   rX   rY   BLP_ENCODING_UNCOMPRESSEDr   rK   r_   r   rW   rh   r   BLP_ENCODING_DXTrZ   BLP_ALPHA_ENCODING_DXT1rP   r   r1   boolrO   BLP_ALPHA_ENCODING_DXT3r<   BLP_ALPHA_ENCODING_DXT5r?   r@   rs   rt   )rR   ru   r   rv   rw   r0   r/   r.   r-   ZlinesizeZybdr   r   r   rg   j  sJ    


 
zBLP2Decoder._loadN)rA   rB   rC   rg   r   r   r   r   r{   i  s   r{   c                 C   s   | d d dkS )Nr   )rE   rG   r   )pr   r   r   <lambda>      r   z.blprF   rH   )F)ra   r   ior    r   r   rq   r|   r}   Z"BLP_ENCODING_UNCOMPRESSED_RAW_BGRAr~   r   r   r   r1   r<   r?   NotImplementedErrorr@   rD   Z	PyDecoderrd   ro   r{   Zregister_openrb   Zregister_extensionZregister_decoderr   r   r   r   <module>   s6   
75F**+5  