3
U                  @   s8  d Z ddlZddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
mZ ddlmZ ydd	lmZ W n  ek
r   dd
lmZ Y nX dgZeeddZeeddZeeddZyeedddZW n ek
r   dZY nX ej  G dd deZdd ZG dd deZdd Zdd Z dd Z!d d! Z"d"d# Z#dS )$z
    cairocffi.pixbuf
    ~~~~~~~~~~~~~~~~

    Loading various image formats with GDK-PixBuf

    :copyright: Copyright 2013 by Simon Sapin
    :license: BSD, see LICENSE for details.

    N)BytesIO)partial)array   )dlopenImageSurfaceContext	constants)xrange)ffi)
ffi_pixbufdecode_to_image_surfacezgdk_pixbuf-2.0zgdk_pixbuf-2.0-0zgobject-2.0zgobject-2.0-0zglib-2.0z
glib-2.0-0zgdk-3zgdk-x11-2.0zgdk-win32-2.0-0c               @   s   e Zd ZdZdS )ImageLoadingErrorz`PixBuf returned an error when loading an image.

    The image data is probably corrupted.

    N)__name__
__module____qualname____doc__ r   r   P/var/www/html/enquirykeeper_venv/lib/python3.6/site-packages/cairocffi/pixbuf.pyr   (   s   r   c             C   sh   | d } t || tjkkst| tjkrd| jtjkrNdtj| jjdd }nd}tj|  t	|dS )z`Convert a :c:type:`GError**` to a Python :exception:`ImageLoadingError`,
    and raise it.

    r   zPixbuf error: utf8replacezPixbuf errorN)
boolr   NULLAssertionErrormessagestringdecodeglibZg_error_freer   )errorZreturn_valuer   r   r   r   handle_g_error0   s    

r   c               @   s    e Zd ZdZdd Zdd ZdS )Pixbufz8Wrap a :c:type:`GdkPixbuf` pointer and simulate methods.c             C   s   t j| tj|t j| _d S )N)gobjectZg_object_refr   gcg_object_unref_pointer)selfpointerr   r   r   __init__C   s    
zPixbuf.__init__c             C   s   t td| }t|| jS )NZgdk_pixbuf_)getattr
gdk_pixbufr   r$   )r%   namefunctionr   r   r   __getattr__G   s    zPixbuf.__getattr__N)r   r   r   r   r'   r,   r   r   r   r   r    A   s   r    c             C   s   t jtj tj}t jd}t|tj|t jd| t	| | t|tj
|| tj|}|t jkrzt jtj|jdnd}tj|}|t jkrtdt||fS )ah  Decode an image from memory with GDK-PixBuf.
    The file format is detected automatically.

    :param image_data: A byte string
    :returns:
        A tuple of a new :class:`PixBuf` object
        and the name of the detected image format.
    :raises:
        :exc:`ImageLoadingError` if the image data is invalid
        or in an unsupported format.

    z	GError **zguchar[]asciiNz*Not enough image data (got a NULL pixbuf.))r   r"   r)   Zgdk_pixbuf_loader_newr!   r#   newr   Zgdk_pixbuf_loader_writelenZgdk_pixbuf_loader_closeZgdk_pixbuf_loader_get_formatr   r   Zgdk_pixbuf_format_get_namer   Zgdk_pixbuf_loader_get_pixbufr   r    )
image_dataloaderr   Zformat_format_namepixbufr   r   r   decode_to_pixbufL   s    

$

r4   c             C   s<   t | \}}tdk	rt|n|j s,t|nt|}||fS )a~  Decode an image from memory into a cairo surface.
    The file format is detected automatically.

    :param image_data: A byte string
    :returns:
        A tuple of a new :class:`~cairocffi.ImageSurface` object
        and the name of the detected image format.
    :raises:
        :exc:`ImageLoadingError` if the image data is invalid
        or in an unsupported format.

    N)r4   gdkpixbuf_to_cairo_gdkZget_has_alphapixbuf_to_cairo_slicespixbuf_to_cairo_png)r0   r3   r2   Zsurfacer   r   r   r   l   s
    c             C   s2   t ttjdd}tj|j| jdd |j j S )zrConvert from PixBuf to ImageSurface, using GDK.

    This method is fastest but GDK is not always available.

    r   r   )	r   r   r	   FORMAT_ARGB32r5   Zgdk_cairo_set_source_pixbufr$   
get_sourceZget_surface)r3   Zdummy_contextr   r   r   r6      s    r6   c             C   s  | j  tjkst| j dks"t| j dks2t| j }| j }| j }t	j
| j | j }|dd }tjtj|}t|| }tjdk}|d }|d }	d| }
xt|D ]}|| }|| }|||d }||d |d }||d |d }|| }||	 }|rR|
|||d< |||d |d< |||d |d< |||d |d< q|
||d |d< |||d |d< |||d |d< ||||d< qW td	|}ttj||||S )
zConvert from PixBuf to ImageSurface, using slice-based byte swapping.

    This method is 2~5x slower than GDK but does not support an alpha channel.
    (cairo uses pre-multiplied alpha, but not Pixbuf.)

          Nbig      r      B)Zget_colorspacer)   ZGDK_COLORSPACE_RGBr   Zget_n_channelsZget_bits_per_sampleZ	get_widthZ
get_heightZget_rowstrider   bufferZ
get_pixelsZget_byte_lengthr   Zformat_stride_for_widthr	   FORMAT_RGB24	bytearraysys	byteorderr
   r   )r3   widthheightZ	rowstrideZpixelsZcairo_stridedataZ
big_endianZpixbuf_row_lengthZcairo_row_lengthalphayoffsetendZredZgreenZbluer   r   r   r7      sF    


r7   c             C   sv   t jd}t jd}t jd}t|| j||t jdd|t jddt jddt j t j|d |d }tjt|S )	zConvert from PixBuf to ImageSurface, by going through the PNG format.

    This method is 10~30x slower than GDK but always works.

    zgchar **zgsize *z	GError **zchar[]s   pngs   compression   0r   )	r   r.   r   Zsave_to_bufferr   rB   r   Zcreate_from_pngr   )r3   Zbuffer_pointerbuffer_sizer   Z	png_bytesr   r   r   r8      s    



r8   )$r   rE   ior   	functoolsr   r    r   r   r   r	   compatr
   Z_ffi_pixbufr   ImportErrorZ	ffi_buildr   __all__r)   r!   r   r5   OSErrorZg_type_init
ValueErrorr   r   objectr    r4   r   r6   r7   r8   r   r   r   r   <module>   s6   
 2