From 69a2357ca3f5787d2fcfb314b52c8bb4887d003c Mon Sep 17 00:00:00 2001 From: Clownacy Date: Fri, 25 Jan 2019 03:55:34 +0000 Subject: [PATCH] Fixed font gamma-correction Also did some cleanup. --- src/Font.cpp | 27 +++++++++++++++++---------- src/Font.h | 14 +++----------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/Font.cpp b/src/Font.cpp index 3061a13f..9449f5a0 100644 --- a/src/Font.cpp +++ b/src/Font.cpp @@ -21,6 +21,18 @@ #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MAX(a,b) ((a) > (b) ? (a) : (b)) +typedef struct FontObject +{ + FT_Library library; + FT_Face face; +#ifndef DISABLE_FONT_ANTIALIASING + bool lcd_mode; +#endif +#ifdef JAPANESE + iconv_t conv; +#endif +} FontObject; + static unsigned long UTF8ToCode(const unsigned char *string, unsigned int *bytes_read) { unsigned int length; @@ -176,21 +188,17 @@ void DrawText(FontObject *font_object, SDL_Renderer *renderer, SDL_Texture *text { for (int ix = MAX(-letter_x, 0); letter_x + ix < MIN(letter_x + (int)converted.width / 3, surface_width); ++ix) { - unsigned char (*font_buffer)[converted.pitch / 3][3] = (unsigned char (*)[converted.pitch / 3][3])converted.buffer; + const unsigned char (*font_buffer)[converted.pitch / 3][3] = (unsigned char (*)[converted.pitch / 3][3])converted.buffer; - unsigned char *font_pixel = font_buffer[iy][ix]; + const unsigned char *font_pixel = font_buffer[iy][ix]; unsigned char *surface_pixel = surface_buffer[letter_y + iy][letter_x + ix]; if (font_pixel[0] || font_pixel[1] || font_pixel[2]) { for (unsigned int j = 0; j < 3; ++j) { - unsigned char colour_accumulator = surface_pixel[j]; - - colour_accumulator = ((colours[j] * font_pixel[j]) / 0xFF) + ((colour_accumulator * (0xFF - font_pixel[j])) / 0xFF); // Alpha blending - colour_accumulator = 255.0 * pow((colour_accumulator / 255.0), 1.0 / 1.8); // Gamma correction - - surface_pixel[j] = colour_accumulator; + const double alpha = pow((font_pixel[j] / 255.0), 1.0 / 1.8); // Gamma correction + surface_pixel[j] = (colours[j] * alpha) + (surface_pixel[j] * (1.0 - alpha)); // Alpha blending } surface_pixel[3] = 0xFF; @@ -203,8 +211,7 @@ void DrawText(FontObject *font_object, SDL_Renderer *renderer, SDL_Texture *text { unsigned char (*font_buffer)[converted.pitch] = (unsigned char (*)[converted.pitch])converted.buffer; - const double raw_alpha = (double)font_buffer[iy][ix] / (converted.num_grays - 1); - const double alpha = pow(raw_alpha, 1.0 / 1.8); // Gamma-corrected + const double alpha = pow((double)font_buffer[iy][ix] / (converted.num_grays - 1), 1.0 / 1.8); // Gamma-corrected unsigned char *surface_pixel = surface_buffer[letter_y + iy][letter_x + ix]; diff --git a/src/Font.h b/src/Font.h index 57953a3a..36cf0f29 100644 --- a/src/Font.h +++ b/src/Font.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -7,17 +9,7 @@ #include "SDL.h" -typedef struct FontObject -{ - FT_Library library; - FT_Face face; -#ifndef DISABLE_FONT_ANTIALIASING - bool lcd_mode; -#endif -#ifdef JAPANESE - iconv_t conv; -#endif -} FontObject; +typedef struct FontObject FontObject; FontObject* LoadFont(unsigned int cell_width, unsigned int cell_height, char *font_filename); void DrawText(FontObject *font_object, SDL_Renderer *renderer, SDL_Texture *texture, int x, int y, unsigned long colour, const char *string, size_t string_length);