a
    i=ic=                     @   sn  d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	 ddl
mZ G dd	 d	eZG d
d deZG dd deZdd Zdd Zd-ddZdd Zdd ZG dd deZdd 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G d%d& d&e	jZd.d'd(Zeej ee e!ej d) e"d*e e"d+e e#ej 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)IntEnum)BytesIO   )Image	ImageFile)	deprecatec                   @   s   e Zd ZdZdS )Formatr   N)__name__
__module____qualname__JPEG r   r   c/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/PIL/BlpImagePlugin.pyr   )   s   r   c                   @   s   e Zd ZdZdZdZdS )Encodingr         N)r	   r
   r   UNCOMPRESSEDDXTZUNCOMPRESSED_RAW_BGRAr   r   r   r   r   -   s   r   c                   @   s   e Zd ZdZdZdZdS )AlphaEncodingr   r      N)r	   r
   r   DXT1DXT3DXT5r   r   r   r   r   3   s   r   c                 C   s   t dtdtdi D ]X\}}| |r| t|d  } | |jv rt| |  d|j d|   ||    S qt	dt d|  dd S )	NZBLP_FORMAT_ZBLP_ENCODING_ZBLP_ALPHA_ENCODING_
   .zmodule 'z' has no attribute '')
r   r   r   items
startswithlen__members__r   r	   AttributeError)nameenumprefixr   r   r   __getattr__9   s    

 r$   c                 C   s*   | d? d@ d> | d? d@ d> | d@ d> fS )N      r      ?   r   r   )ir   r   r   
unpack_565G   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   r   	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_dxt1K   sD    



rJ   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/   )r5   r7   r8   r9   r:   r=   r;   r<   coder>   r?   r@   rA   rB   rC   rD   highr)   alphacode_indexrF   
color_coderG   rH   rI   r   r   r   decode_dxt3   sH    




rU   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)
    rK   z<BBz<6Br   r   r,   r-   r'      r   r   rL   rM   rN   r   rO      r.   r/   )r5   r7   r8   r9   r:   Za0Za1r=   Z
alphacode1Z
alphacode2r;   r<   rQ   r>   r?   r@   rA   rB   rC   rD   r)   rS   Z	alphacoderF   rT   rG   rH   rI   r   r   r   decode_dxt5   s^    ,










rX   c                   @   s   e Zd ZdS )BLPFormatErrorN)r	   r
   r   r   r   r   r   rY      s   rY   c                 C   s   | d d dv S )Nr-      BLP1   BLP2r   )r#   r   r   r   _accept  s    r]   c                   @   s    e Zd ZdZdZdZdd ZdS )BlpImageFilez 
    Blizzard Mipmap Format
    BLPzBlizzard Mipmap Formatc                 C   s   | j d| _| j dtj td| j d\| _| j dtj td| j d| _	| jdv rr| j
 }ntd	t| j | jrd
nd| _|d| j d| jddffg| _d S )Nr-   r'   <br   r   <IIr,   rZ   zBad BLP magic RGBARGBr   r   r   )fpreadmagicseekosSEEK_CURr2   unpack_blp_alpha_depth_sizedecoderY   reprmodesizetile)selfdecoderr   r   r   _open  s    
zBlpImageFile._openN)r	   r
   r   __doc__formatformat_descriptionru   r   r   r   r   r^     s   r^   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )_BLPBaseDecoderTc              
   C   sJ   z|    |   W n0 tjyD } ztd|W Y d }~n
