fix crashes reported by Debian Cylab Mayhem Team
[swftools.git] / lib / art / art_alphagamma.c
blob4651883cf5781ceb722f7b121e655491f1e44b9e
1 /* Libart_LGPL - library of basic graphic primitives
2 * Copyright (C) 1998 Raph Levien
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.
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.
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.
20 /* Some functions to build alphagamma tables */
22 #include "config.h"
23 #include "art_alphagamma.h"
25 #include <math.h>
27 /**
28 * art_alphagamma_new: Create a new #ArtAlphaGamma.
29 * @gamma: Gamma value.
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.
39 * Return value: The newly created #ArtAlphaGamma.
40 **/
41 ArtAlphaGamma *
42 art_alphagamma_new (double gamma)
44 int tablesize;
45 ArtAlphaGamma *alphagamma;
46 int i;
47 int *table;
48 art_u8 *invtable;
49 double s, r_gamma;
51 tablesize = ceil (gamma * 8);
52 if (tablesize < 10)
53 tablesize = 10;
55 alphagamma = (ArtAlphaGamma *)art_alloc (sizeof(ArtAlphaGamma) +
56 ((1 << tablesize) - 1) *
57 sizeof(art_u8));
58 alphagamma->gamma = gamma;
59 alphagamma->invtable_size = tablesize;
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);
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);
72 return alphagamma;
75 /**
76 * art_alphagamma_free: Free an #ArtAlphaGamma.
77 * @alphagamma: An #ArtAlphaGamma.
79 * Frees the #ArtAlphaGamma.
80 **/
81 void
82 art_alphagamma_free (ArtAlphaGamma *alphagamma)
84 art_free (alphagamma);