Commit fab06f9a1cb430f229c8803ad92febfe44751e68

Authored by Gordon Acocella
1 parent fa0c6fb1

Fix to gamma adjust pixels of user screenshots when not using shaders.

... ... @@ -2476,7 +2476,7 @@ static int sdl_redraw_screen_for_screenshot(lua_State *L)
2476 2476 if (for_savefile)
2477 2477 redraw_now(redraw_type_savefile_screenshot);
2478 2478 else
2479   - redraw_now(redraw_type_screenshot);
  2479 + redraw_now(redraw_type_user_screenshot);
2480 2480 return 0;
2481 2481 }
2482 2482
... ... @@ -2987,11 +2987,33 @@ static int sdl_set_gamma(lua_State *L)
2987 2987 return 1;
2988 2988 }
2989 2989
  2990 +static void screenshot_apply_gamma(png_byte *image, unsigned long width, unsigned long height)
  2991 +{
  2992 + // User screenshots (but not saved game screenshots) should have gamma applied.
  2993 + if (gamma_correction != 1.0 && get_current_redraw_type() == redraw_type_user_screenshot)
  2994 + {
  2995 + Uint16 ramp16[256];
  2996 + png_byte ramp8[256];
  2997 + unsigned long i;
  2998 +
  2999 + // This is sufficient for the simple gamma adjustment used above.
  3000 + // If that changes, we may need to query the gamma ramp.
  3001 + SDL_CalculateGammaRamp(gamma_correction, ramp16);
  3002 + for (i = 0; i < 256; i++)
  3003 + ramp8[i] = ramp16[i] / 256;
  3004 +
  3005 + // Red, green and blue component are all the same for simple gamma.
  3006 + for (i = 0; i < width * height * 3; i++)
  3007 + image[i] = ramp8[image[i]];
  3008 + }
  3009 +}
  3010 +
2990 3011 static void png_write_data_fn(png_structp png_ptr, png_bytep data, png_size_t length)
2991 3012 {
2992 3013 luaL_Buffer *B = (luaL_Buffer*)png_get_io_ptr(png_ptr);
2993 3014 luaL_addlstring(B, data, length);
2994 3015 }
  3016 +
2995 3017 static void png_output_flush_fn(png_structp png_ptr)
2996 3018 {
2997 3019 }
... ... @@ -3065,6 +3087,7 @@ static int sdl_get_png_screenshot(lua_State *L)
3065 3087
3066 3088 glPixelStorei(GL_PACK_ALIGNMENT, 1);
3067 3089 glReadPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *)image);
  3090 + screenshot_apply_gamma(image, width, height);
3068 3091
3069 3092 for (i = 0; i < height; i++)
3070 3093 {
... ...
... ... @@ -746,7 +746,7 @@ void on_redraw()
746 746
747 747 switch (current_redraw_type)
748 748 {
749   - case redraw_type_screenshot:
  749 + case redraw_type_user_screenshot:
750 750 case redraw_type_savefile_screenshot:
751 751 // glReadPixels reads from the back buffer, so skip swap when we're doing a screenshot.
752 752 break;
... ... @@ -789,7 +789,6 @@ void redraw_now(redraw_type_t rtype)
789 789
790 790 current_redraw_type = rtype;
791 791 on_redraw();
792   - current_redraw_type = redraw_type_normal;
793 792
794 793 if (changed_gamma)
795 794 {
... ... @@ -1652,6 +1651,7 @@ int main(int argc, char *argv[])
1652 1651 {
1653 1652 case 0:
1654 1653 if (isActive) {
  1654 + current_redraw_type = redraw_type_normal;
1655 1655 on_redraw();
1656 1656 SDL_mutexP(renderingLock);
1657 1657 redraw_pending = 0;
... ...
... ... @@ -56,7 +56,7 @@ extern void do_resize(int w, int h, bool fullscreen, bool borderless, float zoom
56 56
57 57 typedef enum {
58 58 redraw_type_normal,
59   - redraw_type_screenshot,
  59 + redraw_type_user_screenshot,
60 60 redraw_type_savefile_screenshot
61 61 } redraw_type_t;
62 62
... ...