
    0-Phb                        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j        e          ZdZdZdZd	Zd
ZdZdZdZ G d de          Z G d de          Z G d de          Z G d de          Z G 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                   B    e Zd ZdZdZd Z G 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                     dS )NF selfrequests     U/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/imageio/plugins/lytro.py
_can_writezLytroFormat._can_writeI   s    u    c                   (    e Zd ZddZd Zd Zd ZdS )LytroFormat.Writerr   c                 B    | j                                         | _        d S N)r   get_file_fp)r   flagss     r   _openzLytroFormat.Writer._openP   s    |,,..DHHHr   c                     d S r   r   r   s    r   _closezLytroFormat.Writer._closeS   s	     Dr   c                      t          d          )Nz)The lytro format cannot write image data.RuntimeError)r   immetas      r   _append_datazLytroFormat.Writer._append_dataX   s    JKKKr   c                      t          d          )Nz(The lytro format cannot write meta data.r$   )r   r'   s     r   _set_meta_dataz!LytroFormat.Writer._set_meta_data\   s     IJJJr   N)r   )__name__
__module____qualname__r   r"   r(   r*   r   r   r   Writerr   O   s]        	/ 	/ 	/ 	/	 	 	
	L 	L 	L	K 	K 	K 	K 	Kr   r.   N)r+   r,   r-   __doc___modesr   r   r.   r   r   r   r   r   >   sj          F  K K K K K K K K K Kr   r   c                   T    e Zd ZdZd Zed             Z G 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                     |j         dv rdS d S N)z.rawT	extensionr   s     r   	_can_readzLytroIllumRawFormat._can_readp       	))4 *)r   c                    | dd d         }| dd d         }| dd d         }| dd d         }| dd d         }t          j        |d          t          j        |d          z   }t          j        |d          t          j        t          j        |d          d          z   }t          j        |d          t          j        t          j        |d          d          z   }t          j        |d          t          j        t          j        |d	          d
          z   }t          j        t
          t           j                  }|                    t
          d         t
          d         dz  f          |d d dd df<   |                    t
          d         t
          d         dz  f          |d d dd df<   |                    t
          d         t
          d         dz  f          |d d dd df<   |                    t
          d         t
          d         dz  f          |d d dd df<   t          j        |d          	                    t           j
                  S )Nr         r   r   r   r   0         dtypeg     @)np
left_shiftbitwise_andright_shiftzerosLYTRO_ILLUM_IMAGE_SIZEuint16reshapedivideastypefloat64)arrayt0t1t2t3lsbimages          r   rearrange_bitsz"LytroIllumRawFormat.rearrange_bitsu   s0    14a4[14a4[14a4[14a4[ADqDk]2q!!BN3$:$::]2q!!BN2>#r3J3JA$N$NN]2q!!BN2>#r3J3JA$N$NN]2q!!BN2>#s3K3KQ$O$OO/ryAAA#A&(>q(AQ(FG
 
aaaAg #A&(>q(AQ(FG
 
aaaAg #A&(>q(AQ(FG
 
aaaAg #A&(>q(AQ(FG
 
