
     h{>                        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	e          Z G d
 de          Z G d de          Zd Zd Zd&dZd Zd Z G d de          Zd Z G d de	j	                  Z G d de	j                  Z G d de          Z G d de          Z G d d e	j                  Zd&d!Z ej        ej         ee            ej!        ej         d"            ej"        d#e            ej"        d$e            ej#        ej         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)IntEnum)BytesIO   )Image	ImageFile)	deprecatec                       e Zd ZdZdS )Formatr   N)__name__
__module____qualname__JPEG     N/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/PIL/BlpImagePlugin.pyr
   r
   )   s        DDDr   r
   c                       e Zd ZdZdZdZdS )Encodingr         N)r   r   r   UNCOMPRESSEDDXTUNCOMPRESSED_RAW_BGRAr   r   r   r   r   -   s         L
Cr   r   c                       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        DDDDDr   r   c                 V   t           dt          dt          di                                D ]c\  }}|                     |          rI| t          |          d          } | |j        v r)t          | |  d|j         d|             ||          c S ddt           d|  d}t          |          )	NBLP_FORMAT_BLP_ENCODING_BLP_ALPHA_ENCODING_
   .zmodule 'z' has no attribute '')
r
   r   r   items
startswithlen__members__r   r   AttributeError)nameenumprefixmsgs       r   __getattr__r/   9   s    /, egg		" 	"f
 ??6"" 	"F&Dt'''V+T++RDM1J1JD1J1JKKKDz!!!
:X
:
:4
:
:
:C


r   c                 <    | dz	  dz  dz  | dz	  dz  dz  | dz  dz  fS )N      r      ?   r   r   )is    r   
unpack_565r6   H   s2    "W"a1f_$:QX!OKKr   Fc           	      T   t          |           dz  }t                      t                      t                      t                      f}t          |          D ]N}|dz  }t          j        d| |          \  }}}t          |          \  }	}
}t          |          \  }}}t          d          D ]}t          d          D ]}|dz  }|dz	  }d}|dk    r|	|
|}}}n|dk    r|||}}}n||dk    rA||k    r"d|	z  |z   dz  }d|
z  |z   dz  }d|z  |z   dz  }nN|	|z   dz  }|
|z   dz  }||z   dz  }n5|dk    r/||k    r"d|z  |	z   dz  }d|z  |
z   dz  }d|z  |z   dz  }nd	\  }}}}|r ||                             ||||g           ||                             |||g           P|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_fromr6   extend)dataalphablocksretblockidxcolor0color1bitsr0g0b0r1g1b1jr5   controlargbs                         r   decode_dxt1rW   L   s)   
 YY!^F;;	Y[[)++
>Cv *- *-ai%1&$DD''
B''
B q  	-  	-A1XX - - (qya<< "b!qAA\\ "b!qAA\\Vb[Q.Vb[Q.Vb[Q."WN"WN"WN\\Vb[Q.Vb[Q.Vb[Q.%/
1a -FMM1aA,////FMM1a),,,,?- 	-D Jr   c           	         t          |           dz  }t                      t                      t                      t                      f}t          |          D ]i}|dz  }| ||dz            }t          j        d|          }t          j        d|d          \  }}t          j        d|d          \  }t          |          \  }	}
}t          |          \  }}}t          d          D ]}d}t          d          D ]}d|z  |z   d	z  }||         }|rd}|dz  }nd
}|dz  }|dz  }|d	d|z  |z   z  z	  dz  }|dk    r|	|
|}}}n\|dk    r|||}}}nO|d	k    r"d	|	z  |z   dz  }d	|
z  |z   dz  }d	|z  |z   dz  }n'|dk    r!d	|z  |	z   dz  }d	|z  |
z   dz  }d	|z  |z   dz  }||                             ||||g           Ōِk|S )r8      z<8B<HHr9   <I   r:   Fr   T      r   r   r   r<   )rB   rD   rE   rF   rG   rJ   rH   rI   coderK   rL   rM   rN   rO   rP   rQ   highr5   alphacode_indexrS   
color_coderT   rU   rV   s                           r   decode_dxt3rc      s>   
 YY"_F;;	Y[[)++
