test/testgamma.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. CalculateGamma
  2. get_video_args
  3. main

   1 
   2 /* Bring up a window and manipulate the gamma on it */
   3 
   4 #include <stdlib.h>
   5 #include <stdio.h>
   6 #include <string.h>
   7 #include <math.h>
   8 
   9 #include "SDL.h"
  10 
  11 /* Turn a normal gamma value into an appropriate gamma ramp */
  12 void CalculateGamma(double gamma, Uint16 *ramp)
     /* [<][>][^][v][top][bottom][index][help] */
  13 {
  14         int i, value;
  15 
  16         gamma = 1.0 / gamma;
  17         for ( i=0; i<256; ++i ) {
  18                 value = (int)(pow((double)i/256.0, gamma)*65535.0 + 0.5);
  19                 if ( value > 65535 ) {
  20                         value = 65535;
  21                 }
  22                 ramp[i] = (Uint16)value;
  23         }
  24 }
  25 
  26 /* This can be used as a general routine for all of the test programs */
  27 int get_video_args(char *argv[], int *w, int *h, int *bpp, Uint32 *flags)
     /* [<][>][^][v][top][bottom][index][help] */
  28 {
  29         int i;
  30 
  31         *w = 640;
  32         *h = 480;
  33         *bpp = 0;
  34         *flags = SDL_SWSURFACE;
  35 
  36         for ( i=1; argv[i]; ++i ) {
  37                 if ( strcmp(argv[i], "-width") == 0 ) {
  38                         if ( argv[i+1] ) {
  39                                 *w = atoi(argv[++i]);
  40                         }
  41                 } else
  42                 if ( strcmp(argv[i], "-height") == 0 ) {
  43                         if ( argv[i+1] ) {
  44                                 *h = atoi(argv[++i]);
  45                         }
  46                 } else
  47                 if ( strcmp(argv[i], "-bpp") == 0 ) {
  48                         if ( argv[i+1] ) {
  49                                 *bpp = atoi(argv[++i]);
  50                         }
  51                 } else
  52                 if ( strcmp(argv[i], "-fullscreen") == 0 ) {
  53                         *flags |= SDL_FULLSCREEN;
  54                 } else
  55                 if ( strcmp(argv[i], "-hw") == 0 ) {
  56                         *flags |= SDL_HWSURFACE;
  57                 } else
  58                 if ( strcmp(argv[i], "-hwpalette") == 0 ) {
  59                         *flags |= SDL_HWPALETTE;
  60                 } else
  61                         break;
  62         }
  63         return i;
  64 }
  65 
  66 int main(int argc, char *argv[])
     /* [<][>][^][v][top][bottom][index][help] */
  67 {
  68         SDL_Surface *screen;
  69         SDL_Surface *image;
  70         float gamma;
  71         int i;
  72         int w, h, bpp;
  73         Uint32 flags;
  74         Uint16 ramp[256];
  75         Uint16 red_ramp[256];
  76         Uint32 then, timeout;
  77 
  78         /* Check command line arguments */
  79         argv += get_video_args(argv, &w, &h, &bpp, &flags);
  80 
  81         /* Initialize SDL */
  82         if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
  83                 fprintf(stderr,
  84                         "Couldn't initialize SDL: %s\n", SDL_GetError());
  85                 exit(1);
  86         }
  87         atexit(SDL_Quit);
  88 
  89         /* Initialize the display, always use hardware palette */
  90         screen = SDL_SetVideoMode(w, h, bpp, flags | SDL_HWPALETTE);
  91         if ( screen == NULL ) {
  92                 fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
  93                                                 w, h, SDL_GetError());
  94                 exit(1);
  95         }
  96 
  97         /* Set the window manager title bar */
  98         SDL_WM_SetCaption("SDL gamma test", "testgamma");
  99 
 100         /* Set the desired gamma, if any */
 101         gamma = 1.0f;
 102         if ( *argv ) {
 103                 gamma = (float)atof(*argv);
 104         }
 105         if ( SDL_SetGamma(gamma, gamma, gamma) < 0 ) {
 106                 fprintf(stderr, "Unable to set gamma: %s\n", SDL_GetError());
 107                 exit(1);
 108         }
 109 
 110 #if 0 /* This isn't supported.  Integrating the gamma ramps isn't exact */
 111         /* See what gamma was actually set */
 112         float real[3];
 113         if ( SDL_GetGamma(&real[0], &real[1], &real[2]) < 0 ) {
 114                 printf("Couldn't get gamma: %s\n", SDL_GetError());
 115         } else {
 116                 printf("Set gamma values: R=%2.2f, G=%2.2f, B=%2.2f\n",
 117                         real[0], real[1], real[2]);
 118         }
 119 #endif
 120 
 121         /* Do all the drawing work */
 122         image = SDL_LoadBMP("sample.bmp");
 123         if ( image ) {
 124                 SDL_Rect dst;
 125 
 126                 dst.x = (screen->w - image->w)/2;
 127                 dst.y = (screen->h - image->h)/2;
 128                 dst.w = image->w;
 129                 dst.h = image->h;
 130                 SDL_BlitSurface(image, NULL, screen, &dst);
 131                 SDL_UpdateRects(screen, 1, &dst);
 132         }
 133 
 134         /* Wait a bit, handling events */
 135         then = SDL_GetTicks();
 136         timeout = (5*1000);
 137         while ( (SDL_GetTicks()-then) < timeout ) {
 138                 SDL_Event event;
 139 
 140                 while ( SDL_PollEvent(&event) ) {
 141                         switch (event.type) {
 142                             case SDL_QUIT:      /* Quit now */
 143                                 timeout = 0;
 144                                 break;
 145                             case SDL_KEYDOWN:
 146                                 switch (event.key.keysym.sym) {
 147                                     case SDLK_SPACE:    /* Go longer.. */
 148                                         timeout += (5*1000);
 149                                         break;
 150                                     case SDLK_UP:
 151                                         gamma += 0.2f;
 152                                         SDL_SetGamma(gamma, gamma, gamma);
 153                                         break;
 154                                     case SDLK_DOWN:
 155                                         gamma -= 0.2f;
 156                                         SDL_SetGamma(gamma, gamma, gamma);
 157                                         break;
 158                                     case SDLK_ESCAPE:
 159                                         timeout = 0;
 160                                         break;
 161                                     default:
 162                                         break;
 163                                 }
 164                                 break;
 165                         }
 166                 }
 167         }
 168 
 169         /* Perform a gamma flash to red using color ramps */
 170         while ( gamma < 10.0 ) {
 171                 /* Increase the red gamma and decrease everything else... */
 172                 gamma += 0.1f;
 173                 CalculateGamma(gamma, red_ramp);
 174                 CalculateGamma(1.0/gamma, ramp);
 175                 SDL_SetGammaRamp(red_ramp, ramp, ramp);
 176         }
 177         /* Finish completely red */
 178         memset(red_ramp, 255, sizeof(red_ramp));
 179         memset(ramp, 0, sizeof(ramp));
 180         SDL_SetGammaRamp(red_ramp, ramp, ramp);
 181 
 182         /* Now fade out to black */
 183         for ( i=(red_ramp[0] >> 8); i >= 0; --i ) {
 184                 memset(red_ramp, i, sizeof(red_ramp));
 185                 SDL_SetGammaRamp(red_ramp, NULL, NULL);
 186         }
 187         SDL_Delay(1*1000);
 188 
 189         return(0);
 190 }

/* [<][>][^][v][top][bottom][index][help] */