§
    2?Äjk;  ã                   ó¼   — d Z ddlmZ ddlmZmZmZmZ ddlZddl	Z
ddlZddlmZmZmZ ddlmZmZ ddlmZ d	ej        d
eeef         fd„Z G d„ de¦  «        ZdS )aú  Read/Write TIFF files using tifffile.

.. note::
    To use this plugin you need to have `tifffile
    <https://github.com/cgohlke/tifffile>`_ installed::

        pip install tifffile

This plugin wraps tifffile, a powerful library to manipulate TIFF files. It
superseeds our previous tifffile plugin and aims to expose all the features of
tifffile.

The plugin treats individual TIFF series as ndimages. A series is a sequence of
TIFF pages that, when combined describe a meaningful unit, e.g., a volumetric
image (where each slice is stored on an individual page) or a multi-color
staining picture (where each stain is stored on an individual page). Different
TIFF flavors/variants use series in different ways and, as such, the resulting
reading behavior may vary depending on the program used while creating a
particular TIFF file.

Methods
-------
.. note::
    Check the respective function for a list of supported kwargs and detailed
    documentation.

.. autosummary::
    :toctree:

    TifffilePlugin.read
    TifffilePlugin.iter
    TifffilePlugin.write
    TifffilePlugin.properties
    TifffilePlugin.metadata

Additional methods available inside the :func:`imopen <imageio.v3.imopen>`
context:

.. autosummary::
    :toctree:

    TifffilePlugin.iter_pages

é    )ÚBytesIO)ÚAnyÚDictÚOptionalÚcastNé   )Ú	URI_BYTESÚInitializationErrorÚRequest)ÚImagePropertiesÚPluginV3)Ú	ArrayLikeÚpageÚreturnc                 óf  — i }	 | j         d         j        j        |d<   n# t          $ r |cY S w xY w	 | j         d         j        }| j         d         j        }|d         |d         z  |d         |d         z  f|d<   n5# t          $ r Y n)t          $ r t	          j        dt          ¦  «         Y nw xY w|S )	Ni(  Úresolution_uniti  i  r   é   Ú
resolutionzPIgnoring resolution metadata because at least one direction has a 0 denominator.)ÚtagsÚvalueÚKeyErrorÚZeroDivisionErrorÚwarningsÚwarnÚRuntimeWarning)r   ÚmetadataÚresolution_xÚresolution_ys       úLC:\Projects\Historical\venv\Lib\site-packages\imageio/plugins/tifffile_v3.pyÚ_get_resolutionr    :   sý   € Ø€HðØ&*¤i°¤nÔ&:Ô&@ˆÐ"Ñ#Ð#øÝð ð ð àˆˆˆðøøøð
Ø”y ”~Ô+ˆØ”y ”~Ô+ˆð ˜ŒO˜l¨1œoÑ-Ø˜ŒO˜l¨1œoÑ-ð"
ˆ�ÑÐøõ ð ð ð àˆÝð 
ð 
ð 
ÝŒðåñ	
ô 	
ð 	
ð 	
ð 	
ð
øøøð €Os$   „ Ÿ.­.²A	A< Á<
B.Â#B.Â-B.c                   óð   ‡ — e Zd ZdZdeddfˆ fd„Zdddœdededej        fd	„Z	dej        fd
„Z
ddœdededee         fd„Zedddœdedededeeef         fd„Zdddœdededefd„Zdˆ fd„Zdd„Zˆ xZS )ÚTifffilePlugina¶  Support for tifffile as backend.

    Parameters
    ----------
    request : iio.Request
        A request object that represents the users intent. It provides a
        standard interface for a plugin to access the various ImageResources.
        Check the docs for details.
    kwargs : Any
        Additional kwargs are forwarded to tifffile's constructor, i.e.
        to ``TiffFile`` for reading or ``TiffWriter`` for writing.

    Úrequestr   Nc                 óp  •— t          ¦   «                              |¦  «         d | _        |j        j        dk    rS	 t          j        |                     ¦   «         fi |¤Ž| _        d S # t
          j        j        $ r t          d¦  «        ‚w xY wt          j
        |                     ¦   «         fi |¤Ž| _        d S )NÚrz Tifffile can not read this file.)ÚsuperÚ__init__Ú_fhÚmodeÚio_modeÚtifffileÚTiffFileÚget_fileÚTiffFileErrorr
   Ú
