ó
;c›]c           @` sƒ  d  Z  d d l m Z m Z m 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 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 d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ i e d 6e d 6e d 6e d 6Z  d. d/ d0 d1 g Z! d+ „  Z" e" ƒ  d S(2   s    Lytro Illum Plugin.
    Plugin to read Lytro Illum .lfr and .raw files as produced
    by the Lytro Illum light field camera.
i    (   t   absolute_importt   print_functiont   divisionNi   (   t   formats(   t   Format(   t   imreadiø  i0  iÐ  i   i   i-   i#   i   i   t   LytroFormatc           B` s6   e  Z d  Z d Z d „  Z d e j f d „  ƒ  YZ RS(   s   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.
    t   ic         C` s   t  S(   N(   t   False(   t   selft   request(    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyt
   _can_write>   s    t   Writerc           B` s/   e  Z d  d „ Z d „  Z d „  Z d „  Z RS(   i    c         C` s   |  j  j ƒ  |  _ d  S(   N(   R
   t   get_filet   _fp(   R	   t   flags(    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyt   _openE   s    c         C` s   d  S(   N(    (   R	   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyt   _closeH   s    c         C` s   t  d ƒ ‚ d  S(   Ns)   The lytro format cannot write image data.(   t   RuntimeError(   R	   t   imt   meta(    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyt   _append_dataM   s    c         C` s   t  d ƒ ‚ d  S(   Ns(   The lytro format cannot write meta data.(   R   (   R	   R   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyt   _set_meta_dataQ   s    (   t   __name__t
   __module__R   R   R   R   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR   D   s   		(   R   R   t   __doc__t   _modesR   R   R   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR   3   s   	t   LytroIllumRawFormatc           B` s?   e  Z d  Z d „  Z e d „  ƒ Z d e j f d „  ƒ  YZ RS(   sk   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
    ----------------------
    None
    c         C` s4   | j  d |  j d k r0 | j d k r0 t Sn  d  S(   Ni   t   ?s   .raw(   s   .raw(   t   modet   modest	   extensiont   True(   R	   R
   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyt	   _can_readd   s    c         C` s'  |  d d  d … } |  d d  d … } |  d d  d … } |  d d  d … } |  d d  d … } t  j | d ƒ t  j | d ƒ } t  j | d ƒ t  j t  j | d ƒ d ƒ } t  j | d ƒ t  j t  j | d ƒ d ƒ } t  j | d ƒ t  j t  j | d	 ƒ d
 ƒ } t  j t d t  j ƒ} | j t d t d d f ƒ | d  d  … d d  d … f <| j t d t d d f ƒ | d  d  … d d  d … f <| j t d t d d f ƒ | d  d  … d d  d … f <| j t d t d d f ƒ | d  d  … d d  d … f <t  j | d ƒ j	 t  j
 ƒ S(   Ni    i   i   i   i   i   i   i0   iÀ   i   t   dtypeg     ø@(   t   npt
   left_shiftt   bitwise_andt   right_shiftt   zerost   LYTRO_ILLUM_IMAGE_SIZEt   uint16t   reshapet   dividet   astypet   float64(   t   arrayt   t0t   t1t   t2t   t3t   lsbt   image(    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyt   rearrange_bitsj   s&    "...4444t   Readerc           B` s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C` s   |  j  j ƒ  |  _ d  |  _ d  S(   N(   R
   R   t   _filet   Nonet   _data(   R	   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR   Ž   s    c         C` s
   |  `  d  S(   N(   R9   (   R	   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR   ’   s    c         C` s   d S(   Ni   (    (   R	   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyt   _get_length—   s    c         C` s‹   | d k r t  d ƒ ‚ n  |  j d  k r? |  j j ƒ  |  _ n  t j |  j d t j ƒj t j	 ƒ } t
 j | ƒ } | |  j d d ƒ f S(   Ni    R8   s$   Lytro file contains only one datasetR"   t   index(   i    R8   (   t
   IndexErrorR9   R8   R7   t   readR#   t
   frombuffert   uint8R,   R)   R   R5   t   _get_meta_data(   R	   R;   t   rawt   img(    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyt	   _get_data›   s    'c         C` s´   | d k r t d ƒ ‚ n  t j j |  j j ƒ  ƒ d } d  } xL d d d d g D]8 } t j j | | ƒ rS t j	 t
 | | ƒ ƒ } qS qS W| d  k	 rŸ | St j d ƒ i  Sd  S(	   Ni    s.   Lytro meta data file contains only one datasets   .txts   .TXTs   .jsons   .JSONs-   No metadata file found for provided raw file.(   i    N(   R8   R<   t   ost   patht   splitextR
   t   get_local_filenamet   isfilet   jsont   loadt   opent   loggert   warning(   R	   R;   t   filename_baset	   meta_datat   ext(    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR@   ®   s     (   R   R   R   R   R:   RC   R@   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR6      s
   				(   R   R   R   R!   t   staticmethodR5   R   R6   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR   W   s   	#t   LytroLfrFormatc           B` s0   e  Z d  Z d „  Z d e j f d „  ƒ  YZ RS(   s-   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
    ----------------------
    None
    c         C` s4   | j  d |  j d k r0 | j d k r0 t Sn  d  S(   Ni   R   s   .lfr(   s   .lfr(   R   R   R   R    (   R	   R
   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR!   Ó   s    R6   c           B` sY   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   c   
      C` sy  |  j  j ƒ  |  _ d  |  _ i  |  _ i  |  _ d  |  _ |  j ƒ  |  j	 ƒ  |  j
 ƒ  y|  j d d d } | d |  j k r©| d |  j k r©| d |  j k r©|  j | d \ } } |  j j | d ƒ |  j j | ƒ |  _ |  j | d \ } } |  j j | d ƒ |  j j | ƒ } t j | j d ƒ ƒ |  j d <|  j | d \ } } |  j j | d ƒ |  j j | ƒ } t j | j d ƒ ƒ |  _ |  j |  j d	 <n  |  j d
 d } | d |  j k rT|  j | d \ } } |  j j | d ƒ |  j j | ƒ } t | d d ƒ} | d } | d }	 i | d 6| d 6|	 d 6|  j d <n  Wn t k
 rtt d ƒ ‚ n Xd  S(   Nt   framesi    t   framet   metadataReft   imageReft   privateMetadataReft   ASCIIt   metadatat   privateMetadatat
   thumbnailst   formatt   jpegt   heightt   widthR4   t	   thumbnails+   The specified file is not a valid LFR file.(   R
   R   R7   R8   R9   t   _chunksRY   t   _contentt   _find_headert   _find_chunkst
   _find_metat   seekR=   t   raw_image_dataRI   t   loadst   decodet   serial_numbersR   t   KeyErrorR   (
   R	   t
   chunk_dictt   data_post   sizeRY   Rj   t   thumbnail_datat   thumbnail_imgt   thumbnail_heightt   thumbnail_width(    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR   Ü   sN    				




c         C` s
   |  `  d  S(   N(   R9   (   R	   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR     s    c         C` s   d S(   Ni   (    (   R	   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR:      s    c         C` sG   d } |  j  j t ƒ } | | k r3 t d ƒ ‚ n  |  j  j t ƒ d S(   sH   
            Checks if file has correct header and skip it.
            s   ‰LFP

   s   The LFR file header is invalid.N(   R7   R=   t   HEADER_LENGTHR   t   SIZE_LENGTH(   R	   t   file_headert   header(    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyRc   $  s
    c         C` sO   d } xB t  d t ƒ D]1 } |  j | ƒ \ } } } | | f |  j | <q Wd S(   sN   
            Gets start position and size of data chunks in file.
            s   ‰LFC

    i    N(   t   ranget   DATA_CHUNKS_ILLUMt
   _get_chunkRa   (   R	   t   chunk_headerR   Rm   Rn   t   sha1(    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyRd   1  s    c         C` sb   d } |  j  | ƒ \ } } } |  j j | d ƒ |  j j | ƒ } t j | j d ƒ ƒ |  _ d S(   st   
            Gets a data chunk that contains information over content
            of other data chunks.
            s   ‰LFM

    i    RX   N(   Ry   R7   Rf   R=   RI   Rh   Ri   Rb   (   R	   t   meta_headerRm   Rn   R{   t   data(    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyRe   ;  s
    c         C` s  |  j  j t ƒ } | | k r- t d ƒ ‚ n  d } d } t j d |  j  j t ƒ ƒ d } | d k rt |  j  j t	 ƒ j
 d ƒ ƒ } |  j  j t ƒ |  j  j ƒ  } |  j  j | d ƒ |  j  j d ƒ } x" | d k rð |  j  j d ƒ } qÏ W|  j  j d d ƒ n  | | | f S(	   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.
            s    The LFR chunk header is invalid.s   >ii    RX   i   s    iÿÿÿÿN(   R7   R=   Rs   R   R8   t   structt   unpackRt   t   strt   SHA1_LENGTHRi   t   PADDING_LENGTHt   tellRf   (   R	   Rv   t   header_chunkRm   R{   Rn   t   ch(    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyRy   H  s     "!c         C` s^   | d k r t d ƒ ‚ n  t j |  j d t j ƒj t j ƒ } t j	 | ƒ } | |  j
 f S(   Ni    s(   Lytro lfr file contains only one datasetR"   (   i    N(   R8   R<   R#   R>   Rg   R?   R,   R)   R   R5   RY   (   R	   R;   RA   R   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyRC   v  s
    'c         C` s"   | d k r t d ƒ ‚ n  |  j S(   Ni    s.   Lytro meta data file contains only one dataset(   i    N(   R8   R<   RY   (   R	   R;   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR@   ‚  s    (   R   R   R   R   R:   Rc   Rd   Re   Ry   RC   R@   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR6   Û   s   	?				
		.	(   R   R   R   R!   R   R6   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyRR   Ç   s   
	t   LytroF01RawFormatc           B` s?   e  Z d  Z d „  Z e d „  ƒ Z d e j f d „  ƒ  YZ RS(   s†   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
    ----------------------
    None

    c         C` s4   | j  d |  j d k r0 | j d k r0 t Sn  d  S(   Ni   R   s   .raw(   s   .raw(   R   R   R   R    (   R	   R
   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR!   ™  s    c         C` s1  |  d d  d … } |  d d  d … } |  d d  d … } t  j | d ƒ t  j t  j | d ƒ d ƒ } t  j t  j | d ƒ d ƒ | } t  j t d	 t  j ƒ} | j t d t d d f ƒ | d  d  … d d  d … f <| j t d t d d f ƒ | d  d  … d d  d … f <t  j | d
 ƒ j	 t  j
 ƒ S(   Ni    i   i   i   i   ið   i   i   R"   g     þ¯@(   R#   R$   R&   R%   R'   t   LYTRO_F01_IMAGE_SIZER)   R*   R+   R,   R-   (   R.   R/   R0   R1   t   a0t   a1R4   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR5   Ÿ  s    ."44R6   c           B` s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C` s   |  j  j ƒ  |  _ d  |  _ d  S(   N(   R
   R   R7   R8   R9   (   R	   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR   ¹  s    c         C` s
   |  `  d  S(   N(   R9   (   R	   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR   ½  s    c         C` s   d S(   Ni   (    (   R	   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR:   Â  s    c         C` s‹   | d k r t  d ƒ ‚ n  |  j d  k r? |  j j ƒ  |  _ n  t j |  j d t j ƒj t j	 ƒ } t
 j | ƒ } | |  j d d ƒ f S(   Ni    R8   s$   Lytro file contains only one datasetR"   R;   (   i    R8   (   R<   R9   R8   R7   R=   R#   R>   R?   R,   R)   R†   R5   R@   (   R	   R;   RA   RB   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyRC   Æ  s    'c         C` s´   | d k r t d ƒ ‚ n  t j j |  j j ƒ  ƒ d } d  } xL d d d d g D]8 } t j j | | ƒ rS t j	 t
 | | ƒ ƒ } qS qS W| d  k	 rŸ | St j d ƒ i  Sd  S(	   Ni    s.   Lytro meta data file contains only one datasets   .txts   .TXTs   .jsons   .JSONs-   No metadata file found for provided raw file.(   i    N(   R8   R<   RD   RE   RF   R
   RG   RH   RI   RJ   RK   RL   RM   (   R	   R;   RN   RO   RP   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR@   Ù  s     (   R   R   R   R   R:   RC   R@   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR6   ¸  s
   				(   R   R   R   R!   RQ   R5   R   R6   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR†   ‹  s   	t   LytroLfpFormatc           B` s0   e  Z d  Z d „  Z d e j f d „  ƒ  YZ RS(   s+   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
    ----------------------
    None
    c         C` s4   | j  d |  j d k r0 | j d k r0 t Sn  d  S(   Ni   R   s   .lfp(   s   .lfp(   R   R   R   R    (   R	   R
   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR!   þ  s    R6   c           B` sY   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   c         C` sÒ  |  j  j ƒ  |  _ d  |  _ i  |  _ i  |  _ d  |  _ |  j ƒ  |  j	 ƒ  |  j
 ƒ  yZ|  j d d d d } | d |  j k r­| d |  j k r­| d |  j k r­|  j | d \ } } |  j j | d ƒ |  j j | ƒ |  _ |  j | d \ } } |  j j | d ƒ |  j j | ƒ } t j | j d ƒ ƒ |  j d	 <|  j | d \ } } |  j j | d ƒ |  j j | ƒ } t j | j d ƒ ƒ |  _ |  j |  j d
 <n  Wn t k
 rÍt d ƒ ‚ n Xd  S(   Nt   picturet
   frameArrayi    RT   RU   RV   RW   RX   RY   RZ   s+   The specified file is not a valid LFP file.(   R
   R   R7   R8   R9   Ra   RY   Rb   Rc   Re   Rd   Rf   R=   Rg   RI   Rh   Ri   Rj   Rk   R   (   R	   Rl   Rm   Rn   RY   Rj   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR     s6    				


c         C` s
   |  `  d  S(   N(   R9   (   R	   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR   2  s    c         C` s   d S(   Ni   (    (   R	   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR:   7  s    c         C` sG   d } |  j  j t ƒ } | | k r3 t d ƒ ‚ n  |  j  j t ƒ d S(   sH   
            Checks if file has correct header and skip it.
            s   ‰LFP

   s   The LFP file header is invalid.N(   R7   R=   Rs   R   Rt   (   R	   Ru   Rv   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyRc   ;  s
    c         C` sO   d } xB t  d t ƒ D]1 } |  j | ƒ \ } } } | | f |  j | <q Wd S(   sN   
            Gets start position and size of data chunks in file.
            s   ‰LFC

    i    N(   Rw   t   DATA_CHUNKS_F01Ry   Ra   (   R	   Rz   R   Rm   Rn   R{   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyRd   I  s    c         C` st   d } |  j  | ƒ \ } } } |  j j | d ƒ |  j j | ƒ } t j | j d ƒ ƒ |  _ |  j j d ƒ } d S(   st   
            Gets a data chunk that contains information over content
            of other data chunks.
            s   ‰LFM

    i    RX   i   N(   Ry   R7   Rf   R=   RI   Rh   Ri   Rb   (   R	   R|   Rm   Rn   R{   R}   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyRe   S  s    c         C` s  |  j  j t ƒ } | | k r- t d ƒ ‚ n  d } d } t j d |  j  j t ƒ ƒ d } | d k rt |  j  j t	 ƒ j
 d ƒ ƒ } |  j  j t ƒ |  j  j ƒ  } |  j  j | d ƒ |  j  j d ƒ } x" | d k rð |  j  j d ƒ } qÏ W|  j  j d d ƒ n  | | | f S(	   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.
            s    The LFP chunk header is invalid.s   >ii    RX   i   s    iÿÿÿÿN(   R7   R=   Rs   R   R8   R~   R   Rt   R€   R   Ri   R‚   Rƒ   Rf   (   R	   Rv   R„   Rm   R{   Rn   R…   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyRy   b  s     "!c         C` s^   | d k r t d ƒ ‚ n  t j |  j d t j ƒj t j ƒ } t j	 | ƒ } | |  j
 f S(   Ni    s(   Lytro lfp file contains only one datasetR"   (   i    N(   R8   R<   R#   R>   Rg   R?   R,   R)   R†   R5   RY   (   R	   R;   RA   R   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyRC     s
    'c         C` s"   | d k r t d ƒ ‚ n  |  j S(   Ni    s.   Lytro meta data file contains only one dataset(   i    N(   R8   R<   RY   (   R	   R;   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR@   œ  s    (   R   R   R   R   R:   Rc   Rd   Re   Ry   RC   R@   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyR6     s   	+				
		.	(   R   R   R   R!   R   R6   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyRŠ   ò  s   
	s	   lytro-lfrs   lytro-illum-raws	   lytro-lfps   lytro-f01-raws	   LYTRO-LFRs   Lytro Illum lfr image filet   lfrR   s   LYTRO-ILLUM-RAWs   Lytro Illum raw image fileRA   s	   LYTRO-LFPs   Lytro F01 lfp image filet   lfps   LYTRO-F01-RAWs   Lytro F01 raw image filec          C` sg   x` t  D]X \ }  } } } t j |  j ƒ  t ƒ } | r | |  | | | ƒ } t j d | ƒ q q Wd  S(   NR\   (   t   file_formatst   SPECIAL_CLASSESt   gett   lowerR   R   t
   add_format(   t   namet   desRP   R   t   format_classR\   (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyt    _create_predefined_lytro_formats·  s
    (   iø  i0  (   iÐ  iÐ  (   s	   LYTRO-LFRs   Lytro Illum lfr image fileRŽ   R   (   s   LYTRO-ILLUM-RAWs   Lytro Illum raw image fileRA   R   (   s	   LYTRO-LFPs   Lytro F01 lfp image fileR   R   (   s   LYTRO-F01-RAWs   Lytro F01 raw image fileRA   R   (#   R   t
   __future__R    R   R   RD   RI   R~   t   loggingt   numpyR#   t    R   t   coreR   R   t	   getLoggerR   RL   R(   R‡   Rs   Rt   R   R‚   Rx   R   R   R   RR   R†   RŠ   R‘   R   R˜   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/lytro.pyt   <module>	   sD   $pÄg´
		