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_surf.c,v $
37 * Date : $Date: 2003/09/22 16:09:02 $
38 * Version : $Revision: 1.5 $
39 * CVS Id : $Id: imgPrc_surf.c,v 1.5 2003/09/22 16:09:02 tony Exp $
40 *
41 * Author : Legacy TINA
42 */
43
44 /**
45 * @file
46 * @brief Generates a terrain map for visualisation from a grey level image using z-bufferring.
47 *
48 */
49
50 #include "imgPrc_surf.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
64 void terrain_data_free(Terrain_data * surf)
65 {
66 if (surf == NULL)
67 return;
68
69 narray_free((char **) surf->data, 0, surf->m, 0, surf->n, sizeof(Vec3));
70 carray_free(surf->mask, 0, surf->m, 0, surf->n);
71 rfree((void *) surf);
72 }
73
74 Terrain_data *terrain_alloc(int type, int m, int n)
75 {
76 Terrain_data *surf;
77
78 surf = ts_ralloc(Terrain_data);
79 #ifdef TSTRUCT
80 surf->data = ts_narray_alloc(0, 0, m, n, Vec3);
81 #else
82 surf->data = (Vec3 **) narray_alloc(0, 0, m, n, sizeof(Vec3));
83 #endif
84 surf->mask = carray_alloc(0, 0, m, n);
85 surf->type = type;
86 surf->m = m;
87 surf->n = n;
88 return (surf);
89 }
90
91 Terrain_data *terrain_copy(Terrain_data * surf)
92 {
93 Terrain_data *copy;
94 int m, n, i, j;
95
96 if (surf == NULL)
97 return (NULL);
98
99 m = surf->m;
100 n = surf->n;
101
102 copy = terrain_alloc(surf->type, m, n);
103
104 for (i = 0; i < m; ++i)
105 for (j = 0; j < n; ++j)
106 {
107 copy->mask[i][j] = surf->mask[i][j];
108 copy->data[i][j] = surf->data[i][j];
109 }
110
111 return (copy);
112 }
113
114 Terrain_data *terrain_make(Imregion * region, Imrect * mask, int samplex, int sampley)
115 {
116 Terrain_data *surf;
117 int lx, ly, ux, uy;
118 int m, n; /* mrows and n columns */
119 int i, j, r, c;
120 float x, y;
121
122 if (region == NULL)
123 return (NULL);
124 if (samplex < 1)
125 samplex = 1;
126 if (sampley < 1)
127 sampley = 1;
128
129 lx = region->lx;
130 ly = region->ly;
131 ux = region->ux;
132 uy = region->uy;
133
134 m = (uy - ly) / sampley;
135 n = (ux - lx) / samplex;
136
137 uy--;
138 ux--;
139 surf = terrain_alloc(IMAGE, m + 1, n + 1);
140
141 for (i = 0; i < m; i++)
142 {
143 r = ly + i * sampley;
144 y = (float) r;
145 for (j = 0; j < n; ++j)
146 {
147 c = lx + j * samplex;
148 x = (float) c;
149 surf->data[i][j] = vec3(x, y, 0.0);
150 if (mask == NULL || im_get_pix(mask, r, c))
151 surf->mask[i][j] = 1;
152 }
153 surf->data[i][j] = vec3((float) ux, y, 0.0);
154 if (mask == NULL || im_get_pix(mask, r, ux))
155 surf->mask[i][j] = 1;
156 }
157
158 for (j = 0; j < n; ++j)
159 {
160 c = lx + j * samplex;
161 x = (float) c;
162 surf->data[i][j] = vec3(x, (float) uy, 0.0);
163 if (mask == NULL || im_get_pix(mask, uy, c))
164 surf->mask[i][j] = 1;
165 }
166
167 surf->data[i][j] = vec3((float) ux, (float) uy, 0.0);
168 if (mask == NULL || im_get_pix(mask, uy, ux))
169 surf->mask[i][j] = 1;
170
171 return (surf);
172 }
173
174 Terrain_data *im_surface(Imrect * im, Imrect * mask, int samplex, int sampley, double scale)
175 {
176 Terrain_data *surf;
177 int lx, ly, ux, uy;
178 int m, n; /* mrows and n columns */
179 int i, j, r, c;
180 float x, y;
181
182 if (im == NULL || im->region == NULL)
183 return (NULL);
184 if (samplex < 1)
185 samplex = 1;
186 if (sampley < 1)
187 sampley = 1;
188
189 lx = im->region->lx;
190 ly = im->region->ly;
191 ux = im->region->ux;
192 uy = im->region->uy;
193
194 m = (uy - ly) / sampley;
195 n = (ux - lx) / samplex;
196
197 uy--;
198 ux--;
199 surf = terrain_alloc(IMAGE, m + 1, n + 1);
200
201 for (i = 0; i < m; i++)
202 {
203 r = ly + i * sampley;
204 y = (float) r;
205 for (j = 0; j < n; ++j)
206 {
207 c = lx + j * samplex;
208 x = (float) c;
209 if (mask == NULL || im_get_pix(mask, r, c))
210 {
211 surf->data[i][j] = vec3(x, y, im_get_pixf(im, r, c) * scale);
212 surf->mask[i][j] = 1;
213 } else
214 surf->data[i][j] = vec3(x, y, 0.0);
215 }
216 if (mask == NULL || im_get_pix(mask, r, ux))
217 {
218 surf->data[i][j] = vec3((float) ux, y, im_get_pixf(im, r, ux) * scale);
219 surf->mask[i][j] = 1;
220 } else
221 surf->data[i][j] = vec3((float) ux, y, 0.0);
222 }
223
224 for (j = 0; j < n; ++j)
225 {
226 c = lx + j * samplex;
227 x = (float) c;
228 if (mask == NULL || im_get_pix(mask, uy, c))
229 {
230 surf->data[i][j] = vec3(x, (float) uy, im_get_pixf(im, uy, c) * scale);
231 surf->mask[i][j] = 1;
232 } else
233 surf->data[i][j] = vec3(x, (float) uy, 0.0);
234 }
235 if (mask == NULL || im_get_pix(mask, uy, ux))
236 {
237 surf->data[i][j] = vec3((float) ux, (float) uy, im_get_pixf(im, uy, ux) * scale);
238 surf->mask[i][j] = 1;
239 } else
240 surf->data[i][j] = vec3((float) ux, (float) uy, 0.0);
241 return (surf);
242 }
243
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.