
    (ph?                       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                  5      r " S S\5      r " S S\5      r " S S\R<                  5      rS*S  jr \RB                  " \RD                  \\5        \RF                  " \RD                  S!5        \RH                  " S"\5        \RH                  " S#\5        \RJ                  " \RD                  \ 5        \RL                  " 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       E/var/www/html/venv/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 S S S;   $ )Nr/      BLP1   BLP2r   )prefixs    r   _acceptrl      s    "1:+++r   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                   U R                   R                  S5      U l        U R                   R                  S[        R
                  5        [        R                  " SU R                   R                  S5      5      u  U l        U R                   R                  S[        R
                  5        [        R                  " SU R                   R                  S5      5      U l	        U R                  S;   a  U R                  R                  5       nO#S	[        U R                  5       3n[        U5      eU R                  (       a  S
OSU l        USU R                  -   SU R                  SS44/U l        g )Nr/   r(   <br   r   <IIr.   rh   zBad BLP magic RGBARGBr   r   r   )fpreadmagicseekosSEEK_CURr5   unpack_blp_alpha_depth_sizedecodereprre   _modesizemodetile)selfdecodermsgs      r   _openBlpImageFile._open  s    WW\\!_
Q$#)==tww||A#G 	Q$]]5$'',,q/:
::++jj'')G"4

#3"45C %%#44V%
v		11tyy!Q6GHI	r   )r~   r   r   ry   r   NreturnNone)	r   r   r   r   __doc__formatformat_descriptionr   r   r   r   r   rn   rn      s     F1Jr   rn   c                  r    \ 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_blp_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._load#  s    r   c                   U R                   c   eU R                   R                  S5        [        R                  " SU R	                  S5      5      u  U l        [        R                  " SU R	                  S5      5      u  U l        [        R                  " SU R	                  S5      5      u  U l        [        R                  " SU R	                  S5      5      u  U l        U R                   R                  S[        R                  5        [        R                  " SU R	                  S5      5      U l        [        U [        5      (       aW  [        R                  " SU R	                  S5      5      u  U l        U R                   R                  S[        R                  5        [        R                  " SU R	                  S5      5      U l        [        R                  " SU R	                  S5      5      U l        g )	Nr/   <irr   r   rs   r.   <16I@   )fdrz   r5   r}   
_safe_read_blp_compression_blp_encodingr~   _blp_alpha_encodingr{   r|   r   
isinstanceBLP1Decoder_blp_offsets_blp_lengthsr   s    r   r    _BLPBaseDecoder._read_blp_header'  sH   ww"""Q#)==tq7I#J 	 &dDOOA4F G	#)==tq7I#J 	&,mmD$//!:L&M#	!Q$MM%);<	dK(($*MM$8J$K!TGGLLBKK("MM&$//&2IJ"MM&$//&2IJr   c                D    [         R                  " U R                  U5      $ r   )r	   r   r   )r   lengths     r   r   _BLPBaseDecoder._safe_read;  s    ##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                V   [        5       n[        U R                  U R                  S   5      5      n  [        R
                  " SUR                  S5      5      u  nX   u  pVpxXvU4n	U R                  (       a  X4-  n	UR                  U	5        M]  ! [        R                   a     U$ f = f)Nr   <Br   )
r3   r   r   r   r5   r}   rx   r   r~   r7   )
r   paletter8   _dataoffsetrL   rK   rJ   rI   ds
             r   
