B
    %Z\}-                 @   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rXeZneZdd	 ZG d
d dZG dd deZG dd deZG dd deZeddddZe	e dS )z BSDF plugin.
    )absolute_importprint_functiondivisionN   )formats)Format   c             C   sp   ddl m} G dd d|j}G dd d|j}G dd d|}G d	d
 d
|}|||g}|j|f| }||fS )N   )_bsdfc               @   s*   e Zd ZdZdZejZdd Zdd Z	dS )z-get_bsdf_serializer.<locals>.NDArrayExtensionzC Copy of BSDF's NDArrayExtension but deal with lazy blobs.
        ndarrayc             S   s   t |jt|j| dS )N)shapedtypedata)dictr   	text_typer   tobytes)selfsv r   3lib/python3.7/site-packages/imageio/plugins/bsdf.pyencode"   s    z4get_bsdf_serializer.<locals>.NDArrayExtension.encodec             S   s   |S )Nr   )r   r   r   r   r   r   decode%   s    z4get_bsdf_serializer.<locals>.NDArrayExtension.decodeN)
__name__
__module____qualname____doc__namenpr   clsr   r   r   r   r   r   NDArrayExtension   s
   r    c               @   s    e Zd ZdZdd Zdd ZdS )z+get_bsdf_serializer.<locals>.ImageExtensionzH We implement two extensions that trigger on the Image classes.
        c             S   s   t |j|jdS )N)arraymeta)r   r!   r"   )r   r   r   r   r   r   r   ,   s    z2get_bsdf_serializer.<locals>.ImageExtension.encodec             S   s   t |d |d S )Nr!   r"   )Image)r   r   r   r   r   r   r   /   s    z2get_bsdf_serializer.<locals>.ImageExtension.decodeN)r   r   r   r   r   r   r   r   r   r   ImageExtension(   s   r$   c               @   s   e Zd ZdZeZdS )z-get_bsdf_serializer.<locals>.Image2DExtensionZimage2dN)r   r   r   r   Image2Dr   r   r   r   r   Image2DExtension2   s   r&   c               @   s   e Zd ZdZeZdS )z-get_bsdf_serializer.<locals>.Image3DExtensionZimage3dN)r   r   r   r   Image3Dr   r   r   r   r   Image3DExtension7   s   r(   ) r
   Z	ExtensionZBsdfSerializer)optionsbsdfr    r$   r&   r(   ZextsZ
serializerr   r   r   get_bsdf_serializer   s    

r,   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r#   z 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"   r   r   r   __init__H   s    zImage.__init__c             C   sT   t | jtjsN| j}|d }t |ts.| }tj||d d| _|d | j_| jS )Nr   r   )r   r   )
isinstancer!   r   r   bytesZ	get_bytesZ
frombufferr   )r   r   Zblobr   r   r   	get_arrayL   s    
zImage.get_arrayc             C   s   | j S )N)r"   )r   r   r   r   get_metaV   s    zImage.get_metaN)r   r   r   r   r-   r0   r1   r   r   r   r   r#   B   s   
r#   c               @   s   e Zd ZdS )r%   N)r   r   r   r   r   r   r   r%   Z   s   r%   c               @   s   e Zd ZdS )r'   N)r   r   r   r   r   r   r   r'   ^   s   r'   c               @   sD   e Zd ZdZdd Zdd ZG dd dejZG dd	 d	ejZd
S )
BsdfFormata   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   s(   |j d | jd kr$|jdr$dS d S )Nr	   ?s   BSDFT)modemodes
firstbytes
startswith)r   requestr   r   r   	_can_read   s    zBsdfFormat._can_readc             C   s(   |j d | jd kr$|j| jkr$dS d S )Nr	   r3   T)r4   r5   	extension
extensions)r   r8   r   r   r   
_can_write   s    zBsdfFormat._can_writec               @   s6   e Zd ZdddZdd Zdd Zdd	 Zd
d ZdS )zBsdfFormat.ReaderNc             C   s>  | j jd d dkstd| j jdd dksX| j jdd dksX| j jdd d	ksXi }| j jd
r|d krvdnt|}d|d< | |d< n&|d krdnt|}||d< | |d< | j  }t|\}| _| j	|| _
t| j
trd| j
krd| j
krt| j
d | j
d | _
t| j
tt|jfs:tdd S )N   s   BSDFzNot a BSDF file      s	   Mimage2Ds	   Mimage3D      l)zhttp://zhttps://FZ	lazy_blobZload_streamingTr"   r!   z8BSDF file does not look seem to have an image container.)r8   r6   AssertionErrorfilenamer7   boolget_filer,   _serializerload_streamr.   r   r#   list
ListStreamRuntimeError)r   Zrandom_accessr*   Zrafiler+   r   r   r   _open   s,    

