zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SDL_test_compare.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2013 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 */
39 int SDLTest_CompareSurfaces(SDL_Surface *surface, SDL_Surface *referenceSurface, int allowable_error)
40 {
41  int ret;
42  int i,j;
43  int bpp, bpp_reference;
44  Uint8 *p, *p_reference;
45  int dist;
46  Uint8 R, G, B, A;
47  Uint8 Rd, Gd, Bd, Ad;
48  char imageFilename[128];
49  char referenceFilename[128];
50 
51  /* Validate input surfaces */
52  if (surface == NULL || referenceSurface == NULL) {
53  return -1;
54  }
55 
56  /* Make sure surface size is the same. */
57  if ((surface->w != referenceSurface->w) || (surface->h != referenceSurface->h)) {
58  return -2;
59  }
60 
61  /* Sanitize input value */
62  if (allowable_error<0) {
63  allowable_error = 0;
64  }
65 
66  SDL_LockSurface( surface );
67  SDL_LockSurface( referenceSurface );
68 
69  ret = 0;
70  bpp = surface->format->BytesPerPixel;
71  bpp_reference = referenceSurface->format->BytesPerPixel;
72  /* Compare image - should be same format. */
73  for (j=0; j<surface->h; j++) {
74  for (i=0; i<surface->w; i++) {
75  p = (Uint8 *)surface->pixels + j * surface->pitch + i * bpp;
76  p_reference = (Uint8 *)referenceSurface->pixels + j * referenceSurface->pitch + i * bpp_reference;
77 
78  SDL_GetRGBA(*(Uint32*)p, surface->format, &R, &G, &B, &A);
79  SDL_GetRGBA(*(Uint32*)p_reference, referenceSurface->format, &Rd, &Gd, &Bd, &Ad);
80 
81  dist = 0;
82  dist += (R-Rd)*(R-Rd);
83  dist += (G-Gd)*(G-Gd);
84  dist += (B-Bd)*(B-Bd);
85 
86  /* Allow some difference in blending accuracy */
87  if (dist > allowable_error) {
88  ret++;
89  }
90  }
91  }
92 
93  SDL_UnlockSurface( surface );
94  SDL_UnlockSurface( referenceSurface );
95 
96  /* Save test image and reference for analysis on failures */
98  if (ret != 0) {
99  SDL_snprintf(imageFilename, 127, "CompareSurfaces%04d_TestOutput.bmp", _CompareSurfaceCount);
100  SDL_SaveBMP(surface, imageFilename);
101  SDL_snprintf(referenceFilename, 127, "CompareSurfaces%04d_Reference.bmp", _CompareSurfaceCount);
102  SDL_SaveBMP(referenceSurface, referenceFilename);
103  SDLTest_LogError("Surfaces from failed comparison saved as '%s' and '%s'", imageFilename, referenceFilename);
104  }
105 
106  return ret;
107 }
int SDLTest_CompareSurfaces(SDL_Surface *surface, SDL_Surface *referenceSurface, int allowable_error)
Compares a surface and with reference image data for equality.
#define NULL
Definition: ftobjs.h:61
DECLSPEC int SDLCALL SDL_snprintf(char *text, size_t maxlen, const char *fmt,...)
Definition: SDL_string.c:1277
Uint8 BytesPerPixel
Definition: SDL_pixels.h:277
EGLSurface surface
Definition: eglext.h:74
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
int32_t j
Definition: e_log.c:102
#define SDL_SaveBMP(surface, file)
Definition: SDL_surface.h:199
static int _CompareSurfaceCount
ret
Definition: glew_str_glx.c:2
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:145
DECLSPEC void SDLCALL SDL_UnlockSurface(SDL_Surface *surface)
Definition: SDL_surface.c:786
void * pixels
Definition: SDL_surface.h:75
GLfloat GLfloat p
Definition: glew.h:14938
SDL_PixelFormat * format
Definition: SDL_surface.h:72
DECLSPEC void SDLCALL SDL_GetRGBA(Uint32 pixel, const SDL_PixelFormat *format, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
Get the RGBA components from a pixel of the specified format.
Definition: SDL_pixels.c:849
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:129
#define G(x, y, z)
Definition: SDL_test_md5.c:74
R
Definition: e_log.c:153
int i
Definition: pngrutil.c:1377
DECLSPEC int SDLCALL SDL_LockSurface(SDL_Surface *surface)
Sets up a surface for directly accessing the pixels.
Definition: SDL_surface.c:765
void SDLTest_LogError(const char *fmt,...)
Prints given message with a timestamp in the TEST category and the ERROR priority.
Definition: SDL_test_log.c:89