U
    ڲg[*                     @  s  U d dl mZ d dlZd dlZd dlmZ ddlmZ ded< z$d dl	m
Z
 d	Ze
 Zee W n< ek
r Z zdd
lmZ ee Z
ZW 5 dZ[X Y nX eeZerddlmZ G dd dZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZ G dd deZ!G d d! d!eZ"eeeeeeeeeeeeeeee"e d"Z#ej$d#kree#d$< ee#d%< ee#d&< e e#d'< e!e#d(< n(ee#d$< ee#d%< ee#d&< e!e#d'< e e#d(< d0d*d+d,d-d.d/ZdS )1    )annotationsN)TYPE_CHECKING   )	deprecatetypeFFI)r   z|
    struct Pixel_RGBA {
        unsigned char r,g,b,a;
    };
    struct Pixel_I16 {
        unsigned char l,r;
    };
    )DeferredError)Imagec                   @  s   e Zd ZdddddddZddd	d
ZddddddZdddddZeZeZdddddZ	ddddddZ
dddddddZdS ) PyAccessFImage.ImageboolNoneimgreadonlyreturnc                 C  s   t dd t|jj}|| _td|d | _td|d | _td|d | _	|jj
\| _| _|| _|j| _| jjdkr|j| _|   d S )	Nr
      zunsigned char **image8zint **image32imagePPA)r   dictZimZunsafe_ptrsr   fficastr   r   r   sizexsizeysize_img_immodeZpalette_palette
_post_init)selfr   r   vals r&   0/tmp/pip-unpacked-wheel-xela2va7/PIL/PyAccess.py__init__9   s    
zPyAccess.__init__)r   c                 C  s   d S Nr&   )r$   r&   r&   r'   r#   M   s    zPyAccess._post_initztuple[int, int] | list[int]z#float | tuple[int, ...] | list[int])xycolorr   c                 C  s   | j rd}t||\}}|dk r,| j| }|dk r>| j| }| ||f\}}| jjdkrt|tt	frt
|dkr| jjdkrt
|dkr|d nd}|d	d }| j|| j}| jjdkr||fn|}| |||S )
a  
        Modifies the pixel at x,y. The color is given as a single
        numerical value for single band images, and a tuple for
        multi-band images. In addition to this, RGB and RGBA tuples
        are accepted for P and PA images.

        :param xy: The pixel coordinate, given as (x, y). See
           :ref:`coordinate-system`.
        :param color: The pixel value.
        z%Attempt to putpixel a read only imager   r   )      r   r-   r,      N)r   
ValueErrorr   r   check_xyr    r!   
isinstancelisttuplelenr"   Zgetcolorr   	set_pixel)r$   r*   r+   msgxyalphaZpalette_indexr&   r&   r'   __setitem__P   s*    



zPyAccess.__setitem__zfloat | tuple[int, ...])r*   r   c                 C  sJ   |\}}|dk r| j | }|dk r,| j| }| ||f\}}| ||S )aq  
        Returns the pixel at x,y. The pixel is returned as a single
        value for single band images or a tuple for multiple band
        images

        :param xy: The pixel coordinate, given as (x, y). See
          :ref:`coordinate-system`.
        :returns: a pixel value for single band images, a tuple of
          pixel values for multiband images.
        r   )r   r   r0   	get_pixel)r$   r*   r7   r8   r&   r&   r'   __getitem__w   s    

zPyAccess.__getitem__tuple[int, int]c                 C  sJ   |\}}d|  kr| j k r:n nd|  kr8| jk sFn d}t||S )Nr   zpixel location out of range)r   r   r/   )r$   r*   r7   r8   r6   r&   r&   r'   r0      s
    2zPyAccess.check_xyintr7   r8   r   c                 C  s
   t  d S r)   NotImplementedErrorr$   r7   r8   r&   r&   r'   r;      s    zPyAccess.get_pixel)r7   r8   r+   r   c                 C  s
   t  d S r)   r@   r$   r7   r8   r+   r&   r&   r'   r5      s    zPyAccess.set_pixelN)F)__name__
