
    zvg+A                       d Z ddlmZ ddlZddlZddlZddlm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d7dZ	 d8d9dZd:dZd:dZ G d de          Zd;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<d2Z  ej!        ej"        ee            ej#        ej"        d3            ej$        d4e            ej$        d5e            ej%        ej"        e             ej&        d6e           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)IO   )Image	ImageFilec                      e Zd ZdZdS )Formatr   N)__name__
__module____qualname__JPEG     O/var/www/pixelcanvas.ch/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   0   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   6   s        DDDDDr   r   iintreturntuple[int, int, int]c                <    | dz	  dz  dz  | dz	  dz  dz  | dz  dz  fS )N      r      ?   r   r   )r    s    r   
unpack_565r)   <   s2    "W"a1f_$:QX!OKKr   Fdatabytesalphabool1tuple[bytearray, bytearray, bytearray, bytearray]c           	     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)r*   r,   blocksretblock_indexidxcolor0color1bitsr0g0b0r1g1b1jr    controlargbs                         r   decode_dxt1rN   @   s)    YY!^F;;	Y[[)++
>CV}} *- *-Ao%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 )r0      z<8B<HHr1   <I   r2   Fr   T      r   r   r   r4   )r*   r;   r<   r=   r>   blockrA   r?   r@   coderB   rC   rD   rE   rF   rG   rH   highr    alphacode_indexrJ   
color_coderK   rL   rM   s                            r   decode_dxt3r[   y   s?   
 YY"_F;;	Y[[)++
>CV}} (, (,B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)
    rP   z<BBz<6Br   r   r1   r2   r'      r   r   rQ   rR   rS   r   rT      r3   r4   )r*   r;   r<   r=   r>   rV   a0a1rA   
alphacode1
alphacode2r?   r@   rW   rB   rC   rD   rE   rF   rG   rH   r    rY   	alphacoderJ   rZ   rK   rL   rM   s                                r   decode_dxt5rd      sB   
 YY"_F;;	Y[[)++
>CV}} 9, 9,B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   rf   rf      s        Dr   rf   prefixc                    | d d         dv S )Nr2   )   BLP1   BLP2r   )rg   s    r   _acceptrk      s    "1":+++r   c                  "    e Zd ZdZdZdZddZdS )	BlpImageFilez 
    Blizzard Mipmap Format
    BLPzBlizzard Mipmap Formatr"   Nonec                   | j                             d          | _        t          | j                  s&dt	          | j                   }t          |          t          j        d| j                             d                    d         }| j        dk    r8t          j        d| j                             d                    d         dk    }nt          j        d| j                             d                    d         }t          j        d| j                             d                    d         dk    }t          j        d| j                             d                    d         }| j                             dt          j
                   t          j        d	| j                             d
                    | _        | j        dk    r`t          j        d| j                             d                    d         }| j                             dt          j
                   |||f}d}n||||f}d}| j                                        }|rdnd| _        t          j        |d| j        z   ||          g| _        d S )Nr2   zBad BLP magic <ir   ri   rR   <br   <IIr1         RGBARGBr   r   )fpreadmagicrk   reprrf   r8   unpackseekosSEEK_CUR_sizedecode_moder	   _Tilesizetile)	selfmsgcompressionr,   encodingalpha_encodingargsoffsetdecoders	            r   _openzBlpImageFile._open  s   W\\!__
tz"" 	&54
#3#355C %%%mD$',,q//::1=:  M$Q88;q@EE}T47<<??;;A>HM$Q88;q@E#]4aAA!DNGLLBK(((]5$',,q//::
 :  }T47<<??;;A>HGLLBK(((51DFF5.ADF*##%%$/VV%
_Wfty.@&$OOP			r   Nr"   ro   )r   r   r   __doc__formatformat_descriptionr   r   r   r   rm   rm      sE          F1Q Q Q Q Q Qr   rm   c                  \    e Zd ZdZddZej        dd            Zdd	ZddZ	ddZ
ddZdS )_BLPBaseDecoderTbuffer$bytes | Image.SupportsArrayInterfacer"   tuple[int, int]c                    	 |                                   |                                  n)# t          j        $ r}d}t	          |          |d }~ww xY wdS )NzTruncated BLP file)r   )_read_header_loadr8   errorOSError)r   r   er   s       r   r   z_BLPBaseDecoder.decode)  sc    	&JJLLLL| 	& 	& 	&&C#,,A%	& us   (+ AAAro   c                    d S Nr   r   s    r   r   z_BLPBaseDecoder._load2  s    r   c                    t          j        d|                     d                    | _        t          j        d|                     d                    | _        d S )N<16I@   )r8   r}   
_safe_read_offsets_lengthsr   s    r   r   z_BLPBaseDecoder._read_header6  sD    fdoof.E.EFFfdoof.E.EFFr   lengthr!   r+   c                H    | j         J t          j        | j         |          S r   )fdr	   r   )r   r   s     r   r   z_BLPBaseDecoder._safe_read:  s%    w"""#DGV444r   list[tuple[int, int, int, int]]c                    g }t          d          D ]`}	 t          j        d|                     d                    \  }}}}n# t          j        $ r Y  nw xY w|                    ||||f           a|S )N   <4Br2   )r7   r8   r}   r   r   append)r   r<   r    rM   rL   rK   rJ   s          r   _read_palettez_BLPBaseDecoder._read_palette>  s    s 	% 	%A#]5$//!2D2DEE
1a<   JJ1a|$$$$
s   -AAApaletter,   r-   r6   c                `   t                      }t          |                     | j        d                             }	 	 t	          j        d|                    d                    \  }n# t          j        $ r Y n4w xY w||         \  }}}}	|||f}
|r|
|	fz  }
|                    |
           q|S )Nr   T<Br   )	r6   r   r   r   r8   r}   rz   r   r:   )r   r   r,   r*   _datar   rM   rL   rK   rJ   ds              r   
_read_bgraz_BLPBaseDecoder._read_bgraH  s     {{a(899::		"M$

1>>	<    JAq!Q"#QA aT	KKNNN		 s   *A) )A;:A;N)r   r   r"   r   r   )r   r!   r"   r+   )r"   r   )r   r   r,   r-   r"   r6   )r   r   r   	_pulls_fdr   abcabstractmethodr   r   r   r   r   r   r   r   r   r   &  s        I    	   G G G G5 5 5 5        r   r   c                      e Zd ZddZddZdS )BLP1Decoderr"   ro   c                   | j         \  | _        | _        }| j        t          j        k    r|                                  d S | j        dk    rp| j        dv rA|                                 }|                     ||          }|                     |           d S dt          | j                   }t          |          dt          | j                   }t          |          )Nr   )r2   r'   zUnsupported BLP encoding zUnsupported BLP compression )r   _compression	_encodingr   r   _decode_jpeg_streamr   r   
set_as_rawr|   rf   )r   r,   r   r*   r   s        r   r   zBLP1Decoder._load[  s    37904>5++$$&&&&&!##~'',,..w66%%%%%H$t~2F2FHH$S)))Gdn1E1EGGC %%%r   c                H   ddl m} t          j        d|                     d                    \  }|                     |          }| j        J |                     | j        d         | j                                        z
             |                     | j        d                   }||z   } |t          |                    }t          j        |j                   |j        dk    rN|j        d         \  }}}}	t          |	t                     sJ t#          j        ||||	d         df          g|_        |                    d                                          \  }
}}t          j        d|||
f          }|                     |                                           d S )Nr   )JpegImageFilerR   r2   r   CMYKrw   )JpegImagePluginr   r8   r}   r   r   r   tellr   r   r   _decompression_bomb_checkr   moder   
isinstancetupler	   r   convertsplitmerger   tobytes)r   r   jpeg_header_sizejpeg_headerr*   imagedecoder_nameextentsr   r   rK   rL   rM   reversed_images                 r   r   zBLP1Decoder._decode_jpeg_streamm  s   222222$mD$//!2D2DEE	oo&677w"""a(47<<>>9:::t}Q/00T!gdmm,,'
333:27*Q-/L'64dE*****gvQ?PQQEJ --&&,,..1aUQ1I66..0011111r   Nr   )r   r   r   r   r   r   r   r   r   r   Z  s<        & & & &$2 2 2 2 2 2r   r   c                      e Zd ZddZdS )BLP2Decoderr"   ro   c                V   | j         \  | _        | _        }| _        |                                 }| j        J | j                            | j        d                    | j        dk    r| j        t          j	        k    r| 
                    ||          }n| j        t          j        k    rt                      }| j        t          j        k    rd| j        j        dz   dz  dz  }t#          | j        j        dz   dz            D ]-}t'          |                     |          |          D ]}||z  }.n`| j        t          j        k    rb| j        j        dz   dz  dz  }t#          | j        j        dz   dz            D ],}t-          |                     |                    D ]}||z  }-n| j        t          j        k    rb| j        j        dz   dz  dz  }t#          | j        j        dz   dz            D ],}t1          |                     |                    D ]}||z  }-nrdt3          | j                   }t5          |          dt3          | j                   }t5          |          d	t3          | j                   }t5          |          |                     |           d S )
