U
    P’“eÁ  ã                   @   sÀ   d 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 ddl	m
Z
 ddlmZ dd	lmZmZmZmZ dd
lmZmZ G dd„ deƒZG dd„ deƒZG dd„ deƒZedkr¼e ¡  dS )a;  
Texture compression tool
========================

This tool is designed to compress images into:

- PVRTC (PowerVR Texture Compression), mostly iOS devices
- ETC1 (Ericson compression), working on all GLES2/Android devices

Usage
-----

In order to compress a texture::

    texturecompress.py [--dir <directory>] <format> <image.png>

This will create a `image.tex` file with a json header that contains all the
image information and the compressed data.

TODO
----

Support more format, such as:

- S3TC (already supported in Kivy)
- DXT1 (already supported in Kivy)
é    N)Úpack)Úpprint)ÚPopen)ÚImage)ÚArgumentParser)Úexit)ÚjoinÚexistsÚdirnameÚbasename)ÚenvironÚunlinkc                       sV   e Zd Z‡ fdd„Zedd„ ƒZdd„ Zdd„ Zd	d
„ Zddd„Z	e
dd„ ƒZ‡  ZS )ÚToolc                    s2   t t| ƒ ¡  || _|j| _|jp*t|jƒ| _d S ©N)	Úsuperr   Ú__init__ÚoptionsÚimageÚ	source_fnÚdirr
   Údest_dir©Úselfr   ©Ú	__class__© ú>/tmp/pip-unpacked-wheel-xzebddm3/kivy/tools/texturecompress.pyr   )   s    zTool.__init__c                 C   s&   t | jƒ dd¡d d }t| j|ƒS )NÚ.é   r   z.tex)r   r   Úrsplitr   r   )r   Úfnr   r   r   Útex_fn/   s    zTool.tex_fnc                 C   s   d S r   r   )r   r   r   r   Úcompress4   s    zTool.compressc                 C   sL   |d8 }||d? O }||d? O }||d? O }||d? O }||d? O }|d S )Nr   é   é   é   é   r   )r   Úvr   r   r   Únearest_pow27   s    zTool.nearest_pow2c                 C   s$   t d d |¡¡ƒ t|ƒ ¡  d S )NzRun: {}ú )ÚprintÚformatr   r   Úcommunicate)r   Úcmdr   r   r   ÚruncmdA   s    zTool.runcmdFNc           
   	   C   s¢   t |ƒ||||dœ}|r ||d< tj|ddd}| dd¡}t| jdƒ8}	|	 d	¡ |	 td
t |ƒƒ¡ |	 |¡ |	 |¡ W 5 Q R X td 	| j¡ƒ t
|ƒ d S )N)ZdatalenÚ
image_sizeÚtexture_sizeÚmipmapr+   Ú
formatinfor   )ú,ú:)ÚindentÚ
separatorsÚ
Ú ÚwbZKTEXÚIz&Done! Compressed texture written at {})ÚlenÚjsonÚdumpsÚreplaceÚopenr!   Úwriter   r*   r+   r   )
r   ÚdataÚfmtr/   r0   r1   r2   ZinfosÚheaderÚfdr   r   r   Ú	write_texE   s"    û

zTool.write_texc                  C   s    t dd} | jdtddd | jdtd dd | jd	td
dgdd | jdtdd |  ¡ }|jd
krtt|ƒ ¡  n(|jdkrŒt|ƒ ¡  nt	dƒ t
dƒ d S )Nz$Convert images to compressed texture)Údescriptionz--mipmapFzAuto generate mipmaps)ÚtypeÚdefaultÚhelpz--dirz3Output directory to generate the compressed texturer+   ZpvrtcZetc1zFormat of the final texture)rG   ÚchoicesrI   r   zImage filename)rG   rI   zUnknown compression formatr   )r   Úadd_argumentÚboolÚstrÚ
parse_argsr+   Ú	PvrtcToolr"   ÚEtc1Toolr*   r   )ÚparserÚargsr   r   r   ÚrunZ   s,    ÿ
ÿ
ÿÿÿ

zTool.run)FN)Ú__name__Ú
__module__Ú__qualname__r   Úpropertyr!   r"   r(   r.   rE   ÚstaticmethodrS   Ú__classcell__r   r   r   r   r   (   s   

  ÿ
