B
    KpŽ[÷E  ã            %   @   s  d Z ddlmZmZmZ ddlZddlmZ ddl	m
Z
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eeeddddœZddiZddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8g%Zd9d:„ Zd;d<„ Zeƒ  dS )=z¾ Plugin that wraps the freeimage lib. The wrapper for Freeimage is
part of the core of imageio, but it's functionality is exposed via
the plugin system (therefore this plugin is very thin).
é    )Úabsolute_importÚprint_functionÚdivisionNé   )Úformats)ÚFormatÚimage_as_uinté   )ÚfiÚdownloadÚIO_FLAGSÚFNAME_PER_PLATFORMc               @   sT   e Zd ZdZ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dS )ÚFreeimageFormata{   This is the default format used for FreeImage. Each Freeimage
    format has the 'flags' keyword argument. See the Freeimage
    documentation for more information.

    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
    ----------------------
    flags : int
        A freeimage-specific option. In most cases we provide explicit
        parameters for influencing image reading.
    
    Parameters for saving
    ----------------------
    flags : int
        A freeimage-specific option. In most cases we provide explicit
        parameters for influencing image saving.
    Úic             C   s   | j S )N)Ú_fif)Úself© r   ú8lib/python3.7/site-packages/imageio/plugins/freeimage.pyÚfif0   s    zFreeimageFormat.fifc             C   sZ   t  ¡ rVt|dƒsFyt  |jd|j¡|_W n tk
rD   d|_Y nX |j| jkrVdS d S )Nr   ÚréÿÿÿÿT)	r
   Úhas_libÚhasattrÚgetFIFÚfilenameZ
firstbytesr   Ú	Exceptionr   )r   Úrequestr   r   r   Ú	_can_read4   s    
zFreeimageFormat._can_readc             C   sV   t  ¡ rRt|dƒsByt  |jd¡|_W n tk
r@   d|_Y nX |j| jkrRdS d S )Nr   Úwr   T)r
   r   r   r   r   r   r   r   )r   r   r   r   r   Ú
_can_write?   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_lengthM   s    z"FreeimageFormat.Reader._get_lengthr   c             C   s.   t  | jj| jj|¡| _| j | j ¡ ¡ d S )N)	r
   Úcreate_bitmapr   r   Úformatr   Ú_bmZload_from_filenameÚget_local_filename)r   Úflagsr   r   r   Ú_openP   s    zFreeimageFormat.Reader._openc             C   s   | j  ¡  d S )N)r#   Úclose)r   r   r   r   Ú_closeT   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_dataW   s    z FreeimageFormat.Reader._get_datac             C   s    |d ks|dkst ƒ ‚| j ¡ S )Nr   )r)   r#   r*   )r   r+   r   r   r   Ú_get_meta_data\   s    z%FreeimageFormat.Reader._get_meta_dataN)r   )Ú__name__Ú
__module__Ú__qualname__r    r&   r(   r,   r-   r   r   r   r   ÚReaderL   s
   
r1   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Ú_meta)r   r%   r   r   r   r&   d   s    zFreeimageFormat.Writer._openc             C   s.   | j  | j¡ | j  | j ¡ ¡ | j  ¡  d S )N)r#   Zset_meta_datar4   Zsave_to_filenamer   r$   r'   )r   r   r   r   r(   j   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   )r3   ÚRuntimeErrorÚndimÚshaper#   r
   r!   r   r   r"   r   r2   ÚallocateZset_image_datar4   )r   ÚimÚmetar   r   r   Ú_append_dataq   s    
z#FreeimageFormat.Writer._append_datac             C   s
   || _ d S )N)r4   )r   r;   r   r   r   Ú_set_meta_dataˆ   s    z%FreeimageFormat.Writer._set_meta_dataN)r   )r.   r/   r0   r&   r(   r<   r=   r   r   r   r   ÚWriterc   s   
r>   N)r.   r/   r0   Ú__doc__Ú_modesÚpropertyr   r   r   r   r1   r>   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 )N)Úintr   ZBMP_SAVE_RLEZBMP_DEFAULTr   r>   r&   )r   r%   Úcompressionr   r   r   r&   ¨   s
    
