rasterize: refine blending math
Refine the colorization logic to make it more of a blending operation. Previously, we were relying on opengl to carry out blending between layers on our behalf, but that wasn't perfect. This commit is inspired by this post: https://www.puredevsoftware.com/blog/2019/01/22/sub-pixel-gamma-correct-font-rendering/ and factors in the background color when computing the colorized glyph. This appears to reduce the dark fringes/edges that we were seeing before, without noticeably changing the brightness of the result. refs: #491
This commit is contained in:
1 parent
4e2b2eddba
commit
82fc53d736
3 files changed
+29
-27
No files matched your search
@@ -158,7 +158,7 @@ impl FreeTypeRasterizer {
|
||||
let red = data[src_offset + (x * 3)];
|
||||
let green = data[src_offset + (x * 3) + 1];
|
||||
let blue = data[src_offset + (x * 3) + 2];
|
||||
let alpha = red.min(green).min(blue);
|
||||
let alpha = red.max(green).max(blue);
|
||||
|
||||
// Texture is SRGBA, which in OpenGL means
|
||||
// that the RGB values are gamma adjusted
|
||||
|
||||
@@ -50,41 +50,42 @@ vec3 hsv2rgb(vec3 c)
|
||||
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
|
||||
}
|
||||
|
||||
const vec3 unit3 = vec3(1.0, 1.0, 1.0);
|
||||
|
||||
vec4 apply_hsv(vec4 c, vec3 transform)
|
||||
{
|
||||
if (transform == unit3) {
|
||||
return c;
|
||||
}
|
||||
vec3 hsv = rgb2hsv(c.rgb) * transform;
|
||||
return vec4(hsv2rgb(hsv).rgb, c.a);
|
||||
}
|
||||
|
||||
// Given glyph, the greyscale rgba value computed by freetype,
|
||||
// and color, the desired color, compute the resultant pixel
|
||||
// value.
|
||||
// value for rendering over the top of the given background
|
||||
// color.
|
||||
//
|
||||
// The freetype glyph is greyscale (R=G=B=A) when font_antialias=Greyscale,
|
||||
// but holds separate intensity values (alpha) for the R, G and
|
||||
// B channels when font_antialias=Subpixel, with an approximated A
|
||||
// value derived from the RGB values.
|
||||
// In terms of computing the color, we can scale each of the color
|
||||
// RGB values by the glyph RGB values (which are really intensity).
|
||||
// To reduce darker fringes, the RGB values are scaled down by A
|
||||
// so that the overall A value doesn't make them too dark at
|
||||
// end.
|
||||
vec4 colorize(vec4 glyph, vec4 color) {
|
||||
return vec4(glyph.rgb * color.rgb / glyph.a, glyph.a);
|
||||
}
|
||||
// where each channel holds the brightness of the pixel.
|
||||
// It holds separate intensity values for the R, G and B channels when
|
||||
// subpixel anti-aliasing is in use, with an approximated A value
|
||||
// derived from the R, G, B values.
|
||||
//
|
||||
// In sub-pixel mode we don't want to look at glyph.a as we effective
|
||||
// have per-channel alpha. In greyscale mode, glyph.a is the same
|
||||
// as the other channels, so this routine ignores glyph.a when
|
||||
// computing the blend, but does include that value for the returned
|
||||
// alpha value.
|
||||
//
|
||||
// See also: https://www.puredevsoftware.com/blog/2019/01/22/sub-pixel-gamma-correct-font-rendering/
|
||||
vec4 colorize(vec4 glyph, vec4 color, vec4 background) {
|
||||
float r = glyph.r * color.r + (1.0 - glyph.r) * background.r;
|
||||
float g = glyph.g * color.g + (1.0 - glyph.g) * background.g;
|
||||
float b = glyph.b * color.b + (1.0 - glyph.b) * background.b;
|
||||
|
||||
// The same thing as colorize above, but carried out by first
|
||||
// translating the color to HSV and then scaling the V (intensity)
|
||||
// by the glyph alpha level, and then converting back.
|
||||
// By manipulating the intensity in HSV colorspace, we more accurately
|
||||
// model the perceived brightness of the individual RGB channel values
|
||||
// and the appearance of darker AA fringes is reduced.
|
||||
// However, because this takes only glyph.a into consideration, it may not
|
||||
// be "as good" as `colorize` when font_antialias=Subpixel.
|
||||
// To my eye, colorize_hsv looks better than colorize in both of those AA modes.
|
||||
vec4 colorize_hsv(vec4 glyph, vec4 color) {
|
||||
vec3 hsv = rgb2hsv(color.rgb);
|
||||
hsv.b *= glyph.a;
|
||||
return vec4(hsv2rgb(hsv * foreground_text_hsb), glyph.a);
|
||||
return vec4(r, g, b, glyph.a);
|
||||
// return vec4(glyph.rgb * color.rgb, glyph.a);
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,8 @@ void main() {
|
||||
discard;
|
||||
return;
|
||||
} else {
|
||||
color = colorize_hsv(color, o_fg_color);
|
||||
color = colorize(color, o_fg_color, o_bg_color);
|
||||
color = apply_hsv(color, foreground_text_hsb);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user