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

Linux Cross Reference
Tina4/src/file/ani/read_header.c

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

  1 /* Copyright 1995-6 Roger P. Woods, M.D. */
  2 /* Modified 8/3/96 */
  3 
  4 /*
  5  * float read_header()
  6  *
  7  *  This routine should be customized to the particular data
  8  *   type that you want to read.
  9  *
 10  *  There should be sufficient flexibility here to allow you
 11  *    to deal with headers and images in the same file,
 12  *    headers and images in two separate files. You can even
 13  *    deal with images all in separate files provided that
 14  *    you create a global header that will contain the total
 15  *    file dimensions in this last case.
 16  *
 17  *  Note that the plane numbering starts at zero, not at one
 18  *   consequently, the planes will end at one less than the
 19  *   z_dim that this routine loads into struct key_info *stats
 20  *
 21  *  If you need to know how many bits/pixel are wanted (e.g
 22  *      for rescaling), use sizeof(my_pixels) to find out.
 23  *
 24  *  If you need to know the maximum representable number, use
 25  *      MAX_POSS_VALUE (defined in AIR.h).
 26  *
 27  *  The integer variable 'flag' is designed to carry additional
 28  *      coded information to the routine that actually reads
 29  *      the data.
 30  *
 31  *
 32  *  Generically,
 33  *    when plane<0 this routine should:
 34  *      1. open the header file using *fp_hdr
 35  *      2. read data into struct key_info *stats
 36  *              use the # bits/pixel for actual data on disk,
 37  *              not what you expect bits/pixel to become
 38  *              once the data is loaded.
 39  *      3. if *fp_img is not NULL
 40  *              close *fp_img
 41  *              set *fp_img to NULL
 42  *      4. set *flag as required by read_image unless you
 43  *              plan to do this plane by plane
 44  *      5. if successful:
 45  *              leave *fp_hdr open
 46  *              return global scaling factor
 47  *      6. if not successful:
 48  *              close *fp_hdr and make it NULL
 49  *              (don't close if you couldn't open)
 50  *              return 0 (zero)
 51  *
 52  *    when plane>=0 this routine should:
 53  *      1. always return unsuccessful if *fp_hdr is NULL
 54  *              (even if you don't need info from *fp_hdr)
 55  *      2. if *fp_img is NULL:
 56  *              open image file containing plane specified
 57  *         if *fp_img is not NULL:
 58  *              possibly close and reopen *fp_img, iff each
 59  *              plane is stored in a separate file
 60  *      3. position the image file pointer to read plane
 61  *      4. if not successful:
 62  *              close *fp_img and make it NULL
 63  *              close *fp_hdr and make it NULL
 64  *              (don't close if already NULL or unopenable)
 65  *      5. return multiplicative scaling factor for plane
 66  *              (use, if any, will be defined in read routine)
 67  *
 68  *    calling routine should close *fp_hdr and *fp_img after
 69  *      its final call to this routine
 70  *
 71  *
 72  *  Guidelines for routines calling this function:
 73  *
 74  *  1. Your first call to load a given image set should always
 75  *      be with planes<0 (this will open and read the header)
 76  *  2. Check for error after first call by testing whether
 77  *      *fp_hdr is NULL
 78  *  3. You can allocate memory after the first call
 79  *  4. If you are loading multiple planes from the same data
 80  *      set, don't close *fp_hdr between calls.
 81  *  5. If you are making sequential calls to this function
 82  *      to read sequential planes, assume that the function
 83  *      may ignore the plane number and just get you the next
 84  *      plane if *fp_img is left open during later calls;
 85  *      however, don't plan for this to be the case.
 86  *      Operationally, this means that if you want another
 87  *      plane, but it is not one higher than the last one
 88  *      that you just read from *fp_img, then close *fp_img
 89  *      and make it NULL before the later calls
 90  *  6. Check for error after subsequent calls by testing
 91  *      whether *fp_img is NULL
 92  *  7. Close *fp_img and *fp_hdr after last plane is read
 93  *      unless they are NULL (test each separately)
 94  *  8. Note that you must also close *fp_hdr if you
 95  *      only made a single call to routine with planes<0
 96  *  9. Don't forget to verify and/or correct bits/pixel once
 97  *      loading of data is completed so that it will reflect
 98  *      the data in RAM, not the data on the disk
 99  *
100  */
101 
102 #include <tina/AIR.h>
103 #include "HEADER.h"
104 
105 extern int ani_byte_reverse;
106 
107 
108 void  header_endian(struct hdr *hdr)
109 {
110 
111 /*
112   ensure correct endian of header for types > than byte size
113 */
114 
115    word_swap((char *)&(hdr->sizeof_hdr));
116    word_swap((char *)&(hdr->extents));
117    short_swap((char *)&(hdr->dims));
118    short_swap((char *)&(hdr->x_dim));
119    short_swap((char *)&(hdr->y_dim));
120    short_swap((char *)&(hdr->z_dim));
121    short_swap((char *)&(hdr->t_dim));
122    short_swap((char *)&(hdr->datatype));
123    short_swap((char *)&(hdr->bits));
124    if (sizeof(float) == 8)
125    {
126      long_swap((char *)&(hdr->x_size)); 
127      long_swap((char *)&(hdr->y_size)); 
128      long_swap((char *)&(hdr->z_size));
129    }
130    else
131    {
132      word_swap((char *)&(hdr->x_size)); 
133      word_swap((char *)&(hdr->y_size)); 
134      word_swap((char *)&(hdr->z_size));
135    } 
136    word_swap((char *)&(hdr->glmax)); 
137    word_swap((char *)&(hdr->glmin)); 
138 
139    ani_byte_reverse = 1;
140 }
141 
142 
143 float ani_read_header(char *loadfile,FILE **fp_hdr,FILE **fp_img,struct key_info *stats,
144                   int plane, int *flag)
145 
146 {
147 
148         /*
149          *This particular instance of read_header() takes advantage
150          * of the fact that the default AIR file format stores all of
151          * the data contiguously in a single volume.
152          *
153          *If *fp_img is already open,it takes advantage of the
154          * concatenation of sequential planes
155          * and ignores the requested plane number if it is >0 on the
156          * presumption that preceding plane has been read and FILE *fp_img
157          * advanced automatically accordingly.
158          * In other words, if planes >0 and *fp_img!=NULL,this routine
159          *  does nothing except return 1.
160          *
161          *This simplification may not be possible with other file formats
162          *
163          */
164 
165         struct hdr hdr;
166         char filename[128];
167         void clip_suffix();
168         float scale;
169 
170         if(plane<0){
171                 if(*fp_img!=NULL){
172                         fclose(*fp_img);
173                         *fp_img=NULL;
174                 }
175                 strcpy(filename,loadfile);
176                 clip_suffix(filename);
177                 strcat(filename,HDR_SUFF);
178                 if((*fp_hdr=fopen(filename,"rb"))==NULL){
179                         printf("cannot open file %s for input\n",filename);
180                         return 0;
181                 }
182 
183                 if(fread((char *)&hdr,1,sizeof(struct hdr),*fp_hdr)!=sizeof(struct hdr)){
184                         printf("file read error for file %s\n",filename);
185                         fclose(*fp_hdr);
186                         *fp_hdr=NULL;
187                         return 0;
188                 }
189 
190                 if (hdr.bits != 8 && hdr.bits != 16 && hdr.bits != 1)
191                   {
192                      set_swapping_ts(1);
193                      header_endian(&hdr);
194                   }
195                   
196                 if (hdr.bits != 8 && hdr.bits != 16 && hdr.bits != 1)
197                    {
198                         printf("Looks like broken header in %s\n", filename);
199                         fclose(*fp_hdr);
200                         *fp_hdr = NULL;
201                         return 0;
202                    }
203                    
204                 /*Copy required data from header struct*/
205                 stats->bits=hdr.bits;
206                 stats->x_dim=hdr.x_dim;
207                 stats->y_dim=hdr.y_dim;
208                 stats->z_dim=hdr.z_dim;
209                 stats->x_size=hdr.x_size;
210                 stats->y_size=hdr.y_size;
211                 stats->z_size=hdr.z_size;
212 
213                 if(stats->bits!=8 && stats->bits!=16 && stats->bits!=1){
214                         printf("Can't read data with %i bits/pixel (%s)\n",stats->bits,filename);
215                         fclose(*fp_hdr);
216                         *fp_hdr=NULL;
217                         return 0;
218                 }
219 
220 
221                 flag[0]=hdr.glmin;
222                 flag[1]=hdr.glmax;
223                 
224 
225                 /*Calculate global rescaling factor if image is 16 bit and compilation is 8 bit*/
226                 if(stats->bits==16 && sizeof(my_pixels)==1){
227                         if(hdr.glmin>=0){
228                                 if(hdr.glmax>32767){
229                                         return 65535.0/hdr.glmax;
230                                 }
231                                 else{
232                                         return 32767.0/hdr.glmax;
233                                 }
234                         }
235                         else{
236                                 return 65535.0/(hdr.glmax+32768);
237                         }
238                 }
239 
240                         
241                 return 1;
242         }
243 
244         else{
245                 /* If initialization wasn't done--reject*/
246                 if(*fp_hdr==NULL){
247                         if(*fp_img!=NULL){
248                                 fclose(*fp_img);
249                                 *fp_img=NULL;
250                         }
251                         printf("Coding error--failure to initialize read_header\n");
252                         return 0;
253                 }
254 
255                 scale=1;
256 
257                 /* If image file isn't already open, do so now and find plane*/
258                 if(*fp_img==NULL){
259                         strcpy(filename,loadfile);
260                         clip_suffix(filename);
261                         strcat(filename,IMG_SUFF);
262                         if((*fp_img=fopen(filename,"rb"))==NULL){
263                                 printf("cannot open file %s for input\n",filename);
264                                 fclose(*fp_hdr); /* Yes, hdr not img */
265                                 return 0;
266                         }
267                         if(plane>0){
268                                 if(fseek(*fp_img,(long)plane*(stats->bits/8)*stats->x_dim*stats->y_dim,0)<0){
269                                         printf("unable to seek to plane %i in file %s\n",plane,filename);
270                                         fclose(*fp_img);
271                                         *fp_img=NULL;
272                                         fclose(*fp_hdr);
273                                         *fp_hdr=NULL;
274                                         return 0;
275                                 }
276                                 return scale;
277                         }
278                 }
279                 /* Since image file is already open, we should be ready to read next plane*/
280                 return scale;
281         }
282 }
283 

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