TiffWriter)Úselfr#   ÚkwargsÚ	__class__s      €r   r'   zTifffilePlugin.__init__g   sÂ   ø€ Ý‰Œ×Ò˜Ñ!Ô!Ð!ØˆŒàŒ<Ô 3Ò&Ð&ðNÝ#Ô,¨W×-=Ò-=Ñ-?Ô-?ÐJÐJÀ6ÐJÐJ�”��øÝÔ$Ô2ð Nð Nð NÝ)Ð*LÑMÔMÐMðNøøøõ  Ô*¨7×+;Ò+;Ñ+=Ô+=ÐHÐHÀÐHÐHˆDŒHˆHˆHs   »)A& Á&$B
)Úindexr   r3   r   c                óP  — d|vr||d<   n|�t          d¦  «        ‚|�d|v rt          d¦  «        ‚d|v r|                     d¦  «        }n|�nd}|t          u r,|€*t          j        d„  | j        di |¤ŽD ¦   «         ¦  «        }n!|t          u rdn|} | j        j        dd|i|¤Ž}|S )	aã  Read a ndimage or page.

        The ndimage returned depends on the value of both ``index`` and
        ``page``. ``index`` selects the series to read and ``page`` allows
        selecting a single page from the selected series. If ``index=None``,
        ``page`` is understood as a flat index, i.e., the selection ignores
        individual series inside the file. If both ``index`` and ``page`` are
        ``None``, then all the series are read and returned as a batch.

        Parameters
        ----------
        index : int
            If ``int``, select the ndimage (series) located at that index inside
            the file and return ``page`` from it. If ``None`` and ``page`` is
            ``int`` read the page located at that (flat) index inside the file.
            If ``None`` and ``page=None``, read all ndimages from the file and
            return them as a batch.
        page : int
            If ``None`` return the full selected ndimage. If ``int``, read the
            page at the selected index and return it.
        kwargs : Any
            Additional kwargs are forwarded to TiffFile's ``as_array`` method.

        Returns
        -------
        ndarray : np.ndarray
            The decoded ndimage or page.
        ÚkeyNz,Can't use `page` and `key` at the same time.Úseriesz0Can't use `series` and `index` at the same time.r   c                 ó   — g | ]}|‘ŒS © r8   )Ú.0Úxs     r   ú
<listcomp>z'TifffilePlugin.read.<locals>.<listcomp>¦   s   € Ð?Ð?Ð? a Ð?Ð?Ð?ó    r8   )Ú
ValueErrorÚpopÚEllipsisÚnpÚstackÚiterr(   Úasarray)r0   r3   r   r1   Úndimages        r   ÚreadzTifffilePlugin.readw   sô   € ð< ˜ÐÐØ ˆF�5‰MˆMØÐÝÐKÑLÔLÐLð Ð ¨VÐ!3Ð!3ÝÐOÑPÔPÐPØ˜ÐÐØ—J’J˜xÑ(Ô(ˆEˆEØÐØàˆEà•HÐÐ  å”hÐ?Ð?¨9¨4¬9Ð+>Ð+>°vÐ+>Ð+>Ð?Ñ?Ô?Ñ@Ô@ˆGˆGà!¥XÐ-Ð-�D�D°5ˆEØ&�d”hÔ&Ð>Ð>¨eÐ>°vÐ>Ð>ˆGàˆr<   c              +   óF   K  — | j         j        D ]} |j        di |¤ŽV — ŒdS )a!  Yield ndimages from the TIFF.

        Parameters
        ----------
        kwargs : Any
            Additional kwargs are forwarded to the TiffPageSeries' ``as_array``
            method.

        Yields
        ------
        ndimage : np.ndarray
            A decoded ndimage.
        Nr8   )r(   r6   rC   )r0   r1   Úsequences      r   rB   zTifffilePlugin.iter­   sG   è è € ð œœð 	-ð 	-ˆHØ"�(Ô"Ð,Ð, VÐ,Ð,Ð,Ð,Ð,Ð,ð	-ð 	-r<   F)Úis_batchrD   rH   c                ó†  — |st          j        |¦  «        ddd…f         }|D ]¯}t          |¦  «        }t          j        |¦  «        }|j        dk    r&|j        d         dv r|                     dd¦  «         nF|j        dk    r;|j        d         dv r,|                     dd¦  «         |                     dd	¦  «          | j        j        |fi |¤Ž Œ°| j        j	        t          k    rY| j                             ¦   «          t          t          | j                             ¦   «         ¦  «        }|                     ¦   «         S dS )