>Cv (, (,bjS38^$!%//+E5!<<$T5"55''
B''
Bq 	, 	,AD1XX , ,#$q519"2)  D!GAADHAR"a1q519o5=
?? "b!qAA1__ "b!qAA1__R"*AR"*AR"*AA1__R"*AR"*AR"*AAq!Ql++++5,	,< Jr   c           	         t          |           dz  }t                      t                      t                      t                      f}t          |          D ](}|dz  }| ||dz            }t          j        d|          \  }}t          j        d|d          }|d         |d         dz  z  |d         dz  z  |d         d	z  z  }|d
         |d         dz  z  }	t          j        d|d          \  }
}t          j        d|d          \  }t          |
          \  }}}t          |          \  }}}t          d          D ]=}t          d          D ])}dd|z  |z   z  }|dk    r	|	|z	  dz  }n |dk    r|	dz	  |dz  dz  z  }n||dz
  z	  dz  }|d
k    r|}nJ|dk    r|}nA||k    rd|z
  |z  |dz
  |z  z   dz  }n&|dk    rd
}n|dk    rd}nd|z
  |z  |dz
  |z  z   dz  }|dd|z  |z   z  z	  dz  }|d
k    r|||}}}n\|dk    r|||}}}nO|dk    r"d|z  |z   dz  }d|z  |z   dz  }d|z  |z   dz  }n'|dk    r!d|z  |z   dz  }d|z  |z   dz  }d|z  |z   dz  }||                             ||||g           +?*|S )zG
    input: one "row" of data (i.e. will produce 4 * width pixels)
    rY   z<BBz<6Br   r   r9   r:   r3      r   r   rZ   r[   r\   r   r]      r;   r<   )rB   rD   rE   rF   rG   a0a1rJ   
alphacode1
alphacode2rH   rI   r_   rK   rL   rM   rN   rO   rP   rQ   r5   ra   	alphacoderS   rb   rT   rU   rV   s                               r   decode_dxt5rl      sA   
 YY"_F;;	Y[[)++
>Cv 9, 9,bjS38^$#E511B!%22!WQ1-aB?47b=Q
!WQ1-
+E5!<<$T5"55''
B''
Bq (	, (	,A1XX ', ',"#q1uqy/"b((!+!>$ FII$**!+r!1zQ$6N OII!+"0D!E MI>>AA!^^AA"WWi-2-Q"0DDJAA!^^AA!^^AAi-2-Q"0DDJA"a1q519o5=
?? "b!qAA1__ "b!qAA1__R"*AR"*AR"*AA1__R"*AR"*AR"*AAq!Ql++++O',(	,T Jr   c                       e Zd ZdS )BLPFormatErrorN)r   r   r   r   r   r   rn   rn      s        Dr   rn   c                     | d d         dv S )Nr:      BLP1   BLP2r   )r-   s    r   _acceptrs     s    "1":+++r   c                        e Zd ZdZdZdZd ZdS )BlpImageFilez 
    Blizzard Mipmap Format
    BLPzBlizzard Mipmap Formatc                    | j                             d          | _        | j                             dt          j                   t          j        d| j                             d                    \  | _        | j                             dt          j                   t          j        d| j                             d                    | _	        | j        dv r| j        
                                }n&d	t          | j                   }t          |          | j        rd
nd| _        |d| j        z   d| j        ddffg| _        d S )Nr:   r3   <br   r   <IIr9   rp   zBad BLP magic RGBARGBr   r   r   )fpreadmagicseekosSEEK_CURr?   unpack_blp_alpha_depth_sizedecodereprrn   modesizetile)selfdecoderr.   s      r   _openzBlpImageFile._open  s	   W\\!__
Q$$$#)=tw||A#G#G 	Q$$$]5$',,q//::
:+++j''))GG54
#3#355C %%%"3>FF	v	11ty!Q6GHI			r   N)r   r   r   __doc__formatformat_descriptionr   r   r   r   ru   ru     s?          F1J J J J Jr   ru   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )_BLPBaseDecoderTc                     	 |                                   |                                  n)# t          j        $ r}d}t	          |          |d }~ww xY wdS )NzTruncated BLP file)r   )_read_blp_header_loadr?   errorOSError)r   bufferer.   s       r   r   z_BLPBaseDecoder.decode$  sc    	&!!###JJLLLL| 	& 	& 	&&C#,,A%	& us   (+ AAAc                    | j                             d           t          j        d|                     d                    \  | _        t          j        d|                     d                    \  | _        t          j        d|                     d                    \  | _        t          j        d|                     d                    \  | _        | j                             dt          j
                   t          j        d|                     d                    | _        t          | t                    rTt          j        d|                     d                    \  | _        | j                             dt          j
                   t          j        d|                     d                    | _        t          j        d|                     d                    | _        d S )	Nr:   <irx   r   ry   r9   <16I@   )fdr   r?   r   
