The BIG graph update
[rrdtool.git] / libraries / libart_lgpl-2.3.7 / art_alphagamma.c
1 /* Libart_LGPL - library of basic graphic primitives
2  * Copyright (C) 1998 Raph Levien
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /* Some functions to build alphagamma tables */
21
22 #include <math.h>
23 #include "art_misc.h"
24
25 #include "art_alphagamma.h"
26
27 /**
28  * art_alphagamma_new: Create a new #ArtAlphaGamma.
29  * @gamma: Gamma value.
30  *
31  * Create a new #ArtAlphaGamma for a specific value of @gamma. When
32  * correctly implemented (which is generally not the case in libart),
33  * alpha compositing with an alphagamma parameter is equivalent to
34  * applying the gamma transformation to source images, doing the alpha
35  * compositing (in linear intensity space), then applying the inverse
36  * gamma transformation, bringing it back to a gamma-adjusted
37  * intensity space.
38  *
39  * Return value: The newly created #ArtAlphaGamma.
40  **/
41 ArtAlphaGamma *
42 art_alphagamma_new (double gamma)
43 {
44   int tablesize;
45   ArtAlphaGamma *alphagamma;
46   int i;
47   int *table;
48   art_u8 *invtable;
49   double s, r_gamma;
50
51   tablesize = ceil (gamma * 8);
52   if (tablesize < 10)
53     tablesize = 10;
54
55   alphagamma = (ArtAlphaGamma *)art_alloc (sizeof(ArtAlphaGamma) +
56                                            ((1 << tablesize) - 1) *
57                                            sizeof(art_u8));
58   alphagamma->gamma = gamma;
59   alphagamma->invtable_size = tablesize;
60
61   table = alphagamma->table;
62   for (i = 0; i < 256; i++)
63     table[i] = (int)floor (((1 << tablesize) - 1) *
64                            pow (i * (1.0 / 255), gamma) + 0.5);
65
66   invtable = alphagamma->invtable;
67   s = 1.0 / ((1 << tablesize) - 1);
68   r_gamma = 1.0 / gamma;
69   for (i = 0; i < 1 << tablesize; i++)
70     invtable[i] = (int)floor (255 * pow (i * s, r_gamma) + 0.5);
71
72   return alphagamma;
73 }
74
75 /**
76  * art_alphagamma_free: Free an #ArtAlphaGamma.
77  * @alphagamma: An #ArtAlphaGamma.
78  *
79  * Frees the #ArtAlphaGamma.
80  **/
81 void
82 art_alphagamma_free (ArtAlphaGamma *alphagamma)
83 {
84   art_free (alphagamma);
85 }