aaaAg y''..rz:::r   c                   .    e Zd ZddZd Zd Zd Zd ZdS )	LytroIllumRawFormat.ReaderFc                 ^    | j                                         | _        d | _        || _        d S r   r   r   _file_data
_meta_onlyr   	meta_onlys     r   r   z LytroIllumRawFormat.Reader._open   )    ..00DJDJ'DOOOr   c                 
    | ` d S r   rY   r!   s    r   r"   z!LytroIllumRawFormat.Reader._close        


r   c                     dS Nr;   r   r!   s    r   _get_lengthz&LytroIllumRawFormat.Reader._get_length       1r   c                    |dvrt          d          | j        s| j        | j                                        | _        t          j        | j        t
          j                                      t
          j	                  }t                              |          }nt          j        g           }||                     d          fS N)r   Nonez$Lytro file contains only one datasetr?   r   )index)
IndexErrorrZ   rY   rX   readrA   
frombufferuint8rJ   rG   r2   rS   rL   _get_meta_datar   rh   rawimgs       r   	_get_dataz$LytroIllumRawFormat.Reader._get_data   s     K'' !GHHH? #:%!%!2!2DJ mDJbh???FFryQQ *88== hrll ++!+4444r   c                 z   |dvrt          d          t          j                            | j                                                  d         }d }dD ]H}t          j                            ||z             r$t          j        t          ||z                       }I||S t                              d           i 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.ri   ospathsplitextr   get_local_filenameisfilejsonloadopenloggerwarningr   rh   filename_base	meta_dataexts        r   rm   z)LytroIllumRawFormat.Reader._get_meta_data        I%% !QRRR G,,T\-L-L-N-NOOPQRMI9 E E7>>-#"566 E $	$}s/B*C*C D DI$   NOOO	r   NFr+   r,   r-   r   r"   rc   rq   rm   r   r   r   ReaderrU      sd        	( 	( 	( 	(
	 	 	
	 	 		5 	5 	50	 	 	 	 	r   r   N	r+   r,   r-   r/   r7   staticmethodrS   r   r   r   r   r   r2   r2   b   su           
 ; ; \;D= = = = = = = = = =r   r2   c                   >    e Zd ZdZd Z G 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                     |j         dv rdS d S )N)z.lfrTr5   r   s     r   r7   zLytroLfrFormat._can_read   r8   r   c                   F    e Zd ZddZd Zd Zd Zd Zd Zd	 Z	d
 Z
d ZdS )LytroLfrFormat.ReaderFTc                    | j                                         | _        d | _        i | _        i | _        d | _        || _        || _        | 	                                 | 
                                 |                                  	 | j        d         d         d         }|d         | j        v rw|d         | j        v rg|d         | j        v rW| j        sP| j        |d                  \  }}| j                            |d           | j                            |          | _        | j        |d                  \  }}| j                            |d           | j                            |          }t          j        |                    d                    | j        d<   | j        |d                  \  }}| j                            |d           | j                            |          }t          j        |                    d                    | _        | j        | j        d	<   | j        r| j        d
         d         }|d         | j        v r~| j        |d                  \  }}| j                            |d           | j                            |          }t'          |d          }	|d         }
|d         }|	|
|d| j        d<   d S d S d S # t(          $ r t+          d          w xY w)Nframesr   framemetadataRefimageRefprivateMetadataRefASCIImetadataprivateMetadata
thumbnailsjpeg)formatheightwidth)rR   r   r   	thumbnailz+The specified file is not a valid LFR file.)r   r   rX   rY   _chunksr   _contentrZ   _include_thumbnail_find_header_find_chunks
_find_metaseekrj   raw_image_datar|   loadsdecodeserial_numbersr   KeyErrorr%   )r   r\   include_thumbnail
chunk_dictdata_possizer   r   thumbnail_datathumbnail_imgthumbnail_heightthumbnail_widths               r   r   zLytroLfrFormat.Reader._open   s   ..00DJDJDLDM DM'DO&7D#OO3R!]84Q7@
}-==":.$,>>"#78DLHH? D)-j6L)M$
!444.2jood.C.C+ &*\*]2K%LNHdJOOHa000#zt44H04
8??7;S;S0T0TDM*- &*\*=Q2R%SNHdJOOHa000%)Z__T%:%:N*.*^5J5J75S5S*T*TD'7;7JDM"34 * !%|!<Q!?J!*-==)-j6L)M$
!444)-)>)>(.~f(M(M(M+5h+?(*4W*= &3&6%46 6k222 ==$  R R R"#PQQQRs   IK K)c                 
    | ` d S r   r_   r!   s    r   r"   zLytroLfrFormat.Reader._close1  r`   r   c                     dS rb   r   r!   s    r   rc   z!LytroLfrFormat.Reader._get_length6  rd   r   c                     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rX   rj   HEADER_LENGTHr%   SIZE_LENGTHr   file_headerheaders      r   r   z"LytroLfrFormat.Reader._find_header:  sS     EKZ__]33F$$"#DEEE JOOK(((((r   c                     d}t          dt                    D ]'}|                     |          \  }}}||f| j        |<   (dS zN
            Gets start position and size of data chunks in file.
            s   LFC

    r   N)rangeDATA_CHUNKS_ILLUM
_get_chunkr   r   chunk_headerr   r   r   sha1s         r   r   z"LytroLfrFormat.Reader._find_chunksG  sZ     FL1/00 6 6'+|'D'D$$&.%5T""6 6r   c                     d}|                      |          \  }}}| j                            |d           | j                            |          }t	          j        |                    d                    | _        dS )t
            Gets a data chunk that contains information over content
            of other data chunks.
               LFM

    r   r   Nr   rX   r   rj   r|   r   r   r   r   meta_headerr   r   r   datas         r   r   z LytroLfrFormat.Reader._find_metaQ  sn    
 EK#'??;#?#? HdD JOOHa(((:??4((D Jt{{7';';<<DMMMr   c                    | j                             t                    }||k    rt          d          d}d}t	          j        d| j                             t                              d         }|dk    rt          | j                             t                    	                    d                    }| j                             t                     | j                                         }| j                             |d           | j                             d          }|dk    r | j                             d          }|dk     | 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   r   r;       rX   rj   r   r%   structunpackr   strSHA1_LENGTHr   PADDING_LENGTHtellr   r   r   header_chunkr   r   r   chs          r   r   z LytroLfrFormat.Reader._get_chunk^  +   *  :??=99Lv%%"#EFFFHD =tz{'C'CDDQGDaxx4:??;77>>wGGHH