d }~0 0 dS )NzTruncated BLP file)r   )_read_blp_header_loadr2   errorOSError)rs   bufferer   r   r   rn   "  s     z_BLPBaseDecoder.decodec                 C   s   | j d td| d\| _td| d\| _td| d\| _td| d\| _| j dt	j
 td| d| _t| trtd| d\| _| j dt	j
 td| d| _td| d| _d S )	Nr-   <ir`   r   ra   r,   <16I@   )fdrh   r2   rk   
_safe_read_blp_compression_blp_encodingrl   _blp_alpha_encodingri   rj   rq   
isinstanceBLP1Decoder_blp_offsets_blp_lengths)rs   r   r   r   r{   *  s    
z _BLPBaseDecoder._read_blp_headerc                 C   s   t | j|S )N)r   r   r   )rs   lengthr   r   r   r   =  s    z_BLPBaseDecoder._safe_readc              	   C   sb   g }t dD ]P}ztd| d\}}}}W n tjyH   Y  q^Y n0 |||||f q|S )N   <4Br-   )r1   r2   rk   r   r}   append)rs   r8   r)   rI   rH   rG   rF   r   r   r   _read_palette@  s    z_BLPBaseDecoder._read_palettec           
      C   s   t  }t| | jd }ztd|d\}W n tjyJ   Y qY n0 || \}}}}|||f}	| jrv|	|f7 }	|	|	 q|S )Nr   <Br   )
r0   r   r   r   r2   rk   rf   r}   rl   r4   )
rs   paletter5   _dataoffsetrI   rH   rG   rF   dr   r   r   
_read_bgraJ  s    


z_BLPBaseDecoder._read_bgraN)	r	   r
   r   Z	_pulls_fdrn   r{   r   r   r   r   r   r   r   ry     s   
ry   c                   @   s   e Zd Zdd Zdd ZdS )r   c                 C   sz   | j tjkr|   n`| j dkrb| jdv rL|  }| |}| t| qvt	dt
| j nt	dt
| j d S )Nr   )r-   r'   zUnsupported BLP encoding zUnsupported BLP compression )r   r   r   _decode_jpeg_streamr   r   r   
set_as_rawbytesrY   ro   )rs   r   r5   r   r   r   r|   [  s    



zBLP1Decoder._loadc                 C   s   ddl m} td| d\}| |}| | jd | j   | | jd }|| }t	|}||}t
|j d|_dd| j dd	fg|_| |  d S )
Nr   )JpegImageFilerM   r-   r   rc   Zjpegrd   )ZBGRX )ZJpegImagePluginr   r2   rk   r   r   r   tellr   r   r   Z_decompression_bomb_checkrq   rp   rr   r   tobytes)rs   r   Zjpeg_header_sizeZjpeg_headerr5   imager   r   r   r   m  s    
zBLP1Decoder._decode_jpeg_streamN)r	   r
   r   r|   r   r   r   r   r   r   Z  s   r   c                   @   s   e Zd Zdd ZdS )BLP2Decoderc                 C   s  |   }| j| jd  | jdkr| jtjkr@| |}q| jtj	krt
 }| jtjkr| jd d d d }t| jd d d D ]*}t| |t| jdD ]}||7 }qqn| jtjkr| jd d d d }t| jd d d D ]"}t| |D ]}||7 }qqnv| jtjkr|| jd d d d }t| jd d d D ]$}t| |D ]}||7 }qfqTntdt| j ntd	t| j ntd
t| j | t| d S )Nr   r   r   r-   r,   )r6   rK   zUnsupported alpha encoding zUnknown BLP encoding zUnknown BLP compression )r   r   rh   r   r   r   r   r   r   r   r0   r   r   r   rq   r1   rJ   r   boolrl   r   rU   r   rX   rY   ro   r   r   )rs   r   r5   ZlinesizeZybr   r   r   r   r|   ~  s@    
zBLP2Decoder._loadN)r	   r
   r   r|   r   r   r   r   r   }  s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )
BLPEncoderTc              	   C   sZ   d}| j dd}tdD ]:}||d |d d  \}}}}|td||||7 }q|S )N    rb   r   r-   r   r   )imZ
getpaletter1   r2   pack)rs   r5   r   r)   rG   rH   rI   rF   r   r   r   _write_palette  s     zBLPEncoder._write_palettec           	   
   C   s   |   }dt| }tjd|gdR  }| jj\}}|tjd|| gdR  7 }||7 }t|D ].}t|D ] }|td| j||f7 }qlq`t|d|fS )N   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r2   r   r   rq   r1   Zgetpixel)	rs   bufsizeZpalette_datar   r5   whyxr   r   r   encode  s     zBLPEncoder.encodeN)r	   r
   r   Z
_pushes_fdr   r   r   r   r   r   r     s   r   c                 C   s
  | j dkrtd| jddkr&dnd}|| |tdd |td	tj |td	| j	j d
krtdnd |td	d |td	d |tjdg| j
R   |dkr|tdd |tdd t| |dd| j
 d| j fg d S )NPzUnsupported BLP image modeZblp_versionBLP1r[   r\   r   r   r`   rb   r   ra   r'   r_   rd   )rp   
ValueErrorZencoderinfogetwriter2   r   r   r   r   rq   r   _save)r   re   filenameZsave_allrg   r   r   r   r     s    

"r   z.blpr   ZBLP2r_   )F)F)%rv   ri   r2   r"   r   ior   r   r   r   Z
_deprecater   r   r   r   r$   r*   rJ   rU   rX   NotImplementedErrorrY   r]   r^   Z	PyDecoderry   r   r   Z	PyEncoderr   r   Zregister_openrw   Zregister_extensionZregister_decoderZregister_saveZregister_encoderr   r   r   r   <module>   s8   
75F;#0