zFreeimageBmpFormat.Writer._openc             C   s   t |dd}tj | ||¡S )Né   )Úbitdepth)r   r   r>   r<   )r   r:   r;   r   r   r   r<   ²   s    z&FreeimageBmpFormat.Writer._append_dataN)r   F)r.   r/   r0   r&   r<   r   r   r   r   r>   §   s   

r>   N)r.   r/   r0   r?   r   r>   r   r   r   r   rB   ’   s   rB   c               @   s4   e Zd ZdZG dd„ dejƒZG dd„ dejƒZdS )ÚFreeimagePngFormataf   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 )N)rC   r   ZPNG_IGNOREGAMMAr   r1   r&   )r   r%   Zignoregammar   r   r   r&   ×   s    
zFreeimagePngFormat.Reader._openN)r   T)r.   r/   r0   r&   r   r   r   r   r1   Ö   s   r1   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 }y||| O }W n tk
rX   tdƒ‚Y nX t	j
 | |¡S )N)r   r	   é   rH   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_COMPRESSIONrC   ZPNG_INTERLACEDÚKeyErrorÚ
ValueErrorr   r>   r&   )r   r%   rD   ÚquantizeZ
interlacedZcompression_mapr   r   r   r&   â   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rŽtdƒ‚n| j d|¡}| j ¡  || _d S )NZuint16é   )rF   rE   rL   Fr5   r   zCan only quantize RGB imagesr   é   z!PNG quantize param must be 2..256r   )ÚstrZdtyper   r   r>   r<   rC   r   ÚkwargsÚgetr7   r8   rK   r#   rL   r'   )r   r:   r;   ÚqZbmr   r   r   r<   ô   s    


z&FreeimagePngFormat.Writer._append_dataN)r   rH   r   F)r.   r/   r0   r&   r<   r   r   r   r   r>   á   s   
r>   N)r.   r/   r0   r?   r   r1   r>   r   r   r   r   rG   ·   s   rG   c               @   s4   e Zd ZdZG dd„ dejƒZG dd„ dejƒZdS )ÚFreeimageJpegFormataS   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   )rC   r   ZJPEG_EXIFROTATEZJPEG_ACCURATEr   r1   r&   )r   r%   Ú
exifrotateZ	quickreadr   r   r   r&   /  s    

z FreeimageJpegFormat.Reader._openc             C   s&   t j | |¡\}}|  ||¡}||fS )N)r   r1   r,   Ú_rotate)r   r+   r:   r;   r   r   r   r,   9  s    z$FreeimageJpegFormat.Reader._get_datac             C   s’   | j j dd¡dkrŽy|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rŽt |¡}|S )aP   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.
            rT   Nr   Z	EXIF_MAINZOrientation)r	   r   )r5   é   )é   rI   r5   )é   rE   )r   rV   rW   rX   )r   rP   rQ   rJ   ÚnpZrot90Zfliplr)r   r:   r;   Zorir   r   r   rU   >  s    

z"FreeimageJpegFormat.Reader._rotateN)r   TF)r.   r/   r0   r&   r,   rU   r   r   r   r   r1   .  s   

