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

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

Compares a surface and with reference image data for equality.

Parameters

  surface | Surface used in comparison
  referenceSurface | Test Surface used in comparison
  allowable_error | Allowable 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, 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().

{
int ret;
int i,j;
int bpp, bpp_reference;
Uint8 *p, *p_reference;
int dist;
int sampleErrorX, sampleErrorY, sampleDist;
Uint8 R, G, B, A;
Uint8 Rd, Gd, Bd, Ad;
char imageFilename[128];
char referenceFilename[128];
/* Validate input surfaces */
if (surface == NULL || referenceSurface == NULL) {
return -1;
}
/* Make sure surface size is the same. */
if ((surface->w != referenceSurface->w) || (surface->h != referenceSurface->h)) {
return -2;
}
/* Sanitize input value */
if (allowable_error<0) {
allowable_error = 0;
}
SDL_LockSurface( surface );
SDL_LockSurface( referenceSurface );
ret = 0;
bpp = surface->format->BytesPerPixel;
bpp_reference = referenceSurface->format->BytesPerPixel;
/* Compare image - should be same format. */
for (j=0; j<surface->h; j++) {
for (i=0; i<surface->w; i++) {
p = (Uint8 *)surface->pixels + j * surface->pitch + i * bpp;
p_reference = (Uint8 *)referenceSurface->pixels + j * referenceSurface->pitch + i * bpp_reference;
SDL_GetRGBA(*(Uint32*)p, surface->format, &R, &G, &B, &A);
SDL_GetRGBA(*(Uint32*)p_reference, referenceSurface->format, &Rd, &Gd, &Bd, &Ad);
dist = 0;
dist += (R-Rd)*(R-Rd);
dist += (G-Gd)*(G-Gd);
dist += (B-Bd)*(B-Bd);
/* Allow some difference in blending accuracy */
if (dist > allowable_error) {
ret++;
if (ret == 1) {
sampleErrorX = i;
sampleErrorY = j;
sampleDist = dist;
}
}
}
}
SDL_UnlockSurface( surface );
SDL_UnlockSurface( referenceSurface );
/* Save test image and reference for analysis on failures */
_CompareSurfaceCount++;
if (ret != 0) {
SDLTest_LogError("Comparison of pixels with allowable error of %i failed %i times.", allowable_error, ret);
SDLTest_LogError("First detected occurrence at position %i,%i with a squared RGB-difference of %i.", sampleErrorX, sampleErrorY, sampleDist);
SDL_snprintf(imageFilename, 127, "CompareSurfaces%04d_TestOutput.bmp", _CompareSurfaceCount);
SDL_SaveBMP(surface, imageFilename);
SDL_snprintf(referenceFilename, 127, "CompareSurfaces%04d_Reference.bmp", _CompareSurfaceCount);
SDL_SaveBMP(referenceSurface, referenceFilename);
SDLTest_LogError("Surfaces from failed comparison saved as '%s' and '%s'", imageFilename, referenceFilename);
}
return ret;
}
SDL_UnlockSurface
#define SDL_UnlockSurface
Definition: SDL_dynapi_overrides.h:453
Uint32
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:157
SDL_Surface::h
int h
Definition: SDL_surface.h:73
SDL_PixelFormat::BytesPerPixel
Uint8 BytesPerPixel
Definition: SDL_pixels.h:304
p
GLfloat GLfloat p
Definition: SDL_opengl_glext.h:11090
SDL_SaveBMP
#define SDL_SaveBMP(surface, file)
Definition: SDL_surface.h:199
SDL_Surface::w
int w
Definition: SDL_surface.h:73
_CompareSurfaceCount
static int _CompareSurfaceCount
Definition: SDL_test_compare.c:36
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:89
SDL_Surface::pixels
void * pixels
Definition: SDL_surface.h:75
Uint8
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
SDL_GetRGBA
#define SDL_GetRGBA
Definition: SDL_dynapi_overrides.h:295
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:42
NULL
#define NULL
Definition: begin_code.h:143
SDL_Surface::format
SDL_PixelFormat * format
Definition: SDL_surface.h:72
SDL_LockSurface
#define SDL_LockSurface
Definition: SDL_dynapi_overrides.h:452
G
#define G(x, y, z)
Definition: SDL_test_md5.c:74
SDL_Surface::pitch
int pitch
Definition: SDL_surface.h:74
SDL_snprintf
#define SDL_snprintf
Definition: SDL_dynapi_overrides.h:46
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:42