U
    PeH                     @   s"  d dl Z d dlZd dlZd dlZd dlmZ d dlmZ dZdZ	dd ej
D Zdekr~zd d	lmZ eed< W n   Y nX d
d
d
gd d d gd
d d gd d
d gd d d
gd
d
d gd d
d
gd
d d
gd d d gdddgdddgdddgdddgdddgdddgdddgdddgdddgdddgdddgdddgdddgdddgd
d
d
gdZedZdd Zdd Zdd  Zd!d" Zd5d#d$Zd%d& ZG d'd( d(eZee jee	 d)G d*d+ d+ejZG d,d- d-ejZed.krd/d0 ej d1d D Z!e!rd2d e!D Zd3Zej"ej dd1 d4 dS )6    N)defaultdict)ImageLoaderFzimage-testsuitec                 C   s   i | ]}|j |qS  )__name__.0xr   r   ?/tmp/pip-unpacked-wheel-xzebddm3/kivy/tests/test_imageloader.py
<dictcomp>   s      r
   ImageLoaderPygame)r         "   3   D   U   f   w                        )wr   rgbycp0123456789ABCDEFzp^v0_(\d+)x(\d+)_([wxrgbycptA-F0-9]+)_([0-9a-fA-F]{2})_([a-zA-Z0-9-]+)_([a-zA-Z0-9-]+)_([a-zA-Z0-9-]+)\.([a-z]+)$c                  G   s"   t jt jjt jtf|  S N)ospathabspathjoindirname__file__)fnr   r   r	   asset)   s    r:   c                 C   s   | dkS )Nrgbabgraargbabgrr   fmtr   r   r	   	has_alpha-   s    rB   c                 C   s*   | dkrdS | dkrdS t d| d S )N)rgbbgr   r;      z"bytes_per_pixel: unknown format {})	Exceptionformatr@   r   r   r	   bytes_per_pixel1   s
    rI   c                 C   s$   |dkr| d S |dkr | d S dS )N)r<   r=   rE   )r?   r>   r   r   r   )pixrA   r   r   r	   get_pixel_alpha9   s
    rK   c           	         s  t  ttfst  dkr&|dks.tdt | d ksPtd |dks\t|dkrh S  fddtdt dD }|d	krd
