1 /**********
2 *
3 * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
4 * University of Manchester, UK. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
8 *
9 * . Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * . Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * . Neither the name of the University of Manchester nor the names of its
17 * contributors may be used to endorse or promote products derived from this
18 * software without specific prior written permission.
19 *
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 **********
34 *
35 * Program : TINA
36 * File : $Source: /home/tina/cvs/tina-libs/tina/image/imgPrc_add.c,v $
37 * Date : $Date: 2003/09/22 16:09:02 $
38 * Version : $Revision: 1.5 $
39 * CVS Id : $Id: imgPrc_add.c,v 1.5 2003/09/22 16:09:02 tony Exp $
40 *
41 * Author : Legacy TINA
42 */
43 /**
44 * @file
45 * @brief Add a constant value to all elements in an image.
46 *
47 * All pixel types, including complex, are supported.
48 *
49 *
50 */
51
52 #include "imgPrc_add.h"
53
54 #if HAVE_CONFIG_H
55 #include <config.h>
56 #endif
57
58 #include <math.h>
59 #include <tina/sys/sysDef.h>
60 #include <tina/sys/sysPro.h>
61 #include <tina/math/mathDef.h>
62 #include <tina/math/mathPro.h>
63 #include <tina/image/img_GenDef.h>
64 #include <tina/image/img_GenPro.h>
65
66 Imrect *imc_add(int k, Imrect * im)
67 {
68 Imrect *im2;
69 Imregion *roi;
70 int *row1, *row2;
71 int lx, ux, ly, uy;
72 int width, height;
73 int i, j;
74
75 if (im == NULL)
76 return (NULL);
77
78 roi = im->region;
79 if (roi == NULL)
80 return (NULL);
81 lx = roi->lx;
82 ux = roi->ux;
83 ly = roi->ly;
84 uy = roi->uy;
85
86 width = im->width;
87 height = im->height;
88
89 im2 = im_alloc(height, width, roi, char_v);
90 row1 = ivector_alloc(lx, ux);
91 row2 = ivector_alloc(lx, ux);
92
93 for (i = ly; i < uy; ++i)
94 {
95 im_get_row(row1, im, i, lx, ux);
96 for (j = lx; j < ux; ++j)
97 {
98 /*
99 * warning if you are trying to compute negative
100 * chars here there will be problems
101 */
102 if (row1[j] + k < 0)
103 row2[j] = 0;
104 else if (row1[j] + k > 255)
105 row2[j] = 255;
106 else
107 row2[j] = row1[j] + k;
108 }
109 im_put_row(row2, im2, i, lx, ux);
110 }
111
112 ivector_free(row1, lx);
113 ivector_free(row2, lx);
114 return (im2);
115 }
116
117 Imrect *imi_add(int k, Imrect * im)
118 {
119 Imrect *im2;
120 Imregion *roi;
121 int *row1, *row2;
122 int lx, ux, ly, uy;
123 int width, height;
124 int i, j;
125
126 if (im == NULL)
127 return (NULL);
128
129 roi = im->region;
130 if (roi == NULL)
131 return (NULL);
132 lx = roi->lx;
133 ux = roi->ux;
134 ly = roi->ly;
135 uy = roi->uy;
136
137 width = im->width;
138 height = im->height;
139
140 im2 = im_alloc(height, width, roi, int_v);
141 row1 = ivector_alloc(lx, ux);
142 row2 = ivector_alloc(lx, ux);
143
144 for (i = ly; i < uy; ++i)
145 {
146 im_get_row(row1, im, i, lx, ux);
147 for (j = lx; j < ux; ++j)
148 row2[j] = row1[j] + k;
149 im_put_row(row2, im2, i, lx, ux);
150 }
151
152 ivector_free(row1, lx);
153 ivector_free(row2, lx);
154 return (im2);
155 }
156
157 Imrect *imf_add(double k, Imrect * im)
158 {
159 Imrect *im2;
160 Imregion *roi;
161 float *row1, *row2;
162 int lx, ux, ly, uy;
163 int width, height;
164 int i, j;
165
166 if (im == NULL)
167 return (NULL);
168
169 roi = im->region;
170 if (roi == NULL)
171 return (NULL);
172 lx = roi->lx;
173 ux = roi->ux;
174 ly = roi->ly;
175 uy = roi->uy;
176
177 width = im->width;
178 height = im->height;
179
180 im2 = im_alloc(height, width, roi, float_v);
181 row1 = fvector_alloc(lx, ux);
182 row2 = fvector_alloc(lx, ux);
183
184 for (i = ly; i < uy; ++i)
185 {
186 im_get_rowf(row1, im, i, lx, ux);
187 for (j = lx; j < ux; ++j)
188 row2[j] = (float) (row1[j] + k);
189 im_put_rowf(row2, im2, i, lx, ux);
190 }
191
192 fvector_free(row1, lx);
193 fvector_free(row2, lx);
194 return (im2);
195 }
196
197 Imrect *imz_add(Complex k, Imrect * im)
198 {
199 Imrect *im2;
200 Imregion *roi;
201 Complex *row1, *row2;
202 int lx, ux, ly, uy;
203 int width, height;
204 int i, j;
205
206 if (im == NULL)
207 return (NULL);
208
209 roi = im->region;
210 if (roi == NULL)
211 return (NULL);
212 lx = roi->lx;
213 ux = roi->ux;
214 ly = roi->ly;
215 uy = roi->uy;
216
217 width = im->width;
218 height = im->height;
219
220 im2 = im_alloc(height, width, roi, complex_v);
221 row1 = zvector_alloc(lx, ux);
222 row2 = zvector_alloc(lx, ux);
223
224 for (i = ly; i < uy; ++i)
225 {
226 im_get_rowz(row1, im, i, lx, ux);
227 for (j = lx; j < ux; ++j)
228 {
229 row2[j].x = row1[j].x + k.x;
230 row2[j].y = row1[j].y + k.y;
231 }
232 im_put_rowz(row2, im2, i, lx, ux);
233 }
234
235 zvector_free(row1, lx);
236 zvector_free(row2, lx);
237 return (im2);
238 }
239
240 Imrect *im_add(k, im)
241 Imrect *im;
242 double k;
243 {
244 if (im == NULL)
245 return (NULL);
246 switch (im->vtype)
247 {
248 case uchar_v:
249 case char_v:
250 return (imc_add((int) k, im));
251 case short_v:
252 case ushort_v:
253 case int_v:
254 return (imi_add((int) k, im));
255 case float_v:
256 return (imf_add(k, im));
257 case complex_v: /* im sure this should be cmplx(k,k) NAT */
258 return (imz_add(cmplx(k, 0.0), im));
259 default:
260 return (NULL);
261 }
262 }
263
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.