
    9ib                         S r SSKrSSKrSSKrSSKrSSKrSSKJr  SSK	J
r
  \R                  " \5      rSrSrSrS	rS
rSrSrSr " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)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                   L    \ rS rSrSrSrS r " S S\R                  5      rSr	g)	LytroFormat>   zBase 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                     g)NF selfrequests     U/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/imageio/plugins/lytro.py
_can_writeLytroFormat._can_writeI   s        c                   0    \ rS rSrSS jrS rS rS rSrg)	LytroFormat.WriterO   c                 B    U R                   R                  5       U l        g N)r   get_file_fp)r   flagss     r   _openLytroFormat.Writer._openP   s    ||,,.DHr   c                     g r   r   r   s    r   _closeLytroFormat.Writer._closeS   s     r   c                     [        S5      e)Nz)The lytro format cannot write image data.RuntimeError)r   immetas      r   _append_dataLytroFormat.Writer._append_dataX   s    JKKr   c                     [        S5      e)Nz(The lytro format cannot write meta data.r)   )r   r,   s     r   _set_meta_data!LytroFormat.Writer._set_meta_data\   s     IJJr   )r    N)r   )	__name__
__module____qualname____firstlineno__r"   r&   r-   r0   __static_attributes__r   r   r   Writerr   O   s    	/	
	L	Kr   r7   r   N)
