/** * @file expander.c (the unix command 'expand' is already taken) * @author Robert S Laramee * @version 1.0 * @see ARrenderer, MarchingCubesReader *
* start date Sun 23 May 99 * finish date none *
* Description This program is meant to read in raw data, preferably
* ASCII floats, with fixed dimensions e.g. 64 X 64 X 64,
* and output that same data expanded by 1 in each dimension e.g. 65 X 65 x 65
* This is in order to make it suitable for input into Phil Rhodes' and
* Mike Guildersleeve's application: build_mr.c, which build voxels data
* from raw floating point data.
*/
#include
* Compile program using:
* % gcc -Wall expander.c -Iinclude -lm -o expander.exe
* Start the program using:
* % expander.exe [input file] [xDim] [yDim] [zDim] > [output file]
* e.g.
* % expander.exe headRes128floats.ascii 128 128 128 > headRes129floats.ascii
*
* The main() function starts the filter.
* @param -the name of the file to read from
*/
int main (int argc, char *argv[]) {
FILE *inputFile;
char *inputFileName;
/* check command line arguments */
if (argc != 5) {
fprintf(stderr, "*** Error, usage: expander.exe [inputfile] "
"[xDim] [yDim] [zDim] > [output file]\n");
exit(1);
}
inputFileName = argv[1];
if ( !( inputFile = fopen(inputFileName,"rb"))){
printf("***Error, couldn't open input file: %s\n", inputFileName);
exit(1);
}
setXrows( atoi(argv[2]));
setYcolumns(atoi(argv[3]));
setZlayers( atoi(argv[4]));
fprintf(stderr, "# expander: input file is: %s\n", inputFileName);
fprintf(stderr, "# expander: with x, y, z dimensions: %d, %d, %d\n",
getXrows(), getYcolumns(), getZlayers());
parseFile(inputFile);
fclose(inputFile);
return 0;
}
/**
* This method parses the data file
* @param inputFile -a file descriptor
*/
void parseFile(FILE *inputFile) {
int counter = 0;
int x,y,z;
char string[NUMBER];
float lastRow[getYcolumns() + 1];
float lastLayer[(getXrows() + 1) * (getYcolumns() + 1)];
float fvalue;
/* initialize arrays */
for (counter = 0; counter < (getXrows() + 1); counter++)
lastRow[counter] = 0;
for (counter = 0; counter < (getXrows()+1)*(getYcolumns()+1); counter++)
lastLayer[counter] = 0;
for (z = 0; z < getZlayers(); z++) { /* FOR EACH ZLAYER */
for (y = 0; y < getYcolumns(); y++) { /* FOR EACH YCOLUMN */
for (x = 0; x < getXrows(); x++) { /* FOR EACH XROW */
fscanf(inputFile, "%s", string);
fvalue = atof(string);
fprintf(stdout, "%f\n", fvalue);
/* IF we're reading the last element in a row, extend
* the row
*/
if (x == (getXrows() - 1) ) {
extendRow(fvalue, y);
}
/* IF we're reading the last row, save it to extend the
* columns
*/
if( (y == (getYcolumns() - 1)) ) {
lastRow[x] = fvalue;
}
/* IF we're reading the last row AND the last column,
* extend all columns
*/
if( (x == (getXrows() - 1)) &&
(y == (getYcolumns() - 1)) ) {
extendColumns(lastRow);
}
/* IF we're reading the last layer, save the whole layer
* so we can extend the layers
*/
if( (z == (getZlayers() - 1)) ) {
lastLayer[x + (y*getYcolumns())] = fvalue;
}
/* IF we're the last row AND last column AND last layer,
* extend the layer
*/
if( (x == (getXrows() - 1)) &&
(y == (getYcolumns() - 1)) &&
(z == (getZlayers() - 1)) ) {
extendLayer(lastLayer);
}
} /* end for() each XROW */
} /* end for() each YCOL */
} /* end for() each ZLAY */
}
/**
* extendRow() just reprints the last floating point number read in.
* Conceptually, it extends the current row by one data point
* @param lastColumn -an array of strings to print
* @param y -the row being extended, for debugging purposes
*/
void extendRow(float fvalue, int y) {
fprintf(stdout, "%f\n", fvalue);
}
/**
* extendColumn() just reprints the last column of floating point numbers read
* in. Conceptually, it extends the current column by one more column
* @param string -the number to copy
*/
void extendColumns(float lastRow[]) {
int i;
for(i = 0; i < getXrows(); i++) {
fprintf(stdout, "%f\n", lastRow[i]);
/* IF we're at the last element */
if (i == (getXrows() - 1) ) {
extendRow(lastRow[i], i);
}
}
}
/**
* extendRow() just reprints the last layer of floating point numbers read
* in. Conceptually, it extends the last layer by one more layer, a copy
* of itself. The bug is that the *last* element gets left out.
* @param string -the number to copy
* @param x -current row
* @param y -current column
* @param z -current layer
*/
void extendLayer(float lastLayer[]) {
int x,y;
float lastRow[getYcolumns() + 1];
for(y = 0; y < getYcolumns(); y++) { /* FOR EACH COLUMN */
for(x = 0; x < getXrows(); x++) { /* FOR EACH ROW */
/* IF we're reading the last element in a row, extend
* the row
*/
if (x == (getXrows() - 1) ) {
extendRow(lastLayer[x + (y*getYcolumns())], y);
}
/* IF we're reading the last row, save it to extend the
* columns
*/
if( (y == (getYcolumns() - 1)) ) {
lastRow[x] = lastLayer[x + (y*getYcolumns())];
}
/* IF we're reading the last row AND the last column,
* extend all columns
*/
if( (x == (getXrows() - 1)) && (y == (getYcolumns() - 1)) ) {
extendColumns(lastRow);
}
fprintf(stdout, "%f\n", lastLayer[x + (y*getYcolumns())]);
} /* end FOR EACH XROW */
} /* end FOR EACH YCOLUMN */
/* fprintf(stdout, "last layer extended\n"); */
}