__module____qualname__r(   r#   r:   r<   ZputpixelZgetpixelr0   r;   r5   r&   r&   r&   r'   r
   8   s   'r
   c                   @  s2   e Zd ZdZdd ZddddddZd	d
 ZdS )_PyAccess32_2z7PA, LA, stored in first and last bytes of a 32 bit wordc                 O  s   t d| j| _d S Nzstruct Pixel_RGBA **r   r   r   pixelsr$   argskwargsr&   r&   r'   r#      s    z_PyAccess32_2._post_initr>   r=   r?   c                 C  s   | j | | }|j|jfS r)   )rJ   rar$   r7   r8   pixelr&   r&   r'   r;      s    z_PyAccess32_2.get_pixelc                 C  s2   | j | | }t|d d|_t|d d|_d S )Nr   r.   r   )rJ   minrN   rO   r$   r7   r8   r+   rQ   r&   r&   r'   r5      s    z_PyAccess32_2.set_pixelNrD   rE   rF   __doc__r#   r;   r5   r&   r&   r&   r'   rG      s   rG   c                   @  s2   e Zd ZdZdd ZddddddZd	d
 ZdS )_PyAccess32_3zARGB and friends, stored in the first three bytes of a 32 bit wordc                 O  s   t d| j| _d S rH   rI   rK   r&   r&   r'   r#      s    z_PyAccess32_3._post_initr>   ztuple[int, int, int]r?   c                 C  s   | j | | }|j|j|jfS r)   )rJ   rN   gbrP   r&   r&   r'   r;      s    z_PyAccess32_3.get_pixelc                 C  sH   | j | | }t|d d|_t|d d|_t|d d|_d|_d S )Nr   r.   r      rJ   rR   rN   rW   rX   rO   rS   r&   r&   r'   r5      s
    z_PyAccess32_3.set_pixelNrT   r&   r&   r&   r'   rV      s   rV   c                   @  s2   e Zd ZdZdd ZddddddZd	d
 ZdS )_PyAccess32_4z&RGBA etc, all 4 bytes of a 32 bit wordc                 O  s   t d| j| _d S rH   rI   rK   r&   r&   r'   r#      s    z_PyAccess32_4._post_initr>   ztuple[int, int, int, int]r?   c                 C  s"   | j | | }|j|j|j|jfS r)   )rJ   rN   rW   rX   rO   rP   r&   r&   r'   r;      s    z_PyAccess32_4.get_pixelc                 C  sR   | j | | }t|d d|_t|d d|_t|d d|_t|d d|_d S )Nr   r.   r   rY   r,   rZ   rS   r&   r&   r'   r5      s
    z_PyAccess32_4.set_pixelNrT   r&   r&   r&   r'   r[      s   r[   c                   @  s2   e Zd ZdZdd ZddddddZdd	 Zd
