
    kj                     2   d dl Z d dlmZ d dlmZmZ 	 d dlmZ n+# e$ r#Z	 ed
                    e	                    dZ	[	ww xY w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  e j        e          Z G d de          Zdej        deeef         fdZdS )    N)Path)OptionalUnion)Imagez1Texture loader 'PillowLoader' requires Pillow: {})ImproperlyConfigured)
BaseLoader)ResourceDescription)TextureDescription)
TextureAnyc                        e Zd ZU dZdZej        ed<   eed<   def fdZ	de
fdZdej        fdZd	eeef         dej        fd
Zdej        dej        fdZddej        dee         dej        fdZddZ xZS )PillowLoaderzBase loader using PIL/Pillow__unknown__imagemetac                 J    t                                          |           d S N)super__init__)selfr   	__class__s     a/home/agentuser/manim-venv/lib/python3.11/site-packages/moderngl_window/loaders/texture/pillow.pyr   zPillowLoader.__init__   s!        returnc                     t                      r   )NotImplementedErrorr   s    r   loadzPillowLoader.load   s    !###r   c                 @   | j         j        r| j         j        | _        n|                     | j         j                  | j         _        t
                              d| j         j                   | j         j        s,t          d                    | j         j                            t          j
        | j         j                  | _        t          | j        d          r| j        j        rt          | j        d          r| j        j        | _        t          j        | j        j        | j        j        j        nd| j        j        | j        j        | j        j        z  f          }|                    d           t+          | j        j                  D ]]}| j                            |           |                     | j        d          }|                    |d|| j        j        z  f           ^|| _        |                     | j                  | _        | j        S )	N
loading %sCannot find texture: {}is_animatedn_framesLr   RGBA)mode)r   r   find_texturepathresolved_pathloggerinfor   formatr   openhasattrr!   r"   layersnewpaletter%   widthheightputalpharangeseek_palette_to_rawpaste_apply_modifiers)r   animframe_numberframes       r   _open_imagezPillowLoader._open_image    s   9? 	"DJJ&*&7&7	&G&GDI#KKdi&=>>>9* ]*+D+K+KDIN+[+[\\\DI$;<<DJ 
M22"J*" DJ
33"
 #j1y/3z/A/MDJ&++SVZ%tz'84:;N'NO  a   $)$**=$>$> M MLJOOL111 00&0IIEJJuq,9J*J&KLLLL!
**4:66
zr   r'   c                     |                      |          }t                              d|           |s"t          d                    |                    t          j        |          }|                     |          S )zTFind and load separate texture. Useful when multiple textue files needs to be loadedr   r    )r&   r)   r*   r   r+   r   r,   r8   )r   r'   r(   r   s       r   _load_texturezPillowLoader._load_textureB   sr    ))$//L-000 	O&'@'G'G'M'MNNN
=))$$U+++r   c                     | j         j        r$|                    t          j        j                  }| j         j        r$|                    t          j        j                  }|                     |          S r   )	r   flip_x	transposer   	TransposeFLIP_LEFT_RIGHTflip_yFLIP_TOP_BOTTOMr6   )r   r   s     r   r8   zPillowLoader._apply_modifiersL   s^    9 	EOOEO$CDDE9 	EOOEO$CDDE##E***r   Nr%   c                     |j         r^|j         j                                        dv r>|p|j         j        }t                              d|           |                    |          S |S )z+Converts image to raw if palette is present)rgbrgbaz&Converting P image to %s using palette)r0   r%   lowerr)   debugconvert)r   r   r%   s      r   r6   zPillowLoader._palette_to_rawU   sb    = 	'U]/5577?JJ-5=-DLLA4HHH==&&&r   c                 8    | j                                          d S r   )r   closer   s    r   _close_imagezPillowLoader._close_image^   s    
r   r   )r   N)__name__
__module____qualname____doc__kindr   __annotations__r
   r	   r   r   r   r<   r   strr   r>   r8   r   r6   rN   __classcell__)r   s   @r   r   r      sD        &&D;
0      $j $ $ $ $ U[        D,%T	"2 ,u{ , , , ,+ek +ek + + + + U[  QVQ\           r   r   r   r   c           	      
   |                                  }t          |          | j        d         | j        d         z  z  }t                              d| j        d         | j        d         |t          |                     ||fS )zGet components and bytes for an image.
    The number of components is assumed by image
    size and the byte length of the raw data.

    Returns:
        tuple[int, bytes]: Number of components, byte data
    r      z/image_data size=[%s, %s] components=%s bytes=%s)tobyteslensizer)   rJ   )r   data
componentss      r   
image_datar^   b   sv     ==??DTuz!}uz!}<=J
LL9
1
1D		   tr   )loggingpathlibr   typingr   r   PILr   ImportErrorexr+   moderngl_window.exceptionsr   moderngl_window.loaders.baser   moderngl_window.meta.baser	   moderngl_window.meta.texturer
   "moderngl_window.resources.texturesr   	getLoggerrO   r)   r   tupleintbytesr^    r   r   <module>ro      sn          " " " " " " " "V V V V
+IPPQSTT
U
UUV < ; ; ; ; ; 3 3 3 3 3 3 9 9 9 9 9 9 ; ; ; ; ; ; 9 9 9 9 9 9		8	$	$L L L L L: L L L^ek eCJ&7      s    A>A