
    ⨽g59                         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y)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         |   ||||       || _        y )N)
extensionsmodes)super__init___fif)selfnamedescriptionr   r   r   	__class__s         b/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-packages/imageio/plugins/freeimage.pyr   zFreeimageFormat.__init__*   s     {zO	    c                     | j                   S N)r   r   s    r   r   zFreeimageFormat.fif.   s    yyr   c                 H   t        j                         rxt        |d      s1	 t        j                  |j                  d|j
                        |_        |j                  | j                  k(  ry|j                  dk(  r| j                  dk(  ryy y y # t        $ r
 d|_        Y Nw xY w)Nr   rT      )	r   has_libhasattrgetFIFfilename
firstbytesr   	Exceptionr   r   requests     r   	_can_readzFreeimageFormat._can_read2   s    ::<7F+&#%99W-=-=sGDVDV#WGL ||txx'"txx2~  (6"  ! &#%GL&s   0B B! B!c                 :   t        j                         rLt        |d      s&	 t        j                  |j                  d      |_        |j
                  | j                  u ryy y # t        $ r/ |j                  t        k(  r| j                  |_        nd|_        Y Rw xY w)Nr   wr!   T)
r   r$   r%   r&   r'   r   
ValueErrorraw_urir   r   r*   s     r   
_can_writezFreeimageFormat._can_writeA   s    ::<7F+*#%99W-=-=s#CGL ||txx' (  " *,6'+xx')	*s   %A" "5BBc                   ,    e Zd Zd ZddZd Zd Zd Zy)FreeimageFormat.Readerc                      y)Nr    r   s    r   _get_lengthz"FreeimageFormat.Reader._get_lengthR   s    r   c                     t        j                  | j                  j                  | j                  j
                  |      | _        | j                  j                  | j                  j                                y r   )	r   create_bitmapr+   r'   formatr   _bmload_from_filenameget_local_filenamer   flagss     r   _openzFreeimageFormat.Reader._openU   sJ    ''(=(=t{{PUVDHHH''(G(G(IJr   c                 8    | j                   j                          y r   )r:   closer   s    r   _closezFreeimageFormat.Reader._closeY   s    HHNNr   c                     |dk7  rt        d      | j                  j                         | 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\   s:    z !NOO88**,dhh.D.D.FFFr   c                 X    ||dk(  s
t               | j                  j                         S )Nr   )rD   r:   rF   rG   s     r   _get_meta_dataz%FreeimageFormat.Reader._get_meta_dataa   s(    MUaZ l"88))++r   Nr   )__name__
__module____qualname__r6   r?   rB   rI   rK   r5   r   r   Readerr3   Q   s    		K		G
	,r   rP   c                   &    e Zd ZddZd Zd Zd Zy)FreeimageFormat.Writerc                 <    || _         d | _        d| _        i | _        y )NF)_flagsr:   _is_set_metar=   s     r   r?   zFreeimageFormat.Writer._openi   s    DKDH DLDJr   c                     | j                   j                  | j                         | j                   j                  | j                  j                                | j                   j                          y r   )r:   set_meta_datarV   save_to_filenamer+   r<   rA   r   s    r   rB   zFreeimageFormat.Writer._closeo   sE    HH""4::.HH%%dll&E&E&GHHHNNr   c                    | j                   sd| _         nt        d      |j                  dk(  r|j                  d   dk(  r|d d d d df   }| j                  it        j                  | j                  j                  | j                  j                  | j                        | _        | j                  j                  |       | j                  j                  |       || _        y )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  ww!| 11a[xx++LL))4;;??DKK !!"%HH##B' DJr   c                     || _         y r   )rV   )r   rc   s     r   _set_meta_dataz%FreeimageFormat.Writer._set_meta_data   s	    DJr   NrL   )rM   rN   rO   r?   rB   rd   rf   r5   r   r   WriterrR   h   s    			.	r   rg   )NN)rM   rN   rO   __doc___modesr   propertyr   r,   r1   r   rP   rg   __classcell__)r   s   @r   r   r   %   sZ    .Fd    , ,.& & &r   r   c                   :    e Zd ZdZ G d dej
                        Zy)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y)FreeimageBmpFormat.Writerc                     t        |      }|r|t        j                  z  }n|t        j                  z  }t        j
                  j                  | |      S r   )intr	   BMP_SAVE_RLEBMP_DEFAULTr   rg   r?   )r   r>   compressions      r   r?   zFreeimageBmpFormat.Writer._open   sG    JE...---"))//e<<r   c                 ^    t        |d      }t        j                  j                  | ||      S )N   bitdepth)r   r   rg   rd   ra   s      r   rd   z&FreeimageBmpFormat.Writer._append_data   s)    rA.B"))66tRFFr   N)r   FrM   rN   rO   r?   rd   r5   r   r   rg   ro      s    	=	Gr   rg   NrM   rN   rO   rh   r   rg   r5   r   r   rm   rm      s    (G'' Gr   rm   c                   d    e Zd ZdZ G d dej
                        Z G d dej                        Zy)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y)FreeimagePngFormat.Readerc                     t        |      }|r|t        j                  z  }t        j                  j                  | |      S r   )rq   r	   PNG_IGNOREGAMMAr   rP   r?   )r   r>   ignoregammas      r   r?   zFreeimagePngFormat.Reader._open   s7    JE1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y)FreeimagePngFormat.Writerc                 B   t         j                  t         j                  t         j                  t         j                  d}t        |      }|r|t         j                  z  }	 |||   z  }t        j                  j                  | |      S # t        $ r t        d      w xY w)N)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0,,5522	O JE000K55 #))//e<<  K !IJJKs   !B	 	Bc                    t        |j                        dk(  rt        |d      }nt        |d      }t        j                  j                  | ||       t        | j                  j                  j                  dd            }|sy |j                  dk(  r|j                  d   dk(  st        d	      |d
