U
    Z$dÞb  ã                   @   sÂ   d 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 ddl	m
Z
 e e¡ZdZdZdZd	Zd
ZdZdZdZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )a½   Read LFR files (Lytro Illum).

Backend: internal

Plugin to read Lytro Illum .lfr and .raw files as produced
by the Lytro Illum light field camera. It is actually a collection
of plugins, each supporting slightly different keyword arguments

Parameters
----------
meta_only : bool
    Whether to only read the metadata.
include_thumbnail : bool
    (only for lytro-lfr and lytro-lfp)
    Whether to include an image thumbnail in the metadata.

é    Né   )ÚFormat)Úimread)iø  i0  )éÐ  r   é   é   é-   é#   é   é   c                   @   s.   e Zd ZdZdZdd„ ZG dd„ dejƒZdS )ÚLytroFormata  Base class for Lytro format.
    The subclasses LytroLfrFormat, LytroLfpFormat, LytroIllumRawFormat and
    LytroF01RawFormat implement the Lytro-LFR, Lytro-LFP and Lytro-RAW format
    for the Illum and original F01 camera respectively.
    Writing is not supported.
    Úic                 C   s   dS )NF© ©ÚselfÚrequestr   r   ú9/tmp/pip-unpacked-wheel-zwww1ayp/imageio/plugins/lytro.pyÚ
_can_writeI   s    zLytroFormat._can_writec                   @   s.   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
S )zLytroFormat.Writerr   c                 C   s   | j  ¡ | _d S ©N)r   Úget_fileÚ_fp)r   Úflagsr   r   r   Ú_openP   s    zLytroFormat.Writer._openc                 C   s   d S r   r   ©r   r   r   r   Ú_closeS   s    zLytroFormat.Writer._closec                 C   s   t dƒ‚d S )Nz)The lytro format cannot write image data.©ÚRuntimeError)r   ÚimÚmetar   r   r   Ú_append_dataX   s    zLytroFormat.Writer._append_datac                 C   s   t dƒ‚d S )Nz(The lytro format cannot write meta data.r   )r   r   r   r   r   Ú_set_meta_data\   s    z!LytroFormat.Writer._set_meta_dataN)r   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r    r   r   r   r   ÚWriterO   s   
r$   N)r!   r"   r#   Ú__doc__Z_modesr   r   r$   r   r   r   r   r   >   s   r   c                   @   s6   e Zd ZdZdd„ Zedd„ ƒZG dd„ dejƒZdS )	ÚLytroIllumRawFormata¡  This is the Lytro Illum RAW format.
    The raw format is a 10bit image format as used by the Lytro Illum
    light field camera. The format will read the specified raw file and will
    try to load a .txt or .json file with the associated meta data.
    This format does not support writing.


    Parameters for reading
    ----------------------
    meta_only : bool
        Whether to only read the metadata.
    c                 C   s   |j dkrdS d S ©N)z.rawT©Ú	extensionr   r   r   r   Ú	_can_readp   s    