dd |D S |dkrd
dd |D S |dkrd
dd |D S |d krd d d@ }n|dkrd }d
}d|d   } fddtdt d D D ]rfddtdtdD }|dkr|d
dd |D 7 }n"|dkr|d
dd |D 7 }||7 }q>|S )Nr   zMust specify w and hrF   zInvalid rgba data {})r<   r=   r>   r?   rC   rD   r<   c                    s   g | ]} ||d   qS rF   r   r   i)pix_inr   r	   
<listcomp>L   s     zrgba_to.<locals>.<listcomp>r=       c                 S   s2   g | ]*}t |d d d d d |dd   qS NrE   bytesr   r!   r   r   r	   rP   N   s     r?   c                 S   s2   g | ]*}t |d d |dd  ddd  qS )rE   NrS   rT   rV   r   r   r	   rP   P   s     r>   c                 S   s(   g | ] }t |d d |dd   qS )rE   NrT   rV   r   r   r	   rP   R   s     rE       c                    s    g | ]} ||d    qS rL   r   rM   )rO   r   r   r	   rP   ]   s     c                    s   g | ]} ||d   qS rL   r   rM   )rowr   r	   rP   ^   s     rC   c                 S   s   g | ]}t |d d qS )NrE   rT   rV   r   r   r	   rP   `   s     rD   c                 S   s&   g | ]}t |d d d d d qS rR   rT   rV   r   r   r	   rP   b   s     )
isinstancerU   	bytearrayAssertionErrorlenrH   ranger6   )	rO   Z
target_fmtr   hpitchpixelsoutpaddingZpixelrowr   )rO   rY   r   r	   rgba_toB   s8    "
(

rd   c              	   C   sJ  t |d dkstt |d dks(tt|}|d | }|d krR|d d@ }n|dkrf|d | }|| }g }|j}t | ||d  kr|d	||d t |  d}	d}
|d D ]X}tt| |	|	|  }t ||kr|d
|t ||  q|dkr0t||dkr|d|
t||| ndt| tt	|d  }t
||dddd}tt|}|r~|r~||kr|d|
|||| |r |
d |d  dkr tt| |	| |	| |  }|dg| kr|d||
|	| | |	|7 }	|	|7 }	|
d7 }
q|	t | kr:|d|
|	t | || t |dk|fS )Nalpha   patternr   r   rE   rW   r_   z2Pitch error: pitch {} * {} height != {} pixelbytesz#Want {} bytes per pixel, got {}: {}tz+pixel {} nonzero 't' pixel alpha {:02X}: {}   )r`   z3pixel {} {} format mismatch: want {} ({}) -- got {}z2Want {} 0x00 pitch align pixnum={}, pos={} got: {}z<Excess data: pixnum={} ptr={} bytes={}, bpp={} pitchalign={})r]   r\   rI   appendrH   listr[   rK   	v0_PIXELSfromhexrd   )ra   rA   fdr`   ZbppZrowlenZ
pitchalignerrorsfailptrZpixnumcharrJ   ZsrcpixZpredictcheckr   r   r	   match_predictiong   s        
          
    rt   c                   @   sf   e Zd Zdd Zedd Zedd Zdd Zdd
dZdd Z	dd Z
dd Zdd Zdd Zd	S )_TestContextc                 C   s2   || _ d | _d | _d| _d| _d| _tt| _d S )Nr   )		loadercls_fd_fn_ok_skip_failr   dict_stats)selfrv   r   r   r	   __init__   s    z_TestContext.__init__c                 C   s   | j S r2   )r}   r~   r   r   r	   stats   s    z_TestContext.statsc                 C   s   | j | j| j| jfS r2   )ry   rz   r{   r}   r   r   r   r	   results   s    z_TestContext.resultsc                 C   s,   | j rtdt|tst|| _ || _d S )Nz'unexpected ctx.start(), already started)rx   r\   rZ   r|   rw   )r~   r9   rn   r   r   r	   start   s    z_TestContext.startNc                 C   s&   |r| j |kstdd | _ d | _d S )Nz!unexpected ctx.end(), fn mismatch)rx   r\   rw   )r~   r9   r   r   r	   end   s    z_TestContext.endc                 C   sB   | j std|  jd7  _| d| | d | | j  d S )Nzunexpected ctx.ok(), fn=Noneri   ZPASSok)rx   r\   ry   dbg_incstatr   r~   infor   r   r	   r      s
    
z_TestContext.okc                 C   sB   | j std|  jd7  _| d| | d | | j  d S )Nzunexpected ctx.skip(), fn=Noneri   ZSKIPskip)rx   r\   rz   r   r   r   r   r   r   r	   r      s
    
z_TestContext.skipc                 C   sB   | j std|  jd7  _| d| | d | | j  d S )Nzunexpected ctx.fail(), fn=Noneri   FAILrp   )rx   r\   r{   r   r   r   r   r   r   r	   rp      s
    
z_TestContext.failc                 C   s0   | j stdtr,td| jj|| j | d S )Nzunexpected ctx.dbg(), fn=Nonez{} {} {}: {})rx   r\   DEBUGprintrH   rv   r   )r~   Zmsgtyper   r   r   r	   r      s       z_TestContext.dbgc                    s   j stdj } fdd}|d |d|d  |d|d  |d	|d
  |d|d  |d|d |d  |d|d |d  |d|d |d  |d|d
 |d  d S )Nz"unexpected ctx._incstat(), fd=Nonec                    s"   j  tt|   d7  < d S )Nri   )r}   
setdefaultr   int)keysr~   r   r	   IS   s    z!_TestContext._incstat.<locals>.IStotalzextension:{}extz
encoder:{}encoderz
fmtinfo:{}fmtinfoztestname:{}testnameztestname+ext:{}+{}zencoder+ext:{}+{}zencoder+testname:{}+{}zfmtinfo+ext:{}+{})rw   r\   rH   )r~   r   rn   r   r   r   r	   r      s    z_TestContext._incstat)N)r   
__module____qualname__r   propertyr   r   r   r   r   r   rp   r   r   r   r   r   r	   ru      s   	


ru   z'Need 'make image-testsuite' to run testc                   @   s~   e Zd Zdd Zdd Zdd Zddd	Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )ImageLoaderTestCasec                 C   s   d | _ |   d S r2   )_context_prepare_imagesr   r   r   r	   setUp   s    zImageLoaderTestCase.setUpc           
      C   s   t r
| jsd S | j}|jj}|j}tdd | D }|j}t|D ]T}|di |di |di   }}}	t	d
||||d||d|	|d qFd S )Nc                 S   s   g | ]}|  D ]}|qqS r   )keys)r   r   kr   r   r	   rP      s     
  z0ImageLoaderTestCase.tearDown.<locals>.<listcomp>r   r   rp   z%REPORT {} {}: ok={}, skip={}, fail={}r   )r   r   rv   r   r   setvaluesgetsortedr   rH   )
r~   ctxZilr   r   Zsgr   r   r   rp   r   r   r	   tearDown   s     
"  
 
 
zImageLoaderTestCase.tearDownc                 C   s   t | drd S i | _tttD ]`}t|}|s6q"| \}}}}}}}	}
|t	|t	||||||	|
d|kpvd|kd
| j|< q"d S )N_image_filesBINARYZALPHA)
filenamer   r_   rg   re   r   r   r   r   require_alpha)
hasattrr   r3   listdirr:   ASSETDIR
v0_FILE_REmatchgroupsr   )r~   r   matchesr   r_   patre   r   Ztstr   r   r   r   r	   r      s&    

