src/test/SDL_test_compare.c
author Andreas Schiffler
Mon, 24 Dec 2012 14:43:57 -0800
changeset 6772 5229a117ef97
parent 6763 9cbd31a3450b
child 6773 5ebc5a9e35b4
permissions -rw-r--r--
Added audio test suite; minor code cleanups in test lib
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@6772
    45
   /* Validate input surfaces */
aschiffler@6772
    46
   if (surface == NULL || referenceSurface == NULL) {
aschiffler@6763
    47
      return -1;
aschiffler@6763
    48
   }
aschiffler@6763
    49
aschiffler@6772
    50
   /* Make surface size is the same. */
aschiffler@6772
    51
   if ((surface->w != referenceSurface->w) || (surface->h != referenceSurface->h)) {
aschiffler@6772
    52
      return -2;
aschiffler@6772
    53
   }
aschiffler@6772
    54
aschiffler@6772
    55
   /* Sanitize input */
aschiffler@6772
    56
   if (allowable_error<0) {
aschiffler@6772
    57
      allowable_error = 0;
aschiffler@6772
    58
   }
aschiffler@6772
    59
   
aschiffler@6763
    60
   SDL_LockSurface( surface );
aschiffler@6763
    61
   SDL_LockSurface( referenceSurface );
aschiffler@6763
    62
aschiffler@6763
    63
   ret = 0;
aschiffler@6763
    64
   bpp = surface->format->BytesPerPixel;
aschiffler@6763
    65
   bpp_reference = referenceSurface->format->BytesPerPixel;
aschiffler@6763
    66
   /* Compare image - should be same format. */
aschiffler@6763
    67
   for (j=0; j<surface->h; j++) {
aschiffler@6763
    68
      for (i=0; i<surface->w; i++) {
aschiffler@6763
    69
         p  = (Uint8 *)surface->pixels + j * surface->pitch + i * bpp;
aschiffler@6763
    70
         p_reference = (Uint8 *)referenceSurface->pixels + j * referenceSurface->pitch + i * bpp_reference;
aschiffler@6763
    71
aschiffler@6763
    72
         SDL_GetRGBA(*(Uint32*)p, surface->format, &R, &G, &B, &A);
aschiffler@6763
    73
         SDL_GetRGBA(*(Uint32*)p_reference, referenceSurface->format, &Rd, &Gd, &Bd, &Ad);
aschiffler@6763
    74
aschiffler@6772
    75
         dist = 0;
aschiffler@6763
    76
         dist += (R-Rd)*(R-Rd);
aschiffler@6763
    77
         dist += (G-Gd)*(G-Gd);
aschiffler@6763
    78
         dist += (B-Bd)*(B-Bd);
aschiffler@6763
    79
                  
aschiffler@6763
    80
         /* Allow some difference in blending accuracy */
aschiffler@6763
    81
         if (dist > allowable_error) {
aschiffler@6763
    82
            ret++;
aschiffler@6763
    83
         }
aschiffler@6763
    84
      }
aschiffler@6763
    85
   }
aschiffler@6763
    86
aschiffler@6763
    87
   SDL_UnlockSurface( surface );
aschiffler@6763
    88
   SDL_UnlockSurface( referenceSurface );
aschiffler@6763
    89
aschiffler@6763
    90
   return ret;
aschiffler@6763
    91
}