• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/perl
2#
3# Produce gamma-correction tables for alpha blending, assuming sRGB space.
4#
5
6sub srgb_to_linear($)
7{
8    my($s) = @_;
9
10    if ($s <= 10) {
11	return $s/(255*12.92);
12    } else {
13	return (($s+14.025)/269.025)**2.4;
14    }
15}
16
17sub linear_to_srgb($)
18{
19    my($l) = @_;
20    my $s;
21
22    if ($l <= 0.00304) {
23	$s = 12.92*$l;
24    } else {
25	$s = 1.055*$l**(1.0/2.4) - 0.055;
26    }
27
28    return int($s*255+0.5);
29}
30
31# Header
32print "#include <inttypes.h>\n\n";
33
34#
35# Table 1: convert 8-bit sRGB values to 16-bit linear values
36#
37
38print "const uint16_t __vesacon_srgb_to_linear[256] = {\n";
39for ($i = 0; $i <= 255; $i++) {
40    printf "\t%5d,\n", int(srgb_to_linear($i)*65535+0.5);
41}
42print "};\n\n";
43
44#
45# Table 2: convert linear values in the range [0, 65535*255],
46#          shifted right by 12 bits, to sRGB
47#
48
49print "const uint8_t __vesacon_linear_to_srgb[4080] = {\n";
50for ($i = 0; $i <= 4079; $i++) {
51    printf "\t%3d,\n", linear_to_srgb(($i+0.5)/4079.937744);
52}
53print "};\n\n";
54