d2::exposure::exposure_default: Increase interpolation bits from 4 to 6.
[Ale.git] / d2 / exposure / exposure_default.h
blobcddcb0b228baed8ab22f7b05ce43fbe8ac6298d4
1 // Copyright 2004 David Hilvert <dhilvert@auricle.dyndns.org>,
2 // <dhilvert@ugcs.caltech.edu>
4 /* This file is part of the Anti-Lamenessing Engine.
6 The Anti-Lamenessing Engine is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 The Anti-Lamenessing Engine is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with the Anti-Lamenessing Engine; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * exposure_default.h: Default exposure properties.
25 #ifndef __exposure_default_h__
26 #define __exposure_default_h__
29 * The default exposure is modeled after the simple power transfer function
30 * described in
32 * http://netpbm.sourceforge.net/doc/pnmgamma.html
34 * Note: optimizations in d2/image_rw.h depend on the details of this function.
37 class exposure_default : public exposure {
38 public:
39 pixel linearize(pixel input) const {
40 #if 0
42 * Calling pow() may be expensive on some platforms (e.g.,
43 * those lacking hardware support for floating point).
46 return ppow(input, 1/0.45) * get_multiplier();
47 #else
48 const int table_size = 1024;
49 const int table_bits = 10;
50 const int interp_bits = 6;
51 static int table_is_built = 0;
52 static ale_real table[table_size];
54 pixel result;
56 if (!table_is_built) {
57 for (int i = 0; i < table_size; i++) {
58 table[i] = pow((float) i / (float) (table_size - 1), 1/0.45);
60 table_is_built = 1;
63 for (int k = 0; k < 3; k++) {
65 * Clamp.
67 if (input[k] >= 1) {
68 result[k] = 1;
69 continue;
70 } else if (input[k] <= 0) {
71 result[k] = 0;
72 continue;
75 int index1 = ale_real_to_int(input[k], 65535);
77 int index2 = index1 >> (16 - table_bits);
78 int index3 = (index1 >> (16 - table_bits - interp_bits))
79 & ((1 << interp_bits) - 1);
81 if (index2 >= table_size - 1) {
82 result[k] = 1;
83 continue;
86 ale_real frac = ale_real_from_int((index3 << (16 - interp_bits)), 65535);
88 result[k] = (1 - frac) * table[index2] + frac * table[index2 + 1];
91 return result * get_multiplier();
92 #endif
94 pixel unlinearize(pixel input) const {
95 #if 0
97 * Calling pow() may be expensive on some platforms (e.g.,
98 * those lacking hardware support for floating point).
101 return ppow(input / get_multiplier(), 0.45);
102 #else
104 input /= get_multiplier();
106 const int table_size = 1024;
107 const int table_bits = 10;
108 const int interp_bits = 6;
109 static int table_is_built = 0;
110 static ale_real table[table_size];
112 pixel result;
114 if (!table_is_built) {
115 for (int i = 0; i < table_size; i++) {
116 table[i] = pow((float) i / (float) (table_size - 1), 0.45);
118 table_is_built = 1;
121 for (int k = 0; k < 3; k++) {
123 * Clamp.
125 if (input[k] >= 1) {
126 result[k] = 1;
127 continue;
128 } else if (input[k] <= 0) {
129 result[k] = 0;
130 continue;
133 int index1 = ale_real_to_int(input[k], 65535);
135 int index2 = index1 >> (16 - table_bits);
136 int index3 = (index1 >> (16 - table_bits - interp_bits))
137 & ((1 << interp_bits) - 1);
139 if (index2 >= table_size - 1) {
140 result[k] = 1;
141 continue;
144 ale_real frac = ale_real_from_int((index3 << (16 - interp_bits)), 65535);
146 result[k] = (1 - frac) * table[index2] + frac * table[index2 + 1];
149 return result;
150 #endif
154 #endif