U
    Z$d59                     @   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__ =/tmp/pip-unpacked-wheel-zwww1ayp/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   sr   t  rnt|dsFzt |jd|j|_W n tk
rD   d|_Y nX |j| jkrVdS |jdkrn| jdkrnd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   sj   t  rft|dsVzt |jd|_W n. tk
rT   |jtkrJ| j	|_nd|_Y nX |j| j	krfd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 k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_datarA   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rD|jd dkrD|d d d d df }| jd krtt| jj| j	j
| j| _| j| | j| || _d S )NTz1Singleton image; can only append image data once.   r   r   r   )r@   RuntimeErrorndimshaper.   r
   r,   r&   r#   r-   r   r?   allocateZset_image_datarA   r   immetar   r   r   _append_datav   s     
  z#FreeimageFormat.Writer._append_datac                 C   s
   || _ d S r   )rA   )r   rI   r   r   r   _set_meta_data   s    z%FreeimageFormat.Writer._set_meta_dataN)r   )r;   r<   r=   r2   r4   rJ   rK   r   r   r   r   Writerh   s   
rL   )NN)r;   r<   r=   __doc__Z_modesr   propertyr   r'   r*   r   r>   rL   __classcell__r   r   r   r   r   %   s   
r   c                   @   s"   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   rL   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   rL   rJ   rG   r   r   r   rJ      s    z&FreeimageBmpFormat.Writer._append_dataN)r   Fr;   r<   r=   r2   rJ   r   r   r   r   rL      s   

rL   Nr;   r<   r=   rM   r   rL   r   r   r   r   rP      s   rP   c                   @   s4   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                   @   s   e Zd ZdddZdS )zFreeimagePngFormat.Readerr   Tc                 C   s$   t |}|r|tjO }tj| |S r   )rQ   r   ZPNG_IGNOREGAMMAr   r>   r2   )r   r1   Zignoregammar   r   r   r2      s    
zFreeimagePngFormat.Reader._openN)r   Tr;   r<   r=   r2   r   r   r   r   r>      s   r>   c                   @   s   e Zd Zd	ddZdd ZdS )
zFreeimagePngFormat.Writerr   	   Fc                 C   sh   t jt jt jt jd}t|}|r,|t jO }z||| O }W n tk
rX   tdY nX t	j
| |S )N)r   r      rY   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_COMPRESSIONrQ   ZPNG_INTERLACEDKeyErrorr)   r   rL   r2   )r   r1   rR   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}|sRnZ|j
dkrj|jd dksttd	n8|d
k s|dkrtdn| jd|}| j  || _d S )NZuint16   rT   rS   r\   FrB   r   zCan only quantize RGB imagesr      z!PNG quantize param must be 2..256r   )strZdtyper   r   rL   rJ   rQ   r&   kwargsgetrD   rE   r)   r.   r\   r3   )r   rH   rI   qZbmr   r   r   rJ      s    


z&FreeimagePngFormat.Writer._append_dataN)r   rY   r   FrU   r   r   r   r   rL      s   
rL   Nr;   r<   r=   rM   r   r>   rL   r   r   r   r   rW      s   rW   c                   @   s4   e Zd ZdZG dd dejZG d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                   @   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   )rQ   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   rH   rI   r   r   r   r9   >  s    z$FreeimageJpegFormat.Reader._get_datac                 C   s   | j jdddkrz|d d }W n tk
r8   Y nVX |dkrB|dkrVt|d}|dkrjt|d	}|d
kr|t|}|dkrt|}|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.
            re   Nr   Z	EXIF_MAINZOrientation)r   r   )rB      )   rZ   rB   )r   rS   )r   rg   rh   r   )r&   r`   ra   r[   npZrot90Zfliplr)r   rH   rI   Zorir   r   r   rf   C  s    

z"FreeimageJpegFormat.Reader._rotateN)r   TF)r;   r<   r=   r2   r9   rf   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 }|rL|tjO }|rZ|tjO }tj| |S )Nr   d   z)JPEG quality should be between 1 and 100.)	rQ   r)   r   ZJPEG_PROGRESSIVEZJPEG_OPTIMIZEZJPEG_BASELINEr   rL   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 )NrB   r   rg   z$JPEG does not support alpha channel.rS   rT   )rD   rE   IOErrorr   r   rL   rJ   rG   r   r   r   rJ   s  s    z'FreeimageJpegFormat.Writer._append_dataN)r   rj   FFFrU   r   r   r   r   rL   _  s            
rL   Nrc   r   r   r   r   rd     s   %,rd   c                   @   s"   e Zd ZdZG d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                   @   s   e Zd ZdddZdS )zFreeimagePnmFormat.Writerr   Tc                 C   s$   t |}|r|tjO }tj| |S r   )rQ   r   ZPNM_SAVE_ASCIIr   rL   r2   )r   r1   Z	use_asciir   r   r   r2     s    
zFreeimagePnmFormat.Writer._openN)r   TrX   r   r   r   r   rL     s   rL   NrV   r   r   r   r   rn   z  s   rn   )rM   Znumpyri   corer   r   Zcore.requestr   Z
_freeimager   r   r	   r
   r   rP   rW   rd   rn   r   r   r   r   <module>   s   r%Qm