ó
È4ec           @   sM  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
 „  Z e d „ Z d „  Z d „  Z d e f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e j e j e d „  ƒ e j e j d ƒ e j d e ƒ e j d e ƒ d S(   sÑ  
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.
iÿÿÿÿN(   t   BytesIOi   (   t   Imaget	   ImageFilei    i   i   i   c         C   s-   |  d ?d @d >|  d ?d @d >|  d @d >f S(   Ni   i   i   i   i?   i   (    (   t   i(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyt
   unpack_5650   s    c         C   sM  t  |  ƒ d } t ƒ  t ƒ  t ƒ  t ƒ  f } xt | ƒ D]
} | d } t j d |  | ƒ \ } } } t | ƒ \ }	 }
 } t | ƒ \ } } } x¯t d ƒ D]¡} x˜t d ƒ D]Š} | d @} | d ?} d } | d k rö |	 |
 | } } } n| d k r| | | } } } nä | d k r—| | k rjd |	 | d } d |
 | d } d | | d } qý|	 | d } |
 | d } | | d } nf | d k rý| | k rèd | |	 d } d | |
 d } d | | d } qýd	 \ } } } } n  | r#| | j | | | | g ƒ q³ | | j | | | g ƒ q³ Wq  Wq; W| S(
   sE   
    input: one "row" of data (i.e. will produce 4*width pixels)
    i   s   <HHIi   i   i   iÿ   i    i   (   i    i    i    i    (   t   lent	   bytearrayt   ranget   structt   unpack_fromR   t   extend(   t   datat   alphat   blockst   rett   blockt   idxt   color0t   color1t   bitst   r0t   g0t   b0t   r1t   g1t   b1t   jR   t   controlt   at   rt   gt   b(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyt   decode_dxt14   sD    


 &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 | ƒ \ } } } x‡t d ƒ D]y} t } xjt d ƒ D]\} d | | d } | | } | r,t } | d L} n t } | d	 M} | d
 9} | d d | | ?d @} | d k rƒ|	 |
 | } } } n­ | d k r¦| | | } } } nŠ | d k rëd |	 | d } d |
 | d } d | | d } nE | d k r0d | |	 d } d | |
 d } d | | d } n  | | j | | | | g ƒ qñ WqØ Wq; W| S(   sE   
    input: one "row" of data (i.e. will produce 4*width pixels)
    i   s   <8Bs   <HHi   s   <Ii   i   i   i   i   i   i    i   (	   R   R   R   R   R	   R   t   Falset   TrueR
   (   R   R   R   R   R   R   R   R   t   codeR   R   R   R   R   R   R   t   highR   t   alphacode_indexR   t
   color_codeR   R   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyt   decode_dxt3k   sH    



)c         C   sl  t  |  ƒ d } t ƒ  t ƒ  t ƒ  t ƒ  f } x7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 | ƒ \ } } } x7t d ƒ D])} x t d ƒ D]} d d | | } | d k r|	 | ?d @} n7 | d k r¤|	 d ?| d >d @B} n | | d ?d @} | d
 k rË| } nŠ | d k rà| } nu | | k rd | | | d | d } nH | d k r"d
 } n3 | d k r7d } n d | | | d | d } | d d | | ?d @} | d
 k r’| | | } } } n­ | d k rµ| | | } } } nŠ | d k rúd | | d } d | | d } d | | d } nE | d k r?d | | d } d | | d } d | | d } n  | | j | | | | g ƒ qJWq7Wq; W| S(   sG   
    input: one "row" of data (i.e. will produce 4 * width pixels)
    i   s   <BBs   <6Bi   i   i   i   i   i   i    i   s   <HHs   <Ii   i   i   i   iÿ   (   R   R   R   R   R	   R   R
   (   R   R   R   R   R   t   a0t   a1R   t
   alphacode1t
   alphacode2R   R   R#   R   R   R   R   R   R   R   R   R%   t	   alphacodeR   R&   R   R   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyt   decode_dxt5    s^    
.		!		)t   BLPFormatErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyR.   æ   s   t   BlpImageFilec           B   s,   e  Z d  Z d Z d Z d „  Z d „  Z RS(   s    
    Blizzard Mipmap Format
    t   BLPs   Blizzard Mipmap Formatc         C   s²   |  j  j d ƒ |  _ |  j ƒ  |  j d k r@ d } d |  _ nC |  j d k rp d } |  j rd d n d |  _ n t d |  j ƒ ‚ | d	 |  j d |  j d d f f g |  _ d  S(
   Ni   t   BLP1t   RGBt   BLP2t   RGBAs   Bad BLP magic %ri    i   (   i    i    (	   t   fpt   readt   magict   _read_blp_headert   modet   _blp_alpha_depthR.   t   sizet   tile(   t   selft   decoder(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyt   _openò   s    
c         C   su  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 ƒ ƒ \ |  _ n  t  j d |  j j d
 ƒ ƒ |  _ t  j d |  j j d ƒ ƒ |  _ d  S(   Ns   <ii   s   <bi   s   <IIi   R3   s   <16Ii   i@   i@   (   R   t   unpackR7   R8   t   _blp_compressiont   _blp_encodingR<   t   _blp_alpha_encodingt	   _blp_mipst   _sizeR9   t   _blp_subtypet   _blp_offsetst   _blp_lengths(   R?   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyR:     s    $$$$$!$'!(   R/   R0   t   __doc__t   formatt   format_descriptionRA   R:   (    (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyR1   ê   s
   	t   _BLPBaseDecoderc           B   s)   e  Z e Z d  „  Z d „  Z d „  Z RS(   c         C   sd   y= |  j  j d ƒ |  j  j d ƒ |  _ |  j ƒ  |  j ƒ  Wn  t j k
 r_ t d ƒ ‚ n Xd S(   Ni    i   s   Truncated Blp file(   i    i    (	   t   fdt   seekR8   R9   R:   t   _loadR   t   errort   IOError(   R?   t   buffer(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyt   decode  s    
c         C   s€   g  } xs t  d ƒ D]e } y. t j d |  j j d ƒ ƒ \ } } } } Wn t j k
 r^ Pn X| j | | | | f ƒ q W| S(   Ni   s   <4Bi   (   R   R   RB   RO   R8   RR   t   append(   R?   R   R   R   R   R   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyt   _read_palette!  s    .c         C   su  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 ƒ ƒ \ |  _ n  t  j d |  j j d
 ƒ ƒ |  _ t  j d |  j j d ƒ ƒ |  _ d  S(   Ns   <ii   s   <bi   s   <IIi   R3   s   <16Ii   i@   i@   (   R   RB   RO   R8   RC   RD   R<   RE   RF   R=   R9   RH   RI   RJ   (   R?   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyR:   +  s    $$$$$!$'!(   R/   R0   R"   t	   _pulls_fdRU   RW   R:   (    (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyRN     s   	
	
t   BLP1Decoderc           B   s   e  Z d  „  Z d „  Z RS(   c   	      C   s$  |  j  t k r |  j ƒ  n|  j  d k r|  j d k r÷ t ƒ  } |  j ƒ  } t |  j j |  j	 d ƒ ƒ } xp t
 rà y" t j d | j d ƒ ƒ \ } Wn t j k
 r° Pn X| | \ } } } } | j | | | g ƒ qq W|  j t | ƒ ƒ q t d |  j ƒ ‚ n t d |  j ƒ ‚ d  S(	   Ni   i   i   i    s   <Bs   Unsupported BLP encoding %rs   Unsupported BLP compression %r(   i   i   (   RC   t   BLP_FORMAT_JPEGt   _decode_jpeg_streamRD   R   RW   R    RO   R8   RJ   R"   R   RB   RR   R
   t
   set_as_rawt   bytesR.   (	   R?   R   t   palettet   _datat   offsetR   R   R   R   (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyRQ   ?  s&    		"c         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(   Niÿÿÿÿ(   t   JpegImageFiles   <Ii   i    (   t   PIL.JpegImagePluginRa   R   RB   RO   R8   RI   t   tellRJ   R    R>   R7   R;   (   R?   Ra   t   jpeg_header_sizet   jpeg_headerR   t   image(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyR[   Z  s    !$
(   R/   R0   RQ   R[   (    (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyRY   >  s   	t   BLP2Decoderc           B   s   e  Z d  „  Z RS(   c         C   sÍ  |  j  ƒ  } t ƒ  } |  j j |  j d ƒ |  j d k r£|  j t k rß t |  j j	 |  j
 d ƒ ƒ } x4t rÛ y" t j d | j	 d ƒ ƒ \ } Wn t j k
 r« Pn X| | \ } } } } | j | | | f ƒ ql Wq¶|  j t k r|  j t k r{|  j d d d d }	 xqt |  j d d d ƒ D]B }
 x9 t |  j j	 |	 ƒ d t |  j ƒ ƒD] } | | 7} q`Wq2Wq |  j t k rù|  j d d d d }	 xä t |  j d d d ƒ D]3 }
 x* t |  j j	 |	 ƒ ƒ D] } | | 7} qÞWq¿Wq |  j t k rw|  j d d d d }	 xf t |  j d d d ƒ D]3 }
 x* t |  j j	 |	 ƒ ƒ D] } | | 7} q\Wq=Wq t d	 |  j ƒ ‚ q¶t d
 |  j ƒ ‚ n t d |  j ƒ ‚ |  j t | ƒ ƒ d  S(   Ni    i   s   <Bi   i   i   R   i   s   Unsupported alpha encoding %rs   Unknown BLP encoding %rs   Unknown BLP compression %r(   RW   R   RO   RP   RI   RC   RD   t   BLP_ENCODING_UNCOMPRESSEDR    R8   RJ   R"   R   RB   RR   R
   t   BLP_ENCODING_DXTRE   t   BLP_ALPHA_ENCODING_DXT1R=   R   R    t   boolR<   t   BLP_ALPHA_ENCODING_DXT3R'   t   BLP_ALPHA_ENCODING_DXT5R-   R.   R\   R]   (   R?   R^   R   R_   R`   R   R   R   R   t   linesizet   ybt   d(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyRQ   j  sF    		""(""(   R/   R0   RQ   (    (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyRg   i  s   c         C   s   |  d  d k S(   Ni   R3   R5   (   R3   R5   (    (   t   p(    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyt   <lambda>Ÿ  t    s   .blpR3   R5   (   RK   R   t   ioR    Rs   R   R   RZ   Rh   Ri   t"   BLP_ENCODING_UNCOMPRESSED_RAW_BGRARj   Rl   Rm   R   R!   R    R'   R-   t   NotImplementedErrorR.   R1   t	   PyDecoderRN   RY   Rg   t   register_openRL   t   register_extensiont   register_decoder(    (    (    s<   /usr/local/lib/python2.7/dist-packages/PIL/BlpImagePlugin.pyt   <module>   s0   	7	5	F**+5