d2::align::_align(): Limit skipping of the alignment process based on
[Ale.git] / d2 / image_weighted_simple.h
blobabc4b41b52d4e95d91189af9d3897bd5f470c8e1
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 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 * 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 *colors;
36 image *weights;
38 public:
39 image_weighted_simple (unsigned int dimy, unsigned int dimx, unsigned int
40 depth, invariant *inv, const 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 image *_extend(int top, int bottom, int left, int right) {
58 extend(&colors, top, bottom, left, right);
59 extend(&weights, top, bottom, left, right);
61 _dimx = colors->width();
62 _dimy = colors->height();
63 _offset = colors->offset();
65 return NULL;
69 * Pre-transformation check for whether an area should be skipped.
70 * Takes image weights as an argument.
72 int accumulate_norender(int i, int j) {
74 * Initial value
76 if (inv->is_first() && weights->get_chan(i, j, 0) != 0)
77 return 1;
79 * Weight limit satisfied
81 if (inv->is_avgf()
82 && weights->get_chan(i, j, 0) > inv->get_param()
83 && weights->get_chan(i, j, 1) > inv->get_param()
84 && weights->get_chan(i, j, 2) > inv->get_param())
85 return 1;
87 return 0;
91 * Accumulate pixels
93 void accumulate(int i, int j, int f, pixel new_value, pixel new_weight) {
96 * Perform operations separately for each channel
98 for (unsigned int k = 0; k < 3; k++) {
101 * Cases independent of the old pixel value and weight
102 * for which the update can be ignored.
105 if (!inv->is_avgx()
106 && new_weight[k] < render::get_wt())
107 continue;
110 * Cases independent of the old pixel value and weight for which
111 * previous pixel values can be ignored.
114 if (inv->is_last() && new_weight[k] >= render::get_wt()) {
115 colors->set_chan(i, j, k, new_value[k]);
116 weights->set_chan(i, j, k, new_weight[k]);
117 continue;
121 * Obtain the old pixel weight.
124 ale_real old_weight = weights->get_chan(i, j, k);
127 * Cases independent of the old pixel value for which the
128 * update can be ignored.
131 if (old_weight >= render::get_wt()
132 && inv->is_first())
133 continue;
135 if (inv->is_avgf()
136 && old_weight >= inv->get_param())
137 continue;
140 * Cases independent of the old pixel value for which previous
141 * pixel values can be ignored.
144 if (old_weight == 0
145 || (old_weight < render::get_wt()
146 && !inv->is_avgx())) {
147 weights->set_chan(i, j, k, new_weight[k]);
148 colors ->set_chan(i, j, k, new_value[k]);
149 continue;
152 if (inv->is_max()) {
155 * Cases in which the old pixel value can be ignored
158 if (new_value[k] * (ale_real) weights->get_chan(i, j, k)
159 > (ale_real) colors->get_chan(i, j, k) * new_weight[k]) {
160 weights->set_chan(i, j, k, new_weight[k]);
161 colors-> set_chan(i, j, k, new_value[k]);
164 continue;
166 } else if (inv->is_min()) {
168 * Cases in which the old pixel value can be ignored
171 if (new_value[k] * (ale_real) weights->get_chan(i, j, k)
172 < (ale_real) colors->get_chan(i, j, k) * new_weight[k]) {
173 weights->set_chan(i, j, k, new_weight[k]);
174 colors-> set_chan(i, j, k, new_value[k]);
177 continue;
182 * Update weight and color values.
185 weights->set_chan(i, j, k, weights->get_chan(i, j, k) + new_weight[k]);
186 colors->set_chan(i, j, k, colors->get_chan(i, j, k) + new_value[k]);
191 spixel get_pixel(unsigned int y, unsigned int x) const {
192 return (pixel) colors->get_pixel(y, x) / (pixel) weights->get_pixel(y, x);
195 image *get_weights() {
196 return weights;
199 image *get_colors() {
200 assert(0);
201 return colors;
205 #endif