From e4ca9f30991272859acfbe4a9f71f8caa21c1202 Mon Sep 17 00:00:00 2001 From: Gareth Evans Date: Mon, 30 Jan 2017 22:21:50 +0000 Subject: [PATCH 1/8] Add stdout support --- inputs.cc | 67 +++++++------ main.cc | 82 +++++++++------ models/los.cc | 10 +- outputs.cc | 272 ++++++++++++++++++++++++++++++-------------------- 4 files changed, 251 insertions(+), 180 deletions(-) diff --git a/inputs.cc b/inputs.cc index 9245766..8d034df 100644 --- a/inputs.cc +++ b/inputs.cc @@ -45,8 +45,8 @@ int loadClutter(char *filename, double radius, struct site tx) return 0; // can't work with this yet } if (debug) { - fprintf(stdout, "\nLoading clutter file \"%s\" %d x %d...\n", filename, w,h); - fflush(stdout); + fprintf(stderr, "\nLoading clutter file \"%s\" %d x %d...\n", filename, w,h); + fflush(stderr); } if (fgets(line, 25, fd) != NULL) { sscanf(pch, "%lf", &xll); @@ -58,8 +58,8 @@ int loadClutter(char *filename, double radius, struct site tx) } if (debug) { - fprintf(stdout, "\nxll %.2f yll %.2f\n", xll, yll); - fflush(stdout); + fprintf(stderr, "\nxll %.2f yll %.2f\n", xll, yll); + fflush(stderr); } fgets(line, 25, fd); // cellsize @@ -117,7 +117,7 @@ int loadClutter(char *filename, double radius, struct site tx) pch = strtok(NULL, " "); }//while } else { - fprintf(stdout, "Clutter error @ x %d y %d\n", x, y); + fprintf(stderr, "Clutter error @ x %d y %d\n", x, y); }//if }//for } @@ -204,7 +204,7 @@ void readLIDAR(FILE *fd, int h, int w, int indx,double n, double e, double s, do } else { - fprintf(stdout, "LIDAR error @ x %d y %d indx %d\n", + fprintf(stderr, "LIDAR error @ x %d y %d indx %d\n", x, y, indx); }//if }//for @@ -244,8 +244,8 @@ int loadLIDAR(char *filenames) width = atoi(pch); // ncols if (debug) { - fprintf(stdout, "Loading \"%s\" into page %d with width %d...\n", files[indx], indx, width); - fflush(stdout); + fprintf(stderr, "Loading \"%s\" into page %d with width %d...\n", files[indx], indx, width); + fflush(stderr); } if (fgets(line, 255, fd) != NULL) @@ -297,7 +297,7 @@ int loadLIDAR(char *filenames) westoffset = xll; if (debug) - fprintf(stdout,"%d, %d, %.7f, %.7f, %.7f, %.7f, %.7f\n",width,height,xll,yll,cellsize,yur,xur); + fprintf(stderr,"%d, %d, %.7f, %.7f, %.7f, %.7f, %.7f\n",width,height,xll,yll,cellsize,yur,xur); // Greenwich straddling hack @@ -317,7 +317,7 @@ int loadLIDAR(char *filenames) xur = xur * -1; } if (debug) - fprintf(stdout, "POST yll %.7f yur %.7f xur %.7f xll %.7f delta %.6f\n", yll, yur, xur, xll, delta); + fprintf(stderr, "POST yll %.7f yur %.7f xur %.7f xll %.7f delta %.6f\n", yll, yur, xur, xll, delta); fgets(line, 255, fd); // NODATA @@ -325,13 +325,13 @@ int loadLIDAR(char *filenames) // tile 0 [x| ] if (debug) - fprintf(stdout, "readLIDAR(fd,%d,%d,%d,%.4f,%.4f,%.4f,%.4f)\n", height, width, indx, yur, xur, yll, xll); + fprintf(stderr, "readLIDAR(fd,%d,%d,%d,%.4f,%.4f,%.4f,%.4f)\n", height, width, indx, yur, xur, yll, xll); readLIDAR(fd, height, width, indx, yur, xur, yll, xll); fclose(fd); if (debug) - fprintf(stdout, "LIDAR LOADED %d x %d\n", width, height); + fprintf(stderr, "LIDAR LOADED %d x %d\n", width, height); } else { return -1; } @@ -343,7 +343,7 @@ int loadLIDAR(char *filenames) width = (unsigned)((max_west-min_west) / cellsize); if (debug) - fprintf(stdout, "fc %d WIDTH %d HEIGHT %d ippd %d minN %.5f maxN %.5f minW %.5f maxW %.5f avgCellsize %.5f\n", fc, width, height, ippd,min_north,max_north,min_west,max_west,avgCellsize); + fprintf(stderr, "fc %d WIDTH %d HEIGHT %d ippd %d minN %.5f maxN %.5f minW %.5f maxW %.5f avgCellsize %.5f\n", fc, width, height, ippd,min_north,max_north,min_west,max_west,avgCellsize); return 0; } @@ -417,10 +417,10 @@ int LoadSDF_SDF(char *name) if (fd != NULL) { if (debug == 1) { - fprintf(stdout, + fprintf(stderr, "Loading \"%s\" into page %d...", path_plus_name, indx + 1); - fflush(stdout); + fflush(stderr); } if (fgets(line, 19, fd) != NULL) { @@ -590,10 +590,10 @@ char LoadSDF(char *name) if (free_page && found == 0 && indx >= 0 && indx < MAXPAGES) { if (debug == 1) { - fprintf(stdout, - "Region \"%s\" assumed as sea-level into page %d...", + fprintf(stderr, + "Region \"%s\" assumed as sea-level into page %d...\n", name, indx + 1); - fflush(stdout); + fflush(stderr); } dem[indx].max_west = maxlon; @@ -720,7 +720,7 @@ void LoadPAT(char *filename) from true North. */ if (fgets(string, 254, fd) == NULL) { - //fprintf(stdout,"Azimuth read error\n"); + //fprintf(stderr,"Azimuth read error\n"); //exit(0); } pointer = strchr(string, ';'); @@ -735,7 +735,7 @@ void LoadPAT(char *filename) (0.0 to 1.0) until EOF is reached. */ if (fgets(string, 254, fd) == NULL) { - //fprintf(stdout,"Azimuth read error\n"); + //fprintf(stderr,"Azimuth read error\n"); //exit(0); } pointer = strchr(string, ';'); @@ -754,7 +754,7 @@ void LoadPAT(char *filename) } if (fgets(string, 254, fd) == NULL) { - //fprintf(stdout,"Azimuth read error\n"); + //fprintf(stderr,"Azimuth read error\n"); // exit(0); } pointer = strchr(string, ';'); @@ -850,7 +850,7 @@ void LoadPAT(char *filename) clockwise from true North. */ if (fgets(string, 254, fd) == NULL) { - //fprintf(stdout,"Tilt read error\n"); + //fprintf(stderr,"Tilt read error\n"); //exit(0); } pointer = strchr(string, ';'); @@ -865,7 +865,7 @@ void LoadPAT(char *filename) (0.0 to 1.0) until EOF is reached. */ if (fgets(string, 254, fd) == NULL) { - //fprintf(stdout,"Ant elevation read error\n"); + //fprintf(stderr,"Ant elevation read error\n"); //exit(0); } pointer = strchr(string, ';'); @@ -1108,8 +1108,9 @@ void LoadSignalColors(struct site xmtr) fd = fopen(filename, "r"); - if (fd == NULL) - fd = fopen(filename, "r"); + if (fd == NULL && xmtr.filename[0] == '\0') + /* Don't save if we don't have an output file */ + return; if (fd == NULL) { fd = fopen(filename, "w"); @@ -1261,8 +1262,9 @@ void LoadLossColors(struct site xmtr) fd = fopen(filename, "r"); - if (fd == NULL) - fd = fopen(filename, "r"); + if (fd == NULL && xmtr.filename[0] == '\0') + /* Don't save if we don't have an output file */ + return; if (fd == NULL) { fd = fopen(filename, "w"); @@ -1414,8 +1416,9 @@ void LoadDBMColors(struct site xmtr) fd = fopen(filename, "r"); - if (fd == NULL) - fd = fopen(filename, "r"); + if (fd == NULL && xmtr.filename[0] == '\0') + /* Don't save if we don't have an output file */ + return; if (fd == NULL) { fd = fopen(filename, "w"); @@ -1678,10 +1681,10 @@ void LoadUDT(char *filename) if (z == 0) /* No duplicate found */ - //fprintf(stdout,"%lf, %lf \n",xpix*dpp, ypix*dpp); - fflush(stdout); + //fprintf(stderr,"%lf, %lf \n",xpix*dpp, ypix*dpp); + fflush(stderr); AddElevation(xpix * dpp, ypix * dpp, height, 1); - fflush(stdout); + fflush(stderr); n = fscanf(fd1, "%d, %d, %lf", &xpix, &ypix, &height); y++; diff --git a/main.cc b/main.cc index 20bb00c..d5c2902 100644 --- a/main.cc +++ b/main.cc @@ -27,6 +27,7 @@ double version = 2.95; #include #include #include +#include #include "common.h" #include "inputs.hh" @@ -54,6 +55,8 @@ int ippd, mpi, unsigned char got_elevation_pattern, got_azimuth_pattern, metric = 0, dbm = 0; +bool to_stdout = false; + __thread double *elev; __thread struct path path; struct site tx_site[2]; @@ -1197,7 +1200,13 @@ int main(int argc, char *argv[]) strncpy(tx_site[0].name, "Tx", 2); strncpy(tx_site[0].filename, argv[z], 253); LoadPAT(argv[z]); - + } else if (z <= y && argv[z][0] && argv[z][0] == '-' && argv[z][1] == '\0' ) { + /* Handle writing image data to stdout */ + to_stdout = true; + mapfile[0] = '\0'; + strncpy(tx_site[0].name, "Tx", 2); + tx_site[0].filename[0] = '\0'; + fprintf(stderr,"Writing to stdout\n"); } } @@ -1497,66 +1506,72 @@ int main(int argc, char *argv[]) /* ERROR DETECTION */ if (tx_site[0].lat > 90 || tx_site[0].lat < -90) { - fprintf(stdout, + fprintf(stderr, "ERROR: Either the lat was missing or out of range!"); - exit(0); + exit(EINVAL); } if (tx_site[0].lon > 360 || tx_site[0].lon < 0) { - fprintf(stdout, + fprintf(stderr, "ERROR: Either the lon was missing or out of range!"); - exit(0); + exit(EINVAL); } if (LR.frq_mhz < 20 || LR.frq_mhz > 100000) { - fprintf(stdout, + fprintf(stderr, "ERROR: Either the Frequency was missing or out of range!"); - exit(0); + exit(EINVAL); } if (LR.erp > 500000000) { - fprintf(stdout, "ERROR: Power was out of range!"); - exit(0); + fprintf(stderr, "ERROR: Power was out of range!"); + exit(EINVAL); } if (LR.eps_dielect > 80 || LR.eps_dielect < 0.1) { - fprintf(stdout, "ERROR: Ground Dielectric value out of range!"); - exit(0); + fprintf(stderr, "ERROR: Ground Dielectric value out of range!"); + exit(EINVAL); } if (LR.sgm_conductivity > 0.01 || LR.sgm_conductivity < 0.000001) { - fprintf(stdout, "ERROR: Ground conductivity out of range!"); - exit(0); + fprintf(stderr, "ERROR: Ground conductivity out of range!"); + exit(EINVAL); } if (tx_site[0].alt < 0 || tx_site[0].alt > 60000) { - fprintf(stdout, + fprintf(stderr, "ERROR: Tx altitude above ground was too high: %f", tx_site[0].alt); - exit(0); + exit(EINVAL); } if (altitudeLR < 0 || altitudeLR > 60000) { - fprintf(stdout, + fprintf(stderr, "ERROR: Rx altitude above ground was too high!"); - exit(0); + exit(EINVAL); } if(!lidar){ if (ippd < 300 || ippd > 10000) { - fprintf(stdout, "ERROR: resolution out of range!"); - exit(0); + fprintf(stderr, "ERROR: resolution out of range!"); + exit(EINVAL); } } if (contour_threshold < -200 || contour_threshold > 200) { - fprintf(stdout, + fprintf(stderr, "ERROR: Receiver threshold out of range (-200 / +200)"); - exit(0); + exit(EINVAL); } if (propmodel > 2 && propmodel < 7 && LR.frq_mhz < 150) { - fprintf(stdout, + fprintf(stderr, "ERROR: Frequency too low for Propagation model"); - exit(0); + exit(EINVAL); + } + + if (to_stdout == true && ppa != 0) { + fprintf(stderr, + "ERROR: Cannot write to stdout in ppa mode"); + exit(EINVAL); } if (metric) { @@ -1627,7 +1642,7 @@ int main(int argc, char *argv[]) err = loadLIDAR(lidar_tiles); if (err) { - fprintf(stdout, "Couldn't find one or more of the " + fprintf(stderr, "Couldn't find one or more of the " "lidar files. Please ensure their paths are " "correct and try again.\n"); exit(EXIT_FAILURE); @@ -1635,7 +1650,7 @@ int main(int argc, char *argv[]) if(debug){ - fprintf(stdout,"%.4f,%.4f,%.4f,%.4f,%d x %d\n",max_north,min_west,min_north,max_west,width,height); + fprintf(stderr,"%.4f,%.4f,%.4f,%.4f,%d x %d\n",max_north,min_west,min_north,max_west,width,height); } ppd=rint(height / (max_north-min_north)); yppd=rint(width / (max_west-min_west)); @@ -1768,7 +1783,7 @@ int main(int argc, char *argv[]) propmodel, knifeedge, haf, pmenv, use_threads); if(debug) - fprintf(stdout,"Finished PlotPropagation()\n"); + fprintf(stderr,"Finished PlotPropagation()\n"); if(!lidar){ if (LR.erp == 0.0) @@ -1786,13 +1801,13 @@ int main(int argc, char *argv[]) } } - + // Write bitmap if (LR.erp == 0.0) DoPathLoss(mapfile, geo, kml, ngs, tx_site, txsites); else if (dbm) - DoRxdPwr(mapfile, geo, kml, ngs, tx_site, + DoRxdPwr((to_stdout == true ? NULL : mapfile), geo, kml, ngs, tx_site, txsites); else DoSigStr(mapfile, geo, kml, ngs, tx_site, @@ -1805,10 +1820,11 @@ int main(int argc, char *argv[]) } // Print WGS84 bounds - fprintf(stdout, "|%.6f", north); - fprintf(stdout, "|%.6f", east); - fprintf(stdout, "|%.6f", south); - fprintf(stdout, "|%.6f|", west); + fprintf(stderr, "|%.6f", north); + fprintf(stderr, "|%.6f", east); + fprintf(stderr, "|%.6f", south); + fprintf(stderr, "|%.6f|", west); + fprintf(stderr, "\n"); } else { strncpy(tx_site[0].name, "Tx", 3); @@ -1819,7 +1835,7 @@ int main(int argc, char *argv[]) SeriesData(tx_site[1], tx_site[0], tx_site[0].filename, 1, normalise); } - fflush(stdout); + fflush(stderr); return 0; } diff --git a/models/los.cc b/models/los.cc index 7b47ff4..d4655f8 100644 --- a/models/los.cc +++ b/models/los.cc @@ -766,17 +766,17 @@ void PlotPropagation(struct site source, double altitude, char *plo_filename, FILE *fd = NULL; if (LR.erp == 0.0 && debug) - fprintf(stdout, "path loss"); + fprintf(stderr, "path loss"); else { if (debug) { if (dbm) - fprintf(stdout, "signal power level"); + fprintf(stderr, "signal power level"); else - fprintf(stdout, "field strength"); + fprintf(stderr, "field strength"); } } if (debug) { - fprintf(stdout, + fprintf(stderr, " contours of \"%s\"\nout to a radius of %.2f %s with Rx antenna(s) at %.2f %s AGL\n", source.name, metric ? max_range * KM_PER_MILE : max_range, @@ -786,7 +786,7 @@ void PlotPropagation(struct site source, double altitude, char *plo_filename, } if (clutter > 0.0 && debug) - fprintf(stdout, "\nand %.2f %s of ground clutter", + fprintf(stderr, "\nand %.2f %s of ground clutter", metric ? clutter * METERS_PER_FOOT : clutter, metric ? "meters" : "feet"); diff --git a/outputs.cc b/outputs.cc index 4e4ed95..77b4a8c 100644 --- a/outputs.cc +++ b/outputs.cc @@ -38,29 +38,40 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, LoadLossColors(xmtr[0]); - if (filename[0] == 0) { - strncpy(filename, xmtr[0].filename, 254); - filename[strlen(filename) - 4] = 0; /* Remove .qth */ + if( filename != NULL ) { + + if (filename[0] == 0) { + strncpy(filename, xmtr[0].filename, 254); + filename[strlen(filename) - 4] = 0; /* Remove .qth */ + } + + y = strlen(filename); + + if (y > 4) { + if (filename[y - 1] == 'm' && filename[y - 2] == 'p' + && filename[y - 3] == 'p' && filename[y - 4] == '.') + y -= 4; + } + + for (x = 0; x < y; x++) { + mapfile[x] = filename[x]; + } + + mapfile[x] = '.'; + mapfile[x + 1] = 'p'; + mapfile[x + 2] = 'p'; + mapfile[x + 3] = 'm'; + mapfile[x + 4] = 0; + + fd = fopen(mapfile,"wb"); + + } else { + + fprintf(stderr,"Writing to stdout\n"); + fd = stdout; + } - y = strlen(filename); - - if (y > 4) { - if (filename[y - 1] == 'm' && filename[y - 2] == 'p' - && filename[y - 3] == 'p' && filename[y - 4] == '.') - y -= 4; - } - - for (x = 0; x < y; x++) { - mapfile[x] = filename[x]; - } - - mapfile[x] = '.'; - mapfile[x + 1] = 'p'; - mapfile[x + 2] = 'p'; - mapfile[x + 3] = 'm'; - mapfile[x + 4] = 0; - minwest = ((double)min_west) + dpp; if (minwest > 360.0) @@ -76,14 +87,13 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, east = (minwest < 180.0 ? -minwest : 360.0 - min_west); west = (double)(max_west < 180 ? -max_west : 360 - max_west); - fd = fopen(mapfile, "wb"); - fprintf(fd, "P6\n%u %u\n255\n", width, (kml ? height : height + 30)); if (debug) { - fprintf(stdout, "\nWriting \"%s\" (%ux%u pixmap image)... ", - mapfile, width, (kml ? height : height + 30)); - fflush(stdout); + fprintf(stderr, "\nWriting \"%s\" (%ux%u pixmap image)...\n", + filename != NULL ? mapfile : "to stdout", width, (kml ? height : height + 30)); + fflush(stderr); } + for (y = 0, lat = north; y < (int)height; y++, lat = north - (dpp * (double)y)) { for (x = 0, lon = max_west; x < (int)width; @@ -242,7 +252,10 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, } } - fclose(fd); + if( filename != NULL ) { + fclose(fd); + fd = NULL; + } } @@ -269,29 +282,40 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml, LoadSignalColors(xmtr[0]); - if (filename[0] == 0) { - strncpy(filename, xmtr[0].filename, 254); - filename[strlen(filename) - 4] = 0; /* Remove .qth */ + if( filename != NULL ) { + + if (filename[0] == 0) { + strncpy(filename, xmtr[0].filename, 254); + filename[strlen(filename) - 4] = 0; /* Remove .qth */ + } + + y = strlen(filename); + + if (y > 4) { + if (filename[y - 1] == 'm' && filename[y - 2] == 'p' + && filename[y - 3] == 'p' && filename[y - 4] == '.') + y -= 4; + } + + for (x = 0; x < y; x++) { + mapfile[x] = filename[x]; + } + + mapfile[x] = '.'; + mapfile[x + 1] = 'p'; + mapfile[x + 2] = 'p'; + mapfile[x + 3] = 'm'; + mapfile[x + 4] = 0; + + fd = fopen(mapfile,"wb"); + + } else { + + fprintf(stderr,"Writing to stdout\n"); + fd = stdout; + } - y = strlen(filename); - - if (y > 4) { - if (filename[y - 1] == 'm' && filename[y - 2] == 'p' - && filename[y - 3] == 'p' && filename[y - 4] == '.') - y -= 4; - } - - for (x = 0; x < y; x++) { - mapfile[x] = filename[x]; - } - - mapfile[x] = '.'; - mapfile[x + 1] = 'p'; - mapfile[x + 2] = 'p'; - mapfile[x + 3] = 'm'; - mapfile[x + 4] = 0; - minwest = ((double)min_west) + dpp; if (minwest > 360.0) @@ -303,14 +327,14 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml, east = (minwest < 180.0 ? -minwest : 360.0 - min_west); west = (double)(max_west < 180 ? -max_west : 360 - max_west); - fd = fopen(mapfile, "wb"); fprintf(fd, "P6\n%u %u\n255\n", width, (kml ? height : height + 30)); if (debug) { - fprintf(stdout, "\nWriting \"%s\" (%ux%u pixmap image)... ", - mapfile, width, (kml ? height : height + 30)); - fflush(stdout); + fprintf(stderr, "\nWriting \"%s\" (%ux%u pixmap image)...\n", + filename != NULL ? mapfile : "to stdout", width, (kml ? height : height + 30)); + fflush(stderr); } + for (y = 0, lat = north; y < (int)height; y++, lat = north - (dpp * (double)y)) { for (x = 0, lon = max_west; x < (int)width; @@ -483,7 +507,10 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml, } } - fclose(fd); + if( filename != NULL ) { + fclose(fd); + fd = NULL; + } } @@ -510,29 +537,40 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, LoadDBMColors(xmtr[0]); - if (filename[0] == 0) { - strncpy(filename, xmtr[0].filename, 254); - filename[strlen(filename) - 4] = 0; /* Remove .qth */ + if( filename != NULL ) { + + if (filename[0] == 0) { + strncpy(filename, xmtr[0].filename, 254); + filename[strlen(filename) - 4] = 0; /* Remove .qth */ + } + + y = strlen(filename); + + if (y > 4) { + if (filename[y - 1] == 'm' && filename[y - 2] == 'p' + && filename[y - 3] == 'p' && filename[y - 4] == '.') + y -= 4; + } + + for (x = 0; x < y; x++) { + mapfile[x] = filename[x]; + } + + mapfile[x] = '.'; + mapfile[x + 1] = 'p'; + mapfile[x + 2] = 'p'; + mapfile[x + 3] = 'm'; + mapfile[x + 4] = 0; + + fd = fopen(mapfile,"wb"); + + } else { + + fprintf(stderr,"Writing to stdout\n"); + fd = stdout; + } - y = strlen(filename); - - if (y > 4) { - if (filename[y - 1] == 'm' && filename[y - 2] == 'p' - && filename[y - 3] == 'p' && filename[y - 4] == '.') - y -= 4; - } - - for (x = 0; x < y; x++) { - mapfile[x] = filename[x]; - } - - mapfile[x] = '.'; - mapfile[x + 1] = 'p'; - mapfile[x + 2] = 'p'; - mapfile[x + 3] = 'm'; - mapfile[x + 4] = 0; - minwest = ((double)min_west) + dpp; if (minwest > 360.0) @@ -545,13 +583,11 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, east = (minwest < 180.0 ? -minwest : 360.0 - min_west); west = (double)(max_west < 180 ? -max_west : 360 - max_west); - fd = fopen(mapfile, "wb"); - fprintf(fd, "P6\n%u %u\n255\n", width, (kml ? height : height)); if (debug) { - fprintf(stdout, "\nWriting \"%s\" (%ux%u pixmap image)...\n", - mapfile, width, (kml ? height : height)); - fflush(stdout); + fprintf(stderr, "\nWriting \"%s\" (%ux%u pixmap image)...\n", + (filename != NULL ? mapfile : "to stdout"), width, (kml ? height : height)); + fflush(stderr); } // Draw image of x by y pixels @@ -719,8 +755,12 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, } } + fflush(fd); - fclose(fd); + if( filename != NULL ) { + fclose(fd); + fd = NULL; + } } @@ -745,29 +785,40 @@ void DoLOS(char *filename, unsigned char geo, unsigned char kml, 255.0 / pow((double)(max_elevation - min_elevation), one_over_gamma); - if (filename[0] == 0) { - strncpy(filename, xmtr[0].filename, 254); - filename[strlen(filename) - 4] = 0; /* Remove .qth */ + if( filename != NULL ){ + + if (filename[0] == 0) { + strncpy(filename, xmtr[0].filename, 254); + filename[strlen(filename) - 4] = 0; /* Remove .qth */ + } + + y = strlen(filename); + + if (y > 4) { + if (filename[y - 1] == 'm' && filename[y - 2] == 'p' + && filename[y - 3] == 'p' && filename[y - 4] == '.') + y -= 4; + } + + for (x = 0; x < y; x++) { + mapfile[x] = filename[x]; + } + + mapfile[x] = '.'; + mapfile[x + 1] = 'p'; + mapfile[x + 2] = 'p'; + mapfile[x + 3] = 'm'; + mapfile[x + 4] = 0; + + fd = fopen(mapfile,"wb"); + + } else { + + fprintf(stderr,"Writing to stdout\n"); + fd = stdout; + } - y = strlen(filename); - - if (y > 4) { - if (filename[y - 1] == 'm' && filename[y - 2] == 'p' - && filename[y - 3] == 'p' && filename[y - 4] == '.') - y -= 4; - } - - for (x = 0; x < y; x++) { - mapfile[x] = filename[x]; - } - - mapfile[x] = '.'; - mapfile[x + 1] = 'p'; - mapfile[x + 2] = 'p'; - mapfile[x + 3] = 'm'; - mapfile[x + 4] = 0; - minwest = ((double)min_west) + dpp; if (minwest > 360.0) @@ -780,13 +831,11 @@ void DoLOS(char *filename, unsigned char geo, unsigned char kml, east = (minwest < 180.0 ? -minwest : 360.0 - min_west); west = (double)(max_west < 180 ? -max_west : 360 - max_west); - fd = fopen(mapfile, "wb"); - fprintf(fd, "P6\n%u %u\n255\n", width, (kml ? height : height + 30)); if (debug) { - fprintf(stdout, "\nWriting \"%s\" (%ux%u pixmap image)... ", - mapfile, width, (kml ? height : height + 30)); - fflush(stdout); + fprintf(stderr, "\nWriting \"%s\" (%ux%u pixmap image)...\n", + filename != NULL ? mapfile : "to stdout", width, (kml ? height : height + 30)); + fflush(stderr); } for (y = 0, lat = north; y < (int)height; @@ -954,7 +1003,10 @@ void DoLOS(char *filename, unsigned char geo, unsigned char kml, } } - fclose(fd); + if( filename != NULL) { + fclose(fd); + fd = NULL; + } } @@ -1622,7 +1674,7 @@ void PathReport(struct site source, struct site destination, char *name, fclose(fd2); - fprintf(stdout, + fprintf(stderr, "Path loss (dB), Received Power (dBm), Field strength (dBuV):\n%.1f\n%.1f\n%.1f", loss, dBm, field_strength); @@ -1992,7 +2044,7 @@ void SeriesData(struct site source, struct site destination, char *name, } } - fprintf(stdout, "\n"); - fflush(stdout); + fprintf(stderr, "\n"); + fflush(stderr); } From 29df12461b6d28198adcd9756cbf2ecd1f3111bc Mon Sep 17 00:00:00 2001 From: Gareth Evans Date: Wed, 1 Feb 2017 15:00:37 +0000 Subject: [PATCH 2/8] Move error messages to stderr --- models/cost.cc | 4 ++-- models/ecc33.cc | 2 +- models/ericsson.cc | 4 ++-- models/los.cc | 4 ++-- models/sui.cc | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/models/cost.cc b/models/cost.cc index 1936900..cc23a61 100644 --- a/models/cost.cc +++ b/models/cost.cc @@ -28,8 +28,8 @@ modes 1 = URBAN, 2 = SUBURBAN, 3 = OPEN http://morse.colorado.edu/~tlen5510/text/classwebch3.html */ if (f < 150 || f > 2000) { - printf - ("Error: COST231 Hata model frequency range 150-2000MHz\n"); + fprintf + (stderr,"Error: COST231 Hata model frequency range 150-2000MHz\n"); exit(EXIT_FAILURE); } diff --git a/models/ecc33.cc b/models/ecc33.cc index 642234f..c08cb0c 100644 --- a/models/ecc33.cc +++ b/models/ecc33.cc @@ -11,7 +11,7 @@ double ECC33pathLoss(float f, float TxH, float RxH, float d, int mode) } if (f < 700 || f > 3500) { - printf("Error: ECC33 model frequency range 700-3500MHz\n"); + fprintf(stderr,"Error: ECC33 model frequency range 700-3500MHz\n"); exit(EXIT_FAILURE); } // MHz to GHz diff --git a/models/ericsson.cc b/models/ericsson.cc index 0082958..66c8d20 100644 --- a/models/ericsson.cc +++ b/models/ericsson.cc @@ -12,8 +12,8 @@ double EricssonpathLoss(float f, float TxH, float RxH, float d, int mode) double a0 = 36.2, a1 = 30.2, a2 = -12, a3 = 0.1; if (f < 150 || f > 3500) { - printf - ("Error: Ericsson9999 model frequency range 150-3500MHz\n"); + fprintf + (stderr,"Error: Ericsson9999 model frequency range 150-3500MHz\n"); exit(EXIT_FAILURE); } diff --git a/models/los.cc b/models/los.cc index d4655f8..778e017 100644 --- a/models/los.cc +++ b/models/los.cc @@ -150,7 +150,7 @@ namespace { int rc = pthread_create(&threads[thread_count], NULL, rangePropagation, arg); if (rc) - printf("ERROR; return code from pthread_create() is %d\n", rc); + fprintf(stderr,"ERROR; return code from pthread_create() is %d\n", rc); else ++thread_count; } @@ -161,7 +161,7 @@ namespace { for(unsigned int i=0; i 11000) { - printf("Error: SUI model frequency range 1.9-11GHz\n"); + fprintf(stderr,"Error: SUI model frequency range 1.9-11GHz\n"); exit(EXIT_FAILURE); } // Terrain mode A is default From 93aa2e472a04a7050320136faf19a511f4e4324f Mon Sep 17 00:00:00 2001 From: Gareth Evans Date: Wed, 1 Feb 2017 23:44:00 +0000 Subject: [PATCH 3/8] Add genericised image handling and implement in DoRxdPwr --- Makefile | 8 ++- image-ppm.cc | 61 ++++++++++++++++ image-ppm.hh | 13 ++++ image.cc | 199 +++++++++++++++++++++++++++++++++++++++++++++++++++ image.hh | 42 +++++++++++ main.cc | 9 +++ outputs.cc | 62 +++++++--------- 7 files changed, 357 insertions(+), 37 deletions(-) create mode 100644 image-ppm.cc create mode 100644 image-ppm.hh create mode 100644 image.cc create mode 100644 image.hh diff --git a/Makefile b/Makefile index a82615a..65d44ff 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,11 @@ CC = gcc CXX = g++ CFLAGS = -Wall -O3 -s -ffast-math CXXFLAGS = -Wall -O3 -s -ffast-math -LIBS = -lm -lpthread +LIBS = -lm -lpthread -ldl VPATH = models objects = main.o cost.o ecc33.o ericsson.o fspl.o hata.o itwom3.0.o \ - los.o sui.o pel.o inputs.o outputs.o + los.o sui.o pel.o inputs.o outputs.o image.o image-ppm.o GCC_MAJOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d . -f 1) GCC_MINOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d . -f 2) @@ -45,6 +45,10 @@ inputs.o: inputs.cc common.h main.hh outputs.o: outputs.cc common.h inputs.hh main.hh cost.hh ecc33.hh ericsson.hh \ fspl.hh hata.hh itwom3.0.hh sui.hh pel.hh +image.o: image.cc image-ppm.o + +image-ppm.o: image-ppm.cc + los.o: los.cc common.h main.hh cost.hh ecc33.hh ericsson.hh fspl.hh hata.hh \ itwom3.0.hh sui.hh pel.hh diff --git a/image-ppm.cc b/image-ppm.cc new file mode 100644 index 0000000..f37bbe2 --- /dev/null +++ b/image-ppm.cc @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include "image.hh" + +int ppm_init(PIMAGE_CTX ctx){ + size_t buf_size; + + /* Perform simple sanity checking */ + if(ctx->canvas != NULL) + return EINVAL; + ctx->model = IMAGE_RGB; //Override this as we only support RGB + ctx->format = IMAGE_PPM; + ctx->extension = ".ppm"; + + buf_size = ctx->width * ctx->height * RGB_SIZE; + + ctx->canvas = (uint8_t*) calloc(buf_size,sizeof(uint8_t)); + ctx->next_pixel = ctx->canvas; + if(ctx->canvas == NULL) + return ENOMEM; + + return 0; +} + +int ppm_add_pixel(PIMAGE_CTX ctx,const uint8_t r,const uint8_t g,const uint8_t b,const uint8_t a){ + register uint8_t* next; + + next = ctx->next_pixel; + + next[0] = r; + next[1] = g; + next[2] = b; + /*if(ctx->model == IMAGE_RGBA){ + next[3] = a; + ctx->next_pixel += 1; + }*/ + ctx->next_pixel += 3; + + return 0; +} + +int ppm_get_pixel(PIMAGE_CTX ctx,const size_t x,const size_t y,const uint8_t *r,const uint8_t *g,const uint8_t *b,const uint8_t *a){ + /* STUB */ + return 0; +} + +int ppm_write(PIMAGE_CTX ctx, FILE* fd){ + size_t written; + size_t count; + + count = ctx->width * ctx->height * RGB_SIZE; + + fprintf(fd, "P6\n%zu %zu\n255\n", ctx->width, ctx->height); + written = fwrite(ctx->canvas,sizeof(uint8_t),count,fd); + if(written < count) + return EPIPE; + + return 0; +} \ No newline at end of file diff --git a/image-ppm.hh b/image-ppm.hh new file mode 100644 index 0000000..4831f0a --- /dev/null +++ b/image-ppm.hh @@ -0,0 +1,13 @@ +#ifndef _IMAGE_PPM_HH +#define _IMAGE_PPM_HH + +#include +#include +#include + +int ppm_init(PIMAGE_CTX ctx); +int ppm_add_pixel(PIMAGE_CTX ctx,const uint8_t r,const uint8_t g,const uint8_t b,const uint8_t a); +int ppm_get_pixel(PIMAGE_CTX ctx,const size_t x,const size_t y,const uint8_t *r,const uint8_t *g,const uint8_t *b,const uint8_t *a); +int ppm_write(PIMAGE_CTX ctx, FILE* fd); + +#endif \ No newline at end of file diff --git a/image.cc b/image.cc new file mode 100644 index 0000000..467e655 --- /dev/null +++ b/image.cc @@ -0,0 +1,199 @@ +#include +#include +#include +#include +#include +#include +#include "image.hh" +#include "image-ppm.hh" + +typedef int _init(PIMAGE_CTX); +typedef int _add_pixel(PIMAGE_CTX,const uint8_t,const uint8_t,const uint8_t,const uint8_t); +typedef int _get_pixel(PIMAGE_CTX,const size_t,const size_t,const uint8_t*,const uint8_t*,const uint8_t*,const uint8_t*); +typedef int _write(PIMAGE_CTX,FILE*); + +struct image_dispatch_table{ + _init *init; + _add_pixel *add_pixel; + _get_pixel *get_pixel; + _write *write; +}; + +static IMAGE_FORMAT format = IMAGE_PPM; +char *dynamic_backend = NULL; + +int load_library(struct image_dispatch_table *dt){ + void *hndl; + int success = 0; + + if(dynamic_backend == NULL){ + fprintf(stderr,"Custom image processor requested without specification\n"); + return EINVAL; + } + + hndl = dlopen(dynamic_backend,RTLD_LAZY); + if(hndl == NULL){ + fprintf(stderr,"Error loading shared object\n"); + return EINVAL; + } + + dt->init = (_init*)dlsym(hndl,"lib_init"); + dt->add_pixel = (_add_pixel*)dlsym(hndl,"lib_add_pixel"); + dt->get_pixel = (_get_pixel*)dlsym(hndl,"lib_get_pixel"); + dt->write = (_write*)dlsym(hndl,"lib_write"); + + if(dt->init == NULL || + dt->add_pixel == NULL || + dt->get_pixel == NULL || + dt->write == NULL){ + fprintf(stderr,"Invalid image processing module specified\n"); + success = dlclose(hndl); + } + + return success; +} + +int get_dt(struct image_dispatch_table *dt, IMAGE_FORMAT format){ + int success = 0; + + memset((void*)dt,0x00,sizeof(struct image_dispatch_table)); + switch(format){ + case IMAGE_PPM: + dt->init = ppm_init; + dt->add_pixel = ppm_add_pixel; + dt->get_pixel = ppm_get_pixel; + dt->write = ppm_write; + break; + case IMAGE_LIBRARY: + success = load_library(dt); + break; + default: + success = EINVAL; + } + return success; +} + +int image_init(PIMAGE_CTX ctx, \ + const size_t width, \ + const size_t height, \ + const IMAGE_MODEL model) { + + int success = 0; + struct image_dispatch_table *dt; + + /* Perform some sanity checking on provided arguments */ + if(ctx == NULL) + return EINVAL; + if(width == 0 || height == 0) + return EINVAL; + if(model < 0 || model > IMAGE_MODEL_MAX) + return EINVAL; + if(format < 0 || format > IMAGE_FORMAT_MAX) + return EINVAL; + + memset(ctx,0x00,sizeof(IMAGE_CTX)); + + /* Assign the initialize values to the processing context */ + ctx->width = width; + ctx->height = height; + ctx->model = model; + ctx->format = format; + + /* Get the dispatch table for this image format */ + dt = (struct image_dispatch_table*) calloc(1,sizeof(struct image_dispatch_table)); + if(dt == NULL){ + fprintf(stderr,"Error allocating dispatch table\n"); + return ENOMEM; + } + success = get_dt(dt,format); + if(success != 0){ + fprintf(stderr,"Error locating dispatch table\n"); + free(dt); + return success; + } + ctx->_dt = (void*)dt; + + /* Call the format-specific initialization function */ + success = dt->init(ctx); + if(success != 0){ + fprintf(stderr,"Error initializing image context\n"); + free(dt); + return success; + } + + ctx->initialized = 1; + + return success; +} +int image_add_pixel(PIMAGE_CTX ctx, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a){ + struct image_dispatch_table *dt = (struct image_dispatch_table*)ctx->_dt; + return dt->add_pixel(ctx,r,g,b,a); +} +int image_set_pixel(PIMAGE_CTX ctx, const size_t x, const size_t y, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a){ + size_t block_size; + block_size = ctx->model == IMAGE_RGB ? RGB_SIZE : RGBA_SIZE; + ctx->next_pixel = ctx->canvas + (x * block_size) + (ctx->width * block_size * y); + struct image_dispatch_table *dt = (struct image_dispatch_table*)ctx->_dt; + return dt->add_pixel(ctx,r,g,b,a); +} +int image_get_pixel(PIMAGE_CTX ctx, const size_t x, const size_t y, const uint8_t *r, const uint8_t *g, const uint8_t *b, const uint8_t *a){ + struct image_dispatch_table *dt = (struct image_dispatch_table*)ctx->_dt; + return dt->get_pixel(ctx,x,y,r,g,b,a); +} +int image_get_filename(PIMAGE_CTX ctx, char *out, size_t len_out, char *in){ + size_t len_src; + size_t len_ext; + int success = 0; + + if(ctx->initialized != 1){ + fprintf(stderr,"Called get filename before initialization\n"); + return EINVAL; + } + + len_src = strlen(in); + len_ext = strlen(ctx->extension); + + if(len_src == 0){ + in = "output"; + len_src = 6; + } + + if(len_src > len_ext && strcmp(in+len_src-len_ext,ctx->extension) == 0){ + /* Already has correct extension and fits in buffer */ + if(len_src < len_out) + strncpy(in,out,len_out); + else + success = ENOMEM; + }else if(len_src > len_ext){ + /* Doesn't have correct extension and fits */ + if(len_src + len_ext < len_out){ + strncpy(out,in,len_out); + strncat(out,ctx->extension,len_out); + }else + success = ENOMEM; + }else{ + /* The input buffer plus an extension cannot fit in the output buffer */ + fprintf(stderr,"Error building image output filename\n"); + success = ENOMEM; + } + return success; +} +int image_write(PIMAGE_CTX ctx, FILE *fd){ + struct image_dispatch_table *dt = (struct image_dispatch_table*)ctx->_dt; + return dt->write(ctx,fd); +} +int image_set_library(char *library){ + char *libname; + size_t length; + + length = strlen(library) + 1; + libname = (char*)calloc(length,sizeof(char)); + if(libname == NULL) + return ENOMEM; + strncpy(libname,library,length); + + dynamic_backend = libname; + format = IMAGE_LIBRARY; + return 0; +} + diff --git a/image.hh b/image.hh new file mode 100644 index 0000000..f620508 --- /dev/null +++ b/image.hh @@ -0,0 +1,42 @@ +#ifndef _IMAGE_HH_ +#define _IMAGE_HH_ + +#include + +#define RGB_SIZE 3 +#define RGBA_SIZE 4 + +typedef enum _IMAGE_FORMAT{ IMAGE_PPM, \ + IMAGE_LIBRARY, \ + IMAGE_FORMAT_MAX \ + } IMAGE_FORMAT; + +typedef enum _IMAGE_MODEL{ IMAGE_RGB, \ + IMAGE_RGBA, \ + IMAGE_MODEL_MAX + } IMAGE_MODEL; + +typedef struct _IMAGE_CTX{ + size_t width; + size_t height; + IMAGE_MODEL model; + IMAGE_FORMAT format; + uint8_t *canvas; + uint8_t *next_pixel; + uint32_t initialized; + char *extension; + void *_dt; +} IMAGE_CTX, *PIMAGE_CTX; + +int image_init(PIMAGE_CTX ctx, const size_t width, const size_t height, const IMAGE_MODEL model); +int image_add_pixel(PIMAGE_CTX ctx, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a); +int image_set_pixel(PIMAGE_CTX ctx, const size_t x, const size_t y, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a); +int image_get_pixel(PIMAGE_CTX ctx,const size_t x,const size_t y, uint8_t const *r, uint8_t const *g, uint8_t const *b, uint8_t const *a); +int image_get_filename(PIMAGE_CTX ctx, char* out, size_t len, char* in); +int image_write(PIMAGE_CTX ctx, FILE *fd); +int image_set_library(char *library); + +#define ADD_PIXEL(ctx,r,g,b) image_add_pixel((ctx),(r),(g),(b),0) +#define ADD_PIXELA(ctx,r,g,b,a) image_add_pixel((ctx),(r),(g),(b),(a)) + +#endif \ No newline at end of file diff --git a/main.cc b/main.cc index d5c2902..c4f5ea3 100644 --- a/main.cc +++ b/main.cc @@ -35,6 +35,7 @@ double version = 2.95; #include "models/itwom3.0.hh" #include "models/los.hh" #include "models/pel.hh" +#include "image.hh" int MAXPAGES = 64; int ARRAYSIZE = 76810;//76810; @@ -1210,6 +1211,14 @@ int main(int argc, char *argv[]) } } + if (strcmp(argv[x], "-so") == 0) { + z = x + 1; + if(image_set_library(argv[z]) != 0){ + fprintf(stderr,"Error configuring image processor\n"); + exit(EINVAL); + } + } + if (strcmp(argv[x], "-rt") == 0) { z = x + 1; diff --git a/outputs.cc b/outputs.cc index 77b4a8c..ca90da5 100644 --- a/outputs.cc +++ b/outputs.cc @@ -14,6 +14,7 @@ #include "models/hata.hh" #include "models/itwom3.0.hh" #include "models/sui.hh" +#include "image.hh" void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, unsigned char ngs, struct site *xmtr, unsigned char txsites) @@ -529,6 +530,13 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, int indx, x, y, z = 1, x0 = 0, y0 = 0, dBm, match; double conversion, one_over_gamma, lat, lon, minwest; FILE *fd; + IMAGE_CTX ctx; + int success; + + if((success = image_init(&ctx, width, (kml ? height : height + 30), IMAGE_RGB)) != 0){ + fprintf(stderr,"Error initializing image\n"); + exit(success); + } one_over_gamma = 1.0 / GAMMA; conversion = @@ -544,24 +552,11 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, filename[strlen(filename) - 4] = 0; /* Remove .qth */ } - y = strlen(filename); - - if (y > 4) { - if (filename[y - 1] == 'm' && filename[y - 2] == 'p' - && filename[y - 3] == 'p' && filename[y - 4] == '.') - y -= 4; + if(image_get_filename(&ctx,mapfile,sizeof(mapfile),filename) != 0){ + fprintf(stderr,"Error creating file name\n"); + exit(1); } - for (x = 0; x < y; x++) { - mapfile[x] = filename[x]; - } - - mapfile[x] = '.'; - mapfile[x + 1] = 'p'; - mapfile[x + 2] = 'p'; - mapfile[x + 3] = 'm'; - mapfile[x + 4] = 0; - fd = fopen(mapfile,"wb"); } else { @@ -583,7 +578,6 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, east = (minwest < 180.0 ? -minwest : 360.0 - min_west); west = (double)(max_west < 180 ? -max_west : 360 - max_west); - fprintf(fd, "P6\n%u %u\n255\n", width, (kml ? height : height)); if (debug) { fprintf(stderr, "\nWriting \"%s\" (%ux%u pixmap image)...\n", (filename != NULL ? mapfile : "to stdout"), width, (kml ? height : height)); @@ -652,11 +646,11 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, if (red >= 180 && green <= 75 && blue <= 75 && dBm != 0) - fprintf(fd, "%c%c%c", 255 ^ red, + ADD_PIXEL(&ctx, 255 ^ red, 255 ^ green, 255 ^ blue); else - fprintf(fd, "%c%c%c", 255, 0, + ADD_PIXEL(&ctx, 255, 0, 0); cityorcounty = 1; @@ -664,7 +658,7 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, else if (mask & 4) { /* County Boundaries: Black */ - fprintf(fd, "%c%c%c", 0, 0, 0); + ADD_PIXEL(&ctx, 0, 0, 0); cityorcounty = 1; } @@ -672,15 +666,14 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, if (contour_threshold != 0 && dBm < contour_threshold) { if (ngs) /* No terrain */ - fprintf(fd, "%c%c%c", + ADD_PIXEL(&ctx, 255, 255, 255); else { /* Display land or sea elevation */ if (dem[indx]. data[x0][y0] == 0) - fprintf(fd, - "%c%c%c", + ADD_PIXEL(&ctx, 0, 0, 170); else { @@ -688,8 +681,7 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, (unsigned) (0.5 + pow((double)(dem[indx].data[x0][y0] - min_elevation), one_over_gamma) * conversion); - fprintf(fd, - "%c%c%c", + ADD_PIXEL(&ctx, terrain, terrain, terrain); @@ -702,15 +694,14 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, if (red != 0 || green != 0 || blue != 0) - fprintf(fd, "%c%c%c", + ADD_PIXEL(&ctx, red, green, blue); else { /* terrain / sea-level */ if (ngs) - fprintf(fd, - "%c%c%c", + ADD_PIXEL(&ctx, 255, 255, 255); // WHITE @@ -718,9 +709,7 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, if (dem[indx]. data[x0][y0] == 0) - fprintf - (fd, - "%c%c%c", + ADD_PIXEL(&ctx, 0, 0, 170); // BLUE @@ -733,9 +722,7 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, + pow ((double)(dem[indx].data[x0][y0] - min_elevation), one_over_gamma) * conversion); - fprintf - (fd, - "%c%c%c", + ADD_PIXEL(&ctx, terrain, terrain, terrain); @@ -750,11 +737,16 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, /* We should never get here, but if */ /* we do, display the region as black */ - fprintf(fd, "%c%c%c", 0, 0, 0); + ADD_PIXEL(&ctx, 0, 0, 0); } } } + if((success = image_write(&ctx,fd)) != 0){ + fprintf(stderr,"Error writing image\n"); + exit(success); + } + fflush(fd); if( filename != NULL ) { From fb950bafef3032c55e8b1a2da9124af7319dfdb7 Mon Sep 17 00:00:00 2001 From: Gareth Evans Date: Thu, 2 Feb 2017 12:35:19 +0000 Subject: [PATCH 4/8] Large code cleanup of image rendering engine --- image-ppm.cc | 6 +- image.cc | 196 +++++++++++++++++++++++++++++++++++---------------- image.hh | 20 +++--- outputs.cc | 2 +- 4 files changed, 149 insertions(+), 75 deletions(-) diff --git a/image-ppm.cc b/image-ppm.cc index f37bbe2..a9d8044 100644 --- a/image-ppm.cc +++ b/image-ppm.cc @@ -16,6 +16,7 @@ int ppm_init(PIMAGE_CTX ctx){ buf_size = ctx->width * ctx->height * RGB_SIZE; + /* Allocate the canvas buffer */ ctx->canvas = (uint8_t*) calloc(buf_size,sizeof(uint8_t)); ctx->next_pixel = ctx->canvas; if(ctx->canvas == NULL) @@ -32,10 +33,7 @@ int ppm_add_pixel(PIMAGE_CTX ctx,const uint8_t r,const uint8_t g,const uint8_t b next[0] = r; next[1] = g; next[2] = b; - /*if(ctx->model == IMAGE_RGBA){ - next[3] = a; - ctx->next_pixel += 1; - }*/ + ctx->next_pixel += 3; return 0; diff --git a/image.cc b/image.cc index 467e655..da67bef 100644 --- a/image.cc +++ b/image.cc @@ -1,3 +1,10 @@ +/* + * Generic image output handling. This feature allows + * for extensible image output formats and permits for + * cleaner image rendering code in the model generation + * routines by moving all of the file-format specific + * logic to be handled here. + */ #include #include #include @@ -12,6 +19,9 @@ typedef int _add_pixel(PIMAGE_CTX,const uint8_t,const uint8_t,const uint8_t,cons typedef int _get_pixel(PIMAGE_CTX,const size_t,const size_t,const uint8_t*,const uint8_t*,const uint8_t*,const uint8_t*); typedef int _write(PIMAGE_CTX,FILE*); +int get_dt(struct image_dispatch_table *dt, IMAGE_FORMAT format); +int load_library(struct image_dispatch_table *dt); + struct image_dispatch_table{ _init *init; _add_pixel *add_pixel; @@ -19,64 +29,31 @@ struct image_dispatch_table{ _write *write; }; -static IMAGE_FORMAT format = IMAGE_PPM; +static IMAGE_FORMAT default_format = IMAGE_PPM; char *dynamic_backend = NULL; -int load_library(struct image_dispatch_table *dt){ - void *hndl; - int success = 0; - - if(dynamic_backend == NULL){ - fprintf(stderr,"Custom image processor requested without specification\n"); +/* + * image_set_format + * Changes the default format for the next + * uninitialized image canvas + */ +int image_set_format(IMAGE_FORMAT format){ + if(format <= IMAGE_DEFAULT || format >= IMAGE_FORMAT_MAX) return EINVAL; - } - - hndl = dlopen(dynamic_backend,RTLD_LAZY); - if(hndl == NULL){ - fprintf(stderr,"Error loading shared object\n"); - return EINVAL; - } - - dt->init = (_init*)dlsym(hndl,"lib_init"); - dt->add_pixel = (_add_pixel*)dlsym(hndl,"lib_add_pixel"); - dt->get_pixel = (_get_pixel*)dlsym(hndl,"lib_get_pixel"); - dt->write = (_write*)dlsym(hndl,"lib_write"); - - if(dt->init == NULL || - dt->add_pixel == NULL || - dt->get_pixel == NULL || - dt->write == NULL){ - fprintf(stderr,"Invalid image processing module specified\n"); - success = dlclose(hndl); - } - - return success; -} - -int get_dt(struct image_dispatch_table *dt, IMAGE_FORMAT format){ - int success = 0; - - memset((void*)dt,0x00,sizeof(struct image_dispatch_table)); - switch(format){ - case IMAGE_PPM: - dt->init = ppm_init; - dt->add_pixel = ppm_add_pixel; - dt->get_pixel = ppm_get_pixel; - dt->write = ppm_write; - break; - case IMAGE_LIBRARY: - success = load_library(dt); - break; - default: - success = EINVAL; - } - return success; + default_format = format; + return 0; } +/* + * image_init + * Initialize an image context. Must be called + * before attempting to write any image data + */ int image_init(PIMAGE_CTX ctx, \ const size_t width, \ const size_t height, \ - const IMAGE_MODEL model) { + const IMAGE_MODEL model, \ + const IMAGE_FORMAT format) { int success = 0; struct image_dispatch_table *dt; @@ -88,7 +65,7 @@ int image_init(PIMAGE_CTX ctx, \ return EINVAL; if(model < 0 || model > IMAGE_MODEL_MAX) return EINVAL; - if(format < 0 || format > IMAGE_FORMAT_MAX) + if(format >= IMAGE_FORMAT_MAX || (format == IMAGE_LIBRARY && dynamic_backend == NULL)) return EINVAL; memset(ctx,0x00,sizeof(IMAGE_CTX)); @@ -97,7 +74,10 @@ int image_init(PIMAGE_CTX ctx, \ ctx->width = width; ctx->height = height; ctx->model = model; - ctx->format = format; + if(format == IMAGE_DEFAULT) + ctx->format = default_format; + else + ctx->format = format; /* Get the dispatch table for this image format */ dt = (struct image_dispatch_table*) calloc(1,sizeof(struct image_dispatch_table)); @@ -105,7 +85,7 @@ int image_init(PIMAGE_CTX ctx, \ fprintf(stderr,"Error allocating dispatch table\n"); return ENOMEM; } - success = get_dt(dt,format); + success = get_dt(dt,ctx->format); if(success != 0){ fprintf(stderr,"Error locating dispatch table\n"); free(dt); @@ -125,12 +105,20 @@ int image_init(PIMAGE_CTX ctx, \ return success; } +/* + * image_add_pixel, image_set_pixel, image_get_pixel, image_write + * Various setters ad getters for assigning pixel data. image_write + * takes an open file handle and writes image data to it. + * These functions simply wrap the underlying format-specific functions + */ int image_add_pixel(PIMAGE_CTX ctx, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a){ struct image_dispatch_table *dt = (struct image_dispatch_table*)ctx->_dt; + if(ctx->initialized != 1) return EINVAL; return dt->add_pixel(ctx,r,g,b,a); } int image_set_pixel(PIMAGE_CTX ctx, const size_t x, const size_t y, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a){ size_t block_size; + if(ctx->initialized != 1) return EINVAL; block_size = ctx->model == IMAGE_RGB ? RGB_SIZE : RGBA_SIZE; ctx->next_pixel = ctx->canvas + (x * block_size) + (ctx->width * block_size * y); struct image_dispatch_table *dt = (struct image_dispatch_table*)ctx->_dt; @@ -138,23 +126,35 @@ int image_set_pixel(PIMAGE_CTX ctx, const size_t x, const size_t y, const uint8_ } int image_get_pixel(PIMAGE_CTX ctx, const size_t x, const size_t y, const uint8_t *r, const uint8_t *g, const uint8_t *b, const uint8_t *a){ struct image_dispatch_table *dt = (struct image_dispatch_table*)ctx->_dt; + if(ctx->initialized != 1) return EINVAL; return dt->get_pixel(ctx,x,y,r,g,b,a); } +int image_write(PIMAGE_CTX ctx, FILE *fd){ + struct image_dispatch_table *dt = (struct image_dispatch_table*)ctx->_dt; + if(ctx->initialized != 1) return EINVAL; + return dt->write(ctx,fd); +} + +/* + * image_get_filename + * Creates an appropriate file name using data supplied + * by the user. If the extension is already correct, return + * that; if not append if there is space + */ int image_get_filename(PIMAGE_CTX ctx, char *out, size_t len_out, char *in){ size_t len_src; size_t len_ext; int success = 0; - if(ctx->initialized != 1){ - fprintf(stderr,"Called get filename before initialization\n"); + if(ctx->initialized != 1) return EINVAL; - } + /* Get various lengths */ len_src = strlen(in); len_ext = strlen(ctx->extension); if(len_src == 0){ - in = "output"; + in = (char*)"output"; len_src = 6; } @@ -178,10 +178,43 @@ int image_get_filename(PIMAGE_CTX ctx, char *out, size_t len_out, char *in){ } return success; } -int image_write(PIMAGE_CTX ctx, FILE *fd){ - struct image_dispatch_table *dt = (struct image_dispatch_table*)ctx->_dt; - return dt->write(ctx,fd); + +/* + * get_dt + * Load the dispatch table for the specified image + * format. Currently only pixmap supported. + */ +int get_dt(struct image_dispatch_table *dt, IMAGE_FORMAT format){ + int success = 0; + + memset((void*)dt,0x00,sizeof(struct image_dispatch_table)); + switch(format){ + case IMAGE_PPM: + dt->init = ppm_init; + dt->add_pixel = ppm_add_pixel; + dt->get_pixel = ppm_get_pixel; + dt->write = ppm_write; + break; + case IMAGE_LIBRARY: + success = load_library(dt); + break; + default: + success = EINVAL; + } + return success; } + +/* + * ==WARNING==: Here be dragons! + * Experimantal features beyond this point. + * Only use if you are sure you know what you + * are doing! + */ + +/* + * image_set_library + * Set the library to use for generating images + */ int image_set_library(char *library){ char *libname; size_t length; @@ -193,7 +226,48 @@ int image_set_library(char *library){ strncpy(libname,library,length); dynamic_backend = libname; - format = IMAGE_LIBRARY; + default_format = IMAGE_LIBRARY; return 0; } +/* + * image_get_library + * Returns the current saved image rendering + * library + */ +char* image_get_library(){ + return dynamic_backend; +} +/* + * load_library + * External image processing: experimental feature + * Load an external library to perform image processing + * It must be a custom compatible library + */ +int load_library(struct image_dispatch_table *dt){ + void *hndl; + int success = 0; + + /* Validate object file */ + if(dynamic_backend == NULL || strlen(dynamic_backend) == 0){ + fprintf(stderr,"Custom image processor requested without specification\n"); + return EINVAL; + } + + /* Load shared object and locate required exports */ + hndl = dlopen(dynamic_backend,RTLD_LAZY); + if(hndl == NULL){ + fprintf(stderr,"Error loading shared object\n"); + return EINVAL; + } + /* Perform symbol lookup */ + if((dt->init = (_init*)dlsym(hndl,"lib_init")) == NULL || + (dt->add_pixel = (_add_pixel*)dlsym(hndl,"lib_add_pixel")) == NULL || + (dt->get_pixel = (_get_pixel*)dlsym(hndl,"lib_get_pixel")) == NULL || + (dt->write = (_write*)dlsym(hndl,"lib_write")) == NULL){ + fprintf(stderr,"Invalid image processing module specified\n\t%s",dlerror()); + success = dlclose(hndl); + } + + return success; +} \ No newline at end of file diff --git a/image.hh b/image.hh index f620508..45437d0 100644 --- a/image.hh +++ b/image.hh @@ -6,7 +6,8 @@ #define RGB_SIZE 3 #define RGBA_SIZE 4 -typedef enum _IMAGE_FORMAT{ IMAGE_PPM, \ +typedef enum _IMAGE_FORMAT{ IMAGE_DEFAULT = 0, \ + IMAGE_PPM, \ IMAGE_LIBRARY, \ IMAGE_FORMAT_MAX \ } IMAGE_FORMAT; @@ -28,15 +29,16 @@ typedef struct _IMAGE_CTX{ void *_dt; } IMAGE_CTX, *PIMAGE_CTX; -int image_init(PIMAGE_CTX ctx, const size_t width, const size_t height, const IMAGE_MODEL model); -int image_add_pixel(PIMAGE_CTX ctx, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a); -int image_set_pixel(PIMAGE_CTX ctx, const size_t x, const size_t y, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a); -int image_get_pixel(PIMAGE_CTX ctx,const size_t x,const size_t y, uint8_t const *r, uint8_t const *g, uint8_t const *b, uint8_t const *a); -int image_get_filename(PIMAGE_CTX ctx, char* out, size_t len, char* in); -int image_write(PIMAGE_CTX ctx, FILE *fd); -int image_set_library(char *library); +int image_set_format(IMAGE_FORMAT); +int image_init(PIMAGE_CTX, const size_t, const size_t, const IMAGE_MODEL, const IMAGE_FORMAT); +int image_add_pixel(PIMAGE_CTX ctx, const uint8_t, const uint8_t, const uint8_t, const uint8_t); +int image_set_pixel(PIMAGE_CTX ctx, const size_t, const size_t, const uint8_t, const uint8_t, const uint8_t, const uint8_t); +int image_get_pixel(PIMAGE_CTX ctx,const size_t,const size_t, uint8_t const*, uint8_t const*, uint8_t const*, uint8_t const*); +int image_get_filename(PIMAGE_CTX, char*, size_t, char*); +int image_write(PIMAGE_CTX, FILE*); +int image_set_library(char*); -#define ADD_PIXEL(ctx,r,g,b) image_add_pixel((ctx),(r),(g),(b),0) +#define ADD_PIXEL(ctx,r,g,b) image_add_pixel((ctx),(r),(g),(b),0xff) #define ADD_PIXELA(ctx,r,g,b,a) image_add_pixel((ctx),(r),(g),(b),(a)) #endif \ No newline at end of file diff --git a/outputs.cc b/outputs.cc index ca90da5..6425096 100644 --- a/outputs.cc +++ b/outputs.cc @@ -533,7 +533,7 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, IMAGE_CTX ctx; int success; - if((success = image_init(&ctx, width, (kml ? height : height + 30), IMAGE_RGB)) != 0){ + if((success = image_init(&ctx, width, (kml ? height : height + 30), IMAGE_RGB, IMAGE_DEFAULT)) != 0){ fprintf(stderr,"Error initializing image\n"); exit(success); } From 6fecec8e4e369dfc7f54b3592c4d781b8ab85665 Mon Sep 17 00:00:00 2001 From: Gareth Evans Date: Thu, 2 Feb 2017 12:41:36 +0000 Subject: [PATCH 5/8] Add image_free to tidy-up --- image.cc | 6 +++++- image.hh | 1 + outputs.cc | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/image.cc b/image.cc index da67bef..ecee80d 100644 --- a/image.cc +++ b/image.cc @@ -106,7 +106,7 @@ int image_init(PIMAGE_CTX ctx, \ return success; } /* - * image_add_pixel, image_set_pixel, image_get_pixel, image_write + * image_add_pixel, image_set_pixel, image_get_pixel, image_write, image_free * Various setters ad getters for assigning pixel data. image_write * takes an open file handle and writes image data to it. * These functions simply wrap the underlying format-specific functions @@ -134,6 +134,10 @@ int image_write(PIMAGE_CTX ctx, FILE *fd){ if(ctx->initialized != 1) return EINVAL; return dt->write(ctx,fd); } +void image_free(PIMAGE_CTX ctx){ + if(ctx->initialized != 1) return; + free(ctx->canvas); +} /* * image_get_filename diff --git a/image.hh b/image.hh index 45437d0..67812b9 100644 --- a/image.hh +++ b/image.hh @@ -36,6 +36,7 @@ int image_set_pixel(PIMAGE_CTX ctx, const size_t, const size_t, const uint8_t, c int image_get_pixel(PIMAGE_CTX ctx,const size_t,const size_t, uint8_t const*, uint8_t const*, uint8_t const*, uint8_t const*); int image_get_filename(PIMAGE_CTX, char*, size_t, char*); int image_write(PIMAGE_CTX, FILE*); +void image_free(PIMAGE_CTX); int image_set_library(char*); #define ADD_PIXEL(ctx,r,g,b) image_add_pixel((ctx),(r),(g),(b),0xff) diff --git a/outputs.cc b/outputs.cc index 6425096..b1b9202 100644 --- a/outputs.cc +++ b/outputs.cc @@ -749,6 +749,8 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, fflush(fd); + image_free(&ctx); + if( filename != NULL ) { fclose(fd); fd = NULL; From de3c4edc2f251d0bd273d46b4af970983f649bf8 Mon Sep 17 00:00:00 2001 From: Gareth Evans Date: Thu, 2 Feb 2017 12:51:03 +0000 Subject: [PATCH 6/8] Complete migration of models to image rendering engine --- outputs.cc | 199 +++++++++++++++++++++++++---------------------------- 1 file changed, 93 insertions(+), 106 deletions(-) diff --git a/outputs.cc b/outputs.cc index b1b9202..0c3df48 100644 --- a/outputs.cc +++ b/outputs.cc @@ -31,6 +31,13 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, int indx, x, y, z, x0 = 0, y0 = 0, loss, match; double lat, lon, conversion, one_over_gamma, minwest; FILE *fd; + IMAGE_CTX ctx; + int success; + + if((success = image_init(&ctx, width, (kml ? height : height + 30), IMAGE_RGB, IMAGE_DEFAULT)) != 0){ + fprintf(stderr,"Error initializing image\n"); + exit(success); + } one_over_gamma = 1.0 / GAMMA; conversion = @@ -46,24 +53,11 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, filename[strlen(filename) - 4] = 0; /* Remove .qth */ } - y = strlen(filename); - - if (y > 4) { - if (filename[y - 1] == 'm' && filename[y - 2] == 'p' - && filename[y - 3] == 'p' && filename[y - 4] == '.') - y -= 4; + if(image_get_filename(&ctx,mapfile,sizeof(mapfile),filename) != 0){ + fprintf(stderr,"Error creating file name\n"); + exit(1); } - for (x = 0; x < y; x++) { - mapfile[x] = filename[x]; - } - - mapfile[x] = '.'; - mapfile[x + 1] = 'p'; - mapfile[x + 2] = 'p'; - mapfile[x + 3] = 'm'; - mapfile[x + 4] = 0; - fd = fopen(mapfile,"wb"); } else { @@ -88,7 +82,6 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, east = (minwest < 180.0 ? -minwest : 360.0 - min_west); west = (double)(max_west < 180 ? -max_west : 360 - max_west); - fprintf(fd, "P6\n%u %u\n255\n", width, (kml ? height : height + 30)); if (debug) { fprintf(stderr, "\nWriting \"%s\" (%ux%u pixmap image)...\n", filename != NULL ? mapfile : "to stdout", width, (kml ? height : height + 30)); @@ -161,11 +154,11 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, if (red >= 180 && green <= 75 && blue <= 75 && loss == 0) - fprintf(fd, "%c%c%c", 255 ^ red, + ADD_PIXEL(&ctx, 255 ^ red, 255 ^ green, 255 ^ blue); else - fprintf(fd, "%c%c%c", 255, 0, + ADD_PIXEL(&ctx, 255, 0, 0); cityorcounty = 1; @@ -174,7 +167,7 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, else if (mask & 4) { /* County Boundaries: Black */ - fprintf(fd, "%c%c%c", 0, 0, 0); + ADD_PIXEL(&ctx, 0, 0, 0); cityorcounty = 1; } @@ -185,15 +178,14 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, && loss > abs(contour_threshold))) { if (ngs) /* No terrain */ - fprintf(fd, "%c%c%c", + ADD_PIXEL(&ctx, 255, 255, 255); else { /* Display land or sea elevation */ if (dem[indx]. data[x0][y0] == 0) - fprintf(fd, - "%c%c%c", + ADD_PIXEL(&ctx, 0, 0, 170); else { @@ -201,8 +193,7 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, (unsigned) (0.5 + pow((double)(dem[indx].data[x0][y0] - min_elevation), one_over_gamma) * conversion); - fprintf(fd, - "%c%c%c", + ADD_PIXEL(&ctx, terrain, terrain, terrain); @@ -215,7 +206,7 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, if (red != 0 || green != 0 || blue != 0) - fprintf(fd, "%c%c%c", + ADD_PIXEL(&ctx, red, green, blue); @@ -223,8 +214,7 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, if (dem[indx]. data[x0][y0] == 0) - fprintf(fd, - "%c%c%c", + ADD_PIXEL(&ctx, 0, 0, 170); else { @@ -233,8 +223,7 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, (unsigned) (0.5 + pow((double)(dem[indx].data[x0][y0] - min_elevation), one_over_gamma) * conversion); - fprintf(fd, - "%c%c%c", + ADD_PIXEL(&ctx, terrain, terrain, terrain); @@ -248,11 +237,18 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, /* We should never get here, but if */ /* we do, display the region as black */ - fprintf(fd, "%c%c%c", 0, 0, 0); + ADD_PIXEL(&ctx, 0, 0, 0); } } } + if((success = image_write(&ctx,fd)) != 0){ + fprintf(stderr,"Error writing image\n"); + exit(success); + } + + image_free(&ctx); + if( filename != NULL ) { fclose(fd); fd = NULL; @@ -275,6 +271,13 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml, int indx, x, y, z = 1, x0 = 0, y0 = 0, signal, match; double conversion, one_over_gamma, lat, lon, minwest; FILE *fd; + IMAGE_CTX ctx; + int success; + + if((success = image_init(&ctx, width, (kml ? height : height + 30), IMAGE_RGB, IMAGE_DEFAULT)) != 0){ + fprintf(stderr,"Error initializing image\n"); + exit(success); + } one_over_gamma = 1.0 / GAMMA; conversion = @@ -290,24 +293,11 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml, filename[strlen(filename) - 4] = 0; /* Remove .qth */ } - y = strlen(filename); - - if (y > 4) { - if (filename[y - 1] == 'm' && filename[y - 2] == 'p' - && filename[y - 3] == 'p' && filename[y - 4] == '.') - y -= 4; + if(image_get_filename(&ctx,mapfile,sizeof(mapfile),filename) != 0){ + fprintf(stderr,"Error creating file name\n"); + exit(1); } - for (x = 0; x < y; x++) { - mapfile[x] = filename[x]; - } - - mapfile[x] = '.'; - mapfile[x + 1] = 'p'; - mapfile[x + 2] = 'p'; - mapfile[x + 3] = 'm'; - mapfile[x + 4] = 0; - fd = fopen(mapfile,"wb"); } else { @@ -329,7 +319,6 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml, east = (minwest < 180.0 ? -minwest : 360.0 - min_west); west = (double)(max_west < 180 ? -max_west : 360 - max_west); - fprintf(fd, "P6\n%u %u\n255\n", width, (kml ? height : height + 30)); if (debug) { fprintf(stderr, "\nWriting \"%s\" (%ux%u pixmap image)...\n", filename != NULL ? mapfile : "to stdout", width, (kml ? height : height + 30)); @@ -403,11 +392,11 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml, if (red >= 180 && green <= 75 && blue <= 75) - fprintf(fd, "%c%c%c", 255 ^ red, + ADD_PIXEL(&ctx, 255 ^ red, 255 ^ green, 255 ^ blue); else - fprintf(fd, "%c%c%c", 255, 0, + ADD_PIXEL(&ctx, 255, 0, 0); cityorcounty = 1; @@ -416,7 +405,7 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml, else if (mask & 4) { /* County Boundaries: Black */ - fprintf(fd, "%c%c%c", 0, 0, 0); + ADD_PIXEL(&ctx, 0, 0, 0); cityorcounty = 1; } @@ -425,15 +414,14 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml, if (contour_threshold != 0 && signal < contour_threshold) { if (ngs) - fprintf(fd, "%c%c%c", + ADD_PIXEL(&ctx, 255, 255, 255); else { /* Display land or sea elevation */ if (dem[indx]. data[x0][y0] == 0) - fprintf(fd, - "%c%c%c", + ADD_PIXEL(&ctx, 0, 0, 170); else { @@ -441,8 +429,7 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml, (unsigned) (0.5 + pow((double)(dem[indx].data[x0][y0] - min_elevation), one_over_gamma) * conversion); - fprintf(fd, - "%c%c%c", + ADD_PIXEL(&ctx, terrain, terrain, terrain); @@ -455,15 +442,14 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml, if (red != 0 || green != 0 || blue != 0) - fprintf(fd, "%c%c%c", + ADD_PIXEL(&ctx, red, green, blue); else { /* terrain / sea-level */ if (ngs) - fprintf(fd, - "%c%c%c", + ADD_PIXEL(&ctx, 255, 255, 255); @@ -471,9 +457,7 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml, if (dem[indx]. data[x0][y0] == 0) - fprintf - (fd, - "%c%c%c", + ADD_PIXEL(&ctx, 0, 0, 170); @@ -486,9 +470,7 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml, + pow ((double)(dem[indx].data[x0][y0] - min_elevation), one_over_gamma) * conversion); - fprintf - (fd, - "%c%c%c", + ADD_PIXEL(&ctx, terrain, terrain, terrain); @@ -503,11 +485,18 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml, /* We should never get here, but if */ /* we do, display the region as black */ - fprintf(fd, "%c%c%c", 0, 0, 0); + ADD_PIXEL(&ctx, 0, 0, 0); } } } + if((success = image_write(&ctx,fd)) != 0){ + fprintf(stderr,"Error writing image\n"); + exit(success); + } + + image_free(&ctx); + if( filename != NULL ) { fclose(fd); fd = NULL; @@ -773,6 +762,13 @@ void DoLOS(char *filename, unsigned char geo, unsigned char kml, int indx, x, y, x0 = 0, y0 = 0; double conversion, one_over_gamma, lat, lon, minwest; FILE *fd; + IMAGE_CTX ctx; + int success; + + if((success = image_init(&ctx, width, (kml ? height : height + 30), IMAGE_RGB, IMAGE_DEFAULT)) != 0){ + fprintf(stderr,"Error initializing image\n"); + exit(success); + } one_over_gamma = 1.0 / GAMMA; conversion = @@ -786,24 +782,11 @@ void DoLOS(char *filename, unsigned char geo, unsigned char kml, filename[strlen(filename) - 4] = 0; /* Remove .qth */ } - y = strlen(filename); - - if (y > 4) { - if (filename[y - 1] == 'm' && filename[y - 2] == 'p' - && filename[y - 3] == 'p' && filename[y - 4] == '.') - y -= 4; + if(image_get_filename(&ctx,mapfile,sizeof(mapfile),filename) != 0){ + fprintf(stderr,"Error creating file name\n"); + exit(1); } - for (x = 0; x < y; x++) { - mapfile[x] = filename[x]; - } - - mapfile[x] = '.'; - mapfile[x + 1] = 'p'; - mapfile[x + 2] = 'p'; - mapfile[x + 3] = 'm'; - mapfile[x + 4] = 0; - fd = fopen(mapfile,"wb"); } else { @@ -825,7 +808,6 @@ void DoLOS(char *filename, unsigned char geo, unsigned char kml, east = (minwest < 180.0 ? -minwest : 360.0 - min_west); west = (double)(max_west < 180 ? -max_west : 360 - max_west); - fprintf(fd, "P6\n%u %u\n255\n", width, (kml ? height : height + 30)); if (debug) { fprintf(stderr, "\nWriting \"%s\" (%ux%u pixmap image)...\n", filename != NULL ? mapfile : "to stdout", width, (kml ? height : height + 30)); @@ -862,114 +844,113 @@ void DoLOS(char *filename, unsigned char geo, unsigned char kml, if (mask & 2) /* Text Labels: Red */ - fprintf(fd, "%c%c%c", 255, 0, 0); + ADD_PIXEL(&ctx, 255, 0, 0); else if (mask & 4) /* County Boundaries: Light Cyan */ - fprintf(fd, "%c%c%c", 128, 128, 255); + ADD_PIXEL(&ctx, 128, 128, 255); else switch (mask & 57) { case 1: /* TX1: Green */ - fprintf(fd, "%c%c%c", 0, 255, + ADD_PIXEL(&ctx, 0, 255, 0); break; case 8: /* TX2: Cyan */ - fprintf(fd, "%c%c%c", 0, 255, + ADD_PIXEL(&ctx, 0, 255, 255); break; case 9: /* TX1 + TX2: Yellow */ - fprintf(fd, "%c%c%c", 255, 255, + ADD_PIXEL(&ctx, 255, 255, 0); break; case 16: /* TX3: Medium Violet */ - fprintf(fd, "%c%c%c", 147, 112, + ADD_PIXEL(&ctx, 147, 112, 219); break; case 17: /* TX1 + TX3: Pink */ - fprintf(fd, "%c%c%c", 255, 192, + ADD_PIXEL(&ctx, 255, 192, 203); break; case 24: /* TX2 + TX3: Orange */ - fprintf(fd, "%c%c%c", 255, 165, + ADD_PIXEL(&ctx, 255, 165, 0); break; case 25: /* TX1 + TX2 + TX3: Dark Green */ - fprintf(fd, "%c%c%c", 0, 100, + ADD_PIXEL(&ctx, 0, 100, 0); break; case 32: /* TX4: Sienna 1 */ - fprintf(fd, "%c%c%c", 255, 130, + ADD_PIXEL(&ctx, 255, 130, 71); break; case 33: /* TX1 + TX4: Green Yellow */ - fprintf(fd, "%c%c%c", 173, 255, + ADD_PIXEL(&ctx, 173, 255, 47); break; case 40: /* TX2 + TX4: Dark Sea Green 1 */ - fprintf(fd, "%c%c%c", 193, 255, + ADD_PIXEL(&ctx, 193, 255, 193); break; case 41: /* TX1 + TX2 + TX4: Blanched Almond */ - fprintf(fd, "%c%c%c", 255, 235, + ADD_PIXEL(&ctx, 255, 235, 205); break; case 48: /* TX3 + TX4: Dark Turquoise */ - fprintf(fd, "%c%c%c", 0, 206, + ADD_PIXEL(&ctx, 0, 206, 209); break; case 49: /* TX1 + TX3 + TX4: Medium Spring Green */ - fprintf(fd, "%c%c%c", 0, 250, + ADD_PIXEL(&ctx, 0, 250, 154); break; case 56: /* TX2 + TX3 + TX4: Tan */ - fprintf(fd, "%c%c%c", 210, 180, + ADD_PIXEL(&ctx, 210, 180, 140); break; case 57: /* TX1 + TX2 + TX3 + TX4: Gold2 */ - fprintf(fd, "%c%c%c", 238, 201, + ADD_PIXEL(&ctx, 238, 201, 0); break; default: if (ngs) /* No terrain */ - fprintf(fd, "%c%c%c", + ADD_PIXEL(&ctx, 255, 255, 255); else { /* Sea-level: Medium Blue */ if (dem[indx]. data[x0][y0] == 0) - fprintf(fd, - "%c%c%c", + ADD_PIXEL(&ctx, 0, 0, 170); else { @@ -978,8 +959,7 @@ void DoLOS(char *filename, unsigned char geo, unsigned char kml, (unsigned) (0.5 + pow((double)(dem[indx].data[x0][y0] - min_elevation), one_over_gamma) * conversion); - fprintf(fd, - "%c%c%c", + ADD_PIXEL(&ctx, terrain, terrain, terrain); @@ -992,11 +972,18 @@ void DoLOS(char *filename, unsigned char geo, unsigned char kml, /* We should never get here, but if */ /* we do, display the region as black */ - fprintf(fd, "%c%c%c", 0, 0, 0); + ADD_PIXEL(&ctx, 0, 0, 0); } } } + if((success = image_write(&ctx,fd)) != 0){ + fprintf(stderr,"Error writing image\n"); + exit(success); + } + + image_free(&ctx); + if( filename != NULL) { fclose(fd); fd = NULL; From 8d483d9b6e69ce5c3a61739df648494656ddb5d6 Mon Sep 17 00:00:00 2001 From: Gareth Evans Date: Fri, 3 Feb 2017 11:54:50 +0000 Subject: [PATCH 7/8] Tidy up and typedef structs for ease of maintenance --- image-ppm.cc | 10 ++++---- image-ppm.hh | 16 +++++++++--- image.cc | 69 +++++++++++++++++++--------------------------------- image.hh | 56 ++++++++++++++++++++++++++---------------- outputs.cc | 8 +++--- 5 files changed, 81 insertions(+), 78 deletions(-) diff --git a/image-ppm.cc b/image-ppm.cc index a9d8044..8249011 100644 --- a/image-ppm.cc +++ b/image-ppm.cc @@ -4,7 +4,7 @@ #include #include "image.hh" -int ppm_init(PIMAGE_CTX ctx){ +int ppm_init(image_ctx_t *ctx){ size_t buf_size; /* Perform simple sanity checking */ @@ -12,7 +12,7 @@ int ppm_init(PIMAGE_CTX ctx){ return EINVAL; ctx->model = IMAGE_RGB; //Override this as we only support RGB ctx->format = IMAGE_PPM; - ctx->extension = ".ppm"; + ctx->extension = (char*)".ppm"; buf_size = ctx->width * ctx->height * RGB_SIZE; @@ -25,7 +25,7 @@ int ppm_init(PIMAGE_CTX ctx){ return 0; } -int ppm_add_pixel(PIMAGE_CTX ctx,const uint8_t r,const uint8_t g,const uint8_t b,const uint8_t a){ +int ppm_add_pixel(image_ctx_t *ctx,const uint8_t r,const uint8_t g,const uint8_t b,const uint8_t a){ register uint8_t* next; next = ctx->next_pixel; @@ -39,12 +39,12 @@ int ppm_add_pixel(PIMAGE_CTX ctx,const uint8_t r,const uint8_t g,const uint8_t b return 0; } -int ppm_get_pixel(PIMAGE_CTX ctx,const size_t x,const size_t y,const uint8_t *r,const uint8_t *g,const uint8_t *b,const uint8_t *a){ +int ppm_get_pixel(image_ctx_t *ctx,const size_t x,const size_t y,const uint8_t *r,const uint8_t *g,const uint8_t *b,const uint8_t *a){ /* STUB */ return 0; } -int ppm_write(PIMAGE_CTX ctx, FILE* fd){ +int ppm_write(image_ctx_t *ctx, FILE* fd){ size_t written; size_t count; diff --git a/image-ppm.hh b/image-ppm.hh index 4831f0a..dfe007b 100644 --- a/image-ppm.hh +++ b/image-ppm.hh @@ -4,10 +4,18 @@ #include #include #include +#include "image.hh" -int ppm_init(PIMAGE_CTX ctx); -int ppm_add_pixel(PIMAGE_CTX ctx,const uint8_t r,const uint8_t g,const uint8_t b,const uint8_t a); -int ppm_get_pixel(PIMAGE_CTX ctx,const size_t x,const size_t y,const uint8_t *r,const uint8_t *g,const uint8_t *b,const uint8_t *a); -int ppm_write(PIMAGE_CTX ctx, FILE* fd); +int ppm_init(image_ctx_t *ctx); +int ppm_add_pixel(image_ctx_t *ctx,const uint8_t r,const uint8_t g,const uint8_t b,const uint8_t a); +int ppm_get_pixel(image_ctx_t *ctx,const size_t x,const size_t y,const uint8_t *r,const uint8_t *g,const uint8_t *b,const uint8_t *a); +int ppm_write(image_ctx_t *ctx, FILE* fd); + +image_dispatch_table_t ppm_dt = {\ + .init = ppm_init, \ + .add_pixel = ppm_add_pixel, \ + .get_pixel = ppm_get_pixel, \ + .write = ppm_write +}; #endif \ No newline at end of file diff --git a/image.cc b/image.cc index ecee80d..c8e2098 100644 --- a/image.cc +++ b/image.cc @@ -14,22 +14,10 @@ #include "image.hh" #include "image-ppm.hh" -typedef int _init(PIMAGE_CTX); -typedef int _add_pixel(PIMAGE_CTX,const uint8_t,const uint8_t,const uint8_t,const uint8_t); -typedef int _get_pixel(PIMAGE_CTX,const size_t,const size_t,const uint8_t*,const uint8_t*,const uint8_t*,const uint8_t*); -typedef int _write(PIMAGE_CTX,FILE*); +int get_dt(image_dispatch_table_t *dt, int format); +int load_library(image_dispatch_table_t *dt); -int get_dt(struct image_dispatch_table *dt, IMAGE_FORMAT format); -int load_library(struct image_dispatch_table *dt); - -struct image_dispatch_table{ - _init *init; - _add_pixel *add_pixel; - _get_pixel *get_pixel; - _write *write; -}; - -static IMAGE_FORMAT default_format = IMAGE_PPM; +static int default_format = IMAGE_PPM; char *dynamic_backend = NULL; /* @@ -37,7 +25,7 @@ char *dynamic_backend = NULL; * Changes the default format for the next * uninitialized image canvas */ -int image_set_format(IMAGE_FORMAT format){ +int image_set_format(int format){ if(format <= IMAGE_DEFAULT || format >= IMAGE_FORMAT_MAX) return EINVAL; default_format = format; @@ -49,14 +37,14 @@ int image_set_format(IMAGE_FORMAT format){ * Initialize an image context. Must be called * before attempting to write any image data */ -int image_init(PIMAGE_CTX ctx, \ +int image_init(image_ctx_t *ctx, \ const size_t width, \ const size_t height, \ - const IMAGE_MODEL model, \ - const IMAGE_FORMAT format) { + const int model, \ + const int format) { int success = 0; - struct image_dispatch_table *dt; + image_dispatch_table_t *dt; /* Perform some sanity checking on provided arguments */ if(ctx == NULL) @@ -68,7 +56,7 @@ int image_init(PIMAGE_CTX ctx, \ if(format >= IMAGE_FORMAT_MAX || (format == IMAGE_LIBRARY && dynamic_backend == NULL)) return EINVAL; - memset(ctx,0x00,sizeof(IMAGE_CTX)); + memset(ctx,0x00,sizeof(image_ctx_t)); /* Assign the initialize values to the processing context */ ctx->width = width; @@ -80,7 +68,7 @@ int image_init(PIMAGE_CTX ctx, \ ctx->format = format; /* Get the dispatch table for this image format */ - dt = (struct image_dispatch_table*) calloc(1,sizeof(struct image_dispatch_table)); + dt = (image_dispatch_table_t *) calloc(1,sizeof(image_dispatch_table_t)); if(dt == NULL){ fprintf(stderr,"Error allocating dispatch table\n"); return ENOMEM; @@ -111,30 +99,26 @@ int image_init(PIMAGE_CTX ctx, \ * takes an open file handle and writes image data to it. * These functions simply wrap the underlying format-specific functions */ -int image_add_pixel(PIMAGE_CTX ctx, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a){ - struct image_dispatch_table *dt = (struct image_dispatch_table*)ctx->_dt; +int image_add_pixel(image_ctx_t *ctx, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a){ if(ctx->initialized != 1) return EINVAL; - return dt->add_pixel(ctx,r,g,b,a); + return ((image_dispatch_table_t*)ctx->_dt)->add_pixel(ctx,r,g,b,a); } -int image_set_pixel(PIMAGE_CTX ctx, const size_t x, const size_t y, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a){ +int image_set_pixel(image_ctx_t *ctx, const size_t x, const size_t y, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a){ size_t block_size; if(ctx->initialized != 1) return EINVAL; block_size = ctx->model == IMAGE_RGB ? RGB_SIZE : RGBA_SIZE; - ctx->next_pixel = ctx->canvas + (x * block_size) + (ctx->width * block_size * y); - struct image_dispatch_table *dt = (struct image_dispatch_table*)ctx->_dt; - return dt->add_pixel(ctx,r,g,b,a); + ctx->next_pixel = ctx->canvas + PIXEL_OFFSET(x,y,ctx->width,block_size); + return ((image_dispatch_table_t*)ctx->_dt)->add_pixel(ctx,r,g,b,a); } -int image_get_pixel(PIMAGE_CTX ctx, const size_t x, const size_t y, const uint8_t *r, const uint8_t *g, const uint8_t *b, const uint8_t *a){ - struct image_dispatch_table *dt = (struct image_dispatch_table*)ctx->_dt; +int image_get_pixel(image_ctx_t *ctx, const size_t x, const size_t y, const uint8_t *r, const uint8_t *g, const uint8_t *b, const uint8_t *a){ if(ctx->initialized != 1) return EINVAL; - return dt->get_pixel(ctx,x,y,r,g,b,a); + return ((image_dispatch_table_t*)ctx->_dt)->get_pixel(ctx,x,y,r,g,b,a); } -int image_write(PIMAGE_CTX ctx, FILE *fd){ - struct image_dispatch_table *dt = (struct image_dispatch_table*)ctx->_dt; +int image_write(image_ctx_t *ctx, FILE *fd){ if(ctx->initialized != 1) return EINVAL; - return dt->write(ctx,fd); + return ((image_dispatch_table_t*)ctx->_dt)->write(ctx,fd); } -void image_free(PIMAGE_CTX ctx){ +void image_free(image_ctx_t *ctx){ if(ctx->initialized != 1) return; free(ctx->canvas); } @@ -145,7 +129,7 @@ void image_free(PIMAGE_CTX ctx){ * by the user. If the extension is already correct, return * that; if not append if there is space */ -int image_get_filename(PIMAGE_CTX ctx, char *out, size_t len_out, char *in){ +int image_get_filename(image_ctx_t *ctx, char *out, size_t len_out, char *in){ size_t len_src; size_t len_ext; int success = 0; @@ -188,16 +172,13 @@ int image_get_filename(PIMAGE_CTX ctx, char *out, size_t len_out, char *in){ * Load the dispatch table for the specified image * format. Currently only pixmap supported. */ -int get_dt(struct image_dispatch_table *dt, IMAGE_FORMAT format){ +int get_dt(image_dispatch_table_t *dt, int format){ int success = 0; - memset((void*)dt,0x00,sizeof(struct image_dispatch_table)); + memset((void*)dt,0x00,sizeof(image_dispatch_table_t)); switch(format){ case IMAGE_PPM: - dt->init = ppm_init; - dt->add_pixel = ppm_add_pixel; - dt->get_pixel = ppm_get_pixel; - dt->write = ppm_write; + *dt = ppm_dt; break; case IMAGE_LIBRARY: success = load_library(dt); @@ -248,7 +229,7 @@ char* image_get_library(){ * Load an external library to perform image processing * It must be a custom compatible library */ -int load_library(struct image_dispatch_table *dt){ +int load_library(image_dispatch_table_t *dt){ void *hndl; int success = 0; diff --git a/image.hh b/image.hh index 67812b9..fa456ef 100644 --- a/image.hh +++ b/image.hh @@ -6,40 +6,54 @@ #define RGB_SIZE 3 #define RGBA_SIZE 4 -typedef enum _IMAGE_FORMAT{ IMAGE_DEFAULT = 0, \ - IMAGE_PPM, \ - IMAGE_LIBRARY, \ - IMAGE_FORMAT_MAX \ - } IMAGE_FORMAT; +enum _image_format{ IMAGE_DEFAULT = 0, \ + IMAGE_PPM, \ + IMAGE_LIBRARY, \ + IMAGE_FORMAT_MAX \ + }; -typedef enum _IMAGE_MODEL{ IMAGE_RGB, \ - IMAGE_RGBA, \ - IMAGE_MODEL_MAX - } IMAGE_MODEL; +enum _image_model{ IMAGE_RGB, \ + IMAGE_RGBA, \ + IMAGE_MODEL_MAX + }; -typedef struct _IMAGE_CTX{ +typedef struct _image_ctx{ size_t width; size_t height; - IMAGE_MODEL model; - IMAGE_FORMAT format; + int model; + int format; uint8_t *canvas; uint8_t *next_pixel; uint32_t initialized; char *extension; void *_dt; -} IMAGE_CTX, *PIMAGE_CTX; +} image_ctx_t, *pimage_ctx_t; -int image_set_format(IMAGE_FORMAT); -int image_init(PIMAGE_CTX, const size_t, const size_t, const IMAGE_MODEL, const IMAGE_FORMAT); -int image_add_pixel(PIMAGE_CTX ctx, const uint8_t, const uint8_t, const uint8_t, const uint8_t); -int image_set_pixel(PIMAGE_CTX ctx, const size_t, const size_t, const uint8_t, const uint8_t, const uint8_t, const uint8_t); -int image_get_pixel(PIMAGE_CTX ctx,const size_t,const size_t, uint8_t const*, uint8_t const*, uint8_t const*, uint8_t const*); -int image_get_filename(PIMAGE_CTX, char*, size_t, char*); -int image_write(PIMAGE_CTX, FILE*); -void image_free(PIMAGE_CTX); +typedef int _init(image_ctx_t*); +typedef int _add_pixel(image_ctx_t*,const uint8_t,const uint8_t,const uint8_t,const uint8_t); +typedef int _get_pixel(image_ctx_t*,const size_t,const size_t,const uint8_t*,const uint8_t*,const uint8_t*,const uint8_t*); +typedef int _write(image_ctx_t*,FILE*); + +typedef struct _image_dispatch_table{ + _init *init; + _add_pixel *add_pixel; + _get_pixel *get_pixel; + _write *write; +} image_dispatch_table_t; + +int image_set_format(int); +int image_init(image_ctx_t*, const size_t, const size_t, const int, const int); +int image_add_pixel(image_ctx_t* ctx, const uint8_t, const uint8_t, const uint8_t, const uint8_t); +int image_set_pixel(image_ctx_t* ctx, const size_t, const size_t, const uint8_t, const uint8_t, const uint8_t, const uint8_t); +int image_get_pixel(image_ctx_t* ctx,const size_t,const size_t, uint8_t const*, uint8_t const*, uint8_t const*, uint8_t const*); +int image_get_filename(image_ctx_t*, char*, size_t, char*); +int image_write(image_ctx_t*, FILE*); +void image_free(image_ctx_t*); int image_set_library(char*); #define ADD_PIXEL(ctx,r,g,b) image_add_pixel((ctx),(r),(g),(b),0xff) #define ADD_PIXELA(ctx,r,g,b,a) image_add_pixel((ctx),(r),(g),(b),(a)) +#define PIXEL_OFFSET(x,y,width,pixel_size) (((x) * (pixel_size)) + ((width) * (pixel_size) * (y))) + #endif \ No newline at end of file diff --git a/outputs.cc b/outputs.cc index 0c3df48..1dc278c 100644 --- a/outputs.cc +++ b/outputs.cc @@ -31,7 +31,7 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, int indx, x, y, z, x0 = 0, y0 = 0, loss, match; double lat, lon, conversion, one_over_gamma, minwest; FILE *fd; - IMAGE_CTX ctx; + image_ctx_t ctx; int success; if((success = image_init(&ctx, width, (kml ? height : height + 30), IMAGE_RGB, IMAGE_DEFAULT)) != 0){ @@ -271,7 +271,7 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml, int indx, x, y, z = 1, x0 = 0, y0 = 0, signal, match; double conversion, one_over_gamma, lat, lon, minwest; FILE *fd; - IMAGE_CTX ctx; + image_ctx_t ctx; int success; if((success = image_init(&ctx, width, (kml ? height : height + 30), IMAGE_RGB, IMAGE_DEFAULT)) != 0){ @@ -519,7 +519,7 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml, int indx, x, y, z = 1, x0 = 0, y0 = 0, dBm, match; double conversion, one_over_gamma, lat, lon, minwest; FILE *fd; - IMAGE_CTX ctx; + image_ctx_t ctx; int success; if((success = image_init(&ctx, width, (kml ? height : height + 30), IMAGE_RGB, IMAGE_DEFAULT)) != 0){ @@ -762,7 +762,7 @@ void DoLOS(char *filename, unsigned char geo, unsigned char kml, int indx, x, y, x0 = 0, y0 = 0; double conversion, one_over_gamma, lat, lon, minwest; FILE *fd; - IMAGE_CTX ctx; + image_ctx_t ctx; int success; if((success = image_init(&ctx, width, (kml ? height : height + 30), IMAGE_RGB, IMAGE_DEFAULT)) != 0){ From 594857c92c44f1f1c3acf3d807ef3e91b89c911b Mon Sep 17 00:00:00 2001 From: Gareth Evans Date: Fri, 3 Feb 2017 12:52:15 +0000 Subject: [PATCH 8/8] Various maintainability and speed improvements --- image-ppm.hh | 4 +++- image.cc | 31 ++++++++++++++++++++++++------- image.hh | 12 ++++++++---- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/image-ppm.hh b/image-ppm.hh index dfe007b..fe40ede 100644 --- a/image-ppm.hh +++ b/image-ppm.hh @@ -14,8 +14,10 @@ int ppm_write(image_ctx_t *ctx, FILE* fd); image_dispatch_table_t ppm_dt = {\ .init = ppm_init, \ .add_pixel = ppm_add_pixel, \ + .set_pixel = NULL, \ .get_pixel = ppm_get_pixel, \ - .write = ppm_write + .write = ppm_write, \ + .free = NULL }; #endif \ No newline at end of file diff --git a/image.cc b/image.cc index c8e2098..1af4e72 100644 --- a/image.cc +++ b/image.cc @@ -17,6 +17,8 @@ int get_dt(image_dispatch_table_t *dt, int format); int load_library(image_dispatch_table_t *dt); +#define DISPATCH_TABLE(ctx) ((image_dispatch_table_t*)(ctx)->_dt) + static int default_format = IMAGE_PPM; char *dynamic_backend = NULL; @@ -101,26 +103,35 @@ int image_init(image_ctx_t *ctx, \ */ int image_add_pixel(image_ctx_t *ctx, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a){ if(ctx->initialized != 1) return EINVAL; - return ((image_dispatch_table_t*)ctx->_dt)->add_pixel(ctx,r,g,b,a); + return DISPATCH_TABLE(ctx)->add_pixel(ctx,r,g,b,a); } int image_set_pixel(image_ctx_t *ctx, const size_t x, const size_t y, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a){ size_t block_size; if(ctx->initialized != 1) return EINVAL; + /* Image format handlers have the option to specify a _set_pixel handler */ + if(DISPATCH_TABLE(ctx)->set_pixel != NULL){ + return DISPATCH_TABLE(ctx)->set_pixel(ctx,x,y,r,g,b,a); + } block_size = ctx->model == IMAGE_RGB ? RGB_SIZE : RGBA_SIZE; ctx->next_pixel = ctx->canvas + PIXEL_OFFSET(x,y,ctx->width,block_size); - return ((image_dispatch_table_t*)ctx->_dt)->add_pixel(ctx,r,g,b,a); + return DISPATCH_TABLE(ctx)->add_pixel(ctx,r,g,b,a); } int image_get_pixel(image_ctx_t *ctx, const size_t x, const size_t y, const uint8_t *r, const uint8_t *g, const uint8_t *b, const uint8_t *a){ if(ctx->initialized != 1) return EINVAL; - return ((image_dispatch_table_t*)ctx->_dt)->get_pixel(ctx,x,y,r,g,b,a); + if(DISPATCH_TABLE(ctx)->get_pixel != NULL) + return DISPATCH_TABLE(ctx)->get_pixel(ctx,x,y,r,g,b,a); + return ENOSYS; } int image_write(image_ctx_t *ctx, FILE *fd){ if(ctx->initialized != 1) return EINVAL; - return ((image_dispatch_table_t*)ctx->_dt)->write(ctx,fd); + return DISPATCH_TABLE(ctx)->write(ctx,fd); } void image_free(image_ctx_t *ctx){ if(ctx->initialized != 1) return; - free(ctx->canvas); + if(DISPATCH_TABLE(ctx)->free != NULL){ + DISPATCH_TABLE(ctx)->free(ctx); + } + if(ctx->canvas != NULL) free(ctx->canvas); } /* @@ -248,10 +259,16 @@ int load_library(image_dispatch_table_t *dt){ /* Perform symbol lookup */ if((dt->init = (_init*)dlsym(hndl,"lib_init")) == NULL || (dt->add_pixel = (_add_pixel*)dlsym(hndl,"lib_add_pixel")) == NULL || - (dt->get_pixel = (_get_pixel*)dlsym(hndl,"lib_get_pixel")) == NULL || (dt->write = (_write*)dlsym(hndl,"lib_write")) == NULL){ fprintf(stderr,"Invalid image processing module specified\n\t%s",dlerror()); - success = dlclose(hndl); + success = EINVAL; + (void) dlclose(hndl); + } + /* Lookup optional symbols, these can return NULL */ + if(success == 0){ + dt->get_pixel = (_get_pixel*)dlsym(hndl,"lib_get_pixel"); + dt->set_pixel = (_set_pixel*)dlsym(hndl,"lib_set_pixel"); + dt->free = (_free*)dlsym(hndl,"lib_free"); } return success; diff --git a/image.hh b/image.hh index fa456ef..537b053 100644 --- a/image.hh +++ b/image.hh @@ -31,14 +31,18 @@ typedef struct _image_ctx{ typedef int _init(image_ctx_t*); typedef int _add_pixel(image_ctx_t*,const uint8_t,const uint8_t,const uint8_t,const uint8_t); +typedef int _set_pixel(image_ctx_t*,const size_t,const size_t,const uint8_t,const uint8_t,const uint8_t,const uint8_t); typedef int _get_pixel(image_ctx_t*,const size_t,const size_t,const uint8_t*,const uint8_t*,const uint8_t*,const uint8_t*); typedef int _write(image_ctx_t*,FILE*); +typedef void _free(image_ctx_t*); typedef struct _image_dispatch_table{ - _init *init; - _add_pixel *add_pixel; - _get_pixel *get_pixel; - _write *write; + _init *init; + _add_pixel *add_pixel; + _set_pixel *set_pixel; + _get_pixel *get_pixel; + _write *write; + _free *free; } image_dispatch_table_t; int image_set_format(int);