r2   r3   r4   r5   __doc___modesr   r   r7   r6   r   r   r   r   r   >   s%     FK Kr   r   c                   X    \ rS rSrSrS r\S 5       r " S S\R                  5      r	Sr
g)	LytroIllumRawFormatb   a}  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                 &    UR                   S;   a  gg N)z.rawT	extensionr   s     r   	_can_readLytroIllumRawFormat._can_readp       	) *r   c                 n   U SS S2   nU SS S2   nU SS S2   nU SS S2   nU SS S2   n[         R                  " US5      [         R                  " US5      -   n[         R                  " US5      [         R                  " [         R                  " US5      S5      -   n[         R                  " US5      [         R                  " [         R                  " US5      S5      -   n[         R                  " US5      [         R                  " [         R                  " US	5      S
5      -   n[         R                  " [
        [         R                  S9nUR                  [
        S   [
        S   S-  45      US S 2SS S24'   UR                  [
        S   [
        S   S-  45      US S 2SS S24'   UR                  [
        S   [
        S   S-  45      US S 2SS S24'   UR                  [
        S   [
        S   S-  45      US S 2SS S24'   [         R                  " US5      R                  [         R                  5      $ )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_bits"LytroIllumRawFormat.rearrange_bitsu   s    14a4[14a4[14a4[14a4[ADqDk]]2q!BNN3$::]]2q!BNN2>>#r3JA$NN]]2q!BNN2>>#r3JA$NN]]2q!BNN2>>#s3KQ$OO/ryyA#A&(>q(AQ(FG
aAg #A&(>q(AQ(FG
aAg #A&(>q(AQ(FG
aAg #A&(>q(AQ(FG
aAg yy'..rzz::r   c                   6    \ rS rSrS	S jrS rS rS rS rSr	g)
LytroIllumRawFormat.Reader   c                 \    U R                   R                  5       U l        S U l        Xl        g r   r   r   _file_data
_meta_onlyr   	meta_onlys     r   r"    LytroIllumRawFormat.Reader._open   "    ..0DJDJ'Or   c                     U ? g r   rf   r%   s    r   r&   !LytroIllumRawFormat.Reader._close   	     
r   c                     gNrF   r   r%   s    r   _get_length&LytroIllumRawFormat.Reader._get_length       r   c                    US;  a  [        S5      eU R                  (       d  U R                  c  U R                  R	                  5       U l        [
        R                  " U R                  [
        R                  S9R                  [
        R                  5      n[        R                  U5      nO[
        R                  " / 5      nX0R                  SS94$ N)r   Nonez$Lytro file contains only one datasetrJ   r   )index)
IndexErrorrg   rf   re   readrL   
frombufferuint8rU   rR   r;   r^   rW   _get_meta_datar   rx   rawimgs       r   	_get_data$LytroIllumRawFormat.Reader._get_data   s     K' !GHH??::%!%!2DJ mmDJJbhh?FFryyQ *88= hhrl ++!+444r   c                 x   US;  a  [        S5      e[        R                  R                  U R                  R                  5       5      S   nS nS HL  n[        R                  R                  X$-   5      (       d  M+  [        R                  " [        X$-   5      5      nMN     Ub  U$ [        R                  S5        0 $ Nr   N.Lytro meta data file contains only one datasetr   )z.txtz.TXTz.jsonz.JSONz-No metadata file found for provided raw file.ry   ospathsplitextr   get_local_filenameisfilejsonloadopenloggerwarningr   rx   filename_base	meta_dataexts        r   r}   )LytroIllumRawFormat.Reader._get_meta_data        I% !QRR GG,,T\\-L-L-NOPQRMI977>>-"566 $		$}/B*C DI : $   NO	r   rf   re   rg   NF
r2   r3   r4   r5   r"   r&   rr   r   r}   r6   r   r   r   Readerra      s    	(
	
		50	r   r   r   Nr2   r3   r4   r5   r8   rA   staticmethodr^   r   r   r6   r   r   r   r;   r;   b   s1    
 ; ;D= =r   r;   c                   H    \ rS rSrSrS r " S S\R                  5      rSrg)LytroLfrFormat   a  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                 &    UR                   S;   a  gg )N)z.lfrTr?   r   s     r   rA   LytroLfrFormat._can_read   rC   r   c                   N    \ rS rSrSS jrS rS rS rS rS r	S r
S	 rS
 rSrg)LytroLfrFormat.Reader   c                    U R                   R                  5       U l        S U l        0 U l        0 U l        S U l        Xl        X l        U R                  5         U R                  5         U R                  5          U R                  S   S   S   nUS   U R                  ;   Ga  US   U R                  ;   Ga  US   U R                  ;   Gal  U R                  (       dP  U R                  US      u  pEU R                  R                  US5        U R                  R                  U5      U l        U R                  US      u  pEU R                  R                  US5        U R                  R                  U5      n[        R                   " UR#                  S5      5      U R
                  S'   U R                  US      u  pEU R                  R                  US5        U R                  R                  U5      n[        R                   " UR#                  S5      5      U l        U R$                  U R
                  S	'   U R                  (       a  U R                  S
   S   nUS   U R                  ;   as  U R                  US      u  pEU R                  R                  US5        U R                  R                  U5      n['        USS9n	US   n
US   nU	U
US.U R
                  S'   g g g ! [(         a    [+        S5      ef = f)Nframesr   framemetadataRefimageRefprivateMetadataRefASCIImetadataprivateMetadata
thumbnailsjpeg)formatheightwidth)r]   r   r   	thumbnailz+The specified file is not a valid LFR file.)r   r   re   rf   _chunksr   _contentrg   _include_thumbnail_find_header_find_chunks
_find_metaseekrz   raw_image_datar   loadsdecodeserial_numbersr   KeyErrorr*   )r   ri   include_thumbnail
chunk_dictdata_possizer   r   thumbnail_datathumbnail_imgthumbnail_heightthumbnail_widths               r   r"   LytroLfrFormat.Reader._open   s   ..0DJDJDLDM DM'O&7#OO3R!]]84Q7@
}-=":.$,,>"#78DLLH??)-j6L)M

!4.2jjood.C+ &*\\*]2K%LNHJJOOHa0#zzt4H04

8??7;S0TDMM*- &*\\*=Q2R%SNHJJOOHa0%)ZZ__T%:N*.**^5J5J75S*TD'7;7J7JDMM"34 **!%|!<Q!?J!*-=)-j6L)M

!4)-)>(.~f(M+5h+?(*4W*= &3&6%46k2 > +(  R"#PQQRs   9I%K! !K7c                     U ? g r   rm   r%   s    r   r&   LytroLfrFormat.Reader._close1  ro   r   c                     grq   r   r%   s    r   rr   !LytroLfrFormat.Reader._get_length6  rt   r   c                     SnU R                   R                  [        5      nX!:w  a  [        S5      eU R                   R                  [        5        g)0
Checks if file has correct header and skip it.
   LFP

   zThe LFR file header is invalid.Nre   rz   HEADER_LENGTHr*   SIZE_LENGTHr   file_headerheaders      r   r   "LytroLfrFormat.Reader._find_header:  s@     EKZZ__]3F$"#DEE JJOOK(r   c                 ~    Sn[        S[        5       H'  nU R                  U5      u  p4nX44U R                  U'   M)     gz6
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   "LytroLfrFormat.Reader._find_chunksG  s@     FL1/0'+|'D$&.%5T" 1r   c                     SnU R                  U5      u  p#nU R                  R                  US5        U R                  R                  U5      n[        R
                  " UR                  S5      5      U l        g)P
Gets a data chunk that contains information over content
of other data chunks.
   LFM

    r   r   Nr   re   r   rz   r   r   r   r   r   meta_headerr   r   r   datas         r   r    LytroLfrFormat.Reader._find_metaQ  s\    
 EK#'??;#? HD JJOOHa(::??4(D JJt{{7';<DMr   c                    U R                   R                  [        5      nX!:w  a  [        S5      eSnSn[        R
                  " SU R                   R                  [        5      5      S   nUS:  a  [        U R                   R                  [        5      R                  S5      5      nU R                   R                  [        5        U R                   R                  5       nU R                   R                  US5        U R                   R                  S5      nUS:X  a#  U R                   R                  S5      nUS:X  a  M#  U R                   R                  SS5        X5U4$ )	  
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   rF       re   rz   r   r*   structunpackr   strSHA1_LENGTHr   PADDING_LENGTHtellr   r   r   header_chunkr   r   r   chs          r   r    LytroLfrFormat.Reader._get_chunk^     *  ::??=9L%"#EFFHD ==tzz{'CDQGDax4::??;7>>wGH

/::??,

a(ZZ__Q'Ek+B Ek

A&4''r   c                 L   US;  a  [        S5      eU R                  (       d`  [        R                  " U R                  [        R
                  S9R                  [        R                  5      n[        R                  U5      nO[        R                  " / 5      nX0R                  4$ )Nr   z(Lytro lfr file contains only one datasetrJ   )ry   rg   rL   r{   r   r|   rU   rR   r;   r^   rW   r   r   rx   r   r+   s       r   r   LytroLfrFormat.Reader._get_data  sw    I% !KLL??mmD$7$7rxxHOOII )77<XXb\ }}$$r   c                 <    US;  a  [        S5      eU R                  $ Nr   r   ry   r   r   rx   s     r   r}   $LytroLfrFormat.Reader._get_meta_data  "     I% !QRR== r   )	r   r   rf   re   r   rg   r   r   r   N)FTr2   r3   r4   r5   r"   r&   rr   r   r   r   r   r   r}   r6   r   r   r   r   r      s3    @	RD	
		)	6	=,	(\	%"	!r   r   r   N	r2   r3   r4   r5   r8   rA   r   r   r6   r   r   r   r   r      s    u! u!r   r   c                   X    \ rS rSrSrS r\S 5       r " S S\R                  5      r	Sr
g)	LytroF01RawFormati  a  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                 &    UR                   S;   a  gg r>   r?   r   s     r   rA   LytroF01RawFormat._can_read  rC   r   c                    U SS S2   nU SS S2   nU SS S2   n[         R                  " US5      [         R                  " [         R                  " US5      S5      -   n[         R                  " [         R                  " US5      S5      U-   n[         R                  " [
        [         R                  S	9nUR                  [
        S   [
        S   S-  45      US S 2SS S24'   UR                  [
        S   [
        S   S-  45      US S 2SS S24'   [         R                  " US
5      R                  [         R                  5      $ )Nr   r   rF   r   r            rJ   g     @)rL   rM   rO   rN   rP   LYTRO_F01_IMAGE_SIZErR   rS   rT   rU   rV   )rW   rX   rY   rZ   a0a1r]   s          r   r^    LytroF01RawFormat.rearrange_bits  s    14a4[14a4[14a4[]]2q!BNN2>>"c3JA$NN]]2>>"b115:-RYY?!!$&:1&=&BC
aAg !!$&:1&=&BC
aAg yy'..rzz::r   c                   6    \ rS rSrS	S jrS rS rS rS rSr	g)
LytroF01RawFormat.Readeri  c                 \    U R                   R                  5       U l        S U l        Xl        g r   rd   rh   s     r   r"   LytroF01RawFormat.Reader._open  rk   r   c                     U ? g r   rm   r%   s    r   r&   LytroF01RawFormat.Reader._close  ro   r   c                     grq   r   r%   s    r   rr   $LytroF01RawFormat.Reader._get_length  rt   r   c                    US;  a  [        S5      eU R                  (       d  U R                  c  U R                  R	                  5       U l        [
        R                  " U R                  [
        R                  S9R                  [
        R                  5      n[        R                  U5      nO[
        R                  " / 5      nX0R                  SS94$ rv   )ry   rg   rf   re   rz   rL   r{   r|   rU   rR   r  r^   rW   r}   r~   s       r   r   "LytroF01RawFormat.Reader._get_data  s     K' !GHH??::%!%!2DJ mmDJJbhh?FFryyQ (66s; hhrl ++!+444r   c                 x   US;  a  [        S5      e[        R                  R                  U R                  R                  5       5      S   nS nS HL  n[        R                  R                  X$-   5      (       d  M+  [        R                  " [        X$-   5      5      nMN     Ub  U$ [        R                  S5        0 $ r   r   r   s        r   r}   'LytroF01RawFormat.Reader._get_meta_data  r   r   r   Nr   r   r   r   r   r   r    s    	(
	
		5.	r   r   r   Nr   r   r   r   r  r    s0    
 ; ;0< <r   r  c                   H    \ rS rSrSrS r " S S\R                  5      rSrg)LytroLfpFormati  a  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                 &    UR                   S;   a  gg )N)z.lfpTr?   r   s     r   rA   LytroLfpFormat._can_read!  rC   r   c                   N    \ rS rSrSS jrS rS rS rS rS r	S r
S	 rS
 rSrg)LytroLfpFormat.Readeri(  c                    U R                   R                  5       U l        S U l        0 U l        0 U l        S U l        Xl        U R                  5         U R                  5         U R                  5          U R                  S   S   S   S   nUS   U R                  ;   Ga  US   U R                  ;   Ga  US   U R                  ;   Gam  U R                  (       dP  U R                  US      u  p4U R                  R                  US5        U R                  R                  U5      U l        U R                  US      u  p4U R                  R                  US5        U R                  R                  U5      n[        R                  " UR!                  S5      5      U R
                  S	'   U R                  US      u  p4U R                  R                  US5        U R                  R                  U5      n[        R                  " UR!                  S5      5      U l        U R"                  U R
                  S
'   g g g g ! [$         a    ['        S5      ef = f)Npicture
frameArrayr   r   r   r   r   r   r   r   z+The specified file is not a valid LFP file.)r   r   re   rf   r   r   r   rg   r   r   r   r   rz   r   r   r   r   r   r   r*   )r   ri   r   r   r   r   r   s          r   r"   LytroLfpFormat.Reader._open)  s   ..0DJDJDLDM DM'OOOR!]]95lCAFwO
}-=":.$,,>"#78DLLH??)-j6L)M

!4.2jjood.C+ &*\\*]2K%LNHJJOOHa0#zzt4H04

8??7;S0TDMM*- &*\\*=Q2R%SNHJJOOHa0%)ZZ__T%:N*.**^5J5J75S*TD'7;7J7JDMM"34+ I ? >2  R"#PQQRs   3G H7 7Ic                     U ? g r   rm   r%   s    r   r&   LytroLfpFormat.Reader._closeU  ro   r   c                     grq   r   r%   s    r   rr   !LytroLfpFormat.Reader._get_lengthZ  rt   r   c                     SnU R                   R                  [        5      nX!:w  a  [        S5      eU R                   R                  [        5        g)r   r   zThe LFP file header is invalid.Nr   r   s      r   r   "LytroLfpFormat.Reader._find_header^  sB     EK ZZ__]3F$"#DEE JJOOK(r   c                 ~    Sn[        S[        5       H'  nU R                  U5      u  p4nX44U R                  U'   M)     gr   )r   DATA_CHUNKS_F01r   r   r   s         r   r   "LytroLfpFormat.Reader._find_chunksl  s?     FL1o.'+|'D$&.%5T" /r   c                 (   SnU R                  U5      u  p#nU R                  R                  US5        U R                  R                  U5      n[        R
                  " UR                  S5      5      U l        U R                  R                  S5      ng)r   r   r   r   rE   Nr   r   s         r   r    LytroLfpFormat.Reader._find_metav  sn    
 EK#'??;#? HD JJOOHa(::??4(D JJt{{7';<DM::??1%Dr   c                    U R                   R                  [        5      nX!:w  a  [        S5      eSnSn[        R
                  " SU R                   R                  [        5      5      S   nUS:  a  [        U R                   R                  [        5      R                  S5      5      nU R                   R                  [        5        U R                   R                  5       nU R                   R                  US5        U R                   R                  S5      nUS:X  a#  U R                   R                  S5      nUS:X  a  M#  U R                   R                  SS5        X5U4$ )	r   z The LFP chunk header is invalid.Nr   r   r   rF   r   r   r   r   s          r   r    LytroLfpFormat.Reader._get_chunk  r   r   c                 L   US;  a  [        S5      eU R                  (       d`  [        R                  " U R                  [        R
                  S9R                  [        R                  5      n[        R                  U5      nO[        R                  " / 5      nX0R                  4$ )Nr   z(Lytro lfp file contains only one datasetrJ   )ry   rg   rL   r{   r   r|   rU   rR   r  r^   rW   r   r   s       r   r   LytroLfpFormat.Reader._get_data  sw    I% !KLL??mmD$7$7rxxHOOII '55c:XXb\ }}$$r   c                 <    US;  a  [        S5      eU R                  $ r   r   r   s     r   r}   $LytroLfpFormat.Reader._get_meta_data  r  r   )r   r   rf   re   rg   r   r   r   Nr   r  r   r   r   r   r"  (  s2    *	RX	
		)	6	&,	(\	%"	!r   r   r   Nr  r   r   r   r  r    s    b! b!r   r  )r8   r   r   r   loggingnumpyrL   corer   v2r   	getLoggerr2   r   rQ   r  r   r   r   r   r   r.  r   r;   r   r  r  r   r   r   <module>r=     s   8 
       
		8	$ & #   !K& !KHs+ slK![ K!\i iXx![ x!r   