home *** CD-ROM | disk | FTP | other *** search
/ vis-ftp.cs.umass.edu / vis-ftp.cs.umass.edu.tar / vis-ftp.cs.umass.edu / pub / Software / universal_plane_file_format / multi_read_write_plane.c < prev    next >
C/C++ Source or Header  |  1990-11-05  |  8KB  |  240 lines

  1. /* -*-c-mode-*- */
  2. /*------------------------------------------------------
  3.  *  MULTI_READ_WRITE_PLANE.C - Generic C code to read and write LLVS plane files
  4.  *                             containing multiple LLVS planes
  5.  *  Robert Heller Created on Thu Jul 13 08:27:09 1989
  6.  *  (Adapted from READ_WRITE_PLANE.C)
  7.  *  Last mod - 
  8.  *--------------------------------------------------------
  9.  *  Contents:
  10.  *--------------------------------------------------------
  11.  * (c) Copyright 1989 by The University of Massachusetts
  12.  *------------------------------------------------------*/
  13.  
  14. #include <stdio.h>        /* standard I/O defs */
  15.  
  16. #include <llvs_per_plane.h>    /* llvs per_plane defs */
  17. #include <llvs_plane.h>        /* llvs plane file defs */
  18.  
  19. char *malloc(), *calloc();
  20.  
  21.  /* 
  22.   * FILE *multi_plane_read_start(filename,plcount) - open a plane file and return
  23.   * a FILE *pointer and set plcount to the number of planes in the file.  Reads 
  24.   * the first plane file header to get its multi_plane_flag value.
  25.   */
  26.  
  27. FILE *multi_plane_read_start(filename,plcount)
  28. char *filename;
  29. int  *plcount;
  30. {
  31.     /* plane file header record structure */
  32.     static LLVS_PLANE_FILE_HEADER header;
  33.     FILE *plfile;        /* plane file */
  34.     int need_swap, need_cvt_float; /* flags to indicate if conversions needed */
  35.     /* open file.  abort if open failure */
  36.     plfile = fopen(filename,"r");
  37.     if (plfile == NULL) return(NULL);
  38.     /* read header record */
  39.     if (fread(&header,32,1,plfile) != 1) {
  40.     fclose(plfile);
  41.     return(NULL);
  42.     }
  43.     rewind(plfile);
  44.     need_swap = header.bsex != LLVS_NATIVE_BYTE_SEX;
  45.     need_cvt_float = header.floatfmt != LLVS_NATIVE_FLOATFMT;
  46.  
  47.     if (need_swap) swap_longs(&header.pl_level,7);
  48.     *plcount = header.multi_plane_flag + 1;
  49.     return(plfile);
  50.     }
  51.  
  52.  
  53.  /*
  54.   * int read_one_plane(plane,plane_info,associations,filepointer) - read in a plane file.
  55.   * plane is a pointer to a pointer to a PLANE object, plane_info is a pointer
  56.   * to a pointer to a PLANE_INFO object, associations is a pointer to a pointer
  57.   * to char, and filepointer is a FILE pointer.
  58.   * plane, plane_info, and associations are set to malloc'ed space, so should
  59.   * be addresses of cloberable pointers (i.e. addresses of variables or 
  60.   * structure fields, etc.).
  61.   */
  62.  
  63.  
  64. int read_one_plane(plane,plane_info,associations,filepointer)
  65. PLANE **plane;
  66. PLANE_INFO **plane_info;
  67. char **associations;
  68. FILE *filepointer;
  69. {
  70.     /* plane file header record structure */
  71.     static LLVS_PLANE_FILE_HEADER header;
  72.     /* plane size header record structure */
  73.     static LLVS_PLANE_SIZE_HEADER size_header;
  74.     int plsize;            /* # bytes in plane */
  75.     llvs_ubyte *data_pointer; /* pointer to data buffer */
  76.     int rbytes, bytesleft;    /* I/O byte counters */
  77.     int need_swap, need_cvt_float; /* flags to indicate if conversions needed */
  78.  
  79.     /* read header record */
  80.     if (fread(&header,32,1,filepointer) != 1) return(-1);
  81.     need_swap = header.bsex != LLVS_NATIVE_BYTE_SEX;
  82.     need_cvt_float = header.floatfmt != LLVS_NATIVE_FLOATFMT;
  83.  
  84.     if (need_swap) swap_longs(&header.pl_level,7);
  85.     if (header.ptype == LLVS_PLF_FLOAT && need_cvt_float) 
  86.     cvt_floats(&header.background,1,header.floatfmt,LLVS_NATIVE_FLOATFMT);
  87.     /* computer plane size */
  88.     plsize = header.data_length - 12;
  89.     /* allocate plane info struct */
  90.     *plane_info = (PLANE_INFO *) malloc(sizeof(PLANE_INFO));
  91.     if (*plane_info == NULL) return(-1);
  92.     /* fill in plane info slots */
  93.     (*plane_info)->datatype = header.ptype;
  94.     (*plane_info)->level = header.pl_level;
  95.     (*plane_info)->row_location = header.row_location;
  96.     (*plane_info)->column_location = header.col_location;
  97.     (*plane_info)->background.fixnum = header.background.iback;
  98.     /* allocate plane it self */
  99.     *plane = (PLANE *) malloc(plsize);
  100.     if (*plane == NULL) return(-1);
  101.     /* allocate space for association list */
  102.     *associations = calloc(header.alist_length+1,sizeof(char));
  103.     if (*associations == NULL) return(-1);
  104.     /* read in association list */
  105.     data_pointer = (llvs_ubyte *) *associations;    /* start of buffer */
  106.     bytesleft = header.alist_length; /* number of bytes to read */
  107.     while (bytesleft > 0) {
  108.     rbytes = bytesleft;
  109.     if (rbytes > BLOCKSIZE) rbytes = BLOCKSIZE;
  110.     if (fread(data_pointer,rbytes,1,filepointer) != 1) return(-1);
  111.     data_pointer += rbytes;
  112.     bytesleft -= rbytes;
  113.     }
  114.     /* read size header */
  115.     if (fread(&size_header,12,1,filepointer) != 1) return(-1);
  116.     if (need_swap) swap_longs(&size_header,3);
  117.     /* fill in addition slots in plane info */
  118.     (*plane_info)->row_dimension = size_header.row_dimension;
  119.     (*plane_info)->column_dimension = size_header.col_dimension;
  120.     /* read in plane */
  121.     data_pointer = (unsigned char *) (*plane)->plane_base;
  122.     bytesleft = plsize;
  123.     while (bytesleft > 0) {
  124.     rbytes = bytesleft;
  125.     if (rbytes > BLOCKSIZE) rbytes = BLOCKSIZE;
  126.     if (fread(data_pointer,rbytes,1,filepointer) != 1) return(-1);
  127.     if (need_swap && header.ptype == LLVS_PLF_SHORT) 
  128.         swap_words(data_pointer,rbytes >> 1);
  129.     if (need_swap && (header.ptype == LLVS_PLF_INT || 
  130.               header.ptype == LLVS_PLF_FLOAT))
  131.         swap_longs(data_pointer,rbytes >> 2);
  132.     if (need_cvt_float && header.ptype == LLVS_PLF_FLOAT)
  133.         cvt_floats(data_pointer,rbytes >> 2,header.floatfmt,LLVS_NATIVE_FLOATFMT);
  134.     data_pointer += rbytes;
  135.     bytesleft -= rbytes;
  136.     }
  137.     return(header.multi_plane_flag);
  138.     }
  139.  
  140. multi_plane_read_end(filepointer)
  141. FILE *filepointer;
  142. {
  143.     /* close file */
  144.     fclose(filepointer);
  145.     }
  146.  
  147.   /*
  148.    * write_multiple_planes(planes,plane_infos,association_lists,plcount,filename) - 
  149.    * write out a list of planes.
  150.    */
  151.  
  152. write_multiple_planes(planes,plane_infos,association_lists,plcount,filename)
  153. PLANE *planes[];
  154. PLANE_INFO *plane_infos[];
  155. char *association_lists[];
  156. int plcount;
  157. char *filename;
  158. {
  159. static  LLVS_PLANE_FILE_HEADER header;
  160.  /* plane size header record structure */
  161. static  LLVS_PLANE_SIZE_HEADER size_header;
  162. int     plsize;        /* # bytes in plane */
  163. FILE * plfile;        /* plane file */
  164. unsigned char  *data_pointer;/* pointer to data buffer */
  165. int     rbytes, bytesleft;/* I/O byte counters */
  166. register int    iplane;
  167. register char *assoc;
  168.  
  169.  /* open file.  abort if open failure */
  170. #ifdef VMS
  171.     plfile = fopen(filename, "w", "rfm=var");
  172. #else
  173.     plfile = fopen(filename, "w");
  174. #endif
  175.     if (plfile == NULL)
  176.     return(-1);
  177.     for (iplane = 0; iplane < plcount; iplane++) {
  178.     /* fill in plane file header record */
  179.     header.ptype = plane_infos[iplane]->datatype;
  180.     header.bsex = LLVS_NATIVE_BYTE_SEX;
  181.     header.floatfmt = LLVS_NATIVE_FLOATFMT;
  182.     header.reserved = 0;
  183.     header.pl_level = plane_infos[iplane]->level;
  184.     header.row_location = plane_infos[iplane]->row_location;
  185.     header.col_location = plane_infos[iplane]->column_location;
  186.     header.background.iback = plane_infos[iplane]->background.fixnum;
  187.     header.data_length = plane_size(plane_infos[iplane]) + 12;
  188.     if (association_lists[iplane] == NULL)
  189.         assoc = "NIL";
  190.     else assoc = association_lists[iplane];
  191.     header.alist_length = strlen(assoc);
  192.     header.multi_plane_flag =(plcount - iplane) - 1;
  193.  
  194.     /* write header record */
  195.     if (fwrite(&header, 32, 1, plfile) != 1)
  196.         return(-1);
  197.     /* compute plane size (for use later) */
  198.     plsize = header.data_length - 12;
  199.     /* get pointer to associaions list */
  200.     data_pointer =(llvs_ubyte *) assoc;
  201.     /* and length */
  202.     bytesleft = header.alist_length;
  203.     /* write out associations list */
  204.     while (bytesleft > 0) {
  205.         rbytes = bytesleft;
  206.         if (rbytes > BLOCKSIZE)
  207.         rbytes = BLOCKSIZE;
  208.         if (fwrite(data_pointer, rbytes, 1, plfile) != 1)
  209.         return(-1);
  210.         data_pointer += rbytes;
  211.         bytesleft -= rbytes;
  212.     }
  213.     /* fill in size header */
  214.     size_header.datatype_again = header.ptype;
  215.     size_header.row_dimension = plane_infos[iplane]->row_dimension;
  216.     size_header.col_dimension = plane_infos[iplane]->column_dimension;
  217.  
  218.     /* write out size header */
  219.     if (fwrite(&size_header, 12, 1, plfile) != 1)
  220.         return(-1);
  221.     /* get pointer & size of plane data */
  222.     data_pointer =(unsigned char *) planes[iplane]->plane_base;
  223.     bytesleft = plsize;
  224.     /* write plane data */
  225.     while (bytesleft > 0) {
  226.         rbytes = bytesleft;
  227.         if (rbytes > BLOCKSIZE)
  228.         rbytes = BLOCKSIZE;
  229.         if (fwrite(data_pointer, rbytes, 1, plfile) != 1)
  230.         return(-1);
  231.         data_pointer += rbytes;
  232.         bytesleft -= rbytes;
  233.     }
  234.     }
  235.  /* close file */
  236.     fclose(plfile);
  237.     return(0);
  238. }
  239.  
  240.