o
    e59                     @   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__ ID:\Projects\ConvertPro\env\Lib\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filenameZ
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   
ValueErrorZ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   _bmZload_from_filenameget_local_filenamer   flagsr   r   r   _openU   s   zFreeimageFormat.Reader._openc                 C   s   | j   d S r   )r.   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.)
IndexErrorr.   Z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   )r5   r.   r6   r7   r   r   r   _get_meta_dataa   s   
z%FreeimageFormat.Reader._get_meta_dataNr   )__name__
__module____qualname__r+   r2   r4   r9   r:   r   r   r   r   ReaderQ   s    
r?   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)_flagsr.   _is_set_metar0   r   r   r   r2   i   s   
zFreeimageFormat.Writer._openc                 C   s.   | j | j | j | j  | j   d S r   )r.   Zset_meta_datarB   Zsave_to_filenamer&   r/   r3   r   r   r   r   r4   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   )rA   RuntimeErrorndimshaper.   r
   r,   r&   r#   r-   r   r@   allocateZset_image_datarB   r   immetar   r   r   _append_datav   s   

z#FreeimageFormat.Writer._append_datac                 C   s
   || _ d S r   )rB   )r   rJ   r   r   r   _set_meta_data   s   
z%FreeimageFormat.Writer._set_meta_dataNr;   )r<   r=   r>   r2   r4   rK   rL   r   r   r   r   Writerh   s
    
rM   )NN)r<   r=   r>   __doc__Z_modesr   propertyr   r'   r*   r   r?   rM   __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   ZBMP_SAVE_RLEZBMP_DEFAULTr   rM   r2   )r   r1   compressionr   r   r   r2      s
   
zFreeimageBmpFormat.Writer._openc                 C   s   t |dd}tj| ||S )N   Zbitdepth)r   r   rM   rK   rH   r   r   r   rK      s   z&FreeimageBmpFormat.Writer._append_dataN)r   Fr<   r=   r>   r2   rK   r   r   r   r   rM      s    

rM   Nr<   r=   r>   rN   r   rM   r   r   r   r   rR      s    rR   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   )rS   r   ZPNG_IGNOREGAMMAr   r?   r2   )r   r1   Zignoregammar   r   r   r2         
zFreeimagePngFormat.Reader._openNr   Tr<   r=   r>   r2   r   r   r   r   r?          r?   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      ra   z&Png compression must be 0, 1, 6, or 9.)r   ZPNG_Z_NO_COMPRESSIONZPNG_Z_BEST_SPEEDZPNG_Z_DEFAULT_COMPRESSIONZPNG_Z_BEST_COMPRESSIONrS   ZPNG_INTERLACEDKeyErrorr)   r   rM   r2   )r   r1   rT   quantizeZ
interlacedZcompression_mapr   r   r   r2      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 )NZuint16   rV   rU   rd   FrC   r   zCan only quantize RGB imagesr      z!PNG quantize param must be 2..256r   )strZdtyper   r   rM   rK   rS   r&   kwargsgetrE   rF   r)   r.   rd   r3   )r   rI   rJ   qZbmr   r   r   rK      s   

z&FreeimagePngFormat.Writer._append_dataN)r   ra   r   FrW   r   r   r   r   rM      s    
rM   Nr<   r=   r>   rN   r   r?   rM   r   r   r   r   rZ      s    rZ   c                   @   rY   )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   )rS   r   ZJPEG_EXIFROTATEZJPEG_ACCURATEr   r?   r2   )r   r1   
exifrotateZ	quickreadr   r   r   r2   4  s   

z FreeimageJpegFormat.Reader._openc                 C   s&   t j| |\}}| ||}||fS r   )r   r?   r9   _rotate)r   r8   rI   rJ   r   r   r   r9   >  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.
            rm   Nr   Z	EXIF_MAINZOrientation)r   r   )rC      )   rb   rC   )r   rU   )r   ro   rp   r   )r&   rh   ri   rc   npZrot90Zfliplr)r   rI   rJ   Zorir   r   r   rn   C  s$   

z"FreeimageJpegFormat.Reader._rotateN)r   TF)r<   r=   r>   r2   r9   rn   r   r   r   r   r?   3  s    

r?   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.)	rS   r)   r   ZJPEG_PROGRESSIVEZJPEG_OPTIMIZEZJPEG_BASELINEr   rM   r2   )r   r1   ZqualityZprogressiveoptimizeZbaseliner   r   r   r2   `  s   


z FreeimageJpegFormat.Writer._openc                 C   s<   |j dkr|jd dkrtdt|dd}tj| ||S )NrC   r   ro   z$JPEG does not support alpha channel.rU   rV   )rE   rF   IOErrorr   r   rM   rK   rH   r   r   r   rK   s  s   z'FreeimageJpegFormat.Writer._append_dataN)r   rr   FFFrW   r   r   r   r   rM   _  s    
rM   Nrk   r   r   r   r   rl     s    %,rl   c                   @   rQ   )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                   @   r[   )zFreeimagePnmFormat.Writerr   Tc                 C   r\   r   )rS   r   ZPNM_SAVE_ASCIIr   rM   r2   )r   r1   Z	use_asciir   r   r   r2     r]   zFreeimagePnmFormat.Writer._openNr^   r_   r   r   r   r   rM     r`   rM   NrX   r   r   r   r   rv   z  s    rv   )rN   numpyrq   corer   r   Zcore.requestr   Z
_freeimager   r   r	   r
   r   rR   rZ   rl   rv   r   r   r   r   <module>   s   r%Qm