§
    `Ò³g=4  ã                   ó  — d Z ddlZddl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  ej        e¦  «        Z	 ddlZn# e$ r dZY nw xY w	 ddlZn# e$ r dZY nw xY w G d„ dej        ¦  «        Zd„ Z edd	¦  «        ZdS )
)Ú	PdfBitmapÚPdfBitmapInfoé    N)Ú
namedtuple)ÚPdfiumErrorc                   óÈ   ‡ — e Zd ZdZˆ fd„Zed„ ¦   «         Zd„ Zedd„¦   «         Z	edd„¦   «         Z
edd	„¦   «         Zedd
„¦   «         Zd„ Zd„ Zd„ Zedd„¦   «         Zˆ xZS )r   aˆ  
    Bitmap helper class.
    
    Hint:
        This class provides built-in converters (e. g. :meth:`.to_pil`, :meth:`.to_numpy`) that may be used to create a different representation of the bitmap.
        Converters can be applied on :class:`.PdfBitmap` objects either as bound method (``bitmap.to_*()``), or as function (``PdfBitmap.to_*(bitmap)``)
        The second pattern is useful for API methods that need to apply a caller-provided converter (e. g. :meth:`.PdfDocument.render`)
    
    .. _PIL Modes: https://pillow.readthedocs.io/en/stable/handbook/concepts.html#concept-modes
    
    Note:
        All attributes of :class:`.PdfBitmapInfo` are available in this class as well.
    
    Warning:
        ``bitmap.close()``, which frees the buffer of foreign bitmaps, is not validated for safety.
        A bitmap must not be closed when other objects still depend on its buffer!
    
    Attributes:
        raw (FPDF_BITMAP):
            The underlying PDFium bitmap handle.
        buffer (~ctypes.c_ubyte):
            A ctypes array representation of the pixel data (each item is an unsigned byte, i. e. a number ranging from 0 to 255).
    c	                 ón  •— ||||f\  | _         | _        | _        | _        |||c| _        | _        | _        t          j        | j                 | _	        | j        rt          j
        nt          j        | j                 | _        t          ¦   «                              t          j        || j        ¬¦  «         d S )N)Ú
needs_freeÚobj)ÚrawÚbufferÚwidthÚheightÚstrideÚformatÚrev_byteorderÚpdfium_iÚBitmapTypeToNChannelsÚ
n_channelsÚBitmapTypeToStrReverseÚBitmapTypeToStrÚmodeÚsuperÚ__init__Úpdfium_cÚFPDFBitmap_Destroy)
Úselfr   r   r   r   r   r   r   r	   Ú	__class__s
            €úW/var/www/html/mpstechhub/venv/lib/python3.11/site-packages/pypdfium2/_helpers/bitmap.pyr   zPdfBitmap.__init__4   sš   ø€ Ø9<¸fÀeÈVÐ9SÑ6ˆŒ�$”+˜tœz¨4¬;Ø7=¸vÀ}Ð4ˆŒ�T”[ $Ô"4Ý"Ô8¸¼ÔEˆŒØ8<Ô8JÐh•XÔ4Ð4ÕPXÔPhÐjnÔjuÔvˆŒ	Ý‰Œ×Ò�Ô4ÀÐQUÔQ\ÐÑ]Ô]Ð]Ð]Ð]ó    c                 ó   — d S )N© ©r   s    r   ÚparentzPdfBitmap.parent<   s   € àˆtr   c           	      ót   — t          | j        | j        | j        | j        | j        | j        | j        ¬¦  «        S )zY
        Returns:
            PdfBitmapInfo: A namedtuple describing the bitmap.
        )r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r"   s    r   Úget_infozPdfBitmap.get_infoA   s:   € õ
 Ø”* T¤[¸¼ÈTÌ[ØÔ,¸¼ÈtÌyð
ñ 
ô 
ð 	
r   FNc           
      óœ  — t          j        |¦  «        }t          j        |¦  «        }t          j        |¦  «        }t          j        |¦  «        }|€dd}t          j        |¦  «        }	|	€t          d¦  «        ‚t          j        |	t          j	        t          j
        ||z  z  ¦  «        ¦  «        j        }
nd}|}
 | ||
||||||¬¦  «        S )aÐ  
        Construct a :class:`.PdfBitmap` wrapper around a raw PDFium bitmap handle.
        
        Parameters:
            raw (FPDF_BITMAP):
                PDFium bitmap handle.
            rev_byteorder (bool):
                Whether the bitmap uses reverse byte order.
            ex_buffer (~ctypes.c_ubyte | None):
                If the bitmap was created from a buffer allocated by Python/ctypes, pass in the ctypes array to keep it referenced.
        NTz3Failed to get bitmap buffer (null pointer returned)F)r   r   r   r   r   r   r   r	   )r   ÚFPDFBitmap_GetWidthÚFPDFBitmap_GetHeightÚFPDFBitmap_GetFormatÚFPDFBitmap_GetStrideÚFPDFBitmap_GetBufferr   ÚctypesÚcastÚPOINTERÚc_ubyteÚcontents)Úclsr   r   Ú	ex_bufferr   r   r   r   r	   Ú
buffer_ptrr   s              r   Úfrom_rawzPdfBitmap.from_rawL   sÕ   € õ Ô,¨SÑ1Ô1ˆÝÔ.¨sÑ3Ô3ˆÝÔ.¨sÑ3Ô3ˆÝÔ.¨sÑ3Ô3ˆàÐØˆJÝ!Ô6°sÑ;Ô;ˆJØÐ!Ý!Ð"WÑXÔXÐXÝ”[ ­V¬^½F¼NÈfÐW]ÉoÑ<^Ñ-_Ô-_Ñ`Ô`ÔiˆFˆFàˆJØˆFàˆsØ˜F¨%¸ÀvØ¨À:ð
ñ 
ô 
ð 	
r   c                 óÀ   — |t           j        |         z  }|€t          j        ||z  z  ¦   «         }t	          j        |||||¦  «        }|                      |||¦  «        S )zÌ
        Create a new bitmap using :func:`FPDFBitmap_CreateEx`, with a buffer allocated by Python/ctypes.
        Bitmaps created by this function are always packed (no unused bytes at line end).
        )r   r   r,   r/   r   ÚFPDFBitmap_CreateExr4   )r1   r   r   r   r   r   r   r   s           r   Ú
new_nativezPdfBitmap.new_nativep   sa   € ð �Ô7¸Ô?Ñ?ˆØˆ>Ý”n¨°©Ñ8Ñ;Ô;ˆFÝÔ*¨5°&¸&À&È&ÑQÔQˆð �|Š|˜C °Ñ7Ô7Ð7r   c                 ó�   — |r|t           j        |         z  nd}t          j        |||d|¦  «        }|                      ||¦  «        S )zÂ
        Create a new bitmap using :func:`FPDFBitmap_CreateEx`, with a buffer allocated by PDFium.
        
        Using this method is discouraged. Prefer :meth:`.new_native` instead.
        r   N)r   r   r   r6   r4   )r1   r   r   r   r   Úforce_packedr   r   s           r   Únew_foreignzPdfBitmap.new_foreign€   sO   € ð DPÐV��Ô7¸Ô?Ñ?Ð?ÐUVˆÝÔ*¨5°&¸&À$ÈÑOÔOˆØ�|Š|˜C Ñ/Ô/Ð/r   c                 óZ   — t          j        |||¦  «        }|                      ||¦  «        S )a$  
        Create a new bitmap using :func:`FPDFBitmap_Create`. The buffer is allocated by PDFium.
        The resulting bitmap is supposed to be packed (i. e. no gap of unused bytes between lines).
        
        Using this method is discouraged. Prefer :meth:`.new_native` instead.
        )r   ÚFPDFBitmap_Creater4   )r1   r   r   Ú	use_alphar   r   s         r   Únew_foreign_simplezPdfBitmap.new_foreign_simpleŒ   s,   € õ Ô(¨°¸	ÑBÔBˆØ�|Š|˜C Ñ/Ô/Ð/r   c                 ól   — t          j        || j        ¦  «        }t          j        | |||||¦  «         dS )aµ  
        Fill a rectangle on the bitmap with the given color.
        The coordinate system starts at the top left corner of the image.
        
        Note:
            This function replaces the color values in the given rectangle. It does not perform alpha compositing.
        
        Parameters:
            color (tuple[int, int, int, int]):
                RGBA fill color (a tuple of 4 integers ranging from 0 to 255).
        N)r   Úcolor_tohexr   r   ÚFPDFBitmap_FillRect)r   ÚleftÚtopr   r   ÚcolorÚc_colors          r   Ú	fill_rectzPdfBitmap.fill_rect˜   s:   € õ Ô& u¨dÔ.@ÑAÔAˆÝÔ$ T¨4°°e¸VÀWÑMÔMÐMÐMÐMr   c                 ó’   — t          j        | j        | j        | j        ft
          j        | j        | j        | j        df¬¦  «        }|S )a*  
        Convert the bitmap to a :mod:`numpy` array.
        
        The array contains as many rows as the bitmap is high.
        Each row contains as many pixels as the bitmap is wide.
        The length of each pixel corresponds to the number of channels.
        
        The resulting array is supposed to share memory with the original bitmap buffer,
        so changes to the buffer should be reflected in the array, and vice versa.
        
        Returns:
            numpy.ndarray: NumPy array (representation of the bitmap buffer).
        é   )ÚshapeÚdtyper   Ústrides)	ÚnumpyÚndarrayr   r   r   r,   r/   r   r   )r   Úarrays     r   Úto_numpyzPdfBitmap.to_numpyµ   sI   € õ" ”à”[ $¤*¨d¬oÐ>Ý”NØ”[à”{ D¤O°QÐ7ð
ñ 
ô 
ˆð ˆr   c           	      óÂ   — t           j        | j                 }t          j                             || j        | j        f| j        d| j	        | j
        d¦  «        }d|_        |S )a[  
        Convert the bitmap to a :mod:`PIL` image, using :func:`PIL.Image.frombuffer`.
        
        For ``RGBA``, ``RGBX`` and ``L`` buffers, PIL is supposed to share memory with
        the original bitmap buffer, so changes to the buffer should be reflected in the image, and vice versa.
        Otherwise, PIL will make a copy of the data.
        
        Returns:
            PIL.Image.Image: PIL image (representation or copy of the bitmap buffer).
        
        .. versionchanged:: 4.16 Set ``image.readonly = False`` so that changes to the image are also reflected in the buffer.
        r   rH   F)r   r   r   ÚPILÚImageÚ
frombufferr   r   r   r   r   Úreadonly)r   Ú	dest_modeÚimages      r   Úto_pilzPdfBitmap.to_pilÒ   s\   € õ" Ô3°D´KÔ@ˆ	Ý”	×$Ò$ØØŒZ˜œÐ%ØŒKØØŒIØŒKØñ
ô 
ˆð ˆŒàˆr   c                 ó|  — |j         t          j        v rt          j        |j                  }n&t          |¦  «        }t          j        |j                  }|                     ¦   «         }|r0t          j        t          |¦  «        z   	                    |¦  «        }n|}|j
        \  }}|                      |||d|¬¦  «        S )al  
        Convert a :mod:`PIL` image to a PDFium bitmap.
        Due to the restricted number of color formats and bit depths supported by PDFium's
        bitmap implementation, this may be a lossy operation.
        
        Bitmaps returned by this function should be treated as immutable (i.e. don't call :meth:`.fill_rect`).
        
        Parameters:
            pil_image (PIL.Image.Image):
                The image.
        Returns:
            PdfBitmap: PDFium bitmap (with a copy of the PIL image's data).
        
        .. deprecated:: 4.25
           The *recopy* parameter has been deprecated.
        F)r   r   )r   r   ÚBitmapStrToConstÚ_pil_convert_for_pdfiumÚBitmapStrReverseToConstÚtobytesr,   r/   ÚlenÚfrom_buffer_copyÚsizer7   )r1   Ú	pil_imageÚrecopyr   Ú	py_bufferr   ÚwÚhs           r   Úfrom_pilzPdfBitmap.from_pilò   s¨   € ð& Œ>�XÔ6Ð6Ð6åÔ.¨y¬~Ô>ˆFˆFå/°	Ñ:Ô:ˆIÝÔ5°i´nÔEˆFà×%Ò%Ñ'Ô'ˆ	Øð 	Ý”n¥s¨9¡~¤~Ñ5×GÒGÈ	ÑRÔRˆFˆFàˆFàŒ~‰ˆˆ1Ø�~Š~˜a  F¸%Èˆ~ÑOÔOÐOr   )FN)FF)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr#   r%   Úclassmethodr4   r7   r:   r>   rF   rO   rW   re   Ú__classcell__)r   s   @r   r   r      sK  ø€ € € € € ðð ð0^ð ^ð ^ð ^ð ^ð ðð ñ „Xðð
ð 
ð 
ð ð
ð 
ð 
ñ „[ð
ðF ð8ð 8ð 8ñ „[ð8ð ð0ð 0ð 0ñ „[ð0ð ð0ð 0ð 0ñ „[ð0ðNð Nð Nð:ð ð ð:ð ð ð@ ð Pð  Pð  Pñ „[ð Pð  Pð  Pð  Pð  Pr   r   c                 ó–  — | j         dk    r|                      d¦  «        } nO| j                              d¦  «        rn4d| j         v r|                      d¦  «        } n|                      d¦  «        } | j         dk    r<|                      ¦   «         \  }}}t          j                             d|||f¦  «        } n‘| j         dk    r>|                      ¦   «         \  }}}}t          j                             d||||f¦  «        } nH| j         dk    r=|                      ¦   «         \  }}}}t          j                             d||||f¦  «        } | S )NÚ1ÚLÚRGBÚAÚRGBAÚRGBX)r   ÚconvertÚ
startswithÚsplitrQ   rR   Úmerge)r`   ÚrÚgÚbÚaÚxs         r   rZ   rZ     sA  € ð „~˜ÒÐØ×%Ò% cÑ*Ô*ˆ	ˆ	Ø	Œ×	"Ò	" 5Ñ	)Ô	)ð -ØØ	�	”Ð	Ð	Ø×%Ò% fÑ-Ô-ˆ	ˆ	à×%Ò% eÑ,Ô,ˆ	ð „~˜ÒÐØ—/’/Ñ#Ô#‰ˆˆ1ˆaÝ”I—O’O E¨A¨q°!¨9Ñ5Ô5ˆ	ˆ	Ø	Œ˜6Ò	!Ð	!Ø—_’_Ñ&Ô&‰
ˆˆ1ˆa�Ý”I—O’O F¨Q°°1°a¨LÑ9Ô9ˆ	ˆ	Ø	Œ˜6Ò	!Ð	!à—_’_Ñ&Ô&‰
ˆˆ1ˆa�Ý”I—O’O F¨Q°°1°a¨LÑ9Ô9ˆ	àÐr   r   z8width height stride format rev_byteorder n_channels mode)Ú__all__r,   ÚloggingÚweakrefÚcollectionsr   Úpypdfium2.rawr   r   Úpypdfium2.internalÚinternalr   Úpypdfium2._helpers.miscr   Ú	getLoggerrf   ÚloggerÚ	PIL.ImagerQ   ÚImportErrorrL   ÚAutoCloseabler   rZ   r   r!   r   r   ú<module>rŠ      sX  ðð )€à €€€Ø €€€Ø €€€Ø "Ð "Ð "Ð "Ð "Ð "Ø  Ð  Ð  Ð  Ð  Ð  Ø %Ð %Ð %Ð %Ð %Ð %Ø /Ð /Ð /Ð /Ð /Ð /à	ˆÔ	˜8Ñ	$Ô	$€ðØÐÐÐÐøØð ð ð Ø
€C€C€CðøøøðØ€L€L€L€LøØð ð ð Ø€E€E€EðøøøðxPð xPð xPð xPð xP�Ô'ñ xPô xPð xPð|ð ð ð8 �
˜?Ð,fÑgÔg€ðð s!   ¸= ½AÁAÁA ÁAÁA