_safe_read_blp_compression_blp_encodingr   _blp_alpha_encodingr   r   r   
isinstanceBLP1Decoder_blp_offsets_blp_lengths)r   s    r   r   z _BLPBaseDecoder._read_blp_header-  si   Q#)=tq7I7I#J#J 	 &dDOOA4F4F G G	#)=tq7I7I#J#J 	&,mD$//!:L:L&M&M#	!Q$$$M%););<<	dK(( 	)$*M$8J8J$K$K!TGLLBK((("M&$//&2I2IJJ"M&$//&2I2IJJr   c                 6    t          j        | j        |          S )N)r   r   r   )r   lengths     r   r   z_BLPBaseDecoder._safe_read@  s    #DGV444r   c                     g }t          d          D ]`}	 t          j        d|                     d                    \  }}}}n# t          j        $ r Y  nw xY w|                    ||||f           a|S )N   <4Br:   )r>   r?   r   r   r   append)r   rE   r5   rV   rU   rT   rS   s          r   _read_palettez_BLPBaseDecoder._read_paletteC  s    s 	% 	%A#]5$//!2D2DEE
1a<   JJ1a|$$$$
s   -AAAc                 j   t                      }t          |                     | j        d                             }	 	 t	          j        d|                    d                    \  }n# t          j        $ r Y n9w xY w||         \  }}}}|||f}	| j        r|	|fz  }	|	                    |	           v|S )Nr   T<Br   )
r=   r   r   r   r?   r   r~   r   r   rA   )
r   paletterB   _dataoffsetrV   rU   rT   rS   ds
             r   
_read_bgraz_BLPBaseDecoder._read_bgraM  s    {{(9!(<==>>		"M$

1>>	<    JAq!QAq	A$ aT	KKNNN		 s   *A) )A;:A;N)	r   r   r   	_pulls_fdr   r   r   r   r   r   r   r   r   r   !  sg        I  K K K&5 5 5      r   r   c                       e Zd Zd Zd ZdS )r   c                    | j         t          j        k    r|                                  d S | j         dk    r|| j        dv rM|                                 }|                     |          }|                     t          |                     d S dt          | j                   }t          |          dt          | j                   }t          |          )Nr   )r:   r3   zUnsupported BLP encoding zUnsupported BLP compression )r   r
   r   _decode_jpeg_streamr   r   r   
