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
aschiffler@6763
     1
/*
aschiffler@6763
     2
  Simple DirectMedia Layer
aschiffler@6763
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
aschiffler@6763
     4
aschiffler@6763
     5
  This software is provided 'as-is', without any express or implied
aschiffler@6763
     6
  warranty.  In no event will the authors be held liable for any damages
aschiffler@6763
     7
  arising from the use of this software.
aschiffler@6763
     8
aschiffler@6763
     9
  Permission is granted to anyone to use this software for any purpose,
aschiffler@6763
    10
  including commercial applications, and to alter it and redistribute it
aschiffler@6763
    11
  freely, subject to the following restrictions:
aschiffler@6763
    12
aschiffler@6763
    13
  1. The origin of this software must not be misrepresented; you must not
aschiffler@6763
    14
     claim that you wrote the original software. If you use this software
aschiffler@6763
    15
     in a product, an acknowledgment in the product documentation would be
aschiffler@6763
    16
     appreciated but is not required.
aschiffler@6763
    17
  2. Altered source versions must be plainly marked as such, and must not be
aschiffler@6763
    18
     misrepresented as being the original software.
aschiffler@6763
    19
  3. This notice may not be removed or altered from any source distribution.
aschiffler@6763
    20
*/
aschiffler@6763
    21
aschiffler@6763
    22
/*
aschiffler@6763
    23
aschiffler@6763
    24
 Based on automated SDL_Surface tests originally written by Edgar Simo 'bobbens'.
aschiffler@6763
    25
 
aschiffler@6763
    26
 Rewritten for test lib by Andreas Schiffler.
aschiffler@6763
    27
aschiffler@6763
    28
*/
aschiffler@6763
    29
aschiffler@6763
    30
#include "SDL_config.h"
aschiffler@6763
    31
aschiffler@6763
    32
#include "SDL_test.h"
aschiffler@6763
    33
aschiffler@6763
    34
aschiffler@6763
    35
int SDLTest_CompareSurfaces( SDL_Surface *surface, SDL_Surface *referenceSurface, int allowable_error )
aschiffler@6763
    36
{
aschiffler@6763
    37
   int ret;
aschiffler@6763
    38
   int i,j;
aschiffler@6763
    39
   int bpp, bpp_reference;
aschiffler@6763
    40
   Uint8 *p, *p_reference;
aschiffler@6763
    41
   int dist;
aschiffler@6763
    42
   Uint8 R, G, B, A;
aschiffler@6763
    43
   Uint8 Rd, Gd, Bd, Ad;
aschiffler@6763
    44
aschiffler@6763
    45
   /* Make surfacee size is the same. */
aschiffler@6763
    46
   if ((surface->w != referenceSurface->w) || (surface->h != referenceSurface->h))
aschiffler@6763
    47
   {
aschiffler@6763
    48
      return -1;
aschiffler@6763
    49
   }
aschiffler@6763
    50
aschiffler@6763
    51
   SDL_LockSurface( surface );
aschiffler@6763
    52
   SDL_LockSurface( referenceSurface );
aschiffler@6763
    53
aschiffler@6763
    54
   ret = 0;
aschiffler@6763
    55
   bpp = surface->format->BytesPerPixel;
aschiffler@6763
    56
   bpp_reference = referenceSurface->format->BytesPerPixel;
aschiffler@6763
    57
aschiffler@6763
    58
   /* Compare image - should be same format. */
aschiffler@6763
    59
   for (j=0; j<surface->h; j++) {
aschiffler@6763
    60
      for (i=0; i<surface->w; i++) {
aschiffler@6763
    61
         p  = (Uint8 *)surface->pixels + j * surface->pitch + i * bpp;
aschiffler@6763
    62
         p_reference = (Uint8 *)referenceSurface->pixels + j * referenceSurface->pitch + i * bpp_reference;
aschiffler@6763
    63
         dist = 0;
aschiffler@6763
    64
aschiffler@6763
    65
         SDL_GetRGBA(*(Uint32*)p, surface->format, &R, &G, &B, &A);
aschiffler@6763
    66
         SDL_GetRGBA(*(Uint32*)p_reference, referenceSurface->format, &Rd, &Gd, &Bd, &Ad);
aschiffler@6763
    67
aschiffler@6763
    68
         dist += (R-Rd)*(R-Rd);
aschiffler@6763
    69
         dist += (G-Gd)*(G-Gd);
aschiffler@6763
    70
         dist += (B-Bd)*(B-Bd);
aschiffler@6763
    71
                  
aschiffler@6763
    72
         /* Allow some difference in blending accuracy */
aschiffler@6763
    73
         if (dist > allowable_error) {
aschiffler@6763
    74
            ret++;
aschiffler@6763
    75
         }
aschiffler@6763
    76
      }
aschiffler@6763
    77
   }
aschiffler@6763
    78
aschiffler@6763
    79
   SDL_UnlockSurface( surface );
aschiffler@6763
    80
   SDL_UnlockSurface( referenceSurface );
aschiffler@6763
    81
aschiffler@6763
    82
   return ret;
aschiffler@6763
    83
}