r   c                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )rP   c                    s"   t t| ƒ |¡ d | _|  ¡  d S r   )r   rP   r   Úetc1toolÚlocate_etc1toolr   r   r   r   r   r   s    zEtc1Tool.__init__c                 C   sŒ   t  dd¡g}|t  dd¡ d¡7 }|D ]D}t|dƒ}t|ƒsTt|ddƒ}t|ƒsTq(td |¡ƒ || _ d S | jd krˆtd	ƒ td
ƒ d S )NZ
ANDROIDSDKú/ÚPATHr8   r4   rZ   ZtoolsúFound texturetool at {}zError: Unable to locate "etc1tool".
Make sure that "etc1tool" is available in your PATH.
Or export the path of your Android SDK to ANDROIDSDKr   )	r   ÚgetÚsplitr   r	   r*   r+   rZ   r   ©r   Zsearch_directoriesÚ	directoryr    r   r   r   r[   w   s    

zEtc1Tool.locate_etc1toolc           
   	   C   sÈ   t  | j¡}|j\}}tdj|jŽ ƒ |  |¡}|  |¡}td ||¡ƒ | jd }| j| jdd|g}z,|  |¡ t|dƒ}| ¡ }	W 5 Q R X W 5 t	|ƒr¤t
|ƒ X |  |	d||f||f| jj¡ d S )NúImage size is {}x{}ú Nearest power-of-2 size is {}x{}ú.rawz--encodeNoHeaderú-oÚrbZ	etc1_rgb8)r   r?   r   Úsizer*   r+   r(   r!   rZ   r	   r   r.   ÚreadrE   r   r1   )
r   r   ÚwÚhÚw2Úh2Ú
raw_tex_fnr-   rD   rA   r   r   r   r"   Š   s&    



ÿ

ÿzEtc1Tool.compress)rT   rU   rV   r   r[   r"   rY   r   r   r   r   rP   q   s   rP   c                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )rO   c                    s"   t t| ƒ |¡ d | _|  ¡  d S r   )r   rO   r   ÚtexturetoolÚlocate_texturetoolr   r   r   r   r   §   s    zPvrtcTool.__init__c                 C   sh   ddg}|t  dd¡ d¡7 }|D ]0}t|dƒ}t|ƒs:q"td |¡ƒ || _ d S tdƒ td	ƒ d S )
NzY/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/z9/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/r]   r8   r4   ro   r^   z–Error: Unable to locate "texturetool".
Please install the iPhone SDK, or the PowerVR SDK.
Then make sure that "texturetool" is available in your PATH.r   )	r   r_   r`   r   r	   r*   r+   ro   r   ra   r   r   r   rp   ¬   s    ý
zPvrtcTool.locate_texturetoolc              	   C   s4  t  | j¡}|j\}}tdj|jŽ ƒ |  |¡}|  |¡}td ||¡ƒ t||ƒ}td |¡ƒ | j dd¡d }d |¡}| 	||f¡}| 
|¡ | jd }	| jg}
| jjrº|
d	g7 }
|
d
dd|	dd|g7 }
z,|  |
¡ t|	dƒ}| ¡ }W 5 Q R X W 5 t|	ƒrt|	ƒ X |  |d||f||f| jj¡ d S )Nrc   rd   z4PVR need a square image, the texture will be {0}x{0}r   r   éÿÿÿÿz/tmp/ktexturecompress.{}re   z-mz-eZPVRTCrf   z-fZRAWrg   Zpvrtc_rgba4)r   r?   r   rh   r*   r+   r(   Úmaxr   ÚresizeÚsaver!   ro   r   r1   r	   r   r.   ri   rE   )r   r   rj   rk   rl   rm   Ús2ÚextZtmpfilern   r-   rD   rA   r   r   r   r"   À   s8    



ÿ






ÿzPvrtcTool.compress)rT   rU   rV   r   rp   r"   rY   r   r   r   r   rO   ¦   s   rO   Ú__main__)Ú__doc__r<   Ústructr   r   Ú
subprocessr   ZPILr   Úargparser   Úsysr   Úos.pathr   r	   r
   r   Úosr   r   Úobjectr   rP   rO   rT   rS   r   r   r   r   Ú<module>   s   I5B