U
    %d                      @   s$   d Z ddlmZ G dd deZdS )aM  Read TIFF from FEI SEM microscopes.

Backend Library: internal

This format is based on :mod:`TIFF <imageio.plugins.tifffile>`, and supports the
same parameters. FEI microscopes append metadata as ASCII text at the end of the
file, which this reader correctly extracts.

Parameters
----------
discard_watermark : bool
    If True (default), discard the bottom rows of the image, which
    contain no image data, only a watermark with metadata.
watermark_height : int
    The height in pixels of the FEI watermark. The default is 70.

See Also
--------
    :mod:`imageio.plugins.tifffile`

   )
TiffFormatc                   @   s*   e Zd ZdZdd ZG dd dejZdS )FEISEMFormatz!See :mod:`imageio.plugins.feisem`c                 C   s   dS )NF )selfrequestr   r   :/tmp/pip-unpacked-wheel-x84omsqu/imageio/plugins/feisem.py
_can_write!   s    zFEISEMFormat._can_writec                       s(   e Zd Zd	 fdd	Zd
ddZ  ZS )zFEISEMFormat.Reader    TF   c                    s0   t tj| |\}}|r(|d|  }||fS )a"  Get image and metadata from given index.

            FEI images usually (always?) contain a watermark at the
            bottom of the image, 70 pixels high. We discard this by
            default as it does not contain any information not present
            in the metadata.
            N)superr   Reader	_get_data)r   indexZdiscard_watermarkZwatermark_heightZimmeta	__class__r   r   r   %   s    zFEISEMFormat.Reader._get_dataNc                 C   s  t | dr| jS di i}d}d}| j }t|ddd}|D ]}|sX|dsTq>nd}| }|d	r|d	d
}i ||< q>d|kr>|jddd\}}	t	t
fD ].}
z|
|	}	W n tk
r   Y qY qX  qq|	|| |< q>W 5 Q R X |d st|dkrtd| || _|S )a  Read the metadata from an FEI SEM TIFF.

            This metadata is included as ASCII text at the end of the file.

            The index, if provided, is ignored.

            Returns
            -------
            metadata : dict
                Dictionary of metadata.
            	_fei_metarootFutf8ignore)encodingerrorszDate=T[]=r   )maxsplitz'Input file %s contains no FEI metadata.)hasattrr   r   Zget_local_filenameopen
startswithrstriplstripsplitintfloat
ValueErrorlen)r   r   ZmdZcurrent_tagZreading_metadatafilenameZfinlinekeyvalZtag_typer   r   r   _get_meta_data2   s:    





z"FEISEMFormat.Reader._get_meta_data)r	   Tr
   )N)__name__
__module____qualname__r   r*   __classcell__r   r   r   r   r   $   s   r   N)r+   r,   r-   __doc__r   r   r   r   r   r   r   r      s   r   N)r/   Ztifffiler   r   r   r   r   r   <module>   s   