Nr   r   r   r2   r1   rP   zUnsupported alpha encoding zUnknown BLP encoding zUnknown BLP compression )r   r   r   _alpha_encodingr   r   r~   r   r   r   r   r   r6   r   r   statexsizer7   ysizerN   r   r   r[   r   rd   r|   rf   r   )r   r,   r   r*   linesizeybr   r   s           r   r   zBLP2Decoder._load  s   IMF4>5$2F$$&&w"""T]1%&&&!! ~!666w668<// {{'=+=== $
 01 4:Q>H#TZ%5%9a$?@@ & &!,T__X-F-F!N!N & &A AIDD&& )]-??? $
 01 4:R?H#TZ%5%9a$?@@ & &!,T__X-F-F!G!G & &A AIDD&& )]-??? $
 01 4:R?H#TZ%5%9a$?@@ & &!,T__X-F-F!G!G & &A AIDD&& UT=Q8R8RTTC(---Dd4>.B.BDD$S))) GT$2C-D-DFFC %%%r   Nr   )r   r   r   r   r   r   r   r   r     s(        , , , , , ,r   r   c                  "    e Zd ZdZd
dZddZd	S )
BLPEncoderTr"   r+   c           	     R   d}| j         J | 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   rv   r2   r   r   i   s       )im
getpaletter7   r5   r8   pack)r   r*   r   r    rK   rL   rM   rJ   s           r   _write_palettezBLPEncoder._write_palette  s    w"""'$$VV44s7||q()) 	3 	3A Q!a%1!45JAq!QFKq!Q222DD$ii'!!KD $ii'!!r   bufsizer!   tuple[int, int, bytes]c           
        |                                  }dt          |          z   }t          j        d|gdR  }| j        J | 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   r5   r8   r   r   r   r7   getpixel)	r   r   palette_datar   r*   whyxs	            r   encodezBLPEncoder.encode  s    **,, 3|#4#44{668Y888w"""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   r!   r"   r   )r   r   r   
