Ë
    Eü´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y)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                 ó|  — i }	 | j                   d   j                  j                  |d<   	 | j                   d   j                  }| j                   d   j                  }|d   |d   z  |d   |d   z  f|d<   |S # t        $ r |cY S w xY w# t        $ r Y |S t        $ r t	        j
                  dt        «       Y |S w xY w)	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       úY/root/workspace/ytshorts/venv/lib/python3.12/site-packages/imageio/plugins/tifffile_v3.pyÚ_get_resolutionr    :   sà   € Ø€HðØ&*§i¡i°¡n×&:Ñ&:×&@Ñ&@ˆÐ"Ñ#ð

Ø—y‘y ‘~×+Ñ+ˆØ—y‘y ‘~×+Ñ+ˆð ˜‰O˜l¨1™oÑ-Ø˜‰O˜l¨1™oÑ-ð"
ˆ�Ñð €Oøô- ò àŠðûô ò àð €Oô ò 
Ü�‰ðäõ	
ð €Oð
ús)   „&A8 «AB	 Á8BÂBÂ		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                 ód  •— t         ‰| �  |«       d | _        |j                  j                  dk(  r+	 t        j                  |j                  «       fi |¤Ž| _        y t        j                  |j                  «       fi |¤Ž| _        y # t
        j
                  j                  $ r t        d«      ‚w xY w)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À6ÑJ�•ô  ×*Ñ*¨7×+;Ñ+;Ó+=ÑHÀÑHˆD�Høô ×$Ñ$×2Ñ2ò NÜ)Ð*LÓMÐMðNús   ²)B Â)B/)Úindexr   r3   r   c                óh  — d|vr||d<   n|�t        d«      ‚|�d|v rt        d«      ‚d|v r|j                  d«      }n|�nd}|t        u r7|€5t        j                   | j
                  di |¤ŽD �cg c]  }|‘Œ c}«      }|S |t        u rdn|} | j                  j                  dd|i|¤Ž}|S c c}w )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   © )Ú
ValueErrorÚpopÚEllipsisÚnpÚstackÚiterr(   Úasarray)r0   r3   r   r1   ÚxÚndimages         r   ÚreadzTifffilePlugin.readw   sÙ   € ð< ˜ÑØ ˆF�5ŠMØÐÜÐKÓLÐLð Ð ¨VÑ!3ÜÐOÓPÐPØ˜ÑØ—J‘J˜xÓ(‰EØÐØàˆEà”HÑ  ä—h‘h¨9¨4¯9©9Ñ+>°vÑ+>Ö? a¢Ò?Ó@ˆGð
 ˆð "¤XÑ-‘D°5ˆEØ&�d—h‘h×&Ñ&Ñ>¨eÐ>°vÑ>ˆGàˆùò  @s   Á1	B/c              +   ój   K  — | j                   j                  D ]  } |j                  di |¤Ž–— Œ y­w)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.
        Nr7   )r(   r6   r>   )r0   r1   Úsequences      r   r=   zTifffilePlugin.iter­   s5   è ø€ ð Ÿ™Ÿ™ò 	-ˆHØ"�(×"Ñ"Ñ, VÑ,Ó,ñ	-ùs   ‚13F)Úis_batchr@   rD   c                ó”  — |st        j                  |«      ddd…f   }|D ]¶  }t        |«      }t        j                  |«      }|j                  dk\  r$|j                  d   dv r|j                  dd«       nD|j                  dk\  r5|j                  d   dv r$|j                  dd«       |j                  dd	«        | j                  j                  |fi |¤Ž Œ¸ | j                  j                  t        k(  rR| j                  j                  «        t        t        | j                  j                  «       «      }|j                  «       S y)
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é   éÿÿÿÿ)rF   é   ÚphotometricÚrgbéýÿÿÿÚplanarconfigÚseparate)r;   r>   ÚdictÚndimÚshapeÚ
setdefaultr(   ÚwriteÚ_requestÚ	_uri_typer	   Úcloser   r   r-   Úgetvalue)r0   r@   rD   r1   ÚimageÚwrite_kwargsÚarrÚfiles           r   rR   zTifffilePlugin.write¿   s  € ñL Ü—j‘j Ó)¨$²¨'Ñ2ˆGàò 	2ˆEÜ ›<ˆLÜ—*‘*˜UÓ#ˆCØ�x‰x˜1Š} §¡¨2¡°&Ñ!8Ø×'Ñ'¨°uÕ=Ø—‘˜Q’ 3§9¡9¨R¡=°FÑ#:Ø×'Ñ'¨°uÔ=Ø×'Ñ'¨¸
ÔCØˆD�H‰H�N‰N˜5Ñ1 LÓ1ð	2ð �=‰=×"Ñ"¤iÒ/Ø�H‰H�N‰NÔÜœ §¡×!7Ñ!7Ó!9Ó:ˆDØ—=‘=“?Ð"ð 0ó    T)r3   r   Úexclude_appliedr\   c          
      ó&  — |t         ur)|�'| j                  j                  |   j                  |   }nW|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   <   |sŒ't        | j                  |dz   «      sŒAt        | j                  |dz   «      }t        |t        «      r|j                  |d   «       Œ|j                  |«       Œ‘ |S |j                  }|j                  |j                  D �	ci c]  }	|	j                  |	j                   “Œ c}	«       |j                  |j"                  |j$                  |j&                  d|j(                  |j*                  |j,                  dœt/        |«      ¥d|j0                  i¥«       |S c c}	w )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_Ú	_metadata)Úplanar_configurationÚcompressionÚ	predictorÚorientationÚdescription1ÚdescriptionÚsoftwareÚdatetime)r:   r(   r6   Úpagesr^   r+   ÚTIFFÚ
