B
    5Z\]+                 @   s   d Z ddlmZmZmZ ddlZddlZddlmZ ddl	m
Z
 ddlZdadd Zd	Zd
ZdZG dd de
ZeddedZee dS )z' Storage of image data in tiff format.
    )absolute_importprint_functiondivisionN   )formats)Formatc              C   sn   t jdk r<ydd l} dd l}W n tk
r:   tdY nX ydd laW n  tk
rh   ddlma Y nX tS )N)   r   ztThe Imageio TIFF format has extra dependencies on Python 2.7. Install these using e.g. "pip install enum34 futures".   )	_tifffile)sysversion_infoenumZconcurrent.futuresImportErrorZtifffiler
    )r   Z
concurrent r   7lib/python3.7/site-packages/imageio/plugins/tifffile.pyload_lib   s    
r   )z.tifz.tiffz.stkz.lsm)	ZphotometricZplanarconfigZ
resolutiondescriptioncompressZvolumeZ
writeshapeZ	extratagsdatetime)Zplanar_configurationZis_fluoviewZis_nihZ	is_contigZis_micromanagerZis_omeZis_lsmis_paletteZ
is_reducedZis_rgbZis_sgiZ	is_shapedZis_stkZis_tiledZis_mdgelresolution_unitZcompressionZ
is_mediacyZorientationr   Zdescription1Z	is_imagejsoftwarec               @   sD   e Zd ZdZdd Zdd ZG dd dejZG dd	 d	ejZd
S )
TiffFormata   Provides support for a wide range of Tiff images.
    
    Images that contain multiple pages can be read using ``imageio.mimread()``
    to read the individual pages, or ``imageio.volread()`` to obtain a
    single (higher dimensional) array.

    Parameters for reading
    ----------------------
    offset : int
        Optional start position of embedded file. By default this is
        the current file position.
    size : int
        Optional size of embedded file. By default this is the number
        of bytes from the 'offset' to the end of the file.
    multifile : bool
        If True (default), series may include pages from multiple files.
        Currently applies to OME-TIFF only.
    multifile_close : bool
        If True (default), keep the handles of other files in multifile
        series closed. This is inefficient when few files refer to
        many pages. If False, the C runtime may run out of resources.

    Parameters for saving
    ---------------------
    bigtiff : bool
        If True, the BigTIFF format is used.
    byteorder : {'<', '>'}
        The endianness of the data in the file.
        By default this is the system's native byte order.
    software : str
        Name of the software used to create the image.
        Saved with the first page only.

    Metadata for reading
    --------------------
    planar_configuration : {'contig', 'planar'}
        Specifies if samples are stored contiguous or in separate planes.
        By default this setting is inferred from the data shape.
        'contig': last dimension contains samples.
        'planar': third last dimension contains samples.
    resolution_unit : (float, float) or ((int, int), (int, int))
        X and Y resolution in dots per inch as float or rational numbers.
    compression : int
        Values from 0 to 9 indicating the level of zlib compression.
        If 0, data is uncompressed.
    orientation : {'top_left', 'bottom_right', ...}
        Oriented of image array.
    is_rgb : bool
        True if page contains a RGB image.
    is_contig : bool
        True if page contains a contiguous image.
    is_tiled : bool
        True if page contains tiled image.
    is_palette : bool
        True if page contains a palette-colored image and not OME or STK.
    is_reduced : bool
        True if page is a reduced image of another image.
    is_shaped : bool
        True if page contains shape in image_description tag.
    is_fluoview : bool
        True if page contains FluoView MM_STAMP tag.
    is_nih : bool
        True if page contains NIH image header.
    is_micromanager : bool
        True if page contains Micro-Manager metadata.
    is_ome : bool
        True if page contains OME-XML in image_description tag.
    is_sgi : bool
        True if page contains SGI image and tile depth tags.
    is_stk : bool
        True if page contains UIC2Tag tag.
    is_mdgel : bool
        True if page contains md_file_tag tag.
    is_mediacy : bool
        True if page contains Media Cybernetics Id tag.
    is_stk : bool
        True if page contains UIC2Tag tag.
    is_lsm : bool
        True if page contains LSM CZ_LSM_INFO tag.
    description : str
        Image description
    description1 : str
        Additional description
    is_imagej : None or str
        ImageJ metadata
    software : str
        Software used to create the TIFF file
    datetime : datetime.datetime
        Creation date and time

    Metadata for writing
    --------------------
    photometric : {'minisblack', 'miniswhite', 'rgb'}
        The color space of the image data.
        By default this setting is inferred from the data shape.
    planarconfig : {'contig', 'planar'}
        Specifies if samples are stored contiguous or in separate planes.
        By default this setting is inferred from the data shape.
        'contig': last dimension contains samples.
        'planar': third last dimension contains samples.
    resolution : (float, float) or ((int, int), (int, int))
        X and Y resolution in dots per inch as float or rational numbers.
    description : str
        The subject of the image. Saved with the first page only.
    compress : int
        Values from 0 to 9 controlling the level of zlib compression.
        If 0, data are written uncompressed (default).
    volume : bool
        If True, volume data are stored in one tile (if applicable) using
        the SGI image_depth and tile_depth tags.
        Image width and depth must be multiple of 16.
        Few software can read this format, e.g. MeVisLab.
    writeshape : bool
        If True, write the data shape to the image_description tag
        if necessary and no other description is given.
    extratags: sequence of tuples
        Additional tags as [(code, dtype, count, value, writeonce)].

        code : int
            The TIFF tag Id.
        dtype : str
            Data type of items in 'value' in Python struct format.
            One of B, s, H, I, 2I, b, h, i, f, d, Q, or q.
        count : int
            Number of data values. Not used for string values.
        value : sequence
            'Count' values compatible with 'dtype'.
        writeonce : bool
            If True, the tag is written to the first page only.
    c             C   s   |j | jkS )N)	extension
