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.

References _CompareSurfaceCount, bpp, SDL_PixelFormat::BytesPerPixel, SDL_Surface::format, G, SDL_Surface::h, i, j, NULL, SDL_Surface::pitch, SDL_Surface::pixels, 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().

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 
67  SDL_LockSurface( surface );
68  SDL_LockSurface( referenceSurface );
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 
99  SDL_UnlockSurface( surface );
100  SDL_UnlockSurface( referenceSurface );
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 }
#define SDL_UnlockSurface
Uint8 BytesPerPixel
Definition: SDL_pixels.h:320
GLfloat GLfloat p
#define SDL_SaveBMP(surface, file)
Definition: SDL_surface.h:223
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
static int _CompareSurfaceCount
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
void * pixels
Definition: SDL_surface.h:75
uint8_t Uint8
Definition: SDL_stdinc.h:179
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
#define SDL_GetRGBA
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
#define NULL
Definition: begin_code.h:164
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_LockSurface
uint32_t Uint32
Definition: SDL_stdinc.h:203
#define G(x, y, z)
Definition: SDL_test_md5.c:74
#define SDL_snprintf

Variable Documentation

◆ _CompareSurfaceCount

int _CompareSurfaceCount = 0
static

Definition at line 36 of file SDL_test_compare.c.

Referenced by SDLTest_CompareSurfaces().