
'"V"                 @   sM  d  d l  m Z d  d l Z d  d l Z d  d l m Z e j e  Z d Z	 e   Z
 e
 j e	  Gd d   d e  Z Gd d   d e  Z Gd	 d
   d
 e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z i e d 6e d 6e d 6e d 6e d 6e d  6e d! 6e d" 6e d# 6e d$ 6e d% 6e d& 6e d' 6e d( 6e d) 6Z e j d* k re e d+ <e e d, <e e d- <e e d. <e e d/ <n2 e e d+ <e e d, <e e d- <e e d. <e e d/ <d0 d1 d2  Z d S)3    )print_functionN)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 j d | d  |  _ t j d | d  |  _ t j d | d  |  _ | d |  _	 | d |  _
 |  j   d  S)Nzunsigned char **image8zint **image32imagexsizeysize)dictZimZunsafe_ptrsreadonlyfficastr   r   r   r   r	   
_post_init)selfimgr   vals r   ./tmp/pip-build-fj3bmf4v/Pillow/PIL/PyAccess.py__init__0   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   n  |  j |  \ } } |  j | | |  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

        :param xy: The pixel coordinate, given as (x, y).
        :param value: The pixel value.
        z%Attempt to putpixel a read only image)r   
ValueErrorcheck_xy	set_pixel)r   xycolorxyr   r   r   __setitem__A   s    		zPyAccess.__setitem__c             C   s%   |  j  |  \ } } |  j | |  S)aI  
        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).
        :returns: a pixel value for single band images, a tuple of
          pixel values for multiband images.
        )r   	get_pixel)r   r   r   r   r   r   r   __getitem__O   s    zPyAccess.__getitem__c             C   s]   | \ } } d | k o& |  j  k  n oG d | k oE |  j k  n sY t d   n  | S)Nr   zpixel location out of range)r   r	   r   )r   r   r   r   r   r   r   r   `   s    >zPyAccess.check_xyN)
__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  j d |  j  |  _ d  S)Nzstruct Pixel_RGBA **)r   r   r   pixels)r   argskwargsr   r   r   r   i   s    z_PyAccess32_2._post_initc             C   s!   |  j  | | } | j | j f S)N)r#   ra)r   r   r   pixelr   r   r   r   l   s    z_PyAccess32_2.get_pixelc             C   sA   |  j  | | } t | d d  | _ t | d d  | _ d  S)Nr         )r#   minr&   r'   )r   r   r   r   r(   r   r   r   r   p   s    z_PyAccess32_2.set_pixelN)r   r    r!   __doc__r   r   r   r   r   r   r   r"   g   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  j d |  j  |  _ d  S)Nzstruct Pixel_RGBA **)r   r   r   r#   )r   r$   r%   r   r   r   r   z   s    z_PyAccess32_3._post_initc             C   s'   |  j  | | } | j | j | j f S)N)r#   r&   gb)r   r   r   r(   r   r   r   r   }   s    z_PyAccess32_3.get_pixelc             C   sW   |  j  | | } t | d d  | _ t | d d  | _ t | d d  | _ d  S)Nr   r)   r*      )r#   r+   r&   r.   r/   )r   r   r   r   r(   r   r   r   r      s    z_PyAccess32_3.set_pixelN)r   r    r!   r,   r   r   r   r   r   r   r   r-   w   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  j d |  j  |  _ d  S)Nzstruct Pixel_RGBA **)r   r   r   r#   )r   r$   r%   r   r   r   r      s    z_PyAccess32_4._post_initc             C   s-   |  j  | | } | j | j | j | j f S)N)r#   r&   r.   r/   r'   )r   r   r   r(   r   r   r   r      s    z_PyAccess32_4.get_pixelc             C   sm   |  j  | | } t | d d  | _ t | d d  | _ t | d d  | _ t | d d  | _ d  S)Nr   r)   r*   r0      )r#   r+   r&   r.   r/   r'   )r   r   r   r   r(   r   r   r   r      s
    z_PyAccess32_4.set_pixelN)r   r    r!   r,   r   r   r   r   r   r   r   r1      s   r1   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   r$   r%   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   sK   y t  | d  |  j | | <Wn& t  | d d  |  j | | <Yn Xd  S)Nr)   r   )r+   r#   )r   r   r   r   r   r   r   r      s    z_PyAccess8.set_pixelN)r   r    r!   r,   r   r   r   r   r   r   r   r3      s   r3   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  j d |  j  |  _ d  S)Nzunsigned short **)r   r   r   r#   )r   r$   r%   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   sK   y t  | d  |  j | | <Wn& t  | d d  |  j | | <Yn Xd  S)Ni  r   )r+   r#   )r   r   r   r   r   r   r   r      s    z_PyAccessI16_N.set_pixelN)r   r    r!   r,   r   r   r   r   r   r   r   r4      s   r4   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  j d |  j  |  _ d  S)Nzstruct Pixel_I16 **)r   r   r   r#   )r   r$   r%   r   r   r   r      s    z_PyAccessI16_L._post_initc             C   s#   |  j  | | } | j | j d S)N   )r#   lr&   )r   r   r   r(   r   r   r   r      s    z_PyAccessI16_L.get_pixelc             C   sj   |  j  | | } y t | d  } Wn% t k
 rK t | d d  } Yn X| d @| _ | d ?| _ d  S)Ni  r   r)      )r#   r+   	TypeErrorr7   r&   )r   r   r   r   r(   r   r   r   r      s    z_PyAccessI16_L.set_pixelN)r   r    r!   r,   r   r   r   r   r   r   r   r5      s   r5   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  j d |  j  |  _ d  S)Nzstruct Pixel_I16 **)r   r   r   r#   )r   r$   r%   r   r   r   r      s    z_PyAccessI16_B._post_initc             C   s#   |  j  | | } | j d | j S)Nr6   )r#   r7   r&   )r   r   r   r(   r   r   r   r      s    z_PyAccessI16_B.get_pixelc          	   C   s`   |  j  | | } y t | d  } Wn t | d d  } Yn X| d ?| _ | d @| _ d  S)Ni  r   r8   r)   )r#   r+   r7   r&   )r   r   r   r   r(   r   r   r   r      s    z_PyAccessI16_B.set_pixelN)r   r    r!   r,   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)	_PyAccessI32_Nz$ Signed Int32 access, native endian c             O   s   |  j  |  _ d  S)N)r   r#   )r   r$   r%   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!   r,   r   r   r   r   r   r   r   r;      s   r;   c               @   sF   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   r$   r%   r   r   r   r      s    z_PyAccessI32_Swap._post_initc             C   sv   t  j d |  } t  j d |  } | d | d | d | d f \ | d <| d <| d <| d <t  j d |  d S)Nzint *zunsigned char *r2   r0   r*   r   )r   newr   )r   iorigcharsr   r   r   reverse   s
    0z_PyAccessI32_Swap.reversec             C   s   |  j  |  j | |  S)N)rA   r#   )r   r   r   r   r   r   r      s    z_PyAccessI32_Swap.get_pixelc             C   s   |  j  |  |  j | | <d  S)N)rA   r#   )r   r   r   r   r   r   r   r      s    z_PyAccessI32_Swap.set_pixelN)r   r    r!   r,   r   rA   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)	
_PyAccessFz 32 bit float access c             O   s   t  j d |  j  |  _ d  S)Nzfloat **)r   r   r   r#   )r   r$   r%   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   s9   y | |  j  | | <Wn | d |  j  | | <Yn Xd  S)Nr   )r#   )r   r   r   r   r   r   r   r     s    z_PyAccessF.set_pixelN)r   r    r!   r,   r   r   r   r   r   r   r   rB     s   rB   1LPZ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 |  j d   } | s2 t j d |  j  d  S| |  |  S)NzPyAccess Not Implemented: %s)mode_mapgetmodeloggerdebug)r   r   Zaccess_typer   r   r   r=   6  s
    r=   )
__future__r   loggingsysZcffir   	getLoggerr   rL   Zdefsr   Zcdefobjectr   r"   r-   r1   r3   r4   r5   r:   r;   r<   rB   rI   	byteorderr=   r   r   r   r   <module>   sZ   
	9










