doc/user: Add captions to uncaptioned tables.
[Ale.git] / d2 / image_bayer_ale_real.h
blob7cb3cdd864e7489b26c5d13ba0ddcd351367e851
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_bayer_ale_real.h: Bayer-patterned image represented by an array of ale_reals
25 #ifndef __image_bayer_ale_real_h__
26 #define __image_bayer_ale_real_h__
28 #include "exposure/exposure.h"
29 #include "point.h"
30 #include "image.h"
31 #include "image_ale_real.h"
33 class image_bayer_ale_real : public image {
34 private:
35 ale_real *_p;
37 private:
39 * X offset of 'R' element
41 unsigned int r_x_offset() const {
42 return bayer & 0x1;
46 * Y offset of 'R' element
48 unsigned int r_y_offset() const {
49 return (bayer & 0x2) >> 1;
52 public:
54 * Return the color of a given pixel.
56 unsigned int bayer_color(unsigned int i, unsigned int j) const {
57 return (i + r_y_offset()) % 2 + (j + r_x_offset()) % 2;
60 private:
61 void trigger(pixel multiplier) {
62 for (unsigned int i = 0; i < _dimy; i++)
63 for (unsigned int j = 0; j < _dimx; j++)
64 _p[i * _dimx + j] *= multiplier[bayer_color(i, j)];
67 public:
68 image_bayer_ale_real (unsigned int dimy, unsigned int dimx, unsigned int depth,
69 unsigned int bayer, char *name = "anonymous", exposure *exp = NULL)
70 : image(dimy, dimx, depth, name, exp, bayer) {
72 assert (bayer == IMAGE_BAYER_BGRG
73 || bayer == IMAGE_BAYER_GBGR
74 || bayer == IMAGE_BAYER_GRGB
75 || bayer == IMAGE_BAYER_RGBG);
77 _p = new ale_real[dimx * dimy];
79 assert (_p);
81 if (!_p) {
82 fprintf(stderr, "Could not allocate memory for image data.\n");
83 exit(1);
87 virtual ~image_bayer_ale_real() {
88 delete[] _p;
91 ale_real &chan(unsigned int y, unsigned int x, unsigned int k) {
92 assert (k == bayer_color(y, x));
93 return _p[y * _dimx + x];
96 const ale_real &chan(unsigned int y, unsigned int x, unsigned int k) const {
97 assert (k == bayer_color(y, x));
98 return _p[y * _dimx + x];
101 spixel &pix(unsigned int y, unsigned int x) {
102 assert(0);
104 static spixel foo;
105 return foo;
108 const spixel &pix(unsigned int y, unsigned int x) const {
109 static spixel foo = get_pixel(y, x);
110 return foo;
114 * This method throws away data not stored at this pixel
115 * position.
117 void set_pixel(unsigned int y, unsigned int x, spixel p) {
118 chan(y, x, bayer_color(y, x)) = p[bayer_color(y, x)];
122 * This method uses bilinear interpolation.
124 pixel get_pixel(unsigned int y, unsigned int x) const {
125 pixel result;
126 unsigned int k = bayer_color(y, x);
127 ale_real sum;
128 unsigned int num;
130 result[k] = chan(y, x, k);
132 if (k == 1) {
133 unsigned int k1 = bayer_color(y + 1, x);
134 unsigned int k2 = 2 - k1;
136 sum = 0; num = 0;
137 if (y > 0) {
138 sum += chan(y - 1, x, k1);
139 num++;
141 if (y < _dimy - 1) {
142 sum += chan(y + 1, x, k1);
143 num++;
145 assert (num > 0);
146 result[k1] = sum / num;
148 sum = 0; num = 0;
149 if (x > 0) {
150 sum += chan(y, x - 1, k2);
151 num++;
153 if (x < _dimx - 1) {
154 sum += chan(y, x + 1, k2);
155 num++;
157 assert (num > 0);
158 result[k2] = sum / num;
160 return result;
163 sum = 0; num = 0;
164 if (y > 0) {
165 sum += chan(y - 1, x, 1);
166 num++;
168 if (x > 0) {
169 sum += chan(y, x - 1, 1);
170 num++;
172 if (y < _dimy - 1) {
173 sum += chan(y + 1, x, 1);
174 num++;
176 if (x < _dimx - 1) {
177 sum += chan(y, x + 1, 1);
178 num++;
180 assert (num > 0);
181 result[1] = sum / num;
183 sum = 0; num = 0;
184 if (y > 0 && x > 0) {
185 sum += chan(y - 1, x - 1, 2 - k);
186 num++;
188 if (y > 0 && x < _dimx - 1) {
189 sum += chan(y - 1, x + 1, 2 - k);
190 num++;
192 if (y < _dimy - 1 && x > 0) {
193 sum += chan(y + 1, x - 1, 2 - k);
194 num++;
196 if (y < _dimy - 1 && x < _dimx - 1) {
197 sum += chan(y + 1, x + 1, 2 - k);
198 num++;
200 result[2 - k] = sum/num;
202 return result;
206 * Make a new image suitable for receiving scaled values.
208 virtual image *scale_generator(int height, int width, int depth, char *name) const {
209 return new image_ale_real(height, width, depth, name, _exp);
213 * Extend the image area to the top, bottom, left, and right,
214 * initializing the new image areas with black pixels.
216 void extend(int top, int bottom, int left, int right) {
218 * Bayer-patterned images should always represent inputs,
219 * which should not ever be extended.
221 assert(0);
226 #endif