o
    hDM                     @   s  d Z ddlZddlZddlZddlZddlZddlmZ ddlZddl	Z	ddl
Z
ddlmZmZ ddlZddlZddlZddlmZ ddlZddlmZ ddlmZ eeZg dZd	d
 Zdd Zdd Zd1ddZ G dd de!Z"G dd dZ#G dd de#Z$G dd de#Z%G dd de%Z&dd Z'i Z(e'  e& Z)dd Z*d d! Z+d"d# Z,ej-d$d% Z.d&d' Z/d(d) Z0d*d+ Z1d2d-d.Z2d/d0 Z3dS )3z(
Utilities for comparing image results.
    N)Path)TemporaryDirectoryTemporaryFile)Image)cbook)ImageComparisonFailure)calculate_rmscomparable_formatscompare_imagesc                 C   s"   t j| \}}| d| | S )zQ
    Make a new filename by inserting *purpose* before the file's extension.
    -)ospathsplitext)fnamepurposebaseext r   n/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/matplotlib/testing/compare.pymake_test_filename   s   r   c                  C   s    t t d} | jddd | S )N
test_cacheT)parentsexist_ok)r   mplget_cachedirmkdir)	cache_dirr   r   r   _get_cache_path&   s   r   c                   C   s
   t t S N)strr   r   r   r   r   get_cache_dir,   s   
r       c                 C   s   t jdd}t| d}	 ||}|sn|| qW d    n1 s%w   Y  t| jdkrC|tt	dj
d | S t| jdkrX|tt	d	j
d | S )
NF)usedforsecurityrbTz.pdfgszutf-8.svginkscape)hashlibsha256openreadupdater   suffixr   r   _get_executable_infoversionencode	hexdigest)r   
block_sizer(   fddatar   r   r   get_file_hash0   s    

r4   c                   @   s   e Zd ZdS )_ConverterErrorN)__name__
__module____qualname__r   r   r   r   r5   A   s    r5   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )
_Converterc                 C   s   d | _ t| j d S r   )_procatexitregister__del__selfr   r   r   __init__F   s   z_Converter.__init__c                 C   sR   | j r'| j   | j   td | j j| j j| j jgD ]}|  qd | _ d S d S r   )r:   killwaitfilterstdinstdoutstderrclose)r?   streamr   r   r   r=   N   s   





z_Converter.__del__c                 C   sJ   t  }	 | jjd}|sttt||| |	|r$t|S q)z!Read until the prompt is reached.T   )
	bytearrayr:   rE   r*   r5   r   fsdecodebytesextendendswith)r?   
terminatorbufcr   r   r   _read_untilX   s   

z_Converter._read_untilN)r6   r7   r8   r@   r=   rR   r   r   r   r   r9   E   s    
r9   c                   @   s   e Zd Zdd ZdS )_GSConverterc              
   C   s  | j s6tjtdjddddgtjtjd| _ z| d W n ty5 } zt	d|j
d	  d d }~ww d
d }| j jd|| d || d  | j j  | d}|drb| dnd}|sltj|s|rvt|d d nd	}| j jd|  t|| t dd S )Nr$   z	-dNOSAFERz	-dNOPAUSEz	-dEPSCropz-sDEVICE=png16m)rD   rE   s   
GSzFailed to start Ghostscript:

r   c                 S   s"   t | ddddddS )N   \s   \\   (s   \(   )s   \))r   fsencodereplace)namer   r   r   encode_and_escapeq   s
   z0_GSConverter.__call__.<locals>.encode_and_escapes   << /OutputFile (s   ) >> setpagedevice (s   ) run flush
)   GS<s   GS>r[      >    s   pop
rX   )r:   
subprocessPopenr   r-   
executablePIPErR   r5   OSErrorargsrD   writeflushrN   r   r   existsintr   decodesysgetfilesystemencoding)r?   origdesterZ   errstack
stack_sizer   r   r   __call__e   sF   

z_GSConverter.__call__N)r6   r7   r8   rr   r   r   r   r   rS   d   s    rS   c                       s$   e Zd Zdd Z fddZ  ZS )_SVGConverterc              
   C   s  t djjdk }|rdnd}t| ds t | _t| j| j	 | j
r*| j
 d ur| j
d urK| j
 d urKtd | j
