~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/vision/improc/im_create.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**@(#)
  2 **/
  3 /**
  4 im_create.c:
  5 creating simple images
  6 **/
  7 
  8 #include <math.h>
  9 #include <tina/sys.h>
 10 #include <tina/sysfuncs.h>
 11 #include <tina/math.h>
 12 #include <tina/mathfuncs.h>
 13 #include <tina/vision.h>
 14 #include <tina/visionfuncs.h>
 15 
 16 Imrect *imf_checquer(int width, int height, int dx, int dy)
 17 {
 18     float  *row1, *row2;
 19     Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
 20     int     i, j;
 21 
 22     row1 = fvector_alloc(0, width);
 23     for (i = 0, j = 0; i < width; i++, j++)
 24     {
 25         if (j < dx)
 26             row1[i] = (float) 0.0;
 27         else if (j < 2 * dx)
 28             row1[i] = (float) 255.0;
 29 
 30         if (j == 2 * dx - 1)
 31             j = -1;
 32     }
 33 
 34     row2 = fvector_alloc(0, width);
 35     for (i = 0, j = 0; i < width; i++, j++)
 36     {
 37         if (j < dx)
 38             row2[i] = (float) 255.0;
 39         else if (j < 2 * dx)
 40             row2[i] = (float) 0.0;
 41 
 42         if (j == 2 * dx - 1)
 43             j = -1;
 44     }
 45 
 46     for (i = 0, j = 0; i < height; i++, j++)
 47     {
 48         if (j < dy)
 49             im_put_rowf(row1, im, i, 0, width);
 50         else if (j < 2 * dx)
 51             im_put_rowf(row2, im, i, 0, width);
 52 
 53         if (j == 2 * dy - 1)
 54             j = -1;
 55     }
 56     fvector_free((void *) row1, 0);
 57     fvector_free((void *) row2, 0);
 58     return (im);
 59 }
 60 
 61 Imrect *imf_rect(int width, int height, int lx, int ly, int ux, int uy)
 62 {
 63     float  *row1, *row2;
 64     Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
 65     int     i;
 66 
 67     row1 = fvector_alloc(0, width);
 68     for (i = 0; i < width; i++)
 69     {
 70         if (i < lx)
 71             row1[i] = (float) 0.0;
 72         else if (i < ux)
 73             row1[i] = (float) 255.0 *(i-lx)/(double)(ux-lx);
 74         else
 75             row1[i] = (float) 0.0;
 76     }
 77     row2 = fvector_alloc(0, width);
 78 
 79     for (i = 0; i < height; i++)
 80     {
 81         if (i < ly)
 82             im_put_rowf(row2, im, i, 0, width);
 83         else if (i < uy)
 84             im_put_rowf(row1, im, i, 0, width);
 85         else
 86             im_put_rowf(row2, im, i, 0, width);
 87     }
 88     fvector_free((void *) row1, 0);
 89     fvector_free((void *) row2, 0);
 90     return (im);
 91 }
 92 
 93 Imrect *imf_ellipse(int width, int height, double cx, double cy, double ax, double ay)
 94 {
 95     float  *row;
 96     Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
 97     int     x, y;
 98 
 99     row = fvector_alloc(0, width);
100     for (y = 0; y < height; y++)
101     {
102         for (x = 0; x < width; x++)
103         {
104             float   x1 = (float) ((x - cx) / ax);
105             float   y1 = (float) ((y - cy) / ay);
106 
107             if (x1 * x1 + y1 * y1 > 1.0)
108                 row[x] = (float) 0.0;
109             else
110                 row[x] = (float) 255.0;
111         }
112         im_put_rowf(row, im, y, 0, width);
113     }
114 
115     fvector_free((void *) row, 0);
116     return (im);
117 }
118 
119 Imrect *imf_subpix_ellipse(int width, int height, double cx, double cy, double ax, double ay)
120 {
121     float  *row;
122     Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
123     int     x, y;
124 
125     row = fvector_alloc(0, width);
126     for (y = 0; y < height; y++)
127     {
128         for (x = 0; x < width; x++)
129         {
130             double  x1 = (x - cx) / ax;
131             double  y1 = (y - cy) / ay;
132             double  x2 = (x - cx + 1) / ax;
133             double  y2 = (y - cy + 1) / ay;
134             double  s11, s12, s21, s22;
135 
136             s11 = x1 * x1 + y1 * y1;
137             s21 = x2 * x2 + y1 * y1;
138             s12 = x1 * x1 + y2 * y2;
139             s22 = x2 * x2 + y2 * y2;
140             if (s11 > 1.0 && s12 > 1.0 && s21 > 1.0 && s22 > 1.0)
141                 row[x] = (float) 0.0;
142             else if (s11 < 1.0 && s12 < 1.0 && s21 < 1.0 && s22 < 1.0)
143                 row[x] = (float) 255.0;
144             else
145             {
146                 int     n = 32, k, l;
147                 double  sum = 0.0, dx, dy;
148 
149                 dx = 1.0 / (n * ax);
150                 dy = 1.0 / (n * ay);
151                 for (k = 0; k < n; k++)
152                 {
153                     double  xk = x1 + k * dx;
154 
155                     for (l = 0; l < n; l++)
156                     {
157                         double  yl = y1 + l * dy;
158 
159                         if (xk * xk + yl * yl < 1.0)
160                             sum += 1.0;
161                     }
162                 }
163                 row[x] = (float) (sum * 255.0 / (n * n));
164             }
165         }
166         im_put_rowf(row, im, y, 0, width);
167     }
168 
169     fvector_free((void *) row, 0);
170     return (im);
171 }
172 
173 Imrect *imf_subpix_ellipsoid(int width, int height, double cx, double cy, double ax, double ay)
174 {
175     float  *row;
176     Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
177     int     x, y;
178 
179     row = fvector_alloc(0, width);
180     for (y = 0; y < height; y++)
181     {
182         for (x = 0; x < width; x++)
183         {
184             double  x1 = (x - cx) / ax;
185             double  y1 = (y - cy) / ay;
186             double  x2 = (x - cx + 1) / ax;
187             double  y2 = (y - cy + 1) / ay;
188             double  s11, s12, s21, s22;
189 
190             s11 = x1 * x1 + y1 * y1;
191             s21 = x2 * x2 + y1 * y1;
192             s12 = x1 * x1 + y2 * y2;
193             s22 = x2 * x2 + y2 * y2;
194             if (s11 > 1.0 && s12 > 1.0 && s21 > 1.0 && s22 > 1.0)
195                 row[x] = (float) 0.0;
196             else if (s11 < 1.0 && s12 < 1.0 && s21 < 1.0 && s22 < 1.0)
197                 row[x] = (float) (255.0*(1.0-(y1 +0.5/ay)*(y1 +0.5/ay)-(x1 +0.5/ax)*(x1 +0.5/ax)));
198             else
199             {
200                 int     n = 32, k, l;
201                 double  sum = 0.0, dx, dy;
202 
203                 dx = 1.0 / (n * ax);
204                 dy = 1.0 / (n * ay);
205                 for (k = 0; k < n; k++)
206                 {
207                     double  xk = x1 + k * dx;
208 
209                     for (l = 0; l < n; l++)
210                     {
211                         double  yl = y1 + l * dy;
212 
213                         if (xk * xk + yl * yl < 1.0)
214                             sum += (1.0-xk * xk - yl * yl);
215                     }
216                 }
217                 row[x] = (float) (sum * 255.0 / (n * n));
218             }
219         }
220         im_put_rowf(row, im, y, 0, width);
221     }
222 
223     fvector_free((void *) row, 0);
224     return (im);
225 }
226 
227 Imrect *imf_subpix_algebraic(int width, int height, double (*f) ( /* ??? */ ), void *data)
228 {
229     float  *row;
230     Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
231     Imrect *im1 = im_alloc(height + 1, width + 1, (Imregion *) NULL, float_v);
232     int     x, y;
233 
234     row = fvector_alloc(0, width + 1);
235     for (y = 0; y < height + 1; y++)
236     {
237         for (x = 0; x < width + 1; x++)
238             row[x] = (float) f((double) x, (double) y, data);;
239         im_put_rowf(row, im1, y, 0, width + 1);
240     }
241 
242     for (y = 0; y < height; y++)
243     {
244         for (x = 0; x < width; x++)
245         {
246             double  s11 = IM_FLOAT(im1, y, x);
247             double  s12 = IM_FLOAT(im1, y + 1, x);
248             double  s21 = IM_FLOAT(im1, y, x + 1);
249             double  s22 = IM_FLOAT(im1, y + 1, x + 1);
250 
251             if (s11 > 0.0 && s12 > 0.0 && s21 > 0.0 && s22 > 0.0)
252                 row[x] = (float) 0.0;
253             else if (s11 < 0.0 && s12 < 0.0 && s21 < 0.0 && s22 < 0.0)
254                 row[x] = (float) 255.0;
255             else
256             {
257                 int     n = 16, k, l;
258                 double  sum = 0.0, dx, dy;
259 
260                 dx = 1.0 / n;
261                 dy = 1.0 / n;
262                 for (k = 0; k < n; k++)
263                 {
264                     double  xk = x + k * dx;
265 
266                     for (l = 0; l < n; l++)
267                     {
268                         double  yl = y + l * dy;
269 
270                         if (f(xk, yl, data) < 0.0)
271                             sum += 1.0;
272                     }
273                 }
274                 row[x] = (float) (sum * 255.0 / (n * n));
275             }
276         }
277         im_put_rowf(row, im, y, 0, width);
278     }
279 
280     im_free(im1);
281     fvector_free((void *) row, 0);
282     return (im);
283 }
284 
285 static double cx0, cy0, cos0, sin0, ax0, ay0, ex0, ey0;
286 
287 static double sellipse(double x, double y)
288 {
289     double  x0, y0;
290 
291     x -= cx0;
292     y -= cy0;
293     x0 = x * cos0 + y * sin0;
294     y0 = -x * sin0 + y * cos0;
295     x0 /= ax0;
296     y0 /= ay0;
297     x0 = fabs(x0);
298     y0 = fabs(y0);
299     return (pow(x0, ex0) + pow(y0, ey0) - 1.0);
300 }
301 
302 Imrect *imf_subpix_sellipse(int width, int height, double cx, double cy, double theta, double ax, double ay, double ex, double ey)
303 {
304     cx0 = cx;
305     cy0 = cy;
306     ax0 = ax;
307     ay0 = ay;
308     ex0 = ex;
309     ey0 = ey;
310     cos0 = cos(theta);
311     sin0 = sin(theta);
312     return (imf_subpix_algebraic(width, height, sellipse, NULL));
313 }
314 
315 Imrect *imf_delta(int width, int height, double cx, double cy)
316 {
317     Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
318     int     x = (int) (cx + 0.5);
319     int     y = (int) (cy + 0.5);
320 
321     im_put_pixf(255.0, im, y, x);
322     return (im);
323 }
324 
325 Imrect *imf_unif_noise(int width, int height, int dx, int dy, double a, double b)
326 {
327     float  *row;
328     Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
329     int     i, x, y;
330 
331     row = fvector_alloc(0, width);
332     for (y = 0; y < height; y += dy)
333     {
334         for (x = 0; x < width; x += dx)
335         {
336             float   g = (float) rand_unif(a, b);
337 
338             for (i = 0; i < dx && x + i < width; i++)
339                 row[x + i] = g;
340         }
341         for (i = 0; i < dy && y + i < height; i++)
342             im_put_rowf(row, im, y + i, 0, width);
343     }
344 
345     fvector_free((void *) row, 0);
346     return (im);
347 }
348 
349 Imrect *imf_norm_noise(int width, int height, int dx, int dy, double a, double b)
350 {
351     float  *row;
352     Imrect *im = im_alloc(height, width, (Imregion *) NULL, float_v);
353     int     i, x, y;
354 
355     row = fvector_alloc(0, width);
356     for (y = 0; y < height; y += dy)
357     {
358         for (x = 0; x < width; x += dx)
359         {
360             float   g = (float) rand_normal(a, b);
361 
362             for (i = 0; i < dx && x + i < width; i++)
363                 row[x + i] = g;
364         }
365         for (i = 0; i < dy && y + i < height; i++)
366             im_put_rowf(row, im, y + i, 0, width);
367     }
368 
369     fvector_free((void *) row, 0);
370     return (im);
371 }
372 
373 Imrect *im_corrupt(Imrect * im, int dx, int dy, double a, double b)
374 {
375     Imrect *noise = imf_unif_noise(im->width, im->height, dx, dy,
376                                    a, b);
377     Imrect *im1;
378 
379     im1 = im_sum(im, noise);
380     im_free(noise);
381     return (im1);
382 }
383 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.