
    0-Ph3I                        d Z ddl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	Z	ddl
Z
 e
j        d          Zdej        v Zej                            ej                            e                    Zd ZddZdd	Z G d
 dej                  ZeZd Z G d de          Z G d de          Z G d de          ZddZd Z d Z!d Z"d Z#dS )z
Various utilities for imageio
    )OrderedDictNimageio__pypy__c                  \    	 ddl m} n# t          $ r t          d          w xY w || i |S )zCompatibility function for the urlopen function. Raises an
    RuntimeError if urlopen could not be imported (which can occur in
    frozen applications.
    r   )urlopenzCould not import urlopen.)urllib.requestr   ImportErrorRuntimeError)argskwargsr   s      Q/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/imageio/core/util.pyr   r      s[    
8******* 8 8 8677787D#F###s   	 # c                 j    d}t                               |                    | |||                     d S )Nz`Lossy conversion from {} to {}. {} Convert image to {} prior to saving to suppress this warning.)loggerwarningformat)p1p2extrats       r   _precision_warnr   %   s8    	+  NN188BE2../////    c                    |sd}t          | t          j                  st          d          |dk    rt          j        }n"|dk    rt          j        }nt          d          t          | j                  }|j        }| j        t          j        k    r|dk    s| j        t          j        k    r|dk    r| S |	                    d          r~t          j
        |           dk    rft          j        |           dk    rNt          ||d           |                     t          j                  t          j        d	|          dz
  z  d
z   } n| j        t          j        k    r.|dk    r(t          ||d           t          j        | d          } n| j        t          j        k    rAt          ||d                    d|z
                       t          j        | d|z
            } n?| j        t          j        k    r@t          ||d                    d|z
                       t          j        | d|z
            } nt          j
        |           }t          j        |           }t          j        |          st          d          t          j        |          st          d          ||k    r|                     |          S t          ||d                    ||                     |                     d          } | |z
  ||z
  z  t          j        d	|          dz
  z  d
z   } t          j
        |           dk    sJ t          j        |           t          j        d	|          k     sJ |                     |          S )a  Convert the given image to uint (default: uint8)

    If the dtype already matches the desired format, it is returned
    as-is. If the image is float, and all values are between 0 and 1,
    the values are multiplied by np.power(2.0, bitdepth). In all other
    situations, the values are scaled such that the minimum value
    becomes 0 and the maximum value becomes np.power(2.0, bitdepth)-1
    (255 for 8-bit and 65535 for 16-bit).
       zImage must be a numpy array   zBitdepth must be either 8 or 16floatr      zRange [0, 1].g       @gA?zLosing 8 bits of resolution.zLosing {} bits of resolution.    @   z!Minimum image value is not finitez!Maximum image value is not finitezRange [{}, {}].float64)
isinstancenpndarray
ValueErroruint8uint16strdtype__name__
startswithnanminnanmaxr   astyper    powerright_shiftuint32r   uint64isfinite)imbitdepthout_type
dtype_str1
dtype_str2mimas          r   image_as_uintr:   -   s<     b"*%% 867771}}8	R9:;;;RXJ"J
BHQ
BI(b.. 	W%% !Q")B--1*<*<2RSASAS
J@@@YYrz""bhsH&=&=&AB[P	RY		8q==
J0NOOO^B""	RY		+222=AA	
 	
 	

 ^BX..	RY		+222=AA	
 	
 	

 ^BX..Yr]]Yr]]{2 	B@AAA{2 	B@AAA8899X&&&
J0A0H0HR0P0PQQQYYy!!2g"r'"bhsH&=&=&AB[P9R==A9R==28C22222299Xr   c                   D    e Zd ZdZddZd Zed             Zd ZddZ	dS )	ArrayzArray(array, meta=None)

    A subclass of np.ndarray that has a meta attribute. Get the dictionary
    that contains the meta data using ``im.meta``. Convert to a plain numpy
    array using ``np.asarray(im)``.

    Nc                 J   t          |t          j                  st          d          |$t          |t                    st          d          ||nt          |di           }	 |                    |           }n# t          $ r |cY S w xY w|                    |           |S )NzArray expects a numpy array.z%Array expects meta data to be a dict.meta)	r!   r"   r#   r$   dictgetattrviewAttributeError
