o
    h59                     @   s   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	 d	eZG d
d deZG dd deZG dd deZG d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                       sl   e Zd ZdZdZddd fddZedd Zd	d
 Zdd Z	G dd de
jZG dd de
jZ  ZS )FreeimageFormatz$See :mod:`imageio.plugins.freeimage`iN)fifc                   s   t  j||||d || _d S )N)
extensionsmodes)super__init___fif)selfnamedescriptionr   r   r   	__class__ T/var/www/html/scripts/venv/lib/python3.10/site-packages/imageio/plugins/freeimage.pyr   *   s   
zFreeimageFormat.__init__c                 C   s   | j S N)r   r   r   r   r   r   .   s   zFreeimageFormat.fifc                 C   sx   t  r6t|ds"zt |jd|j|_W n ty!   d|_Y nw |j| jkr*dS |jdkr8| jdkr:dS d S d S d S )Nr   rT      )	r
   has_libhasattrgetFIFfilename
firstbytesr   	Exceptionr   r   requestr   r   r   	_can_read2   s   

zFreeimageFormat._can_readc                 C   sl   t  r2t|ds*z
t |jd|_W n ty)   |jtkr$| j	|_nd|_Y nw |j| j	u r4dS d S d S )Nr   wr   T)
r
   r    r!   r"   r#   r   
ValueErrorraw_urir   r   r&   r   r   r   
_can_writeA   s   


	zFreeimageFormat._can_writec                   @   s6   e Zd Zdd ZdddZdd Zdd	 Zd
d ZdS )zFreeimageFormat.Readerc                 C   s   dS )Nr   r   r   r   r   r   _get_lengthR   s   z"FreeimageFormat.Reader._get_lengthr   c                 C   s.   t | jj| jj|| _| j| j  d S r   )	r
   create_bitmapr'   r#   formatr   _bmload_from_filenameget_local_filenamer   flagsr   r   r   _openU   s   zFreeimageFormat.Reader._openc                 C   s   | j   d S r   )r0   closer   r   r   r   _closeY   s   zFreeimageFormat.Reader._closec                 C   s$   |dkrt d| j | j fS )Nr   z+This format only supports singleton images.)
IndexErrorr0   get_image_dataget_meta_datar   indexr   r   r   	_get_data\   s   z FreeimageFormat.Reader._get_datac                 C   s    |d u s|dkst  | j S )Nr   )r8   r0   r:   r;   r   r   r   _get_meta_dataa   s   
z%FreeimageFormat.Reader._get_meta_dataNr   )__name__
__module____qualname__r-   r5   r7   r=   r>   r   r   r   r   ReaderQ   s    
rC   c                   @   s.   e Zd ZdddZdd Zdd Zdd	 Zd
S )zFreeimageFormat.Writerr   c                 C   s   || _ d | _d| _i | _d S )NF)_flagsr0   _is_set_metar3   r   r   r   r5   i   s   
zFreeimageFormat.Writer._openc                 C   s.   | j | j | j | j  | j   d S r   )r0   set_meta_datarF   save_to_filenamer'   r2   r6   r   r   r   r   r7   o   s   zFreeimageFormat.Writer._closec                 C   s   | j sd| _ ntd|jdkr"|jd dkr"|d d d d df }| jd u r:t| jj| j	j
| j| _| j| | j| || _d S )NTz1Singleton image; can only append image data once.   r   r   r   )rE   RuntimeErrorndimshaper0   r
   r.   r'   r#   r/   r   rD   allocateset_image_datarF   r   immetar   r   r   _append_datav   s   

z#FreeimageFormat.Writer._append_datac                 C   s
   || _ d S r   )rF   )r   rQ   r   r   r   _set_meta_data   s   
z%FreeimageFormat.Writer._set_meta_dataNr?   )r@   rA   rB   r5   r7   rR   rS   r   r   r   r   Writerh   s
    
rT   )NN)r@   rA   rB   __doc___modesr   propertyr   r(   r,   r   rC   rT   __classcell__r   r   r   r   r   %   s    
r   c                   @   "   e Zd ZdZG d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                   @   s   e Zd ZdddZdd ZdS )	zFreeimageBmpFormat.Writerr   Fc                 C   s0   t |}|r|tjO }n|tjO }tj| |S r   )intr   BMP_SAVE_RLEBMP_DEFAULTr   rT   r5   )r   r4   compressionr   r   r   r5      s
   
zFreeimageBmpFormat.Writer._openc                 C   s   t |dd}tj| ||S )N   bitdepth)r   r   rT   rR   rO   r   r   r   rR      s   z&FreeimageBmpFormat.Writer._append_dataN)r   Fr@   rA   rB   r5   rR   r   r   r   r   rT      s    