zLytroIllumRawFormat._can_readc                 C   s’  | dd d… }| dd d… }| dd d… }| dd d… }| dd d… }t  |d¡t  |d¡ }t  |d¡t  t  |d¡d¡ }t  |d¡t  t  |d¡d¡ }t  |d¡t  t  |d	¡d
¡ }t jtt jd}| td td d f¡|d d …dd d…f< | td td d f¡|d d …dd d…f< | td td d f¡|d d …dd d…f< | td td d f¡|d d …dd d…f< t  |d¡ 	t j
¡S )Nr   é   é   r   r   r   r   é0   éÀ   é   ©Zdtypeg     ø@)ÚnpÚ
left_shiftÚbitwise_andÚright_shiftÚzerosÚLYTRO_ILLUM_IMAGE_SIZEÚuint16ÚreshapeÚdivideÚastypeÚfloat64)ÚarrayÚt0Út1Út2Út3ZlsbÚimager   r   r   Úrearrange_bitsu   s.       ÿÿÿÿz"LytroIllumRawFormat.rearrange_bitsc                   @   s6   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )zLytroIllumRawFormat.ReaderFc                 C   s   | j  ¡ | _d | _|| _d S r   ©r   r   Ú_fileÚ_dataÚ
_meta_only©r   Ú	meta_onlyr   r   r   r   ™   s    z LytroIllumRawFormat.Reader._openc                 C   s   | ` d S r   ©rE   r   r   r   r   r   ž   s    z!LytroIllumRawFormat.Reader._closec                 C   s   dS ©Nr,   r   r   r   r   r   Ú_get_length£   s    z&LytroIllumRawFormat.Reader._get_lengthc                 C   sl   |dkrt dƒ‚| jsR| jd kr,| j ¡ | _tj| jtjd tj	¡}t
 |¡}n
t g ¡}|| jddfS ©N)r   ÚNonez$Lytro file contains only one datasetr0   r   )Úindex)Ú
IndexErrorrF   rE   rD   Úreadr1   Ú
frombufferÚuint8r:   r7   r&   rB   r<   Ú_get_meta_data©r   rN   ÚrawÚimgr   r   r   Ú	_get_data§   s    

z$LytroIllumRawFormat.Reader._get_datac                 C   st   |dkrt dƒ‚tj | j ¡ ¡d }d }dD ]&}tj || ¡r.t t	|| ƒ¡}q.|d k	rb|S t
 d¡ i S d S ©N©r   Nú.Lytro meta data file contains only one datasetr   )z.txtz.TXTz.jsonz.JSONz-No metadata file found for provided raw file.©rO   ÚosÚpathÚsplitextr   Zget_local_filenameÚisfileÚjsonÚloadÚopenÚloggerÚwarning©r   rN   Úfilename_baseZ	meta_dataÚextr   r   r   rS   ¿   s    
z)LytroIllumRawFormat.Reader._get_meta_dataN)F©r!   r"   r#   r   r   rK   rW   rS   r   r   r   r   ÚReader˜   s
   
ri   N©	r!   r"   r#   r%   r*   ÚstaticmethodrB   r   ri   r   r   r   r   r&   b   s
   
"r&   c                   @   s*   e Zd ZdZdd„ ZG dd„ dejƒZdS )ÚLytroLfrFormata¿  This is the Lytro Illum LFR format.
    The lfr is a image and meta data container format as used by the
    Lytro Illum light field camera.
    The format will read the specified lfr file.
    This format does not support writing.

    Parameters for reading
    ----------------------
    meta_only : bool
        Whether to only read the metadata.
    include_thumbnail : bool
        Whether to include an image thumbnail in the metadata.
    c                 C   s   |j dkrdS d S )N)z.lfrTr(   r   r   r   r   r*   ç   s    
zLytroLfrFormat._can_readc                   @   sV   e Zd Zddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )zLytroLfrFormat.ReaderFTc                 C   sô  | j  ¡ | _d | _i | _i | _d | _|| _|| _|  	¡  |  
¡  |  ¡  z†| jd d d }|d | jkrN|d | jkrN|d | jkrN| jsÂ| j|d  \}}| j |d¡ | j |¡| _| j|d  \}}| j |d¡ | j |¡}t | d¡¡| jd< | j|d  \}}| j |d¡ | j |¡}t | d¡¡| _| j| jd	< | jrÎ| jd
 d }|d | jkrÎ| j|d  \}}| j |d¡ | j |¡}t|dd}	|d }
|d }|	|
|dœ| jd< W n tk
rî   tdƒ‚Y nX d S )NÚframesr   ÚframeÚmetadataRefÚimageRefÚprivateMetadataRefÚASCIIÚmetadataÚprivateMetadataZ
thumbnailsZjpeg)ÚformatÚheightÚwidth)rA   rv   rw   Z	thumbnailz+The specified file is not a valid LFR file.)r   r   rD   rE   Ú_chunksrs   Ú_contentrF   Z_include_thumbnailÚ_find_headerÚ_find_chunksÚ
_find_metaÚseekrP   Úraw_image_datar`   ÚloadsÚdecodeÚserial_numbersr   ÚKeyErrorr   )r   rH   Zinclude_thumbnailÚ
chunk_dictÚdata_posÚsizers   r   Zthumbnail_dataZthumbnail_imgZthumbnail_heightZthumbnail_widthr   r   r   r   ï   s\    ÿþýýzLytroLfrFormat.Reader._openc                 C   s   | ` d S r   rI   r   r   r   r   r   1  s    zLytroLfrFormat.Reader._closec                 C   s   dS rJ   r   r   r   r   r   rK   6  s    z!LytroLfrFormat.Reader._get_lengthc                 C   s0   d}| j  t¡}||kr tdƒ‚| j  t¡ dS )úH
            Checks if file has correct header and skip it.
            ó   ‰LFP

   zThe LFR file header is invalid.N©rD   rP   ÚHEADER_LENGTHr   ÚSIZE_LENGTH©r   Zfile_headerÚheaderr   r   r   rz   :  s
    z"LytroLfrFormat.Reader._find_headerc                 C   s6   d}t dtƒD ]"}|  |¡\}}}||f| j|< qdS ©zN
            Gets start position and size of data chunks in file.
            s   ‰LFC

    r   N)ÚrangeÚDATA_CHUNKS_ILLUMÚ
_get_chunkrx   ©r   Zchunk_headerr   r„   r…   Úsha1r   r   r   r{   G  s    z"LytroLfrFormat.Reader._find_chunksc                 C   sD   d}|   |¡\}}}| j |d¡ | j |¡}t | d¡¡| _dS )út
            Gets a data chunk that contains information over content
            of other data chunks.
            ó   ‰LFM

    r   rr   N©r   rD   r}   rP   r`   r   r€   ry   ©r   Zmeta_headerr„   r…   r’   Údatar   r   r   r|   Q  s
    z LytroLfrFormat.Reader._find_metac                 C   s¸   | j  t¡}||krtdƒ‚d}d}t d| j  t¡¡d }|dkr®t| j  t¡ 	d¡ƒ}| j  t
