5 * ***** BEGIN GPL LICENSE BLOCK *****
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
22 * All rights reserved.
24 * The Original Code is: all of this file.
26 * Contributor(s): none yet.
28 * ***** END GPL LICENSE BLOCK *****
31 /** \file blender/render/intern/source/gammaCorrectionTables.c
36 #include "gammaCorrectionTables.h"
40 /* WARNING; optimized, cannot be used to do gamma(invgamma()) and expect */
41 /* result remain identical (ton) */
43 /* gamma is only used here for correcting adding colors or alpha */
44 #define RE_DEFAULT_GAMMA 2.0
46 /* This 400 is sort of based on the number of intensity levels needed for */
47 /* the typical dynamic range of a medium, in this case CRTs. (Foley) */
48 /* (Actually, it says the number should be between 400 and 535.) */
49 #define RE_GAMMA_TABLE_SIZE 400
51 /* These indicate the status of the gamma lookup table --------------------- */
53 static float gamma_range_table[RE_GAMMA_TABLE_SIZE + 1];
54 static float gamfactor_table[RE_GAMMA_TABLE_SIZE];
55 static float inv_gamma_range_table[RE_GAMMA_TABLE_SIZE + 1];
56 static float inv_gamfactor_table[RE_GAMMA_TABLE_SIZE];
57 static float color_domain_table[RE_GAMMA_TABLE_SIZE + 1];
58 static float color_step;
59 static float inv_color_step;
60 static float valid_gamma;
61 static float valid_inv_gamma;
63 /* ------------------------------------------------------------------------- */
65 float gammaCorrect(float c)
70 i = floor(c * inv_color_step);
71 /* Clip to range [0,1]: outside, just do the complete calculation. */
72 /* We may have some performance problems here. Stretching up the LUT */
73 /* may help solve that, by exchanging LUT size for the interpolation. */
74 /* Negative colors are explicitly handled. */
75 if (i < 0) res = -pow(abs(c), valid_gamma);
76 else if (i >= RE_GAMMA_TABLE_SIZE ) res = pow(c, valid_gamma);
77 else res = gamma_range_table[i] +
78 ( (c - color_domain_table[i]) * gamfactor_table[i]);
81 } /* end of float gammaCorrect(float col) */
83 /* ------------------------------------------------------------------------- */
85 float invGammaCorrect(float col)
90 i = floor(col*inv_color_step);
91 /* Negative colors are explicitly handled. */
92 if (i < 0) res = -pow(abs(col), valid_inv_gamma);
93 else if (i >= RE_GAMMA_TABLE_SIZE) res = pow(col, valid_inv_gamma);
94 else res = inv_gamma_range_table[i] +
95 ( (col - color_domain_table[i]) * inv_gamfactor_table[i]);
98 } /* end of float invGammaCorrect(float col) */
101 /* ------------------------------------------------------------------------- */
103 void makeGammaTables(float gamma)
105 /* we need two tables: one forward, one backward */
109 valid_inv_gamma = 1.0f / gamma;
110 color_step = 1.0 / RE_GAMMA_TABLE_SIZE;
111 inv_color_step = (float) RE_GAMMA_TABLE_SIZE;
113 /* We could squeeze out the two range tables to gain some memory. */
114 for (i = 0; i < RE_GAMMA_TABLE_SIZE; i++) {
115 color_domain_table[i] = i * color_step;
116 gamma_range_table[i] = pow(color_domain_table[i],
118 inv_gamma_range_table[i] = pow(color_domain_table[i],
122 /* The end of the table should match 1.0 carefully. In order to avoid */
123 /* rounding errors, we just set this explicitly. The last segment may */
124 /* have a different length than the other segments, but our */
125 /* interpolation is insensitive to that. */
126 color_domain_table[RE_GAMMA_TABLE_SIZE] = 1.0;
127 gamma_range_table[RE_GAMMA_TABLE_SIZE] = 1.0;
128 inv_gamma_range_table[RE_GAMMA_TABLE_SIZE] = 1.0;
130 /* To speed up calculations, we make these calc factor tables. They are */
131 /* multiplication factors used in scaling the interpolation. */
132 for (i = 0; i < RE_GAMMA_TABLE_SIZE; i++ ) {
133 gamfactor_table[i] = inv_color_step
134 * (gamma_range_table[i + 1] - gamma_range_table[i]);
135 inv_gamfactor_table[i] = inv_color_step
136 * (inv_gamma_range_table[i + 1] - inv_gamma_range_table[i]);
139 } /* end of void makeGammaTables(float gamma) */
143 /* ------------------------------------------------------------------------- */