
    hDM                        S r SSKrSSKrSSKrSSKrSSKrSSKJr  SSKrSSK	r	SSK
r
SSKJrJr  SSKrSSKrSSKrSSKJr  SSKrSSKJr  SSKJr  \R2                  " \5      r/ SQrS	 rS
 rS rS!S jr  " S S\!5      r" " S S5      r# " S S\#5      r$ " S S\#5      r% " S S\%5      r&S r'0 r(\'" 5         \&" 5       r)S r*S r+S r,\RZ                  S 5       r.S r/S r0S r1S"S jr2S  r3g)#z(
Utilities for comparing image results.
    N)Path)TemporaryDirectoryTemporaryFile)Image)cbook)ImageComparisonFailure)calculate_rmscomparable_formatscompare_imagesc                 V    [         R                  R                  U 5      u  p#U SU U 3$ )zI
Make a new filename by inserting *purpose* before the file's extension.
-)ospathsplitext)fnamepurposebaseexts       L/var/www/html/env/lib/python3.13/site-packages/matplotlib/testing/compare.pymake_test_filenamer      s/       'IDV1WIcU##    c                  d    [        [        R                  " 5       S5      n U R                  SSS9  U $ )N
test_cacheT)parentsexist_ok)r   mplget_cachedirmkdir)	cache_dirs    r   _get_cache_pathr    &   s-    S%%'6IOOD4O0r   c                  (    [        [        5       5      $ N)strr     r   r   get_cache_dirr%   ,   s     !!r   c                    [         R                  " SS9n[        U S5       n UR                  U5      nU(       d  OUR	                  U5        M,  S S S 5        [        U 5      R                  S:X  aW  UR	                  [        [        R                  " S5      R                  5      R                  S5      5        UR                  5       $ [        U 5      R                  S:X  aG  UR	                  [        [        R                  " S5      R                  5      R                  S5      5        UR                  5       $ ! , (       d  f       N= f)	NF)usedforsecurityrbz.pdfgszutf-8.svginkscape)hashlibsha256openreadupdater   suffixr#   r   _get_executable_infoversionencode	hexdigest)r   
block_sizer-   fddatas        r   get_file_hashr9   0   s    ^^E2F	dD	R77:&DMM$	  
 DzF"c#2248@@AHHQR  
d		f	$c#22:>FFGNNwWX 
	s   .D77