extensions)selfrequestr   r   r   	_can_read   s    zTiffFormat._can_readc             C   s   |j | jkS )N)r   r   )r   r   r   r   r   
_can_write   s    zTiffFormat._can_writec               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )zTiffFormat.Readerc             K   sZ   t s
t  | jjdr0t| j d | _}nd | _| j }t j	|f|| _
i | _d S )N)zhttp://zhttps://rb)r
   r   r   filename
startswithopenZget_local_filename_fget_fileZTiffFile_tf_meta)r   kwargsfr   r   r   _open   s    
zTiffFormat.Reader._openc             C   s"   | j   | jd k	r| j  d S )N)r$   closer"   )r   r   r   r   _close   s    

zTiffFormat.Reader._closec             C   s$   | j jd dkrdS t| jjS d S )Nr	   vV)r   modelenr$   pages)r   r   r   r   _get_length   s    zTiffFormat.Reader._get_lengthc             C   sv   | j jd dkr2|dkr td| j }| j}n<|dk sF||  krNtd| jj|  }| jpl| |}||fS )Nr	   r+   r   z-Tiff support no more than 1 "volume" per filez/Index out of range while reading from tiff file)	r   r,   
IndexErrorr$   Zasarrayr%   r/   r.   _get_meta_data)r   indeximmetar   r   r   	_get_data   s    
zTiffFormat.Reader._get_datac          	   C   s   | j j|pd }x6tD ].}yt||| j|< W q tk
rB   Y qX qW xDdD ]<}y"tj|j| j	d| jd< P W qN tk
r   Y qNX qNW | jS )Nr   )r   ZDateTimez%Y:%m:%d %H:%M:%Sr   )
r$   r.   READ_METADATA_KEYSgetattrr%   	Exceptionr   ZstrptimeZtagsvalue)r   r2   Zpagekeyr   r   r   r1     s    



z TiffFormat.Reader._get_meta_dataN)__name__
__module____qualname__r(   r*   r/   r5   r1   r   r   r   r   Reader   s
   r>   c               @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )zTiffFormat.WriterNc             C   sj   t s
t  y$t j| j |||d| _d | _W n0 tk
r^   t | j ||| _|| _Y nX i | _d S )N)r   )	r
   r   Z
TiffWriterr   r#   r$   	_software	TypeErrorr%   )r   Zbigtiff	byteorderr   r   r   r   r(     s    
zTiffFormat.Writer._openc             C   s   | j   d S )N)r$   r)   )r   r   r   r   r*   .  s    zTiffFormat.Writer._closec             C   sX   |r|  | | jd kr2| jjt|f| j n"| jjt|fd| ji| j d S )Nr   )set_meta_datar?   r$   ZsavenpZ
asanyarrayr%   )r   r3   r4   r   r   r   _append_data1  s
    

zTiffFormat.Writer._append_datac             C   s2   i | _ x&| D ]\}}|tkr|| j |< qW d S )N)r%   itemsWRITE_METADATA_KEYS)r   r4   r:   r9   r   r   r   rB   <  s    zTiffFormat.Writer.set_meta_data)NNN)r;   r<   r=   r(   r*   rD   rB   r   r   r   r   Writer  s   
rG   N)	r;   r<   r=   __doc__r   r   r   r>   rG   r   r   r   r   r   M   s    ?r   ZtiffzTIFF formatZiIvV)rH   Z
__future__r   r   r   r   r   r   r   Zcorer   ZnumpyrC   r
   r   ZTIFF_FORMATSrF   r6   r   formatZ
add_formatr   r   r   r   <module>   s    x