ó
;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 m	 Z	 d d l
 m Z e j d d k Z e r| e Z n e Z d „  Z d	 d d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e d d d d ƒ Z e	 j e ƒ d S(   s    BSDF plugin.
i    (   t   absolute_importt   print_functiont   divisionNi   (   t   formats(   t   Formati   c         C` s™   d d l  m } d | j f d „  ƒ  Y} d | j f d „  ƒ  Y} d | f d „  ƒ  Y} d	 | f d
 „  ƒ  Y} | | | g } | j | |   } | | f S(   Ni   (   t   _bsdft   NDArrayExtensionc           B` s/   e  Z d  Z d Z e j Z d „  Z d „  Z RS(   sC    Copy of BSDF's NDArrayExtension but deal with lazy blobs.
        t   ndarrayc         S` s+   t  d | j d t | j ƒ d | j ƒ  ƒ S(   Nt   shapet   dtypet   data(   t   dictR   t	   text_typeR	   t   tobytes(   t   selft   st   v(    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt   encode"   s    c         S` s   | S(   N(    (   R   R   R   (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt   decode%   s    (	   t   __name__t
   __module__t   __doc__t   namet   npR   t   clsR   R   (    (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyR      s
   		t   ImageExtensionc           B` s    e  Z d  Z d „  Z d „  Z RS(   sH    We implement two extensions that trigger on the Image classes.
        c         S` s   t  d | j d | j ƒ S(   Nt   arrayt   meta(   R   R   R   (   R   R   R   (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyR   ,   s    c         S` s   t  | d | d ƒ S(   NR   R   (   t   Image(   R   R   R   (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyR   /   s    (   R   R   R   R   R   (    (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyR   (   s   	t   Image2DExtensionc           B` s   e  Z d  Z e Z RS(   t   image2d(   R   R   R   t   Image2DR   (    (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyR   2   s   t   Image3DExtensionc           B` s   e  Z d  Z e Z RS(   t   image3d(   R   R   R   t   Image3DR   (    (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyR    7   s   (   t    R   t	   Extensiont   BsdfSerializer(   t   optionst   bsdfR   R   R   R    t   extst
   serializer(    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt   get_bsdf_serializer   s    
R   c           B` s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s«    Class in which we wrap the array and meta data. By using an extension
    we can make BSDF trigger on these classes and thus encode the images.
    as actual images.
    c         C` s   | |  _  | |  _ d  S(   N(   R   R   (   R   R   R   (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt   __init__H   s    	c         C` s|   t  |  j t j ƒ su |  j } | d } t  | t ƒ sF | j ƒ  } n  t j | d | d ƒ|  _ | d |  j _ n  |  j S(   NR
   R	   R   (   t
   isinstanceR   R   R   t   bytest	   get_bytest
   frombufferR   (   R   R   t   blob(    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt	   get_arrayL   s    	
c         C` s   |  j  S(   N(   R   (   R   (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt   get_metaV   s    (   R   R   R   R+   R1   R2   (    (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyR   B   s   		
R   c           B` s   e  Z RS(    (   R   R   (    (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyR   Z   s   R"   c           B` s   e  Z RS(    (   R   R   (    (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyR"   ^   s   t
   BsdfFormatc           B` sR   e  Z d  Z d „  Z d „  Z d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ RS(   s   The BSDF format enables reading and writing of image data in the
    BSDF serialization format. This format allows storage of images, volumes,
    and series thereof. Data can be of any numeric data type, and can
    optionally be compressed. Each image/volume can have associated
    meta data, which can consist of any data type supported by BSDF.
    
    By default, image data is lazily loaded; the actual image data is
    not read until it is requested. This allows storing multiple images
    in a single file and still have fast access to individual images.
    Alternatively, a series of images can be read in streaming mode, reading
    images as they are read (e.g. from http).
    
    BSDF is a simple generic binary format. It is easy to extend and there
    are standard extension definitions for 2D and 3D image data.
    Read more at http://bsdf.io.
    
    Parameters for reading
    ----------------------
    random_access : bool
        Whether individual images in the file can be read in random order.
        Defaults to True for normal files, and to False when reading from HTTP.
        If False, the file is read in "streaming mode", allowing reading
        files as they are read, but without support for "rewinding".
        Note that setting this to True when reading from HTTP, the whole file
        is read upon opening it (since lazy loading is not possible over HTTP).
    
    Parameters for saving
    ---------------------
    compression : {0, 1, 2}
        Use ``0`` or "no" for no compression, ``1`` or "zlib" for Zlib
        compression (same as zip files and PNG), and ``2`` or "bz2" for Bz2
        compression (more compact but slower). Default 1 (zlib).
        Note that some BSDF implementations may not support compression
        (e.g. JavaScript).
    
    c         C` s7   | j  d |  j d k r3 | j j d ƒ r3 t Sn  d  S(   Ni   t   ?t   BSDF(   t   modet   modest
   firstbytest
   startswitht   True(   R   t   request(    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt	   _can_readˆ   s    c         C` s7   | j  d |  j d k r3 | j |  j k r3 t Sn  d  S(   Ni   R4   (   R6   R7   t	   extensiont
   extensionsR:   (   R   R;   (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt
   _can_write   s    t   Readerc           B` s8   e  Z d d  „ Z d „  Z d „  Z d „  Z d „  Z RS(   c         C` s±  |  j  j d  d k s" t d ƒ ‚ |  j  j d d !d k pj |  j  j d d !d k pj |  j  j d d !d	 k sp n  i  } |  j  j j d ƒ rÁ | d  k r t n	 t | ƒ } t | d <| | d <n3 | d  k rÓ t n	 t | ƒ } | | d <| | d <|  j  j	 ƒ  } t
 | ƒ \ } |  _ |  j j | ƒ |  _ t |  j t ƒ r€d |  j k r€d |  j k r€t |  j d |  j d ƒ |  _ n  t |  j t t | j f ƒ s­t d ƒ ‚ n  d  S(   Ni   R5   s   Not a BSDF filei   i   s	   Mimage2Ds	   Mimage3Di   t   ls   http://s   https://t	   lazy_blobt   load_streamingR   R   s8   BSDF file does not look seem to have an image container.(   s   http://s   https://(   R;   R8   t   AssertionErrort   filenameR9   t   Nonet   Falset   boolR:   t   get_fileR*   t   _serializert   loadt   _streamR,   R   R   t   listt
   ListStreamt   RuntimeError(   R   t   random_accessR&   t   rat   fileR'   (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt   _open—   s.    "	

#c         C` s   d  S(   N(    (   R   (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt   _closeÃ   s    c         C` sX   t  |  j t ƒ r d St  |  j t ƒ r5 t |  j ƒ S|  j j d k  rN t j S|  j j S(   Ni   i    (   R,   RL   R   RM   t   lent   countR   t   inf(   R   (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt   _get_lengthÆ   s    c         C` s  | d k  s | |  j  ƒ  k r= t d | |  j  ƒ  f ƒ ‚ n  t |  j t ƒ r[ |  j } nx t |  j t ƒ r} |  j | } nV | |  j j k  rž t d ƒ ‚ n  x# | |  j j k rÃ |  j j ƒ  q¡ W|  j j ƒ  } t | t ƒ rd | k rd | k rt | d | d ƒ } n  t | t ƒ r9| j	 ƒ  | j
 ƒ  f St | ƒ } t | ƒ d k  r]| n | d  d } t d	 | ƒ ‚ d  S(
   Ni    s   Image index %i not in [0 %i].sI   BSDF file is being read in streaming mode, thus does not allow rewinding.R   R   iÈ   iÅ   s   ...s   BSDF file contains non-image (   t
   get_lengtht
   IndexErrorR,   RL   R   RM   t   indext   nextR   R1   R2   t   reprRU   RO   (   R   R[   t   image_obt   r(    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt	   _get_dataÏ   s,    &c         C` s   i  S(   N(    (   R   R[   (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt   _get_meta_dataô   s    N(   R   R   RF   RS   RT   RX   R`   Ra   (    (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyR@   –   s
   ,				%t   Writerc           B` s/   e  Z d  d „ Z d „  Z d „  Z d „  Z RS(   i   c         C` s…   i | d 6} t  | ƒ \ } |  _ |  j j d d k rM d  |  _ t |  _ n4 |  j j ƒ  } | j	 ƒ  |  _ |  j j
 | |  j ƒ d  S(   Nt   compressioni   t   iv(   R*   RJ   R;   R6   RF   RL   RG   t   _writtenRI   RN   t   save(   R   Rc   R&   R'   RR   (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyRS   ú   s    	c         C` s&   |  j  d  k	 r" |  j  j t ƒ n  d  S(   N(   RL   RF   t   closeRG   (   R   (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyRT     s    c         C` s§  d  } |  j j d d k r% d } n_ |  j j d d k rD d } n@ d } | j d k s{ | j d k r„ | j d d k r„ d } n  | d	 k s– t ‚ | d k rÜ | j d k s| j d k rÓ | j d d k st ‚ n7 | j d k s| j d k r| j d d k st ‚ | d k r1t | | ƒ } n t | | ƒ } |  j d  k r“|  j	 set d ƒ ‚ t
 |  _	 |  j j ƒ  } |  j j | | ƒ n |  j j | ƒ d  S(
   Ni   t   iIi   t   vVi   iÿÿÿÿi   s"   Cannot write singleton image twice(   i   i   (   RF   R;   R6   t   ndimR   RD   R   R"   RL   Re   R:   RI   RJ   Rf   t   append(   R   t   imR   Rj   t   obRR   (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt   _append_data  s*    		1	:7	c         C` s   t  d ƒ ‚ d  S(   Ns2   The BSDF format only supports per-image meta data.(   RO   (   R   R   (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt   set_meta_data.  s    (   R   R   RS   RT   Rn   Ro   (    (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyRb   ù   s   		 (   R   R   R   R<   R?   R   R@   Rb   (    (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyR3   b   s
   $		cR'   s1   Format based on the Binary Structured Data Formats   .bsdft   iIvV(    (   R   t
   __future__R    R   R   t   syst   numpyR   R#   R   t   coreR   t   version_infot   PY3t   strR   t   unicodeR*   R   R   R"   R3   t   formatt
   add_format(    (    (    s3   lib/python2.7/site-packages/imageio/plugins/bsdf.pyt   <module>   s(   		*Ð	