///:??,,
a(((Z__Q''Ekk++B Ekk
A&&&T4''r   c                 *   |dvrt          d          | j        s]t          j        | j        t          j                                      t          j                  }t          	                    |          }nt          j
        g           }|| j        fS )Nrt   z(Lytro lfr file contains only one datasetr?   )ri   rZ   rA   rk   r   rl   rJ   rG   r2   rS   rL   r   r   rh   ro   r&   s       r   rq   zLytroLfrFormat.Reader._get_data  s    I%% !KLLL? "mD$7rxHHHOOI  )77<<Xb\\ t}$$r   c                 6    |dvrt          d          | j        S Nrt   ru   ri   r   r   rh   s     r   rm   z$LytroLfrFormat.Reader._get_meta_data  &     I%% !QRRR= r   N)FTr+   r,   r-   r   r"   rc   r   r   r   r   rq   rm   r   r   r   r   r      s        @	R @	R @	R @	RD	 	 	
	 	 		) 	) 	)	6 	6 	6	= 	= 	=,	( ,	( ,	(\	% 	% 	%"	! 	! 	! 	! 	!r   r   Nr+   r,   r-   r/   r7   r   r   r   r   r   r   r      sc           u! u! u! u! u! u! u! u! u! u!r   r   c                   T    e Zd ZdZd Zed             Z G 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                     |j         dv rdS d S r4   r5   r   s     r   r7   zLytroF01RawFormat._can_read  r8   r   c                    | dd d         }| dd d         }| dd d         }t          j        |d          t          j        t          j        |d          d          z   }t          j        t          j        |d          d          |z   }t          j        t
          t           j        	          }|                    t
          d         t
          d         dz  f          |d d dd df<   |                    t
          d         t
          d         dz  f          |d d dd df<   t          j        |d
          	                    t           j
                  S )Nr   r   r;   r   r            r?   g     @)rA   rB   rD   rC   rE   LYTRO_F01_IMAGE_SIZErG   rH   rI   rJ   rK   )rL   rM   rN   rO   a0a1rR   s          r   rS   z LytroF01RawFormat.rearrange_bits  s8    14a4[14a4[14a4[]2q!!BN2>"c3J3JA$N$NN]2>"b11155:-RY???!!$&:1&=&BC
 
aaaAg !!$&:1&=&BC
 
aaaAg y''..rz:::r   c                   .    e Zd ZddZd Zd Zd Zd ZdS )	LytroF01RawFormat.ReaderFc                 ^    | j                                         | _        d | _        || _        d S r   rW   r[   s     r   r   zLytroF01RawFormat.Reader._open  r]   r   c                 
    | ` d S r   r_   r!   s    r   r"   zLytroF01RawFormat.Reader._close  r`   r   c                     dS rb   r   r!   s    r   rc   z$LytroF01RawFormat.Reader._get_length  rd   r   c                    |dvrt          d          | j        s| j        | j                                        | _        t          j        | j        t
          j                                      t
          j	                  }t                              |          }nt          j        g           }||                     d          fS rf   )ri   rZ   rY   rX   rj   rA   rk   rl   rJ   rG   r   rS   rL   rm   rn   s       r   rq   z"LytroF01RawFormat.Reader._get_data  s     K'' !GHHH? #:%!%!2!2DJ mDJbh???FFryQQ (66s;; hrll ++!+4444r   c                 z   |dvrt          d          t          j                            | j                                                  d         }d }dD ]H}t          j                            ||z             r$t          j        t          ||z                       }I||S t                              d           i S rs   rv   r   s        r   rm   z'LytroF01RawFormat.Reader._get_meta_data  r   r   Nr   r   r   r   r   r   r     sd        	( 	( 	( 	(
	 	 	
	 	 		5 	5 	5.	 	 	 	 	r   r   Nr   r   r   r   r   r     st           
 ; ; \;0< < < < < < < < < <r   r   c                   >    e Zd ZdZd Z G 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                     |j         dv rdS d S )N)z.lfpTr5   r   s     r   r7   zLytroLfpFormat._can_read!  r8   r   c                   F    e Zd ZddZd Zd Zd Zd Zd Zd Z	d	 Z