¡ | j  ¡ }| j  |d¡ | j  d¡}|dkr | j  d¡}qŠ| j  dd¡ |||fS )	á_  
            Checks if chunk has correct header and skips it.
            Finds start position and length of next chunk and reads
            sha1-string that identifies the following data chunk.

            Parameters
            ----------
            header : bytes
                Byte string that identifies start of chunk.

            Returns
            -------
                data_pos : int
                    Start position of data chunk in file.
                size : int
                    Size of data chunk.
                sha1 : str
                    Sha1 value of chunk.
            z The LFR chunk header is invalid.Nú>ir   rr   r,   ó    éÿÿÿÿ©rD   rP   r‰   r   ÚstructÚunpackrŠ   ÚstrÚSHA1_LENGTHr€   ÚPADDING_LENGTHÚtellr}   ©r   rŒ   Zheader_chunkr„   r’   r…   Úchr   r   r   r   ^  s     
z LytroLfrFormat.Reader._get_chunkc                 C   sP   |dkrt dƒ‚| js<tj| jtjd tj¡}t 	|¡}n
t 
g ¡}|| jfS )NrY   z(Lytro lfr file contains only one datasetr0   )rO   rF   r1   rQ   r~   rR   r:   r7   r&   rB   r<   rs   ©r   rN   rU   r   r   r   r   rW   Œ  s    ÿ
zLytroLfrFormat.Reader._get_datac                 C   s   |dkrt dƒ‚| jS ©NrY   rZ   ©rO   rs   ©r   rN   r   r   r   rS     s    z$LytroLfrFormat.Reader._get_meta_dataN)FT©r!   r"   r#   r   r   rK   rz   r{   r|   r   rW   rS   r   r   r   r   ri   î   s   
B
.ri   N©r!   r"   r#   r%   r*   r   ri   r   r   r   r   rl   Ø   s   rl   c                   @   s6   e Zd ZdZdd„ Zedd„ ƒZG dd„ dejƒZdS )	ÚLytroF01RawFormata¼  This is the Lytro RAW format for the original F01 Lytro camera.
    The raw format is a 12bit image format as used by the Lytro F01
    light field camera. The format will read the specified raw file and will
    try to load a .txt or .json file with the associated meta data.
    This format does not support writing.


    Parameters for reading
    ----------------------
    meta_only : bool
        Whether to only read the metadata.

    c                 C   s   |j dkrdS d S r'   r(   r   r   r   r   r*   µ  s    
