B
    ñ¶I`x%  ã               @   sº  d dl Z d dlZy$d dlmZ dZeƒ Ze e¡ W n: ek
rn Z zddl	m
Z
 e
eƒ ZZW ddZ[X Y nX e  e¡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dœZejdk�r„e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;
    };
    é   )Údeferred_errorc               @   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-unpacked-wheel-wndzrte2/PIL/PyAccess.pyÚ__init__2   s    zPyAccess.__init__c             C   s   d S )Nr   )r   r   r   r   r   D   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__G   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__d   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   r   r   r   z   s    2zPyAccess.check_xyN)F)
Ú__name__Ú
__module__Ú__qualname__r   r   r(   r*   ZputpixelZgetpixelr   r   r   r   r   r   1   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 )N)r/   Ú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   éÿ   r   )r/   Úminr2   r3   )r   r&   r'   r%   r4   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 )Nzstruct Pixel_RGBA **)r   r   r   r/   )r   r0   r1   r   r   r   r   •   s    z_PyAccess32_3._post_initc             C   s   | j | | }|j|j|jfS )N)r/   r2   ÚgÚb)r   r&   r'   r4   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   r5   r   é   )r/   r6   r2   r9   r:   r3   )r   r&   r'   r%   r4   r   r   r   r#   œ   s
    z_PyAccess32_3.set_pixelN)r+   r,   r-   r7   r   r)   r#   r   r   r   r   r8   ’   s   r8   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 )Nzstruct Pixel_RGBA **)r   r   r   r/   )r   r0   r1   r   r   r   r   ¨   s    z_PyAccess32_4._post_initc             C   s"   | j | | }|j|j|j|jfS )N)r/   r2   r9   r:   r3   )r   r&   r'   r4   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   r5   r   r;   r   )r/   r6   r2   r9   r:   r3   )r   r&   r'   r%   r4   r   r   r   r#   ¯   s
    z_PyAccess32_4.set_pixelN)r+   r,   r-   r7   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 )	Ú
_PyAccess8z' 1, L, P, 8 bit images stored as uint8 c             O   s   | j | _d S )N)r   r/   )r   r0   r1   r   r   r   r   »   s    z_PyAccess8._post_initc             C   s   | j | | S )N)r/   )r   r&   r'   r   r   r   r)   ¾   s    z_PyAccess8.get_pixelc             C   sJ   yt |dƒ| j| |< W n, tk
rD   t |d dƒ| j| |< Y nX d S )Nr5   r   )r6   r/   Ú	TypeError)r   r&   r'   r%   r   r   r   r#   Á   s    z_PyAccess8.set_pixelN)r+   r,   r-   r7   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 )	Ú_PyAccessI16_Nz2 I;16 access, native bitendian without conversion c             O   s   t  d| j¡| _d S )Nzunsigned short **)r   r   r   r/   )r   r0   r1   r   r   r   r   Í   s    z_PyAccessI16_N._post_initc             C   s   | j | | S )N)r/   )r   r&   r'   r   r   r   r)   Ð   s    z_PyAccessI16_N.get_pixelc             C   sJ   yt |dƒ| j| |< W n, tk
rD   t |d dƒ| j| |< Y nX d S )Niÿÿ  r   )r6   r/   r>   )r   r&   r'   r%   r   r   r   r#   Ó   s    z_PyAccessI16_N.set_pixelN)r+   r,   r-   r7   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 )	Ú_PyAccessI16_Lz I;16L access, with conversion c             O   s   t  d| j¡| _d S )Nzstruct Pixel_I16 **)r   r   r   r/   )r   r0   r1   r   r   r   r   ß   s    z_PyAccessI16_L._post_initc             C   s   | j | | }|j|jd  S )Né   )r/   Úlr2   )r   r&   r'   r4   r   r   r   r)   â   s    z_PyAccessI16_L.get_pixelc             C   sX   | j | | }yt|dƒ}W n" tk
r>   t|d dƒ}Y nX |d@ |_|d? |_d S )Niÿÿ  r   r5   é   )r/   r6   r>   rB   r2   )r   r&   r'   r%   r4   r   r   r   r#   æ   s    
z_PyAccessI16_L.set_pixelN)r+   r,   r-   r7   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 )	Ú_PyAccessI16_Bz I;16B access, with conversion c             O   s   t  d| j¡| _d S )Nzstruct Pixel_I16 **)r   r   r   r/   )r   r0   r1   r   r   r   r   ô   s    z_PyAccessI16_B._post_initc             C   s   | j | | }|jd |j S )NrA   )r/   rB   r2   )r   r&   r'   r4   r   r   r   r)   ÷   s    z_PyAccessI16_B.get_pixelc             C   sX   | j | | }yt|dƒ}W n" tk
r>   t|d dƒ}Y nX |d? |_|d@ |_d S )Niÿÿ  r   rC   r5   )r/   r6   Ú	ExceptionrB   r2   )r   r&   r'   r%   r4   r   r   r   r#   û   s    
z_PyAccessI16_B.set_pixelN)r+   r,   r-   r7   r   r)   r#   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 )	Ú_PyAccessI32_Nz$ Signed Int32 access, native endian c             O   s   | j | _d S )N)r   r/   )r   r0   r1   r   r   r   r   	  s    z_PyAccessI32_N._post_initc             C   s   | j | | S )N)r/   )r   r&   r'   r   r   r   r)     s    z_PyAccessI32_N.get_pixelc             C   s   || j | |< d S )N)r/   )r   r&   r'   r%   r   r   r   r#     s    z_PyAccessI32_N.set_pixelN)r+   r,   r-   r7   r   r)   r#   r   r   r   r   rF     s   rF   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 )N)r   r/   )r   r0   r1   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   r;   r   r   )r   Únewr   )r   ÚiÚorigÚcharsr   r   r   Úreverse  s    4z_PyAccessI32_Swap.reversec             C   s   |   | j| | ¡S )N)rL   r/   )r   r&   r'   r   r   r   r)     s    z_PyAccessI32_Swap.get_pixelc             C   s   |   |¡| j| |< d S )N)rL   r/   )r   r&   r'   r%   r   r   r   r#   "  s    z_PyAccessI32_Swap.set_pixelN)r+   r,   r-   r7   r   rL   r)   r#   r   r   r   r   rG     s
   rG   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 **)r   r   r   r/   )r   r0   r1   r   r   r   r   )  s    z_PyAccessF._post_initc             C   s   | j | | S )N)r/   )r   r&   r'   r   r   r   r)   ,  s    z_PyAccessF.get_pixelc             C   s>   y|| j | |< W n& tk
r8   |d | j | |< Y nX d S )Nr   )r/   r>   )r   r&   r'   r%   r   r   r   r#   /  s    z_PyAccessF.set_pixelN)r+   r,   r-   r7   r   r)   r#   r   r   r   r   rM   &  s   rM   )Ú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   rH   [  s
    rH   )F)ÚloggingÚsysZcffir   Zdefsr   ZcdefÚImportErrorÚexZ_utilr   Ú	getLoggerr+   rU   r   r.   r8   r<   r=   r?   r@   rD   rF   rG   rM   rS   Ú	byteorderrH   r   r   r   r   Ú<module>   sb   	
P