aC  Save a ndimage as TIFF.

        Parameters
        ----------
        ndimage : ArrayLike
            The ndimage to encode and write to the ImageResource.
        is_batch : bool
            If True, the first dimension of the given ndimage is treated as a
            batch dimension and each element will create a new series.
        kwargs : Any
            Additional kwargs are forwarded to TiffWriter's ``write`` method.

        Returns
        -------
        encoded_image : bytes
            If the ImageResource is ``"<bytes>"``, return the encoded bytes.
            Otherwise write returns None.

        Notes
        -----
        Incremental writing is supported. Subsequent calls to ``write`` will
        create new series unless ``contiguous=True`` is used, in which case the
        call to write will append to the current series.

        When ``photometric`` / ``planarconfig`` are omitted, ImageIO sets
        defaults for common RGB layouts so tifffile does not emit deprecation
        warnings about ambiguous channel-axis interpretation:

        - channel-last arrays (shape ``(..., 3)`` or ``(..., 4)``):
          ``photometric="rgb"``
        - channel-first arrays (shape ``(3, ...)`` or ``(4, ...)`` on the
          third-to-last axis): ``photometric="rgb"`` and
          ``planarconfig="separate"``
        Né   éÿÿÿÿ)rJ   é   ÚphotometricÚrgbéýÿÿÿÚplanarconfigÚseparate)r@   rC   ÚdictÚndimÚshapeÚ
setdefaultr(   ÚwriteÚ_requestÚ	_uri_typer	   Úcloser   r   r-   Úgetvalue)r0   rD   rH   r1   ÚimageÚwrite_kwargsÚarrÚfiles           r   rV   zTifffilePlugin.write¿   sA  € ðL ð 	3Ý”j Ñ)Ô)¨$°°°¨'Ô2ˆGàð 	2ð 	2ˆEÝ ™<œ<ˆLÝ”*˜UÑ#Ô#ˆCØŒx˜1Š}ˆ} ¤¨2¤°&Ð!8Ð!8Ø×'Ò'¨°uÑ=Ô=Ð=Ð=Ø”˜Q’� 3¤9¨R¤=°FÐ#:Ð#:Ø×'Ò'¨°uÑ=Ô=Ð=Ø×'Ò'¨¸
ÑCÔCÐCØˆDŒHŒN˜5Ð1Ð1 LÐ1Ð1Ð1Ð1àŒ=Ô"¥iÒ/Ð/ØŒH�NŠNÑÔÐÝ� ¤×!7Ò!7Ñ!9Ô!9Ñ:Ô:ˆDØ—=’=‘?”?Ð"ð 0Ð/r<   T)r3   r   Úexclude_appliedr_   c          
      óf  — |t           ur |�| j        j        |         j        |         }nI|t           ur |€| j        j        |         j        d         }n |t           u r|�| j        j        |         }nd}i }|€¼| j        j        |d<   t
          j        j        D ]š}t          | j        d|z   ¦  «        }||d|z   <   |rvt          | j        |dz   ¦  «        r^t          | j        |dz   ¦  «        }t          |t          ¦  «        r|                     |d         ¦  «         Œ…|                     |¦  «         Œ›n~|j        }|                     d„ |j        D ¦   «         ¦  «         |                     |j        |j        |j        d|j        |j        |j        dœt+          |¦  «        ¥d|j        i¥¦  «         |S )	as  Format-Specific TIFF metadata.

        The metadata returned depends on the value of both ``index`` and
        ``page``. ``index`` selects a series and ``page`` allows selecting a
        single page from the selected series. If ``index=Ellipsis``, ``page`` is
        understood as a flat index, i.e., the selection ignores individual
        series inside the file. If ``index=Ellipsis`` and ``page=None`` then
        global (file-level) metadata is returned.

        Parameters
        ----------
        index : int
            Select the series of which to extract metadata from. If Ellipsis, treat
            page as a flat index into the file's pages.
        page : int
            If not None, select the page of which to extract metadata from. If
            None, read series-level metadata or, if ``index=...`` global,
            file-level metadata.
        exclude_applied : bool
            For API compatibility. Currently ignored.

        Returns
        -------
        metadata : dict
            A dictionary with information regarding the tiff flavor (file-level)
            or tiff tags (page-level).
        Nr   Ú	byteorderÚis_Ú	_metadatac                 ó(   — i | ]}|j         |j        “ŒS r8   )Únamer   )r9   Útags     r   ú
