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_create.c,v $
37 * Date : $Date: 2003/09/22 16:09:02 $
38 * Version : $Revision: 1.5 $
39 * CVS Id : $Id: imgPrc_create.c,v 1.5 2003/09/22 16:09:02 tony Exp $
40 *
41 * Author : Legacy TINA
42 */
43
44 /**
45 * @file
46 * @brief Generate various image patterns, for test purposes.
47 *
48 */
49
50 #include "imgPrc_create.h"
51
52 #if HAVE_CONFIG_H
53 #include <config.h>
54 #endif
55
56 #include <math.h>
57 #include <tina/sys/sysDef.h>
58 #include <tina/sys/sysPro.h>
59 #include <tina/math/mathDef.h>
60 #include <tina/math/mathPro.h>
61 #include <tina/image/img_GenDef.h>
62 #include <tina/image/img_GenPro.h>
63 #include <tina/image/imgPrc_combine.h>
64
65 Imrect *imf_checquer(int width, int height, int dx, int dy)
66 {
67 float *row1, *row2;
68 Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
69 int i, j;
70
71 row1 = fvector_alloc(0, width);
72 for (i = 0, j = 0; i < width; i++, j++)
73 {
74 if (j < dx)
75 row1[i] = (float) 0.0;
76 else if (j < 2 * dx)
77 row1[i] = (float) 255.0;
78
79 if (j == 2 * dx - 1)
80 j = -1;
81 }
82
83 row2 = fvector_alloc(0, width);
84 for (i = 0, j = 0; i < width; i++, j++)
85 {
86 if (j < dx)
87 row2[i] = (float) 255.0;
88 else if (j < 2 * dx)
89 row2[i] = (float) 0.0;
90
91 if (j == 2 * dx - 1)
92 j = -1;
93 }
94
95 for (i = 0, j = 0; i < height; i++, j++)
96 {
97 if (j < dy)
98 im_put_rowf(row1, im, i, 0, width);
99 else if (j < 2 * dx)
100 im_put_rowf(row2, im, i, 0, width);
101
102 if (j == 2 * dy - 1)
103 j = -1;
104 }
105 fvector_free(row1, 0);
106 fvector_free(row2, 0);
107 return (im);
108 }
109
110 Imrect *imf_rect(int width, int height, int lx, int ly, int ux, int uy)
111 {
112 float *row1, *row2;
113 Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
114 int i;
115
116 row1 = fvector_alloc(0, width);
117 for (i = 0; i < width; i++)
118 {
119 if (i < lx)
120 row1[i] = (float) 0.0;
121 else if (i < ux)
122 row1[i] = (float) 255.0 *(i - lx) / (double) (ux - lx);
123 else
124 row1[i] = (float) 0.0;
125 }
126 row2 = fvector_alloc(0, width);
127
128 for (i = 0; i < height; i++)
129 {
130 if (i < ly)
131 im_put_rowf(row2, im, i, 0, width);
132 else if (i < uy)
133 im_put_rowf(row1, im, i, 0, width);
134 else
135 im_put_rowf(row2, im, i, 0, width);
136 }
137 fvector_free(row1, 0);
138 fvector_free(row2, 0);
139 return (im);
140 }
141
142 Imrect *imf_ellipse(int width, int height, double cx, double cy, double ax, double ay)
143 {
144 float *row;
145 Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
146 int x, y;
147
148 row = fvector_alloc(0, width);
149 for (y = 0; y < height; y++)
150 {
151 for (x = 0; x < width; x++)
152 {
153 float x1 = (float) ((x - cx) / ax);
154 float y1 = (float) ((y - cy) / ay);
155
156 if (x1 * x1 + y1 * y1 > 1.0)
157 row[x] = (float) 0.0;
158 else
159 row[x] = (float) 255.0;
160 }
161 im_put_rowf(row, im, y, 0, width);
162 }
163
164 fvector_free(row, 0);
165 return (im);
166 }
167
168 Imrect *imf_subpix_ellipse(int width, int height, double cx, double cy, double ax, double ay)
169 {
170 float *row;
171 Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
172 int x, y;
173
174 row = fvector_alloc(0, width);
175 for (y = 0; y < height; y++)
176 {
177 for (x = 0; x < width; x++)
178 {
179 double x1 = (x - cx) / ax;
180 double y1 = (y - cy) / ay;
181 double x2 = (x - cx + 1) / ax;
182 double y2 = (y - cy + 1) / ay;
183 double s11, s12, s21, s22;
184
185 s11 = x1 * x1 + y1 * y1;
186 s21 = x2 * x2 + y1 * y1;
187 s12 = x1 * x1 + y2 * y2;
188 s22 = x2 * x2 + y2 * y2;
189 if (s11 > 1.0 && s12 > 1.0 && s21 > 1.0 && s22 > 1.0)
190 row[x] = (float) 0.0;
191 else if (s11 < 1.0 && s12 < 1.0 && s21 < 1.0 && s22 < 1.0)
192 row[x] = (float) 255.0;
193 else
194 {
195 int n = 32, k, l;
196 double sum = 0.0, dx, dy;
197
198 dx = 1.0 / (n * ax);
199 dy = 1.0 / (n * ay);
200 for (k = 0; k < n; k++)
201 {
202 double xk = x1 + k * dx;
203
204 for (l = 0; l < n; l++)
205 {
206 double yl = y1 + l * dy;
207
208 if (xk * xk + yl * yl < 1.0)
209 sum += 1.0;
210 }
211 }
212 row[x] = (float) (sum * 255.0 / (n * n));
213 }
214 }
215 im_put_rowf(row, im, y, 0, width);
216 }
217
218 fvector_free(row, 0);
219 return (im);
220 }
221
222 Imrect *imf_subpix_ellipsoid(int width, int height, double cx, double cy, double ax, double ay)
223 {
224 float *row;
225 Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
226 int x, y;
227
228 row = fvector_alloc(0, width);
229 for (y = 0; y < height; y++)
230 {
231 for (x = 0; x < width; x++)
232 {
233 double x1 = (x - cx) / ax;
234 double y1 = (y - cy) / ay;
235 double x2 = (x - cx + 1) / ax;
236 double y2 = (y - cy + 1) / ay;
237 double s11, s12, s21, s22;
238
239 s11 = x1 * x1 + y1 * y1;
240 s21 = x2 * x2 + y1 * y1;
241 s12 = x1 * x1 + y2 * y2;
242 s22 = x2 * x2 + y2 * y2;
243 if (s11 > 1.0 && s12 > 1.0 && s21 > 1.0 && s22 > 1.0)
244 row[x] = (float) 0.0;
245 else if (s11 < 1.0 && s12 < 1.0 && s21 < 1.0 && s22 < 1.0)
246 row[x] = (float) (255.0 * (1.0 - (y1 + 0.5 / ay) * (y1 + 0.5 / ay) - (x1 + 0.5 / ax) * (x1 + 0.5 / ax)));
247 else
248 {
249 int n = 32, k, l;
250 double sum = 0.0, dx, dy;
251
252 dx = 1.0 / (n * ax);
253 dy = 1.0 / (n * ay);
254 for (k = 0; k < n; k++)
255 {
256 double xk = x1 + k * dx;
257
258 for (l = 0; l < n; l++)
259 {
260 double yl = y1 + l * dy;
261
262 if (xk * xk + yl * yl < 1.0)
263 sum += (1.0 - xk * xk - yl * yl);
264 }
265 }
266 row[x] = (float) (sum * 255.0 / (n * n));
267 }
268 }
269 im_put_rowf(row, im, y, 0, width);
270 }
271
272 fvector_free(row, 0);
273 return (im);
274 }
275
276 Imrect *imf_subpix_algebraic(int width, int height, double (*f) ( /* ??? */ ), void *data)
277 {
278 float *row;
279 Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
280 Imrect *im1 = im_alloc(height + 1, width + 1, (Imregion *) NULL, float_v);
281 int x, y;
282
283 row = fvector_alloc(0, width + 1);
284 for (y = 0; y < height + 1; y++)
285 {
286 for (x = 0; x < width + 1; x++)
287 row[x] = (float) f((double) x, (double) y, data);;
288 im_put_rowf(row, im1, y, 0, width + 1);
289 }
290
291 for (y = 0; y < height; y++)
292 {
293 for (x = 0; x < width; x++)
294 {
295 double s11 = IM_FLOAT(im1, y, x);
296 double s12 = IM_FLOAT(im1, y + 1, x);
297 double s21 = IM_FLOAT(im1, y, x + 1);
298 double s22 = IM_FLOAT(im1, y + 1, x + 1);
299
300 if (s11 > 0.0 && s12 > 0.0 && s21 > 0.0 && s22 > 0.0)
301 row[x] = (float) 0.0;
302 else if (s11 < 0.0 && s12 < 0.0 && s21 < 0.0 && s22 < 0.0)
303 row[x] = (float) 255.0;
304 else
305 {
306 int n = 16, k, l;
307 double sum = 0.0, dx, dy;
308
309 dx = 1.0 / n;
310 dy = 1.0 / n;
311 for (k = 0; k < n; k++)
312 {
313 double xk = x + k * dx;
314
315 for (l = 0; l < n; l++)
316 {
317 double yl = y + l * dy;
318
319 if (f(xk, yl, data) < 0.0)
320 sum += 1.0;
321 }
322 }
323 row[x] = (float) (sum * 255.0 / (n * n));
324 }
325 }
326 im_put_rowf(row, im, y, 0, width);
327 }
328
329 im_free(im1);
330 fvector_free(row, 0);
331 return (im);
332 }
333
334
335 static double cx0, cy0, cos0, sin0, ax0, ay0, ex0, ey0; /* static data! */
336
337 static double sellipse(double x, double y)
338 {
339 double x0, y0;
340
341 x -= cx0;
342 y -= cy0;
343 x0 = x * cos0 + y * sin0;
344 y0 = -x * sin0 + y * cos0;
345 x0 /= ax0;
346 y0 /= ay0;
347 x0 = fabs(x0);
348 y0 = fabs(y0);
349 return (pow(x0, ex0) + pow(y0, ey0) - 1.0);
350 }
351
352 Imrect *imf_subpix_sellipse(int width, int height, double cx, double cy, double theta, double ax, double ay, double ex, double ey)
353 {
354 cx0 = cx;
355 cy0 = cy;
356 ax0 = ax;
357 ay0 = ay;
358 ex0 = ex;
359 ey0 = ey;
360 cos0 = cos(theta);
361 sin0 = sin(theta);
362 return (imf_subpix_algebraic(width, height, sellipse, NULL));
363 }
364
365 Imrect *imf_delta(int width, int height, double cx, double cy)
366 {
367 Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
368 int x = (int) (cx + 0.5);
369 int y = (int) (cy + 0.5);
370
371 im_put_pixf(255.0, im, y, x);
372 return (im);
373 }
374
375 Imrect *imf_unif_noise(int width, int height, int dx, int dy, double a, double b)
376 {
377 float *row;
378 Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
379 int i, x, y;
380
381 row = fvector_alloc(0, width);
382 for (y = 0; y < height; y += dy)
383 {
384 for (x = 0; x < width; x += dx)
385 {
386 float g = (float) rand_unif(a, b);
387
388 for (i = 0; i < dx && x + i < width; i++)
389 row[x + i] = g;
390 }
391 for (i = 0; i < dy && y + i < height; i++)
392 im_put_rowf(row, im, y + i, 0, width);
393 }
394
395 fvector_free(row, 0);
396 return (im);
397 }
398
399 Imrect *imf_norm_noise(int width, int height, int dx, int dy, double a, double b)
400 {
401 float *row;
402 Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
403 int i, x, y;
404
405 row = fvector_alloc(0, width);
406 for (y = 0; y < height; y += dy)
407 {
408 for (x = 0; x < width; x += dx)
409 {
410 float g = (float) rand_normal(a, b);
411
412 for (i = 0; i < dx && x + i < width; i++)
413 row[x + i] = g;
414 }
415 for (i = 0; i < dy && y + i < height; i++)
416 im_put_rowf(row, im, y + i, 0, width);
417 }
418
419 fvector_free(row, 0);
420 return (im);
421 }
422
423 Imrect *im_corrupt(Imrect * im, int dx, int dy, double a, double b)
424 {
425 Imrect *noise = imf_unif_noise(im->width, im->height, dx, dy,
426 a, b);
427 Imrect *im1;
428
429 im1 = im_sum(im, noise);
430 im_free(noise);
431 return (im1);
432 }
433
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.