SDL  2.0
SDL_test_compare.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 /*
23 
24  Based on automated SDL_Surface tests originally written by Edgar Simo 'bobbens'.
25 
26  Rewritten for test lib by Andreas Schiffler.
27 
28 */
29 
30 #include "SDL_config.h"
31 
32 #include "SDL_test.h"
33 
34 
35 /* Counter for _CompareSurface calls; used for filename creation when comparisons fail */
36 static int _CompareSurfaceCount = 0;
37 
38 /* Compare surfaces */
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 }
116 
117 /* vi: set ts=4 sw=4 expandtab: */
int SDLTest_CompareSurfaces(SDL_Surface *surface, SDL_Surface *referenceSurface, int allowable_error)
Compares a surface and with reference image data for equality.
#define SDL_UnlockSurface
Uint8 BytesPerPixel
Definition: SDL_pixels.h:320
GLfloat GLfloat p
EGLSurface surface
Definition: eglext.h:248
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_SaveBMP(surface, file)
Definition: SDL_surface.h:223
uint32_t Uint32
Definition: SDL_stdinc.h:181
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:157
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
#define G(x, y, z)
Definition: SDL_test_md5.c:74
#define SDL_snprintf
static SDL_Surface * referenceSurface