SDL 2.0
SDL_test_compare.h File Reference
#include "SDL.h"
#include "SDL_test_images.h"
#include "begin_code.h"
#include "close_code.h"
+ Include dependency graph for SDL_test_compare.h:
+ This graph shows which files directly or indirectly include this file:

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...
 

Detailed Description

Include file for SDL test framework.

This code is a part of the SDL2_test library, not the main SDL library.

Definition in file SDL_test_compare.h.

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}
#define SDL_UnlockSurface
#define SDL_LockSurface
#define SDL_GetRGBA
#define SDL_snprintf
GLfloat GLfloat p
uint32_t Uint32
Definition: SDL_stdinc.h:203
uint8_t Uint8
Definition: SDL_stdinc.h:179
#define SDL_SaveBMP(surface, file)
Definition: SDL_surface.h:224
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
#define G(x, y, z)
Definition: SDL_test_md5.c:74
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
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:167
EGLSurface surface
Definition: eglext.h:248
Uint8 BytesPerPixel
Definition: SDL_pixels.h:320
SDL_PixelFormat * format
Definition: SDL_surface.h:73
void * pixels
Definition: SDL_surface.h:76
static SDL_Surface * referenceSurface

References _CompareSurfaceCount, 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().