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

Linux Cross Reference
Tina6/tina-libs/tina/file/fileAvs_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/fileAvs_io.c,v $
 37  * Date    :  $Date: 2005/01/23 19:10:21 $
 38  * Version :  $Revision: 1.5 $
 39  * CVS Id  :  $Id: fileAvs_io.c,v 1.5 2005/01/23 19:10:21 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes   :
 44  * 
 45  * As of conversion this code was unused.  The volume read/write code introduces an
 46  * illegal call structure - requiring knowledge of the sequence tool structure
 47  * and has been commented out.
 48  *
 49  *
 50  * avs_io.c 
 51  * James S Perrin Time-stamp: <Tuesday 25/07/00 11:06:00 zzcgujp> 
 52  * read/write avs uniform field binary files 
 53  * the header and data are written to separate files xxxx.fld and xxx.dat 
 54  * for sequences write 3D field (2D if only one image) 
 55  * for images write 2D field though AVS does have a v.simple image format it
 56 is not suitable
 57  *
 58  *********
 59 */
 60 
 61 #include "fileAvs_io.h"
 62 
 63 #if HAVE_CONFIG_H
 64   #include <config.h>
 65 #endif
 66 
 67 #include <stdio.h>
 68 #include <stdlib.h>
 69 #include <string.h>
 70 #include <tina/image/imgPro.h>
 71 #include <tina/file/file_UtilPro.h>
 72 
 73 #define DEBUG
 74 
 75 
 76 static char *avs_data_out[] = { "byte", "byte", "short", "short",  "integer", "integer",  "float", "double" };  /* static data! */
 77 /* AVS doesn't defined signedness of types */
 78 static char *avs_data_in[] = { "", "byte", "", "short",  "integer", "",  "float", "double" };                                   /* static data! */      
 79 static char avs_ndata = 8;                                                                                                                                                                              /* static data! */
 80  char avs_end_head[3] = "\f\f";
 81 
 82 typedef struct _avs_header {
 83   int ndim;
 84   int dim1, dim2, dim3; /* assume we'll only deal with <= 3D data in Tina */
 85   int datatype; /* im.vtype */
 86   int filetype; /* 0 bin, 1 ascii */
 87   int skip;
 88   char dataname[512]; /* name of the file containg the data, NULL otherwise */
 89 } avs_header;
 90 
 91 
 92 /* internal functions */
 93 static int avs_read_header(FILE *fp, avs_header *header)
 94 {
 95   int i,j;
 96   char buffer[256], *tok, *tmp;
 97   int buffer_size = 255;
 98   
 99   char *avs_keys[] = { "ndim", "dim1", "dim2", "dim3", "nspace",
100                       "veclen", "data", "field", "variable", "#", "label", "unit" };
101   const int avs_nkeys = 10;
102   char *avs_format_keys[] = { "file", "filetype", "skip", "offset", "stride" };
103   char avs_format_nkeys = 5;
104   
105   char *filetype_str="rb";
106   int nspace=0, veclen=1, offset=0, stride=1, data=4;
107 
108   /* is this an AVS field file? */
109   tmp = fgets(buffer, buffer_size, fp);
110   
111   if(!tmp || strncmp(buffer, "# AVS field file", 16))
112   {
113     errorf(non_fatal, "File is not an AVS file\n");
114     fclose(fp);
115     return 0;
116   }
117 
118   /* set defaults */
119   header->dataname[0]='\0';
120   header->skip=0;
121   header->filetype=0;
122   header->datatype=4;
123   
124   /* parse rest of header */
125   tmp = fgets(buffer, buffer_size, fp);
126 
127   while(tmp && strncmp(tmp, avs_end_head, 2))
128   {
129     /* find avs keyword */
130     tok = strtok(buffer, " =\n\t");
131 
132     if(tok) /* not a blank line */
133     {
134       for(i=0; i<avs_nkeys; i++)
135       {
136         if(!strcmp(avs_keys[i], tok))
137           break;
138       }
139 
140       switch(i)
141       {
142       case 0: /* ndim */
143         header->ndim = atoi(strtok(NULL, " =\n\t"));
144       
145         if(header->ndim<2)
146         {
147           errorf(non_fatal, "AVS file must have 2D or 3D data\n");
148           fclose(fp);
149           return 0;
150         }
151         break;
152       case 1: /* dim1 */
153         header->dim1 = atoi(strtok(NULL, " =\n\t"));
154         break;
155       case 2: /* dim2 */
156         header->dim2 = atoi(strtok(NULL, " =\n\t"));
157         break;
158       case 3: /* dim3 */
159         header->dim3 = atoi(strtok(NULL, " =\n\t"));
160         break;
161       case 4: /* nspace */
162         nspace = atoi(strtok(NULL, " =\n\t"));
163         break;
164       case 5: /* veclen */
165         /* if not scalar only first value will be read */
166         veclen = atoi(strtok(NULL, " =\n\t"));
167         if(veclen!=1)
168           errorf(non_fatal, "AVS file must should scalar data\n");
169       
170         break;
171       case 6: /* data */
172         tok = strtok(NULL, " =\n\t");
173         
174         for(data=0; data<avs_ndata; data++)
175         {
176           if(!strcmp(avs_data_in[data], tok))
177             break;
178         }
179 
180         if(data==avs_ndata)
181         {
182           errorf(non_fatal, "AVS file data type not recognised\n");
183           fclose(fp);
184           return 0;
185         }
186         
187         header->datatype = data;
188         break;
189       case 7: /* field */
190         if(strcmp("uniform", strtok(NULL, " =\n\t")) )
191         {
192           errorf(non_fatal, "AVS file must have uniform data\n");
193           fclose(fp);
194           return 0;
195         }
196       
197         break;
198       case 8: /* variable */
199         if(atoi(strtok(NULL, " ="))!=1)
200           errorf(non_fatal, "AVS file should contain scalar data\nignoring other values\n");
201         else {
202           while((tok=strtok(NULL, " =\n\t")))
203           {
204             for(j=0; j<avs_format_nkeys; j++)
205             {
206               if(!strcmp(avs_format_keys[j], tok))
207                 break;
208             }
209         
210             switch(j)
211             {
212             case 0: /* file */
213               strcpy(header->dataname, strtok(NULL, " =\n\t"));
214               break;
215             case 1: /* filetype */
216               if(!strcmp("binary",strtok(NULL, " =\n\t")) )
217               {
218                 header->filetype = 0;
219                 filetype_str="rb";
220               } else {
221                 header->filetype = 1;
222                 filetype_str="r";
223                 errorf(non_fatal, "AVS reader can only handle binary data at the moment\n");
224                 fclose(fp);
225                 return 0;
226               }
227               break;
228             case 2: /* skip */
229               header->skip = atoi(strtok(NULL, " =\n\t"));
230               break;
231             case 3: /* offset */
232               offset = atoi(strtok(NULL, " =\n\t"));
233               break;
234             case 4: /* stride */
235               stride = atoi(strtok(NULL, " =\n\t"));
236               if(stride!=1)
237               {
238                 errorf(non_fatal, "AVS reader can only handle stride=1 at the moment\n");
239                 fclose(fp);
240                 return 0;
241               }
242               break;
243             }   
244           } /* while */
245         
246         }
247         break;
248       
249       case 9: /* # */
250       case 10: /* label */
251       case 11: /* unit */
252         /* do nothing */
253         break;
254       default:
255         errorf(non_fatal, "Invalid keyword in AVS file: %s\n", tok);
256       }
257       
258     } /* not a blank line */
259     
260     /* next line */
261     tmp = fgets(buffer, buffer_size, fp);
262   } /* while */
263   
264 #ifdef DEBUG
265   /* print header */
266   printf("ndim %d dim1 %d dim2 %d dim3 %d\n", header->ndim, header->dim1, header->dim2, header->dim3);
267   printf("nspace %d veclen %d data %s\n", nspace, veclen, avs_data_in[header->datatype]);
268   printf("filetype %d skip %d offset %d stride %d\n", header->filetype, header->skip, offset, stride);
269 #endif
270 
271   return 1;
272 }
273 
274 
275 static int avs_write_header(FILE *fp, avs_header *header)
276 {
277   fprintf(fp, "# AVS field file\n");
278   fprintf(fp, "# Output generated by Tina\n");
279   fprintf(fp, "ndim = %d\n", header->ndim);
280   fprintf(fp, "dim1 = %d\n", header->dim1);
281   fprintf(fp, "dim2 = %d\n",  header->dim2);
282   if(header->ndim==3) fprintf(fp, "dim3 = %d\n", header->dim3);
283   fprintf(fp, "nspace = %d\n", header->ndim);
284   fprintf(fp, "veclen = 1\n");
285 
286   if(header->datatype>= avs_ndata || *avs_data_out[header->datatype] == '\0')
287   {
288     errorf(non_fatal, "AVS files don't support current datatype %d\n", header->datatype);
289     return 0;
290   }
291   
292   fprintf(fp, "data = %s\n", avs_data_out[header->datatype]);
293   fprintf(fp, "field = uniform\n\n");
294   fprintf(fp, "variable 1 file = %s filetype = binary\n", header->dataname);
295   fprintf(fp, "\f\f");
296 
297   return 1;
298 }
299 
300 /*
301 Imrect *avs_read_volume(Sequence *seq)
302 {
303   FILE *fp;
304   int i, j;
305   char buffer[256], *tok, *tmp;
306   int buffer_size = 255;
307   
308   char filename_data[512], dirname_tmp[512], *filetype_str[]={"rb","r"};
309   avs_header header;
310 
311   Imrect *im = NULL;
312   Imregion *roi = NULL;
313   
314   filename_data[0]='\0';
315 
316   fp = fopen(seq->filename, "rb");
317 
318   if(!fp)
319   {
320     strcat(seq->filename, ".fld");
321     fp = fopen(seq->filename, "rb");
322     
323     if(!fp)
324     {
325       errorf(non_fatal, "Couldn't open AVS file: %s\n", seq->filename);
326       return NULL;
327     }
328   }
329 
330   avs_read_header(fp, &header);
331   if(header.ndim == 2) header.dim3 = 1;
332 
333   seq->seq_start = 0;
334   seq->seq_end = header.dim3 - 1;
335   
336   if(header.dataname != NULL)
337   {
338     fclose(fp);
339 
340     if(header.dataname[0]!='/')
341     {
342       char *slash;
343       strcpy(dirname_tmp, seq->filename);
344       
345       if ((slash = strrchr(dirname_tmp, '/')))
346         *(++slash) = '\0';
347       else
348         (void) strcpy(dirname_tmp, "./");
349 
350       strcat(dirname_tmp, header.dataname);
351       strcpy(filename_data, dirname_tmp);
352     }
353     
354     fp = fopen(filename_data, filetype_str[header.filetype]);
355     
356     if(!fp)
357     {
358       errorf(non_fatal, "Can't open AVS data file: %s\n", filename_data);
359       im_free(im);
360       fclose(fp);
361       return NULL;
362     }
363   } else { 
364     if(strncmp(tmp, avs_end_head, 2)==0)
365     {
366       int len;
367 
368       len = strlen(tmp);
369       fseek(fp, 2 - len, SEEK_CUR);
370     }
371   }
372   
373   if(header.skip)
374   {
375     if(header.filetype==0)
376       for(j=0; j<header.skip; j++)
377         getc(fp);
378     else
379       for(j=0; j<header.skip; j++)
380         fgets(buffer, buffer_size, fp);
381   }
382     
383   for(i=0; i<header.dim3; i++)
384   {
385     roi = roi_alloc(0, 0, header.dim1, header.dim2);
386     im = im_alloc(header.dim2, header.dim1, roi, (Vartype)header.datatype);
387     rfree((void *) roi);
388 
389 
390     if (!fread_imrect_data(im, fp, filename_data))
391     {
392       im_free(im);
393       im = NULL;
394     }
395    
396     set_frame_no(im, i);
397     frame_insert(im, seq);
398   }
399   
400   fclose(fp);
401   
402   return im;
403 }
404 
405 
406 Bool avs_write_volume(Sequence *seq, List *store)
407 {
408   FILE *fp;
409   int i;
410   char headname[512], dataname[512], *slash;
411   avs_header header;
412 
413   Imrect *im;
414   Imregion *roi;
415 
416   sprintf(headname, "%s.fld", seq->filename);
417   sprintf(dataname, "%s.dat", seq->filename);
418   
419   fp = fopen(headname, "w");
420 
421   if(!fp)
422   {
423     errorf(non_fatal, "Can't open AVS header file: %s\n", headname);
424     return 0;
425   }
426   
427   im = store->to;
428   roi = im->region;
429   header.dim1 = roi->ux - roi->lx;
430   header.dim2 = roi->uy - roi->ly;
431   header.dim3 = seq->seq_end - seq->seq_start +1;
432 
433   if(header.dim3>1)
434     header.ndim = 3;
435   else
436     header.ndim = 2;
437   
438   header.datatype = im->vtype;
439   
440   if ((slash = strrchr(dataname, '/')))
441      strcpy(header.dataname, ++slash);
442   else
443     strcpy(header.dataname, dataname);
444   
445   avs_write_header(fp, &header);
446   fclose(fp);
447 
448   while(store->next!=NULL)
449     store = store->next;
450   
451   fp = fopen(dataname, "wb");
452 
453   if(!fp)
454   {
455     errorf(non_fatal, "Can't open AVS data file: %s\n", dataname);
456     return 0;
457   }
458   
459   for(i=0; i<header.dim3; i++)
460   {    
461     im = store->to;
462 
463     if ((store != NULL) && (store->to != NULL))
464     {
465       if(!fwrite_imrect_data(im, fp, dataname))
466       {
467         errorf(non_fatal, "Can't write data to AVS file: %s\n", dataname);
468         fclose(fp);
469         return 0;
470       }
471     } else {
472       errorf(non_fatal, "Can't write image %d to AVS file: %s\n", i, dataname);
473       fclose(fp);
474       return 0;
475     }
476 
477     store = store->last; 
478   }
479 
480   fclose(fp);
481 
482   return 1;
483 }
484 
485 */
486 
487 
488 Imrect *avs_read_image(char *filename)
489 {
490   FILE *fp=NULL;
491   int j;
492   char buffer[256], *tmp=NULL;
493   int buffer_size = 255;
494   
495   char filename_head[512], filename_data[512], dirname_tmp[512], *filetype_str[]={"rb", "r"};
496   avs_header header;
497 
498   Imrect *im = NULL;
499   Imregion *roi = NULL;
500 
501   strcpy(filename_head, filename);
502   filename_data[0]='\0';
503 
504   fp = fopen(filename, "rb");
505 
506   if(!fp)
507   {
508     /* !!! */
509     strcat(filename_head, ".fld");
510     fp = fopen(filename_head, "rb");
511     
512     if(!fp)
513     {
514       errorf(non_fatal, "Couldn't open AVS file: %s\n", filename);
515       return NULL;
516     }
517   }
518 
519   /* parse header */
520   avs_read_header(fp, &header);
521   
522   if(header.ndim == 3)
523       errorf(non_fatal, "Warning AVS file contains 3D data\nonly first slice will be read\n");
524 
525   /* redirect input from new file if necessary */
526   if(header.dataname != NULL)
527   {
528     fclose(fp);
529 
530     if(header.dataname[0]!='/')
531     {
532       char *slash;
533       strcpy(dirname_tmp, filename_head);
534       
535       if ((slash = strrchr(dirname_tmp, '/')))
536         *(++slash) = '\0';
537       else
538         (void) strcpy(dirname_tmp, "./");
539 
540       strcat(dirname_tmp, header.dataname);
541       strcpy(filename_data, dirname_tmp);
542     }
543     
544     fp = fopen(filename_data, filetype_str[header.filetype]);
545     
546     if(!fp)
547     {
548       errorf(non_fatal, "Can't open AVS data file: %s\n", filename_data);
549       im_free(im);
550       fclose(fp);
551       return NULL;
552     }
553   } else { /* repostion file ptr due to over reading of \f\f */
554     if(strncmp(tmp, avs_end_head, 2)==0)
555     {
556       int len;
557 
558       len = strlen(tmp);
559       fseek(fp, 2 - len, SEEK_CUR);
560     }
561   }
562   
563   /* skip lines/bytes */
564   if(header.skip)
565   {
566     if(header.filetype==0)
567       for(j=0; j<header.skip; j++)
568         getc(fp);
569     else
570       for(j=0; j<header.skip; j++)
571         fgets(buffer, buffer_size, fp);
572   }
573     
574   /* read in data */
575   roi = roi_alloc(0, 0, header.dim1, header.dim2);
576   im = im_alloc(header.dim2, header.dim1, roi, (Vartype)header.datatype);
577   rfree((void *) roi);
578   
579   if (!fread_imrect_data(im, fp, filename_data))
580   {
581     im_free(im);
582     im = NULL;
583   }
584   
585   fclose(fp);
586   
587   return im;
588 }
589 
590 
591 Bool avs_write_image(char *filename, Imrect *im)
592 {
593   FILE *fp;
594   char headname[512], dataname[512], *slash;
595   avs_header header;
596 
597   Imregion *roi;
598 
599   sprintf(headname, "%s.fld", filename);
600   sprintf(dataname, "%s.dat", filename);
601   
602   fp = fopen(headname, "w");
603 
604   if(!fp)
605   {
606     errorf(non_fatal, "Can't open AVS file: %s\n", filename);
607     return 0;
608   }
609 
610   /* write header */
611   roi = im->region;
612   header.ndim = 2;
613   header.dim1 = roi->ux - roi->lx;
614   header.dim2 = roi->uy - roi->ly;
615   header.datatype = im->vtype;
616   
617   /* we want to write the .dat file relative to the .fld */
618   if ((slash = strrchr(dataname, '/')))
619      strcpy(header.dataname, ++slash);
620   else
621     strcpy(header.dataname, dataname);
622       
623   avs_write_header(fp, &header);
624   fclose(fp);
625   
626   /* write data */
627   fp = fopen(dataname, "wb");
628   if(!fp)
629   {
630     errorf(non_fatal, "Can't open AVS data file: %s\n", dataname);
631     return 0;
632   }
633   
634   if(!fwrite_imrect_data(im, fp, dataname))
635   {
636     errorf(non_fatal, "Can't write data to AVS file: %s\n", dataname);
637     fclose(fp);
638     return 0;
639   }
640   
641   fclose(fp);
642 
643   return 1;
644 }
645 

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