test/automated/common/common.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 19 Nov 2009 05:06:01 +0000
changeset 3456 6b182cbe38ac
parent 3441 5271ce790fed
child 3476 ab99313951cd
permissions -rw-r--r--
Allow some variation in the pixel values to account for blending accuracy differences.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3259
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/**
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     2
 * Automated SDL_Surface test.
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     3
 *
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
 * Written by Edgar Simo "bobbens"
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
 *
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
 * Released under Public Domain.
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
 */
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
#include "SDL.h"
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
#include "SDL_at.h"
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
#include "common/common.h"
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
/**
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
 * @brief Compares a surface and a surface image for equality.
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
 */
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
int surface_compare( SDL_Surface *sur, const SurfaceImage_t *img )
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
{
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
   int ret;
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
   int i,j;
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
   int bpp;
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
   Uint8 *p, *pd;
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
   /* Make sure size is the same. */
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
   if ((sur->w != img->width) || (sur->h != img->height))
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
      return -1;
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
   SDL_LockSurface( sur );
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
   ret = 0;
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
   bpp = sur->format->BytesPerPixel;
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
   /* Compare image - should be same format. */
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
   for (j=0; j<sur->h; j++) {
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
      for (i=0; i<sur->w; i++) {
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
         p  = (Uint8 *)sur->pixels + j * sur->pitch + i * bpp;
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
         pd = (Uint8 *)img->pixel_data + (j*img->width + i) * img->bytes_per_pixel;
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
         switch (bpp) {
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
            case 1:
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
            case 2:
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
            case 3:
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
               ret += 1;
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
               printf("%d BPP not supported yet.\n",bpp);
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
               break;
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
            case 4:
3439
0acec8c9f5c9 Fixed some bugs in the automated test suite, revealed by working SDL_RenderReadPixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3259
diff changeset
    49
               {
3456
6b182cbe38ac Allow some variation in the pixel values to account for blending accuracy differences.
Sam Lantinga <slouken@libsdl.org>
parents: 3441
diff changeset
    50
                  int dist = 0;
3439
0acec8c9f5c9 Fixed some bugs in the automated test suite, revealed by working SDL_RenderReadPixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3259
diff changeset
    51
                  Uint8 R, G, B, A;
0acec8c9f5c9 Fixed some bugs in the automated test suite, revealed by working SDL_RenderReadPixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3259
diff changeset
    52
0acec8c9f5c9 Fixed some bugs in the automated test suite, revealed by working SDL_RenderReadPixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3259
diff changeset
    53
                  SDL_GetRGBA(*(Uint32*)p, sur->format, &R, &G, &B, &A);
0acec8c9f5c9 Fixed some bugs in the automated test suite, revealed by working SDL_RenderReadPixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3259
diff changeset
    54
0acec8c9f5c9 Fixed some bugs in the automated test suite, revealed by working SDL_RenderReadPixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3259
diff changeset
    55
                  if (img->bytes_per_pixel == 3) {
3456
6b182cbe38ac Allow some variation in the pixel values to account for blending accuracy differences.
Sam Lantinga <slouken@libsdl.org>
parents: 3441
diff changeset
    56
                     dist += (R-pd[0])*(R-pd[0]);
6b182cbe38ac Allow some variation in the pixel values to account for blending accuracy differences.
Sam Lantinga <slouken@libsdl.org>
parents: 3441
diff changeset
    57
                     dist += (G-pd[1])*(G-pd[1]);
6b182cbe38ac Allow some variation in the pixel values to account for blending accuracy differences.
Sam Lantinga <slouken@libsdl.org>
parents: 3441
diff changeset
    58
                     dist += (B-pd[2])*(B-pd[2]);
3439
0acec8c9f5c9 Fixed some bugs in the automated test suite, revealed by working SDL_RenderReadPixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3259
diff changeset
    59
                  } else {
3456
6b182cbe38ac Allow some variation in the pixel values to account for blending accuracy differences.
Sam Lantinga <slouken@libsdl.org>
parents: 3441
diff changeset
    60
                     dist += (R-pd[0])*(R-pd[0]);
6b182cbe38ac Allow some variation in the pixel values to account for blending accuracy differences.
Sam Lantinga <slouken@libsdl.org>
parents: 3441
diff changeset
    61
                     dist += (G-pd[1])*(G-pd[1]);
6b182cbe38ac Allow some variation in the pixel values to account for blending accuracy differences.
Sam Lantinga <slouken@libsdl.org>
parents: 3441
diff changeset
    62
                     dist += (B-pd[2])*(B-pd[2]);
6b182cbe38ac Allow some variation in the pixel values to account for blending accuracy differences.
Sam Lantinga <slouken@libsdl.org>
parents: 3441
diff changeset
    63
                     dist += (A-pd[3])*(A-pd[3]);
3439
0acec8c9f5c9 Fixed some bugs in the automated test suite, revealed by working SDL_RenderReadPixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3259
diff changeset
    64
                  }
3456
6b182cbe38ac Allow some variation in the pixel values to account for blending accuracy differences.
Sam Lantinga <slouken@libsdl.org>
parents: 3441
diff changeset
    65
                  /* Allow up to sqrt(32) difference in blending accuracy */
6b182cbe38ac Allow some variation in the pixel values to account for blending accuracy differences.
Sam Lantinga <slouken@libsdl.org>
parents: 3441
diff changeset
    66
                  if (dist > 32) {
6b182cbe38ac Allow some variation in the pixel values to account for blending accuracy differences.
Sam Lantinga <slouken@libsdl.org>
parents: 3441
diff changeset
    67
                     /*printf("pixel %d,%d varies by %d\n", i, j, dist);*/
3439
0acec8c9f5c9 Fixed some bugs in the automated test suite, revealed by working SDL_RenderReadPixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3259
diff changeset
    68
                     ++ret;
0acec8c9f5c9 Fixed some bugs in the automated test suite, revealed by working SDL_RenderReadPixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3259
diff changeset
    69
                  }
0acec8c9f5c9 Fixed some bugs in the automated test suite, revealed by working SDL_RenderReadPixels()
Sam Lantinga <slouken@libsdl.org>
parents: 3259
diff changeset
    70
               }
3259
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
               break;
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
         }
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
      }
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
   }
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
   SDL_UnlockSurface( sur );
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
3441
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    78
   if (ret) {
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    79
      SDL_SaveBMP(sur, "fail.bmp");
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    80
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    81
      SDL_LockSurface( sur );
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    82
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    83
      bpp = sur->format->BytesPerPixel;
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    84
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    85
      /* Compare image - should be same format. */
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    86
      if (bpp == 4) {
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    87
         for (j=0; j<sur->h; j++) {
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    88
            for (i=0; i<sur->w; i++) {
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    89
               p  = (Uint8 *)sur->pixels + j * sur->pitch + i * bpp;
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    90
               pd = (Uint8 *)img->pixel_data + (j*img->width + i) * img->bytes_per_pixel;
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    91
               Uint8 R, G, B, A;
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    92
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    93
               R = pd[0];
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    94
               G = pd[1];
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    95
               B = pd[2];
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    96
               if (img->bytes_per_pixel == 4) {
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    97
                  A = pd[3];
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    98
               } else {
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
    99
                  A = 0;
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
   100
               }
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
   101
               *(Uint32*)p = (A << 24) | (R << 16) | (G << 8) | B;
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
   102
            }
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
   103
         }
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
   104
      }
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
   105
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
   106
      SDL_UnlockSurface( sur );
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
   107
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
   108
      SDL_SaveBMP(sur, "good.bmp");
5271ce790fed Debug info to help track down render test failures
Sam Lantinga <slouken@libsdl.org>
parents: 3439
diff changeset
   109
   }
3259
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
   return ret;
22ac66da0765 Merged Edgar's code changes from Google Summer of Code 2009
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
}