3

h8                 @   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jejee 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   R/var/www/html/enquirykeeper_venv/lib/python3.6/site-packages/PIL/BlpImagePlugin.py
unpack_5650   s    r   Fc             C   s  t | d }t t t t f}xt|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rd| |	 d }d| |
 d }d| | d }nd	\}}}}|r|| j||||g q|| j|||g qW 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}xt|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	 }|| }|rd}|dL }nd
}|dM }|d9 }|d	d| |  ? d@ }|dkr.|	|
|  }}}n|dkrJ|||  }}}nv|d	krd	|	 | d }d	|
 | d }d	| | d }n:|dkrd	| |	 d }d	| |
 d }d	| | d }|| j||||g qW qW 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}xt|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|\}}}xtdD ]}xt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rd| | |d |  d }n<|dkrd
}n,|dkrd}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rd| | d }d| | d }d| | d }|| j||||g qW qW 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| _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 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rt 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   @   rO   )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               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )_BLPBaseDecoderTc             C   sb   y.| j jd | j jd| _| j  | j  W n. tjk
r\ } ztd|W Y d d }~X 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   s   t j| j|S )N)r   
_safe_readr]   )rL   lengthr   r   r   re   !  s    z_BLPBaseDecoder._safe_readc             C   sd   g }xZt dD ]N}ytjd| jd\}}}}W n tjk
rH   P Y nX |j||||f qW |S )N   z<4Br   )r   r   rP   re   r`   append)rL   r   r   r.   r-   r,   r+   r   r   r   _read_palette$  s    z_BLPBaseDecoder._read_palettec             C   s   t jd| jd\| _t jd| jd\| _t jd| jd\| _t jd| jd\| _t jd| jd\| _t jd| jd| _| j	dkrt jd| jd\| _t jd| jd\| _
t jd| jd
| _t jd| jd| _d S )Nz<ir   z<br   z<IIr   s   BLP1z<16Ir0   rO   rO   )r   rP   re   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   re   ri   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rt }| j }t| j| jd }xVyt	j
d|jd\}W n t	jk
r|   P Y nX || \}}}}|j|||g qLW | jt| qtdt| j ntdt| j d S )	Nr   r   r   r   z<BzUnsupported BLP encoding zUnsupported BLP compression )r   r   )rQ   BLP_FORMAT_JPEG_decode_jpeg_streamrR   r   ri   r   re   rX   r   rP   rD   r`   r   
set_as_rawbytesr<   rI   )	rL   r   palette_dataoffsetr.   r-   r,   r+   r   r   r   r_   B  s&    



zBLP1Decoder._loadc             C   s   ddl m} tjd| jd\}| j|}| j| jd | jj   | j| jd }|| }t	|}||}t
j|j |j| _|j| _|j| _d S )Nr   )JpegImageFilez<Ir   )ZPIL.JpegImagePluginrr   r   rP   re   rW   r]   tellrX   r   r   Z_decompression_bomb_checkrJ   rK   rC   rG   )rL   rr   Zjpeg_header_sizeZjpeg_headerr   imager   r   r   rl   ]  s    
zBLP1Decoder._decode_jpeg_streamN)r=   r>   r?   r_   rl   r   r   r   r   rj   A  s   rj   c               @   s   e Zd Zdd ZdS )BLP2Decoderc             C   sD  | j  }t }| jj| jd  | jdkr| jtkrt| j	| j
d }xVytjd|jd\}W n tjk
r|   P Y nX || \}}}}|j|||f qLW q2| jtkr| jtkr"| jd d d d }	xJt| jd d d D ]0}
x*t| j	|	t| jdD ]}||7 }q
W qW n| jtkr| jd d d d }	xt| jd d d D ](}
x t| j	|	D ]}||7 }qpW q\W n|| jtkr| jd d d d }	xXt| jd d d D ](}
x t| j	|	D ]}||7 }qW qW ntd	t| j ntd
t| j ntdt| j | jt| d S )Nr   r   z<Br   r   r   )r   r0   zUnsupported alpha encoding zUnknown BLP encoding zUnknown BLP compression ) ri   r   r]   r^   rW   rQ   rR   BLP_ENCODING_UNCOMPRESSEDr   re   rX   r   rP   rD   r`   r   BLP_ENCODING_DXTrS   BLP_ALPHA_ENCODING_DXT1rJ   r   r/   boolrH   BLP_ALPHA_ENCODING_DXT3r8   BLP_ALPHA_ENCODING_DXT5r;   r<   rI   rm   rn   )rL   ro   r   rp   rq   r.   r-   r,   r+   ZlinesizeZybdr   r   r   r_   n  sH    
zBLP2Decoder._loadN)r=   r>   r?   r_   r   r   r   r   ru   m  s   ru   c             C   s   | d d dkS )Nr      BLP1   BLP2)r}   r~   r   )prefixr   r   r   _accept  s    r   z.blprA   rB   )F)rY   r   ior    r   r   rk   rv   rw   Z"BLP_ENCODING_UNCOMPRESSED_RAW_BGRArx   rz   r{   r   r/   r8   r;   NotImplementedErrorr<   r@   Z	PyDecoderr\   rj   ru   r   Zregister_openrZ   Zregister_extensionZregister_decoderr   r   r   r   <module>   s0   
75F*-,7