_read_bgra_BLPBaseDecoder._read_bgraH  s    {(9(9!(<=>"MM$

1>	 !JA!"#A$$T	KKN  <<  s   (B B('B()r~   r   r   r   r   r   r   N)r   bytesr   ztuple[int, int]r   )r   intr   r   )r   list[tuple[int, int, int, int]])r   r   r   r3   )r   r   r   r   	_pulls_fdr   abcabstractmethodr   r   r   r   r   r   r   r   r   r   r     s;    I 	 K(5r   r   c                  (    \ rS rSrSS jrSS jrSrg)r   iX  c                   U R                   [        R                  :X  a  U R                  5         g U R                   S:X  af  U R                  S;   a3  U R                  5       nU R                  U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   r   r   _decode_jpeg_streamr   r   r   
set_as_rawr   re   )r   r   r8   r   s       r   r   BLP1Decoder._loadY  s      FKK/$$&""a'!!V+,,.w/%1$t7I7I2J1KL$S))0d6H6H1I0JKC %%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  a!  UR                  S   u  pgpXgXS   S44/Ul        UR                  S5      R!                  5       u  pn[        R"                  " SXU
45      nU R%                  UR'                  5       5        g )Nr   )JpegImageFilerQ   r/   r   CMYKru   )JpegImagePluginr   r5   r}   r   r   r   tellr   r   r   _decompression_bomb_checkr   r   r   convertsplitmerger   tobytes)r   r   jpeg_header_sizejpeg_headerr8   imagedecoder_nameextentsr   argsrJ   rK   rL   reversed_images                 r   r   BLP1Decoder._decode_jpeg_streami  s"   2$mmD$//!2DE	oo&67ww"""))!,tww||~=>t0034!gdm,''

3::27**Q-/L6'&7F:KLMEJ--&,,.aUQ1I6..01r   r   Nr   )r   r   r   r   r   r   r   r   r   r   r   r   X  s    & 2r   r   c                      \ rS rSrSS jrSrg)BLP2Decoderi|  c                   U R                  5       nU R                  c   eU R                  R                  U R                  S   5        U R                  S:X  Ga6  U R
                  [        R                  :X  a  U R                  U5      nGO(U R
                  [        R                  :X  Ga  [        5       nU R                  [        R                  :X  av  U R                  S   S-   S-  S-  n[        U R                  S   S-   S-  5       H:  n[!        U R#                  U5      [%        U R&                  5      S9 H  nX%-  nM	     M<     GOkU R                  [        R(                  :X  ac  U R                  S   S-   S-  S-  n[        U R                  S   S-   S-  5       H(  n[+        U R#                  U5      5       H  nX%-  nM	     M*     OU R                  [        R,                  :X  ac  U R                  S   S-   S-  S-  n[        U R                  S   S-   S-  5       H(  n[/        U R#                  U5      5       H  nX%-  nM	     M*     OiS[1        U R                  5       3n[3        U5      eS	[1        U R
                  5       3n[3        U5      eS
[1        U R                  5       3n[3        U5      eU R5                  U5        g )Nr   r   r   r/   r.   )r9   rO   zUnsupported alpha encoding zUnknown BLP encoding zUnknown BLP compression )r   r   rz   r   r   r   r   r   r   r   r3   r   r   r"   r   r4   rM   r   boolr~   r#   rZ   r$   rc   r   re   r   )r   r   r8   linesizeybr   r   s          r   r   BLP2Decoder._load}  sg   $$&ww"""T&&q)*  A% !!X%:%::w/##x||3 {++}/A/AA $		!q 0Q6:H#TYYq\A%5!$;<!, OOH5T$BWBW=X"A !ID" = --1C1CC $		!q 0Q6;H#TYYq\A%5!$;<!,T__X-F!GA ID "H = --1C1CC $		!q 0Q6;H#TYYq\A%5!$;<!,T__X-F!GA ID "H = 8T=U=U8V7WXC(---d43E3E.F-GH$S)) -T$2G2G-H,IJC %%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   rt   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	                  [
        R                  " SS5      5        UR	                  [
        R                  " S	[        R                  5      5        UR	                  [
        R                  " S	U R                  R                   S
:X  a  SOS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SSU R                  -   SU R                   4/5        g )NPzUnsupported BLP image modeblp_versionBLP1ri   rj   r   r   rr   rt   r   rs   r(   rp   rv   )r   
ValueErrorencoderinfogetwriter5   r   r   r   r   r   r	   _save)r   rw   filenamer   ry   s        r   r   r     sI   	ww#~*o~~))-8FBGEHHUOHHV[[q!"HHV[[x4456HHV[[BJJOOv$=q1EFHHV[[q!"HHV[[q!"HHV[[))*
T1%&
T1%&OOBeVbgg%5q"''BCDr   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.Imagerw   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   	PyDecoderr   r   r   	PyEncoderr   r   register_openr   register_extensionregister_decoderregister_saveregister_encoderr   r   r   <module>r     s`  > # 
 	     W w G L
  %6
6666r2jCL	( 	,J9&& J6>i)) >B!2/ !2H-/ -`"$$ "BE*   L''w ?   ,,f 5   v{ +   v{ +   L'' /   uj )r   