bugs: suggest image_rw return either d2::image or ale_image, according to request.
[Ale.git] / d2 / image_accel.h
blobc7f567ff61ad43b36bbb6b1cba644ebee91e3e81
1 // Copyright 2002, 2003, 2004, 2008 David Hilvert <dhilvert@auricle.dyndns.org>,
2 // <dhilvert@ugcs.caltech.edu>,
3 // <dhilvert@gmail.com>
5 /* This file is part of the Anti-Lamenessing Engine.
7 The Anti-Lamenessing Engine is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 The Anti-Lamenessing Engine is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with the Anti-Lamenessing Engine; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * image_accel.h: Image represented by a (possibly accelerated) opaque type.
26 #ifndef __image_accel_h__
27 #define __image_accel_h__
29 #include "exposure/exposure.h"
30 #include "point.h"
31 #include "image.h"
33 class image_accel : public image {
34 private:
36 * Libale opaque type.
39 void *im;
41 public:
43 image_accel (unsigned int dimy, unsigned int dimx, unsigned int
44 depth, unsigned int bayer, const char *name = "anonymous",
45 exposure *exp = NULL)
46 : image(dimy, dimx, depth, name, exp, bayer) {
48 assert (depth == 3);
50 // int libale_bayer;
52 switch (bayer) {
53 case IMAGE_BAYER_NONE:
54 // libale_bayer = ALE_FORMAT_RGB;
55 break;
56 case IMAGE_BAYER_RGBG:
57 // libale_bayer = ALE_FORMAT_BAYER_RGBG;
58 break;
59 case IMAGE_BAYER_GBGR:
60 // libale_bayer = ALE_FORMAT_BAYER_GBGR;
61 break;
62 case IMAGE_BAYER_GRGB:
63 // libale_bayer = ALE_FORMAT_BAYER_GRGB;
64 break;
65 case IMAGE_BAYER_BGRG:
66 // libale_bayer = ALE_FORMAT_BAYER_BGRG;
67 break;
68 default:
69 assert(0);
72 // im = ale_new_domain_2d(accel::context(), libale_bayer, ALE_TYPE_FLOAT_32, dimy, dimx);
74 assert (im);
76 if (!im) {
77 fprintf(stderr, "Could not allocate image data.\n");
78 exit(1);
82 image_accel (const image *source) : image(source, 0) {
84 assert (_depth == 3);
86 // int libale_bayer;
88 switch (bayer) {
89 case IMAGE_BAYER_NONE:
90 // libale_bayer = ALE_FORMAT_RGB;
91 break;
92 case IMAGE_BAYER_RGBG:
93 // libale_bayer = ALE_FORMAT_BAYER_RGBG;
94 break;
95 case IMAGE_BAYER_GBGR:
96 // libale_bayer = ALE_FORMAT_BAYER_GBGR;
97 break;
98 case IMAGE_BAYER_GRGB:
99 // libale_bayer = ALE_FORMAT_BAYER_GRGB;
100 break;
101 case IMAGE_BAYER_BGRG:
102 // libale_bayer = ALE_FORMAT_BAYER_BGRG;
103 break;
104 default:
105 assert(0);
108 // im = ale_new_domain_2d(accel::context(), libale_bayer, ALE_TYPE_FLOAT_32, _dimy, _dimx);
110 if (!im) {
111 fprintf(stderr, "Could not allocate Libale domain.\n");
112 exit(1);
116 * Copy image data
119 float *data = NULL;
121 if (bayer == IMAGE_BAYER_NONE) {
122 data = (float *) malloc(_dimy * _dimx * _depth * sizeof(float));
124 if (!data) {
125 fprintf(stderr, "Could not allocate image data.\n");
126 exit(1);
129 for (unsigned int i = 0; i < _dimy; i++)
130 for (unsigned int j = 0; j < _dimx; j++)
131 for (unsigned int k = 0; k < _depth; k++)
132 data[i * _dimx * _depth + j * _depth + k] = source->get_chan(i, j, k);
134 } else {
135 data = (float *) malloc(_dimy * _dimx * sizeof(float));
137 if (!data) {
138 fprintf(stderr, "Could not allocate image data.\n");
139 exit(1);
142 for (unsigned int i = 0; i < _dimy; i++)
143 for (unsigned int j = 0; j < _dimx; j++)
144 data[i * _dimx + j] = source->get_chan(i, j, source->bayer_color(i, j));
147 // ale_load_into_domain(im, data);
149 free(data);
152 virtual ~image_accel() {
153 // ale_delete_domain_2d(im);
156 virtual int accel_type() {
157 return 1;
160 virtual image *unaccel_equiv() {
162 * XXX: An unaccelerated equivalent should be created here.
165 assert(0);
167 return NULL;
170 spixel get_pixel(unsigned int y, unsigned int x) const {
171 assert(0);
174 void set_pixel(unsigned int y, unsigned int x, spixel p) {
175 assert(0);
178 void mul_pixel(unsigned int y, unsigned int x, spixel p) {
179 assert(0);
182 void add_pixel(unsigned int y, unsigned int x, pixel p) {
183 assert(0);
186 ale_sreal get_chan(unsigned int y, unsigned int x, unsigned int k) const {
187 assert(0);
190 void set_chan(unsigned int y, unsigned int x, unsigned int k, ale_sreal c) {
191 assert(0);
194 void div_chan(unsigned int y, unsigned int x, unsigned int k, ale_sreal c) {
195 assert(0);
199 * Make a new image suitable for receiving scaled values.
201 virtual image *scale_generator(int height, int width, int depth, const char *name) const {
202 return new image_accel(height, width, depth, IMAGE_BAYER_NONE, name, _exp);
206 * Extend the image area to the top, bottom, left, and right,
207 * initializing the new image areas with black pixels. Negative values
208 * shrink the image.
210 image *_extend(int top, int bottom, int left, int right) {
212 image *is = new image_accel (
213 height() + top + bottom,
214 width() + left + right , depth(), bayer, name, _exp);
216 assert(is);
218 unsigned int min_i = (-top > 0)
219 ? -top
220 : 0;
222 unsigned int min_j = (-left > 0)
223 ? -left
224 : 0;
226 unsigned int max_i = (height() < is->height() - top)
227 ? height()
228 : is->height() - top;
230 unsigned int max_j = (width() < is->width() - left)
231 ? width()
232 : is->width() - left;
234 for (unsigned int i = min_i; i < max_i; i++)
235 for (unsigned int j = min_j; j < max_j; j++)
236 is->set_pixel(i + top, j + left, get_pixel(i, j));
238 is->set_offset(_offset[0] - top, _offset[1] - left);
240 return is;
243 private:
244 void trigger(pixel multiplier) {
245 for (unsigned int i = 0; i < height(); i++)
246 for (unsigned int j = 0; j < width(); j++) {
247 mul_pixel(i, j, multiplier);
252 #endif