
    0-Ph42                         d Z ddlZddlmZ d Z G d d          Z G d d	e          Z G d
 de          Z G d de          Z	dS )as   Read/Write BSDF files.

Backend Library: internal

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
----------
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).

compression : int
    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).

    N   )Formatc                     ddl m}  G d d|j                  } G d d|j                  } G d d|          } G d	 d
|          }|||g} |j        |fi | }||fS )N   )_bsdfc                   0    e Zd ZdZdZej        Zd Zd Z	dS )-get_bsdf_serializer.<locals>.NDArrayExtensionz9Copy of BSDF's NDArrayExtension but deal with lazy blobs.ndarrayc                 x    t          |j        t          |j                  |                                          S )N)shapedtypedata)dictr   strr   tobytesselfsvs      T/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/imageio/plugins/bsdf.pyencodez4get_bsdf_serializer.<locals>.NDArrayExtension.encode:   s)    agS\\		LLLL    c                     |S N r   s      r   decodez4get_bsdf_serializer.<locals>.NDArrayExtension.decode=   s    Hr   N)
__name__
__module____qualname____doc__namenpr
   clsr   r   r   r   r   NDArrayExtensionr	   4   sG        GGj	M 	M 	M	 	 	 	 	r   r$   c                       e Zd ZdZd Zd ZdS )+get_bsdf_serializer.<locals>.ImageExtensionz>We implement two extensions that trigger on the Image classes.c                 8    t          |j        |j                  S )Narraymeta)r   r)   r*   r   s      r   r   z2get_bsdf_serializer.<locals>.ImageExtension.encodeC   s    agAF3333r   c                 :    t          |d         |d                   S )Nr)   r*   )Imager   s      r   r   z2get_bsdf_serializer.<locals>.ImageExtension.decodeF   s    7QvY///r   N)r   r   r   r    r   r   r   r   r   ImageExtensionr&   @   s8        LL	4 	4 	4	0 	0 	0 	0 	0r   r-   c                       e Zd ZdZeZdS )-get_bsdf_serializer.<locals>.Image2DExtensionimage2dN)r   r   r   r!   Image2Dr#   r   r   r   Image2DExtensionr/   I           r   r2   c                       e Zd ZdZeZdS )-get_bsdf_serializer.<locals>.Image3DExtensionimage3dN)r   r   r   r!   Image3Dr#   r   r   r   Image3DExtensionr5   M   r3   r   r8   ) r   	ExtensionBsdfSerializer)optionsbsdfr$   r-   r2   r8   exts
serializers           r   get_bsdf_serializerr@   1   s    
 
 
 
 
4> 
 
 
0 0 0 0 0 0 0 0    >       >    .0@AD$$T55W55Jr   c                   $    e Zd ZdZd Zd Zd ZdS )r,   zClass 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                 "    || _         || _        d S r   r(   )r   r)   r*   s      r   __init__zImage.__init__]   s    
			r   c                 $   t          | j        t          j                  sk| j        }|d         }t          |t                    s|                                }t          j        ||d                   | _        |d         | j        _        | j        S )Nr   r   )r   r   )
isinstancer)   r"   r
   bytes	get_bytes
frombufferr   )r   r   blobs      r   	get_arrayzImage.get_arraya   sy    $*bj11 	*
AV9DdE** (~~''t1W:>>>DJ zDJzr   c                     | j         S r   )r*   r   s    r   get_metazImage.get_metak   s
    yr   N)r   r   r   r    rC   rJ   rM   r   r   r   r,   r,   W   sK         
        r   r,   c                       e Zd ZdS )r1   Nr   r   r   r   r   r   r1   r1   o           Dr   r1   c                       e Zd ZdS )r7   NrO   r   r   r   r7   r7   s   rP   r   r7   c                   j    e Zd ZdZd Zd Z G d dej                  Z G 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                 p    |j         d         | j        dz   v r|j                            d          rdS d S d S )Nr   ?   BSDFT)modemodes
firstbytes
startswithr   requests     r   	_can_readzBsdfFormat._can_read   sL    <?tzC/00 !,,W55 t	 10 r   c                 X    |j         d         | j        dz   v r|j        | j        v rdS d S d S )Nr   rU   T)rW   rX   	extension
extensionsr[   s     r   
_can_writezBsdfFormat._can_write   s=    <?tzC/00 DO33t 1033r   c                   .    e Zd ZddZd Zd Zd Zd ZdS )BsdfFormat.ReaderNc                 P   | j         j        d d         dk    s
J d            | j         j        dd         dk    s1| j         j        dd         dk    s| j         j        dd         d	k    s	 i }| j         j                            d
          r|dnt	          |          }d|d<   | |d<   n|dnt	          |          }||d<   | |d<   | j                                         }t          |          \  }| _        | j                            |          | _	        t          | j	        t                    r=d| j	        v r4d| j	        v r+t          | j	        d         | j	        d                   | _	        t          | j	        t          t          |j        f          st          d          d S )N   rV   zNot a BSDF file      s	   Mimage2Ds	   Mimage3D      l)zhttp://zhttps://F	lazy_blobload_streamingTr*   r)   z8BSDF file does not look seem to have an image container.)r\   rY   filenamerZ   boolget_filer@   _serializerload_streamrE   r   r,   list
