
     hL                     >   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ej)                           G d de      Zd Z G d de      ZddZddZd Zd Zd Zy# e$ r dZY Dw xY w)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y)ImageLoadingErrorz`PixBuf returned an error when loading an image.

    The image data is probably corrupted.

    N)__name__
__module____qualname____doc__     l/home/developers/rajanand/mypropertyqr-fmb-refixing-v2/venv/lib/python3.12/site-packages/cairocffi/pixbuf.pyr   r   *   s    r   r   c                 Z   | d   } t        |      | t        j                  k(  k(  sJ | t        j                  k7  rr| j                  t        j                  k7  r3dt        j                  | j                        j                  dd      z   }nd}t        j                  |        t        |      y)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%388"3444==CHH$'zz%--077	JKG %G% (( r   c                       e Zd ZdZd Zd Zy)Pixbufz2Wrap a ``GdkPixbuf`` pointer and simulate methods.c                     t         j                  |       t        j                  |t         j                        | _        y )N)gobjectg_object_refr   gcg_object_unref_pointer)selfpointers     r   __init__zPixbuf.__init__E   s(    W%w(>(>?r   c                 T    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    <@0r   r%   c           
         t        j                  t        j                         t        j
                        }t        j                  d      }|r|rt        j                  |||       t        |t        j                  || t        |       |             t        |t        j                  ||             t        j                  |      }|t         j                  k7  r7t        j                  t        j                  |            j!                  d      nd}t        j#                  |      }|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   s    VV((*G,B,BDFGGK E--feVD5*<<
C
OU4 55*<<VUKL55f=G chh 	

:88AB	$( 
 44V<F LMM&>;&&r   c                     t        | ||      \  }}t        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
    :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   sR     +:ufEFK'*F#393G3G3I#F+ (  Kr   c                     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_ARGB32rK   gdk_cairo_set_source_pixbufr+   
get_sourceget_surface)rH   dummy_contexts     r   rL   rL      sV     L)@)@!QGHM##A7##%1133r   c                    | j                         t        j                  k(  sJ | j                         dk(  sJ | j	                         dk(  sJ | j                         }| j                         }| j                         }t        j                  | j                         | 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<   a|
||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      s>      "j&C&CCCC  "a'''%%'1,,,E F$$&IZZ))+V-C-C-EFFAYF  7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        || 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 *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     WWZ(N'')$KGGK E5&//SWWXv%>.)3778T+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 "$
%	2 !
 	,KM 	&BD
$?AC    
 )"0V 0!'H .	4/;d<G  
Cs   
B BB