Add qemu 2.4.0
[kvmfornfv.git] / qemu / pixman / pixman / make-srgb.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 sub linear_to_srgb
6 {
7     my ($c) = @_;
8
9     if ($c < 0.0031308)
10     {
11         return $c * 12.92;
12     }
13     else
14     {
15         return 1.055 * $c ** (1.0/2.4) - 0.055;
16     }
17 }
18
19 sub srgb_to_linear
20 {
21     my ($c) = @_;
22
23     if ($c < 0.04045)
24     {
25         return $c / 12.92;
26     }
27     else
28     {
29         return (($c + 0.055) / 1.055) ** 2.4
30     }
31 }
32
33 my @linear_to_srgb;
34 for my $linear (0 .. 4095)
35 {
36     my $srgb = int(linear_to_srgb($linear / 4095.0) * 255.0 + 0.5);
37     push @linear_to_srgb, $srgb;
38 }
39
40 my @srgb_to_linear;
41 for my $srgb (0 .. 255)
42 {
43     my $linear = int(srgb_to_linear($srgb / 255.0) * 65535.0 + 0.5);
44     push @srgb_to_linear, $linear;
45 }
46
47 # Ensure that we have a lossless sRGB and back conversion loop.
48 # some of the darkest shades need a little bias -- maximum is just
49 # 5 increments out of 16. This gives us useful property with
50 # least amount of error in the sRGB-to-linear table, and keeps the actual
51 # table lookup in the other direction as simple as possible.
52 for my $srgb (0 .. $#srgb_to_linear)
53 {
54     my $add = 0;
55     while (1)
56     {
57         my $linear = $srgb_to_linear[$srgb];
58         my $srgb_lossy = $linear_to_srgb[$linear >> 4];
59         last if $srgb == $srgb_lossy;
60
61         # Add slight bias to this component until it rounds correctly
62         $srgb_to_linear[$srgb] ++;
63         $add ++;
64     }
65     die "Too many adds at $srgb" if $add > 5;
66 }
67
68 print <<"PROLOG";
69 /* WARNING: This file is generated by $0.
70  * Please edit that file instead of this one.
71  */
72
73 #include <stdint.h>
74
75 #ifdef HAVE_CONFIG_H
76 #include <config.h>
77 #endif
78
79 #include "pixman-private.h"
80
81 PROLOG
82
83 print "const uint8_t linear_to_srgb[" . @linear_to_srgb . "] =\n";
84 print "{\n";
85 for my $linear (0 .. $#linear_to_srgb)
86 {
87     if (($linear % 10) == 0)
88     {
89         print "\t";
90     }
91     print sprintf("%d, ", $linear_to_srgb[$linear]);
92     if (($linear % 10) == 9)
93     {
94         print "\n";
95     }
96 }
97 print "\n};\n";
98 print "\n";
99
100 print "const uint16_t srgb_to_linear[" . @srgb_to_linear . "] =\n";
101 print "{\n";
102 for my $srgb (0 .. $#srgb_to_linear)
103 {
104     if (($srgb % 10) == 0)
105     {
106         print "\t";
107     }
108     print sprintf("%d, ", $srgb_to_linear[$srgb]);
109     if (($srgb % 10) == 9)
110     {
111         print "\n";
112     }
113 }
114 print "\n};\n";
115