_pushes_fdr   r   r   r   r   r   r     sA        J	 	 	 	" " " " " "r   r   r   Image.Imagery   	IO[bytes]filenamestr | bytesro   c           
     Z   | j         dk    rd}t          |          | j                            d          dk    rdnd}|                    |           | j        J |                    t          j        dd                     | j        j         d	k    rdnd
}|dk    r)|                    t          j        d|                     n|                    t          j        dt          j	                             |                    t          j        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        | |t          j        dd| j
        z   d
| j                   g           d S )NPzUnsupported BLP image modeblp_versionBLP1ri   rj   rq   r   rv   r   z<Lrr   rs   r'   rn   rx   )r   
ValueErrorencoderinfogetwriter   r8   r   r   r   r   r	   _saver   )r   ry   r   r   r{   alpha_depths         r   r   r     s   	w#~~*oo~))-88FBBGGEHHUOOO:!!!HHV[q!!"""z&00!!aK
T;//0000
T8#899:::
T;//000
T1%%&&&
T1%%&&&HHV[))))***
T1%%&&&
T1%%&&&OBY_UFRW4DaQQRSSSSSr   z.blpr   BLP2rn   )r    r!   r"   r#   )F)r*   r+   r,   r-   r"   r.   )r*   r+   r"   r.   )rg   r+   r"   r-   )r   r   ry   r   r   r   r"   ro   )'r   
__future__r   r   r   r8   enumr   ior   typingr    r   r	   r   r   r   r)   rN   r[   rd   NotImplementedErrorrf   rk   rm   	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 L
  %6 6 6 6 6r2 2 2 2jC C C CL	 	 	 	 	( 	 	 	, , , ,'Q 'Q 'Q 'Q 'Q9& 'Q 'Q 'QT1 1 1 1 1i) 1 1 1h&2 &2 &2 &2 &2/ &2 &2 &2R- - - - -/ - - -`" " " " "$ " " "BT T T T6  L'w ? ? ?  ,f 5 5 5  v{ + + +  v{ + + +  L' / / /  uj ) ) ) ) )r   