<dictcomp>z+TifffilePlugin.metadata.<locals>.<dictcomp>4  s   € ÐHÐHÐH°S˜SœX s¤yÐHÐHÐHr<   )Úplanar_configurationÚcompressionÚ	predictorÚorientationÚdescription1ÚdescriptionÚsoftwareÚdatetime)r?   r(   r6   Úpagesra   r+   ÚTIFFÚ
FILE_FLAGSÚgetattrÚhasattrÚ
isinstanceÚtupleÚupdateÚkeyframer   rP   ri   rj   rl   rm   rn   r    ro   )	r0   r3   r   r_   Útargetr   ÚflagÚ
flag_valueÚflavor_metadatas	            r   r   zTifffilePlugin.metadata÷   sâ  € ð> �Ð Ð  TÐ%5Ø”X”_ UÔ+Ô1°$Ô7ˆFˆFØ�(Ð"Ð" t |ð ”X”_ UÔ+Ô1°!Ô4ˆFˆFØ•hÐÐ 4Ð#3Ø”X”^ DÔ)ˆFˆFàˆFàˆØˆ>à$(¤HÔ$6ˆH�[Ñ!å œÔ0ð 	9ð 	9�Ý$ T¤X¨u°t©|Ñ<Ô<�
Ø)3�˜ ™Ñ&àð 9¥'¨$¬(°D¸;Ñ4FÑ"GÔ"Gð 9Ý&-¨d¬h¸¸{Ñ8JÑ&KÔ&K�OÝ! /µ5Ñ9Ô9ð 9Ø Ÿš¨¸Ô(:Ñ;Ô;Ð;Ð;à Ÿš¨Ñ8Ô8Ð8øð	9ð ”_ˆFà�OŠOÐHÐH¸F¼KÐHÑHÔHÑIÔIÐIØ�OŠOà,2Ô,?Ø#)Ô#5Ø!'Ô!1Ø#'Ø$*Ô$7Ø#)Ô#5Ø &¤ð
ð 
õ & fÑ-Ô-ð
ð  ¤ð
ð 
ñô ð ð ˆr<   c          	      óÂ  — |pd}|dt           fv rdn|}|t           u r| j        j        |         }n| j        j        |         j        |         }|t           u r^|€\t	          | j        j        ¦  «        }t          |g|j        ¢R |j        |dt          |¦  «         	                    d¦  «        ¬¦  «        }n«|t           u re|t           u r\t	          | j        j        ¦  «        }t          |g|j        ¢R |j        |dt          |¦  «         	                    d¦  «        ¬¦  «        }n=t          |j        |j        dt          |¦  «         	                    d¦  «        ¬¦  «        }|S )a€  Standardized metadata.

        The properties returned depend on the value of both ``index`` and
        ``page``. ``index`` selects a series and ``page`` allows selecting a
        single page from the selected series. If ``index=Ellipsis``, ``page`` is
        understood as a flat index, i.e., the selection ignores individual
        series inside the file. If ``index=Ellipsis`` and ``page=None`` then
        global (file-level) properties are returned. If ``index=Ellipsis``
        and ``page=...``, file-level properties for the flattened index are
        returned.

        Parameters
        ----------
        index : int
            If ``int``, select the ndimage (series) located at that index inside
            the file. If ``Ellipsis`` and ``page`` is ``int`` extract the
            properties of the page located at that (flat) index inside the file.
            If ``Ellipsis`` and ``page=None``, return the properties for the
            batch of all ndimages in the file.
        page : int
            If ``None`` return the properties of the full ndimage. If ``...``
            return the properties of the flattened index. If ``int``,
            return the properties of the page at the selected index only.

        Returns
        -------
        image_properties : ImageProperties
            The standardized metadata (properties) of the selected ndimage or series.

        r   NTr   )rT   ÚdtypeÚn_imagesrH   ÚspacingF)rT   r~   rH   r€   )
