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
/
ASCENDER
/
ascender.tar.Z
/
ascender.tar
/
Triangulate
/
fbip-io.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-05-25
|
9KB
|
377 lines
#include <stdio.h>
#include <varargs.h>
#include "fbip.h"
/*
* HISTORY
*
* 30-Dec-94 Chris McGlone (jcm) at the Digital Mapping Laboratory,
* School of Computer Science at Carnegie-Mellon University.
* Installed. Originally supplied by Lynn Quam (quam@ai.sri.com)
* for use with classified RADIUS imagery.
* changed 5/24/95 Frank Stolle UMass Amherst
* added a flag if we want to print warnings about unrecognized tokens
*
*/
/*
#define WARNINGS_ON 1
*/
#ifdef NOLISP
void lispprint(s)
STRING s;
{printf("%s",s);
}
#endif
STRING string_copy(s)
STRING s;
{
STRING s2;
int n;
n = strlen(s);
if (n == 0) return(0);
s2 = (STRING ) malloc(n+1);
strcpy(s2,s);
return(s2);
}
STRING fbip_read_token(st)
FILE* st;
{
int how_many;
static char buffer[1000];
how_many = fscanf(st," %s", buffer);
if (how_many == 0){
fprintf(stderr, "fbip_read_token failed\n");
return(0);
}
/* debprintf("fbip_read_token malloc(%d)\n", strlen(buffer));*/
if (1)
{return(string_copy(buffer));}
else {return(buffer);}
}
int fbip_read_list_of_doubles (st, array, n)
FILE* st;
double* array;
int n;
{
int i, flag;
fscanf(st," (");
for (i = 0; i<n; i++){
flag = fscanf(st,"%lf", &array[i]);
if (flag == 0) break;
}
fscanf(st," )");
return(i);
}
/* ANSI C doesn't define case insensitive string compare. */
/*int strcasecmp(STRING s1, STRING s2)
{return strcmp(s1,s2);
}*/
#define TokenIs(str) (strcasecmp(token,(str)) == 0)
#define NextTokenIs(str) (strcasecmp((token=fbip_read_token(st)),(str)) == 0)
int fbip_read_header (st, fbip)
FILE* st;
FBIP fbip;
{
int i,j,n;
STRING token;
static char first_line[100];
STRING result;
static double darray[20];
result = fgets(first_line, 100, st);
if (strcasecmp(result, "RADIUS BLOCK INTERPOLATION PROJECTION\n") != 0){
fprintf(stderr, "fbip_read_header illegal file header: %s", result);
return(-1);
}
initialize_fbip_struct(fbip);
for (i = 0; i <200 ; i++){
token = fbip_read_token(st);
if TokenIs("IMAGE-ORIGIN")
{fbip_read_list_of_doubles(st, darray, 20);
fbip->umin = darray[0];fbip->vmin = darray[1];}
else if TokenIs("BLOCK-SIZE")
{fbip_read_list_of_doubles(st, darray, 20);
fbip->du = darray[0];fbip->dv = darray[1];}
else if TokenIs("N-BLOCKS"){
fbip_read_list_of_doubles(st, darray, 20);
fbip->blocks_wide = (int)darray[0];fbip->blocks_hi = (int)darray[1];
}
else if TokenIs("SCALE-FACTOR"){
fscanf(st, "%lf", &(fbip->scale_factor));
}
else if TokenIs("HORIZONTAL-UNITS"){
if NextTokenIs("METERS")
fbip->horiz_scale_factor = 1.0;
else if TokenIs("FEET")
fbip->horiz_scale_factor = 0.3048;
else
fprintf(stderr, "Unknown token '%s' for HORIZONTAL-UNITS \n", token);
}
else if TokenIs("VERTICAL-UNITS"){
if NextTokenIs("METERS")
fbip->vert_scale_factor = 1.0;
else if TokenIs("FEET")
fbip->vert_scale_factor = 0.3048;
else
fprintf(stderr, "Unknown token '%s' for VERTICAL-UNITS \n", token);
}
else if TokenIs("ELEVATION-LEVELS"){
n=fbip_read_list_of_doubles(st, darray, 20);
fbip->z_levels = (double *) malloc(n*sizeof(double));
fbip->n_levels = n;
for (j = 0; j<n; j++)
fbip->z_levels[j] = darray[j];
}
else if TokenIs("WORLD-COORDINATE-SYSTEM"){
fbip->lat_long_p = NextTokenIs("LONG-LAT");
}
else if TokenIs("ORIGIN-LAT-LONG"){
fbip_read_list_of_doubles(st, darray, 20);
fbip->xorg = darray[0];fbip->yorg = darray[1];
}
else if TokenIs("REFERENCE-ELLIPSOID"){
fbip->ref_ellipsoid = string_copy(fbip_read_token(st));
}
else if TokenIs("DATA-FORMAT"){
fbip->binaryp = NextTokenIs("BINARY");
}
else if TokenIs("BYTE-ORDER"){
fbip->binendianp = NextTokenIs("BIG-ENDIAN");
}
else if TokenIs("FLOATING-POINT-FORMAT"){
fbip->doublep = NextTokenIs("IEEE-64-BIT");
}
else if TokenIs("HEADER-END"){
do {} while (getc(st) != '\n');
break;}
else
#ifdef WARNINGS_ON
fprintf(stderr, "fbip_read_header Unrecognized token: %s\n", token);
#endif
;
}
return(1);
}
#ifdef JCM
FBIP fbip_read_header_test (pathname)
STRING pathname;
{
FILE *st;
FBIP fbip;
int binaryp, binendianp, doublep;
STRING ref_ellipsoid;
fbip = (FBIP) malloc(sizeof(struct fbip_struct));
st = fopen("/home/clam1/quam/tmp/xxx.bip2","r");
fbip_read_header (st, fbip, &binaryp, &binendianp, &doublep, &ref_ellipsoid);
fclose(st);
return(fbip);
}
#endif
/* this assumes bigendian byte order */
void read_binary_double_data_bip_file(st, fbip, corners_array)
FILE* st;
FBIP fbip;
double*** corners_array;
{
#ifdef BOZO
int ui, vi, level, i;
double u,v;
/* debprintf("read_binary_double_data_bip_file ... ");
debprintf("corners array allocated ... ");*/
for (vi=0, v=fbip->vmin; vi<=fbip->blocks_hi; vi++, v+=fbip->dv)
for (ui=0, u=fbip->umin; ui<=fbip->blocks_wide; ui++, u+=fbip->du)
{fread((STRING ) &corners_array[ui][vi][2], sizeof(double), 2* fbip->n_levels, st);
corners_array[ui][vi][0] = u;
corners_array[ui][vi][1] = v;
for (i=2; i<2+2*fbip->n_levels; i+=2)
{corners_array[ui][vi][i ] += fbip->xorg;
corners_array[ui][vi][i+1] += fbip->yorg;
}
}
/* debprintf(" done\n");*/
#endif
}
FBIP read_fbip_file(filename)
STRING filename;
{
FILE *st;
FBIP fbip;
double ***corners_array;
fbip = (FBIP) malloc(sizeof(struct fbip_struct));
st = fopen(filename,"r");
if (st == 0){
fprintf(stderr, "Problem opening fbip file '%s' \n", filename);
return(0);
}
if (fbip_read_header(st, fbip) == -1){
fprintf(stderr, "Problem reading fbip file header \n");
return(0);
}
fbip_make_arrays(fbip);
fbip_compute_height_interpolation_coeffs(fbip);
/* read the corner data */
corners_array = allocate_corners_array(fbip->blocks_wide, fbip->blocks_hi, fbip->n_levels);
if (fbip->binaryp) {
fprintf(stderr,"Sorry--can't read a binary FBIP file \n");
return;
}
else /* ascii FBIP file */
read_ascii_data_bip_file(st, fbip, corners_array);
fill_fbip(fbip, corners_array);
deallocate_corners_array(corners_array, fbip->blocks_wide, fbip->blocks_hi);
fclose(st);
return(fbip);
}
/*
CMU additions
*/
int write_ascii_data_bip_file(st, fbip, corners_array)
FILE* st;
FBIP fbip;
double*** corners_array;
{
int ui, vi, i, ii;
double *temp;
temp = (double *)calloc(2 * fbip->n_levels, sizeof(double));
for (vi=0; vi<=fbip->blocks_hi; vi++)
for (ui=0; ui<=fbip->blocks_wide; ui++){
for (ii = 0, i = 2; i < 2 + 2 * fbip->n_levels; i += 2, ii += 2){
temp[ii] = corners_array[ui][vi][i ] - fbip->xorg;
temp[ii+1] = corners_array[ui][vi][i+1] - fbip->yorg;
fprintf(st, "%20.5f %20.5f", temp[ii], temp[ii+1]);
}
fprintf(st, "\n");
}
cfree(temp);
}
int read_ascii_data_bip_file(st, fbip, corners_array)
FILE* st;
FBIP fbip;
double*** corners_array;
{
int ui, vi, i;
double u,v, dum1, dum2;
for (vi = 0, v = fbip->vmin; vi <= fbip->blocks_hi; vi++, v += fbip->dv){
for (ui = 0, u = fbip->umin; ui <= fbip->blocks_wide; ui++, u += fbip->du){
corners_array[ui][vi][0] = u; /* image coordinates */
corners_array[ui][vi][1] = v;
for (i = 2; i < 2 + 2 * fbip->n_levels; i += 2){
fscanf(st, "%lf %lf", &dum1, &dum2);
corners_array[ui][vi][i ] = dum1 + fbip->xorg;
corners_array[ui][vi][i+1] = dum2 + fbip->yorg;
}
}
}
}
/****************************
originally from lispio.c
***************************/
/*void lprintf(char *format, ...)
{va_list ap;
char pbuf[1000];
va_start(ap,format);
vsprintf(pbuf,format,ap);
va_end(ap);
lispprint(pbuf);
}*/
int debug_flag = 0;
/*void debprintf(char *format, ...)
{va_list ap;
char pbuf[1000];
if (debug_flag)
{va_start(ap,format);
vsprintf(pbuf,format,ap);
va_end(ap);
lispprint(pbuf);
}
}
*/
/*
void errprintf(char *format, ...)
{va_list ap;
char pbuf[1000];
va_start(ap,format);
vsprintf(pbuf,format,ap);
va_end(ap);
lispprint(pbuf);
}
*/
void lispprint(str)
char *str;
{
printf("%s", str);
}
int initialize_fbip_struct(fbip)
FBIP fbip;
{
fbip->ref_ellipsoid = (char *)calloc(256, sizeof(char));
strcpy(fbip->ref_ellipsoid, "");
fbip->binaryp = 0;
fbip->scale_factor = 1.0;
fbip->horiz_scale_factor = 1.0;
fbip->vert_scale_factor = 1.0;
fbip->lat_long_p = 0;
fbip->binendianp = 1;
fbip->doublep = 1;
fbip->xorg = 0.0; fbip->yorg = 0.0; fbip->zorg = 0.0;
fbip->prev_block_ui=0;
fbip->prev_block_vi=0;
}