📄 xccxct.c
字号:
the_collection = the_collection->next;
/* Free the arrays of component indices for each collection */
for(j=0;j<temp_ptr->no_collections;j++)
local_free((void *) temp_ptr->collections[j].indices);
/* Free the structure pointing to the collections */
local_free((void *)temp_ptr);
temp_ptr = the_collection;
i++;
}while(the_collection != NULL);
}
else
big_kahuna->intermed_segment = NULL;
fclose(fp);
return(big_kahuna);
}
/*****************************************************************************
* STATIC skip_comments *
*****************************************************************************/
/* Reads in lines of input from a file, skipping any comment only lines. Once */
/* a line is found that contains more than just acomment, the function calls */
/* strip_comment to remove any comments lurking at the end of the line. */
/* */
/* Returns the length of the processed line. If the length is zero, an error */
/* occurred (EOF or other). Use feof or ferror on the file pointer to determine */
/* the error condition. */
static uint skip_comments(char *buffer,int buf_len,FILE *fp,char *filename)
{
char *retval;
uint length;
do{
retval = fgets2(buffer,buf_len,fp);
if(retval == NULL){
if(feof(fp)){
length = 0;
break;
}
else{
local_error("File error in file: %s (skip_comments)",
filename);
}
}
length = strip_comment(buffer);
}while(length == 0);
return(length);
}
/*****************************************************************************
* STATIC my_sscanf *
*****************************************************************************/
/* This block of code occurred so frequently in the .fct and .ict file reading */
/* code, that it became its own function. It is very limited, it only will */
/* read one item from the string at a time. */
static int my_sscanf(char *buffer,char *format,void *dest,FILE *fp,
char *filename)
{
static char *retval = NULL;
static char delimiters[] = {'\t','\n','\v','\f','\r',' ','\0'};
int number;
uint length;
/* The following block is used to re-initialize */
/* the function. This might be done when re-winding */
/* a file or switching streams. Note this function */
/* cannot be used switching back and forth between */
/* multiple open streams due to the nature of strtok. */
/* We might however use it with one stream and when */
/* finished with that stream, switch to another one. */
if(buffer == NULL){
retval = NULL;
return(0);
}
if(retval != NULL){
number = sscanf(retval,format,dest);
retval = strtok(NULL,delimiters);
}
else{
length = skip_comments(buffer,BUF_LEN,fp,filename);
if(length == 0){
if(feof(fp))
number = EOF;
else
number = 0;
return(number);
}
retval = strtok(buffer,delimiters);
number = sscanf(buffer,format,dest);
retval = strtok(NULL,delimiters);
}
return(number);
}
/*****************************************************************************
* STATIC get_matrix_data *
*****************************************************************************/
/* This function reads in the source of the matrix data for an offset or */
/* transform matrix (decorrelation or dependency). If the data source is */
/* 'TEXT' or 'FILE', matrix storage is allocated and the data read in from */
/* the proper file. */
/* */
/* If the matrix data source is 'NULL', then an appropriate NULL transform */
/* matrix/vector is formed. For this function to know what type of NULL matrix */
/* is needed, the calling function must pass in what matrix type is being read. */
/* For a decorrelation transform matrix, the NULL matrix is an identity matrix. */
/* The NULL dependency transform matrix is a zero matrix. The NULL */
/* decorrelation offset and dependency offset vectors are zero vectors. Note */
/* that NULL transform matrices MUST BE SQUARE (think about it)!! */
/* */
/* Note this function returns the matrix data via a float pointer. For */
/* transformation matrices, which are 2D matrices, the calling function must */
/* generate any desired row pointers. */
static float *get_matrix_data(int rows,int cols,matrix_type matrix,
char *buffer,FILE *fp,char *filename)
{
int i,j,pos;
int number_read;
char fname[150];
float *data;
FILE *mfp;
/* Read in the matrix data source type */
my_sscanf(buffer,"%s",fname,fp,filename);
convert_to_lower(fname);
if(strcmp(fname,"text") == 0){ /* ASCII values in the fct/ict file */
data = (float *)local_malloc(COMPONENT_COLLECTION_MEM_KEY,
rows*cols*sizeof(float));
pos = 0;
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
my_sscanf(buffer,"%f",data+pos,fp,filename);
pos++;
}
}
}
else if(strcmp(fname,"file") == 0){ /* Binary values in a file */
data = (float *)local_malloc(COMPONENT_COLLECTION_MEM_KEY,
rows*cols*sizeof(float));
/* Note the filename cannot have any spaces in it! */
my_sscanf(buffer,"%s",fname,fp,filename);
mfp = fopen(fname,"rb");
if(mfp == NULL){
local_error("Unable to open matrix file: %s (get_matrix_data)",
fname);
}
number_read = fread(data,sizeof(float),rows*cols,mfp);
if(number_read != rows*cols){
local_error("Matrix file read error in file: %s (get_matrix_data)",
fname);
}
fclose(mfp);
}
else if(strcmp(fname,"null") == 0){
/* No transform (pass through), so we generate an */
/* appropriate NULL transform matrix or vector */
if(((matrix == DECORREL_XFM)||(matrix == DEPEND_XFM))&&(rows != cols))
local_error("Improper NULL matrix specification in file %s "
"(get_matrix_data)",filename);
/* Allocate memory and clear it. This is the proper NULL matrix */
/* to use unless we have a decorrelation transform matrix */
data = (float *)local_malloc(COMPONENT_COLLECTION_MEM_KEY,
rows*cols*sizeof(float));
memset(data,0,rows*cols*sizeof(float));
/* Generate an identity matrix for NULL decorrelation transform */
if(matrix == DECORREL_XFM)
for(i=0,pos=0;i<rows;i++,pos+=cols+1)
*(data+pos) = 1.0f;
}
else{
local_error("Unknown matrix source: %s in file: %s (get_matrix_data)",
fname,filename);
}
return(data);
}
/*****************************************************************************
* EXTERN read_ct_file *
*****************************************************************************/
/* Reads an .fct or .ict file to determine the decorrelation and dependency */
/* transformation matrices. The format of the file should look something like */
/* the following: */
/* */
/* N1 /* Number of dependency offset matrices */
/* #_rows #_columns /* Size of 0th dependency offset matrix */
/* 2 /* Data type of matrix (always 2 for float) */
/* matrix_source /* TEXT, NULL, or FILE (see below) */
/* data /* Matrix data, filename, or prediction depth */
/* #_rows #_columns /* Size of 1st dependency offset matrix */
/* 2 /* Data type of matrix (always 2 for float) */
/* matrix_source /* TEXT, NULL, or FILE (see below) */
/* data /* Matrix data, filename, or prediction depth */
/* . */
/* . /* The above information repeats for all N1 */
/* . /* dependency offset matrices. */
/* . */
/* N2 /* Number of dependency transform matrices */
/* #_rows #_columns /* Size of 0th dependency transform matrix */
/* 2 /* Data type of matrix (always 2 for float) */
/* matrix_source /* TEXT, NULL, or FILE (see below) */
/* data /* Matrix data, filename, or prediction depth */
/* #_rows #_columns /* Size of 1st dependency transform matrix */
/* 2 /* Data type of matrix (always 2 for float) */
/* matrix_source /* TEXT, NULL, or FILE (see below) */
/* data /* Matrix data, filename, or prediction depth */
/* . */
/* . /* The above information repeats for all N2 */
/* . /* dependency transform matrices. */
/* . */
/* N3 /* Number of decorrelation offset matrices */
/* #_rows #_columns /* Size of 0th decorrelation offset matrix */
/* 2 /* Data type of matrix (always 2 for float) */
/* matrix_source /* TEXT, NULL, or FILE (see below) */
/* data /* Matrix data or filename */
/* #_rows #_columns /* Size of 1st decorrelation offset matrix */
/* 2 /* Data type of matrix (always 2 for float) */
/* matrix_source /* TEXT, NULL, or FILE (see below) */
/* data /* Matrix data or filename */
/* . */
/* . /* The above information repeats for all N3 */
/* . /* decorrelation offset matrices. */
/* . */
/* N4 /* Number of decorrelation transform matrices */
/* #_rows #_columns /* Size of 0th decorrelation transform matrix */
/* 2 /* Data type of matrix (always 2 for float) */
/* matrix_source /* TEXT, NULL, or FILE (see below) */
/* data /* Matrix data or filename */
/* #_rows #_columns /* Size of 1st decorrelation transform matrix */
/* 2 /* Data type of matrix (always 2 for float) */
/* matrix_source /* TEXT, NULL, or FILE (see below) */
/* data /* Matrix data or filename */
/* . */
/* . /* The above information repeats for all N4 */
/* . /* decorrelation transform matrices. */
/* . */
/* */
/* Note that comments are permitted using the C++ comment style ('/*'). */
/* */
/* 'matrix_source' parameter values and meanings */
/* ------------------------------------------------------------------------- */
/* If matrix_source == TEXT, then a user-supplied matrix follows with the */
/* floating-point matix entries in ASCII. */
/* */
/* If matrix_source == FILE, then 'data' contains a file name which contains */
/* the transform or offset matrix in binary floating-point format. */
/* */
/* If matrix_source == NULL, then the default transform or offset matrix is */
/* to be used. The 'default' decorrelation transform matrix is the identity */
/* matrix, and the default decorrelation offset, dependency transform, and */
/* dependency offset matrices are the zero or null matrix. */
/* */
/* Note that this function always allocates one more of each transform matrix */
/* type than is indicated in the component transform file. This is done */
/* because the '0' index matrix is not used (it indicates a NULL transform) */
/* condition. */
#define MAX(a,b) (((a)>(b))?(a):(b))
component_xfms *read_ct_file(char *filename)
{
char suffix[5],buffer[BUF_LEN];
char delimiters[] = {'\t','\n','\v','\f','\r',' ','\0'};
uint length,OK;
int rows,cols,data_type,i,j;
component_xfms *big_kahuna;
float *temp_flt_ptr;
FILE *fp;
/* Check for suffix '.fct' or '.ict'. If it's not present, */
/* throw an error. Note we do not care about case. If the */
/* case is wrong and we are on an operating system that */
/* cares, then the file will fail to open. */
length = strlen(filename);
strncpy(suffix,filename+length-4,5);
convert_to_lower(suffix);
if(strcmp(suffix,".fct") != 0){
if(strcmp(suffix,".ict") != 0)
OK = 0;
else
OK = 1;
}
else
OK = 1;
if(!OK){
local_error("Invalid component transform matrix filename: %s"
" (read_ct_file).\nFile name must end in '.fct' or '.ict'",
filename);
}
fp = fopen(filename,"rt");
if(fp == NULL){
local_error("Unable to open file: %s (read_ct_file)",filename);
}
/* Allocate storage for the component_transforms */
/* structure and the all_xform_types structure. */
big_kahuna =
(component_xfms *)local_malloc(COMPONENT_COLLECTION_MEM_KEY,
sizeof(component_xfms));
/* Initialize the my_sscanf function */
my_sscanf(NULL,NULL,NULL,NULL,NULL);
/*---------------- Dependency Offset Matrix ---------------- */
/* The first non-comment line in an xct file must be N1, */
/* the number of dependency offset matrices. */
my_sscanf(buffer,"%d",&(big_kahuna->n_depend_off),fp,filename);
if(big_kahuna->n_depend_off > 0){
big_kahuna->depend_offs =
(vector_data *)local_malloc(COMPONENT_COLLECTION_MEM_KEY,
(big_kahuna->n_depend_off+1)*sizeof(vector_data));
memset(big_kahuna->depend_offs,0,
(big_kahuna->n_depend_off+1)*sizeof(vector_data));
for(i=1;i<=big_kahuna->n_depend_off;i++){
my_sscanf(buffer,"%d",&rows,fp,filename);
my_sscanf(buffer,"%d",&cols,fp,filename);
my_sscanf(buffer,"%d",&data_type,fp,filename);
if(data_type != 2){
local_error("Matrix data type must equal 2 (floating point)"
" file: %s (read_cc_file)",filename);
}
/* Allocate storage for vector */
big_kahuna->depend_offs[i].length = MAX(rows,cols);
big_kahuna->depend_offs[i].vector_values =
get_matrix_data(1,big_kahuna->depend_offs[i].length,
DEPEND_OFF,buffer,fp,filename);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -