
    0-Ph59                         d Z ddlZddlmZmZ ddlmZ ddlm	Z	m
Z
mZmZ  G d d	e          Z G d
 de          Z G d de          Z G d de          Z G d de          ZdS )ag  Read/Write images using FreeImage.

Backend Library: `FreeImage <https://freeimage.sourceforge.io/>`_

.. note::
    To use this plugin you have to install its backend::

        imageio_download_bin freeimage

    or you can download the backend using the function::

        imageio.plugins.freeimage.download()

Each Freeimage format has the ``flags`` keyword argument. See the `Freeimage
documentation <https://freeimage.sourceforge.io/>`_ for more information.

Parameters
----------
flags : int
    A freeimage-specific option. In most cases we provide explicit
    parameters for influencing image reading.

    N   )Formatimage_as_uint)RETURN_BYTES   )FNAME_PER_PLATFORMIO_FLAGSdownloadfic                        e Zd ZdZdZddd fdZed             Zd Zd Z	 G d	 d
e
j                  Z G d de
j                  Z xZS )FreeimageFormatz$See :mod:`imageio.plugins.freeimage`iN)fifc                `    t                                          ||||           || _        d S )N)
extensionsmodes)super__init___fif)selfnamedescriptionr   r   r   	__class__s         Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/imageio/plugins/freeimage.pyr   zFreeimageFormat.__init__*   s/    {zOOO			    c                     | j         S N)r   r   s    r   r   zFreeimageFormat.fif.   s
    yr   c                 $   t          j                    rxt          |d          s>	 t          j        |j        d|j                  |_        n# t          $ r
 d|_        Y nw xY w|j        | j        k    rdS |j        dk    r| j        dk    rdS d S d S d S )Nr   rT      )	r   has_libhasattrgetFIFfilename
firstbytesr   	Exceptionr   r   requests     r   	_can_readzFreeimageFormat._can_read2   s    :<< 	7F++ &&#%9W-=sGDV#W#WGLL  & & &#%GLLL&|tx''t""tx2~~ t	 	 #"~~s   %A AAc                    t          j                    rut          |d          sU	 t          j        |j        d          |_        n4# t          $ r' |j        t          k    r| j	        |_        nd|_        Y nw xY w|j        | j	        u rdS d S d S )Nr   wr!   T)
r   r$   r%   r&   r'   r   
ValueErrorraw_urir   r   r*   s     r   
_can_writezFreeimageFormat._can_writeA   s    :<< 
	7F++ **#%9W-=s#C#CGLL! * * *,66'+x')	*
 |tx''t
	 
	 ('s   A .A65A6c                   .    e Zd Zd ZddZd Zd Zd ZdS )	FreeimageFormat.Readerc                     dS )Nr    r   s    r   _get_lengthz"FreeimageFormat.Reader._get_lengthR   s    1r   r   c                     t          j        | j        j        | j        j        |          | _        | j                            | j                                                   d S r   )	r   create_bitmapr+   r'   formatr   _bmload_from_filenameget_local_filenamer   flagss     r   _openzFreeimageFormat.Reader._openU   sL    '(=t{PUVVDHH''(G(G(I(IJJJJJr   c                 8    | j                                          d S r   )r:   closer   s    r   _closezFreeimageFormat.Reader._closeY   s    HNNr   c                     |dk    rt          d          | j                                        | j                                        fS )Nr   z+This format only supports singleton images.)
IndexErrorr:   get_image_dataget_meta_datar   indexs     r   	_get_dataz FreeimageFormat.Reader._get_data\   sA    zz !NOOO8**,,dh.D.D.F.FFFr   c                 `    ||dk    st                      | j                                        S )Nr   )rD   r:   rF   rG   s     r   _get_meta_dataz%FreeimageFormat.Reader._get_meta_dataa   s-    MUaZZ ll"8))+++r   Nr   )__name__
__module____qualname__r6   r?   rB   rI   rK   r5   r   r   Readerr3   Q   sk        	 	 		K 	K 	K 	K	 	 		G 	G 	G
	, 	, 	, 	, 	,r   rP   c                   (    e Zd ZddZd Zd Zd ZdS )FreeimageFormat.Writerr   c                 >    || _         d | _        d| _        i | _        d S )NF)_flagsr:   _is_set_metar=   s     r   r?   zFreeimageFormat.Writer._openi   s"    DKDH DLDJJJr   c                     | j                             | j                   | j                             | j                                                   | j                                          d S r   )r:   set_meta_datarV   save_to_filenamer+   r<   rA   r   s    r   rB   zFreeimageFormat.Writer._closeo   sV    H""4:...H%%dl&E&E&G&GHHHHNNr   c                    | j         sd| _         nt          d          |j        dk    r |j        d         dk    r|d d d d df         }| j        Nt          j        | j        j        | j	        j
        | j                  | _        | j                            |           | j                            |           || _        d S )NTz1Singleton image; can only append image data once.   r!   r   r   )rU   RuntimeErrorndimshaper:   r   r8   r+   r'   r9   r   rT   allocateset_image_datarV   r   immetas      r   _append_dataz#FreeimageFormat.Writer._append_datav   s    < #"J   w!|| 1 1111a[x+L)4;?DK  !!"%%%H##B''' DJJJr   c                     || _         d S r   )rV   )r   rc   s     r   _set_meta_dataz%FreeimageFormat.Writer._set_meta_data   s    DJJJr   NrL   )rM   rN   rO   r?   rB   rd   rf   r5   r   r   WriterrR   h   sU        	 	 	 		 	 		 	 	.	 	 	 	 	r   rg   )NN)rM   rN   rO   __doc___modesr   propertyr   r,   r1   r   rP   rg   __classcell__)r   s   @r   r   r   %   s        ..Fd          X     , , , , , , , ,.& & & & & & & & & & & &r   r   c                   8    e Zd ZdZ G d dej                  ZdS )FreeimageBmpFormata  A BMP format based on the Freeimage library.

    This format supports grayscale, RGB and RGBA images.

    The freeimage plugin requires a `freeimage` binary. If this binary
    not available on the system, it can be downloaded manually from
    <https://github.com/imageio/imageio-binaries> by either

    - the command line script ``imageio_download_bin freeimage``
    - the Python method ``imageio.plugins.freeimage.download()``

    Parameters for saving
    ---------------------
    compression : bool
        Whether to compress the bitmap using RLE when saving. Default False.
        It seems this does not always work, but who cares, you should use
        PNG anyway.

    c                       e Zd ZddZd ZdS )FreeimageBmpFormat.Writerr   Fc                     t          |          }|r|t          j        z  }n|t          j        z  }t          j                            | |          S r   )intr	   BMP_SAVE_RLEBMP_DEFAULTr   rg   r?   )r   r>   compressions      r   r?   zFreeimageBmpFormat.Writer._open   sJ    JJE ...--")//e<<<r   c                 f    t          |d          }t          j                            | ||          S )N   bitdepth)r   r   rg   rd   ra   s      r   rd   z&FreeimageBmpFormat.Writer._append_data   s/    rA...B")66tRFFFr   N)r   FrM   rN   rO   r?   rd   r5   r   r   rg   ro      s<        	= 	= 	= 	=	G 	G 	G 	G 	Gr   rg   NrM   rN   rO   rh   r   rg   r5   r   r   rm   rm      sU         (G G G G G' G G G G Gr   rm   c                   ^    e Zd ZdZ G d dej                  Z G d dej                  ZdS )FreeimagePngFormataY  A PNG format based on the Freeimage library.

    This format supports grayscale, RGB and RGBA images.

    The freeimage plugin requires a `freeimage` binary. If this binary
    not available on the system, it can be downloaded manually from
    <https://github.com/imageio/imageio-binaries> by either

    - the command line script ``imageio_download_bin freeimage``
    - the Python method ``imageio.plugins.freeimage.download()``

    Parameters for reading
    ----------------------
    ignoregamma : bool
        Avoid gamma correction. Default True.

    Parameters for saving
    ---------------------
    compression : {0, 1, 6, 9}
        The compression factor. Higher factors result in more
        compression at the cost of speed. Note that PNG compression is
        always lossless. Default 9.
    quantize : int
        If specified, turn the given RGB or RGBA image in a paletted image
        for more efficient storage. The value should be between 2 and 256.
        If the value of 0 the image is not quantized.
    interlaced : bool
        Save using Adam7 interlacing. Default False.
    c                       e Zd ZddZdS )FreeimagePngFormat.Readerr   Tc                     t          |          }|r|t          j        z  }t          j                            | |          S r   )rq   r	   PNG_IGNOREGAMMAr   rP   r?   )r   r>   ignoregammas      r   r?   zFreeimagePngFormat.Reader._open   s;    JJE 211")//e<<<r   Nr   TrM   rN   rO   r?   r5   r   r   rP   r~      (        	= 	= 	= 	= 	= 	=r   rP   c                       e Zd ZddZd ZdS )FreeimagePngFormat.Writerr   	   Fc                 4   t           j        t           j        t           j        t           j        d}t          |          }|r|t           j        z  }	 |||         z  }n# t          $ r t          d          w xY wt          j
                            | |          S )N)r   r      r   z&Png compression must be 0, 1, 6, or 9.)r	   PNG_Z_NO_COMPRESSIONPNG_Z_BEST_SPEEDPNG_Z_DEFAULT_COMPRESSIONPNG_Z_BEST_COMPRESSIONrq   PNG_INTERLACEDKeyErrorr/   r   rg   r?   )r   r>   rt   quantize
interlacedcompression_maps         r   r?   zFreeimagePngFormat.Writer._open   s    0,52	 O JJE 100K55 K K K !IJJJK #)//e<<<s   A A7c                 "   t          |j                  dk    rt          |d          }nt          |d          }t          j                            | ||           t          | j        j        	                    dd                    }|sd S |j
        dk    r|j        d         dk    st          d	          |d
k     s|dk    rt          d          | j                            d|          }| j                                         || _        d S )Nuint16   rw   rv   r   Fr[   r!   zCan only quantize RGB imagesr      z!PNG quantize param must be 2..256r   )strdtyper   r   rg   rd   rq   r+   kwargsgetr]   r^   r/   r:   r   rA   )r   rb   rc   qbms        r   rd   z&FreeimagePngFormat.Writer._append_data   s   28}}(("2333"2222"//b$???DL'++J>>??A 	gllrx|q'8'8 !?@@@Q!c'' !DEEEX&&q!,,   r   N)r   r   r   Fry   r5   r   r   rg   r      s7        	= 	= 	= 	=$	 	 	 	 	r   rg   NrM   rN   rO   rh   r   rP   rg   r5   r   r   r|   r|      sx         <= = = = =' = = =$ $ $ $ $' $ $ $ $ $r   r|   c                   ^    e Zd ZdZ G d dej                  Z G d dej                  ZdS )FreeimageJpegFormataA  A JPEG format based on the Freeimage library.

    This format supports grayscale and RGB images.

    The freeimage plugin requires a `freeimage` binary. If this binary
    not available on the system, it can be downloaded manually from
    <https://github.com/imageio/imageio-binaries> by either

    - the command line script ``imageio_download_bin freeimage``
    - the Python method ``imageio.plugins.freeimage.download()``

    Parameters for reading
    ----------------------
    exifrotate : bool
        Automatically rotate the image according to the exif flag.
        Default True. If 2 is given, do the rotation in Python instead
        of freeimage.
    quickread : bool
        Read the image more quickly, at the expense of quality.
        Default False.

    Parameters for saving
    ---------------------
    quality : scalar
        The compression factor of the saved image (1..100), higher
        numbers result in higher quality but larger file size. Default 75.
    progressive : bool
        Save as a progressive JPEG file (e.g. for images on the web).
        Default False.
    optimize : bool
        On saving, compute optimal Huffman coding tables (can reduce a
        few percent of file size). Default False.
    baseline : bool
        Save basic JPEG, without metadata or any markers. Default False.

    c                   "    e Zd ZddZd Zd ZdS )	FreeimageJpegFormat.Readerr   TFc                     t          |          }|r|dk    r|t          j        z  }|s|t          j        z  }t          j                            | |          S )Nr   )rq   r	   JPEG_EXIFROTATEJPEG_ACCURATEr   rP   r?   )r   r>   
exifrotate	quickreads       r   r?   z FreeimageJpegFormat.Reader._open4  sX    JJE 2jAoo11 0//")//e<<<r   c                 |    t           j                            | |          \  }}|                     ||          }||fS r   )r   rP   rI   _rotate)r   rH   rb   rc   s       r   rI   z$FreeimageJpegFormat.Reader._get_data>  s:    &-77eDDHBb$''Bt8Or   c                 \   | j         j                            dd          dk    r	 |d         d         }|dv r	 |dv rt          j        |d          }|dv rt          j        |d	          }|d
v rt          j        |          }|dv rt          j        |          }n# t          $ r Y nw xY w|S )aN  Use Orientation information from EXIF meta data to
            orient the image correctly. Freeimage is also supposed to
            support that, and I am pretty sure it once did, but now it
            does not, so let's just do it in Python.
            Edit: and now it works again, just leave in place as a fallback.
            r   Nr   	EXIF_MAINOrientation)r   r   )r[      )   r   r[   )r"   rv   )r   r   r   r"   )r+   r   r   nprot90fliplrr   )r   rb   rc   oris       r   r   z"FreeimageJpegFormat.Reader._rotateC  s     |"&&|T::a??+{+M:C
 f}}f}}Xb!__f}}Xb!__f}}Xb\\l**Yr]]     D Is   B 
B)(B)N)r   TF)rM   rN   rO   r?   rI   r   r5   r   r   rP   r   3  sF        	= 	= 	= 	=	 	 	
	 	 	 	 	r   rP   c                       e Zd Z	 ddZd ZdS )FreeimageJpegFormat.Writerr   K   Fc                 $   t          |          }|dk     s|dk    rt          d          t          |          }||z  }|r|t          j        z  }|r|t          j        z  }|r|t          j        z  }t          j                            | |          S )Nr   d   z)JPEG quality should be between 1 and 100.)	rq   r/   r	   JPEG_PROGRESSIVEJPEG_OPTIMIZEJPEG_BASELINEr   rg   r?   )r   r>   qualityprogressiveoptimizebaselines         r   r?   z FreeimageJpegFormat.Writer._open`  s     'llG{{gmm !LMMMJJEWE 322 0// 0//")//e<<<r   c                     |j         dk    r |j        d         dk    rt          d          t          |d          }t          j                            | ||          S )Nr[   r!   r   z$JPEG does not support alpha channel.rv   rw   )r]   r^   IOErrorr   r   rg   rd   ra   s      r   rd   z'FreeimageJpegFormat.Writer._append_datas  sY    w!|| 1 1DEEErA...B")66tRFFFr   N)r   r   FFFry   r5   r   r   rg   r   _  s?        SX	= 	= 	= 	=&	G 	G 	G 	G 	Gr   rg   Nr   r5   r   r   r   r     s        # #J( ( ( ( (' ( ( (XG G G G G' G G G G Gr   r   c                   8    e Zd ZdZ G d dej                  ZdS )FreeimagePnmFormatal  A PNM format based on the Freeimage library.

    This format supports single bit (PBM), grayscale (PGM) and RGB (PPM)
    images, even with ASCII or binary coding.

    The freeimage plugin requires a `freeimage` binary. If this binary
    not available on the system, it can be downloaded manually from
    <https://github.com/imageio/imageio-binaries> by either

    - the command line script ``imageio_download_bin freeimage``
    - the Python method ``imageio.plugins.freeimage.download()``

    Parameters for saving
    ---------------------
    use_ascii : bool
        Save with ASCII coding. Default True.
    c                       e Zd ZddZdS )FreeimagePnmFormat.Writerr   Tc                     t          |          }|r|t          j        z  }t          j                            | |          S r   )rq   r	   PNM_SAVE_ASCIIr   rg   r?   )r   r>   	use_asciis      r   r?   zFreeimagePnmFormat.Writer._open  s;    JJE 100")//e<<<r   Nr   r   r5   r   r   rg   r     r   r   rg   Nrz   r5   r   r   r   r   z  sK         $= = = = =' = = = = =r   r   )rh   numpyr   corer   r   core.requestr   
_freeimager   r	   r
   r   r   rm   r|   r   r   r5   r   r   <module>r      ss   0     ( ( ( ( ( ( ( ( ' ' ' ' ' ' B B B B B B B B B B B B
i i i i if i i id"G "G "G "G "G "G "G "GJN N N N N N N NbjG jG jG jG jG/ jG jG jGZ= = = = = = = = = =r   