d
 ZdS )LytroLfpFormat.ReaderFc                    | j                                         | _        d | _        i | _        i | _        d | _        || _        |                                  | 	                                 | 
                                 	 | j        d         d         d         d         }|d         | j        v ry|d         | j        v rk|d         | j        v r]| j        sP| j        |d                  \  }}| j                            |d           | j                            |          | _        | j        |d                  \  }}| j                            |d           | j                            |          }t          j        |                    d                    | j        d	<   | j        |d                  \  }}| j                            |d           | j                            |          }t          j        |                    d                    | _        | j        | j        d
<   d S d S d S d S # t$          $ r t'          d          w xY w)Npicture
frameArrayr   r   r   r   r   r   r   r   z+The specified file is not a valid LFP file.)r   r   rX   rY   r   r   r   rZ   r   r   r   r   rj   r   r|   r   r   r   r   r%   )r   r\   r   r   r   r   r   s          r   r   zLytroLfpFormat.Reader._open)  s/   ..00DJDJDLDM DM'DOOOR!]95lCAFwO
}-==":.$,>>"#78DLHH? D)-j6L)M$
!444.2jood.C.C+ &*\*]2K%LNHdJOOHa000#zt44H04
8??7;S;S0T0TDM*- &*\*=Q2R%SNHdJOOHa000%)Z__T%:%:N*.*^5J5J75S5S*T*TD'7;7JDM"3444/ >=>>HH.  R R R"#PQQQRs   ?F&H- -Ic                 
    | ` d S r   r_   r!   s    r   r"   zLytroLfpFormat.Reader._closeU  r`   r   c                     dS rb   r   r!   s    r   rc   z!LytroLfpFormat.Reader._get_lengthZ  rd   r   c                     d}| j                             t                    }||k    rt          d          | j                             t                     dS )r   r   zThe LFP file header is invalid.Nr   r   s      r   r   z"LytroLfpFormat.Reader._find_header^  sU     EK Z__]33F$$"#DEEE JOOK(((((r   c                     d}t          dt                    D ]'}|                     |          \  }}}||f| j        |<   (dS r   )r   DATA_CHUNKS_F01r   r   r   s         r   r   z"LytroLfpFormat.Reader._find_chunksl  sY     FL1o.. 6 6'+|'D'D$$&.%5T""6 6r   c                 2   d}|                      |          \  }}}| j                            |d           | j                            |          }t	          j        |                    d                    | _        | j                            d          }dS )r   r   r   r   r:   Nr   r   s         r   r   z LytroLfpFormat.Reader._find_metav  s    
 EK#'??;#?#? HdD JOOHa(((:??4((D Jt{{7';';<<DM:??1%%DDDr   c                    | j                             t                    }||k    rt          d          d}d}t	          j        d| j                             t                              d         }|dk    rt          | j                             t                    	                    d                    }| j                             t                     | j                                         }| j                             |d           | j                             d          }|dk    r | j                             d          }|dk     | j                             dd           |||fS )	r   z The LFP chunk header is invalid.Nr   r   r   r;   r   r   r   r   s          r   r   z LytroLfpFormat.Reader._get_chunk  r   r   c                 *   |dvrt          d          | j        s]t          j        | j        t          j                                      t          j                  }t          	                    |          }nt          j
        g           }|| j        fS )Nrt   z(Lytro lfp file contains only one datasetr?   )ri   rZ   rA   rk   r   rl   rJ   rG   r   rS   rL   r   r   s       r   rq   zLytroLfpFormat.Reader._get_data  s    I%% !KLLL? "mD$7rxHHHOOI  '55c::Xb\\ t}$$r   c                 6    |dvrt          d          | j        S r   r   r   s     r   rm   z$LytroLfpFormat.Reader._get_meta_data  r   r   Nr   r   r   r   r   r   r   (  s        *	R *	R *	R *	RX	 	 	
	 	 		) 	) 	)	6 	6 	6	& 	& 	&,	( ,	( ,	(\	% 	% 	%"	! 	! 	! 	! 	!r   r   Nr   r   r   r   r   r     sc           b! b! b! b! b! b! b! b! b! b!r   r   )r/   rw   r|   r   loggingnumpyrA   corer   v2r   	getLoggerr+   r   rF   r   r   r   r   r   r   r  r   r2   r   r   r   r   r   r   <module>r     s   8 
			                    
	8	$	$ & #   !K !K !K !K !K& !K !K !KHs s s s s+ s s slK! K! K! K! K![ K! K! K!\i i i i i i i iXx! x! x! x! x![ x! x! x! x! x!r   