k  s|dkD  rt        d      | j                  j                  d|      }| j                  j                          || _        y )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    288}("23"22""//b$?DLL''++J>?Agglrxx|q'8 !?@@Q!c' !DEEXX&&q!, r   N)r   r   r   Fry   r5   r   r   rg   r      s    	=$	r   rg   NrM   rN   rO   rh   r   rP   rg   r5   r   r   r|   r|      s+    <='' =$'' $r   r|   c                   d    e Zd ZdZ G d dej
                        Z G d dej                        Zy)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y)FreeimageJpegFormat.Readerc                     t        |      }|r|dk7  r|t        j                  z  }|s|t        j                  z  }t        j
                  j                  | |      S )Nr   )rq   r	   JPEG_EXIFROTATEJPEG_ACCURATEr   rP   r?   )r   r>   
exifrotate	quickreads       r   r?   z FreeimageJpegFormat.Reader._open4  sP    JEjAo111///"))//e<<r   c                 t    t         j                  j                  | |      \  }}| j                  ||      }||fS r   )r   rP   rI   _rotate)r   rH   rb   rc   s       r   rI   z$FreeimageJpegFormat.Reader._get_data>  s7    &--77eDHBb$'Bt8Or   c                 d   | j                   j                  j                  dd      dk(  rv	 |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
                  |      }|S |S # t        $ r Y |S w xY w)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}XXb!_f}XXb!_f}XXb\l*YYr]I2I    Is   B" "	B/.B/N)r   TF)rM   rN   rO   r?   rI   r   r5   r   r   rP   r   3  s    	=	
	r   rP   c                       e Zd Z	 ddZd Zy)FreeimageJpegFormat.Writerc                     t        |      }|dk  s|dkD  rt        d      t        |      }||z  }|r|t        j                  z  }|r|t        j                  z  }|r|t        j
                  z  }t        j                  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     'lG{gm !LMMJEWE222//////"))//e<<r   c                     |j                   dk(  r|j                  d   dk(  rt        d      t        |d      }t        j
                  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  sO    ww!| 1DEErA.B"))66tRFFr   N)r   K   FFFry   r5   r   r   rg   r   _  s    SX	=&	Gr   rg   Nr   r5   r   r   r   r     s/    #J('' (XG'' Gr   r   c                   :    e Zd ZdZ G d dej
                        Zy)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y)FreeimagePnmFormat.Writerc                     t        |      }|r|t        j                  z  }t        j                  j                  | |      S r   )rq   r	   PNM_SAVE_ASCIIr   rg   r?   )r   r>   	use_asciis      r   r?   zFreeimagePnmFormat.Writer._open  s7    JE000"))//e<<r   Nr   r   r5   r   r   rg   r     r   r   rg   Nrz   r5   r   r   r   r   z  s    $='' =r   r   )rh   numpyr   corer   r   core.requestr   
_freeimager   r	   r
   r   r   rm   r|   r   r   r5   r   r   <module>r      se   0  ( ' B B
if id"G "GJN NbjG/ jGZ= =r   