src/test/SDL_test_compare.c
changeset 6763 9cbd31a3450b
child 6772 5229a117ef97
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/test/SDL_test_compare.c	Sat Dec 22 16:06:55 2012 -0800
     1.3 @@ -0,0 +1,83 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +/*
    1.26 +
    1.27 + Based on automated SDL_Surface tests originally written by Edgar Simo 'bobbens'.
    1.28 + 
    1.29 + Rewritten for test lib by Andreas Schiffler.
    1.30 +
    1.31 +*/
    1.32 +
    1.33 +#include "SDL_config.h"
    1.34 +
    1.35 +#include "SDL_test.h"
    1.36 +
    1.37 +
    1.38 +int SDLTest_CompareSurfaces( SDL_Surface *surface, SDL_Surface *referenceSurface, int allowable_error )
    1.39 +{
    1.40 +   int ret;
    1.41 +   int i,j;
    1.42 +   int bpp, bpp_reference;
    1.43 +   Uint8 *p, *p_reference;
    1.44 +   int dist;
    1.45 +   Uint8 R, G, B, A;
    1.46 +   Uint8 Rd, Gd, Bd, Ad;
    1.47 +
    1.48 +   /* Make surfacee size is the same. */
    1.49 +   if ((surface->w != referenceSurface->w) || (surface->h != referenceSurface->h))
    1.50 +   {
    1.51 +      return -1;
    1.52 +   }
    1.53 +
    1.54 +   SDL_LockSurface( surface );
    1.55 +   SDL_LockSurface( referenceSurface );
    1.56 +
    1.57 +   ret = 0;
    1.58 +   bpp = surface->format->BytesPerPixel;
    1.59 +   bpp_reference = referenceSurface->format->BytesPerPixel;
    1.60 +
    1.61 +   /* Compare image - should be same format. */
    1.62 +   for (j=0; j<surface->h; j++) {
    1.63 +      for (i=0; i<surface->w; i++) {
    1.64 +         p  = (Uint8 *)surface->pixels + j * surface->pitch + i * bpp;
    1.65 +         p_reference = (Uint8 *)referenceSurface->pixels + j * referenceSurface->pitch + i * bpp_reference;
    1.66 +         dist = 0;
    1.67 +
    1.68 +         SDL_GetRGBA(*(Uint32*)p, surface->format, &R, &G, &B, &A);
    1.69 +         SDL_GetRGBA(*(Uint32*)p_reference, referenceSurface->format, &Rd, &Gd, &Bd, &Ad);
    1.70 +
    1.71 +         dist += (R-Rd)*(R-Rd);
    1.72 +         dist += (G-Gd)*(G-Gd);
    1.73 +         dist += (B-Bd)*(B-Bd);
    1.74 +                  
    1.75 +         /* Allow some difference in blending accuracy */
    1.76 +         if (dist > allowable_error) {
    1.77 +            ret++;
    1.78 +         }
    1.79 +      }
    1.80 +   }
    1.81 +
    1.82 +   SDL_UnlockSurface( surface );
    1.83 +   SDL_UnlockSurface( referenceSurface );
    1.84 +
    1.85 +   return ret;
    1.86 +}