_copy_meta)clsarrayr>   obs       r   __new__zArray.__new__x   s    %,, 	=;<<<
4 6 6DEEE'ttWUFB-G-G	CBB 	 	 	LLL	 	d	s   &A< <B
Bc                     t                      | _        |                                D ]3\  }}t          |t                    rt          |          }|| j        |<   4dS )z0Make a 2-level deep copy of the meta dictionary.N)Dict_metaitemsr!   r?   )selfr>   keyvals       r   rC   zArray._copy_meta   s\    VV


 	" 	"HC#t$$  3ii!DJsOO	" 	"r   c                     | j         S )z*The dict with the meta data of this image.)rJ   rL   s    r   r>   z
Array.meta   s     zr   c                     t          |t                    r|                     |j                   dS |                     i            dS )zWSo the meta info is maintained when doing calculations with
        the array.
        N)r!   r<   rC   r>   )rL   rF   s     r   __array_finalize__zArray.__array_finalize__   sH     b%   	 OOBG$$$$$OOBr   c                     |j         s|j                            |          S |j         | j         k    r |                    t          j                  S t          |t                    st          || j                  S |S )zSo that we return a native numpy array (or scalar) when a
        reducting ufunc is applied (such as sum(), std(), etc.)
        type)	shaper(   rU   rA   r"   r#   r!   r<   r>   )rL   outcontexts      r   __array_wrap__zArray.__array_wrap__   sp     y 	9>>#&&&Y$*$$888,,,C'' 	di(((Jr   N)
r)   
__module____qualname____doc__rG   rC   propertyr>   rR   rY    r   r   r<   r<   o   s             " " "   X          r   r<   c                     t          | t          j                  r=t          r|                                 } |                     t          j                  }|S t          j        |           S )zPypy-safe version of np.asarray. Pypy's np.asarray consumes a
    *lot* of memory if the given array is an ndarray subclass. This
    function does not.
    rT   )r!   r"   r#   IS_PYPYcopyrA   asarray)aplains     r   rc   rc      sU    
 !RZ    	ABJ'':a==r   c                   p    e Zd ZdZ e e                      Z e e                      Zd Z	d Z
d ZdS )rI   a  A dict in which the keys can be get and set as if they were
    attributes. Very convenient in combination with autocompletion.

    This Dict still behaves as much as possible as a normal dict, and
    keys can be anything that are otherwise valid keys. However,
    keys that are not valid identifiers or that are names of the dict
    class (such as 'items' and 'copy') cannot be get/set as attributes.
    c                 t    	 t                               | |          S # t          $ r || v r
