U
    &^$                     @   sz  d dl Z d dlZd dlmZ e eZdZe Ze	e 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dZejdkrDe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 < d$d"d#ZdS )%    N)FFIz`
struct Pixel_RGBA {
    unsigned char r,g,b,a;
};
struct Pixel_I16 {
    unsigned char l,r;
};
c                   @   s>   e Zd ZdddZdd Zdd Zdd	 ZeZeZd
d Z	dS )PyAccessFc                 C   s   t |jj}|| _td|d | _td|d | _td|d | _|jj	\| _
| _|j| _| jjdkrt|j| _|   d S )Nzunsigned char **image8zint **image32imageP)dictZimZunsafe_ptrsreadonlyfficastr   r   r   sizexsizeysize_immodeZpalette_palette
_post_init)selfimgr	   vals r   0/tmp/pip-install-a1j0c_p1/Pillow/PIL/PyAccess.py__init__,   s    zPyAccess.__init__c                 C   s   d S Nr   )r   r   r   r   r   >   s    zPyAccess._post_initc                 C   s   | j rtd|\}}|dk r(| j| }|dk r:| j| }| ||f\}}| jjdkr~t|tt	fr~t
|dkr~| j|}| |||S )a0  
        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

        :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	   
ValueErrorr   r   check_xyr   r   
isinstancelisttuplelenr   Zgetcolor	set_pixel)r   xycolorxyr   r   r   __setitem__A   s     




zPyAccess.__setitem__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   r   	get_pixelr   r#   r%   r&   r   r   r   __getitem__^   s    

zPyAccess.__getitem__c                 C   sF   |\}}d|  kr| j k r:n nd|  kr8| jk sBn td|S )Nr   zpixel location out of range)r   r   r   r)   r   r   r   r   t   s    2zPyAccess.check_xyN)F)
__name__
__module____qualname__r   r   r'   r*   ZputpixelZgetpixelr   r   r   r   r   r   +   s   
r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_PyAccess32_2z9 PA, LA, stored in first and last bytes of a 32 bit word c                 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_initc                 C   s   | j | | }|j|jfS r   )r1   rar   r%   r&   pixelr   r   r   r(      s    z_PyAccess32_2.get_pixelc                 C   s2   | j | | }t|d d|_t|d d|_d S )Nr         )r1   minr5   r6   r   r%   r&   r$   r8   r   r   r   r"      s    z_PyAccess32_2.set_pixelNr+   r,   r-   __doc__r   r(   r"   r   r   r   r   r.   {   s   r.   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_PyAccess32_3zC RGB and friends, stored in the first three bytes of a 32 bit word c                 O   s   t d| j| _d S r/   r0   r2   r   r   r   r      s    z_PyAccess32_3._post_initc                 C   s   | j | | }|j|j|jfS r   )r1   r5   gbr7   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   r9   r:      r1   r;   r5   r@   rA   r6   r<   r   r   r   r"      s
    z_PyAccess32_3.set_pixelNr=   r   r   r   r   r?      s   r?   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_PyAccess32_4z( RGBA etc, all 4 bytes of a 32 bit word c                 O   s   t d| j| _d S r/   r0   r2   r   r   r   r      s    z_PyAccess32_4._post_initc                 C   s"   | j | | }|j|j|j|jfS r   )r1   r5   r@   rA   r6   r7   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   r9   r:   rB   r   rC   r<   r   r   r   r"      s
    z_PyAccess32_4.set_pixelNr=   r   r   r   r   rD      s   rD   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	
_PyAccess8z' 1, L, P, 8 bit images stored as uint8 c                 O   s   | j | _d S r   )r   r1   r2   r   r   r   r      s    z_PyAccess8._post_initc                 C   s   | j | | S r   r1   r   r%   r&   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 )Nr9   r   r;   r1   	TypeErrorr   r%   r&   r$   r   r   r   r"      s    z_PyAccess8.set_pixelNr=   r   r   r   r   rE      s   rE   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_PyAccessI16_Nz2 I;16 access, native bitendian without conversion c                 O   s   t d| j| _d S )Nzunsigned short **r
   r   r   r1   r2   r   r   r   r      s    z_PyAccessI16_N._post_initc                 C   s   | j | | S r   rF   rG   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   rH   rJ   r   r   r   r"      s    z_PyAccessI16_N.set_pixelNr=   r   r   r   r   rK      s   rK   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_PyAccessI16_Lz I;16L access, with conversion c                 O   s   t d| j| _d S Nzstruct Pixel_I16 **rL   r2   r   r   r   r      s    z_PyAccessI16_L._post_initc                 C   s   | j | | }|j|jd  S N   r1   lr5   r7   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 )NrM   r   r9      )r1   r;   rI   rS   r5   r<   r   r   r   r"      s    
z_PyAccessI16_L.set_pixelNr=   r   r   r   r   rN      s   rN   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_PyAccessI16_Bz I;16B access, with conversion c                 O   s   t d| j| _d S rO   rL   r2   r   r   r   r      s    z_PyAccessI16_B._post_initc                 C   s   | j | | }|jd |j S rP   rR   r7   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 )NrM   r   rT   r9   )r1   r;   	ExceptionrS   r5   r<   r   r   r   r"      s    
z_PyAccessI16_B.set_pixelNr=   r   r   r   r   rU      s   rU   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_PyAccessI32_Nz$ Signed Int32 access, native endian c                 O   s   | j | _d S r   r   r1   r2   r   r   r   r     s    z_PyAccessI32_N._post_initc                 C   s   | j | | S r   rF   rG   r   r   r   r(     s    z_PyAccessI32_N.get_pixelc                 C   s   || j | |< d S r   rF   rJ   r   r   r   r"   	  s    z_PyAccessI32_N.set_pixelNr=   r   r   r   r   rW      s   rW   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )_PyAccessI32_Swapz. I;32L/B access, with byteswapping conversion c                 O   s   | j | _d S r   rX   r2   r   r   r   r     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   rB   r:   r   )r
   newr   )r   iorigcharsr   r   r   reverse  s    4z_PyAccessI32_Swap.reversec                 C   s   |  | j| | S r   r^   r1   rG   r   r   r   r(     s    z_PyAccessI32_Swap.get_pixelc                 C   s   |  || j| |< d S r   r_   rJ   r   r   r   r"     s    z_PyAccessI32_Swap.set_pixelN)r+   r,   r-   r>   r   r^   r(   r"   r   r   r   r   rY     s
   rY   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	
_PyAccessFz 32 bit float access c                 O   s   t d| j| _d S )Nzfloat **r0   r2   r   r   r   r   #  s    z_PyAccessF._post_initc                 C   s   | j | | S r   rF   rG   r   r   r   r(   &  s    z_PyAccessF.get_pixelc                 C   s>   z|| j | |< W n& tk
r8   |d | j | |< Y nX d S )Nr   )r1   rI   rJ   r   r   r   r"   )  s    z_PyAccessF.set_pixelNr=   r   r   r   r   r`      s   r`   )1Lr   ZLAZLaZPAZRGBZLABZHSVZYCbCrZRGBAZRGBaZRGBXZCMYKFIlittlezI;16zI;16LzI;16BzI;32LzI;32BFc                 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   rZ   U  s
    rZ   )F)loggingsysZcffir   	getLoggerr+   rh   Zdefsr
   Zcdefr   r.   r?   rD   rE   rK   rN   rU   rW   rY   r`   rf   	byteorderrZ   r   r   r   r   <module>   s\   

P
