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/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..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 = @@ -45,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 { @@ -87,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)); @@ -160,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; @@ -173,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; } @@ -184,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 { @@ -200,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); @@ -214,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); @@ -222,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 { @@ -232,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); @@ -247,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; @@ -274,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 = @@ -289,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 { @@ -328,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)); @@ -402,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; @@ -415,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; } @@ -424,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 { @@ -440,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); @@ -454,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); @@ -470,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); @@ -485,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); @@ -502,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; @@ -529,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 = @@ -544,24 +541,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 +567,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 +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; @@ -664,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; } @@ -672,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 { @@ -688,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); @@ -702,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 @@ -718,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 @@ -733,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); @@ -750,13 +726,20 @@ 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); + image_free(&ctx); + if( filename != NULL ) { fclose(fd); fd = NULL; @@ -779,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_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 = @@ -792,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 { @@ -831,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)); @@ -868,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 { @@ -984,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); @@ -998,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;