Ec                       \ rS rSrSrg)_ConverterErrorA   r$   N)__name__
__module____qualname____firstlineno____static_attributes__r$   r   r   r;   r;   A   s    r   r;   c                   &    \ rS rSrS rS rS rSrg)
_ConverterE   c                 R    S U l         [        R                  " U R                  5        g r"   )_procatexitregister__del__)selfs    r   __init___Converter.__init__F   s    

 	%r   c                 b   U R                   (       a  U R                   R                  5         U R                   R                  5         [        S U R                   R                  U R                   R
                  U R                   R                  /5       H  nUR                  5         M     S U l         g g r"   )rF   killwaitfilterstdinstdoutstderrclose)rJ   streams     r   rI   _Converter.__del__N   sz    ::JJOOJJOO 

(8(8(,

(9(9(,

(9(9(; < < DJ r   c                 (   [        5       n U R                  R                  R                  S5      nU(       d(  [	        [
        R                  " [        U5      5      5      eUR                  U5        UR                  U5      (       a  [        U5      $ M  )z!Read until the prompt is reached.   )
	bytearrayrF   rR   r/   r;   r   fsdecodebytesextendendswith)rJ   
terminatorbufcs       r   _read_until_Converter._read_untilX   sj    k

!!&&q)A%bkk%*&=>>JJqM||J''Sz! r   rF   N)r=   r>   r?   r@   rK   rI   ra   rA   r$   r   r   rC   rC   E   s    &	"r   rC   c                       \ rS rSrS rSrg)_GSConverterd   c                    U R                   (       dl  [        R                  " [        R                  " S5      R
                  SSSS/[        R                  [        R                  S9U l          U R                  S5        S
 nU R                   R                  R                  SU" U5      -   S-   U" U5      -   S-   5        U R                   R                  R                  5         U R                  S5      nUR                  S5      (       a  U R                  S5      OSnU(       d$  [        R                   R#                  U5      (       do  U(       a  [%        US S 5      OS	nU R                   R                  R                  SU-  5        ['        XV-   R)                  [*        R,                  " 5       S5      5      eg ! [         a!  n[        SUR                  S	    35      S eS nAff = f)Nr)   z	-dNOSAFERz	-dNOPAUSEz	-dEPSCropz-sDEVICE=png16m)rQ   rR   s   
GSzFailed to start Ghostscript:

r   c                     [         R                  " U 5      R                  SS5      R                  SS5      R                  SS5      $ )N   \s   \\   (s   \(   )s   \))r   fsencodereplace)names    r   encode_and_escape0_GSConverter.__call__.<locals>.encode_and_escapeq   s7    KK%WUG,WT6*WT6*,r   s   << /OutputFile (s   ) >> setpagedevice (s   ) run flush
)   GS<s   GS>rq      >r   s   pop
rm   )rF   
subprocessPopenr   r2   
executablePIPEra   r;   OSErrorargsrQ   writeflushr]   r   r   existsintr   decodesysgetfilesystemencoding)rJ   origdestero   errstack
stack_sizes           r   __call___GSConverter.__call__e   s   zz#))))$/::k;8IK !oojoo	?DJ
X  )	, 	

%&%&  %& 		  	

 /0*-,,v*>*>  &Ct,,,1U3BZqJJJ""8j#89($$S%>%>%@)LN N	 -' # X @LMSWWXs   -F7 7
G"GG"rc   N)r=   r>   r?   r@   r   rA   r$   r   r   re   re   d   s    !Nr   re   c                   .   ^  \ rS rSrS rU 4S jrSrU =r$ )_SVGConverter   c           	         [         R                  " S5      R                  R                  S:  nU(       a  SOSn[	        U S5      (       d:  [        5       U l        [        R                  " U R                  U R                  5        U R                  (       a  U R                  R                  5       Gb;  U R                  b}  U R                  R                  5       bb  [        S U R                  R                  U R                  R                  U R                  R                  /5       H  nUR!                  5         M     0 ["        R$                  ESU R                  R&                  S.En[)        5       n[*        R,                  " U(       a  / SQOSS	/[*        R.                  [*        R.                  UX`R                  R&                  S
9U l
        XpR                  l         U R1                  U5        [9        U R                  R&                  ["        R:                  " S5      5      n	[9        U R                  R&                  ["        R:                  " S5      5      n
 U	R=                  [9        U5      R?                  5       5        U R                  R                  RE                  U(       a  SOS5        U R                  R                  RG                  5          U R1                  U5        ["        RT                  " U	5        [@        RV                  " X5        g ! [2         a!  n[5        SUR6                  S   -   5      UeS nAff = f! [4         a    [@        RB                  " X5         Nf = f! [2         a{  nU R                  R                  RI                  S5        [K        U R                  R                  RM                  5       RO                  [P        RR                  " 5       S5      5      UeS nAff = f)Nr+   rX   s   
>s   > _tmpdir )DISPLAYINKSCAPE_PROFILE_DIR)r+   z--without-gui--shellr   )rQ   rR   rS   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
rm   ),r   r2   r3   majorhasattrr   r   weakreffinalizerI   rF   pollrP   rQ   rR   rS   rT   r   environrn   r   rt   ru   rw   ra   r;   rx   ry   r   rZ   
symlink_toresolveshutilcopyfilerz   r{   seekr   r/   r~   r   r   removemove)rJ   r   r   old_inkscaper^   rU   r   rS   r   inkscape_originkscape_dests              r   r   _SVGConverter.__call__   s   //
;CCIIAM+V
tY''-/DL T\\4<<8

::??$0zz%$**//*;*G$TDJJ,<,<,0JJ,=,=,0JJ,=,=,? @F LLN@
**
 (,(9(9
C #_F#))<H8Y' oojoof\\..	0DJ !'JJ,  , T\\..H0EFT\\..H0EF	1$$T$Z%7%7%9: 	

-9)K	M 	

 		FZ( 			- M(; # ,Ghhqk"#(+,,  	1OOD0	1  	F JJ""1%(

!!&&(//--/<=BEF	FsC   L (L< M" 
L9L44L9< MM"
O',A6O""O'c                 z   > [         TU ]  5         [        U S5      (       a  U R                  R	                  5         g g )Nr   )superrI   r   r   cleanup)rJ   	__class__s    r   rI   _SVGConverter.__del__   s/    4##LL  " $r   )rF   r   )r=   r>   r?   r@   r   rI   rA   __classcell__r   s   @r   r   r      s    E)N# #r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ ) _SVGWithMatplotlibFontsConverter   z
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                    > [        U S5      (       dX  [        5       U l        [        R                  " [
        R                  " S5      [        U R                  R                  S5      5        [        TU ])  X5      $ )Nr   z	fonts/ttffonts)r   r   r   r   copytreer   _get_data_pathr   rn   r   r   )rJ   r   r   r   s      r   r   )_SVGWithMatplotlibFontsConverter.__call__   sW    tY''-/DLOOE00= !2!2G<>w++r   )r   )r=   r>   r?   r@   __doc__r   rA   r   r   s   @r   r   r      s    , ,r   r   c                      [         R                  " S5        [        5       =[        S'   [        S'    [         R                  " S5        [        5       [        S'   g ! [         R                   a     N?f = f! [         R                   a     g f = f)Nr)   pdfepsr+   svg)r   r2   re   	converterExecutableNotFoundErrorr   r$   r   r   _update_converterr      s    =  & /;n<	%9U++  , )?	% &&  && s"   A A5 A21A25BBc                      S/[         Q$ )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     Ir   c                 4   [        U 5      nUR                  5       (       d  [        U S35      eUR                  SS [        ;  a#  SSKnUR                  SUR                   S35        UR                  UR                   SUR                  SS  S3-  nUR                  5       (       a7  UR                  5       R                  UR                  5       R                  :  Ga.  U(       a
  [        5       OSnUbr  [        5         [        U5      nXVUR                  -   -  nUR                  5       (       a7  [        R                  S	U 5        [         R"                  " Xt5        [%        U5      $ [        R                  S
U 5        [        UR                  SS    nUR                  S:X  a2  UR'                  5       n	[(        R*                  " SU	5      (       a  [,        nU" X$5        Ub-  [        R                  SU 5        [         R"                  " UW5        [%        U5      $ )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 existrX   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   r|   rx   r1   r   pytestskipparentstemstatst_mtimer    '_register_conversion_cache_cleaner_oncer9   _logdebugr   r   r#   	read_textresearch$_svg_with_matplotlib_fonts_converter)
filenamecacher   r   newpathr   
hash_valuecached_pathconvertcontentss
             r   r   r   
  s    >D;;==o.//{{12i'0]KLkktyyk4;;qr?*;4@@G >>w||~669M9MM).O%D	 35&t,J#GNN'BCK!!##

?J57|#

/:DKKO,;;& ~~'H yy M  ? JJ;XFOOG[1w<r   c                  X  ^ [        S [        [        R                  5      R                  R                  S5       5       5      n SU -  n[        R                  " [        5       5         [        5       R                  S5       Vs0 s H  o"UR                  5       _M     snm[        S TR                  5        5       5      n[        TU4S jSS9nX1:  a9  UR                  5       nUTU   R                  -  nUR                  5         X1:  a  M9  S S S 5        g s  snf ! , (       d  f       g = f)	Nc              3   V   #    U  H  nUR                  5       R                  v   M!     g 7fr"   )r   st_size).0r   s     r   	<genexpr>*_clean_conversion_cache.<locals>.<genexpr>K  s$      OMD 			Ms   ')z**/baseline_images/**/*   *c              3   8   #    U  H  oR                   v   M     g 7fr"   )r   )r   r   s     r   r   r   U  s     F2E$2Es   c                 "   > TU    R                   $ r"   )st_atime)r   
cache_stats    r   <lambda>)_clean_conversion_cache.<locals>.<lambda>W  s    D)9)B)Br   T)keyreverse)sumr   r   __file__r   globr   
_lock_pathr    r   valuessortedpopr   unlink)baseline_images_sizemax_cache_sizer   
cache_sizepaths_by_atimer   s        @r   _clean_conversion_cacher   H  s     O&--223LMO O
 --N			/+	,*9*;*@*@*EG*E$$))+*EG
F*2C2C2EFF
B )!%%'D*T*222JKKM ) 
-	,G 
-	,s   'DDA.DD
D)c                  8    [         R                  " [        5        g r"   )rG   rH   r   r$   r   r   r   r   _  s    
OO+,r   c                    U SS S:X  az  USS S:X  aq  UR                   u  pEnUR                   u  pxn	U[        US-  US-  -
  5      [        US-  US-  -   5      2[        US-  US-  -
  5      [        US-  US-  -   5      24   nX4$ )Nir   r   r   )shaper}   )
actual_pathactual_imageexpected_pathexpected_imageawahadeweheds
             r   crop_to_samer   d  s     2bU"}R';u'D!''
#))
#CQa$8FR!VO: %!"q&26/23rAvQ3GG%H I''r   c                     U R                   UR                   :w  a%  [        SU R                    SUR                    35      e[        R                  " X-
  R	                  [
        5      S-  R                  5       5      $ )zJ
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   s     r   r	   r	   o  sw     |111$6~7K7K6L M'--.01 	1 77^2::5AQFLLNOOr   c                     [         R                  " U 5      nUR                  S:w  d  UR                  5       S   S   S:X  a  UR	                  S5      n[
        R                  " U5      $ )NRGBA   r      RGB)r   r.   mode
getextremar   r  asarray)r   imgs     r   _load_imager    sR    
**T
C xx6S^^-a03s:kk% ::c?r   c           
         [         R                  " U5      n[         R                  R                  U5      (       d  [	        SU S35      e[         R
                  " U5      R                  S:X  a  [	        SU S35      e[         R                  " U 5      n [         R                  R                  U 5      (       d  [        SU < S35      eU R                  S5      S   nUS	:w  a  [        US
S9n[        U S
S9n [        U 5      n[        U5      n[        XX5      u  pe[        US5      nUS::  a  [        R                  " XV5      (       a  gUR                  [        R                   5      nUR                  [        R                   5      n[#        XV5      nX::  a  g[%        XU5        ['        U[)        U 5      [)        U5      [)        U5      US9n	U(       d5  / SQn
SR+                  U
 Vs/ s H  oR,                  " S0 U	D6PM     sn5      n	U	$ s  snf )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 .rs   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
  r$   )r   fspathr   r|   	Exceptionr   r   rx   splitr   r  r   r   r  array_equalr  int16r	   save_diff_imagedictr#   joinformat)r  r  r  in_decorator	extensionr   r   
diff_imager  resultstemplatelines               r   r   r     s   b YYvF77>>&!!-x/?@AA	wwv!#,VHJ?@@ yy"H77>>(##|3CDEEs#B'IEt,840 !*Nv&L#/h$8 L $FM:J
ax>>.77 $**2884N&&rxx0L

5C
zHj1sS]f+C
OFG 0 ++(K(${{5W5(KLN Ls   $Hc                 `   [        U 5      n[        U5      n[        XX5      u  pC[        R                  " U[        5      n[        R                  " U[        5      nUR
                  UR
                  :w  a%  [        SUR
                   SUR
                   35      e[        R                  " X4-
  5      nUS-  n[        R                  " USS5      R                  [        R                  5      nUR
                  S   S:X  a  SUSS2SS2S	4'   [        R                  " U5      R                  US
S9  g)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_diffs         r   r  r    s    !*Nv&L#/h$8 LXXne4N88L%0L|111$6~7K7K6L M'--.01 	1 vvn34H NHwwxC(//9H~~aAAq	OOH""6%"8r   )i   )F)4r   rG   	functoolsr,   loggingr   pathlibr   r   rt   r   tempfiler   r   r   r   numpyr  PILr   
matplotlibr   r   matplotlib.testing.exceptionsr   	getLoggerr=   r   __all__r   r    r%   r9   r  r;   rC   re   r   r   r   r   r   r
   r   r   r   r   r   r	   r  r   r  r$   r   r   <module>r=     s       	    
 6  	     @"
C$""	i 	" ">"N: "NJK#J K#\,} ,+" 	  'G'I $;|. - -(	P eP9r   