set_as_rawbytesr   rn   )r   r   rB   r.   s       r   r   zBLP1Decoder._load^  s     FK//$$&&&&&"a''!V++,,..w//d,,,,,L$t7I2J2JLL$S)))Kd6H1I1IKKC %%%r   c                    ddl m} t          j        d|                     d                    \  }|                     |          }|                     | j        d         | j                                        z
             |                     | j        d                   }||z   }t          |          } ||          }t          j        |j                   |j        dk    r&|j        d         \  }}}}	||||	d         dffg|_        |                    d                                          \  }
}}t          j        d|||
f          }|                     |                                           d S )Nr   )JpegImageFiler[   r:   r   CMYKr{   )JpegImagePluginr   r?   r   r   r   r   tellr   r   r   _decompression_bomb_checkr   r   r   convertsplitmerger   tobytes)r   r   jpeg_header_sizejpeg_headerrB   imagedecoder_nameextentsr   argsrT   rU   rV   s                r   r   zBLP1Decoder._decode_jpeg_streamn  sW   222222$mD$//!2D2DEE	oo&677)!,tw||~~=>>>t0344T!t}}d##'
333:27*Q-/L'64'&47F:KLMEJ--&&,,..1aEAq!9--(((((r   N)r   r   r   r   r   r   r   r   r   r   ]  s2        & & & ) ) ) ) )r   r   c                       e Zd Zd ZdS )BLP2Decoderc                 X   |                                  }| j                            | j        d                    | j        dk    r| j        t          j        k    r|                     |          }n| j        t          j	        k    rt                      }| j        t          j        k    ry| j        d         dz   dz  dz  }t          | j        d         dz   dz            D ]@}t!          |                     |          t%          | j                            D ]}||z  }And| j        t          j        k    rd| j        d         dz   dz  dz  }t          | j        d         dz   dz            D ],}t+          |                     |                    D ]}||z  }-n| j        t          j        k    rd| j        d         dz   dz  dz  }t          | j        d         dz   dz            D ],}t/          |                     |                    D ]}||z  }-nrdt1          | j                   }t3          |          d	t1          | j                   }t3          |          d
t1          | j                   }t3          |          |                     t7          |                     d S )Nr   r   r   r:   r9   )rC   rY   zUnsupported alpha encoding zUnknown BLP encoding zUnknown BLP compression )r   r   r   r   r   r   r   r   r   r   r=   r   r   r   r   r>   rW   r   boolr   r   rc   r   rl   r   rn   r   r   )r   r   rB   linesizeybr   r.   s          r   r   zBLP2Decoder._load  s   $$&&T&q)*** A%% !X%:::w//#x|33 {{+}/AAA $	!q 0Q6:H#TYq\A%5!$;<< & &!, OOH55T$BW=X=X" " " & &A !AIDD&& -1CCC $	!q 0Q6;H#TYq\A%5!$;<< & &!,T__X-F-F!G!G & &A AIDD&& -1CCC $	!q 0Q6;H#TYq\A%5!$;<< & &!,T__X-F-F!G!G & &A AIDD&& YT=U8V8VXXC(---Hd43E.F.FHH$S))) KT$2G-H-HJJC %%%d$$$$$r   N)r   r   r   r   r   r   r   r   r     s#        +% +% +% +% +%r   r   c                       e Zd ZdZd Zd ZdS )
BLPEncoderTc           	          d}| j                             dd          }t          d          D ]5}||dz  |dz   dz           \  }}}}|t          j        d||||          z  }6|S )Nr   rz   r   r:   r   r   )im
getpaletter>   r?   pack)r   rB   r   r5   rT   rU   rV   rS   s           r   _write_palettezBLPEncoder._write_palette  sy    '$$VV44s 	3 	3A Q!a%1!45JAq!QFKq!Q222DDr   c           
         |                                  }dt          |          z   }t          j        d|gdR  }| j        j        \  }}|t          j        d||z  gdR  z  }||z  }t          |          D ]F}t          |          D ]4}|t          j        d| j                            ||f                    z  }5Gt          |          d|fS )N   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r(   r?   r   r   r   r>   getpixel)	r   bufsizepalette_datar   rB   whyxs	            r   encodezBLPEncoder.encode  s    **,, 3|#4#44{668Y888w|1FAE8Y8888q 	D 	DA1XX D DD$'*:*:Aq6*B*BCCCD 4yy!T!!r   N)r   r   r   
_pushes_fdr   r   r   r   r   r   r     s7        J  " " " " "r   r   c                    | j         dk    rd}t          |          | j                            d          dk    rdnd}|                    |           |                    t          j        dd                     |                    t          j        d	t          j                             |                    t          j        d	| j	        j         d
k    rdnd                     |                    t          j        d	d                     |                    t          j        d	d                     |                    t          j        dg| j
        R             |dk    rP|                    t          j        dd                     |                    t          j        dd                     t          j        | |dd| j
        z   d| j         fg           d S )NPzUnsupported BLP image modeblp_versionBLP1rq   rr   r   r   rx   rz   r   ry   r3   rv   r|   )r   
ValueErrorencoderinfogetwriter?   r   r   r   r   r   r   _save)r   r}   filenamesave_allr.   r   s         r   r   r     s   	w#~~*oo~))-88FBBGGEHHUOOOHHV[q!!"""HHV[x455666HHV[BJOv$=$=qq1EEFFFHHV[q!!"""HHV[q!!"""HHV[))))***
T1%%&&&
T1%%&&&OBeVbg%5q"'BCDDDDDr   z.blpr   BLP2rv   )F)%r   r   r?   r,   r   ior    r   r   
_deprecater   r
   r   r   r/   r6   rW   rc   rl   NotImplementedErrorrn   rs   ru   	PyDecoderr   r   r   	PyEncoderr   r   register_openr   register_extensionregister_decoderregister_saveregister_encoderr   r   r   <module>r      s   > 
			                      ! ! ! ! ! !    W       w       G     L L L4 4 4 4n2 2 2jC C CL	 	 	 	 	( 	 	 	, , ,J J J J J9& J J J69 9 9 9 9i) 9 9 9x!) !) !) !) !)/ !) !) !)H,% ,% ,% ,% ,%/ ,% ,% ,%^" " " " "$ " " ":E E E E*  L'w ? ? ?  ,f 5 5 5  v{ + + +  v{ + + +  L' / / /  uj ) ) ) ) )r   