ListStreamRuntimeError)r   random_accessr<   rafiler=   s         r   _openzBsdfFormat.Reader._open   s    <*2A2.'999;L999 '"-@@<*1R40OCC<*1Q3/477 G|$//0GHH 3+3UUm9L9L',$02F())*2TT]8K8K')$02F()<((**D%8%A%A"D$"+0066DL 4<..Rdl**t|++$T\'%:DL<PQQdlUD$/,JKK "Q   r   c                     d S r   r   rL   s    r   _closezBsdfFormat.Reader._close   s    Dr   c                     t          | j        t                    rdS t          | j        t                    rt	          | j                  S | j        j        dk     rt          j        S | j        j        S )Nr   r   )rE   rq   r,   rr   lencountr"   infrL   s    r   _get_lengthzBsdfFormat.Reader._get_length   sa    $,.. qDL$// 4<(((#a''v<%%r   c                 r   |dk     s||                                  k    r&t          d||                                  fz            t          | j        t                    r| j        }nt          | j        t
                    r| j        |         }nq|| j        j        k     rt          d          || j        j        k    r)| j                                         || j        j        k    )| j                                        }t          |t                    r$d|v r d|v rt	          |d         |d                   }t          |t                    r(|	                                |
                                fS t          |          }t          |          dk     r|n|d d         dz   }t          d	|z             )
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 )
get_length
IndexErrorrE   rq   r,   rr   indexnextr   rJ   rM   reprr|   rt   )r   r   image_obrs       r   	_get_datazBsdfFormat.Reader._get_data   s   qyyET__%6%666 3udoo>O>O6PP   $,.. /<DL$// /<. 4<---$?   dl000L%%''' dl000<,,.. 8T**Fh&&x'' '!2HV4DEE(E** H))++X->->-@-@@@NNQ#AA1TcT7U?"#BQ#FGGGr   c                     i S r   r   )r   r   s     r   _get_meta_dataz BsdfFormat.Reader._get_meta_data	  s    Ir   r   )r   r   r   rx   rz   r   r   r   r   r   r   Readerrc      si        *	 *	 *	 *	X	 	 		& 	& 	&#	H #	H #	HJ	 	 	 	 	r   r   c                   (    e Zd ZddZd Zd Zd ZdS )BsdfFormat.Writerr   c                 (   d|i}t          |          \  }| _        | j        j        d         dv rd | _        d| _        d S | j                                        }|                                | _        | j                            || j                   d S )Ncompressionr   ivF)	r@   ro   r\   rW   rq   _writtenrn   rs   save)r   r   r<   r=   rw   s        r   rx   zBsdfFormat.Writer._open  s    $k2G%8%A%A"D$"| #t++# % |,,..#00 %%dDL99999r   c                 L    | j         | j                             d           d S d S )NF)rq   closerL   s    r   rz   zBsdfFormat.Writer._close  s0    
 |'""5))))) ('r   c                    d }| j         j        d         dv rd}nB| j         j        d         dv rd}n+d}|j        dk    s|j        dk    r|j        d         dk    rd}|dv sJ |dk    r*|j        dk    s|j        dk    r|j        d         dk    sJ n)|j        dk    s|j        dk    r|j        d         dk    sJ |dk    rt	          ||          }nt          ||          }| j        N| j        r
J d	            d
| _        | j                                         }| j	        
                    ||           d S | j                            |           d S )Nr   iIr   vV   re   )r   r   z"Cannot write singleton image twiceT)r\   rW   ndimr   r1   r7   rq   r   rn   ro   r   append)r   imr*   r   obrw   s         r   _append_datazBsdfFormat.Writer._append_data#  sz   D| #t++"1%--7a<<BGqLLRXb\Q5F5FD6>>>>qyyw!||1"9J9J9J9Jw!||1"9J9J9J9J qyyR&&R&&|#=NN*NNNN $|,,.. %%dB/////##B'''''r   c                      t          d          )Nz2The BSDF format only supports per-image meta data.)rt   )r   r*   s     r   set_meta_datazBsdfFormat.Writer.set_meta_dataC  s    VWWWr   N)r   )r   r   r   rx   rz   r   r   r   r   r   Writerr     s[        
	: 
	: 
	: 
	:	* 	* 	*	( 	( 	(@	X 	X 	X 	X 	Xr   r   N)	r   r   r   r    r]   ra   r   r   r   r   r   r   rS   rS   w   s        # #J    _ _ _ _ _ _ _ _F6X 6X 6X 6X 6X 6X 6X 6X 6X 6Xr   rS   )
r    numpyr"   corer   r@   r,   r1   r7   rS   r   r   r   <module>r      s  & &P          # # #L       0	 	 	 	 	e 	 	 		 	 	 	 	e 	 	 	MX MX MX MX MX MX MX MX MX MXr   