mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-02-05 04:06:08 -05:00
This implements gamma-correct blending, which mainly affects font rendering. The implementation requires compile-time availability of the new color-management protocol (available in wayland-protocols >= 1.41), and run-time support for the same in the compositor (specifically, the EXT_LINEAR TF function and sRGB primaries). How it works: all colors are decoded from sRGB to linear (using a lookup table, generated in the exact same way pixman generates it's internal conversion tables) before being used by pixman. The resulting image buffer is thus in decoded/linear format. We use the color-management protocol to inform the compositor of this, by tagging the wayland surfaces with the 'ext_linear' image attribute. Sixes: all colors are sRGB internally, and decoded to linear before being used in any sixels. Thus, the image buffers will contain linear colors. This is important, since otherwise there would be a decode/encode penalty every time a sixel is blended to the grid. Emojis: we require fcft >= 3.2, which adds support for sRGB decoding color glyphs. Meaning, the emoji pixman surfaces can be blended directly to the grid, just like sixels. Gamma-correct blending is enabled by default *when the compositor supports it*. There's a new option to explicitly enable/disable it: gamma-correct-blending=no|yes. If set to 'yes', and the compositor does not implement the required color-management features, warning logs are emitted. There's a loss of precision when storing linear pixels in 8-bit channels. For this reason, this patch also adds supports for 10-bit surfaces. For now, this is disabled by default since such surfaces only have 2 bits for alpha. It can be enabled with tweak.surface-bit-depth=10-bit. Perhaps, in the future, we can enable it by default if: * gamma-correct blending is enabled * the user has not enabled a transparent background
90 lines
2.5 KiB
Python
Executable file
90 lines
2.5 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import math
|
|
import sys
|
|
|
|
|
|
def srgb_to_linear(f: float) -> float:
|
|
assert(f >= 0 and f <= 1.0)
|
|
|
|
if f <= 0.04045:
|
|
return f / 12.92
|
|
|
|
return math.pow((f + 0.055) / 1.055, 2.4)
|
|
|
|
|
|
def linear_to_srgb(f: float) -> float:
|
|
if f < 0.0031308:
|
|
return f * 12.92
|
|
|
|
return 1.055 * math.pow(f, 1 / 2.4) - 0.055
|
|
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('c_output', type=argparse.FileType('w'))
|
|
parser.add_argument('h_output', type=argparse.FileType('w'))
|
|
opts = parser.parse_args()
|
|
|
|
linear_table: list[int] = []
|
|
srgb_table: list[int] = []
|
|
|
|
for i in range(256):
|
|
linear_table.append(int(srgb_to_linear(float(i) / 255) * 65535 + 0.5))
|
|
|
|
for i in range(4096):
|
|
srgb_table.append(int(linear_to_srgb(float(i) / 4095) * 255 + 0.5))
|
|
|
|
for i in range(256):
|
|
while True:
|
|
linear = linear_table[i]
|
|
srgb = srgb_table[linear >> 4]
|
|
|
|
if i == srgb:
|
|
break
|
|
|
|
linear_table[i] += 1
|
|
|
|
|
|
opts.h_output.write("#pragma once\n")
|
|
opts.h_output.write("#include <stdint.h>\n")
|
|
opts.h_output.write("\n")
|
|
opts.h_output.write('/* 8-bit input, 16-bit output */\n')
|
|
opts.h_output.write("extern const uint16_t srgb_decode_8_to_16_table[256];")
|
|
|
|
opts.h_output.write('\n')
|
|
opts.h_output.write('static inline uint16_t\n')
|
|
opts.h_output.write('srgb_decode_8_to_16(uint8_t v)\n')
|
|
opts.h_output.write('{\n')
|
|
opts.h_output.write(' return srgb_decode_8_to_16_table[v];\n')
|
|
opts.h_output.write('}\n')
|
|
|
|
opts.h_output.write('\n')
|
|
opts.h_output.write('/* 8-bit input, 8-bit output */\n')
|
|
opts.h_output.write("extern const uint8_t srgb_decode_8_to_8_table[256];\n")
|
|
|
|
opts.h_output.write('\n')
|
|
opts.h_output.write('static inline uint8_t\n')
|
|
opts.h_output.write('srgb_decode_8_to_8(uint8_t v)\n')
|
|
opts.h_output.write('{\n')
|
|
opts.h_output.write(' return srgb_decode_8_to_8_table[v];\n')
|
|
opts.h_output.write('}\n')
|
|
|
|
opts.c_output.write('#include "srgb.h"\n')
|
|
opts.c_output.write('\n')
|
|
|
|
opts.c_output.write("const uint16_t srgb_decode_8_to_16_table[256] = {\n")
|
|
for i in range(256):
|
|
opts.c_output.write(f' {linear_table[i]},\n')
|
|
opts.c_output.write('};\n')
|
|
|
|
opts.c_output.write("const uint8_t srgb_decode_8_to_8_table[256] = {\n")
|
|
for i in range(256):
|
|
opts.c_output.write(f' {linear_table[i] >> 8},\n')
|
|
opts.c_output.write('};\n')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|