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

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

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

  1 /**@(#)Functions to read/write RAW image files
  2 */
  3 
  4 #include <stdio.h>
  5 #include <string.h>
  6 #include <limits.h>
  7 #include <tina/file.h>
  8 #include <tina/sys.h>
  9 #include <tina/sys_types.h>
 10 #include <tina/sysfuncs.h>
 11 #include <tina/math.h>
 12 #include <tina/mathfuncs.h>
 13 #include <tina/temporal.h>
 14 #include <tina/AIR.h>
 15 #include <tina/stimdef.h>
 16 
 17 int  ani_byte_reverse;
 18 
 19 extern void im_endian_inplace(Imrect *im);
 20 extern Bool fclose_2(FILE * stream, const char *pathname);
 21 extern FILE *fopen_2(const char *pathname, const char *mode);
 22 extern Bool fread_imrect_data(const Imrect * imrect, FILE * stream, const char *pathname);
 23 extern Bool fwrite_imrect_data(const Imrect * imrect, FILE * stream, const char *pathname);
 24 static float x_s=1.0, y_s = 1.0, z_s=1.0;
 25 
 26 /* Create an imrect and read an image file called 'pathname' into it.
 27  * Open file, read data and close file. On failure, give error message
 28  * and return NULL. */
 29 Imrect *ani_read_image(const char *pathname, int slice)
 30 {
 31     Imrect *imrect = NULL;
 32     struct key_info         dimensions;
 33     float                   ani_read_header();
 34     FILE                    *fp_hdr=NULL;
 35     FILE                    *fp_img=NULL;
 36     int                     dummy[8];
 37     Vartype                 new_vtype;
 38     Vec3                   *iscale;
 39 
 40     ani_byte_reverse = 0;
 41     ani_read_header(pathname,&fp_hdr,&fp_img,&dimensions,-1,dummy);
 42     if(fp_hdr == NULL) return(NULL);
 43     fclose(fp_hdr);
 44 /*
 45     format("bits/pixel=%i\n",dimensions.bits);
 46 */
 47     if (dimensions.bits==32) new_vtype = int_v;
 48     if (dimensions.bits==16) new_vtype = short_v;
 49     if (dimensions.bits==8) new_vtype = uchar_v;
 50 /*
 51     format("x_dim=%i ",dimensions.x_dim);
 52     format("y_dim=%i ",dimensions.y_dim);
 53     format("z_dim=%i\n",dimensions.z_dim);
 54     format("x_s=%e ",dimensions.x_size);
 55     format("y_s=%e ",dimensions.y_size);
 56     format("z_s=%e\n",dimensions.z_size);
 57 */
 58     x_s = dimensions.x_size;
 59     y_s = dimensions.y_size;
 60     z_s = dimensions.z_size;
 61    
 62     ani_read_header(pathname,&fp_hdr,&fp_img,&dimensions,slice,dummy);
 63 
 64     if (fp_img)
 65     {
 66         Imregion *imregion = NULL;
 67 
 68         imrect = im_alloc(dimensions.y_dim, dimensions.x_dim, imregion, (Vartype) new_vtype);
 69         if (fread_imrect_data(imrect, fp_img, pathname)==false)
 70         {
 71             im_free(imrect);
 72             imrect = NULL;
 73         }
 74         else
 75         {
 76         if (ani_byte_reverse == 1)
 77           im_endian_inplace(imrect);
 78           
 79         iscale = vec3_alloc();
 80         iscale->el[0] = dimensions.x_size;
 81         iscale->el[1] = dimensions.y_size;
 82         iscale->el[2] = dimensions.z_size;
 83         imrect->props = proplist_rm(imrect->props, VOXELS);
 84         imrect->props = proplist_addifnp(imrect->props, (void *)iscale, VOXELS, vec3_free, false);
 85         }
 86         fclose_2(fp_img, pathname);
 87     }
 88     return imrect;
 89 }
 90 
 91 void ani_write_image(Sequence *params) 
 92 {
 93     struct key_info         dimensions;
 94     List   *store;
 95     Imrect   *current;
 96     Imregion *region;
 97     char      comment[80];
 98     char      filename[512];
 99     FILE     *stream;
100     int       flag[2],min,max;
101     int       new_type,i;
102     List   *get_start_el();
103     void     *prop_get();
104     Vec3     *iscale;
105 
106     store = get_start_el();
107     if (store == NULL)
108     {
109        sprintf(comment, " nothing to save in %s", params->filename);
110        error(comment, warning);
111        return;
112     }
113     sprintf(comment, "Tina generated processed file, some data fields may be missing");
114     current = store->to;
115     region = current->region;
116     new_type = current->vtype;
117     if (new_type == int_v) dimensions.bits = 32;
118     else if (new_type == short_v) dimensions.bits = 16;
119     else if (new_type == ushort_v) dimensions.bits = 16;
120     else if (new_type == uchar_v) dimensions.bits = 8;
121     else 
122     {
123        sprintf(comment, " data type not supported for output ");
124        error(comment, warning);
125        return;
126     }
127     dimensions.x_dim = region->ux - region->lx; 
128     dimensions.y_dim = region->uy - region->ly;
129     if ((iscale = (Vec3 *)(prop_get(current->props,VOXELS))) == NULL)
130     {
131        dimensions.x_size = 0.0; 
132        dimensions.y_size = 0.0; 
133        dimensions.z_size = 0.0;
134        format("ani_write_image : no voxel sizes available\n");
135     }
136     else   
137     {
138        dimensions.x_size = iscale->el[0];
139        dimensions.y_size = iscale->el[1];
140        dimensions.z_size = iscale->el[2];
141     }
142 
143     sprintf(filename,"%s%s", params->filename, ".img");
144     stream = fopen_2(filename, "wb");
145     if (!stream) return;
146     flag[0] = INT_MIN;
147     flag[1] = INT_MAX;
148     for(i = params->seq_start; i <= params->seq_end; i++)
149     {
150         if ((store == NULL) || (store->to == NULL))
151         {
152            sprintf(comment, " no data for %s, aborting", filename);
153            error(comment, warning);
154            (void) fclose_2(stream,  filename);
155            break;
156         }
157         current = store->to;
158         imf_minmax(current, &min, &max);
159         if (min<flag[0]) flag[0] = min;
160         if (min>flag[1]) flag[1] = max;
161 
162 /* 
163   maintain BIG_ENDIAN format when writing
164   swap - write - swap
165   a.lacey@man.ac.uk
166 */
167 #ifndef BIG_ENDIAN_ARCHITECTURE
168         im_endian_inplace(current);
169         fwrite_imrect_data(current, stream, filename); 
170         im_endian_inplace(current);
171 #else
172         fwrite_imrect_data(current, stream, filename); 
173 #endif
174         store = store->next;
175     }
176     (void) fclose_2(stream,  filename); 
177     dimensions.z_dim = i-params->seq_start;
178     write_ani_header(params->filename,&dimensions,comment,flag);
179 }
180 

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