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

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

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