//========================================================================= #define PROGRAMMER "BAHN, William" #define PROG_CODE "bahw" #define COURSE "ECE-1021" #define YEAR (2003) #define TERM "Fall" #define SECTION (0) #define ASSIGNMENT "Project" #define REVISION (1) #define TITLE "Single Image Random Dot Stereogram Generator" #define SUBTITLE "Last Modified - 02 DEC 03" #define EMAIL "wbahn@eas.uccs.edu" #define FILENAME "SIRDS.c" //========================================================================= // PROBLEM: // // SOLUTION: // //== INCLUDE FILES ======================================================== #include "DirtyD.h" #include "bmp.h" #include "sirds.h" #include // printf(), scanf(), sprintf() #include // sqrt(), sin(), floor() #include // isdigit(), toupper() #include // rand(), abs() #include // strlen() #include // clrscr(), gotoxy(), getch() //== MACRO DEFINITIONS ==================================================== #define CMD_COL (2) #define CMD_ROW (22) #define ERR_COL (2) #define ERR_ROW (24) #define FOCUS_DATA (0) #define FOCUS_IMAGE (1) #define FOCUS_GEN (2) #define ERASE (0) #define DRAW (1) //#define LOG(msg) (if(log) fprintf(fp_rep, "%s", (msg))) #define LOG(msg) (printf("%s", (msg))) //== TOP LEVEL SUPPORT FUNCTION PROTOTYPES ================================ void RefreshDisplay(SIRDS *s, int focus); SIRDSDATA *ChangeSIRDSDATA(SIRDS *s, int cmd); SIRDSIMAGE *ChangeSIRDSIMAGE(SIRDS *s, int cmd); SIRDSGEN *ChangeSIRDSGEN(SIRDS *s, int cmd); int ImportSIRDSDATA(SIRDS *s); int GenerateSIRDS(SIRDS *s); int MakeDataBMP(SIRDS *s); int GenGRD(char *filename); int ShiftFocus(int oldfocus, int newfocus); void DisplayCMD(char *s); void EraseCMD(void); void DisplayERR(char *s); void EraseERR(void); //== MAIN FUNCTION ======================================================== int main(void) { SIRDS *s; int focus; int cmd; int ExitFlag; s = NewSIRDS(); SetSIRDS_REPORTFILE(s, fopen("SIRDSLOG.TXT", "wt")); focus = FOCUS_DATA; RefreshDisplay(s, focus); ExitFlag = FALSE; while(!ExitFlag) { cmd = toupper(CatchKey()); EraseERR(); switch(cmd) { case 'D': focus = ShiftFocus(focus, FOCUS_DATA); break; case 'I': focus = ShiftFocus(focus, FOCUS_IMAGE); break; case 'G': focus = ShiftFocus(focus, FOCUS_GEN); break; case 'Q': ExitFlag = TRUE; break; case 'L': clrscr(); LOG("Loading Z Data.\n"); ImportSIRDSDATA(s); RefreshDisplay(s, focus); break; case 'C': SetSIRDS_IMAGE(s, NewIMAGE(s->si->PixelsY, s->si->PixelsX, 24)); clrscr(); LOG("Creating SIRDS Image.\n"); GenerateSIRDS(s); RefreshDisplay(s, focus); break; case 'X': SetSIRDS_BMP(s, GenerateBMPfromIMAGE(GetSIRDS_BMP(s), GetSIRDS_IMAGE(s)) ); clrscr(); LOG("Exporting BMP File to Disk.\n"); ExportBMP(GetSIRDS_BMP(s), s->si->filename); RefreshDisplay(s, focus); break; case 'M': GenGRD(s->sd->filename); RefreshDisplay(s, focus); break; case 'B': MakeDataBMP(s); SetSIRDS_IMAGE(s, NewIMAGE(s->si->PixelsY, s->si->PixelsX, 24) ); RefreshDisplay(s, focus); break; } if(isdigit(cmd)) { switch(focus) { case FOCUS_DATA: ChangeSIRDSDATA(s, cmd); break; case FOCUS_IMAGE: ChangeSIRDSIMAGE(s, cmd); break; case FOCUS_GEN: ChangeSIRDSGEN(s, cmd); break; } } } fclose(GetSIRDS_REPORTFILE(s)); return(0); } //== SUPPORT FUNCTIONS ==================================================== //== LOWER LEVEL FUNCTION PROTOTYPES ====================================== void DisplaySIRDSDATA(SIRDSDATA *s, int line); void DisplaySIRDSIMAGE(SIRDSIMAGE *s, int line); void DisplaySIRDSGEN(SIRDSGEN *s, int line); void ShowFocus(int focus, int mode); int GetLimitedInt(int min, int max); double GetLimitedDouble(double min, double max); //== TOP LEVEL SUPPORT FUNCTION DEFINITIONS =============================== void RefreshDisplay(SIRDS *s, int focus) { clrscr(); PrintHeader(); gotoxy(60,2); printf("[M] Make Data"); gotoxy(60,3); printf("[L] Load Data"); gotoxy(60,4); printf("[C] Create SIRDS"); gotoxy(60,5); printf("[X] Export BMP"); gotoxy(60,6); printf("[Q] Quit Program"); DisplaySIRDSDATA(GetSIRDS_SIRDSDATA(s), ALL_LINES); DisplaySIRDSIMAGE(GetSIRDS_SIRDSIMAGE(s), ALL_LINES); DisplaySIRDSGEN(GetSIRDS_SIRDSGEN(s), ALL_LINES); focus = ShiftFocus(focus, focus); return; } int ShiftFocus(int oldfocus, int newfocus) { ShowFocus(oldfocus, ERASE); ShowFocus(newfocus, DRAW); return(newfocus); } //== LOWER LEVEL SUPPORT FUNCTION DEFINITIONS ============================= #define SIRDS_DISPLAY_ROW (8) #define SIRDS_DISPLAY_DATA_LABELS ( 2) #define SIRDS_DISPLAY_IMAGE_LABELS (32) #define SIRDS_DISPLAY_GEN_LABELS (57) #define SIRDS_DISPLAY_DATA_OFFSET (25) #define SIRDS_DISPLAY_IMAGE_OFFSET (20) #define SIRDS_DISPLAY_GEN_OFFSET (19) #define SIRDS_DL (SIRDS_DISPLAY_DATA_LABELS) #define SIRDS_IL (SIRDS_DISPLAY_IMAGE_LABELS) #define SIRDS_GL (SIRDS_DISPLAY_GEN_LABELS) #define SIRDS_DO (SIRDS_DISPLAY_DATA_LABELS+SIRDS_DISPLAY_DATA_OFFSET) #define SIRDS_IO (SIRDS_DISPLAY_IMAGE_LABELS+SIRDS_DISPLAY_IMAGE_OFFSET) #define SIRDS_GO (SIRDS_DISPLAY_GEN_LABELS+SIRDS_DISPLAY_GEN_OFFSET) void DisplaySIRDSDATA(SIRDSDATA *s, int line) { switch(line) { case 0: gotoxy(SIRDS_DL, SIRDS_DISPLAY_ROW); printf(" [D]ATA PARAMETERS "); gotoxy(SIRDS_DL, SIRDS_DISPLAY_ROW+1); printf("============================="); break; case 1: gotoxy(SIRDS_DL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] File: ", line); if(NULL == s->filename) { gotoxy(SIRDS_DO, SIRDS_DISPLAY_ROW+line+1); printf("none"); } else { gotoxy(SIRDS_DO+4-strlen(s->filename), SIRDS_DISPLAY_ROW+line+1); printf("%s", s->filename); } break; case 2: gotoxy(SIRDS_DL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Data Points X:.. pts", line); gotoxy(SIRDS_DO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4i", s->PointsX); break; case 3: gotoxy(SIRDS_DL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Data Points Y:.. pts", line); gotoxy(SIRDS_DO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4i", s->PointsY); break; case 4: gotoxy(SIRDS_DL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Aspect:......... %%", line); gotoxy(SIRDS_DO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4.0f", 100.0*s->Aspect); break; case 5: gotoxy(SIRDS_DL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Data Size X:.... cm ", line); gotoxy(SIRDS_DO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4.1f", s->SizeX); break; case 6: gotoxy(SIRDS_DL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Data Size Y:.... cm ", line); gotoxy(SIRDS_DO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4.1f", s->SizeY); break; case 7: break; case 8: break; case 9: break; default: for(line = 0; line < 10; line++) DisplaySIRDSDATA(s, line); break; } return; } void DisplaySIRDSIMAGE(SIRDSIMAGE *s, int line) { switch(line) { case 0: gotoxy(SIRDS_IL, SIRDS_DISPLAY_ROW); printf(" [I]MAGE PARAMETERS "); gotoxy(SIRDS_IL, SIRDS_DISPLAY_ROW+1); printf("========================"); break; case 1: gotoxy(SIRDS_IL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] File: ", line); if(NULL == s->filename) { gotoxy(SIRDS_IO, SIRDS_DISPLAY_ROW+line+1); printf("none"); } else { gotoxy(SIRDS_IO+4-strlen(s->filename), SIRDS_DISPLAY_ROW+line+1); printf("%s", s->filename); } break; case 2: gotoxy(SIRDS_IL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Depth:..... bit", line); gotoxy(SIRDS_IO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4i", s->Depth); break; case 3: gotoxy(SIRDS_IL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Color:..... ", line); gotoxy(SIRDS_IO-1, SIRDS_DISPLAY_ROW+line+1); printf("%c", 'N' + ('Y'-'N')*s->Color); break; case 4: gotoxy(SIRDS_IL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] X Pixels:.. pix", line); gotoxy(SIRDS_IO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4i", s->PixelsX); break; case 5: gotoxy(SIRDS_IL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Y Pixels:.. pix", line); gotoxy(SIRDS_IO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4i", s->PixelsY); break; case 6: gotoxy(SIRDS_IL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Aspect:.... %%", line); gotoxy(SIRDS_IO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4.0f", 100.0*s->Aspect); break; case 7: gotoxy(SIRDS_IL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Size X:.... cm", line); gotoxy(SIRDS_IO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4.1f", s->SizeX); break; case 8: gotoxy(SIRDS_IL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Size Y:.... cm", line); gotoxy(SIRDS_IO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4.1f", s->SizeY); break; case 9: break; default: for(line = 0; line < 10; line++) DisplaySIRDSIMAGE(s, line); break; } return; } void DisplaySIRDSGEN(SIRDSGEN *s, int line) { switch(line) { case 0: gotoxy(SIRDS_GL, SIRDS_DISPLAY_ROW); printf(" [G]EN PARAMETERS "); gotoxy(SIRDS_GL, SIRDS_DISPLAY_ROW+1); printf("======================"); break; case 1: gotoxy(SIRDS_GL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Data dX:.. %", line); gotoxy(SIRDS_GO-4, SIRDS_DISPLAY_ROW+line+1); printf("%+4.0f", 100.0*s->eye_cxi); break; case 2: gotoxy(SIRDS_GL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Data dY:.. %", line); gotoxy(SIRDS_GO-4, SIRDS_DISPLAY_ROW+line+1); printf("%+4.0f", 100.0*s->eye_cyi); break; case 3: gotoxy(SIRDS_GL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Image dX:. %", line); gotoxy(SIRDS_GO-4, SIRDS_DISPLAY_ROW+line+1); printf("%+4.0f", 100.0*s->eye_cxd); break; case 4: gotoxy(SIRDS_GL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Image dY:. %", line); gotoxy(SIRDS_GO-4, SIRDS_DISPLAY_ROW+line+1); printf("%+4.0f", 100.0*s->eye_cyd); break; case 5: gotoxy(SIRDS_GL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Eye Sep:.. cm", line); gotoxy(SIRDS_GO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4.1f", s->eye_sep); break; case 6: gotoxy(SIRDS_GL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Distance:. cm", line); gotoxy(SIRDS_GO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4.1f", s->distance); break; case 7: gotoxy(SIRDS_GL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Far Plane: %", line); gotoxy(SIRDS_GO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4.0f", 100.0*s->depth); break; case 8: gotoxy(SIRDS_GL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Relief:... %", line); gotoxy(SIRDS_GO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4.1f", 100.0*s->relief); break; case 9: gotoxy(SIRDS_GL, SIRDS_DISPLAY_ROW+line+1); printf("[%1i] Null Ht:.. ", line); gotoxy(SIRDS_GO-4, SIRDS_DISPLAY_ROW+line+1); printf("%4i", s->null_ht); break; default: for(line = 0; line < 10; line++) DisplaySIRDSGEN(s, line); break; } return; } void DisplayCMD(char *s) { gotoxy(CMD_COL, CMD_ROW); printf("%s", s); return; } void EraseCMD(void) { int col; for(col = CMD_COL; col < 80; col++) { gotoxy(col, CMD_ROW); putchar(' '); } return; } void DisplayERR(char *s) { gotoxy(ERR_COL, ERR_ROW); printf("%s", s); return; } void EraseERR(void) { int col; for(col = ERR_COL; col < 80; col++) { gotoxy(col, ERR_ROW); putchar(' '); } return; } SIRDSDATA *ChangeSIRDSDATA(SIRDS *sirds, int cmd) { SIRDSDATA *s; int line; s = sirds->sd; switch(line = (cmd-'0')) { case 0: break; case 1: DisplayCMD("Enter the Input Data File Name: "); s->filename = GetFileName(s->filename, "GRD", 0); DisplaySIRDSDATA(s, line); break; case 2: DisplayCMD("Enter the Number of Data Points to use in the X: "); s->PointsX = GetLimitedInt(0, 9999); DisplaySIRDSDATA(s, line); break; case 3: DisplayCMD("Enter the Number of Data Points to use in the Y: "); s->PointsY = GetLimitedInt(0, 9999); DisplaySIRDSDATA(s, line); break; case 4: DisplayCMD("Enter the data pixel aspect ratio (W/H): "); s->Aspect = GetLimitedDouble(0.0, 999.0)/100.0; DisplaySIRDSDATA(s, line); break; case 5: DisplayCMD("Enter the physical size of the data in the X (cm): "); s->SizeX = GetLimitedDouble(0.0, 99.9); DisplaySIRDSDATA(s, line); break; case 6: DisplayCMD("Enter the physical size of the data in the Y (cm): "); s->SizeY = GetLimitedDouble(0.0, 99.9); DisplaySIRDSDATA(s, line); break; case 7: break; case 8: break; case 9: break; } EraseCMD(); return(s); } SIRDSIMAGE *ChangeSIRDSIMAGE(SIRDS *sirds, int cmd) { SIRDSIMAGE *s; int line; line = cmd - '0'; s = sirds->si; switch(line) { case 0: break; case 1: DisplayCMD("Enter the Output BMP File Name: "); s->filename = GetFileName(s->filename, "BMP", 0); DisplaySIRDSIMAGE(s, line); break; case 2: DisplayCMD("Bit Depth Fixed at 24 bits - can't be changed. "); DisplaySIRDSIMAGE(s, line); break; case 3: DisplayCMD("Enter 0 for B&W and 1 for Color BMP Image: "); s->Color = GetLimitedInt(0, 1); DisplaySIRDSIMAGE(s, line); break; case 4: DisplayCMD("Enter the BMP Image Width (pixels): "); s->PixelsX = GetLimitedInt(1, 9999); DisplaySIRDSIMAGE(s, line); break; case 5: DisplayCMD("Enter the BMP Image Height (pixels): "); s->PixelsY = GetLimitedInt(1, 9999); DisplaySIRDSIMAGE(s, line); break; case 6: DisplayCMD("Enter the image pixel aspect ratio (W/H): "); s->Aspect = GetLimitedDouble(0.0, 999.0)/100.0; DisplaySIRDSIMAGE(s, line); break; case 7: DisplayCMD("Enter the Preferred BMP Image Width (cm): "); s->SizeX = GetLimitedDouble(0.0, 99.9); DisplaySIRDSIMAGE(s, line); break; case 8: DisplayCMD("Enter the Preferred BMP Image Height (cm): "); s->SizeY = GetLimitedDouble(0.0, 99.9); DisplaySIRDSIMAGE(s, line); break; case 9: break; } EraseCMD(); return(s); } SIRDSGEN *ChangeSIRDSGEN(SIRDS *sirds, int cmd) { SIRDSGEN *s; int line; s = sirds->sg; line = cmd - '0'; switch(line) { case 0: break; case 1: DisplayCMD("Enter image shift to right of eye center (%): "); s->eye_cxi = GetLimitedDouble(-99.9, 99.9)/100.0; DisplaySIRDSGEN(s, line); break; case 2: DisplayCMD("Enter image shift above eye center (%): "); s->eye_cyi = GetLimitedDouble(-99.9, 99.9)/100.0; DisplaySIRDSGEN(s, line); break; case 3: DisplayCMD("Enter data shift to right of eye center (%): "); s->eye_cxd = GetLimitedDouble(-99.9, 99.9)/100.0; DisplaySIRDSGEN(s, line); break; case 4: DisplayCMD("Enter data shift above eye center (%): "); s->eye_cyd = GetLimitedDouble(-99.9, 99.9)/100.0; DisplaySIRDSGEN(s, line); break; case 5: DisplayCMD("Enter eye separation (cm): "); s->eye_sep = GetLimitedDouble(0.0, 99.9); DisplaySIRDSGEN(s, line); break; case 6: DisplayCMD("Enter Distance from eye to image (cm): "); s->distance = GetLimitedDouble(0.0, 99.9); DisplaySIRDSGEN(s, line); break; case 7: DisplayCMD("Enter Depth from image to far plane (% of Distance): "); s->depth = GetLimitedDouble(0.0, 999.0)/100.0; DisplaySIRDSGEN(s, line); break; case 8: DisplayCMD("Enter maximum relief (% of Depth): "); s->relief = GetLimitedDouble(0.0, 99.9)/100.0; DisplaySIRDSGEN(s, line); break; case 9: DisplayCMD("Enter \"out-of-bounds\" height: "); s->null_ht = GetLimitedInt(0, 255); DisplaySIRDSGEN(s, line); break; } EraseCMD(); return(s); } void ShowFocus(int focus, int mode) { switch(focus) { case FOCUS_DATA: gotoxy(SIRDS_DL+1, SIRDS_DISPLAY_ROW); break; case FOCUS_IMAGE: gotoxy(SIRDS_IL+1, SIRDS_DISPLAY_ROW); break; case FOCUS_GEN: gotoxy(SIRDS_GL+1, SIRDS_DISPLAY_ROW); break; } switch(focus) { case FOCUS_DATA: case FOCUS_IMAGE: case FOCUS_GEN: if(DRAW == mode) putchar('*'); else putchar(' '); } return; } int ImportSIRDSDATA(SIRDS *sirds) { SIRDSDATA *s; FILE *fp; char msg[256]; int c; int row, col; long totalbytes; int bytes; int firstline; int EndOfFile; int firstlinebytes; int lastlinebytes; int lines; int count; double dummy; long int datastart; double min, max; ZDATA *data; s = sirds->sd; clrscr(); sprintf(msg, "//Importing Data from Data File\n"); LOG(msg); if(NULL == s->filename) { LOG("Null Data File Name.\n"); return(1); } fp = fopen(s->filename, "rt"); if(NULL == fp) { LOG("Failed to Open Data File Successfully.\n"); return(2); } bytes = 0; totalbytes = 0; firstline = TRUE; EndOfFile = FALSE; lines = 0; datastart = ftell(fp); while(!EndOfFile) { c = fgetc(fp); bytes++; totalbytes++; switch(c) { case 10: case 13: lines++; if(firstline) { firstlinebytes = bytes; lines = 0; firstline = FALSE; datastart = ftell(fp); } else bytes = 0; break; case EOF: lastlinebytes = bytes-1; // Don't count the EOF EndOfFile = TRUE; break; } } sprintf(msg, "Summary of First Pass through File:\n"); LOG(msg); sprintf(msg, "Bytes on First Line: %i\n", firstlinebytes); LOG(msg); sprintf(msg, "Data Lines Found: %i\n", lines); LOG(msg); sprintf(msg, "Bytes on Last Line: %i\n", lastlinebytes); LOG(msg); sprintf(msg, "Total Bytes in File: %li\n", totalbytes); LOG(msg); if(0 != lastlinebytes) // Indicates last line doesn't have CR/LF at end { lines++; sprintf(msg, "Last line of data does not have CR/LF\n"); LOG(msg); sprintf(msg, "Number of Lines incremented to %i\n", lines); LOG(msg); } // Go back to start of data file rewind(fp); // Read Header Record LOG("Reading Header Record\n"); for(count = -1; datastart > ftell(fp); count++) { fscanf(fp, "%lf", &dummy); } sprintf(msg, "Number of entries on First Line: %i\n", count); LOG(msg); rewind(fp); switch(count) { case 1: // Only a column count on first line fscanf(fp, "%i", &(s->PointsX)); s->PointsY = lines; // Using CR/LF count s->SizeX = 25.0; // Default size s->Aspect = 1.0; // Default s->SizeY = 0.0; // Will be calculated after row count known break; case 2: // Only a column and row count on first line fscanf(fp, "%i", &(s->PointsX)); fscanf(fp, "%i", &(s->PointsY)); s->SizeX = 25.0; // Default size s->Aspect = 1.0; // Default s->SizeY = s->PointsY * (s->SizeX)/(s->Aspect * s->PointsX); break; case 3: // Column and row count plus horizontal size first line fscanf(fp, "%i", &(s->PointsX)); fscanf(fp, "%i", &(s->PointsY)); fscanf(fp, "%lf", &(s->SizeX)); s->Aspect = 1.0; // Default s->SizeY = s->PointsY * (s->SizeX)/(s->Aspect * s->PointsX); break; case 4: // Column and row count, horizontal size, and aspect ratio fscanf(fp, "%i", &(s->PointsX)); fscanf(fp, "%i", &(s->PointsY)); fscanf(fp, "%lf", &(s->SizeX)); fscanf(fp, "%lf", &(s->Aspect)); s->SizeY = s->PointsY * (s->SizeX)/(s->Aspect * s->PointsX); break; default: // First line is actual data, count is PointsX s->PointsX = count; s->PointsY = lines; // Using CR/LF count s->SizeX = 25.0; // Default size s->Aspect = 1.0; // Default s->SizeY = s->PointsY * (s->SizeX)/(s->Aspect * s->PointsX); break; } if(0 == s->PointsY) { LOG("Number of Rows can't be determined - aborting read.\n"); return(3); } if(s->PointsY != lines) { LOG("Row mismatch between header and CR/LF count - using header.\n"); } LOG("Scanning Data for min/max values\n"); datastart = ftell(fp); fscanf(fp, "%lf", &dummy); min=max=dummy; while(EOF != fscanf(fp, "%lf", &dummy)) { if(dummy < min) min = dummy; if(dummy > max) max = dummy; } sprintf(msg, "Data File Range: < %f, %f >\n", min, max); LOG(msg); LOG("Creating Data Structure to Store Memory\n"); data = malloc(sizeof(ZDATA)); data->rows = s->PointsY; data->cols = s->PointsX; sprintf(msg, "Allocating for %i Row Pointers.\n", data->rows); LOG(msg); data->data = malloc( data->rows*sizeof(UBYTE *) ); for(row = 0; row < data->rows; row++) { data->data[row] = malloc( data->cols*sizeof(UBYTE) ); } LOG("Reading in actual data\n"); fseek(fp, datastart, SEEK_SET); for(row = 0; row < data->rows; row++) for(col = 0; col < data->cols; col++) { if(1 != fscanf(fp, "%lf", &dummy)) LOG("Premature End of Data Encountered\n"); else { data->data[row][col] = (UBYTE) (255*((dummy-min)/(max-min))); } } if(1 == fscanf(fp, "%lf", &dummy)) LOG("Data Encountered beyond expected End of Array\n"); LOG("Finished Reading in Data - Updating non-initialized parameters\n"); s->data = data; return(0); } int GenGRD(char *filename) { double d, dy, dx; int row, col; int rows, cols; int sel; FILE *fp; cols = 640; rows = 480; clrscr(); printf("Choose which shape to generate:\n"); printf("[1] Flat Annular Ring\n"); printf("[2] Cone\n"); printf("[3] Sinc() Well\n"); printf("[4] Spiral Down\n"); printf("[5] \n"); sel = CatchKey(); fp = OpenAndVerify(filename, "wt"); fprintf(fp, "%i %i\n", cols, rows); for(row=0; row 100)) fprintf(fp, "%g ", 250.0); else fprintf(fp, "%g ", 0.0); break; case '2': if(d < 200) fprintf(fp, "%g ", (200-d)*(255/200)); else fprintf(fp, "%g ", 0.0); break; case '3': if(d < 250) fprintf(fp, "%g ", sin( (d+1)/25 ) / ( (d+1)/25 ) ); else fprintf(fp, "%g ", 0.0); break; case '4': if( (50 < d) && (d < 200) ) fprintf(fp, "%g ", floor((d-50)/75)+atan2(dy,dx)/(2.0*PI)); else if( 50 < d ) fprintf(fp, "%g ", 3.0); else fprintf(fp, "%g ", 0.0); break; } } fprintf(fp, "\n"); } fclose(fp); printf("Export Complete.\n"); return(0); } int GetLimitedInt(int min, int max) { char msg[255]; int i; scanf("%i", &i); if(i < min) { i = min; sprintf(msg, "Value less than min of (%i). Set to (%i)", min, i); DisplayERR(msg); } if(i > max) { i = max; sprintf(msg, "Value greater than max of (%i). Set to (%i)", max, i); DisplayERR(msg); } return(i); } double GetLimitedDouble(double min, double max) { char msg[255]; double x; scanf("%lf", &x); if(x < min) { x = min; sprintf(msg, "Value less than min of (%g). Set to (%g)", min, x); DisplayERR(msg); } if(x > max) { x = max; sprintf(msg, "Value greater than max of (%g). Set to (%g)", max, x); DisplayERR(msg); } return(x); }