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

Linux Cross Reference
Tina4/src/file/rad/rad_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 <tina/file.h>
  7 #include <tina/sys.h>
  8 #include <tina/sys_types.h>
  9 #include <tina/sysfuncs.h>
 10 
 11 extern Bool fclose_2(FILE * stream, const char *pathname);
 12 extern FILE *fopen_2(const char *pathname, const char *mode);
 13 extern Bool fread_imrect_data(const Imrect * imrect, FILE * stream, const char *pathname);
 14 extern Bool fwrite_imrect_data(const Imrect * imrect, FILE * stream, const char *pathname);
 15 
 16 /* Create an imrect and read an image file called 'pathname' into it.
 17  * Open file, read data and close file. On failure, give error message
 18  * and return NULL. */
 19 Imrect *rad_read_image(const char *pathname)
 20 {
 21     Imrect *imrect = NULL;
 22     FILE                    *fp_img=fopen_2(pathname, "rb");
 23     Vartype                 new_vtype;
 24     short                   fvernum;
 25     short                   svernum;
 26     int                     fimtype;
 27     short                   rvernum;
 28     int                       rowlo;
 29     int                       rowhi;
 30     int                       collo;
 31     int                       colhi;
 32 
 33     if (fp_img==NULL) return(NULL);
 34 
 35     fread(&fvernum, sizeof(short),1,fp_img);
 36     fread(&svernum, sizeof(short),1,fp_img);
 37     fread(&fimtype, sizeof(int),1,fp_img);
 38     fread(&rvernum, sizeof(short),1,fp_img);
 39     fread(&rowlo, sizeof(int),1,fp_img);
 40     fread(&rowhi, sizeof(int),1,fp_img);
 41     fread(&collo, sizeof(int),1,fp_img);
 42     fread(&colhi, sizeof(int),1,fp_img);
 43 
 44     if (fimtype==0) new_vtype = double_v;
 45     if (fimtype==1) new_vtype = uchar_v;
 46     if (fimtype==2) new_vtype = int_v;
 47     if (fimtype==3) new_vtype = float_v;
 48     if (fimtype==256) new_vtype = uchar_v;
 49     format("x_dim=%i\n",colhi-collo+1);
 50     format("y_dim=%i\n",rowhi-rowlo+1);
 51 
 52     if (fp_img)
 53     {
 54         Imregion imregion;
 55         imregion.lx = collo;
 56         imregion.ux = colhi+1;
 57         imregion.ly = rowlo;
 58         imregion.uy = rowhi+1;
 59 
 60         imrect = im_alloc(rowhi-rowlo+1, colhi-collo+1, &imregion, (Vartype) new_vtype);
 61         if (!fread_imrect_data(imrect, fp_img, pathname))
 62         {
 63             im_free(imrect);
 64             imrect = NULL;
 65         }
 66         fclose_2(fp_img, pathname);
 67     }
 68     return imrect;
 69 }
 70 
 71 Bool    rad_write_image(const Imrect * imrect, const char *pathname)
 72 {
 73     Bool                    write_ok = false;
 74     short                   fvernum=1;
 75     short                   svernum=1;
 76     int                     fimtype;
 77     short                   rvernum=1;
 78     int                       rowlo;
 79     int                       rowhi;
 80     int                       collo;
 81     int                       colhi;
 82  
 83     if (imrect)
 84     {
 85     FILE   *stream = fopen_2(pathname, "wb");
 86  
 87         if (stream)
 88         {
 89             if (imrect->vtype==double_v) fimtype=0; 
 90             if (imrect->vtype==uchar_v) fimtype=256; 
 91             if (imrect->vtype==int_v) fimtype=2; 
 92             if (imrect->vtype==float_v) fimtype=3; 
 93             rowlo = imrect->region->ly;
 94             rowhi = imrect->region->uy-1;
 95             collo = imrect->region->lx;
 96             colhi = imrect->region->ux-1;
 97             fwrite(&fvernum, sizeof(short),1,stream);
 98             fwrite(&svernum, sizeof(short),1,stream);
 99             fwrite(&fimtype, sizeof(int),1,stream);
100             fwrite(&rvernum, sizeof(short),1,stream);
101             fwrite(&rowlo, sizeof(int),1,stream);
102             fwrite(&rowhi, sizeof(int),1,stream);
103             fwrite(&collo, sizeof(int),1,stream);
104             fwrite(&colhi, sizeof(int),1,stream);
105 
106             write_ok = fwrite_imrect_data(imrect, stream, pathname);
107             (void) fclose_2(stream, pathname);
108         }
109     } else
110     {  
111         errorf(non_fatal, "No image to write to: %s\n", pathname);
112     }
113     return write_ok;
114 }
115 

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