src/test/SDL_test_compare.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 21 Oct 2018 22:40:17 -0400
changeset 12345 50e1cca28b39
parent 11811 5d94cb6b24d3
child 12503 806492103856
permissions -rw-r--r--
wasapi/win32: Sort initial device lists by device GUID.

This makes an unchanged set of hardware always report devices in the same
order on each run.
aschiffler@6763
     1
/*
aschiffler@6763
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 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;
philipp@10037
    46
   int sampleErrorX = 0, sampleErrorY = 0, sampleDist = 0;
aschiffler@6763
    47
   Uint8 R, G, B, A;
aschiffler@6763
    48
   Uint8 Rd, Gd, Bd, Ad;
aschiffler@6773
    49
   char imageFilename[128];
aschiffler@6773
    50
   char referenceFilename[128];
aschiffler@6763
    51
aschiffler@6772
    52
   /* Validate input surfaces */
aschiffler@6772
    53
   if (surface == NULL || referenceSurface == NULL) {
aschiffler@6763
    54
      return -1;
aschiffler@6763
    55
   }
aschiffler@6763
    56
aschiffler@6773
    57
   /* Make sure surface size is the same. */
aschiffler@6772
    58
   if ((surface->w != referenceSurface->w) || (surface->h != referenceSurface->h)) {
aschiffler@6772
    59
      return -2;
aschiffler@6772
    60
   }
aschiffler@6772
    61
aschiffler@6773
    62
   /* Sanitize input value */
aschiffler@6772
    63
   if (allowable_error<0) {
aschiffler@6772
    64
      allowable_error = 0;
aschiffler@6772
    65
   }
slouken@7191
    66
aschiffler@6763
    67
   SDL_LockSurface( surface );
aschiffler@6763
    68
   SDL_LockSurface( referenceSurface );
aschiffler@6763
    69
aschiffler@6763
    70
   ret = 0;
aschiffler@6763
    71
   bpp = surface->format->BytesPerPixel;
aschiffler@6763
    72
   bpp_reference = referenceSurface->format->BytesPerPixel;
aschiffler@6763
    73
   /* Compare image - should be same format. */
aschiffler@6763
    74
   for (j=0; j<surface->h; j++) {
aschiffler@6763
    75
      for (i=0; i<surface->w; i++) {
aschiffler@6763
    76
         p  = (Uint8 *)surface->pixels + j * surface->pitch + i * bpp;
aschiffler@6763
    77
         p_reference = (Uint8 *)referenceSurface->pixels + j * referenceSurface->pitch + i * bpp_reference;
aschiffler@6763
    78
aschiffler@6763
    79
         SDL_GetRGBA(*(Uint32*)p, surface->format, &R, &G, &B, &A);
aschiffler@6763
    80
         SDL_GetRGBA(*(Uint32*)p_reference, referenceSurface->format, &Rd, &Gd, &Bd, &Ad);
aschiffler@6763
    81
aschiffler@6772
    82
         dist = 0;
aschiffler@6763
    83
         dist += (R-Rd)*(R-Rd);
aschiffler@6763
    84
         dist += (G-Gd)*(G-Gd);
aschiffler@6763
    85
         dist += (B-Bd)*(B-Bd);
slouken@7191
    86
aschiffler@6763
    87
         /* Allow some difference in blending accuracy */
aschiffler@6763
    88
         if (dist > allowable_error) {
aschiffler@6763
    89
            ret++;
aschiffler@9254
    90
            if (ret == 1) {
aschiffler@9254
    91
               sampleErrorX = i;
aschiffler@9254
    92
               sampleErrorY = j;
aschiffler@9254
    93
               sampleDist = dist;
aschiffler@9254
    94
            }
aschiffler@6763
    95
         }
aschiffler@6763
    96
      }
aschiffler@6763
    97
   }
aschiffler@6763
    98
aschiffler@6763
    99
   SDL_UnlockSurface( surface );
aschiffler@6763
   100
   SDL_UnlockSurface( referenceSurface );
aschiffler@6763
   101
aschiffler@6773
   102
   /* Save test image and reference for analysis on failures */
aschiffler@6773
   103
   _CompareSurfaceCount++;
aschiffler@6773
   104
   if (ret != 0) {
aschiffler@9254
   105
      SDLTest_LogError("Comparison of pixels with allowable error of %i failed %i times.", allowable_error, ret);
aschiffler@9254
   106
      SDLTest_LogError("First detected occurrence at position %i,%i with a squared RGB-difference of %i.", sampleErrorX, sampleErrorY, sampleDist); 
aschiffler@6773
   107
      SDL_snprintf(imageFilename, 127, "CompareSurfaces%04d_TestOutput.bmp", _CompareSurfaceCount);
aschiffler@6773
   108
      SDL_SaveBMP(surface, imageFilename);
aschiffler@6773
   109
      SDL_snprintf(referenceFilename, 127, "CompareSurfaces%04d_Reference.bmp", _CompareSurfaceCount);
aschiffler@6773
   110
      SDL_SaveBMP(referenceSurface, referenceFilename);
aschiffler@6773
   111
      SDLTest_LogError("Surfaces from failed comparison saved as '%s' and '%s'", imageFilename, referenceFilename);
aschiffler@6773
   112
   }
aschiffler@6773
   113
aschiffler@6763
   114
   return ret;
aschiffler@6763
   115
}
slouken@11465
   116
slouken@11465
   117
/* vi: set ts=4 sw=4 expandtab: */