r?   r(   rp   r6   Úlenr   rT   r~   r    Úget)r0   r3   r   Úpage_idxÚtarget_pageÚn_seriesÚpropsÚn_pagess           r   Ú
propertieszTifffilePlugin.propertiesE  s{  € ð> �
˜ˆØ ¥xÐ 0Ð0Ð0�1�1°dˆà•HÐÐØœ(œ.¨Ô2ˆKˆKàœ(œ/¨%Ô0Ô6°xÔ@ˆKà•HÐÐ  Ý˜4œ8œ?Ñ+Ô+ˆHÝ#ØÐ4 +Ô"3Ð4Ð4Ø!Ô'Ø!ØÝ'¨Ñ4Ô4×8Ò8¸ÑFÔFðñ ô ˆEˆEð •hÐÐ 4­8Ð#3Ð#3Ý˜$œ(œ.Ñ)Ô)ˆGÝ#ØÐ3 Ô!2Ð3Ð3Ø!Ô'Ø ØÝ'¨Ñ4Ô4×8Ò8¸ÑFÔFðñ ô ˆEˆEõ $Ø!Ô'Ø!Ô'ØÝ'¨Ñ4Ô4×8Ò8¸ÑFÔFð	ñ ô ˆEð ˆr<   c                 óˆ   •— | j         �| j                              ¦   «          t          ¦   «                              ¦   «          d S )N)r(   rY   r&   )r0   r2   s    €r   rY   zTifffilePlugin.closeˆ  s2   ø€ ØŒ8ÐØŒH�NŠNÑÔÐå‰Œ�Š‰Œˆˆˆr<   .c              +   ó‚   K  — |t           u r| j        j        }n| j        j        |         }|D ]} |j        di |¤ŽV — ŒdS )a7  Yield pages from a TIFF file.

        This generator walks over the flat index of the pages inside an
        ImageResource and yields them in order.

        Parameters
        ----------
        index : int
            The index of the series to yield pages from. If Ellipsis, walk over
            the file's flat index (and ignore individual series).
        kwargs : Any
            Additional kwargs are passed to TiffPage's ``as_array`` method.

        Yields
        ------
        page : np.ndarray
            A page stored inside the TIFF file.

        Nr8   )r?   r(   rp   r6   rC   )r0   r3   r1   rp   r   s        r   Ú
iter_pageszTifffilePlugin.iter_pages’  sf   è è € ð* •HÐÐØ”H”NˆEˆEà”H”O EÔ*ˆEàð 	)ð 	)ˆDØ�$”,Ð(Ð( Ð(Ð(Ð(Ð(Ð(Ð(ð	)ð 	)r<   )r   N).)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r'   Úintr@   ÚndarrayrE   rB   r   Úboolr   ÚbytesrV   r?   r   Ústrr   r   r   rˆ   rY   r‹   Ú__classcell__)r2   s   @r   r"   r"   X   sÈ  ø€ € € € € ðð ð
I ð 
I°dð 
Ið 
Ið 
Ið 
Ið 
Ið 
Ið  $(°Tð 4ð 4ð 4˜Sð 4¨sð 4ÈÌ
ð 4ð 4ð 4ð 4ðl- ¤
ð -ð -ð -ð -ð& 7<ð6#ð 6#ð 6#Ø ð6#Ø/3ð6#à	�%Œð6#ð 6#ð 6#ð 6#ðr '°DÐRVðLð Lð LØðLØ.1ðLØKOðLà	ˆc�3ˆhŒðLð Lð Lð Lð\ *.¸4ð Að Að A 3ð A°Sð AÀOð Að Að Að AðFð ð ð ð ð ð)ð )ð )ð )ð )ð )ð )ð )r<   r"   )r�   Úior   Útypingr   r   r   r   r   Únumpyr@   r+   Úcore.requestr	   r
   r   Úcore.v3_plugin_apir   r   r   ÚTiffPager”   r    r"   r8   r<   r   ú<module>rœ      s  ðð+ð +ðZ Ð Ð Ð Ð Ð Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø €€€à Ð Ð Ð Ø €€€à BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BØ :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ø Ð Ð Ð Ð Ð ð˜(Ô+ð °°S¸#°X´ð ð ð ð ð<U)ð U)ð U)ð U)ð U)�Xñ U)ô U)ð U)ð U)ð U)r<   