rT   Nr@   rA   rB   rU   r   rT   r   r   r   r   rZ      s    rZ   c                   @   4   e Zd ZdZG dd dejZG d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dZdS )zFreeimagePngFormat.Readerr   Tc                 C   $   t |}|r|tjO }tj| |S r   )r[   r   PNG_IGNOREGAMMAr   rC   r5   )r   r4   ignoregammar   r   r   r5         
zFreeimagePngFormat.Reader._openNr   Tr@   rA   rB   r5   r   r   r   r   rC          rC   c                   @   s   e Zd Zd	ddZdd ZdS )
zFreeimagePngFormat.Writerr   	   Fc                 C   sb   t jt jt jt jd}t|}|r|t jO }z||| O }W n ty)   tdw t	j
| |S )N)r   r      rn   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_COMPRESSIONr[   PNG_INTERLACEDKeyErrorr*   r   rT   r5   )r   r4   r^   quantize
interlacedcompression_mapr   r   r   r5      s   
zFreeimagePngFormat.Writer._openc                 C   s   t |jdkrt|dd}nt|dd}tj| || t| jj	dd}|s*d S |j
dkr6|jd dks:td	|d
k sB|dkrFtd| jd|}| j  || _d S )Nuint16   r`   r_   rv   FrI   r   zCan only quantize RGB imagesr      z!PNG quantize param must be 2..256r   )strdtyper   r   rT   rR   r[   r'   kwargsgetrK   rL   r*   r0   rv   r6   )r   rP   rQ   qbmr   r   r   rR      s   

z&FreeimagePngFormat.Writer._append_dataN)r   rn   r   Frb   r   r   r   r   rT      s    
rT   Nr@   rA   rB   rU   r   rC   rT   r   r   r   r   re      s    re   c                   @   rd   )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                   @   s&   e Zd ZdddZdd Zdd	 Zd
S )zFreeimageJpegFormat.Readerr   TFc                 C   s:   t |}|r|dkr|tjO }|s|tjO }tj| |S )Nr   )r[   r   JPEG_EXIFROTATEJPEG_ACCURATEr   rC   r5   )r   r4   
exifrotate	quickreadr   r   r   r5   4  s   

z FreeimageJpegFormat.Reader._openc                 C   s&   t j| |\}}| ||}||fS r   )r   rC   r=   _rotate)r   r<   rP   rQ   r   r   r   r=   >  s   z$FreeimageJpegFormat.Reader._get_datac                 C   s   | j jdddkrHz|d d }W n
 ty   Y |S w |dv r"	 |dv r,t|d}|dv r6t|d	}|d
v r?t|}|dv rHt|}|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   )rI      )   ro   rI   )r   r_   )r   r   r   r   )r'   r~   r   ru   nprot90fliplr)r   rP   rQ   orir   r   r   r   C  s$   

z"FreeimageJpegFormat.Reader._rotateN)r   TF)r@   rA   rB   r5   r=   r   r   r   r   r   rC   3  s    

rC   c                   @   s    e Zd Z	d	ddZdd ZdS )
zFreeimageJpegFormat.Writerr   K   Fc                 C   sh   t |}|dk s|dkrtdt |}||O }|r|tjO }|r&|tjO }|r-|tjO }tj| |S )Nr   d   z)JPEG quality should be between 1 and 100.)	r[   r*   r   JPEG_PROGRESSIVEJPEG_OPTIMIZEJPEG_BASELINEr   rT   r5   )r   r4   qualityprogressiveoptimizebaseliner   r   r   r5   `  s   


z FreeimageJpegFormat.Writer._openc                 C   s<   |j dkr|jd dkrtdt|dd}tj| ||S )NrI   r   r   z$JPEG does not support alpha channel.r_   r`   )rK   rL   IOErrorr   r   rT   rR   rO   r   r   r   rR   s  s   z'FreeimageJpegFormat.Writer._append_dataN)r   r   FFFrb   r   r   r   r   rT   _  s    
rT   Nr   r   r   r   r   r     s    %,r   c                   @   rY   )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                   @   rf   )zFreeimagePnmFormat.Writerr   Tc                 C   rg   r   )r[   r   PNM_SAVE_ASCIIr   rT   r5   )r   r4   	use_asciir   r   r   r5     rj   zFreeimagePnmFormat.Writer._openNrk   rl   r   r   r   r   rT     rm   rT   Nrc   r   r   r   r   r   z  s    r   )rU   numpyr   corer   r   core.requestr   
_freeimager   r   r	   r
   r   rZ   re   r   r   r   r   r   r   <module>   s   r%Qm