
;c]c           @ s  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z e	 j e  Z e Z i d d 6d d 6d	 d
 6d d 6d d 6d d 6Z e e d  Z d   Z d   Z e
 j  d d d d e
 j! 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( d! e# f d"     YZ) d# e) f d$     YZ* d% e) f d&     YZ+ e(   Z, d S('   s    Module imageio/freeimage.py

This module contains the wrapper code for the freeimage library.
The functions defined in this module are relatively thin; just thin
enough so that arguments and results are native Python/numpy data
types.

i    (   t   absolute_importt   print_functiont   with_statementNi   (
   t   get_remote_filet   load_libt   Dictt   resource_dirst   string_typest   binary_typet   IS_PYPYt   get_platformt   InternetNotAllowedErrort   NeedDownloadErrors!   libfreeimage-3.16.0-osx10.6.dylibt   osx32t   osx64s   FreeImage-3.15.4-win32.dllt   win32s   FreeImage-3.15.1-win64.dllt   win64s   libfreeimage-3.16.0-linux32.sot   linux32s   libfreeimage-3.16.0-linux64.sot   linux64c         C sR   t    } | rN | t k rN d t | } t d | d |  d |  d t _ n  d S(   s   Download the FreeImage library to your computer.

    Parameters
    ----------
    directory : str | None
        The directory where the file will be cached if a download was
        required to obtain the file. By default, the appdata directory
        is used. This is also the first directory that is checked for
        a local version of the file.
    force_download : bool | str
        If True, the file will be downloaded even if a local copy exists
        (and this copy will be overwritten). Can also be a YYYY-MM-DD date
        to ensure a file is up-to-date (modified date of a file on disk,
        if present, is checked).
    s
   freeimage/t   fnamet	   directoryt   force_downloadN(   R
   t   FNAME_PER_PLATFORMR   t   Nonet   fit   _lib(   R   R   t   platR   (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   download3   s
    	c          C s   t  j d d  }  |  r |  St   } | r | t k r y t d t | d t SWq t k
 rf q t k
 r t d   q t	 k
 r } t
 j t |   q Xn  d S(   s=    Ensure we have our version of the binary freeimage lib.
    t   IMAGEIO_FREEIMAGE_LIBs
   freeimage/t   autos   Need FreeImage library. You can obtain it with either:
  - download using the command: imageio_download_bin freeimage
  - download by calling (in Python): imageio.plugins.freeimage.download()
N(   t   ost   getenvR   R
   R   R   t   FalseR   R   t   RuntimeErrort   loggert   warningt   str(   t   libR   t   e(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   get_freeimage_libJ   s    	c         C s   |  j  t j    S(   N(   t   encodet   syst   getfilesystemencoding(   t   x(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   <lambda>h   t    i   i t   dtypet   FI_TYPESc           B s  e  Z d  Z d Z d Z d Z d Z d Z d Z d Z	 d Z
 d	 Z d
 Z d Z d Z i e j e 6e j e 6e j e 6e j e 6e j e 6e j e 6e j e	 6e j e
 6e j e 6e j e 6e j e 6e j e 6Z i e e j d f 6e e j d f 6e e j d f 6e e j d f 6e e j d f 6e e j d f 6e e j d f 6e e j d f 6e	 e j d f 6e
 e j d f 6e e j d f 6e e j d f 6e e j d f 6e e j d f 6Z i g  e 6g  e 6g  e 6g  e 6g  e 6g  e	 6g  e
 6d g e 6d g e 6d g e 6d g e 6Z RS(   i    i   i   i   i   i   i   i   i   i	   i
   i   i   (   t   __name__t
   __module__t   FIT_UNKNOWNt
   FIT_BITMAPt
   FIT_UINT16t	   FIT_INT16t
   FIT_UINT32t	   FIT_INT32t	   FIT_FLOATt
   FIT_DOUBLEt   FIT_COMPLEXt	   FIT_RGB16t
   FIT_RGBA16t   FIT_RGBFt	   FIT_RGBAFt   numpyt   uint8t   uint16t   int16t   uint32t   int32t   float32t   float64t
   complex128t   dtypest   fi_typest
   extra_dims(    (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR/   n   sj   













t   IO_FLAGSc           B s  e  Z d  Z d Z d Z d Z d Z d Z d Z d Z	 d Z
 d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d	 Z d
 Z d Z  d Z! d Z" d Z# d Z$ d Z% d  Z& d Z' d Z( d Z) d Z* d Z+ d Z, d Z- d Z. d Z/ d Z0 d Z1 d Z2 d Z3 d Z4 d Z5 d Z6 d Z7 d Z8 d
 Z9 d Z: d Z; d Z< d Z= d Z> d Z? d Z@ d ZA d ZB d ZC d ZD d ZE d ZF d ZG d ZH d ZI d ZJ d
 ZK d ZL d ZM d ZN d ZO d ZP d ZQ d  ZR d ZS d ZT d ZU d ZV RS(   i   i    i   i   i   i   i   i    i@   i   i   i   i   i   i    i   i @  i   i   i   i   i   i	   (W   R0   R1   t   FIF_LOAD_NOPIXELSt   BMP_DEFAULTt   BMP_SAVE_RLEt   CUT_DEFAULTt   DDS_DEFAULTt   EXR_DEFAULTt	   EXR_FLOATt   EXR_NONEt   EXR_ZIPt   EXR_PIZt	   EXR_PXR24t   EXR_B44t   EXR_LCt   FAXG3_DEFAULTt   GIF_DEFAULTt   GIF_LOAD256t   GIF_PLAYBACKt   HDR_DEFAULTt   ICO_DEFAULTt   ICO_MAKEALPHAt   IFF_DEFAULTt   J2K_DEFAULTt   JP2_DEFAULTt   JPEG_DEFAULTt	   JPEG_FASTt   JPEG_ACCURATEt	   JPEG_CMYKt   JPEG_EXIFROTATEt   JPEG_QUALITYSUPERBt   JPEG_QUALITYGOODt   JPEG_QUALITYNORMALt   JPEG_QUALITYAVERAGEt   JPEG_QUALITYBADt   JPEG_PROGRESSIVEt   JPEG_SUBSAMPLING_411t   JPEG_SUBSAMPLING_420t   JPEG_SUBSAMPLING_422t   JPEG_SUBSAMPLING_444t   JPEG_OPTIMIZEt   JPEG_BASELINEt   KOALA_DEFAULTt   LBM_DEFAULTt   MNG_DEFAULTt   PCD_DEFAULTt   PCD_BASEt   PCD_BASEDIV4t   PCD_BASEDIV16t   PCX_DEFAULTt   PFM_DEFAULTt   PICT_DEFAULTt   PNG_DEFAULTt   PNG_IGNOREGAMMAt   PNG_Z_BEST_SPEEDt   PNG_Z_DEFAULT_COMPRESSIONt   PNG_Z_BEST_COMPRESSIONt   PNG_Z_NO_COMPRESSIONt   PNG_INTERLACEDt   PNM_DEFAULTt   PNM_SAVE_RAWt   PNM_SAVE_ASCIIt   PSD_DEFAULTt   PSD_CMYKt   PSD_LABt   RAS_DEFAULTt   RAW_DEFAULTt   RAW_PREVIEWt   RAW_DISPLAYt   SGI_DEFAULTt   TARGA_DEFAULTt   TARGA_LOAD_RGB888t   TARGA_SAVE_RLEt   TIFF_DEFAULTt	   TIFF_CMYKt   TIFF_PACKBITSt   TIFF_DEFLATEt   TIFF_ADOBE_DEFLATEt	   TIFF_NONEt   TIFF_CCITTFAX3t   TIFF_CCITTFAX4t   TIFF_LZWt	   TIFF_JPEGt   TIFF_LOGLUVt   WBMP_DEFAULTt   XBM_DEFAULTt   XPM_DEFAULT(    (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyRK      s   t   METADATA_MODELSc           B sD   e  Z d  Z d Z d Z d Z d Z d Z d Z d Z	 d Z
 d	 Z RS(
   i    i   i   i   i   i   i   i   i   i	   (   R0   R1   t   FIMD_COMMENTSt   FIMD_EXIF_MAINt   FIMD_EXIF_EXIFt   FIMD_EXIF_GPSt   FIMD_EXIF_MAKERNOTEt   FIMD_EXIF_INTEROPt	   FIMD_IPTCt   FIMD_XMPt   FIMD_GEOTIFFt   FIMD_ANIMATION(    (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR     s   t   METADATA_DATATYPEc           B sk  e  Z d  Z d Z d Z d Z d Z d Z d Z d Z	 d Z
 d	 Z d
 Z d Z d Z d Z d Z d Z d Z i e j e 6e j e 6e j e 6d e j f d e j f g e 6e j e 6e j e 6e j e 6e j e 6e j e 6e j e	 6e j e
 6d e j f d e j f g e 6e j e 6e j e 6e j e 6d e j f d e j f d e j f d e j f g e 6Z RS(   i   i   i   i   i   i   i   i   i	   i
   i   i   i   i   i   i   i   t	   numeratort   denominatort   Rt   Gt   Bt   A(   R0   R1   t	   FIDT_BYTEt
   FIDT_ASCIIt
   FIDT_SHORTt	   FIDT_LONGt   FIDT_RATIONALt
   FIDT_SBYTEt   FIDT_UNDEFINEDt   FIDT_SSHORTt
   FIDT_SLONGt   FIDT_SRATIONALt
   FIDT_FLOATt   FIDT_DOUBLEt   FIDT_IFDt   FIDT_PALETTEt
   FIDT_LONG8t   FIDT_SLONG8t	   FIDT_IFD8R?   R@   RA   RC   t   uint64t   int64t   int8RB   RD   RE   RF   RH   (    (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR   &  sJ   












t	   Freeimagec           B s:  e  Z d  Z i) e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d	 6e j d: f d
 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d 6e j d: f d  6e j d: f d! 6e j d: f d" 6e j d: f d# 6e j d: f d$ 6e j d: f d% 6e j d: f d& 6e j d: f d' 6e j d: f d( 6e j d: f d) 6Z d*   Z	 e
 d+    Z d,   Z d-   Z d.   Z d/   Z d0   Z d1   Z d2   Z d3   Z d4   Z d5   Z d: d6  Z d7 d8  Z d7 d9  Z RS(;   s   Class to represent an interface to the FreeImage library.
    This class is relatively thin. It provides a Pythonic API that converts
    Freeimage objects to Python objects, but that's about it. 
    The actual implementation should be provided by the plugins.
    
    The recommended way to call into the Freeimage library (so that
    errors and warnings show up in the right moment) is to use this 
    object as a context manager:
    with imageio.fi as lib:
        lib.FreeImage_GetPalette()
    
    t   FreeImage_AllocateTt   FreeImage_FindFirstMetadatat   FreeImage_GetBitst   FreeImage_GetPalettet   FreeImage_GetTagKeyt   FreeImage_GetTagValuet   FreeImage_CreateTagt   FreeImage_Savet   FreeImage_Loadt   FreeImage_LoadFromMemoryt   FreeImage_OpenMultiBitmapt#   FreeImage_LoadMultiBitmapFromMemoryt   FreeImage_LockPaget   FreeImage_OpenMemoryt   FreeImage_GetVersiont   FreeImage_GetFIFExtensionListt   FreeImage_GetFormatFromFIFt   FreeImage_GetFIFDescriptiont   FreeImage_ColorQuantizeExt   FreeImage_IsLittleEndiant   FreeImage_SetOutputMessaget   FreeImage_GetFIFCountt   FreeImage_IsPluginEnabledt   FreeImage_GetFileTypet   FreeImage_GetTagTypet   FreeImage_GetTagLengtht   FreeImage_FindNextMetadatat   FreeImage_FindCloseMetadatat   FreeImage_GetFIFFromFilenamet   FreeImage_FIFSupportsReadingt   FreeImage_FIFSupportsWritingt   FreeImage_FIFSupportsExportTypet   FreeImage_FIFSupportsExportBPPt   FreeImage_GetHeightt   FreeImage_GetWidtht   FreeImage_GetImageTypet   FreeImage_GetBPPt   FreeImage_GetColorsUsedt   FreeImage_ConvertTo32Bitst   FreeImage_GetPitcht   FreeImage_Unloadc          s|   d    _ t j     _ g    _ t j j d  r? t	 j
 } n	 t	 j } | d  t	 j t	 j    f d    } |   _ d  S(   Nt   winc          sO   | j  d  }   j j |  x) t   j  d k rJ   j j d  q" Wd  S(   Ns   utf-8i   i    (   t   decodet	   _messagest   appendt   lent   pop(   t   fift   message(   t   self(    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   error_handler  s    (   R   R   t	   threadingt   RLockt   _lockR   R)   t   platformt
   startswitht   ctypest   WINFUNCTYPEt	   CFUNCTYPEt   c_intt   c_char_pt   _error_handler(   R   t   functypeR   (    (   R   s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   __init__  s    			'c         C s   |  j  d  k rT y |  j   WqT t k
 rP } d |  _  |  j  t |  7_  qT Xn  t |  j  t  rx t |  j    n  |  j  S(   Ns+   The freeimage library could not be loaded: (   R   R   t   load_freeimaget   OSErrorR$   t
   isinstanceR!   (   R   t   err(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR%     s    	c         C s$   y |  j  Wn t k
 r t SXt S(   N(   R%   t	   ExceptionR    t   True(   R   (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   has_lib  s
    c         C s   t  } y? |  j   |  j   |  j j   j d  d k rD t } n  Wn t k
 rX n X| s} t   |  j   |  j   n  |  j j	 |  j
  |  j j   j d  |  _ d S(   s    Try to load the freeimage lib from the system. If not successful,
        try to download the imageio version and try again.
        s   utf-8s   3.15N(   R    t   _load_freeimaget   _register_apiR%   R   R   R  R	  R'   R   R  t   lib_version(   R   t   success(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR    s    


c   
      C s  d d g } d d d d g } t    } t   } | rv t | } x3 | D]( } | j d t j j | d |   qG Wn  t j d d   } | d  k	 r | j d |  n  y t	 | | |  \ } } Wn/ t
 k
 r } t |  d	 }	 t
 |	   n X| |  _ | |  _ d  S(
   Nt	   freeimaget   libfreeimaget	   FreeImages   libfreeimage.dylibs   libfreeimage.sos   libfreeimage.so.3i    R   s&   
Please install the FreeImage library.(   R   R
   R   t   insertR   t   patht   joinR   R   R   R	  R$   R   t	   lib_fname(
   R   t	   lib_namest   exact_lib_namest   res_dirsR   R   t   dirR%   R  t   err_msg(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR    s*    			
)	c         C sN   xG |  j  j   D]6 \ } \ } } t |  j |  } | | _ | | _ q Wd  S(   N(   t   _APIt   itemst   getattrR%   t   restypet   argtypes(   R   t   fR"  R#  t   func(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR    s    "	c         C s   |  j  j   |  j S(   N(   R   t   acquireR%   (   R   (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt	   __enter__  s    c         G s   |  j    |  j j   d  S(   N(   t   _show_any_warningsR   t   release(   R   t   args(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   __exit__  s    
c         C s   g  |  _  d S(   sy    Reset the list of output messages. Call this before 
        loading or saving an image with the FreeImage API.
        N(   R   (   R   (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt
   _reset_log  s    c         C s1   |  j  r) d j |  j   } |  j   | Sd Sd S(   s    Get the output messages produced since the last reset as 
        one string. Returns 'No known reason.' if there are no messages. 
        Also resets the log.
        t    s   No known reason.N(   R   R  R,  (   R   t   res(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   _get_error_message  s
    	
c         C s1   |  j  r- t j d |  j    |  j   n  d S(   s    If there were any messages since the last reset, show them
        as a warning. Otherwise do nothing. Also resets the messages.
        s   imageio.freeimage warning: N(   R   R"   R#   R/  R,  (   R   (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR(  +  s    	c         C s   g  |  j  D] } | ^ q
 S(   sy    Return a list of the last 256 output messages 
        (warnings and errors) produced by the FreeImage library.
        (   R   (   R   t   m(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   get_output_log3  s    c         C s  |  |} d } | d k r* t  d   n  | d k r | d
 k	 r | j t j |  t |   } | j t j |  t |   } | j t j |   n  | d k r t	 j
 j |  r | j t |  d  } q n  | d k r | j t |   } n  | d k rt  d |   n^ | d k rK| j |  rKt  d |   n/ | d k rz| j |  rzt  d	 |   n  | SWd
 QXd
 S(   s   Get the freeimage Format (FIF) from a given filename.
        If mode is 'r', will try to determine the format by reading
        the file, otherwise only the filename is used.
        
        This function also tests whether the format supports reading/writing.
        it   rws"   Invalid mode (must be "r" or "w").t   ri    s$   Cannot determine format of file "%s"t   ws$   Cannot write the format of file "%s"s#   Cannot read the format of file "%s"N(   t
   ValueErrorR   R   R   R  R   t   FreeImage_GetFileTypeFromMemoryt   c_void_pt   FreeImage_CloseMemoryR   R  t   isfileR   t   efnR   R   R   (   R   t   filenamet   modet   bytesR%   t   ftypet   fimemory(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   getFIF:  s,    	i    c         C s   t  |  | | |  S(   sY    create_bitmap(filename, ftype, flags=0)
        Create a wrapped bitmap object.
        (   t   FIBitmap(   R   R;  R>  t   flags(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   create_bitmapc  s    c         C s   t  |  | | |  S(   sm    create_multipage_bitmap(filename, ftype, flags=0)
        Create a wrapped multipage bitmap object.
        (   t   FIMultipageBitmap(   R   R;  R>  RB  (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   create_multipage_bitmapi  s    N(   R0   R1   t   __doc__R   R7  R   R  R  R  R  t   propertyR%   R  R  R  R  R'  R+  R,  R/  R(  R1  R@  RC  RE  (    (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR   R  st   				"								)t   FIBaseBitmapc           B sA   e  Z d    Z d   Z d   Z d d  Z d   Z d   Z RS(   c         C s:   | |  _  | |  _ | |  _ | |  _ d  |  _ g  |  _ d  S(   N(   t   _fit	   _filenamet   _ftypet   _flagsR   t   _bitmapt   _close_funcs(   R   R   R;  R>  RB  (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR  q  s    					c         C s   |  j    d  S(   N(   t   close(   R   (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   __del__y  s    c         C s   |  j  d  k	 r |  j r xQ |  j D]F } y, |  j  | d } | | d   Wd  QXWq" t k
 rg q" Xq" Wg  |  _ d  |  _  n  d  S(   Ni    i   (   RM  R   RN  RI  R  (   R   t
   close_funct   fun(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyRO  |  s    

	c         C s\   |  j  d k	 r n  | d k r6 |  j j j | f } n  | |  _  | rX |  j j |  n  d S(   sK    Function to set the bitmap and specify the function to unload it.
        N(   RM  R   RI  R%   R   RN  R   (   R   t   bitmapRQ  (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   _set_bitmap  s    	c      
   C s@  g  t  j j   D]+ \ } } | j d  r | d | f ^ q } t   } t j   } |  j } x| D]\ } } | j | |  j	 t j
 |   } t j |  } | ri t }	 xj|	 r| j |  j d  }
 | j |  } | j |  } t j | } | j | j |   } t t |   } | } | t j k rQ| j d d  } n | t j k rt j | } t rt | t t f  rqy> t j | d | j   } t |  d k r| d } n  Wqt  k
 rqXn  | j! | t    } | | |
 <| j" | t j
 |   }	 q W| j# |  qi qi W| SWd  QXd  S(   Nt   FIMD_i   s   utf-8t   replaceR.   i   i    ($   R   t   __dict__R   R   R   R   R7  RI  R   RM  t   byrefR  R   R   R   R   t   c_chart   from_addressR   R   t	   bytearrayR   R   RH   R	   R
  t   listt   tupleR?   t
   frombuffert   copyR   R  t
   setdefaultR   R   (   R   t   namet   numbert   modelst   metadatat   tagR%   t
   model_namet   mdhandlet   moret   tag_namet   tag_typet	   byte_sizet   char_ptrt   datat	   tag_bytest   tag_valR.   t   subdict(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   get_meta_data  sN    %		
c         C s  i  } x= t  j j   D], \ } } | j d  r | | | d <q q Wd   } |  j -} x#| j   D]\ } } | j | d   } | d  k r qh n  x| j   D]\ }	 }
 | j   } t j	 |  } zyYt
 } t |
 t  ry |
 j d  } t } Wqt k
 rqXn  | r5t j } t |  } nk t |
 d  sYt j |
 g  }
 n  | |
 j  } | d  k rt j d |	  w n  |
 j   } |
 j } | j | |	 j d   | j | |  | j | |  | j | t |   | j | |  | j  |  } | j! | |  j" | |  Wn< t# k
 rf} t j d |	 |  j j$   t% |  f  n XWd  | j& |  Xq Wqh WWd  QXd  S(	   NRU  i   c         S s8   x1 t  j j   D] \ } } |  | k r | Sq Wd  Sd  S(   N(   R   RH   R   R   (   R.   Rb  t   numpy_dtype(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   get_tag_type_number  s    t   asciiR.   s>   imageio.freeimage warning: Could not determine tag type of %r.s   utf-8s6   imagio.freeimage warning: Could not set tag %r: %s, %s('   R   RW  R   R   RI  t   getR   R   R   R7  R    R
  R   R(   R  t   UnicodeErrorR   R   R   t   hasattrR?   t   arrayR.   R"   R#   t   tostringt   sizet   FreeImage_SetTagKeyt   FreeImage_SetTagTypet   FreeImage_SetTagCountt   FreeImage_SetTagLengtht   FreeImage_SetTagValueR   t   FreeImage_SetMetadataRM  R  R/  R$   t   FreeImage_DeleteTag(   R   Rd  Rc  Ra  Rb  Rs  R%   Rf  Rp  Ri  Ro  Re  t   is_asciiRn  Rj  t	   tag_countt   tag_keyR  (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   set_meta_data  s^    	 
		(N(	   R0   R1   R  RP  RO  R   RT  Rq  R  (    (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyRH  p  s   				GRA  c           B sk   e  Z d  Z d   Z d d  Z d d  Z d   Z d   Z d   Z	 d   Z
 d   Z d	 d
 d  Z RS(   s'    Wrapper for the FI bitmap object.
    c         C sS  t  | t j  s t  | j } | j } | d  \ } } t |  d k rU d } n) t |  d k rt | d } n
 | d } y# t j | j	 | f } | |  _
 Wn t k
 r t d   n X|  j  } d | j | }	 | j | | | |	 d d d  }
 t j |
  }
 |
 s0t d |  j j     n  |  j |
 | j |
 f  Wd  QXd  S(   Ni   i   i   i    s,   Cannot write arrays of given type and shape.i   s)   Could not allocate bitmap for storage: %s(   R
  R?   t   ndarrayt   AssertionErrort   shapeR.   R   R/   RI   t   typet   _fi_typet   KeyErrorR5  RI  t   itemsizeR   R   R7  R!   R/  RT  R   (   R   Rx  R  R.   R3  t   ct
   n_channelst   fi_typeR%   t   bppRS  (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   allocate*  s.    			
!c         C s   | d  k r |  j } n  |  j { } | j |  j t |  |  j  } t j |  } | s t	 d |  j |  j j
   f   n  |  j | | j | f  Wd  QXd  S(   Ns   Could not load bitmap "%s": %s(   R   RJ  RI  R   RK  R:  RL  R   R7  R5  R/  RT  R   (   R   R;  R%   RS  (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   load_from_filenameO  s    !c      	   C s   | d  k r |  j } n  |  j } |  j } |  j } |  j  } | t j k rl | j | | j	 |   } n | j
 | |  } | s t d   n  | j | | t |  |  j  } | s t d |  j |  j j   f   n  Wd  QXd  S(   Ns2   Cannot save image of this format to this file types   Could not save file "%s": %s(   R   RJ  RK  RM  R  RI  R/   R3   R   R   R   t	   TypeErrorR   R:  RL  R!   R/  (   R   R;  R>  RS  R  R%   t	   can_writeR.  (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   save_to_filenameq  s"    			!c         C s3  |  j    \ } } } |  j | | t  } |  j  } | j   } Wd  QXd   } t |  d k r| r| j t j k r| | d  } | | d  }	 | | d  }
 | d d k r t j	 |
 |	 | f  S| d d k r
| | d  } t j	 |
 |	 | | f  St
 d |   n  | |  j   } | S(   Nc         S s   |  j  d k r# |  d  d  d  j S|  j  d k rR |  d  d   d  d  d  f j S|  j  d k r |  d  d   d  d   d  d  d  f j S|  j  d k r |  d  d   d  d   d  d   d  d  d  f j Sd  S(   Ni   ii   i   i   (   t   ndimt   T(   t   arr(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   n  s     )i   i    i   i   i   s    Cannot handle images of shape %s(   t   _get_type_and_shapet   _wrap_bitmap_bits_in_arrayR    RI  R   R   R  R?   R@   t   dstackR5  R_  (   R   R.   R  R  Rx  R%   t   isleR  t   bt   gR3  t   a(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   get_image_data  s"    	*c         C s  t  | t j  s t  | j } | j } |  j  } | j   } Wd  QX| d  \ } } t |  d k r d } | | f }	 n8 t |  d k r | d } | | | f }	 n
 | d } d   }
 |  j	 |	 | t
  } t |  d k r| r| j t j k r| d  d   d  d   d f } | d  d   d  d   d f } | d  d   d  d   d f } |
 |  | d <|
 |  | d <|
 |  | d <| d d k r| d  d   d  d   d f } |
 |  | d <qn |
 |  | (|  j r|  j |  n  t |  d k r| j t j k r|  j  } | j |  j  } Wd  QXt j |  } | sct d   n  y t j j } Wn" t k
 rt j d d } n Xt j | | d	  n  d  S(
   Ni   i   i   i    c         S s   |  d  d  d  j  S(   Ni(   R  (   R  (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR    s    i   s   Could not get image paletteRm  i   (   R
  R?   R  R  R  R.   RI  R   R   R  R  R  R@   t   _need_finisht   _finish_wrapped_arrayR   RM  R   R7  R!   t   GREY_PALETTERm  R  t   __array_interface__t   memmove(   R   Rx  R  R.   R%   R  R3  R  R  t   w_shapeR  t   wrapped_arrayR   R   R   R   t   palettet   palette_data(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   set_image_data  sN    		

	*	$c      	   C sM  |  j  + } | j |  j  } | j |  j  } Wd QX| d } | | } | j }	 t |  d k r |	 | d |	 | f }
 n |	 | f }
 t j | j |  } y> t	 |  _
 t r t    n  t j | d | d | d |
 SWngt k
 rH| rt |  _
 t j | d | St t |   } t j | d | j   } t |  d k r| d |
 d | d | d f | _ | d | d  d | d	  d | d  f } t j | d | j } x t | d  D], } | d d  d d  | f j | | <qWn> | d	 |
 d f | _ | d | d	  d | d  f j } | Sn Xd S(
   s%  Return an ndarray view on the data in a FreeImage bitmap. Only
        valid for as long as the bitmap is loaded (if single page) / locked
        in memory (if multipage). This is used in loading data, but
        also during saving, to prepare a strided numpy array buffer.
        
        Nii   i    R.   t   buffert   stridesi   i   (   RI  R   RM  R   R  R   R   RY  RZ  R    R  t   TEST_NUMPY_NO_STRIDESt   NotImplementedErrorR?   R  R  t   zerosR   R[  R^  R_  R  R.   t   rangeR  (   R   R  R.   t   saveR%   t   pitcht   bitst   heightRk  R  R  Rm  R=  Rx  t   array2t   i(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR    s<    

		#	&1-'c         C s  |  j  = } | j |  j  } | j |  j  } | j |  j  } Wd QX| d | j } | | } | | j d } d | k o d k  n s t  | j d | | f }	 t j	 |	 | j
  }
 | d k r | j |
 d d  d | j d  d f <nY xV t |  D]H } | | d d  d d  f j |
 d d  d | j d  | f <qW|
 j d d } t j | | |
 j  ~
 d S(	   s7    Hardcore way to inject numpy array in bitmap.
        Ni   ii    i
   ii   Rm  (   RI  R   RM  R   R   R  R  R  R?   R  R.   R  R  R  R   R  t   nbytes(   R   Rx  R%   R  R  R  t	   nchannelst	   realwidtht   extrat   newshapeR  R  t   data_ptr(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR  C  s"    
",Fc         C s4  |  j  } |  j P } | j |  } | j |  } | j |  |  _ } | s^ t d   n  Wd  QXd  } t j	 | } | t j
 k r|  j % } | j |  } | j |  } Wd  QX| r| d k rU| j |  }	 t j |	  }	 t j d j |	 j  }
 t j |
 t j  j   }
 t |
 k j   rUg  } t j |  | | | g | f Sn  | j |  } t j |  } |  j |  |  j   S| d k rg  } q| d k rd g } q| d k rd g } q| j |  } t j |  } |  j |  |  j   Sn t j | } t j |  | | | g | f S(	   Ns   Unknown image pixel typei   i   i   i   i   i    i   (   RM  RI  R   R   R   R  R5  R   R/   RH   R3   R   R   R   R   R7  t   c_uint8RZ  t   valueR?   R^  RC   R_  R  t   allR.   R   RT  R  RJ   (   R   RS  R%   R4  t   hR  R  R.   t   has_palletteR  t   pRJ   t	   newbitmap(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR  ^  sL    	#
	i    i   c      
   C s   |  j   } | j |  j | | d d  } t j |  } | sd t d |  j |  j  j   f   n  t	 |  j  |  j |  j
 |  j  } | j | | j | f  |  j | _ | SWd QXd S(   s    Quantize the bitmap to make it 8-bit (paletted). Returns a new
        FIBitmap object.
        Only for 24 bit images.
        i    s"   Could not quantize bitmap "%s": %sN(   RI  R   RM  R   R   R7  R5  RJ  R/  RA  RK  RL  RT  R   R  (   R   t	   quantizert   palettesizeR%   RS  t   new(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   quantize  s    !N(   R0   R1   RF  R  R   R  R  R  R  R  R  R  R  (    (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyRA  &  s   	%"A	%	8	4		5RD  c           B sA   e  Z d  Z d d  Z d d  Z d   Z d   Z d   Z RS(   s1    Wrapper for the multipage FI bitmap object.
    c         C s   | d  k r |  j } n  t } t } t } |  j  } | j |  j t |  | | | |  j  } t	 j
 |  } | s |  j j   } t d |  j | f   n  |  j | | j | f  Wd  QXd  S(   Ns+   Could not open file "%s" as multi-image: %s(   R   RJ  R    R  RI  R   RK  R:  RL  R   R7  R/  R5  RT  t   FreeImage_CloseMultiBitmap(   R   R;  t
   create_newt	   read_onlyt   keep_cache_in_memoryR%   t   multibitmapR  (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR    s(    	c         C s   | d  k r |  j } n  t } t } t } |  j  } | j |  j t |  | | | d  } t j	 |  } | s d |  j |  j j
   f } t |   n  |  j | | j | f  Wd  QXd  S(   Ni    s4   Could not open file "%s" for writing multi-image: %s(   R   RJ  R  R    RI  R   RK  R:  R   R7  R/  R5  RT  R  (   R   R;  R  R  R  R%   R  t   msg(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyR    s(    		c         C s&   |  j   } | j |  j  SWd  QXd  S(   N(   RI  t   FreeImage_GetPageCountRM  (   R   R%   (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   __len__  s    c      
   C s   |  j   } | j |  j |  } t j |  } | s^ t d | |  j |  j  j   f   n  t |  j  |  j |  j	 |  j
  } | j | | j |  j | t f  | SWd QXd S(   sm    Return the sub-bitmap for the given page index.
        Please close the returned bitmap when done.
        s%   Could not open sub-image %i in %r: %sN(   RI  R   RM  R   R7  R5  RJ  R/  RA  RK  RL  RT  t   FreeImage_UnlockPageR    (   R   t   indexR%   RS  t   bm(    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   get_page  s    "!c         C s,   |  j   } | j |  j | j  Wd QXd S(   s4    Add a sub-bitmap to the multi-page bitmap.
        N(   RI  t   FreeImage_AppendPageRM  (   R   RS  R%   (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   append_bitmap1  s    N(	   R0   R1   RF  R   R  R  R  R  R  (    (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyRD    s   3		(-   RF  t
   __future__R    R   R   R   R)   R   R   t   loggingR?   t   coreR   R   R   R   R   R   R	   R
   R   R   t	   getLoggerR0   R"   R    R  R   R   R   R'   R:  t   arangeRC   R  t   objectR/   RK   R   R   R   RH  RA  RD  R   (    (    (    s9   lib/python2.7/site-packages/imageio/plugins/_freeimage.pyt   <module>   s@   F
		>m,  y