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

Linux Cross Reference
Tina5/tina-libs/tina/file/fileMatrix_io.c

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

  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/file/fileMatrix_io.c,v $
 37  * Date    :  $Date: 2005/02/07 23:27:41 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: fileMatrix_io.c,v 1.4 2005/02/07 23:27:41 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes   :
 44  * 
 45  *   matrix_io.c
 46  *   Functions to read/write to/from files/matrices
 47  *
 48  *   Author: A.Lacey
 49  *   Date  : 2-4-93;  version 1.0
 50  *           6.12.95; version 2.0
 51  *   Update: S.Crossley
 52  *   Date  : 9-5-96; version 3.0
 53  *
 54  *********
 55 */
 56 
 57 #include "fileMatrix_io.h"
 58 
 59 #if HAVE_CONFIG_H
 60   #include <config.h>
 61 #endif
 62 
 63 #include <stdio.h>
 64 #include <tina/sys/sysPro.h>
 65 #include <tina/sys/sysDef.h>
 66 #include <tina/math/mathPro.h>
 67 #include <tina/math/mathDef.h>
 68 
 69 #ifndef MAT_HEAD_DEFINED
 70 #define MAT_HEAD_DEFINED
 71 
 72 typedef struct mat_head
 73 {
 74     Vartype vtype;
 75     Matrix_shape shape;
 76     int m, n;
 77 } Mat_hdr;  
 78 
 79 #endif /* MAT_HEAD_DEFINED */
 80 
 81 #define SFPLIMIT 50
 82 
 83 static int isave_matrix(Matrix *mp, FILE *fp)
 84 {
 85   int i, j, val;
 86   
 87   for (i=0; i<mp->m; i++)
 88     {
 89       for (j=0; j<mp->n; j++)
 90         {
 91           val = mp->el.int_v[i][j];
 92           if(!fprintf(fp, "%d\t", val))
 93             return (-1);
 94         }
 95       
 96       fprintf(fp, "\n");
 97     }
 98   
 99   return (0);    
100 }
101 
102 static int fsave_matrix(Matrix *mp, FILE *fp)
103 {
104   int    i, j;
105   float val;
106   
107   for (i=0; i<mp->m; i++)
108     {
109       for (j=0; j<mp->n; j++)
110         {
111           val = mp->el.float_v[i][j];
112           if(!fprintf(fp, "%f\t", val))
113             return (-1);
114         }
115       
116       fprintf(fp, "\n");
117     }
118   
119   return (0);    
120 }
121 
122 static int dsave_matrix(Matrix *mp, FILE *fp)
123 {
124   int i, j;
125   double val;
126   
127   for (i=0; i<mp->m; i++)
128     {
129       for (j=0; j<mp->n; j++)
130         {
131           val = mp->el.double_v[i][j];
132           if (!fprintf(fp, "%f\t", val))
133             return (-1);
134         }
135       
136       fprintf(fp, "\n");
137     }
138   
139   return (0);    
140 }
141 
142 static int iload_matrix(Matrix *mp, FILE *fp)
143 {
144   int i, j;
145   int val;
146   
147   for (i=0; i<mp->m; i++)
148     for (j=0; j<mp->n; j++)
149       {
150         if(!fscanf(fp, " %d ", &val))
151           return (-1);
152         mp->el.int_v[i][j] = val;
153       }
154   
155   return (0);   
156 }
157 
158 static int fload_matrix(Matrix *mp, FILE *fp)
159 {
160   int i, j;
161   float val;
162   
163   for (i = 0; i < mp->m; i++)
164     for (j = 0; j < mp->n; j++)
165       {
166         if(!fscanf(fp, " %f ", &val))
167           return (-1);
168         mp->el.float_v[i][j] = val;
169       }
170   
171   return (0);   
172 }
173 
174 static int dload_matrix(Matrix *mp, FILE *fp)
175 {
176   int i, j;
177   double val;
178   
179   for (i=0; i<mp->m; i++)
180     for (j=0; j<mp->n; j++)
181       {
182         if(!fscanf(fp, " %lf ", &val))
183           return(-1);
184         mp->el.double_v[i][j] = val;
185       }
186   
187   return (0);   
188 }
189 
190 static Mat_hdr *mat_hdr_alloc(Mat_hdr *data)
191 {
192   unsigned int size;
193 
194   if (data != NULL)
195     return (NULL);
196 
197   size = sizeof(Mat_hdr);
198   if ((data = ralloc(size)) == NULL)
199     {
200       error(" memory allocation failure ", non_fatal);
201       return(NULL);
202     }
203 
204   return(data);
205 }
206 
207 static int mat_hdr_free(Mat_hdr *data)
208 {
209   if (data == NULL)
210     return (1);
211 
212   rfree(data);
213   return (0);
214 }
215 
216 static int sfpto_eol(FILE *fp)
217 {
218   char c = 0;
219   int count = 0;
220 
221   while (c!='\n' && count<SFPLIMIT)
222     {
223       c = getc(fp);
224       count++;
225     }
226 
227   if (count < SFPLIMIT)
228     return (0);
229   
230   return (1);   
231 }
232 
233 static Mat_hdr *matrix_read_hdr(char *fname)
234 {
235   Mat_hdr *mhdr = NULL;
236   FILE *fptr;
237   int status;
238 
239 
240   if (fname==NULL)
241     return (NULL);
242   
243   mhdr = mat_hdr_alloc(mhdr);
244   
245   if ((fptr=fopen(fname, "r")) == NULL)
246     {
247       error("matrix_read : cannot open file", warning);
248       mat_hdr_free(mhdr);
249       return (NULL);
250     }
251   
252   status = fscanf(fptr, " %d ; %d ; %d ; %d", &(mhdr->m), &(mhdr->n), &(mhdr->shape), &(mhdr->vtype));
253   
254   fclose(fptr);
255   
256   if (status!=4)
257     {
258       error("matrix_read : header corrupted", warning);
259       mat_hdr_free(mhdr);
260       return (NULL);
261     }
262   
263   return (mhdr);
264 }
265 
266 static int matrix_write_hdr(Mat_hdr *hdr, char *fname)
267 {
268   FILE *fptr;
269   char message[256];
270   
271   if (fname==NULL)
272     return (1);
273   
274   if ((fptr=fopen(fname, "w")) == NULL)
275     {
276       sprintf(message, "Couldn't create file %s", fname);
277       error(message, warning);
278       return (2);
279     }
280   
281   fprintf(fptr, "%d ; %d ; %d ; %d ;\n", hdr->m, hdr->n,
282           hdr->shape, hdr->vtype);
283   
284   fclose(fptr);
285   return (-1); 
286         
287 }
288 
289 Matrix *matrix_read_file(char *fname)
290 {
291   Mat_hdr *mh;
292   Matrix *mptr;
293   FILE *fptr;
294   int status = 0;
295   
296   if (fname==NULL)
297     return (NULL);
298   
299   if ((mh = matrix_read_hdr(fname)) == NULL)
300     return (NULL);
301   
302   mptr = matrix_alloc( mh->m, mh->n, mh->shape, mh->vtype);
303   mat_hdr_free(mh);
304   
305   if ((fptr = fopen(fname, "r")) == NULL)
306     {
307       error("matrix_read : cannot open file", warning);
308       return (NULL);
309     }
310   
311   if (!sfpto_eol(fptr))
312     {
313       switch (mptr->vtype)
314         {
315         case int_v:
316           status = iload_matrix(mptr, fptr);
317           break;
318         case float_v:
319           status = fload_matrix(mptr, fptr);
320           break;
321         case double_v:
322           status = dload_matrix(mptr, fptr);
323           break;
324         default:
325           status = -1;
326         }
327     }
328   fclose(fptr);
329   
330   if (status == -1)
331     {
332       matrix_free(mptr);
333       return (NULL);
334     }
335 
336   return (mptr);
337 }
338 
339 Matrix *matrix_write_file(Matrix *mptr, char *fname)
340 {
341   Mat_hdr   *mh = NULL; 
342   FILE      *fptr;
343   int        status = 0;
344 
345   if (mptr == NULL || fname == NULL)
346     return (NULL);
347   
348   mh = mat_hdr_alloc(mh);
349   
350   mh->m = mptr->m;
351   mh->n = mptr->n;
352   mh->shape = mptr->shape;
353   mh->vtype = mptr->vtype;
354   
355   if (matrix_write_hdr(mh, fname) > -1)
356     return (NULL);
357   
358   mat_hdr_free(mh);
359   
360   if ((fptr = fopen(fname, "a")) == NULL)
361     {
362       error("matrix_write : cannot open file", warning);
363       return (NULL);
364     }
365   
366   switch (mptr->vtype)
367     {
368     case int_v:
369       status = isave_matrix(mptr, fptr);
370       break;
371     case float_v:
372       status = fsave_matrix(mptr, fptr);
373       break;
374     case double_v:
375       status = dsave_matrix(mptr, fptr);
376       break;
377     default:
378       status = -1;
379     }
380   
381   fclose(fptr);
382   
383   if (status == -1)
384     return (NULL);
385   
386   return (mptr);        
387 }
388 
389 
390 
391 

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