Add genericised image handling and implement in DoRxdPwr

This commit is contained in:
Gareth Evans
2017-02-01 23:44:00 +00:00
parent 94aae7544b
commit 93aa2e472a
7 changed files with 357 additions and 37 deletions

View File

@@ -4,11 +4,11 @@ CC = gcc
CXX = g++ CXX = g++
CFLAGS = -Wall -O3 -s -ffast-math CFLAGS = -Wall -O3 -s -ffast-math
CXXFLAGS = -Wall -O3 -s -ffast-math CXXFLAGS = -Wall -O3 -s -ffast-math
LIBS = -lm -lpthread LIBS = -lm -lpthread -ldl
VPATH = models VPATH = models
objects = main.o cost.o ecc33.o ericsson.o fspl.o hata.o itwom3.0.o \ 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_MAJOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d . -f 1)
GCC_MINOR := $(shell $(CXX) -dumpversion 2>&1 | cut -d . -f 2) 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 \ 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 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 \ 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 itwom3.0.hh sui.hh pel.hh

61
image-ppm.cc Normal file
View File

@@ -0,0 +1,61 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#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;
}

13
image-ppm.hh Normal file
View File

@@ -0,0 +1,13 @@
#ifndef _IMAGE_PPM_HH
#define _IMAGE_PPM_HH
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
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

199
image.cc Normal file
View File

@@ -0,0 +1,199 @@
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <dlfcn.h>
#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;
}

42
image.hh Normal file
View File

@@ -0,0 +1,42 @@
#ifndef _IMAGE_HH_
#define _IMAGE_HH_
#include <stdint.h>
#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

View File

@@ -35,6 +35,7 @@ double version = 2.95;
#include "models/itwom3.0.hh" #include "models/itwom3.0.hh"
#include "models/los.hh" #include "models/los.hh"
#include "models/pel.hh" #include "models/pel.hh"
#include "image.hh"
int MAXPAGES = 64; int MAXPAGES = 64;
int ARRAYSIZE = 76810;//76810; 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) { if (strcmp(argv[x], "-rt") == 0) {
z = x + 1; z = x + 1;

View File

