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 >
Wrap
C/C++ Source or Header
|
1990-11-05
|
8KB
|
240 lines
/* -*-c-mode-*- */
/*------------------------------------------------------
* MULTI_READ_WRITE_PLANE.C - Generic C code to read and write LLVS plane files
* containing multiple LLVS planes
* Robert Heller Created on Thu Jul 13 08:27:09 1989
* (Adapted from READ_WRITE_PLANE.C)
* Last mod -
*--------------------------------------------------------
* Contents:
*--------------------------------------------------------
* (c) Copyright 1989 by The University of Massachusetts
*------------------------------------------------------*/
#include <stdio.h> /* standard I/O defs */
#include <llvs_per_plane.h> /* llvs per_plane defs */
#include <llvs_plane.h> /* llvs plane file defs */
char *malloc(), *calloc();
/*
* FILE *multi_plane_read_start(filename,plcount) - open a plane file and return
* a FILE *pointer and set plcount to the number of planes in the file. Reads
* the first plane file header to get its multi_plane_flag value.
*/
FILE *multi_plane_read_start(filename,plcount)
char *filename;
int *plcount;
{
/* plane file header record structure */
static LLVS_PLANE_FILE_HEADER header;
FILE *plfile; /* plane file */
int need_swap, need_cvt_float; /* flags to indicate if conversions needed */
/* open file. abort if open failure */
plfile = fopen(filename,"r");
if (plfile == NULL) return(NULL);
/* read header record */
if (fread(&header,32,1,plfile) != 1) {
fclose(plfile);
return(NULL);
}
rewind(plfile);
need_swap = header.bsex != LLVS_NATIVE_BYTE_SEX;
need_cvt_float = header.floatfmt != LLVS_NATIVE_FLOATFMT;
if (need_swap) swap_longs(&header.pl_level,7);
*plcount = header.multi_plane_flag + 1;
return(plfile);
}
/*
* int read_one_plane(plane,plane_info,associations,filepointer) - read in a plane file.
* plane is a pointer to a pointer to a PLANE object, plane_info is a pointer
* to a pointer to a PLANE_INFO object, associations is a pointer to a pointer
* to char, and filepointer is a FILE pointer.
* plane, plane_info, and associations are set to malloc'ed space, so should
* be addresses of cloberable pointers (i.e. addresses of variables or
* structure fields, etc.).
*/
int read_one_plane(plane,plane_info,associations,filepointer)
PLANE **plane;
PLANE_INFO **plane_info;
char **associations;
FILE *filepointer;
{
/* plane file header record structure */
static LLVS_PLANE_FILE_HEADER header;
/* plane size header record structure */
static LLVS_PLANE_SIZE_HEADER size_header;
int plsize; /* # bytes in plane */
llvs_ubyte *data_pointer; /* pointer to data buffer */
int rbytes, bytesleft; /* I/O byte counters */
int need_swap, need_cvt_float; /* flags to indicate if conversions needed */
/* read header record */
if (fread(&header,32,1,filepointer) != 1) return(-1);
need_swap = header.bsex != LLVS_NATIVE_BYTE_SEX;
need_cvt_float = header.floatfmt != LLVS_NATIVE_FLOATFMT;
if (need_swap) swap_longs(&header.pl_level,7);
if (header.ptype == LLVS_PLF_FLOAT && need_cvt_float)
cvt_floats(&header.background,1,header.floatfmt,LLVS_NATIVE_FLOATFMT);
/* computer plane size */
plsize = header.data_length - 12;
/* allocate plane info struct */
*plane_info = (PLANE_INFO *) malloc(sizeof(PLANE_INFO));
if (*plane_info == NULL) return(-1);
/* fill in plane info slots */
(*plane_info)->datatype = header.ptype;
(*plane_info)->level = header.pl_level;
(*plane_info)->row_location = header.row_location;
(*plane_info)->column_location = header.col_location;
(*plane_info)->background.fixnum = header.background.iback;
/* allocate plane it self */
*plane = (PLANE *) malloc(plsize);
if (*plane == NULL) return(-1);
/* allocate space for association list */
*associations = calloc(header.alist_length+1,sizeof(char));
if (*associations == NULL) return(-1);
/* read in association list */
data_pointer = (llvs_ubyte *) *associations; /* start of buffer */
bytesleft = header.alist_length; /* number of bytes to read */
while (bytesleft > 0) {
rbytes = bytesleft;
if (rbytes > BLOCKSIZE) rbytes = BLOCKSIZE;
if (fread(data_pointer,rbytes,1,filepointer) != 1) return(-1);
data_pointer += rbytes;
bytesleft -= rbytes;
}
/* read size header */
if (fread(&size_header,12,1,filepointer) != 1) return(-1);
if (need_swap) swap_longs(&size_header,3);
/* fill in addition slots in plane info */
(*plane_info)->row_dimension = size_header.row_dimension;
(*plane_info)->column_dimension = size_header.col_dimension;
/* read in plane */
data_pointer = (unsigned char *) (*plane)->plane_base;
bytesleft = plsize;
while (bytesleft > 0) {
rbytes = bytesleft;
if (rbytes > BLOCKSIZE) rbytes = BLOCKSIZE;
if (fread(data_pointer,rbytes,1,filepointer) != 1) return(-1);
if (need_swap && header.ptype == LLVS_PLF_SHORT)
swap_words(data_pointer,rbytes >> 1);
if (need_swap && (header.ptype == LLVS_PLF_INT ||
header.ptype == LLVS_PLF_FLOAT))
swap_longs(data_pointer,rbytes >> 2);
if (need_cvt_float && header.ptype == LLVS_PLF_FLOAT)
cvt_floats(data_pointer,rbytes >> 2,header.floatfmt,LLVS_NATIVE_FLOATFMT);
data_pointer += rbytes;
bytesleft -= rbytes;
}
return(header.multi_plane_flag);
}
multi_plane_read_end(filepointer)
FILE *filepointer;
{
/* close file */
fclose(filepointer);
}
/*
* write_multiple_planes(planes,plane_infos,association_lists,plcount,filename) -
* write out a list of planes.
*/
write_multiple_planes(planes,plane_infos,association_lists,plcount,filename)
PLANE *planes[];
PLANE_INFO *plane_infos[];
char *association_lists[];
int plcount;
char *filename;
{
static LLVS_PLANE_FILE_HEADER header;
/* plane size header record structure */
static LLVS_PLANE_SIZE_HEADER size_header;
int plsize; /* # bytes in plane */
FILE * plfile; /* plane file */
unsigned char *data_pointer;/* pointer to data buffer */
int rbytes, bytesleft;/* I/O byte counters */
register int iplane;
register char *assoc;
/* open file. abort if open failure */
#ifdef VMS
plfile = fopen(filename, "w", "rfm=var");
#else
plfile = fopen(filename, "w");
#endif
if (plfile == NULL)
return(-1);
for (iplane = 0; iplane < plcount; iplane++) {
/* fill in plane file header record */
header.ptype = plane_infos[iplane]->datatype;
header.bsex = LLVS_NATIVE_BYTE_SEX;
header.floatfmt = LLVS_NATIVE_FLOATFMT;
header.reserved = 0;
header.pl_level = plane_infos[iplane]->level;
header.row_location = plane_infos[iplane]->row_location;
header.col_location = plane_infos[iplane]->column_location;
header.background.iback = plane_infos[iplane]->background.fixnum;
header.data_length = plane_size(plane_infos[iplane]) + 12;
if (association_lists[iplane] == NULL)
assoc = "NIL";
else assoc = association_lists[iplane];
header.alist_length = strlen(assoc);
header.multi_plane_flag =(plcount - iplane) - 1;
/* write header record */
if (fwrite(&header, 32, 1, plfile) != 1)
return(-1);
/* compute plane size (for use later) */
plsize = header.data_length - 12;
/* get pointer to associaions list */
data_pointer =(llvs_ubyte *) assoc;
/* and length */
bytesleft = header.alist_length;
/* write out associations list */
while (bytesleft > 0) {
rbytes = bytesleft;
if (rbytes > BLOCKSIZE)
rbytes = BLOCKSIZE;
if (fwrite(data_pointer, rbytes, 1, plfile) != 1)
return(-1);
data_pointer += rbytes;
bytesleft -= rbytes;
}
/* fill in size header */
size_header.datatype_again = header.ptype;
size_header.row_dimension = plane_infos[iplane]->row_dimension;
size_header.col_dimension = plane_infos[iplane]->column_dimension;
/* write out size header */
if (fwrite(&size_header, 12, 1, plfile) != 1)
return(-1);
/* get pointer & size of plane data */
data_pointer =(unsigned char *) planes[iplane]->plane_base;
bytesleft = plsize;
/* write plane data */
while (bytesleft > 0) {
rbytes = bytesleft;
if (rbytes > BLOCKSIZE)
rbytes = BLOCKSIZE;
if (fwrite(data_pointer, rbytes, 1, plfile) != 1)
return(-1);
data_pointer += rbytes;
bytesleft -= rbytes;
}
}
/* close file */
fclose(plfile);
return(0);
}