Add a class for handling reentrant randomization.
[Ale.git] / d2 / image_weighted_simple.h
blob4125f790e8b50f89648c21d5f944f61d6078b13b
1 // Copyright 2002, 2003, 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 2 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 * image_weighted_simple.h: Image representing a weighted average of inputs.
23 * Covers simple cases that require space constant with frame count.
26 #ifndef __image_weighted_simple_h__
27 #define __image_weighted_simple_h__
29 #include "image_weighted_avg.h"
30 #include "render/invariant.h"
32 class image_weighted_simple : public image_weighted_avg {
33 private:
34 invariant *inv;
35 image_ale_real *colors;
36 image_ale_real *weights;
38 public:
39 image_weighted_simple (unsigned int dimy, unsigned int dimx, unsigned int
40 depth, invariant *inv, char *name = "anonymous")
41 : image_weighted_avg(dimy, dimx, depth, name) {
42 colors = new image_ale_real(dimy, dimx, depth);
43 weights = new image_ale_real(dimy, dimx, depth);
44 this->inv = inv;
47 virtual ~image_weighted_simple() {
48 delete colors;
49 delete weights;
53 * Extend the image area to the top, bottom, left, and right,
54 * initializing the new image areas with black pixels. Negative values
55 * shrink the image.
57 void extend(int top, int bottom, int left, int right) {
58 colors->extend(top, bottom, left, right);
59 weights->extend(top, bottom, left, right);
61 _dimx = colors->width();
62 _dimy = colors->height();
63 _offset = colors->offset();
67 * Pre-transformation check for whether an area should be skipped.
68 * Takes image weights as an argument.
70 int accumulate_norender(int i, int j) {
72 * Initial value
74 if (inv->is_first() && weights->get_pixel(i, j)[0] != 0)
75 return 1;
77 return 0;
81 * Accumulate pixels
83 void accumulate(int i, int j, int f, pixel new_value, pixel new_weight) {
86 * Perform operations separately for each channel
88 for (unsigned int k = 0; k < 3; k++) {
90 const double min_weight = 0.001;
93 * Cases independent of the old pixel value and weight
94 * for which the update can be ignored. XXX: the
95 * minimum weight used here should probably be less
96 * than or equal to the certainty floor.
99 if (fabs(new_weight[k]) < min_weight
100 || (!inv->is_avg()
101 && new_weight[k] < render::get_wt())) {
102 continue;
106 * Cases independent of the old pixel value and weight for which
107 * previous pixel values can be ignored.
110 if (inv->is_last() && new_weight[k] >= render::get_wt()) {
111 colors->chan(i, j, k) = new_value[k];
112 weights->chan(i, j, k) = new_weight[k];
113 continue;
117 * Obtain the old pixel weight.
120 ale_real old_weight = weights->chan(i, j, k);
123 * Cases independent of the old pixel value for which the
124 * update can be ignored.
127 if (old_weight >= render::get_wt()
128 && inv->is_first())
129 continue;
132 * Cases independent of the old pixel value for which previous
133 * pixel values can be ignored.
136 if (old_weight == 0
137 || (old_weight < render::get_wt()
138 && !inv->is_avg())) {
139 weights->chan(i, j, k) = new_weight[k];
140 colors ->chan(i, j, k) = new_value[k];
141 continue;
145 * Obtain the old pixel value
148 ale_real old_value = colors->chan(i, j, k);
151 * Cases in which the old pixel value can be ignored
154 if ((inv->is_max()
155 && new_value[k] > old_value)
156 || (inv->is_min()
157 && new_value[k] < old_value)) {
158 weights->chan(i, j, k) = new_weight[k];
159 colors-> chan(i, j, k) = new_value[k];
160 continue;
164 * Cases in which the new pixel value can be ignored
167 if ((inv->is_max()
168 && old_value > new_value[k])
169 || (inv->is_min()
170 && old_value < new_value[k])) {
171 continue;
175 * Update the weight
178 ale_real updated_weight = old_weight + new_weight[k];
180 if (fabs(updated_weight) < min_weight) {
182 * XXX: Give up. Because of the way we
183 * represent weights and values, we can't
184 * handle small weights (although we could
185 * probably handle smaller weights than we
186 * currently handle). This could be fixed
187 * by always storing unnormalized values
188 * separately from weights, and dividing only
189 * once, just prior to image output.
191 continue;
194 weights->chan(i, j, k) = updated_weight;
197 * Update the channel
200 colors->chan(i, j, k) = (old_weight * colors->chan(i, j, k)
201 + new_weight[k] * new_value[k])
202 / updated_weight;
207 pixel get_pixel(unsigned int y, unsigned int x) const {
208 return colors->get_pixel(y, x);
211 image *get_weights() {
212 return weights;
215 image *get_colors() {
216 return colors;
220 #endif