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

Linux Cross Reference
Tina6/tina-libs/tina/medical/medSroi_io.c

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

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

~ [ 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.