BRL-CAD
operations.c
Go to the documentation of this file.
1 /* O P E R A T I O N S . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2013-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file libicv/operations.c
21  *
22  * This file contains routines for operations.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <math.h>
29 
30 #include "icv.h"
31 
32 #include "bio.h"
33 #include "bu/log.h"
34 #include "bu/magic.h"
35 #include "vmath.h"
36 
37 
39 {
40  double *data = NULL;
41  size_t size;
42 
43  ICV_IMAGE_VAL_INT(img);
44 
45  data= img->data;
46  for (size = img->width*img->height*img->channels; size>0; size--) {
47  if (*data>1.0)
48  *data = 1.0;
49  else if (*data<0)
50  *data = 0;
51  data++;
52  }
53  img->flags |= ICV_SANITIZED;
54  return 0;
55 }
56 
57 int icv_add_val(icv_image_t* img, double val)
58 {
59  double *data = NULL;
60  size_t size;
61 
62  data = img->data;
63 
64  ICV_IMAGE_VAL_INT(img);
65 
66  for (size = img->width*img->height*img->channels; size>0; size--) {
67  *data += val;
68  data++;
69  }
70 
71  if (img->flags & ICV_OPERATIONS_MODE)
72  img->flags&=(!ICV_SANITIZED);
73  else
74  icv_sanitize(img);
75 
76  return 0;
77 }
78 
79 int icv_multiply_val(icv_image_t* img, double val)
80 {
81  double *data = NULL;
82  size_t size;
83 
84  ICV_IMAGE_VAL_INT(img);
85 
86  data = img->data;
87 
88  for (size = img->width*img->height*img->channels; size>0; size--) {
89  *data *= val;
90  data++;
91  }
92  if ((img->flags & ICV_OPERATIONS_MODE))
93  img->flags&=(!ICV_SANITIZED);
94  else
95  icv_sanitize(img);
96 
97  return 0;
98 }
99 
100 int icv_divide_val(icv_image_t* img, double val)
101 {
102  double *data = NULL;
103  size_t size;
104 
105  ICV_IMAGE_VAL_INT(img);
106 
107  data = img->data;
108 
109  /* Since data is double dividing by 0 will result in INF and -INF */
110 
111  for (size = img->width*img->height*img->channels; size>0; size--) {
112  *data /= val;
113  data++;
114  }
115 
116  if ((img->flags & ICV_OPERATIONS_MODE))
117  img->flags&=(!ICV_SANITIZED);
118  else
119  icv_sanitize(img);
120 
121  return 0;
122 }
123 
124 int icv_pow_val(icv_image_t* img, double val)
125 {
126  double *data = NULL;
127  size_t size;
128 
129  ICV_IMAGE_VAL_INT(img);
130 
131  data = img->data;
132 
133  for (size = img->width*img->height*img->channels; size>0; size--) {
134  *data = pow(*data,val);
135  data++;
136  }
137 
138  if ((img->flags & ICV_OPERATIONS_MODE))
139  img->flags&=(!ICV_SANITIZED);
140  else
141  icv_sanitize(img);
142 
143  return 0;
144 }
145 
147 {
148  double *data1, *data2, *out_data;
149  size_t size;
150  icv_image_t *out_img;
151 
152  ICV_IMAGE_VAL_PTR(img1);
153  ICV_IMAGE_VAL_PTR(img2);
154 
155  if ((img1->width != img2->width) || (img1->height != img2->height) || (img1->channels != img2->channels)) {
156  bu_log("icv_add : Image Parameters not Equal");
157  return NULL;
158  }
159 
160  data1 =img1->data;
161  data2 =img2->data;
162 
163  out_img = icv_create(img1->width, img1->height, img1->color_space);
164 
165  out_data = out_img->data;
166 
167  for (size = img1->width*img1->height*img1->channels; size>0; size--)
168  *out_data++ = *data1++ + *data2++;
169 
170  icv_sanitize(out_img);
171 
172  return out_img;
173 }
174 
176 {
177  double *data1, *data2, *out_data;
178  size_t size;
179  icv_image_t *out_img;
180 
181  ICV_IMAGE_VAL_PTR(img1);
182  ICV_IMAGE_VAL_PTR(img2);
183 
184  if ((img1->width != img2->width) || (img1->height != img2->height) || (img1->channels != img2->channels)) {
185  bu_log("icv_add : Image Parameters not Equal");
186  return NULL;
187  }
188 
189  data1 =img1->data;
190  data2 =img2->data;
191 
192  out_img = icv_create(img1->width, img1->height, img1->color_space);
193 
194  out_data = out_img->data;
195 
196  for (size = img1->width*img1->height*img1->channels; size>0; size--)
197  *out_data++ = *data1++ - *data2++;
198 
199  icv_sanitize(out_img);
200 
201  return out_img;
202 }
203 
205 {
206  double *data1, *data2, *out_data;
207  size_t size;
208  icv_image_t *out_img;
209 
210  ICV_IMAGE_VAL_PTR(img1);
211  ICV_IMAGE_VAL_PTR(img2);
212 
213  if ((img1->width != img2->width) || (img1->height != img2->height) || (img1->channels != img2->channels)) {
214  bu_log("icv_add : Image Parameters not Equal");
215  return NULL;
216  }
217 
218  data1 =img1->data;
219  data2 =img2->data;
220 
221  out_img = icv_create(img1->width, img1->height, img1->color_space);
222 
223  out_data = out_img->data;
224 
225  for (size = img1->width*img1->height*img1->channels; size>0; size--)
226  *out_data++ = *data1++ * *data2++;
227 
228  icv_sanitize(out_img);
229 
230  return out_img;
231 }
232 
233 
235 {
236  double *data1, *data2, *out_data;
237  size_t size;
238  icv_image_t *out_img;
239 
240  ICV_IMAGE_VAL_PTR(img1);
241  ICV_IMAGE_VAL_PTR(img2);
242 
243  if ((img1->width != img2->width) || (img1->height != img2->height) || (img1->channels != img2->channels)) {
244  bu_log("icv_add : Image Parameters not Equal");
245  return NULL;
246  }
247 
248  data1 =img1->data;
249  data2 =img2->data;
250 
251  out_img = icv_create(img1->width, img1->height, img1->color_space);
252 
253  out_data = out_img->data;
254 
255  for (size = img1->width*img1->height*img1->channels; size>0; size--)
256  *out_data++ = *data1++ / (*data2++ + VDIVIDE_TOL);
257 
258  icv_sanitize(out_img);
259 
260  return out_img;
261 }
262 
263 int icv_saturate(icv_image_t* img, double sat)
264 {
265  double *data;
266  double bw; /* monochrome intensity */
267  double rwgt, gwgt, bwgt;
268  double rt, gt, bt;
269  long size;
270 
271  ICV_IMAGE_VAL_INT(img);
272 
273  if (img == NULL) {
274  bu_log("icv_saturate : Trying to Saturate a Null img");
275  return -1;
276  }
277 
278  if (img->color_space != ICV_COLOR_SPACE_RGB) {
279  bu_log("icv_saturate : Saturates only RGB Images");
280  return -1;
281  }
282 
283  data = img->data;
284  size = img->width*img->height;
285  rwgt = 0.31*(1.0-sat);
286  gwgt = 0.61*(1.0-sat);
287  bwgt = 0.08*(1.0-sat);
288  while (size-- > 0) {
289  rt = *data;
290  gt = *(data+1);
291  bt = *(data+2);
292  bw = (rwgt*rt + gwgt*gt + bwgt*bt);
293  rt = bw + sat*rt;
294  gt = bw + sat*gt;
295  bt = bw + sat*bt;
296  *data++ = rt;
297  *data++ = gt;
298  *data++ = bt;
299  }
300  icv_sanitize(img);
301  return 0;
302 }
303 
304 /*
305  * Local Variables:
306  * tab-width: 8
307  * mode: C
308  * indent-tabs-mode: t
309  * c-file-style: "stroustrup"
310  * End:
311  * ex: shiftwidth=4 tabstop=8
312  */
Definition: db_flip.c:35
double * data
Definition: icv.h:89
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
int width
Definition: icv.h:91
icv_image_t * icv_divide(icv_image_t *img1, icv_image_t *img2)
Definition: operations.c:234
#define ICV_SANITIZED
Definition: icv.h:82
Header file for the BRL-CAD common definitions.
int channels
Definition: icv.h:91
Definition: icv.h:86
int icv_sanitize(icv_image_t *img)
Definition: operations.c:38
int height
Definition: icv.h:91
int icv_pow_val(icv_image_t *img, double val)
Definition: operations.c:124
COMPLEX data[64]
Definition: fftest.c:34
#define ICV_OPERATIONS_MODE
Definition: icv.h:83
#define ICV_IMAGE_VAL_PTR(_i)
Definition: icv.h:128
uint16_t flags
Definition: icv.h:92
int icv_divide_val(icv_image_t *img, double val)
Definition: operations.c:100
ICV_COLOR_SPACE color_space
Definition: icv.h:88
icv_image_t * icv_create(int width, int height, ICV_COLOR_SPACE color_space)
Definition: fileformat.c:276
int icv_saturate(icv_image_t *img, double sat)
Definition: operations.c:263
int icv_multiply_val(icv_image_t *img, double val)
Definition: operations.c:79
icv_image_t * icv_multiply(icv_image_t *img1, icv_image_t *img2)
Definition: operations.c:204
#define ICV_IMAGE_VAL_INT(_i)
Definition: icv.h:123
int icv_add_val(icv_image_t *img, double val)
Definition: operations.c:57
icv_image_t * icv_add(icv_image_t *img1, icv_image_t *img2)
Definition: operations.c:146
icv_image_t * icv_sub(icv_image_t *img1, icv_image_t *img2)
Definition: operations.c:175