babl: fix some annotation to make the function usable in bindings.
[babl.git] / docs / UnifiedAlpha-static.html
blob9b948065f2be0c403cd506a24d82c9b929d1a393
1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5 <!-- The babl webpage is partially autogenerated
6 -->
7 <html>
8 <head>
9 <title>Unified Alpha - babl</title>
10 <meta http-equiv="content-type" content="text/html;charset=utf-8" />
11 <link rel="icon" href="graphics/babl-16x16.png" type="image/png" />
12 <link rel="shortcut icon" href="graphics/babl-16x16.png" type="image/png" />
13 <style type='text/css'>
14 @import url(babl.css);
15 </style>
16 </head>
17 <body>
19 <div class='print'>
20 <div class='print_title'>
21 <h1>Babl</h1>
22 </div>
23 </div>
24 <!--TOC-->
26 <div class='paper'>
27 <div class='content'>
29 <h2>Symmetric transformations for floating point alpha</h2>
32 <p> babl clamps the alpha used when going from separate alpha to associated
33 alpha or from associated alpha to separate alpha to BABL_ALPHA_FLOOR. This
34 replaces asymptotic behavior and direct precision loss of color precision when
35 multiplying or dividing by alphas near 0.0 with a consistent symmetric
36 transformation.</p>
38 <p>Original intent of data as well as non-asymptotic precision loss is thus
39 maintained when the processing chain might temporarily use the other alpha
40 representation.</p>
42 <pre>
43 #define BABL_ALPHA_FLOOR (1.0/65536.0)
44 #define BABL_ALPHA_FLOOR_F (1.0f/65536.0f)
45 </pre>
47 <p>The deviation from not clamping near 0.0 is within the quantization margin
48 of 16bit integer alpha, thus no adaptations for any SIMD or and similar 8bit
49 and 16bit extensions of pixel format conversions are needed.
50 </p>
52 <p>This is the clamping function in use:</p>
53 <pre>
54 static inline float
55 babl_epsilon_for_zero_float (float value)
57 if (value &lt;= BABL_ALPHA_FLOOR_F &amp;&amp;
58 value &gt;= -BABL_ALPHA_FLOOR_F)
59 return BABL_ALPHA_FLOOR_F;
60 else
61 return value;
63 </pre>
64 <p>And an example use of this clamping function that is consistent with babls behavior:</p>
65 <pre>
66 static inline void
67 associated_to_separate_rgba (const float *associated_rgba,
68 float *separate_rgba)
70 float alpha = associated_rgba[3];
71 float reciprocal_alpha = 1.0f / babl_epsilon_for_zero_float (alpha);
73 separate_rgba[0] = associated_rgba[0] * reciprocal_alpha;
74 separate_rgba[1] = associated_rgba[1] * reciprocal_alpha;
75 separate_rgba[2] = associated_rgba[2] * reciprocal_alpha;
76 separate_rgba[3] = alpha;
80 static inline void
81 separate_to_associated_rgba (const float *separate_rgba,
82 float *associated_rgba)
84 float alpha = associated_rgba[3];
85 float limited_alpha = babl_epsilon_for_zero_float (alpha);
87 associated_rgba[0] = separate_rgba[0] * limited_alpha;
88 associated_rgba[1] = separate_rgba[1] * limited_alpha;
89 associated_rgba[2] = separate_rgba[2] * limited_alpha;
90 associated_rgba[3] = alpha;
92 </pre>
95 <p>For more detils see <a href='https://gitlab.gnome.org/GNOME/babl/commit/a4d607843d3cab18745d547fc8a46dec51dcea5e'>the commit message of the most recent refinement</a> as well as <a href='https://www.patreon.com/posts/premultiplied-in-21014115'>blog post with further background</a>.</p>
99 <a href='graphics/index.html'><img class='BablFish' alt='/babl' title='babl' src='graphics/babl-48x48.png'/></a>
100 </div>
101 </div>
103 <div class='graphic'>
104 <div class='print'>
105 <img src='graphics/babl-a4poster.png' alt=' '/>
106 </div>
107 </div>
109 </body>
110 </html>