test/automated/common/common.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 17 Nov 2009 05:17:11 +0000
changeset 3439 0acec8c9f5c9
parent 3259 22ac66da0765
child 3441 5271ce790fed
permissions -rw-r--r--
Fixed some bugs in the automated test suite, revealed by working SDL_RenderReadPixels()
     1 /**
     2  * Automated SDL_Surface test.
     3  *
     4  * Written by Edgar Simo "bobbens"
     5  *
     6  * Released under Public Domain.
     7  */
     8 
     9 
    10 #include "SDL.h"
    11 #include "SDL_at.h"
    12 
    13 #include "common/common.h"
    14 
    15 
    16 /**
    17  * @brief Compares a surface and a surface image for equality.
    18  */
    19 int surface_compare( SDL_Surface *sur, const SurfaceImage_t *img )
    20 {
    21    int ret;
    22    int i,j;
    23    int bpp;
    24    Uint8 *p, *pd;
    25 
    26    /* Make sure size is the same. */
    27    if ((sur->w != img->width) || (sur->h != img->height))
    28       return -1;
    29 
    30    SDL_LockSurface( sur );
    31 
    32    ret = 0;
    33    bpp = sur->format->BytesPerPixel;
    34 
    35    /* Compare image - should be same format. */
    36    for (j=0; j<sur->h; j++) {
    37       for (i=0; i<sur->w; i++) {
    38          p  = (Uint8 *)sur->pixels + j * sur->pitch + i * bpp;
    39          pd = (Uint8 *)img->pixel_data + (j*img->width + i) * img->bytes_per_pixel;
    40          switch (bpp) {
    41             case 1:
    42             case 2:
    43             case 3:
    44                ret += 1;
    45                printf("%d BPP not supported yet.\n",bpp);
    46                break;
    47 
    48             case 4:
    49                {
    50                   int fail;
    51                   Uint8 R, G, B, A;
    52 
    53                   SDL_GetRGBA(*(Uint32*)p, sur->format, &R, &G, &B, &A);
    54 
    55                   if (img->bytes_per_pixel == 3) {
    56                      fail = !( (R == pd[0]) &&
    57                                (G == pd[1]) &&
    58                                (B == pd[2]) );
    59                   } else {
    60                      fail = !( (R == pd[0]) &&
    61                                (G == pd[1]) &&
    62                                (B == pd[2]) &&
    63                                (A == pd[3]) );
    64                   }
    65                   if (fail) {
    66                      ++ret;
    67                   }
    68                }
    69                break;
    70          }
    71       }
    72    }
    73 
    74    SDL_UnlockSurface( sur );
    75 
    76    return ret;
    77 }