int SDL_SetGamma(float red, float green, float blue) { int succeeded; SDL_VideoDevice *video = current_video; SDL_VideoDevice *this = current_video; succeeded = -1; #ifdef USE_MATH_H /* Prefer using SetGammaRamp(), as it's more flexible */ { Uint16 ramp[3][256]; CalculateGammaRamp(red, ramp[0]); CalculateGammaRamp(green, ramp[1]); CalculateGammaRamp(blue, ramp[2]); succeeded = SDL_SetGammaRamp(ramp[0], ramp[1], ramp[2]); } #else SDL_SetError("Gamma correction not supported"); #endif if ( (succeeded < 0) && video->SetGamma ) { SDL_ClearError(); succeeded = video->SetGamma(this, red, green, blue); } return succeeded; }
/* ** GLimp_SetGamma ** ** This routine should only be called if glConfig.deviceSupportsGamma is TRUE */ void GLimp_SetGamma( unsigned char red[256], unsigned char green[256], unsigned char blue[256] ) { Uint16 table[3][256]; int i, j; // float g; if(r_ignorehwgamma->integer) return; // taken from win_gamma.c: for (i = 0; i < 256; i++) { table[0][i] = ( ( ( Uint16 ) red[i] ) << 8 ) | red[i]; table[1][i] = ( ( ( Uint16 ) green[i] ) << 8 ) | green[i]; table[2][i] = ( ( ( Uint16 ) blue[i] ) << 8 ) | blue[i]; } // enforce constantly increasing for (j = 0; j < 3; j++) { for (i = 1; i < 256; i++) { if (table[j][i] < table[j][i-1]) table[j][i] = table[j][i-1]; } } SDL_SetGammaRamp(table[0], table[1], table[2]); // g = Cvar_Get("r_gamma", "1.0", 0)->value; // SDL_SetGamma(g, g, g); }
void SDLWindow::setGammaRamp(const Array<uint16>& gammaRamp) { alwaysAssertM(gammaRamp.size() >= 256, "Gamma ramp must have at least 256 entries"); Log* debugLog = Log::common(); uint16* ptr = const_cast<uint16*>(gammaRamp.getCArray()); #ifdef WIN32 // On windows, use the more reliable SetDeviceGammaRamp function. // It requires separate RGB gamma ramps. uint16 wptr[3 * 256]; for (int i = 0; i < 256; ++i) { wptr[i] = wptr[i + 256] = wptr[i + 512] = ptr[i]; } BOOL success = SetDeviceGammaRamp(wglGetCurrentDC(), wptr); #else bool success = (SDL_SetGammaRamp(ptr, ptr, ptr) != -1); #endif if (! success) { if (debugLog) {debugLog->println("Error setting gamma ramp!");} #ifdef WIN32 debugAssertM(false, "Failed to set gamma ramp"); #else if (debugLog) {debugLog->println(SDL_GetError());} debugAssertM(false, SDL_GetError()); #endif } }
/* ================ VID_Gamma_Restore -- restore system gamma ================ */ static void VID_Gamma_Restore (void) { if (draw_context && vid_gammaworks) { if (SDL_SetGammaRamp(&vid_sysgamma_red[0], &vid_sysgamma_green[0], &vid_sysgamma_blue[0]) == -1) Con_Printf ("VID_Gamma_Restore: failed on SDL_SetGammaRamp\n"); } }
static void APIENTRY SetGammaRamp (Uint16 *redtable, Uint16 *greentable, Uint16 *bluetable) #endif { #ifndef unix SetDeviceGammaRamp(m_hDC, ramp); #else SDL_SetGammaRamp(redtable, greentable, bluetable); #endif }
/* ================= GLimp_SetGamma ================= */ void GLimp_SetGamma( unsigned char red[256], unsigned char green[256], unsigned char blue[256] ) { #if 0 Uint16 table[3][256]; int i, j; if( !glConfig.deviceSupportsGamma || r_ignorehwgamma->integer > 0 ) return; for (i = 0; i < 256; i++) { table[0][i] = ( ( ( Uint16 ) red[i] ) << 8 ) | red[i]; table[1][i] = ( ( ( Uint16 ) green[i] ) << 8 ) | green[i]; table[2][i] = ( ( ( Uint16 ) blue[i] ) << 8 ) | blue[i]; } #ifdef _WIN32 #include <windows.h> // Win2K and newer put this odd restriction on gamma ramps... { OSVERSIONINFO vinfo; vinfo.dwOSVersionInfoSize = sizeof( vinfo ); GetVersionEx( &vinfo ); if( vinfo.dwMajorVersion >= 5 && vinfo.dwPlatformId == VER_PLATFORM_WIN32_NT ) { ri.Printf( PRINT_DEVELOPER, "performing gamma clamp.\n" ); for( j = 0 ; j < 3 ; j++ ) { for( i = 0 ; i < 128 ; i++ ) { if( table[ j ] [ i] > ( ( 128 + i ) << 8 ) ) table[ j ][ i ] = ( 128 + i ) << 8; } if( table[ j ] [127 ] > 254 << 8 ) table[ j ][ 127 ] = 254 << 8; } } } #endif // enforce constantly increasing for (j = 0; j < 3; j++) { for (i = 1; i < 256; i++) { if (table[j][i] < table[j][i-1]) table[j][i] = table[j][i-1]; } } SDL_SetGammaRamp(table[0], table[1], table[2]); #endif }
void UpdateHardwareGamma(void) { float gamma = (vid_gamma->value); Uint16 ramp[256]; CalculateGammaRamp(gamma, ramp, 256); #if SDL_VERSION_ATLEAST(2, 0, 0) if(SDL_SetWindowGammaRamp(window, ramp, ramp, ramp) != 0) { #else if(SDL_SetGammaRamp(ramp, ramp, ramp) < 0) { #endif VID_Printf(PRINT_ALL, "Setting gamma failed: %s\n", SDL_GetError()); } } #endif // X11GAMMA static qboolean IsFullscreen() { #if SDL_VERSION_ATLEAST(2, 0, 0) return !!(SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN); #else return !!(window->flags & SDL_FULLSCREEN); #endif } static qboolean CreateSDLWindow(int flags) { #if SDL_VERSION_ATLEAST(2, 0, 0) int windowPos = SDL_WINDOWPOS_UNDEFINED; // TODO: support fullscreen on different displays with SDL_WINDOWPOS_UNDEFINED_DISPLAY(displaynum) window = SDL_CreateWindow("Yamagi Quake II", windowPos, windowPos, vid.width, vid.height, flags); if(window == NULL) { return false; } context = SDL_GL_CreateContext(window); if(context == NULL) { SDL_DestroyWindow(window); window = NULL; return false; } return true; #else window = SDL_SetVideoMode(vid.width, vid.height, 0, flags); SDL_EnableUNICODE(SDL_TRUE); return window != NULL; #endif }
/* =========== GLimp_RestoreGamma Restores original gamma ramp =========== */ void GLimp_RestoreGamma( void ) { if ( glConfig.deviceSupportsGamma ) { // Restore original gamma if ( SDL_SetGammaRamp( OldGammaRamp[ 0 ], OldGammaRamp[ 1 ], OldGammaRamp[ 2 ] ) == -1 ) { Com_Printf( "SDL_SetGammaRamp failed.\n" ); } } }
int SDL_SetGamma(float red, float green, float blue) { Uint16 ramp[3][256]; CalculateGammaRamp(red, ramp[0]); CalculateGammaRamp(green, ramp[1]); CalculateGammaRamp(blue, ramp[2]); return SDL_SetGammaRamp(ramp[0], ramp[1], ramp[2]); }
/* ================= GLimp_SetGamma ================= */ void GLimp_SetGamma(unsigned short red[256], unsigned short green[256], unsigned short blue[256]) { if ( !SDL_window ) { common->Warning("GLimp_SetGamma called without window"); return; } #if SDL_VERSION_ATLEAST(2, 0, 0) if (SDL_SetWindowGammaRamp( SDL_window, red, green, blue )) #else if (SDL_SetGammaRamp(red, green, blue)) #endif common->Warning("Couldn't set gamma ramp: %s", SDL_GetError()); }
static mrb_value mrb_sdl_video_set_gamma_ramp (mrb_state *mrb, mrb_value self) { mrb_int r; mrb_int g; mrb_int b; mrb_get_args(mrb, "|i", &r); mrb_get_args(mrb, "|i", &g); mrb_get_args(mrb, "|i", &b); uint16_t red = (uint16_t) r; uint16_t green = (uint16_t) g; uint16_t blue = (uint16_t) b; return mrb_fixnum_value(SDL_SetGammaRamp(&red, &green, &blue)); }
static VALUE sdl_setGammaRamp(VALUE mod, VALUE ary_table) { Uint16 table[3][256]; VALUE ary_subtable; int i,j; for( i=0; i<3; ++i ){ ary_subtable = rb_ary_entry( ary_table, i ); for( j=0; j<256; ++j ){ table[i][j] = NUM2INT( rb_ary_entry( ary_subtable, j ) ); } } if( SDL_SetGammaRamp( table[0], table[1], table[2] )==-1 ){ rb_raise(eSDLError,"cannot set gamma lookup table: %s",SDL_GetError()); } return Qnil; }
/* ================= GLimp_SetGamma ================= */ void GLimp_SetGamma( unsigned char red[ 256 ], unsigned char green[ 256 ], unsigned char blue[ 256 ] ) { #if defined( IPHONE ) UNIMPL(); #else #if 1 Uint16 table[ 256 ]; int i, value, lastvalue = 0; for ( i = 0; i < 256; i++ ) { value = ( ( ( Uint16 ) red[ i ] ) << 8 ) | red[ i ]; if ( i < 128 && ( value > ( ( 128 + i ) << 8 ) ) ) { value = ( 128 + i ) << 8; } if ( i && ( value < lastvalue ) ) { value = lastvalue; } lastvalue = table[ i ] = value; } if ( SDL_SetGammaRamp( table, table, table ) == -1 ) { Com_Printf( "SDL_SetGammaRamp failed.\n" ); } #else float g = Cvar_Get( "r_gamma", "1.0", 0 )->value; if ( SDL_SetGamma( g, g, g ) == -1 ) { Com_Printf( "SDL_SetGamma failed.\n" ); } #endif }
void GL_SDL_SetGamma(float gamma, float overbright) { Uint16 ramp[256*3]; float brightness=0; int i; for (i=0 ; i<256 ; i++){ float f = (float) (255 * pow ( (float)i/255.0 , 1/gamma )); f = (f+brightness) * overbright; if (f < 0) f = 0; if (f > 255) f = 255; ramp[i+0] = ramp[i+256] = ramp[i+512] = (unsigned short) (iround(f)<<8); } if (!SDL_SetGammaRamp(ramp, ramp, ramp)) { Console_Printf("GL_SDL_SetGamma() failed\n"); } }
int SDL_SetGamma(float red, float green, float blue) { int succeeded; SDL_VideoDevice *video = current_video; SDL_VideoDevice *this = current_video; succeeded = -1; /* Prefer using SetGammaRamp(), as it's more flexible */ { Uint16 ramp[3][256]; CalculateGammaRamp(red, ramp[0]); CalculateGammaRamp(green, ramp[1]); CalculateGammaRamp(blue, ramp[2]); succeeded = SDL_SetGammaRamp(ramp[0], ramp[1], ramp[2]); } if ( (succeeded < 0) && video->SetGamma ) { SDL_ClearError(); succeeded = video->SetGamma(this, red, green, blue); } return succeeded; }
int OglSetBrightnessInternal (void) { return SDL_SetGammaRamp (gammaRamp + gameStates.ogl.bright.red * 4, gammaRamp + gameStates.ogl.bright.green * 4, gammaRamp + gameStates.ogl.bright.blue * 4); }
// // setvideomode() -- set SDL video mode // int32_t setvideomode(int32_t x, int32_t y, int32_t c, int32_t fs) { int32_t regrab = 0, ret; #ifdef USE_OPENGL static int32_t ovsync = 1; #endif ret = setvideomode_sdlcommon(&x, &y, c, fs, ®rab); if (ret != 1) return ret; // restore gamma before we change video modes if it was changed if (sdl_surface && gammabrightness) { SDL_SetGammaRamp(sysgamma[0], sysgamma[1], sysgamma[2]); gammabrightness = 0; // redetect on next mode switch } // deinit destroy_window_resources(); initprintf("Setting video mode %dx%d (%d-bpp %s)\n", x, y, c, ((fs & 1) ? "fullscreen" : "windowed")); #ifdef USE_OPENGL if (c > 8) { int32_t i, j, multisamplecheck = (glmultisample > 0); if (nogl) return -1; # ifdef _WIN32 win_setvideomode(c); # endif struct glattribs { SDL_GLattr attr; int32_t value; } sdlayer_gl_attributes [] = { { SDL_GL_DOUBLEBUFFER, 1 }, { SDL_GL_MULTISAMPLEBUFFERS, glmultisample > 0 }, { SDL_GL_MULTISAMPLESAMPLES, glmultisample }, { SDL_GL_STENCIL_SIZE, 1 }, { SDL_GL_ACCELERATED_VISUAL, 1 }, { SDL_GL_SWAP_CONTROL, vsync_render }, }; do { SDL_GL_ATTRIBUTES(i, sdlayer_gl_attributes); /* HACK: changing SDL GL attribs only works before surface creation, so we have to create a new surface in a different format first to force the surface we WANT to be recreated instead of reused. */ if (vsync_render != ovsync) { if (sdl_surface) { SDL_FreeSurface(sdl_surface); sdl_surface = SDL_SetVideoMode(1, 1, 8, SDL_NOFRAME | SURFACE_FLAGS | ((fs & 1) ? SDL_FULLSCREEN : 0)); SDL_FreeSurface(sdl_surface); } ovsync = vsync_render; } sdl_surface = SDL_SetVideoMode(x, y, c, SDL_OPENGL | ((fs & 1) ? SDL_FULLSCREEN : 0)); if (!sdl_surface) { if (multisamplecheck) { initprintf("Multisample mode not possible. Retrying without multisampling.\n"); glmultisample = 0; continue; } initprintf("Unable to set video mode!\n"); return -1; } #ifdef _WIN32 loadglextensions(); #endif } while (multisamplecheck--); } else #endif // defined USE_OPENGL { #if !defined SDL_DISABLE_8BIT_BUFFER // We convert paletted contents to non-paletted sdl_surface = SDL_SetVideoMode(x, y, 0, SURFACE_FLAGS | ((fs & 1) ? SDL_FULLSCREEN : 0)); #else sdl_surface = SDL_SetVideoMode(x, y, c, SURFACE_FLAGS | ((fs & 1) ? SDL_FULLSCREEN : 0)); #endif if (!sdl_surface) { initprintf("Unable to set video mode!\n"); return -1; } #if !defined SDL_DISABLE_8BIT_BUFFER sdl_buffersurface = SDL_CreateRGBSurface(SURFACE_FLAGS, x, y, c, 0, 0, 0, 0); if (!sdl_buffersurface) { initprintf("Unable to set video mode: SDL_CreateRGBSurface failed: %s\n", SDL_GetError()); return -1; } #endif } setvideomode_sdlcommonpost(x, y, c, fs, regrab); return 0; }
int main(int argc, char *argv[]) { /* Check command line arguments */ argv += get_video_args(argv, &w, &h, &bpp, &flags); /* Initialize SDL */ if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); return(1); } /* Initialize the display, always use hardware palette */ screen = SDL_SetVideoMode(w, h, bpp, flags | SDL_HWPALETTE); if ( screen == NULL ) { fprintf(stderr, "Couldn't set %dx%d video mode: %s\n", w, h, SDL_GetError()); quit(1); } /* Set the window manager title bar */ SDL_WM_SetCaption("SDL gamma test", "testgamma"); /* Set the desired gamma, if any */ gamma = 1.0f; if ( *argv ) { gamma = (float)atof(*argv); } if ( SDL_SetGamma(gamma, gamma, gamma) < 0 ) { fprintf(stderr, "Unable to set gamma: %s\n", SDL_GetError()); quit(1); } #if 0 /* This isn't supported. Integrating the gamma ramps isn't exact */ /* See what gamma was actually set */ float real[3]; if ( SDL_GetGamma(&real[0], &real[1], &real[2]) < 0 ) { printf("Couldn't get gamma: %s\n", SDL_GetError()); } else { printf("Set gamma values: R=%2.2f, G=%2.2f, B=%2.2f\n", real[0], real[1], real[2]); } #endif /* Do all the drawing work */ image = SDL_LoadBMP("sample.bmp"); if ( image ) { SDL_Rect dst; dst.x = (screen->w - image->w)/2; dst.y = (screen->h - image->h)/2; dst.w = image->w; dst.h = image->h; SDL_BlitSurface(image, NULL, screen, &dst); SDL_UpdateRects(screen, 1, &dst); } /* Wait a bit, handling events */ then = SDL_GetTicks(); timeout = (5*1000); #ifndef EMSCRIPTEN while ( (SDL_GetTicks()-then) < timeout ) { #else emscripten_set_main_loop(&main_loop, 0, 1); } void main_loop() { if ( (SDL_GetTicks()-then) < timeout ) { #endif SDL_Event event; while ( SDL_PollEvent(&event) ) { switch (event.type) { case SDL_QUIT: /* Quit now */ timeout = 0; break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_SPACE: /* Go longer.. */ timeout += (5*1000); break; case SDLK_UP: gamma += 0.2f; SDL_SetGamma(gamma, gamma, gamma); break; case SDLK_DOWN: gamma -= 0.2f; SDL_SetGamma(gamma, gamma, gamma); break; case SDLK_ESCAPE: timeout = 0; break; default: break; } break; } } } #ifdef EMSCRIPTEN else { #endif /* Perform a gamma flash to red using color ramps */ while ( gamma < 10.0 ) { /* Increase the red gamma and decrease everything else... */ gamma += 0.1f; CalculateGamma(gamma, red_ramp); CalculateGamma(1.0/gamma, ramp); SDL_SetGammaRamp(red_ramp, ramp, ramp); } /* Finish completely red */ memset(red_ramp, 255, sizeof(red_ramp)); memset(ramp, 0, sizeof(ramp)); SDL_SetGammaRamp(red_ramp, ramp, ramp); /* Now fade out to black */ for ( i=(red_ramp[0] >> 8); i >= 0; --i ) { memset(red_ramp, i, sizeof(red_ramp)); SDL_SetGammaRamp(red_ramp, NULL, NULL); } #ifndef EMSCRIPTEN SDL_Delay(1*1000); #else emscripten_pause_main_loop(); emscripten_async_call(&end_main, 0, 1*1000); } } void end_main() { #endif SDL_Quit(); #ifndef EMSCRIPTEN return(0); #else exit(0); #endif }
int main(int argc, char *argv[]) { SDL_Surface *screen; SDL_Surface *image; float gamma; int i; int w, h, bpp; Uint32 flags; Uint16 ramp[256]; Uint16 red_ramp[256]; Uint32 then, timeout; argv += get_video_args(argv, &w, &h, &bpp, &flags); if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); return(1); } screen = SDL_SetVideoMode(w, h, bpp, flags | SDL_HWPALETTE); if ( screen == NULL ) { fprintf(stderr, "Couldn't set %dx%d video mode: %s\n", w, h, SDL_GetError()); quit(1); } SDL_WM_SetCaption("SDL gamma test", "testgamma"); gamma = 1.0f; if ( *argv ) { gamma = (float)atof(*argv); } if ( SDL_SetGamma(gamma, gamma, gamma) < 0 ) { fprintf(stderr, "Unable to set gamma: %s\n", SDL_GetError()); quit(1); } #if 0 float real[3]; if ( SDL_GetGamma(&real[0], &real[1], &real[2]) < 0 ) { printf("Couldn't get gamma: %s\n", SDL_GetError()); } else { printf("Set gamma values: R=%2.2f, G=%2.2f, B=%2.2f\n", real[0], real[1], real[2]); } #endif image = SDL_LoadBMP("sample.bmp"); if ( image ) { SDL_Rect dst; dst.x = (screen->w - image->w)/2; dst.y = (screen->h - image->h)/2; dst.w = image->w; dst.h = image->h; SDL_BlitSurface(image, NULL, screen, &dst); SDL_UpdateRects(screen, 1, &dst); } then = SDL_GetTicks(); timeout = (5*1000); while ( (SDL_GetTicks()-then) < timeout ) { SDL_Event event; while ( SDL_PollEvent(&event) ) { switch (event.type) { case SDL_QUIT: timeout = 0; break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_SPACE: timeout += (5*1000); break; case SDLK_UP: gamma += 0.2f; SDL_SetGamma(gamma, gamma, gamma); break; case SDLK_DOWN: gamma -= 0.2f; SDL_SetGamma(gamma, gamma, gamma); break; case SDLK_ESCAPE: timeout = 0; break; default: break; } break; } } } while ( gamma < 10.0 ) { gamma += 0.1f; CalculateGamma(gamma, red_ramp); CalculateGamma(1.0/gamma, ramp); SDL_SetGammaRamp(red_ramp, ramp, ramp); } memset(red_ramp, 255, sizeof(red_ramp)); memset(ramp, 0, sizeof(ramp)); SDL_SetGammaRamp(red_ramp, ramp, ramp); for ( i=(red_ramp[0] >> 8); i >= 0; --i ) { memset(red_ramp, i, sizeof(red_ramp)); SDL_SetGammaRamp(red_ramp, NULL, NULL); } SDL_Delay(1*1000); SDL_Quit(); return(0); }