Update license to GNU GPL version 3.
[Ale.git] / d2 / render / psf / normalizer.h
bloba2c1b168ef46bd8ab08705c76f09465e763e4045
1 // Copyright 2003 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
21 #ifndef __normalizer_h__
22 #define __normalizer_h__
24 #include "../../point.h"
25 #include "rasterizer.h"
26 #include "raster.h"
29 * Normalizer for rasterized PSFs.
31 * This class normalizes a rasterized PSF.
34 class normalizer : public raster {
35 raster *input;
36 public:
37 unsigned int varieties() const {
38 return input->varieties();
41 unsigned int select(unsigned int i, unsigned int j) const {
42 return input->select(i, j);
45 private:
46 void initialize_response_array(ale_real *response_array) {
47 pixel integral;
49 integral = integrate(response_array);
51 for (unsigned int i = 0; i < _filter_dim_i; i++)
52 for (unsigned int j = 0; j < _filter_dim_j; j++)
53 for (unsigned int k = 0; k < 3 ; k++)
54 response_array[i * _filter_dim_j * 3 + j * 3 + k] /= integral[k];
57 public:
58 normalizer (raster *input) {
59 this->input = input;
61 _height = -input->min_i();
62 assert (input->max_i() == _height);
64 _width = -input->min_j();
65 assert (input->max_j() == _width);
68 * The element structure matches that of the input.
71 _filter_dim_i = input->max_elem_i();
72 _filter_dim_j = input->max_elem_j();
75 * Ensure that the array has an odd number of elements in each
76 * direction. This allows us to move the center to the right
77 * place when using FFTW.
80 assert (_filter_dim_i % 2 == 1);
81 assert (_filter_dim_j % 2 == 1);
84 * Determine the number of arrays to create.
87 num_arrays = input->varieties();
90 * Create arrays
93 response_arrays = (ale_real **)malloc(num_arrays * sizeof(ale_real *));
95 if (!response_arrays) {
96 fprintf(stderr, "Could not allocate in normalizer.\n");
97 exit(1);
100 for (unsigned int n = 0; n < num_arrays; n++) {
101 response_arrays[n] = (ale_real *)malloc(_filter_dim_i * _filter_dim_j * 3
102 * sizeof(ale_real));
104 if (!response_arrays[n]) {
105 fprintf(stderr, "Could not allocate in normalizer.\n");
106 exit(1);
109 for (unsigned int i = 0; i < _filter_dim_i; i++)
110 for (unsigned int j = 0; j < _filter_dim_j; j++)
111 for (unsigned int k = 0; k < 3; k++) {
112 response_arrays[n][i * _filter_dim_j * 3 + j * 3 + k]
113 = input->element(n, i, j, k);
116 initialize_response_array(response_arrays[n]);
119 #if 0
120 avg_response = (ale_real *)malloc(_filter_dim_i * _filter_dim_j * 3
121 * sizeof(ale_real));
123 if (!avg_response) {
124 fprintf(stderr, "Could not allocate in normalizer.\n");
125 exit(1);
128 for (unsigned int i = 0; i < _filter_dim_i; i++)
129 for (unsigned int j = 0; j < _filter_dim_j; j++)
130 for (unsigned int k = 0; k < 3; k++) {
131 avg_response[i * _filter_dim_j * 3 + j * 3 + k]
132 = input->element(i, j, k);
135 initialize_response_array(avg_response);
136 #endif
137 compute_integrals();
141 #endif