src/test/SDL_test_compare.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 04 Jun 2014 10:56:56 -0700
changeset 8820 0e935d5b193a
parent 8149 681eb46b8ac4
child 9254 6c469ea796e4
permissions -rw-r--r--
Added annotations to help code analysis tools

CR: Bruce Dawson
aschiffler@6763
     1
/*
aschiffler@6763
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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'.
slouken@7191
    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@6773
    35
/* Counter for _CompareSurface calls; used for filename creation when comparisons fail */
aschiffler@6773
    36
static int _CompareSurfaceCount = 0;
aschiffler@6773
    37
aschiffler@6773
    38
/* Compare surfaces */
aschiffler@6773
    39
int SDLTest_CompareSurfaces(SDL_Surface *surface, SDL_Surface *referenceSurface, int allowable_error)
aschiffler@6763
    40
{
aschiffler@6763
    41
   int ret;
aschiffler@6763
    42
   int i,j;
aschiffler@6763
    43
   int bpp, bpp_reference;
aschiffler@6763
    44
   Uint8 *p, *p_reference;
aschiffler@6763
    45
   int dist;
aschiffler@6763
    46
   Uint8 R, G, B, A;
aschiffler@6763
    47
   Uint8 Rd, Gd, Bd, Ad;
aschiffler@6773
    48
   char imageFilename[128];
aschiffler@6773
    49
   char referenceFilename[128];
aschiffler@6763
    50
aschiffler@6772
    51
   /* Validate input surfaces */
aschiffler@6772
    52
   if (surface == NULL || referenceSurface == NULL) {
aschiffler@6763
    53
      return -1;
aschiffler@6763
    54
   }
aschiffler@6763
    55
aschiffler@6773
    56
   /* Make sure surface size is the same. */
aschiffler@6772
    57
   if ((surface->w != referenceSurface->w) || (surface->h != referenceSurface->h)) {
aschiffler@6772
    58
      return -2;
aschiffler@6772
    59
   }
aschiffler@6772
    60
aschiffler@6773
    61
   /* Sanitize input value */
aschiffler@6772
    62
   if (allowable_error<0) {
aschiffler@6772
    63
      allowable_error = 0;
aschiffler@6772
    64
   }
slouken@7191
    65
aschiffler@6763
    66
   SDL_LockSurface( surface );
aschiffler@6763
    67
   SDL_LockSurface( referenceSurface );
aschiffler@6763
    68
aschiffler@6763
    69
   ret = 0;
aschiffler@6763
    70
   bpp = surface->format->BytesPerPixel;
aschiffler@6763
    71
   bpp_reference = referenceSurface->format->BytesPerPixel;
aschiffler@6763
    72
   /* Compare image - should be same format. */
aschiffler@6763
    73
   for (j=0; j<surface->h; j++) {
aschiffler@6763
    74
      for (i=0; i<surface->w; i++) {
aschiffler@6763
    75
         p  = (Uint8 *)surface->pixels + j * surface->pitch + i * bpp;
aschiffler@6763
    76
         p_reference = (Uint8 *)referenceSurface->pixels + j * referenceSurface->pitch + i * bpp_reference;
aschiffler@6763
    77
aschiffler@6763
    78
         SDL_GetRGBA(*(Uint32*)p, surface->format, &R, &G, &B, &A);
aschiffler@6763
    79
         SDL_GetRGBA(*(Uint32*)p_reference, referenceSurface->format, &Rd, &Gd, &Bd, &Ad);
aschiffler@6763
    80
aschiffler@6772
    81
         dist = 0;
aschiffler@6763
    82
         dist += (R-Rd)*(R-Rd);
aschiffler@6763
    83
         dist += (G-Gd)*(G-Gd);
aschiffler@6763
    84
         dist += (B-Bd)*(B-Bd);
slouken@7191
    85
aschiffler@6763
    86
         /* Allow some difference in blending accuracy */
aschiffler@6763
    87
         if (dist > allowable_error) {
aschiffler@6763
    88
            ret++;
aschiffler@6763
    89
         }
aschiffler@6763
    90
      }
aschiffler@6763
    91
   }
aschiffler@6763
    92
aschiffler@6763
    93
   SDL_UnlockSurface( surface );
aschiffler@6763
    94
   SDL_UnlockSurface( referenceSurface );
aschiffler@6763
    95
aschiffler@6773
    96
   /* Save test image and reference for analysis on failures */
aschiffler@6773
    97
   _CompareSurfaceCount++;
aschiffler@6773
    98
   if (ret != 0) {
aschiffler@6773
    99
      SDL_snprintf(imageFilename, 127, "CompareSurfaces%04d_TestOutput.bmp", _CompareSurfaceCount);
aschiffler@6773
   100
      SDL_SaveBMP(surface, imageFilename);
aschiffler@6773
   101
      SDL_snprintf(referenceFilename, 127, "CompareSurfaces%04d_Reference.bmp", _CompareSurfaceCount);
aschiffler@6773
   102
      SDL_SaveBMP(referenceSurface, referenceFilename);
aschiffler@6773
   103
      SDLTest_LogError("Surfaces from failed comparison saved as '%s' and '%s'", imageFilename, referenceFilename);
aschiffler@6773
   104
   }
aschiffler@6773
   105
aschiffler@6763
   106
   return ret;
aschiffler@6763
   107
}