zBsdfFormat.Reader._openc             C   s   d S )Nr   )r   r   r   r   _close   s    zBsdfFormat.Reader._closec             C   s@   t | jtrdS t | jtr&t| jS | jjdk r8tjS | jjS )Nr	   r   )r.   rH   r#   rI   lencountr   inf)r   r   r   r   _get_length   s    
zBsdfFormat.Reader._get_lengthc             C   s  |dk s||   kr(td||   f t| jtr<| j}nRt| jtrT| j| }n:|| jjk rhtdx|| jjkr| j  qjW | j }t|trd|krd|krt|d |d }t|tr|	 |
 fS t|}t|dk r|n|d d d }td	| d S )
Nr   zImage index %i not in [0 %i].zIBSDF file is being read in streaming mode, thus does not allow rewinding.r"   r!         z...zBSDF file contains non-image )Z
get_length
IndexErrorr.   rH   r#   rI   indexnextr   r0   r1   reprrO   rK   )r   rV   Zimage_obrr   r   r   	_get_data   s,    


 zBsdfFormat.Reader._get_datac             C   s   i S )Nr   )r   rV   r   r   r   _get_meta_data   s    z BsdfFormat.Reader._get_meta_data)N)r   r   r   rM   rN   rR   rZ   r[   r   r   r   r   Reader   s
   
,	%r\   c               @   s.   e Zd ZdddZdd Zdd Zdd	 Zd
S )zBsdfFormat.Writerr	   c             C   s\   d|i}t |\}| _| jjd dkr4d | _d| _n$| j }| | _| j|| j d S )Ncompressionr	   ZivF)	r,   rF   r8   r4   rH   _writtenrE   rJ   save)r   r]   r*   r+   rL   r   r   r   rM      s    

zBsdfFormat.Writer._openc             C   s   | j d k	r| j d d S )NF)rH   close)r   r   r   r   rN     s    
zBsdfFormat.Writer._closec             C   s$  d }| j jd dkrd}n@| j jd dkr0d}n*d}|jdksV|jdkrZ|jd dkrZd}|dksft|dkr|jdks|jdkr|jd dkstn&|jdks|jdkr|jd dkst|dkrt||}n
t||}| jd kr| jrtd	d
| _| j 	 }| j
|| n| j| d S )Nr	   ZiIr   ZvVr   r=   )r   r   z"Cannot write singleton image twiceT)r8   r4   ndimr   rB   r%   r'   rH   r^   rE   rF   r_   append)r   Zimr"   rb   obrL   r   r   r   _append_data  s*    "(&

zBsdfFormat.Writer._append_datac             C   s   t dd S )Nz2The BSDF format only supports per-image meta data.)rK   )r   r"   r   r   r   set_meta_data.  s    zBsdfFormat.Writer.set_meta_dataN)r	   )r   r   r   rM   rN   re   rf   r   r   r   r   Writer   s   
 rg   N)	r   r   r   r   r9   r<   r   r\   rg   r   r   r   r   r2   b   s
   $cr2   r+   z1Format based on the Binary Structured Data Formatz.bsdfZiIvV)r   Z
__future__r   r   r   sysZnumpyr   r)   r   Zcorer   version_infoZPY3strr   Zunicoder,   r#   r%   r'   r2   formatZ
add_formatr   r   r   r   <module>   s*   * Q