j| j
j| j
jgD ]}|  qDi tjd| jjd}t }tj|rbg dndd	gtjtj||| jjd
| _
|| j
_z| | W n ty } z
td|jd  |d }~ww t| jjtd}	t| jjtd}
z|	t|  W n ty   t !||	 Y nw | j
j"|rdnd | j
j#  z| | W n" ty } z| j
j$d t%| j
j& 't() d|d }~ww t*|	 t +|
| d S )Nr&   rI   s   
>s   > _tmpdir )DISPLAYINKSCAPE_PROFILE_DIR)r&   z--without-gui--shellrx   )rD   rE   rF   envcwdz/Failed to start Inkscape in interactive mode:

r   s   f.svgs   f.pngs   f.svg --export-png=f.png
s;   file-open:f.svg;export-filename:f.png;export-do;file-close
rX   ),r   r-   r.   majorhasattrr   rt   weakreffinalizer=   r:   pollrC   rD   rE   rF   rG   r   environrY   r   r_   r`   rb   rR   r5   rc   rd   r   rK   
symlink_toresolveshutilcopyfilere   rf   seekr   r*   ri   rj   rk   removemove)r?   rl   rm   old_inkscaperO   rH   ry   rF   ro   inkscape_originkscape_destr   r   r   rr      s   





z_SVGConverter.__call__c                    s&   t    t| dr| j  d S d S )Nrt   )superr=   r|   rt   cleanupr>   	__class__r   r   r=      s   

z_SVGConverter.__del__)r6   r7   r8   rr   r=   __classcell__r   r   r   r   rs      s    Grs   c                       s    e Zd ZdZ fddZ  ZS ) _SVGWithMatplotlibFontsConverterz
    A SVG converter which explicitly adds the fonts shipped by Matplotlib to
    Inkspace's font search path, to better support `svg.fonttype = "none"`
    (which is in particular used by certain mathtext tests).
    c                    s<   t | dst | _ttdt| jjd t	 
||S )Nrt   z	fonts/ttffonts)r|   r   rt   r   copytreer   _get_data_pathr   rY   r   rr   )r?   rl   rm   r   r   r   rr      s   
z)_SVGWithMatplotlibFontsConverter.__call__)r6   r7   r8   __doc__rr   r   r   r   r   r   r      s    r   c                   C   sj   zt d W n
 t jy   Y n
w t  td< td< zt d W n t jy-   Y d S w t td< d S )Nr$   pdfepsr&   svg)r   r-   ExecutableNotFoundErrorrS   	converterrs   r   r   r   r   _update_converter   s   r   c                   C   s
   dgt S )z
    Return the list of file formats that `.compare_images` can compare
    on this system.

    Returns
    -------
    list of str
        E.g. ``['png', 'pdf', 'svg', 'eps']``.

    png)r   r   r   r   r   r	      s   
r	   c           
      C   sV  t | }| st| d|jdd tvr&ddl}|d|j d |j|j d|jdd  d }| rE|	 j
|	 j
k r|rJt nd}|durrt  t|}|||j  }| rrtd	|  t|| t|S td
|  t|jdd  }|jdkr| }	td|	rt}||| |durtd|  t|| t|S )a  
    Convert the named file to png; return the name of the created file.

    If *cache* is True, the result of the conversion is cached in
    `matplotlib.get_cachedir() + '/test_cache/'`.  The caching is based on a
    hash of the exact contents of the input file.  Old cache entries are
    automatically deleted as needed to keep the size of the cache capped to
    twice the size of all baseline images.
    z does not existrI   Nr   zDon't know how to convert z files to png_z.pngz"For %s: reusing cached conversion.zFor %s: converting to png.r%   z9style="[^"]*font(|-size|-weight|-family|-variant|-style):z"For %s: caching conversion result.)r   rg   rc   r,   r   pytestskipparentstemstatst_mtimer   '_register_conversion_cache_cleaner_oncer4   _logdebugr   r   r   	read_textresearch$_svg_with_matplotlib_fonts_converter)
filenamecacher   r   newpathr   
hash_valuecached_pathconvertcontentsr   r   r   r   
  s>   
"

r   c                     s   t dd ttjjdD } d|  }tt I dd t dD  t dd  	 D }t
  fd	d
dd}||kr[| }| | j8 }|  ||ks@W d    d S W d    d S 1 sfw   Y  d S )Nc                 s   s    | ]}|  jV  qd S r   )r   st_size.0r   r   r   r   	<genexpr>K  s
    
z*_clean_conversion_cache.<locals>.<genexpr>z**/baseline_images/**/*   c                 S   s   i | ]}||  qS r   )r   r   r   r   r   
<dictcomp>S  s    
z+_clean_conversion_cache.<locals>.<dictcomp>*c                 s   s    | ]}|j V  qd S r   )r   )r   r   r   r   r   r   U  s    c                    s
    |  j S r   )st_atime)r   
cache_statr   r   <lambda>W  s   
 z)_clean_conversion_cache.<locals>.<lambda>T)keyreverse)sumr   r   __file__r   globr   
_lock_pathr   valuessortedpopr   unlink)baseline_images_sizemax_cache_size
cache_sizepaths_by_atimer   r   r   r   _clean_conversion_cacheH  s*   

"r   c                   C   s   t t d S r   )r;   r<   r   r   r   r   r   r   _  s   r   c           
      C   s   | dd dkrF|dd dkrF|j \}}}|j \}}}	|t|d |d  t|d |d  t|d |d  t|d |d  f }||fS )Nir   r   r   )shaperh   )
actual_pathactual_imageexpected_pathexpected_imageawahadewehedr   r   r   crop_to_samed  s    &r   c                 C   s@   | j |j krtd| j  d|j  t| | td  S )zR
    Calculate the per-pixel errors, then compute the root mean square error.
    (Image sizes do not match expected size:  actual size r   )r   r   npsqrtastypefloatmean)r   r   r   r   r   r   o  s   
r   c                 C   s<   t | }|jdks| d d dkr|d}t|S )NRGBA   r      RGB)r   r)   mode
getextremar   r   asarray)r   imgr   r   r   _load_image  s   


r   Fc           
         s^  t |}t j|std| dt |jdkr#td| dt | } t j| s6td| d| dd }|d	krMt	|d
d}t	| d
d} t
| }t
|}t||| |\}}t|d}|dkrot||rodS |tj}|tj}t||}||krdS t| || t|t| t|t||d |sg d}	d fdd|	D   S )a+  
    Compare two "image" files checking differences within a tolerance.

    The two given filenames may point to files which are convertible to
    PNG via the `.converter` dictionary. The underlying RMS is calculated
    with the `.calculate_rms` function.

    Parameters
    ----------
    expected : str
        The filename of the expected image.
    actual : str
        The filename of the actual image.
    tol : float
        The tolerance (a color value difference, where 255 is the
        maximal difference).  The test fails if the average pixel
        difference is greater than this value.
    in_decorator : bool
        Determines the output format. If called from image_comparison
        decorator, this should be True. (default=False)

    Returns
    -------
    None or dict or str
        Return *None* if the images are equal within the given tolerance.

        If the images differ, the return value depends on  *in_decorator*.
        If *in_decorator* is true, a dict with the following entries is
        returned:

        - *rms*: The RMS of the image difference.
        - *expected*: The filename of the expected image.
        - *actual*: The filename of the actual image.
        - *diff_image*: The filename of the difference image.
        - *tol*: The comparison tolerance.

        Otherwise, a human-readable multi-line string representation of this
        information is returned.

    Examples
    --------
    ::

        img1 = "./baseline/plot.png"
        img2 = "./output/plot.png"
        compare_images(img1, img2, 0.001)

    zOutput image z does not exist.r   zOutput image file z
 is empty.zBaseline image .r^   r   T)r   zfailed-diffN)rmsexpectedactualdifftol)z!Error: Image files did not match.zRMS Value: {rms}zExpected:  
    {expected}zActual:    
    {actual}zDifference:
    {diff}zTolerance: 
    {tol}z
  c                    s   g | ]
}|j d i  qS )r   format)r   lineresultsr   r   
<listcomp>  s    z"compare_images.<locals>.<listcomp>)r   fspathr   rg   	Exceptionr   r   rc   splitr   r   r   r   r   array_equalr   int16r   save_diff_imagedictr   join)
r   r   r   in_decorator	extensionr   r   
diff_imager   templater   r   r   r
     sD   
1



r
   c                 C   s   t | }t |}t||| |\}}t|t}t|t}|j|jkr/td|j d|j t|| }|d9 }t|dd	tj
}|jd dkrWd|ddddd	f< t|j|d
d dS )z
    Parameters
    ----------
    expected : str
        File path of expected image.
    actual : str
        File path of actual image.
    output : str
        File path to save difference image to.
    r   r   
   r   r   r      Nr   r   r   )r   r   r   arrayr   r   r   absclipr   uint8r   	fromarraysave)r   r   outputr   r   abs_diffr   r   r   r     s&   
r   )r!   )F)4r   r;   	functoolsr'   loggingr   pathlibr   r   r_   rj   tempfiler   r   r}   r   numpyr   PILr   
matplotlibr   r   matplotlib.testing.exceptionsr   	getLoggerr6   r   __all__r   r   r    r4   r   r5   r9   rS   rs   r   r   r   r   r	   r   r   r   r   r   r   r   r
   r   r   r   r   r   <module>   sV    

%N>


h