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

Linux Cross Reference
Tina6/tina-libs/tina/file/fileRad_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/fileRad_io.c,v $
 37  * Date    :  $Date: 2005/01/23 19:10:21 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: fileRad_io.c,v 1.4 2005/01/23 19:10:21 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes   :
 44  * 
 45  *            Functions to read/write RAW image files
 46  *
 47  *********
 48 */
 49 
 50 #include "fileRad_io.h"
 51 
 52 #if HAVE_CONFIG_H
 53   #include <config.h>
 54 #endif
 55 
 56 #include <stdio.h>
 57 #include <string.h>
 58 #include <tina/sys/sysPro.h>
 59 #include <tina/sys/sysDef.h>
 60 #include <tina/image/imgPro.h>
 61 #include <tina/image/imgDef.h>
 62 #include <tina/file/fileUtil_io.h>
 63 
 64 
 65 /* Create an imrect and read an image file called 'pathname' into it.
 66  * Open file, read data and close file. On failure, give error message
 67  * and return NULL. */
 68 Imrect *rad_read_image(const char *pathname)
 69 {
 70     Imrect *imrect = NULL;
 71     FILE                    *fp_img=fopen_2(pathname, "rb");
 72     Vartype                 new_vtype;
 73     short                   fvernum;
 74     short                   svernum;
 75     int                     fimtype;
 76     short                   rvernum;
 77     int                       rowlo;
 78     int                       rowhi;
 79     int                       collo;
 80     int                       colhi;
 81 
 82     if (fp_img==NULL) return(NULL);
 83 
 84     fread(&fvernum, sizeof(short),1,fp_img);
 85     fread(&svernum, sizeof(short),1,fp_img);
 86     fread(&fimtype, sizeof(int),1,fp_img);
 87     fread(&rvernum, sizeof(short),1,fp_img);
 88     fread(&rowlo, sizeof(int),1,fp_img);
 89     fread(&rowhi, sizeof(int),1,fp_img);
 90     fread(&collo, sizeof(int),1,fp_img);
 91     fread(&colhi, sizeof(int),1,fp_img);
 92 
 93     if (fimtype==0) 
 94     {
 95         new_vtype = double_v;
 96     }
 97     else if (fimtype==1) 
 98     {
 99         new_vtype = uchar_v;
100     }
101     else if (fimtype==2) 
102     {
103         new_vtype = int_v;
104     }
105     else if (fimtype==3) 
106     {
107         new_vtype = float_v;
108     }
109     else if (fimtype==256) 
110     {
111         new_vtype = uchar_v;
112     }
113     else
114     {
115         error("Data type not supported", warning);
116         return NULL;
117     }
118     
119     format("x_dim=%i\n",colhi-collo+1);
120     format("y_dim=%i\n",rowhi-rowlo+1);
121 
122     if (fp_img)
123     {
124         Imregion imregion;
125         imregion.lx = collo;
126         imregion.ux = colhi+1;
127         imregion.ly = rowlo;
128         imregion.uy = rowhi+1;
129 
130         imrect = im_alloc(rowhi-rowlo+1, colhi-collo+1, &imregion, (Vartype) new_vtype);
131         if (!fread_imrect_data(imrect, fp_img, pathname))
132         {
133             im_free(imrect);
134             imrect = NULL;
135         }
136         fclose_2(fp_img, pathname);
137     }
138     return imrect;
139 }
140 
141 Bool    rad_write_image(const Imrect * imrect, const char *pathname)
142 {
143     Bool                    write_ok = false;
144     short                   fvernum=1;
145     short                   svernum=1;
146     int                     fimtype;
147     short                   rvernum=1;
148     int                       rowlo;
149     int                       rowhi;
150     int                       collo;
151     int                       colhi;
152  
153     if (imrect)
154     {
155     FILE   *stream = fopen_2(pathname, "wb");
156  
157         if (stream)
158         {
159             if (imrect->vtype==double_v) fimtype=0; 
160             if (imrect->vtype==uchar_v) fimtype=256; 
161             if (imrect->vtype==int_v) fimtype=2; 
162             if (imrect->vtype==float_v) fimtype=3; 
163             rowlo = imrect->region->ly;
164             rowhi = imrect->region->uy-1;
165             collo = imrect->region->lx;
166             colhi = imrect->region->ux-1;
167             fwrite(&fvernum, sizeof(short),1,stream);
168             fwrite(&svernum, sizeof(short),1,stream);
169             fwrite(&fimtype, sizeof(int),1,stream);
170             fwrite(&rvernum, sizeof(short),1,stream);
171             fwrite(&rowlo, sizeof(int),1,stream);
172             fwrite(&rowhi, sizeof(int),1,stream);
173             fwrite(&collo, sizeof(int),1,stream);
174             fwrite(&colhi, sizeof(int),1,stream);
175 
176             write_ok = fwrite_imrect_data(imrect, stream, pathname);
177             (void) fclose_2(stream, pathname);
178         }
179     } else
180     {  
181         errorf(non_fatal, "No image to write to: %s\n", pathname);
182     }
183     return write_ok;
184 }
185 

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