zLytroF01RawFormat._can_readc                 C   sÞ   | dd d… }| dd d… }| dd d… }t  |d¡t  t  |d¡d¡ }t  t  |d¡d¡| }t jtt jd	}| td td d f¡|d d …dd d…f< | td td d f¡|d d …dd d…f< t  |d
¡ 	t j
¡S )Nr   r   r,   r   r   éð   é   é   r0   g     þ¯@)r1   r2   r4   r3   r5   ÚLYTRO_F01_IMAGE_SIZEr7   r8   r9   r:   r;   )r<   r=   r>   r?   Za0Za1rA   r   r   r   rB   º  s     ÿÿz LytroF01RawFormat.rearrange_bitsc                   @   s6   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )zLytroF01RawFormat.ReaderFc                 C   s   | j  ¡ | _d | _|| _d S r   rC   rG   r   r   r   r   Ô  s    zLytroF01RawFormat.Reader._openc                 C   s   | ` d S r   rI   r   r   r   r   r   Ù  s    zLytroF01RawFormat.Reader._closec                 C   s   dS rJ   r   r   r   r   r   rK   Þ  s    z$LytroF01RawFormat.Reader._get_lengthc                 C   sl   |dkrt dƒ‚| jsR| jd kr,| j ¡ | _tj| jtjd tj	¡}t
 |¡}n
t g ¡}|| jddfS rL   )rO   rF   rE   rD   rP   r1   rQ   rR   r:   r7   r«   rB   r<   rS   rT   r   r   r   rW   â  s    

z"LytroF01RawFormat.Reader._get_datac                 C   st   |dkrt dƒ‚tj | j ¡ ¡d }d }dD ]&}tj || ¡r.t t	|| ƒ¡}q.|d k	rb|S t
 d¡ i S d S rX   r[   re   r   r   r   rS   ù  s    
z'LytroF01RawFormat.Reader._get_meta_dataN)Frh   r   r   r   r   ri   Ó  s
   
ri   Nrj   r   r   r   r   r«   ¦  s
   
r«   c                   @   s*   e Zd ZdZdd„ ZG dd„ dejƒZdS )ÚLytroLfpFormata½  This is the Lytro Illum LFP format.
    The lfp is a image and meta data container format as used by the
    Lytro F01 light field camera.
    The format will read the specified lfp file.
    This format does not support writing.

    Parameters for reading
    ----------------------
    meta_only : bool
        Whether to only read the metadata.
    include_thumbnail : bool
        Whether to include an image thumbnail in the metadata.
    c                 C   s   |j dkrdS d S )N)z.lfpTr(   r   r   r   r   r*   !  s    
zLytroLfpFormat._can_readc                   @   sV   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )zLytroLfpFormat.ReaderFc                 C   sr  | j  ¡ | _d | _i | _i | _d | _|| _|  ¡  |  	¡  |  
¡  z
| jd d d d }|d | jkrL|d | jkrL|d | jkrL| jsÀ| j|d  \}}| j |d¡ | j |¡| _| j|d  \}}| j |d¡ | j |¡}t | d¡¡| jd	< | j|d  \}}| j |d¡ | j |¡}t | d¡¡| _| j| jd
< W n tk
rl   tdƒ‚Y nX d S )NZpictureZ
frameArrayr   rn   ro   rp   rq   rr   rs   rt   z+The specified file is not a valid LFP file.)r   r   rD   rE   rx   rs   ry   rF   rz   r|   r{   r}   rP   r~   r`   r   r€   r   r‚   r   )r   rH   rƒ   r„   r…   rs   r   r   r   r   r   )  s@    ÿþýzLytroLfpFormat.Reader._openc                 C   s   | ` d S r   rI   r   r   r   r   r   U  s    zLytroLfpFormat.Reader._closec                 C   s   dS rJ   r   r   r   r   r   rK   Z  s    z!LytroLfpFormat.Reader._get_lengthc                 C   s0   d}| j  t¡}||kr tdƒ‚| j  t¡ dS )r†   r‡   zThe LFP file header is invalid.Nrˆ   r‹   r   r   r   rz   ^  s
    z"LytroLfpFormat.Reader._find_headerc                 C   s6   d}t dtƒD ]"}|  |¡\}}}||f| j|< qdS r   )rŽ   ÚDATA_CHUNKS_F01r   rx   r‘   r   r   r   r{   l  s    z"LytroLfpFormat.Reader._find_chunksc                 C   sP   d}|   |¡\}}}| j |d¡ | j |¡}t | d¡¡| _| j d¡}dS )r“   r”   r   rr   r+   Nr•   r–   r   r   r   r|   v  s    z LytroLfpFormat.Reader._find_metac                 C   s¸   | j  t¡}||krtdƒ‚d}d}t d| j  t¡¡d }|dkr®t| j  t¡ 	d¡ƒ}| j  t
¡ | j  ¡ }| j  |d¡ | j  d¡}|dkr | j  d¡}qŠ| j  dd¡ |||fS )	r˜   z The LFP chunk header is invalid.Nr™   r   rr   r,   rš   r›   rœ   r£   r   r   r   r   …  s     
z LytroLfpFormat.Reader._get_chunkc                 C   sP   |dkrt dƒ‚| js<tj| jtjd tj¡}t 	|¡}n
t 
g ¡}|| jfS )NrY   z(Lytro lfp file contains only one datasetr0   )rO   rF   r1   rQ   r~   rR   r:   r7   r«   rB   r<   rs   r¥   r   r   r   rW   ³  s    ÿ
zLytroLfpFormat.Reader._get_datac                 C   s   |dkrt dƒ‚| jS r¦   r§   r¨   r   r   r   rS   Ä  s    z$LytroLfpFormat.Reader._get_meta_dataN)Fr©   r   r   r   r   ri   (  s   
,
.ri   Nrª   r   r   r   r   r°     s   r°   )r%   r\   r`   r   ÚloggingZnumpyr1   Úcorer   Zv2r   Ú	getLoggerr!   rc   r6   r¯   r‰   rŠ   r    r¡   r   r±   r   r&   rl   r«   r°   r   r   r   r   Ú<module>   s,   
$v Ol