
    jL                     n   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 d	gZ eed
d          Z eedd          Z eedd          Z	  eedd          Zn# e$ r dZY nw xY we                                  G d de          Zd Z G d de          ZddZddZd Zd Zd ZdS )z
    cairocffi.pixbuf
    ~~~~~~~~~~~~~~~~

    Loading various image formats with GDK-PixBuf

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

    N)array)partial)BytesIO   )ContextImageSurface	constantsdlopen)
ffi_pixbufdecode_to_image_surface)zgdk_pixbuf-2.0zlibgdk_pixbuf-2.0-0)zlibgdk_pixbuf-2.0.so.0zlibgdk_pixbuf-2.0.0.dylibzlibgdk_pixbuf-2.0-0.dll)zgobject-2.0zlibgobject-2.0-0)zlibgobject-2.0.so.0zlibgobject-2.0.dylibzlibgobject-2.0-0.dll)zglib-2.0zlibglib-2.0-0)zlibglib-2.0.so.0zlibglib-2.0.dylibzlibglib-2.0-0.dll)zgdk-3z
libgdk-3-0)zlibgdk-3.so.0zlibgdk-3.0.dylibzlibgdk-3-0.dllc                       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__     W/home/ubuntu/budget-transfer-bot/.venv/lib/python3.11/site-packages/cairocffi/pixbuf.pyr   r   *   s           r   r   c                 ^   | d         } t          |          | t          j        k    k    sJ | t          j        k    rq| j        t          j        k    r1dt          j        | j                                      dd          z   }nd}t                              |            t          |          dS )zZConvert a ``GError**`` to a Python :exception:`ImageLoadingError`,
    and raise it.

    r   zPixbuf error: utf8replacezPixbuf errorN)	boolffiNULLmessagestringdecodeglibg_error_freer   )errorreturn_valuer   s      r   handle_g_errorr#   2   s    
 !HE%38"34444=CH$$'z%-0077	JJKGG %G%   ((( r   c                       e Zd ZdZd Zd ZdS )Pixbufz2Wrap a ``GdkPixbuf`` pointer and simulate methods.c                     t                               |           t          j        |t           j                  | _        d S )N)gobjectg_object_refr   gcg_object_unref_pointer)selfpointers     r   __init__zPixbuf.__init__E   s0    W%%%w(>??r   c                 \    t          t          d|z             }t          || j                  S )Ngdk_pixbuf_)getattr
gdk_pixbufr   r+   )r,   namefunctions      r   __getattr__zPixbuf.__getattr__I   s(    :}t';<<x///r   N)r   r   r   r   r.   r5   r   r   r   r%   r%   C   s;        <<@ @ @0 0 0 0 0r   r%   c           
         t          j        t                                          t          j                  }t          j        d          }|r|rt                              |||           t          |t          	                    || t          |           |                     t          |t                              ||                     t                              |          }|t           j        k    r?t          j        t                              |                                        d          nd}t                              |          }|t           j        k    rt%          d          t'          |          |fS )a  Decode an image from memory with GDK-PixBuf.
    The file format is detected automatically.

    :param image_data: A byte string
    :param width: Integer width in pixels or None
    :param height: Integer height in pixels or None
    :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.

    	GError **asciiNz*Not enough image data (got a NULL pixbuf.))r   r)   r2   gdk_pixbuf_loader_newr'   r*   newgdk_pixbuf_loader_set_sizer#   gdk_pixbuf_loader_writelengdk_pixbuf_loader_closegdk_pixbuf_loader_get_formatr   r   gdk_pixbuf_format_get_namer   gdk_pixbuf_loader_get_pixbufr   r%   )
image_datawidthheightloaderr!   format_format_namepixbufs           r   decode_to_pixbufrI   N   sN    V((**G,BD DFGK  E E E--feVDDD5*<<
C
OOU4 4 5 5 55*<<VUKKLLL55f==G ch 	
:88AABB	$( 
 44V<<F LMMM&>>;&&r   c                     t          | ||          \  }}t          t          |          n1|                                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
    :param width: Integer width in pixels or None
    :param height: Integer height in pixels or None
    :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.

    )rI   gdkpixbuf_to_cairo_gdkget_has_alphapixbuf_to_cairo_slicespixbuf_to_cairo_png)rB   rC   rD   rH   rG   surfaces         r   r   r   r   sl     +:ufEEFK'*F###393G3G3I3I )#F+++ ((  Kr   c                     t          t          t          j        dd                    }t                              |j        | j        dd           |                                                                S )zrConvert from PixBuf to ImageSurface, using GDK.

    This method is fastest but GDK is not always available.

    r   r   )	r   r   r	   FORMAT_ARGB32rK   gdk_cairo_set_source_pixbufr+   
get_sourceget_surface)rH   dummy_contexts     r   rL   rL      sd     L)@!QGGHHM##A7 7 7##%%11333r   c                    |                                  t          j        k    sJ |                                 dk    sJ |                                 dk    sJ |                                 }|                                 }|                                 }t          j	        | 
                                |                                           }|dd         }t          j        t          j        |          }t!          ||z            }t"          j        dk    }|dz  }|dz  }	d|z  }
t'          |          D ]}||z  }||z   }|||d         }||dz   |d         }||dz   |d         }||z  }||	z   }|r*|
|||d<   |||dz   |d<   |||dz   |d<   |||dz   |d<   i|
||dz   |d<   |||dz   |d<   |||dz   |d<   ||||d<   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)get_colorspacer2   GDK_COLORSPACE_RGBget_n_channelsget_bits_per_sample	get_width
get_heightget_rowstrider   buffer
get_pixelsget_byte_lengthr   format_stride_for_widthr	   FORMAT_RGB24	bytearraysys	byteorderranger   )rH   rC   rD   	rowstridepixelscairo_stridedata
big_endianpixbuf_row_lengthcairo_row_lengthalphayoffsetendredgreenblues                    r   rN   rN      sq      ""j&CCCCC  ""a''''%%''1,,,,E  F$$&&IZ))++V-C-C-E-EFFFAAAYF  7' 'L\F*++D%'J	qyeOE6]] & &Q((VC\"vz#a'(fqjQ&'!'' 		&!&DA%(D!C!"%*D!C!"%)D!C!""%*D!C!"%(D!C!"%*D!C!"!%DAdD	.vt\; ; ;r   c                    t          j        d          }t          j        d          }t          j        d          }t          ||                     ||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 *r7   zchar[]s   pngs   compression   0r   )	r   r:   r#   save_to_bufferr   rf   r   create_from_pngr   )rH   buffer_pointerbuffer_sizer!   	png_bytess        r   rO   rO      s     WZ((N')$$KGK  E5&//SWXv%>%>.))378T+B+B     
>!,k!n==I'	(:(:;;;r   )NN)r   rl   r   	functoolsr   ior    r   r   r	   r
   r   r   __all__r2   r'   r   rK   OSErrorg_type_init
ValueErrorr   r#   objectr%   rI   r   rL   rN   rO   r   r   r   <module>r      s  	 	 


                   6 6 6 6 6 6 6 6 6 6 6 6 " " " " " "$
%V	2 ! !
 &	,KM M v	&BD D
&$?A ACC    
CCC          
   ) ) )"0 0 0 0 0V 0 0 0!' !' !' !'H       .	4 	4 	4/; /; /;d< < < < <s   A$ $A.-A.