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/imgGen_region.c,v $
37 * Date : $Date: 2004/08/05 14:32:44 $
38 * Version : $Revision: 1.7 $
39 * CVS Id : $Id: imgGen_region.c,v 1.7 2004/08/05 14:32:44 neil Exp $
40 */
41 /**
42 * @file
43 * @brief Allocation and manipulation of Imregion - structure for rectangular regions of interest.
44 *
45 * Imregion is:
46 * int lx, ly lower extents of the region of interest.
47 * int ux, uy upper extents of the region of interest.
48 */
49
50 #include "imgGen_region.h"
51
52 #if HAVE_CONFIG_H
53 #include <config.h>
54 #endif
55
56 #include <stdio.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
63
64 /**
65 * @brief Allocte a new imregion structure.
66 * @param lx Written in roi->lx field (lower array variable for image rows).
67 * @param ly Written in roi->ly field (lower array variable for image columns).
68 * @param ux Written in roi->ux field (upper array variable for image rows).
69 * @param uy Written in roi->uy field (upper array variable for image columns).
70 * @return roi Pointer to the newly allocated imregion structure.
71 *
72 * Allocate a new imregion structure, and write the input arguments into the relevant fields.
73 */
74 Imregion *roi_alloc(int lx, int ly, int ux, int uy)
75 {
76 Imregion *roi = ts_ralloc(Imregion);
77
78 roi->lx = lx;
79 roi->ly = ly;
80 roi->ux = ux;
81 roi->uy = uy;
82 return (roi);
83 }
84
85
86 /**
87 * @brief Make a copy of an imregion structure, allocating storage for the copy.
88 * @param roi Pointer to the imregion structure to be copied.
89 * @return copy Pointer to the newly allocated copy of the input region.
90 *
91 * Allocate the storage for a new imregion structure, copy in the fields
92 * from the input imregion structure, then return a pointer to this copy.
93 */
94 Imregion *roi_copy(Imregion * roi)
95 {
96 Imregion *copy;
97
98 if (roi == NULL)
99 return (NULL);
100
101 copy = ts_ralloc(Imregion);
102
103 copy->lx = roi->lx;
104 copy->ly = roi->ly;
105 copy->ux = roi->ux;
106 copy->uy = roi->uy;
107 return (copy);
108 }
109
110
111 /**
112 * @brief Copy the fields from one imregion structure into another.
113 * @param roi Pointer to the imregion structure acting as a source of data.
114 * @param copy Pointer to the imregion structure into which data is copied.
115 * @return void
116 *
117 * Copy the values for lx, ux, ly and uy from one imregion structure into another:
118 * both must already be allocated.
119 */
120 void roi_update(Imregion * roi, Imregion * copy)
121 {
122 if (roi == NULL || copy == NULL)
123 return;
124
125 copy->lx = roi->lx;
126 copy->ly = roi->ly;
127 copy->ux = roi->ux;
128 copy->uy = roi->uy;
129 }
130
131
132 /**
133 * @brief Allocate a new imregion structure covering the intersection of two existing imregion structures.
134 * @param r1 Pointer to an imregion structure.
135 * @param r2 Pointer to an imregion structure.
136 * @return (roi_alloc(lx, ly, ux, uy)) Pointer to newly allocated imregion structure covering the intersection of the input regions.
137 *
138 * Take two imregion structures and find their intersection: allocate a new imregion structure
139 * covering this intersection, and return the pointer to it.
140 */
141 Imregion *roi_inter(Imregion * r1, Imregion * r2)
142 {
143 int lx, ly, ux, uy;
144
145 if (r1 == NULL || r2 == NULL)
146 return (NULL);
147
148 lx = MAX(r1->lx, r2->lx);
149 ly = MAX(r1->ly, r2->ly);
150 ux = MIN(r1->ux, r2->ux);
151 uy = MIN(r1->uy, r2->uy);
152
153 if (lx >= ux || ly >= uy)
154 return (NULL);
155 return (roi_alloc(lx, ly, ux, uy));
156 }
157
158
159 /**
160 * @brief Allocate a new imregion structure that fully contains two existing imregion structures.
161 * @param r1 Pointer to an imregion structure.
162 * @param r2 Pointer to an imregion structure.
163 * @return (roi_alloc(lx, ly, ux, uy)) Pointer to newly allocated imregion structure fully covering the input regions.
164 *
165 * Take two imregion structure and find the region that fully contains them both: allocate a new imregion
166 * structure covering thsi region, and return the pointer to it.
167 */
168 Imregion *roi_outer(Imregion * r1, Imregion * r2)
169 {
170 int lx, ly, ux, uy;
171
172 if (r1 == NULL || r2 == NULL)
173 return (NULL);
174
175 lx = MIN(r1->lx, r2->lx);
176 ly = MIN(r1->ly, r2->ly);
177 ux = MAX(r1->ux, r2->ux);
178 uy = MAX(r1->uy, r2->uy);
179
180 if (lx >= ux || ly >= uy)
181 return (NULL);
182 return (roi_alloc(lx, ly, ux, uy));
183 }
184
185
186 /**
187 * @brief Check if a point lies within an imregion structure.
188 * @param region The imregion structure to be tested.
189 * @param x x-coordinate of the point to be tested.
190 * @param y y-coordinate of teh point to be tested.
191 * @return 0/1 The (binary) test result.
192 *
193 * Check whether the point defined by x,y lies within the region. If it does, return 1:
194 * if not, return 0.
195 */
196 int roi_inregion(Imregion * region, int x, int y)
197 {
198 if (region == NULL)
199 return (0);
200
201 return (x >= region->lx && x < region->ux && y >= region->ly
202 && y < region->uy);
203 }
204
205
206 /**
207 * @brief Fill in the fields of a region of interest.
208 * @param roi The imregion structure to have its fields filled.
209 * @param lx Written in roi->lx field (lower array variable for image rows).
210 * @param ly Written in roi->ly field (lower array variable for image columns).
211 * @param ux Written in roi->ux field (upper array variable for image rows).
212 * @param uy Written in roi->uy field (upper array variable for image columns).
213 * @return void
214 *
215 * Fill in the fields of a region of interest.
216 */
217 void roi_fill(Imregion * roi, int lx, int ly, int ux, int uy)
218 {
219 roi->lx = lx;
220 roi->ly = ly;
221 roi->ux = ux;
222 roi->uy = uy;
223 }
224
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.