S )
_PyAccess8z%1, L, P, 8 bit images stored as uint8c                 O  s   | j | _d S r)   )r   rJ   rK   r&   r&   r'   r#      s    z_PyAccess8._post_initr>   r?   c                 C  s   | j | | S r)   rJ   rB   r&   r&   r'   r;      s    z_PyAccess8.get_pixelc                 C  sJ   zt |d| j| |< W n, tk
rD   t |d d| j| |< Y nX d S )Nr.   r   rR   rJ   	TypeErrorrC   r&   r&   r'   r5      s    z_PyAccess8.set_pixelNrT   r&   r&   r&   r'   r\      s   r\   c                   @  s2   e Zd ZdZdd ZddddddZdd	 Zd
S )_PyAccessI16_Nz0I;16 access, native bitendian without conversionc                 O  s   t d| j| _d S )Nzunsigned short **r   r   r   rJ   rK   r&   r&   r'   r#      s    z_PyAccessI16_N._post_initr>   r?   c                 C  s   | j | | S r)   r]   rB   r&   r&   r'   r;      s    z_PyAccessI16_N.get_pixelc                 C  sJ   zt |d| j| |< W n, tk
rD   t |d d| j| |< Y nX d S )N  r   r^   rC   r&   r&   r'   r5      s    z_PyAccessI16_N.set_pixelNrT   r&   r&   r&   r'   r`      s   r`   c                   @  s2   e Zd ZdZdd ZddddddZdd	 Zd
S )_PyAccessI16_LzI;16L access, with conversionc                 O  s   t d| j| _d S Nzstruct Pixel_I16 **ra   rK   r&   r&   r'   r#      s    z_PyAccessI16_L._post_initr>   r?   c                 C  s   | j | | }|j|jd  S N   rJ   lrN   rP   r&   r&   r'   r;      s    z_PyAccessI16_L.get_pixelc                 C  sX   | j | | }zt|d}W n" tk
r>   t|d d}Y nX |d@ |_|d? |_d S )Nrb   r   r.      )rJ   rR   r_   rh   rN   rS   r&   r&   r'   r5     s    
z_PyAccessI16_L.set_pixelNrT   r&   r&   r&   r'   rc      s   rc   c                   @  s2   e Zd ZdZdd ZddddddZdd	 Zd
S )_PyAccessI16_BzI;16B access, with conversionc                 O  s   t d| j| _d S rd   ra   rK   r&   r&   r'   r#     s    z_PyAccessI16_B._post_initr>   r?   c                 C  s   | j | | }|jd |j S re   rg   rP   r&   r&   r'   r;     s    z_PyAccessI16_B.get_pixelc                 C  sX   | j | | }zt|d}W n" tk
r>   t|d d}Y nX |d? |_|d@ |_d S )Nrb   r   ri   r.   )rJ   rR   	Exceptionrh   rN   rS   r&   r&   r'   r5     s    
z_PyAccessI16_B.set_pixelNrT   r&   r&   r&   r'   rj     s   rj   c                   @  s2   e Zd ZdZdd ZddddddZdd	 Zd
S )_PyAccessI32_Nz"Signed Int32 access, native endianc                 O  s   | j | _d S r)   r   rJ   rK   r&   r&   r'   r#   %  s    z_PyAccessI32_N._post_initr>   r?   c                 C  s   | j | | S r)   r]   rB   r&   r&   r'   r;   (  s    z_PyAccessI32_N.get_pixelc                 C  s   || j | |< d S r)   r]   rC   r&   r&   r'   r5   +  s    z_PyAccessI32_N.set_pixelNrT   r&   r&   r&   r'   rl   "  s   rl   c                   @  s:   e Zd ZdZdd Zdd Zdddddd	Zd
d ZdS )_PyAccessI32_Swapz,I;32L/B access, with byteswapping conversionc                 O  s   | j | _d S r)   rm   rK   r&   r&   r'   r#   2  s    z_PyAccessI32_Swap._post_initc                 C  s\   t d|}t d|}|d |d |d |d f\|d< |d< |d< |d< t d|d S )Nzint *zunsigned char *r,   rY   r   r   )r   newr   )r$   iorigcharsr&   r&   r'   reverse5  s    4z_PyAccessI32_Swap.reverser>   r?   c                 C  s   |  | j| | S r)   rs   rJ   rB   r&   r&   r'   r;   ;  s    z_PyAccessI32_Swap.get_pixelc                 C  s   |  || j| |< d S r)   rt   rC   r&   r&   r'   r5   >  s    z_PyAccessI32_Swap.set_pixelN)rD   rE   rF   rU   r#   rs   r;   r5   r&   r&   r&   r'   rn   /  s
   rn   c                   @  s2   e Zd ZdZdd ZddddddZd	d
 ZdS )
_PyAccessFz32 bit float accessc                 O  s   t d| j| _d S )Nzfloat **rI   rK   r&   r&   r'   r#   E  s    z_PyAccessF._post_initr>   floatr?   c                 C  s   | j | | S r)   r]   rB   r&   r&   r'   r;   H  s    z_PyAccessF.get_pixelc                 C  s>   z|| j | |< W n& tk
r8   |d | j | |< Y nX d S )Nr   )rJ   r_   rC   r&   r&   r'   r5   K  s    z_PyAccessF.set_pixelNrT   r&   r&   r&   r'   ru   B  s   ru   )1Lr   zI;16NZLAZLar   ZRGBZLABZHSVZYCbCrZRGBAZRGBaZRGBXZCMYKFIlittlezI;16zI;16LzI;16BzI;32LzI;32BFr   r   zPyAccess | Noner   c                 C  s.   t | jd }|s$td| j d S || |S )NzPyAccess Not Implemented: %s)mode_mapgetr!   loggerdebug)r   r   Zaccess_typer&   r&   r'   ro   x  s
    ro   )F)%
__future__r   loggingsystypingr   Z
_deprecater   __annotations__Zcffir   Zdefsr   ZcdefImportErrorexZ_utilr   ro   	getLoggerrD   r~    r	   r
   rG   rV   r[   r\   r`   rc   rj   rl   rn   ru   r|   	byteorderr&   r&   r&   r'   <module>   st     
e