r1   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.)	rC   rK   r   ZJPEG_PROGRESSIVEZJPEG_OPTIMIZEZJPEG_BASELINEr   r>   r&   )r   r%   ZqualityZprogressiveÚoptimizeZbaseliner   r   r   r&   [  s    


z FreeimageJpegFormat.Writer._openc             C   s<   |j dkr |jd dkr tdƒ‚t|dd}tj | ||¡S )Nr5   r   rV   z$JPEG does not support alpha channel.rE   )rF   )r7   r8   ÚIOErrorr   r   r>   r<   )r   r:   r;   r   r   r   r<   n  s    z'FreeimageJpegFormat.Writer._append_dataN)r   rZ   FFF)r.   r/   r0   r&   r<   r   r   r   r   r>   Z  s   
r>   N)r.   r/   r0   r?   r   r1   r>   r   r   r   r   rS     s   $,rS   )ZjpegÚpngÚbmpÚgifÚicoÚmngÚTIFFZFI_TIFF)ZBMPr   zWindows or OS/2 Bitmapr_   )ZCUTé   zDr. HaloZcut)ZDDSé   zDirectX SurfaceZdds)ZEXRé   zILM OpenEXRZexr)ZG3é   zRaw fax format CCITT G.3Zg3)ZGIFé   zGraphics Interchange Formatr`   )ZHDRé   zHigh Dynamic Range ImageZhdr)ZICOr	   zWindows Iconra   )ZIFFrW   zIFF Interleaved Bitmapziff,lbm)ZJ2Ké   zJPEG-2000 codestreamzj2k,j2c)ZJNGr5   zJPEG Network GraphicsZjng)ZJP2é   zJPEG-2000 File FormatZjp2)ZJPEGr   zJPEG - JFIF Compliantzjpg,jif,jpeg,jpe)zJPEG-XRé$   zJPEG XR image formatzjxr,wdp,hdp)ZKOALArV   zC64 Koala GraphicsZkoa)ZMNGrI   zMultiple-image Network Graphicsrb   )ZPBMrX   zPortable Bitmap (ASCII)Úpbm)ZPBMRAWrE   zPortable Bitmap (RAW)rm   )ZPCDrH   zKodak PhotoCDZpcd)ZPCXé
   zZsoft PaintbrushZpcx)ZPFMé    zPortable floatmapZpfm)ZPGMé   zPortable Greymap (ASCII)Úpgm)ZPGMRAWé   zPortable Greymap (RAW)rq   )ZPICTé!   zMacintosh PICTzpct,pict,pic)ZPNGé   zPortable Network Graphicsr^   )ZPPMé   zPortable Pixelmap (ASCII)Úppm)ZPPMRAWé   zPortable Pixelmap (RAW)rv   )ZPSDé   zAdobe PhotoshopZpsd)ZRASrM   zSun Raster ImageZras)ZRAWé"   zRAW camera imagez§3fr,arw,bay,bmq,cap,cine,cr2,crw,cs1,dc2,dcr,drf,dsc,dng,erf,fff,ia,iiq,k25,kc2,kdc,mdc,mef,mos,mrw,nef,nrw,orf,pef,ptx,pxn,qtk,raf,raw,rdc,rw2,rwl,rwz,sr2,srf,srw,sti)ZSGIé   zSGI Image Formatzsgi,rgb,rgba,bw)ZTARGAé   zTruevision Targaz	tga,targa)rc   é   zTagged Image File Formatztif,tiff)ZWBMPé   zWireless Bitmapzwap,wbmp,wbm)ZWebPé#   zGoogle WebP image formatZwebp)ZXBMé   zX11 Bitmap FormatZxbm)ZXPMé   zX11 Pixmap FormatZxpmc              C   sR   xLt D ]D\} }}}t |  ¡ t¡}|r|| d |||jƒ}||_t |¡ qW d S )Nz-FI)	Ú	fiformatsÚSPECIAL_CLASSESrQ   Úlowerr   r@   r   r   Ú
add_format)Únamer   ÚdesÚextÚFormatClassr"   r   r   r   Ú$_create_predefined_freeimage_formats¶  s    r‰   c              C   sÂ   g t dd…< tdkrdS tj} xžt|  ¡ ƒD ]Ž}|  |¡r,|  |¡ d¡}|  |¡ d¡}|  	|¡ d¡}t  
||||f¡ t | ¡ t¡}|s’q,||d |||jƒ}||_tj|dd q,W dS )zÄ By default, imageio registers a list of predefined formats
    that freeimage can handle. If your version of imageio can handle
    more formats, you can call this function to register them.
    NÚasciiz-FIT)Z	overwrite)r   r
   Z_libÚrangeZFreeImage_GetFIFCountZFreeImage_IsPluginEnabledZFreeImage_GetFormatFromFIFÚdecodeZFreeImage_GetFIFDescriptionZFreeImage_GetFIFExtensionListÚappendr‚   rQ   rƒ   r   r@   r   r   r„   )Úlibr   r…   r†   r‡   rˆ   r"   r   r   r   Úcreate_freeimage_formatsÃ  s     
r   )r?   Z
__future__r   r   r   ZnumpyrY   Ú r   Zcorer   r   Z
_freeimager
   r   r   r   r   rB   rG   rS   r‚   ZNAME_MAPr   r‰   r   r   r   r   r   Ú<module>   sp   }%Qp!