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..8249011 --- /dev/null +++ b/image-ppm.cc @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include "image.hh" + +int ppm_init(image_ctx_t *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 = (char*)".ppm"; + + 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) + return ENOMEM; + + return 0; +} + +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; + + next[0] = r; + next[1] = g; + next[2] = b; + + ctx->next_pixel += 3; + + return 0; +} + +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(image_ctx_t *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..fe40ede --- /dev/null +++ b/image-ppm.hh @@ -0,0 +1,23 @@ +#ifndef _IMAGE_PPM_HH +#define _IMAGE_PPM_HH + +#include +#include +#include +#include "image.hh" + +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, \ + .set_pixel = NULL, \ + .get_pixel = ppm_get_pixel, \ + .write = ppm_write, \ + .free = NULL +}; + +#endif \ No newline at end of file diff --git a/image.cc b/image.cc new file mode 100644 index 0000000..1af4e72 --- /dev/null +++ b/image.cc @@ -0,0 +1,275 @@ +/* + * 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 +#include +#include +#include +#include "image.hh" +#include "image-ppm.hh" + +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; + +/* + * image_set_format + * Changes the default format for the next + * uninitialized image canvas + */ +int image_set_format(int format){ + if(format <= IMAGE_DEFAULT || format >= IMAGE_FORMAT_MAX) + return EINVAL; + default_format = format; + return 0; +} + +/* + * image_init + * Initialize an image context. Must be called + * before attempting to write any image data + */ +int image_init(image_ctx_t *ctx, \ + const size_t width, \ + const size_t height, \ + const int model, \ + const int format) { + + int success = 0; + image_dispatch_table_t *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 >= IMAGE_FORMAT_MAX || (format == IMAGE_LIBRARY && dynamic_backend == NULL)) + return EINVAL; + + memset(ctx,0x00,sizeof(image_ctx_t)); + + /* Assign the initialize values to the processing context */ + ctx->width = width; + ctx->height = height; + ctx->model = model; + if(format == IMAGE_DEFAULT) + ctx->format = default_format; + else + ctx->format = format; + + /* Get the dispatch table for this image format */ + dt = (image_dispatch_table_t *) calloc(1,sizeof(image_dispatch_table_t)); + if(dt == NULL){ + fprintf(stderr,"Error allocating dispatch table\n"); + return ENOMEM; + } + success = get_dt(dt,ctx->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; +} +/* + * 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 + */ +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 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 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; + 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 DISPATCH_TABLE(ctx)->write(ctx,fd); +} +void image_free(image_ctx_t *ctx){ + if(ctx->initialized != 1) return; + if(DISPATCH_TABLE(ctx)->free != NULL){ + DISPATCH_TABLE(ctx)->free(ctx); + } + if(ctx->canvas != NULL) free(ctx->canvas); +} + +/* + * 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(image_ctx_t *ctx, char *out, size_t len_out, char *in){ + size_t len_src; + size_t len_ext; + int success = 0; + + if(ctx->initialized != 1) + return EINVAL; + + /* Get various lengths */ + len_src = strlen(in); + len_ext = strlen(ctx->extension); + + if(len_src == 0){ + in = (char*)"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; +} + +/* + * get_dt + * Load the dispatch table for the specified image + * format. Currently only pixmap supported. + */ +int get_dt(image_dispatch_table_t *dt, int format){ + int success = 0; + + memset((void*)dt,0x00,sizeof(image_dispatch_table_t)); + switch(format){ + case IMAGE_PPM: + *dt = ppm_dt; + 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; + + length = strlen(library) + 1; + libname = (char*)calloc(length,sizeof(char)); + if(libname == NULL) + return ENOMEM; + strncpy(libname,library,length); + + dynamic_backend = libname; + 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(image_dispatch_table_t *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->write = (_write*)dlsym(hndl,"lib_write")) == NULL){ + fprintf(stderr,"Invalid image processing module specified\n\t%s",dlerror()); + 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; +} \ No newline at end of file diff --git a/image.hh b/image.hh new file mode 100644 index 0000000..537b053 --- /dev/null +++ b/image.hh @@ -0,0 +1,63 @@ +#ifndef _IMAGE_HH_ +#define _IMAGE_HH_ + +#include + +#define RGB_SIZE 3 +#define RGBA_SIZE 4 + +enum _image_format{ IMAGE_DEFAULT = 0, \ + IMAGE_PPM, \ + IMAGE_LIBRARY, \ + IMAGE_FORMAT_MAX \ + }; + +enum _image_model{ IMAGE_RGB, \ + IMAGE_RGBA, \ + IMAGE_MODEL_MAX + }; + +typedef struct _image_ctx{ + size_t width; + size_t height; + int model; + int format; + uint8_t *canvas; + uint8_t *next_pixel; + uint32_t initialized; + char *extension; + void *_dt; +} image_ctx_t, *pimage_ctx_t; + +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; + _set_pixel *set_pixel; + _get_pixel *get_pixel; + _write *write; + _free *free; +} 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/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..c4f5ea3 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" @@ -34,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; @@ -54,6 +56,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 +1201,21 @@ 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"); + } + } + 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); } } @@ -1497,66 +1515,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 +1651,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 +1659,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 +1792,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 +1810,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 +1829,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 +1844,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/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 7b47ff4..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 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/models/sui.cc b/models/sui.cc index 113962c..03549c9 100644 --- a/models/sui.cc +++ b/models/sui.cc @@ -16,7 +16,7 @@ double SUIpathLoss(float f, float TxH, float RxH, float d, int mode) */ d = d * 1000; // km to m if (f < 1900 || f > 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 diff --git a/outputs.cc b/outputs.cc index 4e4ed95..1dc278c 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) @@ -30,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_t 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 = @@ -38,29 +46,27 @@ 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 */ + } + + if(image_get_filename(&ctx,mapfile,sizeof(mapfile),filename) != 0){ + fprintf(stderr,"Error creating file name\n"); + exit(1); + } + + 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 +82,12 @@ 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; @@ -150,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; @@ -163,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; } @@ -174,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 { @@ -190,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); @@ -204,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); @@ -212,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 { @@ -222,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); @@ -237,12 +237,22 @@ 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); } } } - fclose(fd); + 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; + } } @@ -261,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_t 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 = @@ -269,29 +286,27 @@ 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 */ + } + + if(image_get_filename(&ctx,mapfile,sizeof(mapfile),filename) != 0){ + fprintf(stderr,"Error creating file name\n"); + exit(1); + } + + 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 +318,13 @@ 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; @@ -378,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; @@ -391,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; } @@ -400,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 { @@ -416,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); @@ -430,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); @@ -446,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); @@ -461,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); @@ -478,12 +485,22 @@ 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); } } } - fclose(fd); + 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; + } } @@ -502,6 +519,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_t 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 = @@ -510,29 +534,27 @@ 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 */ + } + + if(image_get_filename(&ctx,mapfile,sizeof(mapfile),filename) != 0){ + fprintf(stderr,"Error creating file name\n"); + exit(1); + } + + 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 +567,10 @@ 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 @@ -616,11 +635,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; @@ -628,7 +647,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; } @@ -636,15 +655,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 { @@ -652,8 +670,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); @@ -666,15 +683,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 @@ -682,9 +698,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 @@ -697,9 +711,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); @@ -714,13 +726,24 @@ 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); + } - fclose(fd); + fflush(fd); + + image_free(&ctx); + + if( filename != NULL ) { + fclose(fd); + fd = NULL; + } } @@ -739,35 +762,40 @@ 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_t 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 = 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 */ + } + + if(image_get_filename(&ctx,mapfile,sizeof(mapfile),filename) != 0){ + fprintf(stderr,"Error creating file name\n"); + exit(1); + } + + 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 +808,10 @@ 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; @@ -819,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 { @@ -935,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); @@ -949,12 +972,22 @@ 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); } } } - fclose(fd); + 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; + } } @@ -1622,7 +1655,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 +2025,7 @@ void SeriesData(struct site source, struct site destination, char *name, } } - fprintf(stdout, "\n"); - fflush(stdout); + fprintf(stderr, "\n"); + fflush(stderr); }