Creating a Gamma Curve Visualisation

How can I draw a chart to visualise an SDL gamma ramp?

This program creates a visual representation of a gamma ramp with a gamma value of 2.0. Here are the key components:

Part 1: We create a 256x256 pixel window - wide enough to show all 256 possible input values, and tall enough to show the complete output range.

Part 2: For each x-coordinate from 0 to 255:

  • We get the corresponding gamma ramp value and convert it from the SDL range (0-65535) to our visualization range (0-255)
  • We draw a white vertical line up to that height
  • The background is drawn in dark blue to make the curve stand out
  • A gray reference line is drawn to show what a gamma value of 1.0 would look like

Part 3: We write directly to the window's surface pixels for performance and to avoid dependencies on SDL's renderer.

Part 4: We keep the window open until the user closes it, so they can study the curve.

#include <SDL.h>
#include <iostream>

int main(int argc, char* argv[]) {
  SDL_Init(SDL_INIT_VIDEO);

  SDL_Window* Window{
    SDL_CreateWindow("Gamma Ramp Visualizer",
                     SDL_WINDOWPOS_UNDEFINED,
                     SDL_WINDOWPOS_UNDEFINED,
                     256, 256,
                     SDL_WINDOW_SHOWN)};

  SDL_Surface* WindowSurface{
    SDL_GetWindowSurface(Window)};

  // Create our gamma ramp with gamma = 2.0
  Uint16 Ramp[256];
  SDL_CalculateGammaRamp(2, Ramp);

  // Lock surface for pixel access
  SDL_LockSurface(WindowSurface);

  // Get pixel format information
  const auto Format{WindowSurface->format};
  Uint32* Pixels{
    static_cast<Uint32*>(WindowSurface->
      pixels)};

  // Draw the gamma ramp visualization
  for (int x{0}; x < 256; ++x) {
    // Convert gamma ramp value (0-65535) to
    // height (0-255)
    int Height{
      static_cast<int>((Ramp[x] * 255) /
        65535)};

    // Draw vertical line
    for (int y{0}; y < 256; ++y) {
      Uint32 Color;

      if (255 - y <= Height) {
        // Part of the curve - white pixel
        Color = SDL_MapRGB(
          Format, 255, 255, 255);
      } else {
        // Background - dark blue
        Color = SDL_MapRGB(Format, 0, 0, 40);
      }

      // Draw pixel at (x,y)
      Pixels[y * WindowSurface->w + x] = Color;
    }

    // Draw reference line in gray
    if (255 - x >= 0) {
      Pixels[(255 - x) * WindowSurface->w + x] =
        SDL_MapRGB(Format, 128, 128, 128);
    }
  }

  SDL_UnlockSurface(WindowSurface);
  SDL_UpdateWindowSurface(Window);

  // Wait for window close
  SDL_Event Event;
  bool Running{true};
  while (Running) {
    while (SDL_PollEvent(&Event)) {
      if (Event.type == SDL_QUIT) {
        Running = false;
      }
    }
  }

  SDL_DestroyWindow(Window);
  SDL_Quit();
  return 0;
}

The resulting visualization shows how a gamma value of 2.0 affects brightness - the curve drops more steeply at first compared to the reference line, meaning darker input values become even darker in the output.

Try modifying the gamma value passed to SDL_CalculateGammaRamp() to see how different gamma values affect the curve:

  • Values less than 1.0 will curve above the reference line, brightening the image
  • Values greater than 1.0 will curve below the reference line, darkening the image
  • A value of exactly 1.0 will match the reference line perfectly

Brightness and Gamma

Learn how to control display brightness and gamma correction using SDL's window management functions

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Control Individual Window Brightness
Why does changing window brightness affect the whole display? Is there a way to only change my game's brightness?
Create Visual Effects with Gamma
Can I use gamma correction to create special effects like flashing lights or day/night transitions?
Create a Brightness Settings Slider
How do I implement a brightness slider in my game's settings menu?
Check Gamma Correction Support
How do I detect if the system supports gamma correction before trying to use it?
Save and Restore Brightness Settings
How can I save the player's preferred brightness settings and restore them the next time they launch the game?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant