1 /**********
2 *
3 * This file is part of the TINA Open Source Image Analysis Environment
4 * henceforth known as TINA
5 *
6 * TINA is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation.
9 *
10 * TINA is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with TINA; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 **********
20 *
21 * Program : TINA
22 * File : $Source: /home/tina/cvs/tina-libs/tina/medical/medSroi_io.c,v $
23 * Date : $Date: 2004/03/10 14:56:22 $
24 * Version : $Revision: 1.7 $
25 * CVS Id : $Id: medSroi_io.c,v 1.7 2004/03/10 14:56:22 neil Exp $
26 *
27 * Author : Legacy TINA
28 *
29 */
30 /**
31 * @file
32 * @brief Functions for reading and writing SROI and ASM model data
33 * Can only read one file at a time due to the use of static variables.
34 *
35 *********
36 */
37
38 #if HAVE_CONFIG_H
39 # include <config.h>
40 #endif
41
42
43 #include <tina/medical/med_SroiDef.h>
44 #include <tina/medical/medSroi_alloc.h>
45
46 #include <math.h>
47 #include <string.h>
48 #include <tina/math/mathDef.h>
49 #include <tina/math/mathPro.h>
50 #include <tina/image/imgDef.h>
51 #include <tina/image/imgPro.h>
52
53 #include "med_SroiDef.h"
54 #include "medSroi_io.h"
55
56
57 /*
58 * Local variables
59 */
60
61 static FILE *blfp = NULL;
62 static int blcount = 0;
63 static int bltotal = 0;
64
65
66 static int get_int(FILE * f)
67 {
68 int n = 0;
69
70 fscanf(f, "%d", &n);
71 return n;
72 }
73
74 static float get_float(FILE * f)
75 {
76 float x = 0.0;
77
78 fscanf(f, "%f", &x);
79 return x;
80 }
81
82 static double get_ledouble(FILE * f)
83 {
84 double x = 0.0;
85
86 fscanf(f, "%le", &x);
87 return x;
88 }
89
90 static double get_lfdouble(FILE * f)
91 {
92 double x = 0.0;
93
94 fscanf(f, "%lf", &x);
95 return x;
96 }
97
98 void dvector_printf(FILE * f, Vector * vec)
99 {
100 double *el = (double *) vec->data;
101 int i, n = vec->n;
102
103 fprintf(f, "double ");
104 fprintf(f, "%d ", n);
105 for (i = 0; i < n; i++)
106 fprintf(f, "%f ", el[i]);
107 }
108
109 void dvector_scanf(FILE * f, Vector * vec)
110 {
111
112 double *el = (double *) vec->data;
113 int i;
114 char type[64];
115
116 fscanf(f, "%s", type);
117 if (strcmp(type, "double") != 0)
118 {
119 error("Wrong data type in file", non_fatal);
120 return;
121 }
122 vec->n = get_int(f);
123 for (i = 0; i < vec->n; i++)
124 el[i] = get_lfdouble(f);
125 }
126
127
128 void dmatrix_scanf(FILE * f, Matrix * mat)
129 {
130 double **el = mat->el.double_v;
131 int i, j;
132 char dtype[64], mtype[64];
133
134 fscanf(f, "%s", dtype);
135 if (strcmp(dtype, "double") != 0)
136 {
137 error("Wrong data type in file", non_fatal);
138 return;
139 }
140 switch (mat->shape)
141 {
142 case matrix_full:
143 fscanf(f, "%s", mtype);
144 if (strcmp(mtype, "full") != 0)
145 {
146 error("Wrong matrix shape in file", non_fatal);
147 return;
148 }
149 mat->m = get_int(f);
150 mat->n = get_int(f);
151 for (i = 0; i < mat->m; ++i)
152 for (j = 0; j < mat->n; ++j)
153 el[i][j] = get_lfdouble(f);
154 break;
155 case matrix_lower:
156 fscanf(f, "%s", mtype);
157 if (strcmp(mtype, "lower") != 0)
158 {
159 error("Wrong matrix shape in file", non_fatal);
160 return;
161 }
162 mat->m = get_int(f);
163 mat->n = get_int(f);
164 for (i = 0; i < mat->m; ++i)
165 for (j = 0; j < i; ++j)
166 el[i][j] = get_lfdouble(f);
167 break;
168 case matrix_upper:
169 fscanf(f, "%s", mtype);
170 if (strcmp(mtype, "upper") != 0)
171 {
172 error("Wrong matrix shape in file", non_fatal);
173 return;
174 }
175 mat->m = get_int(f);
176 mat->n = get_int(f);
177 for (i = 0; i < mat->m; ++i)
178 for (j = i; j < mat->m; ++j)
179 el[i][j] = get_lfdouble(f);
180 break;
181 case matrix_symmetric:
182 fscanf(f, "%s", mtype);
183 if (strcmp(mtype, "symmetric") != 0)
184 {
185 error("Wrong matrix shape in file", non_fatal);
186 return;
187 }
188 mat->m = get_int(f);
189 mat->n = get_int(f);
190 for (i = 0; i < mat->m; ++i)
191 for (j = 0; j <= i; ++j)
192 el[i][j] = get_lfdouble(f);
193 break;
194 default:
195 error("Wrong matrix shape in file", non_fatal);
196 return;
197 }
198 }
199
200 void output_model(char *fname, Model *model)
201 {
202 char temps[256];
203 FILE *fp;
204 int i, j;
205 float **p;
206 double **el;
207
208 if (model == NULL)
209 return;
210
211 if ((fp = fopen(fname, "wt")) == NULL)
212 {
213 string_append(temps, "can't open file", fname, 0);
214 error(temps, non_fatal);
215 return;
216 }
217 el = model->m->el.double_v;
218 p = model->profile->el.float_v;
219
220 fprintf(fp, "%d ", model->r);
221 fprintf(fp, "%d ", model->c);
222 fprintf(fp, "%d ", model->o);
223 fprintf(fp, "%e ", model->tx);
224 fprintf(fp, "%e ", model->ty);
225 fprintf(fp, "%e ", model->s);
226 fprintf(fp, "%e ", model->theta);
227 fprintf(fp, "%d ", model->vsize);
228 fprintf(fp, "\n");
229
230 for (i = 0; i < model->r; i++)
231 {
232 for (j = 0; j < model->c; j++)
233 fprintf(fp, "%e ", el[i][j]);
234 fprintf(fp, "\n");
235 }
236 for (i = 0; i < model->c; i++)
237 {
238 for (j = 0; j < 2 * model->vsize - 1; j++)
239 fprintf(fp, "%f ", p[i][j]);
240 fprintf(fp, "\n");
241 }
242 fflush(fp);
243 fclose(fp);
244 }
245
246
247
248 Model *input_model(char *fname, Model *model)
249 {
250 FILE *fp;
251 int i, j, r, c, o, vsize;
252 float **p;
253 double **el, tx, ty, s, theta;
254 char temps[256];
255
256 if ((fp = fopen(fname, "r")) == NULL)
257 {
258 string_append(temps, "can't open file", fname, 0);
259 error(temps, non_fatal);
260 return (NULL);
261 }
262 r = get_int(fp);
263 c = get_int(fp);
264 o = get_int(fp);
265 tx = get_ledouble(fp);
266 ty = get_ledouble(fp);
267 s = get_ledouble(fp);
268 theta = get_ledouble(fp);
269 vsize = get_int(fp);
270
271 if (model == NULL || model->c != c || model->o != o ||
272 model-> r != r || model->vsize != vsize)
273 {
274 if ((model = model_alloc(r, c, o, vsize)) == NULL)
275 /* warning, memory should have been deleted here NAT */
276 return (NULL);
277 }
278
279 el = model->m->el.double_v;
280 p = model->profile->el.float_v;
281
282 /* these are either already correct or were set in model alloc NAT
283 model->r = r;
284 model->c = c;
285 model->o = o;
286 model->vsize = vsize;
287 */
288 model->tx = tx;
289 model->ty = ty;
290 model->s = s;
291 model->theta = theta;
292
293 for (i = 0; i < model->r; i++)
294 for (j = 0; j < model->c; j++)
295 el[i][j] = get_ledouble(fp);
296 for (i = 0; i < model->c; i++)
297 for (j = 0; j < 2 * model->vsize - 1; j++)
298 p[i][j] = get_float(fp);
299
300 fclose(fp);
301 return(model);
302 }
303
304
305 int sroi_open_buildlist(char *fname)
306 {
307 char temp[512];
308
309 if ((fname == NULL) || (blfp = fopen(fname, "r")) == NULL)
310 {
311 sprintf(temp, "sroi_open_buildlist: build list %s not found", fname);
312 error(temp, warning);
313 return(0);
314 }
315
316 bltotal = blcount = 0;
317 while (fscanf(blfp, "%s", temp) != EOF)
318 bltotal++;
319 rewind(blfp);
320
321 return(bltotal);
322 }
323
324
325 int sroi_getnext_build(char *fname)
326 {
327 int status;
328
329 if (fname == NULL || blfp == NULL)
330 return(0);
331
332 status = fscanf(blfp, "%s", fname);
333 if (status == 0 || status == EOF)
334 {
335 if (blcount != bltotal)
336 return(0);
337 else
338 return(-1);
339 }
340
341 return(1);
342 }
343
344
345 void sroi_close_buildlist(void)
346 {
347 if (blfp == NULL)
348 return;
349
350 fclose(blfp);
351 blfp = NULL;
352
353 return;
354 }
355
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.