FILE_FLAGSÚgetattrÚhasattrÚ
isinstanceÚtupleÚupdateÚkeyframer   Únamer   rL   rb   rc   re   rf   rg   r    rh   )
r0   r3   r   r\   Útargetr   ÚflagÚ
flag_valueÚflavor_metadataÚtags
             r   r   zTifffilePlugin.metadata÷   sÕ  € ð> œÑ  TÐ%5Ø—X‘X—_‘_ UÑ+×1Ñ1°$Ñ7‰FØœ(Ñ" t |ð —X‘X—_‘_ UÑ+×1Ñ1°!Ñ4‰FØ”hÑ 4Ð#3Ø—X‘X—^‘^ DÑ)‰FàˆFàˆØˆ>à$(§H¡H×$6Ñ$6ˆH�[Ñ!ä Ÿ™×0Ñ0ò 	9�Ü$ T§X¡X¨u°t©|Ó<�
Ø)3�˜ ™Ñ&â¤'¨$¯(©(°D¸;Ñ4FÕ"GÜ&-¨d¯h©h¸¸{Ñ8JÓ&K�OÜ! /´5Ô9Ø Ÿ™¨¸Ñ(:Õ;à Ÿ™¨Õ8ð	9ð: ˆð# —_‘_ˆFà�O‰O¸F¿K¹KÖH°S˜SŸX™X s§y¡yÑ0ÒHÔIØ�O‰Oà,2×,?Ñ,?Ø#)×#5Ñ#5Ø!'×!1Ñ!1Ø#'Ø$*×$7Ñ$7Ø#)×#5Ñ#5Ø &§¡ñ
ô & fÓ-ð
ð  §¡ñ
ôð ˆùò Is   Å9Hc          	      óì  — |xs d}|dt         fv rdn|}|t         u r| j                  j                  |   }n&| j                  j                  |   j                  |   }|t         u rc|€at	        | j                  j                  «      }t        |g|j                  ¢­|j                  |dt        |«      j                  d«      ¬«      }|S |t         u ri|t         u rat	        | j                  j                  «      }t        |g|j                  ¢­|j                  |dt        |«      j                  d«      ¬«      }|S t        |j                  |j                  dt        |«      j                  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   )rP   ÚdtypeÚn_imagesrD   ÚspacingF)rP   ry   rD   r{   )
r:   r(   ri   r6   Úlenr   rP   ry   r    Úget)r0   r3   r   Úpage_idxÚtarget_pageÚn_seriesÚpropsÚn_pagess           r   Ú
propertieszTifffilePlugin.propertiesE  s[  € ð> ’
˜ˆØ ¤xÐ 0Ñ0‘1°dˆà”HÑØŸ(™(Ÿ.™.¨Ñ2‰KàŸ(™(Ÿ/™/¨%Ñ0×6Ñ6°xÑ@ˆKà”HÑ  Ü˜4Ÿ8™8Ÿ?™?Ó+ˆHÜ#ØÐ4 +×"3Ñ"3Ñ4Ø!×'Ñ'Ø!ØÜ'¨Ó4×8Ñ8¸ÓFôˆEð0 ˆð# ”hÑ 4¬8Ñ#3Ü˜$Ÿ(™(Ÿ.™.Ó)ˆGÜ#ØÐ3 ×!2Ñ!2Ñ3Ø!×'Ñ'Ø ØÜ'¨Ó4×8Ñ8¸ÓFôˆEð ˆô $Ø!×'Ñ'Ø!×'Ñ'ØÜ'¨Ó4×8Ñ8¸ÓFô	ˆEð ˆr[   c                 ón   •— | j                   �| j                   j                  «        t        ‰| �  «        y )N)r(   rU   r&   )r0   r2   s    €r   rU   zTifffilePlugin.closeˆ  s$   ø€ Ø�8‰8ÐØ�H‰H�N‰NÔä‰‰�r[   c              +   ó²   K  — |t         u r| j                  j                  }n| j                  j                  |   }|D ]  } |j                  di |¤Ž–— Œ y­w)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.

        Nr7   )r:   r(   ri   r6   r>   )r0   r3   r1   ri   r   s        r   Ú
iter_pageszTifffilePlugin.iter_pages’  sR   è ø€ ð* ”HÑØ—H‘H—N‘N‰Eà—H‘H—O‘O EÑ*ˆEàò 	)ˆDØ�$—,‘,Ñ( Ñ(Ó(ñ	)ùs   ‚AA)r   N).)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r'   Úintr;   ÚndarrayrA   r=   r   Úboolr   ÚbytesrR   r:   r   Ústrr   r   r   rƒ   rU   r†   Ú__classcell__)r2   s   @r   r"   r"   X   sè   ø„ ñð
I ð 
I°dõ 
Ið  $(°Tò 4˜Sð 4¨sð 4ÈÏ
É
ó 4ðl- §
¡
ó -ð& 7<ò6#Ø ð6#Ø/3ð6#à	�%‰ó6#ðr '°DÐRVòLØðLØ.1ðLØKOðLà	ˆc�3ˆh‰óLð\ *.¸4ò A 3ð A°Sð AÀOó 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"   r7   r[   r   ú<module>r—      sZ   ðñ+õZ ß ,Ó ,Û ã Û ç BÑ Bß :Ý ð˜(×+Ñ+ð °°S¸#°X±ó ô<U)�Xõ U)r[   