
    Rf<                    z   d Z ddlm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  G d d	e          Z G d
 de          Z G d de          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  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.
    )annotationsN)IntEnum)BytesIO   )Image	ImageFilec                      e Zd ZdZdS )Formatr   N)__name__
__module____qualname__JPEG     D/var/www/html/env/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                <    | 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_565r%   9   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   len	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bs                         r   decode_dxt1rG   =   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 )r'      z<8B<HHr(   <I   r)   Fr   T      r   r   r   r+   )r2   r4   r5   r6   r7   r:   r8   r9   coder;   r<   r=   r>   r?   r@   rA   highr$   alphacode_indexrC   
color_coderD   rE   rF   s                           r   decode_dxt3rS   t   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)
    rI   z<BBz<6Br   r   r(   r)   r"      r   r   rJ   rK   rL   r   rM      r*   r+   )r2   r4   r5   r6   r7   a0a1r:   
alphacode1
alphacode2r8   r9   rO   r;   r<   r=   r>   r?   r@   rA   r$   rQ   	alphacoderC   rR   rD   rE   rF   s                               r   decode_dxt5r\      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   r^   r^      s        Dr   r^   c                    | d d         dv S )Nr)      BLP1   BLP2r   )prefixs    r   _acceptrd      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)   r"   <br   r   <IIr(   r`   zBad BLP magic RGBARGBr   r   r   )fpreadmagicseekosSEEK_CURr/   unpack_blp_alpha_depth_sizedecodereprr^   _modesizemodetile)selfdecodermsgs      r   _openzBlpImageFile._open   s	   W\\!__
Q$$$#)=tw||A#G#G 	Q$$$]5$',,q//::
:+++j''))GG54
#3#355C %%%#4?VV%
v	11ty!Q6GHI			r   N)r   r   r   __doc__formatformat_descriptionr   r   r   r   rf   rf      s?          F1J J J J Jr   rf   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   rw   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)   <iri   r   rj   r(   <16I@   )fdrq   r/   rt   
_safe_read_blp_compression_blp_encodingru   _blp_alpha_encodingrr   rs   rz   
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_read1  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/   rt   r   r   append)r}   r5   r$   rF   rE   rD   rC   s          r   _read_palettez_BLPBaseDecoder._read_palette4  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/   rt   ro   r   ru   r1   )
r}   paletter2   _dataoffsetrF   rE   rD   rC   ds
             r   
_read_bgraz_BLPBaseDecoder._read_bgra>  s    {{(9!(<==>>		"M$

1>>	<    JAq!QAq	A$ aT	KKNNN		 s   *A) )A;:A;N)	r   r   r   	_pulls_fdrw   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)   r"   zUnsupported BLP encoding zUnsupported BLP compression )r   r
   r   _decode_jpeg_streamr   r   r   
set_as_rawbytesrx   r^   )r}   r   r2   r   s       r   r   zBLP1Decoder._loadO  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   )JpegImageFilerK   r)   r   CMYKrl   )JpegImagePluginr   r/   rt   r   r   r   tellr   r   r   _decompression_bomb_checkrz   r{   r|   convertsplitmerger   tobytes)r}   r   jpeg_header_sizejpeg_headerr2   imagedecoder_nameextentsr   argsrD   rE   rF   s                r   r   zBLP1Decoder._decode_jpeg_stream_  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   N  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)   r(   )r3   rI   zUnsupported alpha encoding zUnknown BLP encoding zUnknown BLP compression )r   r   rq   r   r   r   r   r   r   r   r-   r   r   r   rz   r.   rG   r   boolru   r   rS   r   r\   rx   r^   r   r   )r}   r   r2   linesizeybr   r   s          r   r   zBLP2Decoder._loads  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   r  s#        +% +% +% +% +%r   r   c                      e Zd ZdZd Zd ZdS )
BLPEncoderTc           	     @   d}| j                             dd          }t          t          |          dz            D ]5}||dz  |dz   dz           \  }}}}|t	          j        d||||          z  }6t          |          dk     r|dz  }t          |          dk     |S )Nr   rk   r)   r   r   i   s       )im
getpaletter.   r,   r/   pack)r}   r2   r   r$   rD   rE   rF   rC   s           r   _write_palettezBLPEncoder._write_palette  s    '$$VV44s7||q()) 	3 	3A Q!a%1!45JAq!QFKq!Q222DD$ii'!!KD $ii'!!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   rz   r.   getpixel)	r}   bufsizepalette_datar   r2   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BLP1ra   rb   r   r   ri   rk   r   rj   r"   rg   rm   )r{   
ValueErrorencoderinfogetwriter/   r   r   r   r   rz   r   _save)r   rn   filenamer   rp   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   BLP2rg   )F)$r   
__future__r   rr   r/   enumr   ior    r   r   r
   r   r   r%   rG   rS   r\   NotImplementedErrorr^   rd   rf   	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*  L'w ? ? ?  ,f 5 5 5  v{ + + +  v{ + + +  L' / / /  uj ) ) ) ) )r   