@@ -14,6 +14,7 @@
#include "models/hata.hh" #include "models/hata.hh"
#include "models/itwom3.0.hh" #include "models/itwom3.0.hh"
#include "models/sui.hh" #include "models/sui.hh"
#include "image.hh"
void DoPathLoss(char *filename, unsigned char geo, unsigned char kml, void DoPathLoss(char *filename, unsigned char geo, unsigned char kml,
unsigned char ngs, struct site *xmtr, unsigned char txsites) 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; int indx, x, y, z = 1, x0 = 0, y0 = 0, dBm, match;
double conversion, one_over_gamma, lat, lon, minwest; double conversion, one_over_gamma, lat, lon, minwest;
FILE *fd; 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; one_over_gamma = 1.0 / GAMMA;
conversion = conversion =
@@ -544,24 +552,11 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml,
filename[strlen(filename) - 4] = 0; /* Remove .qth */ filename[strlen(filename) - 4] = 0; /* Remove .qth */
} }
y = strlen(filename); if(image_get_filename(&ctx,mapfile,sizeof(mapfile),filename) != 0){
fprintf(stderr,"Error creating file name\n");
if (y > 4) { exit(1);
if (filename[y - 1] == 'm' && filename[y - 2] == 'p'
&& filename[y - 3] == 'p' && filename[y - 4] == '.')
y -= 4;
} }
for (x = 0; x < y; x++) {
mapfile[x] = filename[x];
}
mapfile[x] = '.';
mapfile[x + 1] = 'p';
mapfile[x + 2] = 'p';
mapfile[x + 3] = 'm';
mapfile[x + 4] = 0;
fd = fopen(mapfile,"wb"); fd = fopen(mapfile,"wb");
} else { } else {
@@ -583,7 +578,6 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml,
east = (minwest < 180.0 ? -minwest : 360.0 - min_west); east = (minwest < 180.0 ? -minwest : 360.0 - min_west);
west = (double)(max_west < 180 ? -max_west : 360 - max_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) { if (debug) {
fprintf(stderr, "\nWriting \"%s\" (%ux%u pixmap image)...\n", fprintf(stderr, "\nWriting \"%s\" (%ux%u pixmap image)...\n",
(filename != NULL ? mapfile : "to stdout"), width, (kml ? height : height)); (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 if (red >= 180 && green <= 75
&& blue <= 75 && dBm != 0) && blue <= 75 && dBm != 0)
fprintf(fd, "%c%c%c", 255 ^ red, ADD_PIXEL(&ctx, 255 ^ red,
255 ^ green, 255 ^ green,
255 ^ blue); 255 ^ blue);
else else
fprintf(fd, "%c%c%c", 255, 0, ADD_PIXEL(&ctx, 255, 0,
0); 0);
cityorcounty = 1; cityorcounty = 1;
@@ -664,7 +658,7 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml,
else if (mask & 4) { else if (mask & 4) {
/* County Boundaries: Black */ /* County Boundaries: Black */
fprintf(fd, "%c%c%c", 0, 0, 0); ADD_PIXEL(&ctx, 0, 0, 0);
cityorcounty = 1; cityorcounty = 1;
} }
@@ -672,15 +666,14 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml,
if (contour_threshold != 0 if (contour_threshold != 0
&& dBm < contour_threshold) { && dBm < contour_threshold) {
if (ngs) /* No terrain */ if (ngs) /* No terrain */
fprintf(fd, "%c%c%c", ADD_PIXEL(&ctx,
255, 255, 255); 255, 255, 255);
else { else {
/* Display land or sea elevation */ /* Display land or sea elevation */
if (dem[indx]. if (dem[indx].
data[x0][y0] == 0) data[x0][y0] == 0)
fprintf(fd, ADD_PIXEL(&ctx,
"%c%c%c",
0, 0, 0, 0,
170); 170);
else { else {
@@ -688,8 +681,7 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml,
(unsigned) (unsigned)
(0.5 + (0.5 +
pow((double)(dem[indx].data[x0][y0] - min_elevation), one_over_gamma) * conversion); pow((double)(dem[indx].data[x0][y0] - min_elevation), one_over_gamma) * conversion);
fprintf(fd, ADD_PIXEL(&ctx,
"%c%c%c",
terrain, terrain,
terrain, terrain,
terrain); terrain);
@@ -702,15 +694,14 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml,
if (red != 0 || green != 0 if (red != 0 || green != 0
|| blue != 0) || blue != 0)
fprintf(fd, "%c%c%c", ADD_PIXEL(&ctx,
red, green, red, green,
blue); blue);
else { /* terrain / sea-level */ else { /* terrain / sea-level */
if (ngs) if (ngs)
fprintf(fd, ADD_PIXEL(&ctx,
"%c%c%c",
255, 255,
255, 255,
255); // WHITE 255); // WHITE
@@ -718,9 +709,7 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml,
if (dem[indx]. if (dem[indx].
data[x0][y0] data[x0][y0]
== 0) == 0)
fprintf ADD_PIXEL(&ctx,
(fd,
"%c%c%c",
0, 0,
0, 0,
170); // BLUE 170); // BLUE
@@ -733,9 +722,7 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml,
+ +
pow pow
((double)(dem[indx].data[x0][y0] - min_elevation), one_over_gamma) * conversion); ((double)(dem[indx].data[x0][y0] - min_elevation), one_over_gamma) * conversion);
fprintf ADD_PIXEL(&ctx,
(fd,
"%c%c%c",
terrain, terrain,
terrain, 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 should never get here, but if */
/* we do, display the region as black */ /* 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); fflush(fd);
if( filename != NULL ) { if( filename != NULL ) {