
;c]c        (   @` 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 d	 e
 f d
     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ i e d 6e d 6e d 6e d 6e d 6d d 6d d 6d d 6Z i d d 6Z 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 d d d d d d d d d g% Z d   Z d   Z e   d S(   s    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).
i    (   t   absolute_importt   print_functiont   divisionNi   (   t   formats(   t   Formatt   image_as_uinti   (   t   fit   downloadt   IO_FLAGSt   FNAME_PER_PLATFORMt   FreeimageFormatc           B` sg   e  Z d  Z d Z e d    Z d   Z d   Z d e j	 f d     YZ	 d e j
 f d     YZ
 RS(	   ss   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.
    t   ic         C` s   |  j  S(   N(   t   _fif(   t   self(    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyt   fif0   s    c         C` sz   t  j   rv t | d  s] y" t  j | j d | j  | _ Wq] t k
 rY d | _ q] Xn  | j |  j k rv t	 Sn  d  S(   NR   t   ri(
   R   t   has_libt   hasattrt   getFIFt   filenamet
   firstbytesR   t	   ExceptionR   t   True(   R   t   request(    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyt	   _can_read4   s    "c         C` st   t  j   rp t | d  sW y t  j | j d  | _ WqW t k
 rS d | _ qW Xn  | j |  j k rp t Sn  d  S(   NR   t   wi(	   R   R   R   R   R   R   R   R   R   (   R   R   (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyt
   _can_write?   s    t   Readerc           B` s8   e  Z d    Z d d  Z d   Z d   Z d   Z RS(   c         C` s   d S(   Ni   (    (   R   (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyt   _get_lengthM   s    i    c         C` sA   t  j |  j j |  j j |  |  _ |  j j |  j j    d  S(   N(	   R   t   create_bitmapR   R   t   formatR   t   _bmt   load_from_filenamet   get_local_filename(   R   t   flags(    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyt   _openP   s    $c         C` s   |  j  j   d  S(   N(   R   t   close(   R   (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyt   _closeT   s    c         C` s7   | d k r t  d   n  |  j j   |  j j   f S(   Ni    s+   This format only supports singleton images.(   t
   IndexErrorR   t   get_image_datat   get_meta_data(   R   t   index(    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyt	   _get_dataW   s    c         C` s1   | d  k p | d k s$ t    n  |  j j   S(   Ni    (   t   NoneR&   R   R(   (   R   R)   (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyt   _get_meta_data\   s    (   t   __name__t
   __module__R   R#   R%   R*   R,   (    (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR   L   s
   			t   Writerc           B` s/   e  Z d  d  Z d   Z d   Z d   Z RS(   i    c         C` s(   | |  _  d  |  _ t |  _ i  |  _ d  S(   N(   t   _flagsR+   R   t   Falset   _is_sett   _meta(   R   R"   (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR#   d   s    			c         C` s=   |  j  j |  j  |  j  j |  j j    |  j  j   d  S(   N(   R   t   set_meta_dataR3   t   save_to_filenameR   R!   R$   (   R   (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR%   j   s    c         C` s   |  j  s t |  _  n t d   | j d k re | j d d k re | d  d   d  d   d f } n  |  j d  k r t j |  j	 j
 |  j j |  j  |  _ |  j j |  n  |  j j |  | |  _ d  S(   Ns1   Singleton image; can only append image data once.i   ii   i    (   R2   R   t   RuntimeErrort   ndimt   shapeR   R+   R   R   R   R   R   R   R0   t   allocatet   set_image_dataR3   (   R   t   imt   meta(    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyt   _append_dataq   s    		""!c         C` s   | |  _  d  S(   N(   R3   (   R   R<   (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyt   _set_meta_data   s    (   R-   R.   R#   R%   R=   R>   (    (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR/   c   s   		(   R-   R.   t   __doc__t   _modest   propertyR   R   R   R   R   R/   (    (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR
      s   		t   FreeimageBmpFormatc           B` s'   e  Z d  Z d e j f d     YZ RS(   s   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.

    R/   c           B` s    e  Z d  e d  Z d   Z RS(   i    c         C` sB   t  |  } | r" | t j O} n | t j O} t j j |  |  S(   N(   t   intR   t   BMP_SAVE_RLEt   BMP_DEFAULTR
   R/   R#   (   R   R"   t   compression(    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR#      s
    c         C` s(   t  | d d } t j j |  | |  S(   Nt   bitdepthi   (   R   R
   R/   R=   (   R   R;   R<   (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR=      s    (   R-   R.   R1   R#   R=   (    (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR/      s   
(   R-   R.   R?   R
   R/   (    (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyRB      s   t   FreeimagePngFormatc           B` s@   e  Z d  Z d e j f d     YZ d e j f d     YZ RS(   sZ   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.
    R   c           B` s   e  Z d  e d  Z RS(   i    c         C` s5   t  |  } | r" | t j O} n  t j j |  |  S(   N(   RC   R   t   PNG_IGNOREGAMMAR
   R   R#   (   R   R"   t   ignoregamma(    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR#      s    (   R-   R.   R   R#   (    (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR      s   R/   c           B` s&   e  Z d  d d  e d  Z d   Z RS(   i    i	   c         C` s   i t  j d 6t  j d 6t  j d 6t  j d 6} t |  } | rP | t  j O} n  y | | | O} Wn t k
 r t d   n Xt	 j
 j |  |  S(   Ni    i   i   i	   s&   Png compression must be 0, 1, 6, or 9.(   R   t   PNG_Z_NO_COMPRESSIONt   PNG_Z_BEST_SPEEDt   PNG_Z_DEFAULT_COMPRESSIONt   PNG_Z_BEST_COMPRESSIONRC   t   PNG_INTERLACEDt   KeyErrort
   ValueErrorR
   R/   R#   (   R   R"   RF   t   quantizet
   interlacedt   compression_map(    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR#      s    


c         C` s   t  | j  d k r* t | d d } n t | d d } t j j |  | |  t |  j j j	 d t
   } | sy n | j d k o | j d d k s t d   nR | d	 k  s | d
 k r t d   n+ |  j j d |  } |  j j   | |  _ d  S(   Nt   uint16RG   i   i   RR   i   is   Can only quantize RGB imagesi   i   s!   PNG quantize param must be 2..256i    (   t   strt   dtypeR   R
   R/   R=   RC   R   t   kwargst   getR1   R7   R8   RQ   R   RR   R$   (   R   R;   R<   t   qt   bm(    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR=      s    "(   R-   R.   R1   R#   R=   (    (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR/      s   (   R-   R.   R?   R
   R   R/   (    (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyRH      s   t   FreeimageJpegFormatc           B` s@   e  Z d  Z d e j f d     YZ d e j f d     YZ RS(   sB   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.

    R   c           B` s,   e  Z d  e e d  Z d   Z d   Z RS(   i    c         C` sW   t  |  } | r. | d k r. | t j O} n  | sD | t j O} n  t j j |  |  S(   Ni   (   RC   R   t   JPEG_EXIFROTATEt   JPEG_ACCURATER
   R   R#   (   R   R"   t
   exifrotatet	   quickread(    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR#   /  s    c         C` s7   t  j j |  |  \ } } |  j | |  } | | f S(   N(   R
   R   R*   t   _rotate(   R   R)   R;   R<   (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR*   9  s    c         C` s   |  j  j j d d  d k r y | d d } Wn t k
 rC q X| d k rS n  | d k rt t j | d  } n  | d k r t j | d  } n  | d k r t j |  } n  | d k r t j |  } q n  | S(   sO   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_   i   t	   EXIF_MAINt   Orientationi   i   i   i   i   i   i   N(   i   i   (   i   i   (   i   i   (   i   i   (   i   i   i   i   (   R   RX   RY   R+   RP   t   npt   rot90t   fliplr(   R   R;   R<   t   ori(    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyRa   >  s     (   R-   R.   R   R1   R#   R*   Ra   (    (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR   .  s   
	R/   c           B` s)   e  Z d  d e e e d  Z d   Z RS(   i    iK   c         C` s   t  |  } | d k  s$ | d k r3 t d   n  t  |  } | | O} | r_ | t j O} n  | ru | t j O} n  | r | t j O} n  t j j |  |  S(   Ni   id   s)   JPEG quality should be between 1 and 100.(	   RC   RQ   R   t   JPEG_PROGRESSIVEt   JPEG_OPTIMIZEt   JPEG_BASELINER
   R/   R#   (   R   R"   t   qualityt   progressivet   optimizet   baseline(    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR#   [  s    
c         C` sY   | j  d k r1 | j d d k r1 t d   n  t | d d } t j j |  | |  S(   Ni   ii   s$   JPEG does not support alpha channel.RG   i   (   R7   R8   t   IOErrorR   R
   R/   R=   (   R   R;   R<   (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR=   n  s    "(   R-   R.   R1   R#   R=   (    (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR/   Z  s   (   R-   R.   R?   R
   R   R/   (    (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR\     s   $,t   FreeimagePnmFormatc           B` s'   e  Z d  Z d e j f d     YZ RS(   sm   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.
    R/   c           B` s   e  Z d  e d  Z RS(   i    c         C` s5   t  |  } | r" | t j O} n  t j j |  |  S(   N(   RC   R   t   PNM_SAVE_ASCIIR
   R/   R#   (   R   R"   t	   use_ascii(    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR#     s    (   R-   R.   R   R#   (    (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyR/     s   (   R-   R.   R?   R
   R/   (    (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyRp   u  s   t   jpegt   pngt   bmpt   ppmt   ppmrawt   gift   icot   mngt   FI_TIFFt   TIFFt   BMPs   Windows or OS/2 Bitmapt   CUTi   s   Dr. Halot   cutt   DDSi   s   DirectX Surfacet   ddst   EXRi   s   ILM OpenEXRt   exrt   G3i   s   Raw fax format CCITT G.3t   g3t   GIFi   s   Graphics Interchange Formatt   HDRi   s   High Dynamic Range Imaget   hdrt   ICOs   Windows Icont   IFFi   s   IFF Interleaved Bitmaps   iff,lbmt   J2Ki   s   JPEG-2000 codestreams   j2k,j2ct   JNGi   s   JPEG Network Graphicst   jngt   JP2i   s   JPEG-2000 File Formatt   jp2t   JPEGs   JPEG - JFIF Compliants   jpg,jif,jpeg,jpes   JPEG-XRi$   s   JPEG XR image formats   jxr,wdp,hdpt   KOALAi   s   C64 Koala Graphicst   koat   MNGi   s   Multiple-image Network Graphicst   PBMi   s   Portable Bitmap (ASCII)t   pbmt   PBMRAWi   s   Portable Bitmap (RAW)t   PCDi	   s   Kodak PhotoCDt   pcdt   PCXi
   s   Zsoft Paintbrusht   pcxt   PFMi    s   Portable floatmapt   pfmt   PGMi   s   Portable Greymap (ASCII)t   pgmt   PGMRAWi   s   Portable Greymap (RAW)t   PICTi!   s   Macintosh PICTs   pct,pict,pict   PNGi   s   Portable Network Graphicst   PPMi   s   Portable Pixelmap (ASCII)t   PPMRAWi   s   Portable Pixelmap (RAW)t   PSDi   s   Adobe Photoshopt   psdt   RASi   s   Sun Raster Imaget   rast   RAWi"   s   RAW camera images   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,stit   SGIi   s   SGI Image Formats   sgi,rgb,rgba,bwt   TARGAi   s   Truevision Targas	   tga,targai   s   Tagged Image File Formats   tif,tifft   WBMPi   s   Wireless Bitmaps   wap,wbmp,wbmt   WebPi#   s   Google WebP image formatt   webpt   XBMi   s   X11 Bitmap Formatt   xbmt   XPMi   s   X11 Pixmap Formatt   xpmc          C` st   xm t  D]e \ }  } } } t j |  j   t  } | r | |  d | | | j  } | | _ t j |  q q Wd  S(   Ns   -FI(	   t	   fiformatst   SPECIAL_CLASSESRY   t   lowerR
   R@   R   R   t
   add_format(   t   nameR   t   dest   extt   FormatClassR   (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyt$   _create_predefined_freeimage_formats  s    	c          C` s  g  t  (t d k r d St j }  x t |  j    D] } |  j |  r3 |  j |  j d  } |  j	 |  j d  } |  j
 |  j d  } t  j | | | | f  t j | j   t  } | s q3 n  | | d | | | j  } | | _ t j | d t q3 q3 Wd S(   s    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.
    Nt   asciis   -FIt	   overwrite(   R   R   R+   t   _libt   ranget   FreeImage_GetFIFCountt   FreeImage_IsPluginEnabledt   FreeImage_GetFormatFromFIFt   decodet   FreeImage_GetFIFDescriptiont   FreeImage_GetFIFExtensionListt   appendR   RY   R   R
   R@   R   R   R   R   (   t   libR   R   R   R   R   R   (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyt   create_freeimage_formats  s     		(   R}   i    s   Windows or OS/2 BitmapRu   (   R~   i   s   Dr. HaloR   (   R   i   s   DirectX SurfaceR   (   R   i   s   ILM OpenEXRR   (   R   i   s   Raw fax format CCITT G.3R   (   R   i   s   Graphics Interchange FormatRx   (   R   i   s   High Dynamic Range ImageR   (   R   i   s   Windows IconRy   (   R   i   s   IFF Interleaved Bitmaps   iff,lbm(   R   i   s   JPEG-2000 codestreams   j2k,j2c(   R   i   s   JPEG Network GraphicsR   (   R   i   s   JPEG-2000 File FormatR   (   R   i   s   JPEG - JFIF Compliants   jpg,jif,jpeg,jpe(   s   JPEG-XRi$   s   JPEG XR image formats   jxr,wdp,hdp(   R   i   s   C64 Koala GraphicsR   (   R   i   s   Multiple-image Network GraphicsRz   (   R   i   s   Portable Bitmap (ASCII)R   (   R   i   s   Portable Bitmap (RAW)R   (   R   i	   s   Kodak PhotoCDR   (   R   i
   s   Zsoft PaintbrushR   (   R   i    s   Portable floatmapR   (   R   i   s   Portable Greymap (ASCII)R   (   R   i   s   Portable Greymap (RAW)R   (   R   i!   s   Macintosh PICTs   pct,pict,pic(   R   i   s   Portable Network GraphicsRt   (   R   i   s   Portable Pixelmap (ASCII)Rv   (   R   i   s   Portable Pixelmap (RAW)Rv   (   R   i   s   Adobe PhotoshopR   (   R   i   s   Sun Raster ImageR   (   R   i"   s   RAW camera images   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(   R   i   s   SGI Image Formats   sgi,rgb,rgba,bw(   R   i   s   Truevision Targas	   tga,targa(   R|   i   s   Tagged Image File Formats   tif,tiff(   R   i   s   Wireless Bitmaps   wap,wbmp,wbm(   R   i#   s   Google WebP image formatR   (   R   i   s   X11 Bitmap FormatR   (   R   i   s   X11 Pixmap FormatR   (   R?   t
   __future__R    R   R   t   numpyRd   t    R   t   coreR   R   t
   _freeimageR   R   R   R	   R
   RB   RH   R\   Rp   R+   R   t   NAME_MAPR   R   R   (    (    (    s8   lib/python2.7/site-packages/imageio/plugins/freeimage.pyt   <module>   s~   "}%Qm
   			!