src/test/SDL_test_compare.c
author Andreas Schiffler
Sat, 22 Dec 2012 16:06:55 -0800
changeset 6763 9cbd31a3450b
child 6772 5229a117ef97
permissions -rw-r--r--
Test lib updates: updated harness to support filtering, added surface comparer, updated interface to test images; added Render test suite from GSOC project
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 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 int SDLTest_CompareSurfaces( SDL_Surface *surface, SDL_Surface *referenceSurface, int allowable_error )
    36 {
    37    int ret;
    38    int i,j;
    39    int bpp, bpp_reference;
    40    Uint8 *p, *p_reference;
    41    int dist;
    42    Uint8 R, G, B, A;
    43    Uint8 Rd, Gd, Bd, Ad;
    44 
    45    /* Make surfacee size is the same. */
    46    if ((surface->w != referenceSurface->w) || (surface->h != referenceSurface->h))
    47    {
    48       return -1;
    49    }
    50 
    51    SDL_LockSurface( surface );
    52    SDL_LockSurface( referenceSurface );
    53 
    54    ret = 0;
    55    bpp = surface->format->BytesPerPixel;
    56    bpp_reference = referenceSurface->format->BytesPerPixel;
    57 
    58    /* Compare image - should be same format. */
    59    for (j=0; j<surface->h; j++) {
    60       for (i=0; i<surface->w; i++) {
    61          p  = (Uint8 *)surface->pixels + j * surface->pitch + i * bpp;
    62          p_reference = (Uint8 *)referenceSurface->pixels + j * referenceSurface->pitch + i * bpp_reference;
    63          dist = 0;
    64 
    65          SDL_GetRGBA(*(Uint32*)p, surface->format, &R, &G, &B, &A);
    66          SDL_GetRGBA(*(Uint32*)p_reference, referenceSurface->format, &Rd, &Gd, &Bd, &Ad);
    67 
    68          dist += (R-Rd)*(R-Rd);
    69          dist += (G-Gd)*(G-Gd);
    70          dist += (B-Bd)*(B-Bd);
    71                   
    72          /* Allow some difference in blending accuracy */
    73          if (dist > allowable_error) {
    74             ret++;
    75          }
    76       }
    77    }
    78 
    79    SDL_UnlockSurface( surface );
    80    SDL_UnlockSurface( referenceSurface );
    81 
    82    return ret;
    83 }