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_ptr.c,v $
37 * Date : $Date: 2005/01/23 19:10:21 $
38 * Version : $Revision: 1.5 $
39 * CVS Id : $Id: imgPrc_ptr.c,v 1.5 2005/01/23 19:10:21 paul Exp $
40 *
41 * Author : Legacy TINA
42 */
43
44 /**
45 * @file
46 * @brief Handing of pointer images; operations Vec2 and Mat2.
47 *
48 * Handling images of vec2's and mat2's
49 * Some image apply and combine functions are also defined here
50 *
51 * (shouldn't this file be split?)
52 *
53 */
54
55 #include "imgPrc_ptr.h"
56
57 #if HAVE_CONFIG_H
58 #include <config.h>
59 #endif
60
61 #include <math.h>
62 #include <tina/sys/sysDef.h>
63 #include <tina/sys/sysPro.h>
64 #include <tina/math/mathDef.h>
65 #include <tina/math/mathPro.h>
66 #include <tina/image/img_GenDef.h>
67 #include <tina/image/img_GenPro.h>
68 #include <tina/image/imgPrc_deriv.h>
69
70 /**
71 applying function to image
72 ptr -> void
73 **/
74
75 void im_ptr_apply(Imrect * im, void (*func) (), void *data)
76 {
77 int *row;
78 int lx, ux, ly, uy;
79 int i, j;
80
81 if (im == NULL)
82 return;
83
84 lx = im->region->lx;
85 ux = im->region->ux;
86 ly = im->region->ly;
87 uy = im->region->uy;
88 row = tvector_alloc(lx, ux, int);
89
90 for (i = ly; i < uy; ++i)
91 {
92 im_get_row(row, im, i, lx, ux);
93 for (j = lx; j < ux; ++j)
94 (*func) ((void *) row[j], data);
95 }
96
97 tvector_free(row, lx, int);
98 }
99
100 /**
101 applying function to image
102 ptr -> ptr
103 **/
104
105 Imrect *im_pp_apply(Imrect * im1, void *(*func) (), void *data)
106 {
107 Imrect *im2;
108 int *row1, *row2;
109 int lx, ux, ly, uy;
110 int i, j;
111
112 if (im1 == NULL)
113 return (NULL);
114
115 lx = im1->region->lx;
116 ux = im1->region->ux;
117 ly = im1->region->ly;
118 uy = im1->region->uy;
119 im2 = im_alloc(im1->height, im1->width, im1->region, ptr_v);
120 row1 = tvector_alloc(lx, ux, int);
121 row2 = tvector_alloc(lx, ux, int);
122
123 for (i = ly; i < uy; ++i)
124 {
125 im_get_row(row1, im1, i, lx, ux);
126 for (j = lx; j < ux; ++j)
127 row2[j] = (int) (*func) ((void *) row1[j], data);
128 im_put_row(row2, im2, i, lx, ux);
129 }
130
131 tvector_free(row1, lx, int);
132 tvector_free(row2, lx, int);
133
134 return (im2);
135 }
136
137 /**
138 applying function to image
139 ptr -> float
140 **/
141
142 Imrect *im_pf_apply(Imrect * im1, double (*func) (), void *data)
143 {
144 Imrect *im2;
145 int *row1;
146 float *row2;
147 int lx, ux, ly, uy;
148 int i, j;
149
150 if (im1 == NULL)
151 return (NULL);
152
153 lx = im1->region->lx;
154 ux = im1->region->ux;
155 ly = im1->region->ly;
156 uy = im1->region->uy;
157 im2 = im_alloc(im1->height, im1->width, im1->region, float_v);
158 row1 = tvector_alloc(lx, ux, int);
159 row2 = tvector_alloc(lx, ux, float);
160
161 for (i = ly; i < uy; ++i)
162 {
163 im_get_row(row1, im1, i, lx, ux);
164 for (j = lx; j < ux; ++j)
165 row2[j] = (float) ((*func) ((void *) row1[j], data));
166 im_put_rowf(row2, im2, i, lx, ux);
167 }
168
169 tvector_free(row1, lx, int);
170 tvector_free(row2, lx, float);
171
172 return (im2);
173 }
174
175 /**
176 combining two images
177 ptr ptr -> ptr
178 **/
179
180 Imrect *im_ppp_combine(Imrect * im1, Imrect * im2, void *(*func) (), void *data)
181 {
182 Imrect *im3;
183 Imregion *roi;
184 int *row1, *row2, *row3;
185 int lx, ux, ly, uy;
186 int width, height;
187 int i, j;
188
189 if (im1 == NULL || im2 == NULL)
190 return (NULL);
191
192 roi = roi_inter(im1->region, im2->region);
193 if (roi == NULL)
194 return (NULL);
195 lx = roi->lx;
196 ux = roi->ux;
197 ly = roi->ly;
198 uy = roi->uy;
199
200 width = MIN(im1->width, im2->width);
201 height = MIN(im1->height, im2->height);
202
203 im3 = im_alloc(height, width, roi, ptr_v);
204 row1 = tvector_alloc(lx, ux, int);
205 row2 = tvector_alloc(lx, ux, int);
206 row3 = tvector_alloc(lx, ux, int);
207
208 for (i = ly; i < uy; ++i)
209 {
210 im_get_row(row1, im1, i, lx, ux);
211 im_get_row(row2, im2, i, lx, ux);
212 for (j = lx; j < ux; ++j)
213 row3[j] = (int) (*func) ((void *) row1[j], (void *) row2[j], data);
214 im_put_row(row3, im3, i, lx, ux);
215 }
216
217 tvector_free(row1, lx, int);
218 tvector_free(row2, lx, int);
219 tvector_free(row3, lx, int);
220 rfree(roi);
221 return (im3);
222 }
223
224 /**
225 combining two images
226 float float -> ptr
227 **/
228
229 Imrect *im_ffp_combine(Imrect * im1, Imrect * im2, void *(*func) (), void *data)
230 {
231 Imrect *im3;
232 Imregion *roi;
233 float *row1, *row2;
234 int *row3;
235 int lx, ux, ly, uy;
236 int width, height;
237 int i, j;
238
239 if (im1 == NULL || im2 == NULL)
240 return (NULL);
241
242 roi = roi_inter(im1->region, im2->region);
243 if (roi == NULL)
244 return (NULL);
245 lx = roi->lx;
246 ux = roi->ux;
247 ly = roi->ly;
248 uy = roi->uy;
249
250 width = MIN(im1->width, im2->width);
251 height = MIN(im1->height, im2->height);
252
253 im3 = im_alloc(height, width, roi, ptr_v);
254 row1 = tvector_alloc(lx, ux, float);
255 row2 = tvector_alloc(lx, ux, float);
256 row3 = tvector_alloc(lx, ux, int);
257
258 for (i = ly; i < uy; ++i)
259 {
260 im_get_rowf(row1, im1, i, lx, ux);
261 im_get_rowf(row2, im2, i, lx, ux);
262 for (j = lx; j < ux; ++j)
263 row3[j] = (int) (*func) (row1[j], row2[j], data);
264 im_put_row(row3, im3, i, lx, ux);
265 }
266
267 tvector_free(row1, lx, float);
268 tvector_free(row2, lx, float);
269 tvector_free(row3, lx, int);
270 rfree(roi);
271 return (im3);
272 }
273
274 Imrect *im_fffp_combine(Imrect * im1, Imrect * im2, Imrect * im3, void *(*func) (), void *data)
275 {
276 Imrect *im4=NULL;
277 Imregion *roi=NULL;
278 float *row1=NULL, *row2=NULL, *row3=NULL;
279 int *row4=NULL;
280 int lx, ux, ly, uy;
281 int width, height;
282 int i, j;
283
284 if (im1 == NULL || im2 == NULL)
285 return (NULL);
286
287 roi = roi_inter(im1->region, im2->region);
288 if (roi == NULL)
289 return (NULL);
290 lx = roi->lx;
291 ux = roi->ux;
292 ly = roi->ly;
293 uy = roi->uy;
294
295 width = MIN(im1->width, im2->width);
296 height = MIN(im1->height, im2->height);
297
298 im3 = im_alloc(height, width, roi, ptr_v);
299 row1 = tvector_alloc(lx, ux, float);
300 row2 = tvector_alloc(lx, ux, float);
301 row3 = tvector_alloc(lx, ux, float);
302 row4 = tvector_alloc(lx, ux, int);
303
304 for (i = ly; i < uy; ++i)
305 {
306 im_get_rowf(row1, im1, i, lx, ux);
307 im_get_rowf(row2, im2, i, lx, ux);
308 im_get_rowf(row3, im3, i, lx, ux);
309 for (j = lx; j < ux; ++j)
310 row4[j] = (int) (*func) (row1[j], row2[j], row3[j], data);
311 im_put_row(row4, im4, i, lx, ux);
312 }
313
314 tvector_free(row1, lx, float);
315 tvector_free(row2, lx, float);
316 tvector_free(row3, lx, float);
317 tvector_free(row4, lx, int);
318 rfree(roi);
319 return (im4);
320 }
321
322
323 /**
324 combining two images
325 float ptr -> ptr
326 **/
327
328 Imrect *im_fpp_combine(Imrect * im1, Imrect * im2, void *(*func) (), void *data)
329 {
330 Imrect *im3;
331 Imregion *roi;
332 float *row1;
333 int *row2, *row3;
334 int lx, ux, ly, uy;
335 int width, height;
336 int i, j;
337
338 if (im1 == NULL || im2 == NULL)
339 return (NULL);
340
341 roi = roi_inter(im1->region, im2->region);
342 if (roi == NULL)
343 return (NULL);
344 lx = roi->lx;
345 ux = roi->ux;
346 ly = roi->ly;
347 uy = roi->uy;
348
349 width = MIN(im1->width, im2->width);
350 height = MIN(im1->height, im2->height);
351
352 im3 = im_alloc(height, width, roi, ptr_v);
353 row1 = tvector_alloc(lx, ux, float);
354 row2 = tvector_alloc(lx, ux, int);
355 row3 = tvector_alloc(lx, ux, int);
356
357 for (i = ly; i < uy; ++i)
358 {
359 im_get_rowf(row1, im1, i, lx, ux);
360 im_get_row(row2, im2, i, lx, ux);
361 for (j = lx; j < ux; ++j)
362 row3[j] = (int) (*func) (row1[j], (void *) row2[j], data);
363 im_put_row(row3, im3, i, lx, ux);
364 }
365
366 tvector_free(row1, lx, float);
367 tvector_free(row2, lx, int);
368 tvector_free(row3, lx, int);
369 rfree(roi);
370 return (im3);
371 }
372
373 /**
374 combining two images
375 ptr ptr -> float
376 **/
377
378 Imrect *im_ppf_combine(Imrect * im1, Imrect * im2, double (*func) (), void *data)
379 {
380 Imrect *im3;
381 Imregion *roi;
382 int *row1, *row2;
383 float *row3;
384 int lx, ux, ly, uy;
385 int width, height;
386 int i, j;
387
388 if (im1 == NULL || im2 == NULL)
389 return (NULL);
390
391 roi = roi_inter(im1->region, im2->region);
392 if (roi == NULL)
393 return (NULL);
394 lx = roi->lx;
395 ux = roi->ux;
396 ly = roi->ly;
397 uy = roi->uy;
398
399 width = MIN(im1->width, im2->width);
400 height = MIN(im1->height, im2->height);
401
402 im3 = im_alloc(height, width, roi, ptr_v);
403 row1 = tvector_alloc(lx, ux, int);
404 row2 = tvector_alloc(lx, ux, int);
405 row3 = tvector_alloc(lx, ux, float);
406
407 for (i = ly; i < uy; ++i)
408 {
409 im_get_row(row1, im1, i, lx, ux);
410 im_get_row(row2, im2, i, lx, ux);
411 for (j = lx; j < ux; ++j)
412 row3[j] = (float) ((*func) ((void *) row1[j], (void *) row2[j], data));
413 im_put_rowf(row3, im3, i, lx, ux);
414 }
415
416 tvector_free(row1, lx, int);
417 tvector_free(row2, lx, int);
418 tvector_free(row3, lx, float);
419 rfree(roi);
420 return (im3);
421 }
422
423 /* free image of vec2's or mat2's */
424
425 void im_vec2_free(Imrect * im)
426 {
427 im_ptr_apply(im, rfree, NULL);
428 im_free(im);
429 }
430
431 void im_vec3_free(Imrect * im)
432 {
433 im_ptr_apply(im, rfree, NULL);
434 im_free(im);
435 }
436
437 void im_mat2_free(Imrect * im)
438 {
439 im_ptr_apply(im, rfree, NULL);
440 im_free(im);
441 }
442
443 /* make image of ptr's to vec2's x-coord from im1 y-coord from im2 */
444
445 static void *pvec2_make(double x, double y)
446 {
447 return (vec2_make(vec2(x, y)));
448 }
449
450 Imrect *im_vec2(Imrect * im1, Imrect * im2)
451 {
452 return (im_ffp_combine(im1, im2, pvec2_make, NULL));
453 }
454
455 /* add two vec2 images */
456
457 static void *pvec2_sum(Vec2 * u, Vec2 * v)
458 {
459 return (vec2_make(vec2_sum(*u, *v)));
460 }
461
462 Imrect *im_vec2_sum(Imrect * u, Imrect * v)
463 {
464 return (im_ppp_combine(u, v, pvec2_sum, NULL));
465 }
466
467 /* subtract two vec2 images */
468
469 static void *pvec2_diff(Vec2 * u, Vec2 * v)
470 {
471 return (vec2_make(vec2_diff(*u, *v)));
472 }
473
474 Imrect *im_vec2_diff(Imrect * u, Imrect * v)
475 {
476 return (im_ppp_combine(u, v, pvec2_diff, NULL));
477 }
478
479 /* dot product of two vec2 images */
480
481 static double pvec2_dot(Vec2 * u, Vec2 * v)
482 {
483 return (vec2_dot(*u, *v));
484 }
485
486 Imrect *im_vec2_dot(Imrect * u, Imrect * v)
487 {
488 return (im_ppf_combine(u, v, pvec2_dot, NULL));
489 }
490
491 /* cross product of two vec2 images */
492
493 static double pvec2_cross(Vec2 * u, Vec2 * v)
494 {
495 return (vec2_cross(*u, *v));
496 }
497
498 Imrect *im_vec2_cross(Imrect * u, Imrect * v)
499 {
500 return (im_ppf_combine(u, v, pvec2_cross, NULL));
501 }
502
503 /* product of mat2 image and vec2 image */
504
505 static void *pmat2_vprod(Mat2 * m, Vec2 * v)
506 {
507 return (vec2_make(mat2_vprod(*m, *v)));
508 }
509
510 Imrect *im_mat2_vprod(Imrect * m, Imrect * v)
511 {
512 return (im_ppp_combine(m, v, pmat2_vprod, NULL));
513 }
514
515 /* scalar product of vec2 image, mat2 image and vec2 image */
516
517 Imrect *im_mat2_sprod(Imrect * u, Imrect * m, Imrect * v)
518 {
519 Imrect *mv = im_mat2_vprod(m, v);
520 Imrect *umv;
521
522 umv = im_vec2_dot(u, mv);
523 im_ptr_apply(mv, rfree, NULL);
524 return (umv);
525 }
526
527 /* invert mat2 image */
528
529 static void *pmat2_inverse(Mat2 * m)
530 {
531 return (mat2_make(mat2_inverse(*m)));
532 }
533
534 Imrect *im_mat2_inverse(Imrect * m)
535 {
536 return (im_pp_apply(m, pmat2_inverse, NULL));
537 }
538
539 /* make image of mat2's from images of column vectors */
540
541 static void *pmat2_of_cols(Vec2 * cx, Vec2 * cy)
542 {
543 return (mat2_make(mat2_of_cols(*cx, *cy)));
544 }
545
546 Imrect *im_mat2_of_cols(Imrect * cx, Imrect * cy)
547 {
548 return (im_ppp_combine(cx, cy, pmat2_of_cols, NULL));
549 }
550
551 /* make image of mat2's from images of row vectors */
552
553 static void *pmat2_of_rows(Vec2 * rx, Vec2 * ry)
554 {
555 return (mat2_make(mat2_of_rows(*rx, *ry)));
556 }
557
558 Imrect *im_mat2_of_rows(Imrect * rx, Imrect * ry)
559 {
560 return (im_ppp_combine(rx, ry, pmat2_of_rows, NULL));
561 }
562
563 /* image of gradients */
564
565 Imrect *im_vec2_grad(Imrect * im)
566 {
567 Imrect *imx = imf_diffx(im);
568 Imrect *imy = imf_diffy(im);
569 Imrect *g;
570
571 g = im_vec2(imx, imy);
572 im_free(imx);
573 im_free(imy);
574 return (g);
575 }
576
577 /* setup images of gradients and hessians */
578
579 void im_mat2_grad_hessian(Imrect * im, Imrect ** g, Imrect ** h)
580 {
581 Imrect *imx = imf_diffx(im);
582 Imrect *imy = imf_diffy(im);
583 Imrect *gx;
584 Imrect *gy;
585
586 gx = im_vec2_grad(imx);
587 gy = im_vec2_grad(imy);
588 *g = im_vec2(imx, imy);
589 im_free(imx);
590 im_free(imy);
591 *h = im_mat2_of_cols(gx, gy);
592 im_vec2_free(gx);
593 im_vec2_free(gy);
594 }
595
596 /* return image of hessians */
597
598 Imrect *im_mat2_hessian(Imrect * im)
599 {
600 Imrect *g;
601 Imrect *h;
602
603 im_mat2_grad_hessian(im, &g, &h);
604 im_vec2_free(g);
605 return (h);
606 }
607
608 /* image of determinants */
609
610 static double pmat2_det(Mat2 * m)
611 {
612 return (mat2_det(*m));
613 }
614
615 Imrect *im_mat2_det(Imrect * m)
616 {
617 return (im_pf_apply(m, pmat2_det, NULL));
618 }
619
620 /* components of vec2 and mat2 images */
621
622 static double pvec2_x(Vec2 * v)
623 {
624 return (vec2_x(*v));
625 }
626
627 Imrect *im_vec2_x(Imrect * v)
628 {
629 return (im_pf_apply(v, pvec2_x, NULL));
630 }
631
632 static double pvec2_y(Vec2 * v)
633 {
634 return (vec2_y(*v));
635 }
636
637 Imrect *im_vec2_y(Imrect * v)
638 {
639 return (im_pf_apply(v, pvec2_y, NULL));
640 }
641
642 static double pmat2_xx(Mat2 * m)
643 {
644 return (mat2_xx(*m));
645 }
646
647 Imrect *im_mat2_xx(Imrect * m)
648 {
649 return (im_pf_apply(m, pmat2_xx, NULL));
650 }
651
652 static double pmat2_xy(Mat2 * m)
653 {
654 return (mat2_xy(*m));
655 }
656
657 Imrect *im_mat2_xy(Imrect * m)
658 {
659 return (im_pf_apply(m, pmat2_xy, NULL));
660 }
661
662 static double pmat2_yx(Mat2 * m)
663 {
664 return (mat2_yx(*m));
665 }
666
667 Imrect *im_mat2_yx(Imrect * m)
668 {
669 return (im_pf_apply(m, pmat2_yx, NULL));
670 }
671
672 static double pmat2_yy(Mat2 * m)
673 {
674 return (mat2_yy(*m));
675 }
676
677 Imrect *im_mat2_yy(Imrect * m)
678 {
679 return (im_pf_apply(m, pmat2_yy, NULL));
680 }
681
682 /* make image of ptr's to vec3's coords from im1 im2 im3 */
683 /* UNUSED
684 static void *pvec3_make(double x, double y, double z)
685 {
686 return (vec2_make(vec2(x, y)));
687 }
688 */
689
690 Imrect *im_vec3(Imrect * im1, Imrect * im2, Imrect * im3)
691 {
692 return (im_fffp_combine(im1, im2, im3, pvec2_make, NULL));
693 }
694
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.