
    h@                       S r SSKJr  SSKrSSKrSSKrSSKJr  SSKJ	r	  SSK
Jr  SSKJrJr   " S	 S
\5      r " S S\5      r " S S\5      rS%S jr S&     S'S jjrS(S jrS(S jr " S S\5      rS)S jr " S S\R                  5      r " S S\R4                  \R6                  5      r " S S\5      r " S S\5      r " S S\R>                  5      r S*S  jr!\RD                  " \RF                  \\5        \RH                  " \RF                  S!5        \RJ                  " S"\5        \RJ                  " S#\5        \RL                  " \RF                  \!5        \RN                  " S$\ 5        g)+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.
    )annotationsN)IntEnum)BytesIO)IO   )Image	ImageFilec                      \ rS rSrSrSrg)Format,   r    N)__name__
__module____qualname____firstlineno__JPEG__static_attributes__r       D/var/www/html/env/lib/python3.13/site-packages/PIL/BlpImagePlugin.pyr   r   ,   s    Dr   r   c                       \ rS rSrSrSrSrSrg)Encoding0   r         r   N)r   r   r   r   UNCOMPRESSEDDXTUNCOMPRESSED_RAW_BGRAr   r   r   r   r   r   0   s    L
Cr   r   c                       \ rS rSrSrSrSrSrg)AlphaEncoding6   r   r      r   N)r   r   r   r   DXT1DXT3DXT5r   r   r   r   r   r   6   s    DDDr   r   c                <    U S-	  S-  S-  U S-	  S-  S-  U S-  S-  4$ )N      r      ?   r   r   )is    r   
unpack_565r+   <   s2    "W"a1f_$:QX!OKKr   c           	        [        U 5      S-  n[        5       [        5       [        5       [        5       4n[        U5       GH4  nUS-  n[        R                  " SX5      u  pgn[        U5      u  pn[        U5      u  pn[        S5       H  n[        S5       H  nUS-  nUS-	  nSnUS:X  a  XUnnnOUS:X  a  XUnnnOwUS:X  a=  Xg:  a"  SU	-  U-   S-  nSU
-  U-   S-  nSU-  U-   S-  nOJX-   S-  nX-   S-  nX-   S-  nO4US:X  a.  Xg:  a"  SU-  U	-   S-  nSU-  U
-   S-  nSU-  U-   S-  nOS	u  nnnnU(       a  X?   R                  WWWU/5        M  X?   R                  WWW/5        M     M     GM7     U$ )
=
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_indexidxcolor0color1bitsr0g0b0r1g1b1jr*   controlargbs                         r   decode_dxt1rM   @   s    Y!^F;	Y[)+
>CV}Ao%11&$D'
'
 qA1X (qya< b!qA!\ b!qA!\Vb[Q.Vb[Q.Vb[Q.WNWNWN\Vb[Q.Vb[Q.Vb[Q.%/
1aFMM1aA,/FMM1a),?   %X Jr   c           	     >   [        U 5      S-  n[        5       [        5       [        5       [        5       4n[        U5       GHX  nUS-  nXUS-    n[        R                  " SU5      n[        R                  " SUS5      u  px[        R                  " SUS5      u  n	[        U5      u  pn[        U5      u  pn[        S5       H  nSn[        S5       H  nSU-  U-   S	-  nUU   nU(       a  SnUS-  nOS
nUS-  nUS-  nU	S	SU-  U-   -  -	  S-  nUS:X  a  XUnnnO[US:X  a  XUnnnOOUS	:X  a"  S	U
-  U-   S-  nS	U-  U-   S-  nS	U-  U-   S-  nO'US:X  a!  S	U-  U
-   S-  nS	U-  U-   S-  nS	U-  U-   S-  nUU   R                  WWWU/5        M     M     GM[     U$ )r-      z<8B<HHr.   <I   r/   Fr   T      r   r   r   r1   )r8   r:   r;   r<   r=   blockr@   r>   r?   coderA   rB   rC   rD   rE   rF   rG   highr*   alphacode_indexrI   
color_coderJ   rK   rL   s                            r   decode_dxt3rZ   y   s   
 Y"_F;	Y[)+
>CV}B38$!!%/++E5!<$$T5"5'
'
qAD1X#$q519"2) D!GADHAR"a1q519o5=
? b!qA!1_ b!qA!1_R"*AR"*AR"*A1_R"*AR"*AR"*AAq!Ql+5   %T Jr   c           	        [        U 5      S-  n[        5       [        5       [        5       [        5       4n[        U5       GH  nUS-  nXUS-    n[        R                  " SU5      u  pg[        R                  " SUS5      nUS   US   S-  -  US   S-  -  US   S	-  -  n	US
   US   S-  -  n
[        R                  " SUS5      u  p[        R                  " SUS5      u  n[        U5      u  pn[        U5      u  nnn[        S5       GH5  n[        S5       GH!  nSSU-  U-   -  nUS::  a	  U
U-	  S-  nO US:X  a  U
S-	  U	S-  S-  -  nOU	US-
  -	  S-  nUS
:X  a  UnOIUS:X  a  UnO@Xg:  a  SU-
  U-  US-
  U-  -   S-  nO&US:X  a  S
nOUS:X  a  SnOSU-
  U-  US-
  U-  -   S-  nUSSU-  U-   -  -	  S-  nUS
:X  a  XUnnnO\US:X  a  UUUnnnOOUS:X  a"  SU-  U-   S-  nSU-  U-   S-  nSU-  U-   S-  nO'US:X  a!  SU-  U-   S-  nSU-  U-   S-  nSU-  U-   S-  nUU   R                  WWWU/5        GM$     GM8     GM     U$ )z?
input: one "row" of data (i.e. will produce 4 * width pixels)
rO   z<BBz<6Br   r   r.   r/   r(      r   r   rP   rQ   rR   r!   rS      r0   r1   )r8   r:   r;   r<   r=   rU   a0a1r@   
alphacode1
alphacode2r>   r?   rV   rA   rB   rC   rD   rE   rF   rG   r*   rX   	alphacoderI   rY   rJ   rK   rL   s                                r   decode_dxt5rc      s   
 Y"_F;	Y[)+
>CV}B38$##E51!!%2!WQ1-aB?47b=Q
!WQ1-
++E5!<$$T5"5'
'
BqA1X"#q1uqy/"b(!+!>$ FI$*!+r!1zQ$6N OI!+"0D!E MI>A!^AWi-2-Q"0DDJA!^A!^Ai-2-Q"0DDJA"a1q519o5=
? b!qA!1_ "b!qA!1_R"*AR"*AR"*A1_R"*AR"*AR"*AAq!Ql+O  # %v Jr   c                      \ rS rSrSrg)BLPFormatError   r   N)r   r   r   r   r   r   r   r   re   re      s    r   re   c                $    U R                  S5      $ )N)   BLP1   BLP2)
startswith)prefixs    r   _acceptrl      s    /00r   c                  *    \ rS rSrSrSrSrSS jrSrg)	BlpImageFile   z
Blizzard Mipmap Format
BLPzBlizzard Mipmap Formatc                J   U R                   R                  S5      U l        [        U R                  5      (       d#  S[	        U R                  5       3n[        U5      e[        R                  " SU R                   R                  S5      5      S   nU R                  S:X  a7  [        R                  " SU R                   R                  S5      5      S   S:g  nO[        R                  " SU R                   R                  S5      5      S   n[        R                  " SU R                   R                  S5      5      S   S:g  n[        R                  " SU R                   R                  S5      5      S   nU R                   R                  S[        R                  5        [        R                  " S	U R                   R                  S
5      5      U l        U R                  S:X  ad  [        R                  " SU R                   R                  S5      5      S   nU R                   R                  S[        R                  5        X$U4nSnOUWUW4nSnU R                  R                  5       nU(       a  SOSU l        [        R                  " USU R                   -   Xv5      /U l        g )Nr/   zBad BLP magic <ir   rh   rQ   <br   <IIr.         RGBARGBr   r   )fpreadmagicrl   reprre   r5   unpackseekosSEEK_CUR_sizedecode_moder	   _Tilesizetile)	selfmsgcompressionr9   encodingalpha_encodingargsoffsetdecoders	            r   _openBlpImageFile._open  s   WW\\!_
tzz"""4

#3"45C %%mmD$'',,q/:1=:: MM$Q8;q@E}}T477<<?;A>HMM$Q8;q@E#]]4aA!DNGGLLBKK(]]5$'',,q/:
 :: }}T477<<?;A>HGGLLBKK(51DF5.ADF**##%$V%
__Wftyy.@&OP	r   )r   r   r|   r   NreturnNone)	r   r   r   r   __doc__formatformat_descriptionr   r   r   r   r   rn   rn      s     F1Qr   rn   c                  ~    \ rS rSrSrSS jr\R                  SS j5       rSS jr	SS jr
SS jr      SS jrS	rg
)_BLPBaseDecoderi&  Tc                     U R                  5         U R                  5         g! [        R                   a  nSn[	        U5      UeS nAff = f)NzTruncated BLP file)r   )_read_header_loadr5   errorOSError)r   bufferer   s       r   r   _BLPBaseDecoder.decode)  sH    	&JJL  || 	&&C#,A%	&s    # A
AA
c                    g Nr   r   s    r   r   _BLPBaseDecoder._load2  s    r   c                    [         R                  " SU R                  S5      5      U l        [         R                  " SU R                  S5      5      U l        g )N<16I@   )r5   r~   
_safe_read_offsets_lengthsr   s    r   r   _BLPBaseDecoder._read_header6  s8    fdoof.EFfdoof.EFr   c                b    U R                   c   e[        R                  " U R                   U5      $ r   )fdr	   r   )r   lengths     r   r   _BLPBaseDecoder._safe_read:  s)    ww"""##DGGV44r   c                    / n[        S5       H@  n [        R                  " SU R                  S5      5      u  p4pVUR                  X4XV45        MB     U$ ! [        R                   a       U$ f = f)N   <4Br/   )r4   r5   r~   r   r   append)r   r;   r*   rL   rK   rJ   rI   s          r   _read_palette_BLPBaseDecoder._read_palette>  sm    sA#]]5$//!2DE
a JJa|$  
 << 
s   )AA-,A-c                B   [        5       n[        U R                  U R                  S   5      5      n  [        R
                  " SUR                  S5      5      u  nX   u  pgpXU4n
U(       a  X4-  n
UR                  U
5        MS  ! [        R                   a     U$ f = f)Nr   <Br   )	r3   r   r   r   r5   r~   r{   r   r7   )r   paletter9   r8   _datar   rL   rK   rJ   rI   ds              r   
_read_bgra_BLPBaseDecoder._read_bgraH  s     {a(89:"MM$

1>	 !JA!"#AT	KKN  <<  s   (B BB)r   r   N)r   z$bytes | Image.SupportsArrayInterfacer   ztuple[int, int]r   )r   intr   bytes)r   list[tuple[int, int, int, int]])r   r   r9   boolr   r3   )r   r   r   r   	_pulls_fdr   abcabstractmethodr   r   r   r   r   r   r   r   r   r   r   &  sS    I 	 G56?C	r   r   c                  (    \ rS rSrSS jrSS jrSrg)BLP1DecoderiZ  c                   U R                   u  U l        U l        nU R                  [        R                  :X  a  U R                  5         g U R                  S:X  af  U R                  S;   a3  U R                  5       nU R                  X!5      nU R                  U5        g S[        U R                  5       3n[        U5      eS[        U R                  5       3n[        U5      e)Nr   )r/   r(   zUnsupported BLP encoding zUnsupported BLP compression )r   _compression	_encodingr   r   _decode_jpeg_streamr   r   
set_as_rawr}   re   )r   r9   r   r8   r   s        r   r   BLP1Decoder._load[  s    379904>5+$$&!#~~',,.w6%1$t~~2F1GH$S))0dnn1E0FGC %%r   c                   SSK Jn  [        R                  " SU R	                  S5      5      u  nU R	                  U5      nU R
                  c   eU R	                  U R                  S   U R
                  R                  5       -
  5        U R	                  U R                  S   5      nX4-   nU" [        U5      5      n[        R                  " UR                  5        UR                  S:X  aW  UR                  S   R                  n[!        U["        5      (       d   eUR                  S   R%                  US   S4S9/Ul        U R'                  UR)                  S5      R+                  5       S	5        g )
Nr   )JpegImageFilerQ   r/   r   CMYK)r   rx   BGR)JpegImagePluginr   r5   r~   r   r   r   tellr   r   r   _decompression_bomb_checkr   moder   r   
isinstancetuple_replacer   converttobytes)r   r   jpeg_header_sizejpeg_headerr8   imager   s          r   r   BLP1Decoder._decode_jpeg_streamm  s   2$mmD$//!2DE	oo&67ww"""a(477<<>9:t}}Q/0!gdm,''

3::::a=%%DdE******Q-00tAw6G0HIEJe,446>r   )r   r   Nr   )r   r   r   r   r   r   r   r   r   r   r   r   Z  s    &$?r   r   c                      \ rS rSrSS jrSrg)BLP2Decoderi  c                   U R                   u  U l        U l        ol        U R	                  5       nU R
                  c   eU R
                  R                  U R                  S   5        U R                  S:X  GaO  U R                  [        R                  :X  a  U R                  X!5      nGOAU R                  [        R                  :X  Ga  [        5       nU R                  [        R                  :X  as  U R                  R                   S-   S-  S-  n[#        U R                  R$                  S-   S-  5       H)  n['        U R)                  U5      U5       H  nX6-  nM	     M+     GOU R                  [        R*                  :X  aq  U R                  R                   S-   S-  S-  n[#        U R                  R$                  S-   S-  5       H(  n[-        U R)                  U5      5       H  nX6-  nM	     M*     OU R                  [        R.                  :X  aq  U R                  R                   S-   S-  S-  n[#        U R                  R$                  S-   S-  5       H(  n[1        U R)                  U5      5       H  nX6-  nM	     M*     OiS[3        U R                  5       3n[5        U5      eS[3        U R                  5       3n[5        U5      eS	[3        U R                  5       3n[5        U5      eU R7                  U5        g )
Nr   r   r   r/   r.   rO   zUnsupported alpha encoding zUnknown BLP encoding zUnknown BLP compression )r   r   r   _alpha_encodingr   r   r   r   r   r   r   r   r3   r   r"   statexsizer4   ysizerM   r   r#   rZ   r$   rc   r}   re   r   )r   r9   r   r8   linesizeybr   r   s           r   r   BLP2Decoder._load  sw   IMF4>52F$$&ww"""T]]1%&! ~~!6!66w68<</ {''=+=+== $

 0 01 4:Q>H#TZZ%5%5%9a$?@!,T__X-F!NA ID "O A ))]-?-?? $

 0 01 4:R?H#TZZ%5%5%9a$?@!,T__X-F!GA ID "H A ))]-?-?? $

 0 01 4:R?H#TZZ%5%5%9a$?@!,T__X-F!GA ID "H A 8T=Q=Q8R7STC(---d4>>.B-CD$S)) -T$2C2C-D,EFC %%r   )r   r   r   Nr   )r   r   r   r   r   r   r   r   r   r   r     s    ,r   r   c                  ,    \ rS rSrSrSS jrSS jrSrg)	
BLPEncoderi  Tc           	     <   SnU R                   c   eU R                   R                  SS5      n[        [        U5      S-  5       H.  nX#S-  US-   S-   u  pEpgU[        R
                  " SXeXG5      -  nM0     [        U5      S:  a  US-  n[        U5      S:  a  M  U$ )Nr   rw   r/   r   r   i   s       )im
getpaletter4   r2   r5   pack)r   r8   r   r*   rJ   rK   rL   rI   s           r   _write_paletteBLPEncoder._write_palette  s    ww"""''$$VV4s7|q()A Q!a%15JA!FKKqQ22D * $i'!KD $i'!r   c           
        U R                  5       nS[        U5      -   n[        R                  " SU/SQ76 nU R                  c   eU R                  R
                  u  pVU[        R                  " SXV-  /SQ76 -  nXB-  n[        U5       HI  n[        U5       H7  nU[        R                  " SU R                  R                  X45      5      -  nM9     MK     [        U5      SU4$ )N   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r2   r5   r   r   r   r4   getpixel)	r   bufsizepalette_datar   r8   whyxs	            r   encodeBLPEncoder.encode  s    **, 3|#44{{668Y8ww"""ww||FAE8Y88qA1XD$''*:*:A6*BCC   4y!T!!r   r   N)r   r   )r   r   r   ztuple[int, int, bytes])r   r   r   r   
_pushes_fdr   r   r   r   r   r   r   r     s    J	"r   r   c           
        U R                   S:w  a  Sn[        U5      eU R                  R                  S5      S:X  a  SOSnUR	                  U5        U R
                  c   eUR	                  [        R                  " SS5      5        U R
                  R                   S	:X  a  SOS
nUS:X  a'  UR	                  [        R                  " SU5      5        OUR	                  [        R                  " S[        R                  5      5        UR	                  [        R                  " SU5      5        UR	                  [        R                  " SS
5      5        UR	                  [        R                  " SS
5      5        UR	                  [        R                  " S/U R                  Q76 5        US:X  aL  UR	                  [        R                  " SS5      5        UR	                  [        R                  " SS
5      5        [        R                  " X[        R                  " SSU R                  -   S
U R                   5      /5        g )NPzUnsupported BLP image modeblp_versionBLP1rh   ri   rr   r   rw   r   z<Lrs   rt   r(   rp   ry   )r   
ValueErrorencoderinfogetwriter   r5   r   r   r   r   r	   _saver   )r   rz   filenamer   r|   alpha_depths         r   r  r    s   	ww#~*o~~))-8FBGEHHUO::!!!HHV[[q!"zz&0!aK
T;/0
T8#8#89:
T;/0
T1%&
T1%&HHV[[))*
T1%&
T1%&OOBY__UFRWW4DaQRSr   z.blpr   BLP2rp   )r*   r   r   ztuple[int, int, int])F)r8   r   r9   r   r   1tuple[bytearray, bytearray, bytearray, bytearray])r8   r   r   r	  )rk   r   r   r   )r   zImage.Imagerz   z	IO[bytes]r  zstr | bytesr   r   )(r   
__future__r   r   r   r5   enumr   ior   typingr    r   r	   r   r   r   r+   rM   rZ   rc   NotImplementedErrorre   rl   rn   ABC	PyDecoderr   r   r   	PyEncoderr   r  register_openr   register_extensionregister_decoderregister_saveregister_encoderr   r   r   <module>r     sg  > # 
 	     W w G L
  %6
6666r2jCL	( 	1'Q9&& 'QT1cggy22 1h"?/ "?J-/ -`"$$ "BT6   L''w ?   ,,f 5   v{ +   v{ +   L'' /   uj )r   