SDL  2.0
SDL_test_compare.c File Reference
#include "SDL_config.h"
#include "SDL_test.h"
+ Include dependency graph for SDL_test_compare.c:

Go to the source code of this file.

Functions

int SDLTest_CompareSurfaces (SDL_Surface *surface, SDL_Surface *referenceSurface, int allowable_error)
 Compares a surface and with reference image data for equality. More...
 

Variables

static int _CompareSurfaceCount = 0
 

Function Documentation

◆ SDLTest_CompareSurfaces()

int SDLTest_CompareSurfaces ( SDL_Surface surface,
SDL_Surface referenceSurface,
int  allowable_error 
)

Compares a surface and with reference image data for equality.

Parameters
surfaceSurface used in comparison
referenceSurfaceTest Surface used in comparison
allowable_errorAllowable difference (=sum of squared difference for each RGB component) in blending accuracy.
Returns
0 if comparison succeeded, >0 (=number of pixels for which the comparison failed) if comparison failed, -1 if any of the surfaces were NULL, -2 if the surface sizes differ.

Definition at line 39 of file SDL_test_compare.c.

40 {
41  int ret;
42  int i,j;
43  int bpp, bpp_reference;
44  Uint8 *p, *p_reference;
45  int dist;
46  int sampleErrorX = 0, sampleErrorY = 0, sampleDist = 0;
47  Uint8 R, G, B, A;
48  Uint8 Rd, Gd, Bd, Ad;
49  char imageFilename[128];
50  char referenceFilename[128];
51 
52  /* Validate input surfaces */
53  if (surface == NULL || referenceSurface == NULL) {
54  return -1;
55  }
56 
57  /* Make sure surface size is the same. */
58  if ((surface->w != referenceSurface->w) || (surface->h != referenceSurface->h)) {
59  return -2;
60  }
61 
62  /* Sanitize input value */
63  if (allowable_error<0) {
64  allowable_error = 0;
65  }
66 
69 
70  ret = 0;
71  bpp = surface->format->BytesPerPixel;
72  bpp_reference = referenceSurface->format->BytesPerPixel;
73  /* Compare image - should be same format. */
74  for (j=0; j<surface->h; j++) {
75  for (i=0; i<surface->w; i++) {
76  p = (Uint8 *)surface->pixels + j * surface->pitch + i * bpp;
77  p_reference = (Uint8 *)referenceSurface->pixels + j * referenceSurface->pitch + i * bpp_reference;
78 
79  SDL_GetRGBA(*(Uint32*)p, surface->format, &R, &G, &B, &A);
80  SDL_GetRGBA(*(Uint32*)p_reference, referenceSurface->format, &Rd, &Gd, &Bd, &Ad);
81 
82  dist = 0;
83  dist += (R-Rd)*(R-Rd);
84  dist += (G-Gd)*(G-Gd);
85  dist += (B-Bd)*(B-Bd);
86 
87  /* Allow some difference in blending accuracy */
88  if (dist > allowable_error) {
89  ret++;
90  if (ret == 1) {
91  sampleErrorX = i;
92  sampleErrorY = j;
93  sampleDist = dist;
94  }
95  }
96  }
97  }
98 
101 
102  /* Save test image and reference for analysis on failures */
104  if (ret != 0) {
105  SDLTest_LogError("Comparison of pixels with allowable error of %i failed %i times.", allowable_error, ret);
106  SDLTest_LogError("First detected occurrence at position %i,%i with a squared RGB-difference of %i.", sampleErrorX, sampleErrorY, sampleDist);
107  SDL_snprintf(imageFilename, 127, "CompareSurfaces%04d_TestOutput.bmp", _CompareSurfaceCount);
108  SDL_SaveBMP(surface, imageFilename);
109  SDL_snprintf(referenceFilename, 127, "CompareSurfaces%04d_Reference.bmp", _CompareSurfaceCount);
110  SDL_SaveBMP(referenceSurface, referenceFilename);
111  SDLTest_LogError("Surfaces from failed comparison saved as '%s' and '%s'", imageFilename, referenceFilename);
112  }
113 
114  return ret;
115 }

References _CompareSurfaceCount, bpp, SDL_PixelFormat::BytesPerPixel, SDL_Surface::format, G, SDL_Surface::h, i, j, NULL, SDL_Surface::pitch, SDL_Surface::pixels, referenceSurface, SDL_GetRGBA, SDL_LockSurface, SDL_SaveBMP, SDL_snprintf, SDL_UnlockSurface, SDLTest_LogError(), and SDL_Surface::w.

Referenced by _compare(), surface_testBlit(), surface_testBlitAlphaMod(), surface_testBlitBlendAdd(), surface_testBlitBlendBlend(), surface_testBlitBlendLoop(), surface_testBlitBlendMod(), surface_testBlitBlendNone(), surface_testBlitColorMod(), surface_testCompleteSurfaceConversion(), and surface_testSurfaceConversion().

Variable Documentation

◆ _CompareSurfaceCount

int _CompareSurfaceCount = 0
static

Definition at line 36 of file SDL_test_compare.c.

Referenced by SDLTest_CompareSurfaces().

SDL_UnlockSurface
#define SDL_UnlockSurface
Definition: SDL_dynapi_overrides.h:449
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
SDL_PixelFormat::BytesPerPixel
Uint8 BytesPerPixel
Definition: SDL_pixels.h:323
NULL
#define NULL
Definition: begin_code.h:167
surface
EGLSurface surface
Definition: eglext.h:248
referenceSurface
static SDL_Surface * referenceSurface
Definition: testautomation_surface.c:27
SDL_Surface::pixels
void * pixels
Definition: SDL_surface.h:76
SDL_Surface::w
int w
Definition: SDL_surface.h:74
_CompareSurfaceCount
static int _CompareSurfaceCount
Definition: SDL_test_compare.c:36
SDL_Surface::pitch
int pitch
Definition: SDL_surface.h:75
p
GLfloat GLfloat p
Definition: SDL_opengl_glext.h:11093
SDL_GetRGBA
#define SDL_GetRGBA
Definition: SDL_dynapi_overrides.h:289
bpp
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp
Definition: pixman-arm-neon-asm.h:146
SDL_LockSurface
#define SDL_LockSurface
Definition: SDL_dynapi_overrides.h:448
SDL_Surface::h
int h
Definition: SDL_surface.h:74
SDL_snprintf
#define SDL_snprintf
Definition: SDL_dynapi_overrides.h:40
SDLTest_LogError
void SDLTest_LogError(SDL_PRINTF_FORMAT_STRING const char *fmt,...) SDL_PRINTF_VARARG_FUNC(1)
Prints given message with a timestamp in the TEST category and the ERROR priority.
Definition: SDL_test_log.c:103
j
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
Definition: SDL_x11sym.h:50
G
#define G(x, y, z)
Definition: SDL_test_md5.c:74
SDL_SaveBMP
#define SDL_SaveBMP(surface, file)
Definition: SDL_surface.h:224
SDL_Surface::format
SDL_PixelFormat * format
Definition: SDL_surface.h:73
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:179