From 93aa2e472a04a7050320136faf19a511f4e4324f Mon Sep 17 00:00:00 2001 From: Gareth Evans Date: Wed, 1 Feb 2017 23:44:00 +0000 Subject: [PATCH 1/6] 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 2/6] 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 3/6] 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 4/6] 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 5/6] 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 6/6] 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);