z#ImageLoaderTestCase._prepare_imagesNc                 C   s   |sd S |s|  }t|}|| _t| j D ]~}| j| }|d |krLq0z.||| |tt|dd}|sxt	dW n   |
d Y q0Y nX | |||| |  q0|j\}}}	}
|	r| d|j|||	 |S )Nr   T)Z	keep_datazinvalid resultzError loading file, result=Nonez${}: {} passed, {} skipped, {} failed)
extensionsru   r   r   r   r   r   r:   r   rG   r   _test_imager   r   rp   rH   r   )r~   rv   r   r   r   Zfiledataresultr   r   rp   r   r   r   r	   _test_imageloader  s:    



   z%ImageLoaderTestCase._test_imageloaderc           	   
      s  |j d d\|j d jtts6t fdd}dkrbt pddkrt  tkr|dd	tt  t n"rֈkr|dd	 t
 \}}|sV|s|d nPt|dkr||d  n2|D ]}|d	| q |d
	t||d  |  n d r|ts||d |  n d kr|d	 d  |  nf d kr|d	 d  |  n:dkrdkr|d |  n|d	 tj  d S )Nr   c                      sT   t sd S td td td d  tdt d S )Nz    format: {}x{} {}z     pitch: got {}, want {}z      want: {} in {}rg   z       got: {})r   r   rH   r[   r   rn   rA   r_   r`   ra   r   Z
want_pitchr   r	   debug:  s    z.ImageLoaderTestCase._test_image.<locals>.debugZPITCHzEpitch=0, expected fmt={} to be unaligned @ ({}bpp) = {} bytes, got {}zfmt={}, pitch={}, expected {}zUnknown errorri   ZPREDICTz{} errors, see debug output: {}rS   r   zMissing expected alpha channelr   zWidth mismatch, want {} got {}r_   zHeight mismatch, want {} got {}z$v0 test protocol mandates w=1 or h=1zPassed test as {}x{} {})_dataZ
get_mipmaprA   rZ   rU   r[   rI   r]   r   rH   rt   rp   rB   r   sysstdoutflush)	r~   rn   r   rv   Zimgdatar   successZmsgsmr   r   r	   r   1  sh    
	 
 
   
  
zImageLoaderTestCase._test_imagec                 C   s0   t d}|r,t| dg }| ||}d S )NZImageLoaderSDL2Zgif)LOADERSr   rk   r   r   )r~   rv   extsr   r   r   r	   test_ImageLoaderSDL2l  s    
z(ImageLoaderTestCase.test_ImageLoaderSDL2c                 C   s   t d}| |}d S )NZImageLoaderPILr   r   r   r~   rv   r   r   r   r	   test_ImageLoaderPILs  s    
z'ImageLoaderTestCase.test_ImageLoaderPILc                 C   s   t d}| |}d S )Nr   r   r   r   r   r	   test_ImageLoaderPygamew  s    
z*ImageLoaderTestCase.test_ImageLoaderPygamec                 C   s   t d}| |}d S )NZImageLoaderFFPyr   r   r   r   r	   test_ImageLoaderFFPy{  s    
z(ImageLoaderTestCase.test_ImageLoaderFFPyc                 C   s   t d}| |}d S )NZImageLoaderGIFr   r   r   r   r	   test_ImageLoaderGIF  s    
z'ImageLoaderTestCase.test_ImageLoaderGIFc                 C   s   t d}| |}d S )NZImageLoaderDDSr   r   r   r   r	   test_ImageLoaderDDS  s    
z'ImageLoaderTestCase.test_ImageLoaderDDSc                 C   s   t d}| |}d S )NZImageLoaderTexr   r   r   r   r	   test_ImageLoaderTex  s    
z'ImageLoaderTestCase.test_ImageLoaderTexc                 C   s   t d}| |}d S )NZImageLoaderImageIOr   r   r   r   r	   test_ImageLoaderImageIO  s    
z+ImageLoaderTestCase.test_ImageLoaderImageIOc                 C   sN   t jD ]B}d|j}d|}| t| || | tt| || qd S )Nztest_{}z!Missing ImageLoader test case: {})r   loadersrH   r   
assertTruer   callablegetattr)r~   loaderr   msgr   r   r	   test_missing_tests  s
    

z&ImageLoaderTestCase.test_missing_tests)N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   r      s   
;r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )ConverterTestCasec              	   C   s  ddddddddd	}|j }|d
}| t|d
ddd|d
 | t|dddd|d | t|dddd|d | t|dddd|d | t|dddd|d | t|dddd|d | t|dddd |d | t|dddd |d d S )Ns   s   s   s   s   s   s     s     )r<   r?   r=   r>   rC   rD   
rgb_align4
bgr_align4r<   rf   ri   r   r?   r=   r>   rC   rD   r   r   r   assertEqualrd   )r~   correctsrcr<   r   r   r	   test_internal_converter_2x1  s&    	z-ConverterTestCase.test_internal_converter_2x1c              	   C   s   d}ddddddd| d| d}|j }|d	}| t|d
ddd|d
 | t|dddd|d | t|dddd|d | t|dddd|d | t|dddd|d | t|dddd|d | t|dddd|d d S )Ns            	s   	s   	s   	s
   	 s
   	 )r<   r?   r=   r>   
rgb_align2
bgr_align2
rgb_align8
bgr_align8r<   r=   rE   ri   r   r>   r?   rC   
   r   rD   r      r   r   r   )r~   Zpad6r   r   r<   r   r   r	   test_internal_converter_3x1  s&    	z-ConverterTestCase.test_internal_converter_3x1c              
   C   s6  d}dddddddd| d | d	 | d
| d | d | d	}|j }|d}| t|dddd|d | t|dddd|d | t|dddd |d | t|dddd |d | t|dddd|d | t|dddd|d | t|dddd|d | t|dddd|d d S )Ns        r   s	   	s	   	s     	 s     	 s   s   s   	s   s   s   	)	r<   rgb_rawbgr_rawr   r   r   r   r   r   r<   rC   ri   rE   rF   r   rD   r   r   r   r   r   r      r   r   r   )r~   Zpad5r   r   r<   r   r   r	   test_internal_converter_1x3  sJ    z-ConverterTestCase.test_internal_converter_1x3N)r   r   r   r   r   r   r   r   r   r	   r     s   r   __main__c                 C   s   g | ]}d  |qS )zImageLoader{})rH   r   r   r   r	   rP     s     rP   ri   c                 C   s   i | ]}|t | qS r   )r   r   r   r   r	   r
     s      T)argv)N)#r3   rer   Zunittestcollectionsr   Zkivy.core.imager   r   r   r   r   Zkivy.core.image.img_pygamer   rl   compiler   r:   rB   rI   rK   rd   rt   objectru   ZskipIfr4   isdirZTestCaser   r   r   r   Zaccept_filtermainr   r   r   r	   <module>   sv                   
	
%8J -H
