SDL_test_compare.c (3462B)
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2014 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 */ 36static int _CompareSurfaceCount = 0; 37 38/* Compare surfaces */ 39int 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 */ 97 _CompareSurfaceCount++; 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}