| |         cY S  w xY wrZ   )object__getattribute__rB   )rL   rM   s     r   ri   zDict.__getattribute__   sT    	**4555 	 	 	d{{Cy   		s    77c                     |t           j        v r9|t           j        vrt          j        | ||          S t          dd|z  z             || |<   d S )Nz!Reserved name, this key can only zbe set via ``d[%r] = X``)rI   __reserved_names____pure_names__r   __setattr__rB   )rL   rM   rN   s      r   rm   zDict.__setattr__   sd    $)))$---".tS#>>>$70367   DIIIr   c                 h    d fd|                                  D             }t          j        |z   S )Nc                 \    t          t          j        d| t          j                            S )Nz
[a-z_]\w*$)boolrematchI)xs    r   isidentifierz"Dict.__dir__.<locals>.isidentifier   s     2488999r   c                 T    g | ]$}t          |t                     |          "|%S r_   )r!   r'   ).0kru   s     r   
<listcomp>z Dict.__dir__.<locals>.<listcomp>   s6    TTTqJq#,>,>T<<PQ??TTTTr   )keysrI   rk   )rL   namesru   s     @r   __dir__zDict.__dir__   sD    	: 	: 	: UTTTDIIKKTTT&..r   N)r)   r[   r\   r]   dirr   rk   r?   rl   ri   rm   r|   r_   r   r   rI   rI      sv          [[]]++S[[N    / / / / /r   rI   c                   b    e Zd ZdZd ZddZd ZddZd	 ZddZ	ddZ
d Zd Zd Zd Zd Zd
S )BaseProgressIndicatora  BaseProgressIndicator(name)

    A progress indicator helps display the progress of a task to the
    user. Progress can be pending, running, finished or failed.

    Each task has:
      * a name - a short description of what needs to be done.
      * an action - the current action in performing the task (e.g. a subtask)
      * progress - how far the task is completed
      * max - max number of progress units. If 0, the progress is indefinite
      * unit - the units in which the progress is counted
      * status - 0: pending, 1: in progress, 2: finished, 3: failed

    This class defines an abstract interface. Subclasses should implement
    _start, _stop, _update_progress(progressText), _write(message).
    c                 Z    || _         d| _        d| _        d| _        d| _        d| _        d S )Nr   r   )_name_action_unit_max_status_last_progress_update)rL   names     r   __init__zBaseProgressIndicator.__init__   s3    

	%&"""r   r   r   c                     | j         dk    r|                                  || _        || _        || _        d| _        d| _         |                                  dS )zstart(action='', unit='', max=0)

        Start the progress. Optionally specify an action, a unit,
        and a maximum progress value.
        r   r   N)r   finishr   r   r   	_progress_start)rL   actionunitmaxs       r   startzBaseProgressIndicator.start  sR     <1KKMMM
	r   c                     | j         S )zvstatus()

        Get the status of the progress - 0: pending, 1: in progress,
        2: finished, 3: failed
        )r   rP   s    r   statuszBaseProgressIndicator.status  s     |r   Fc                    || _         |s!t          j                    | j        z
  dk    sdS t          j                    | _        | j        pd}d}|dk    rd|z  }n^| j        dk    r)dt          |          z  | j        z  }d|| j        ||fz  }n*|dk    r$t          |t
                    rd	||fz  }nd
||fz  }|                     |           dS )zset_progress(progress=0, force=False)

        Set the current progress. To avoid unnecessary progress updates
        this will only have a visual effect if the time since the last
        update is > 0.1 seconds, or if force is True.
        g?Nr   %z%2.1f%%r   d   z%i/%i %s (%2.1f%%)z%0.4g %sz%i %s)r   timer   r   r   r   r!   _update_progress)rL   progressforcer   progressTextpercents         r   set_progressz"BaseProgressIndicator.set_progress  s     " 	$)++(BBSHHF%)Y[["zR3;;$x/LLY]]E(OO+di7G/8TYg2VVLL\\(E** :)Xt,<<&(D)99l+++++r   c                 @    |                      | j        |z              dS )z^increase_progress(extra_progress)

        Increase the progress by a certain amount.
        N)r   r   )rL   extra_progresss     r   increase_progressz'BaseProgressIndicator.increase_progress6  s%    
 	$.>9:::::r   Nc                     |                      | j        d           d| _        |                                  ||                     |           dS dS )zfinish(message=None)

        Finish the progress, optionally specifying a message. This will
        not set the progress to the maximum.
        T   Nr   r   r   _stop_writerL   messages     r   r   zBaseProgressIndicator.finish=  sV     	$.$///

KK      r   c                     |                      | j        d           d| _        |                                  d|pdz   }|                     |           dS )zgfail(message=None)

        Stop the progress with a failure, optionally specifying a message.
        T   zFAIL r   Nr   r   s     r   failzBaseProgressIndicator.failI  sV    
 	$.$///

W]+Gr   c                 n    | j         t          k    rt          |           dS |                     |          S )zUwrite(message)

        Write a message during progress (such as a warning).
        N)	__class__r   printr   r   s     r   writezBaseProgressIndicator.writeT  s3    
 >222'NNNNN;;w'''r   c                     d S rZ   r_   rP   s    r   r   zBaseProgressIndicator._starta      r   c                     d S rZ   r_   rP   s    r   r   zBaseProgressIndicator._stopd  r   r   c                     d S rZ   r_   )rL   r   s     r   r   z&BaseProgressIndicator._update_progressg  r   r   c                     d S rZ   r_   r   s     r   r   zBaseProgressIndicator._writej  r   r   )r   r   r   )r   FrZ   )r)   r[   r\   r]   r   r   r   r   r   r   r   r   r   r   r   r   r_   r   r   r   r      s         "' ' '      , , , ,8; ; ;
! 
! 
! 
!	 	 	 		( 	( 	(          r   r   c                   *    e Zd ZdZd Zd Zd Zd ZdS )StdoutProgressIndicatorzStdoutProgressIndicator(name)

    A progress indicator that shows the progress in stdout. It
    assumes that the tty can appropriately deal with backspace
    characters.
    c                    d\  | _         | _        | j        r| j        d| j        d| _         nd| j        z  | _         t          j                            | j                    t          j                                         d S )N)r   r   z (z): z%s: )_chars_prefix_charsr   r   sysstdoutr   flushrP   s    r   r   zStdoutProgressIndicator._startv  sy    *0'DK< 	504


DLLL!ID!'$*!4D
+,,,
r   c                 (   |s,d\  }}}}||||||||i}|                     | j        |          }dt          | j                  z  }|| _        t          j                            || j        z              t          j                                         d S )Nz-\|/)getr   lenr   r   r   r   )rL   r   i1i2i3i4MdelCharss           r   r   z(StdoutProgressIndicator._update_progress  s     	2$NBBRRRR0A55b11L#dk***"
DK/000
r   c                     dx| _         | _        t          j                            d           t          j                                         d S )Nr   
)r   r   r   r   r   r   rP   s    r   r   zStdoutProgressIndicator._stop  sA    +--d(

r   c                 (   dt          | j        | j        z             z  }t          j                            |dz   |z   dz              t          j                            | j        | j        z              t          j                                         d S )Nr   z  r   )r   r   r   r   r   r   r   )rL   r   r   s      r   r   zStdoutProgressIndicator._write  s|    #d04;>???
D72T9:::
+dk9:::
r   N)r)   r[   r\   r]   r   r   r   r   r_   r   r   r   r   n  sZ           
 
 
  
    r   r   Fc                    t          j        dd          }|@t           j                            d          }t           j                            |          sd}d}t
          j                            d          r3t          j        d          t          j        d          }}|r|p|n|p|}n@t
          j                            d          r!t           j                            |d	d
          }|rt           j                            |          s|}t
          j	        }t          t
          dd          rFt           j                            t           j                            t
          j                            }dD ]}t           j                            t           j                            ||                    }t           j                            |          r	 t          t           j                            |d          d                                           t          j        t           j                            |d                     |} n# t"          $ r Y w xY w| rs||k    rd|                     d          z   } t           j                            ||           }t           j                            |          st          j        |d           |S )a  appdata_dir(appname=None, roaming=False)

    Get the path to the application directory, where applications are allowed
    to write user specific files (e.g. configurations). For non-user specific
    data, consider using common_appdata_dir().
    If appname is given, a subdir is appended (and created if necessary).
    If roaming is True, will prefer a roaming directory (Windows Vista/7).
    IMAGEIO_USERDIRN~z/var/tmpwinLOCALAPPDATAAPPDATAdarwinLibraryzApplication Supportfrozen)settingsz../settingsz
test.writewb.T)exist_ok)osgetenvpath
expanduserisdirr   platformr*   joinprefixr@   abspathdirname
executableopencloseremoveIOErrorlstripmakedirs)	appnameroaminguserDirr   path1path2r   reldir	localpaths	            r   appdata_dirr     so    i)400G'$$S))w}}W%% 	! G D
|u%% Gy00")I2F2Fu#*@%		 	 	*	* Gw||GY0EFF RW]]4((  ZFsHd## B!@!@AA- 
 
GOOBGLL$@$@AA	7==## 	RW\\)\::DAAGGIII	"',,y,??@@@ !	    		  -7??GNN3///Gw||D'**w}}T"" 	-Kt,,,, Ks   A2I
IIc                  ,   t                      g} 	 |                     t          d                     n# t          $ r Y nw xY wt	          t
          dd          rZ|                     t          j                            t          j        	                    t
          j
                                       n`t
          j        rTt
          j        d         rB|                     t          j                            t
          j        d                              | S )a  resource_dirs()

    Get a list of directories where imageio resources may be located.
    The first directory in this list is the "resources" directory in
    the package itself. The second directory is the appdata directory
    (~/.imageio on Linux). The list further contains the application
    directory (for frozen apps), and may include additional directories
    in the future.
    r   r   Nr   )resource_package_dirappendr   	Exceptionr@   r   r   r   r   r   r   )dirss    r   resource_dirsr     s     !""#DK	**++++    sHd## 2BGOOBGOOCN$C$CDDEEEE	 2chqk 2BGOOCHQK00111Ks   "4 
A Ac                      	 ddl } n# t          $ r d} Y nw xY w| r|                     dd          }nCt          j                            t          j                            t          dd                    }|S )zpackage_dir

    Get the resources directory in the imageio package installation
    directory.

    Notes
    -----
    This is a convenience method that is used by `resource_dirs` and
    imageio entry point scripts.
    r   Nr   	resourcesz..)pkg_resourcesr	   resource_filenamer   r   r   r   THIS_DIR)r   pdirs     r   r   r     s     	     J ..y+FF wrw||HdKHHIIKs    c                  J   t           j                            d          rd} nht           j                            d          rd} nFt           j                            d          rd} n$t           j                            d          rd} nd	S | t          j        d
          dz  z  S )zget_platform()

    Get a string that specifies the platform more specific than
    sys.platform does. The result can be: linux32, linux64, win32,
    win64, osx32, osx64. Other platforms may be added in the future.
    linuxzlinux%ir   zwin%ir   zosx%ifreebsdz	freebsd%iNPr   )r   r   r*   structcalcsize)plats    r   get_platformr  
  s     |w'' 			 	 	'	' 		 	 	*	* 		 	 	+	+ t6?3''!+,,r   c           	      h   t           j        dk    ruddl}|                     d          }t	          t          |                    D ]=}|j                            d                    |d|dz                                 dS >dS ddl	}	 |
                    |            n# t          $ r Y dS w xY wdS )z-Check to see if a python module is available.)r      r   Nr   r   FT)r   version_info	importlibsplitranger   util	find_specr   impfind_moduler	   )module_namer  
name_partsir
  s        r   
has_moduler     s    
&   &&s++
s:'' 	 	A~''Ga!eG1D(E(EFFNuu Ot


	OOK(((( 	 	 	55	ts   B! !
B/.B/)r   rZ   )NF)$r]   collectionsr   numpyr"   r   rq   r   r   r   logging	getLoggerr   builtin_module_namesra   r   r   r   __file__r   r   r   r:   r#   r<   Imagerc   rI   rh   r   r   r   r   r   r  r  r_   r   r   <module>r     s   
 $ # # # # #     				 				  



   
	9	%	%
0
07??27??84455	$ 	$ 	$0 0 0 0? ? ? ?D: : : : :BJ : : :z 	
 
 
)/ )/ )/ )/ )/; )/ )/ )/XC C C C CF C C CL) ) ) ) )3 ) ) )Z6 6 6 6r  2  :- - -,    r   