From 9ce672e4e945643535e8c7fb8168b7b9e5d3995f Mon Sep 17 00:00:00 2001 From: Joe Da Silva Date: Sun, 20 Oct 2024 16:38:00 -0700 Subject: [PATCH] Follow-up on removing xmalloc by returning NULLs Took a closer look at src directory files blur, convmask, hopfield, lambda, threshold, weights and followed-up with NULL returns for malloc() if no more memory available. Also looked at adding static for functions not called outside of those files. You'll notice the *.h files are a little smaller. gimp-plugin/main-gimp and src/image need similar fixes but there are other parts that need further investigation. In summary, took care of the easier stuff first :-P --- src/blur.c | 22 +++++++++++------- src/convmask.c | 29 ++++++++++++----------- src/convmask.h | 11 +++++---- src/hopfield.c | 26 ++++++++++++++------- src/hopfield.h | 10 ++++---- src/lambda.c | 62 +++++++++++++++++++++++++++++++++++-------------- src/lambda.h | 4 ---- src/threshold.c | 1 - src/threshold.h | 4 ++-- src/weights.c | 45 +++++++++++++++++------------------ src/weights.h | 9 +++---- 11 files changed, 129 insertions(+), 94 deletions(-) diff --git a/src/blur.c b/src/blur.c index 2587514..edc2397 100644 --- a/src/blur.c +++ b/src/blur.c @@ -19,7 +19,6 @@ */ #include -#include #include "blur.h" #ifndef SQR @@ -33,7 +32,7 @@ typedef struct } point_t; /* Return the integral of sqrt(radius^2 - z^2) for z = 0 to x */ -static double circle_integral (double x, double radius) { +static double circle_integral(double x, double radius) { double sin, sq_diff; if (radius < 1e-6) { return (0.0); @@ -54,7 +53,7 @@ static double circle_integral (double x, double radius) { } } -static double circle_intensity (int x, int y, double radius) { +static double circle_intensity(int x, int y, double radius) { double xlo, xhi, ylo, yhi, xc1, xc2; double symmetry_factor; @@ -101,7 +100,8 @@ convmask_t* blur_create_defocus(convmask_t* blur, double radius) { double val; r = (int)(radius + 0.5); - convmask_create(blur, r); //memfull? + if (!(convmask_create(blur, r))) + return NULL; if (r < 1) { convmask_set(blur, 0, 0, 1.0); @@ -109,7 +109,7 @@ convmask_t* blur_create_defocus(convmask_t* blur, double radius) { } else { for (i = 0; i <= r; i++) { for (j = 0; j <= r; j++) { - val = (double)circle_intensity(i, j, radius); + val = circle_intensity(i, j, radius); if (val < 0.0) val = 0.0; convmask_set_circle(blur, i, j, val); } @@ -126,7 +126,8 @@ convmask_t* blur_create_gauss(convmask_t* blur, double variance) { int i, j, radius; if (variance < 1e-6) { - convmask_create(blur, 0); //memfull? + if (!(convmask_create(blur, 0))) + return NULL; convmask_set(blur, 0, 0, 1.0); return blur; } else { @@ -135,7 +136,8 @@ convmask_t* blur_create_gauss(convmask_t* blur, double variance) { var = variance; epsilon = sqrt(-2.0 * log(1e-2)); radius = (int)(var * epsilon + 0.5); - convmask_create(blur, radius); //memfull? + if (!(convmask_create(blur, radius))) + return NULL; var *= var * 2.0; mult = var * M_PI; for (i = 0; i <= radius; i++) { @@ -258,12 +260,14 @@ convmask_t* blur_create_motion(convmask_t* blur, double radius, double angle) { point_t coords_line[4]; if (radius < 1e-4) { - convmask_create(blur, 0.01); //memfull? + if (!(convmask_create(blur, 0.01))) + return NULL; convmask_set(blur, 0, 0, 1.0); return blur; } else { r = (int)(radius + 1.0); - convmask_create(blur, r); //memfull? + if (!(convmask_create(blur, r))) + return NULL; make_coords_line(coords_line, radius, angle); for (i = -r; i <= r; i++) { for (j = -r; j <= r; j++) { diff --git a/src/convmask.c b/src/convmask.c index cd05d6f..3371427 100644 --- a/src/convmask.c +++ b/src/convmask.c @@ -17,9 +17,12 @@ * */ -#include #include "convmask.h" +static double convmask_get_0(convmask_t* convmask, int i, int j) { + return ((abs(i) <= convmask->radius && abs(j) <= convmask->radius) ? convmask_get(convmask, i, j) : 0.0); +} + convmask_t* convmask_create(convmask_t* convmask, int radius) { convmask->radius = radius; radius *= 2; @@ -99,17 +102,6 @@ convmask_t* convmask_normalize(convmask_t* convmask) { return convmask; } -void convmask_print(convmask_t* convmask, FILE* file) { - int i, j; - fprintf(file, "%s\n", "CONVMASK:"); - for (i = -convmask->radius; i <= convmask->radius; i++) { - for (j = -convmask->radius; j <= convmask->radius; j++) { - fprintf(file, "%1.4f ", (float)convmask_get(convmask, j, i)); - } - fprintf(file, "\n"); - } -} - void convmask_set(convmask_t* convmask, int i, int j, double value) { convmask->coef[j*convmask->r21 + convmask->speeder + i] = value; } @@ -118,6 +110,15 @@ double convmask_get(convmask_t* convmask, int i, int j) { return (convmask->coef[j*convmask->r21 + convmask->speeder + i]); } -double convmask_get_0(convmask_t* convmask, int i, int j) { - return ((abs(i) <= convmask->radius && abs(j) <= convmask->radius) ? convmask_get(convmask, i, j) : 0.0); +#if defined(NDEBUG) +void convmask_print(convmask_t* convmask, FILE* file) { + int i, j; + fprintf(file, "CONVMASK:\n"); + for (i = -convmask->radius; i <= convmask->radius; i++) { + for (j = -convmask->radius; j <= convmask->radius; j++) { + fprintf(file, " %1.4f", (float)convmask_get(convmask, j, i)); + } + fprintf(file, "\n"); + } } +#endif diff --git a/src/convmask.h b/src/convmask.h index 178ebc8..cc3a632 100644 --- a/src/convmask.h +++ b/src/convmask.h @@ -20,7 +20,6 @@ #ifndef _CONVMASK_H #define _CONVMASK_H -#include #include "compiler.h" C_DECL_BEGIN @@ -39,13 +38,15 @@ convmask_t* convmask_create(convmask_t* convmask, int radius); void convmask_destroy(convmask_t* convmask); convmask_t* convmask_normalize(convmask_t* convmask); convmask_t* convmask_convolve(convmask_t* ct, convmask_t* c1, convmask_t* c2); +void convmask_set(convmask_t* convmask, int i, int j, double value); +double convmask_get(convmask_t* convmask, int i, int j); void convmask_set_circle(convmask_t* convmask, int i, int j, double value); +#if defined(NDEBUG) +#include +#include void convmask_print(convmask_t* convmask, FILE* file); - -void convmask_set(convmask_t* convmask, int i, int j, double value); -double convmask_get(convmask_t* convmask, int i, int j); -double convmask_get_0(convmask_t* convmask, int i, int j); +#endif C_DECL_END diff --git a/src/hopfield.c b/src/hopfield.c index 059592a..b06c43d 100644 --- a/src/hopfield.c +++ b/src/hopfield.c @@ -17,7 +17,6 @@ * */ -#include #include "hopfield.h" #define hardlim(x) ((x)>=0?1:-1) @@ -362,26 +361,35 @@ static double hopfield_iteration_mirror_lambda(hopfield_t* hopfield) { return Sum; } -/* Public functions */ - -hopfield_t* hopfield_create_mirror(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld) { +static hopfield_t* hopfield_create_mirror(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld) { hopfield->image = image; hopfield->mirror = 1; - weights_create(&(hopfield->weights), convmask); - threshold_create_mirror(&(hopfield->threshold), convmask, image); + if (!(weights_create(&(hopfield->weights), convmask))) + return NULL; + if (!(threshold_create_mirror(&(hopfield->threshold), convmask, image))) { + weights_destroy(&(hopfield->weights)); + return NULL; + } hopfield->lambdafld = lambdafld; return hopfield; } -hopfield_t* hopfield_create_period(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld) { +static hopfield_t* hopfield_create_period(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld) { hopfield->image = image; hopfield->mirror = 0; - weights_create(&(hopfield->weights), convmask); - threshold_create_period(&(hopfield->threshold), convmask, image); + if (!(weights_create(&(hopfield->weights), convmask))) + return NULL; + if (!(threshold_create_mirror(&(hopfield->threshold), convmask, image))) { + weights_destroy(&(hopfield->weights)); + return NULL; + } + hopfield->lambdafld = lambdafld; hopfield->lambdafld = lambdafld; return hopfield; } +/* Public functions */ + hopfield_t* hopfield_create(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld) { if (hopfield->mirror) return hopfield_create_mirror(hopfield, convmask, image, lambdafld); else return hopfield_create_period(hopfield, convmask, image, lambdafld); diff --git a/src/hopfield.h b/src/hopfield.h index dfbf71b..ec5acb3 100644 --- a/src/hopfield.h +++ b/src/hopfield.h @@ -30,16 +30,14 @@ C_DECL_BEGIN typedef struct { - int mirror; + int mirror; image_t *image; - weights_t weights; - double lambda; + weights_t weights; + double lambda; lambda_t *lambdafld; - threshold_t threshold; + threshold_t threshold; } hopfield_t; -hopfield_t* hopfield_create_mirror(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld); -hopfield_t* hopfield_create_period(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld); hopfield_t* hopfield_create(hopfield_t* hopfield, convmask_t* convmask, image_t* image, lambda_t* lambdafld); void hopfield_set_mirror(hopfield_t* hopfield, int mirror); void hopfield_destroy(hopfield_t* hopfield); diff --git a/src/lambda.c b/src/lambda.c index 3cf7271..dd7787a 100644 --- a/src/lambda.c +++ b/src/lambda.c @@ -17,7 +17,6 @@ * */ -#include #include "lambda.h" static void get_variance_mirror(image_t* variance, image_t* img, double* pmin, double* pmax, int winsize) { @@ -121,7 +120,7 @@ void lambda_set_nl(lambda_t* lambda, int nl) { lambda->nl = nl; } -lambda_t* lambda_calculate_period(lambda_t* lambda, image_t* image) { +static lambda_t* lambda_calculate_period(lambda_t* lambda, image_t* image) { image_t imgenh, *imgcal; image_t variance; double minvar, maxvar; @@ -129,13 +128,20 @@ lambda_t* lambda_calculate_period(lambda_t* lambda, image_t* image) { int i, size; if (lambda->filter) { - imgcal = image_create_copyparam(&imgenh, image); - image_convolve_period(imgcal, image, lambda->filter); + if (!(imgcal = image_create_copyparam(&imgenh, image))) + return NULL; + if (!(image_convolve_period(imgcal, image, lambda->filter))) { + image_destroy(imgcal); + return NULL; + } } else { imgcal = image; } - image_create_copyparam(&variance, imgcal); + if (!(image_create_copyparam(&variance, imgcal))) { + if (imgcal == &imgenh) image_destroy(imgcal); + return NULL; + } get_variance_period(&variance, imgcal, &minvar, &maxvar, lambda->winsize); @@ -155,7 +161,7 @@ lambda_t* lambda_calculate_period(lambda_t* lambda, image_t* image) { return lambda; } -lambda_t* lambda_calculate_period_nl(lambda_t* lambda, image_t* image) { +static lambda_t* lambda_calculate_period_nl(lambda_t* lambda, image_t* image) { image_t imgenh, *imgcal; image_t variance; double minvar, maxvar; @@ -163,13 +169,20 @@ lambda_t* lambda_calculate_period_nl(lambda_t* lambda, image_t* image) { int i, size; if (lambda->filter) { - imgcal = image_create_copyparam(&imgenh, image); - image_convolve_period(imgcal, image, lambda->filter); + if (!(imgcal = image_create_copyparam(&imgenh, image))) + return NULL; + if (!(image_convolve_period(imgcal, image, lambda->filter))) { + image_destroy(imgcal); + return NULL; + } } else { imgcal = image; } - image_create_copyparam(&variance, imgcal); + if (!(image_create_copyparam(&variance, imgcal))) { + if (imgcal == &imgenh) image_destroy(imgcal); + return NULL; + } get_variance_period(&variance, imgcal, &minvar, &maxvar, lambda->winsize); @@ -188,8 +201,7 @@ lambda_t* lambda_calculate_period_nl(lambda_t* lambda, image_t* image) { return lambda; } - -lambda_t* lambda_calculate_mirror(lambda_t* lambda, image_t* image) { +static lambda_t* lambda_calculate_mirror(lambda_t* lambda, image_t* image) { image_t imgenh, *imgcal; image_t variance; double minvar, maxvar; @@ -197,13 +209,20 @@ lambda_t* lambda_calculate_mirror(lambda_t* lambda, image_t* image) { int i, size; if (lambda->filter) { - imgcal = image_create_copyparam(&imgenh, image); - image_convolve_mirror(imgcal, image, lambda->filter); + if (!(imgcal = image_create_copyparam(&imgenh, image))) + return NULL; + if (!(image_convolve_period(imgcal, image, lambda->filter))) { + image_destroy(imgcal); + return NULL; + } } else { imgcal = image; } - image_create_copyparam(&variance, imgcal); + if (!(image_create_copyparam(&variance, imgcal))) { + if (imgcal == &imgenh) image_destroy(imgcal); + return NULL; + } get_variance_mirror(&variance, imgcal, &minvar, &maxvar, lambda->winsize); @@ -223,7 +242,7 @@ lambda_t* lambda_calculate_mirror(lambda_t* lambda, image_t* image) { return lambda; } -lambda_t* lambda_calculate_mirror_nl(lambda_t* lambda, image_t* image) { +static lambda_t* lambda_calculate_mirror_nl(lambda_t* lambda, image_t* image) { image_t imgenh, *imgcal; image_t variance; double minvar, maxvar; @@ -231,13 +250,20 @@ lambda_t* lambda_calculate_mirror_nl(lambda_t* lambda, image_t* image) { int i, size; if (lambda->filter) { - imgcal = image_create_copyparam(&imgenh, image); - image_convolve_mirror(imgcal, image, lambda->filter); + if (!(imgcal = image_create_copyparam(&imgenh, image))) + return NULL; + if (!(image_convolve_period(imgcal, image, lambda->filter))) { + image_destroy(imgcal); + return NULL; + } } else { imgcal = image; } - image_create_copyparam(&variance, imgcal); + if (!(image_create_copyparam(&variance, imgcal))) { + if (imgcal == &imgenh) image_destroy(imgcal); + return NULL; + } get_variance_mirror(&variance, imgcal, &minvar, &maxvar, lambda->winsize); diff --git a/src/lambda.h b/src/lambda.h index 852aaf1..2336a06 100644 --- a/src/lambda.h +++ b/src/lambda.h @@ -41,10 +41,6 @@ typedef struct { lambda_t* lambda_create(lambda_t* lambda, int x, int y, double minlambda, int winsize, convmask_t* filter); void lambda_destroy(lambda_t* lambda); -lambda_t* lambda_calculate_period(lambda_t* lambda, image_t* image); -lambda_t* lambda_calculate_period_nl(lambda_t* lambda, image_t* image); -lambda_t* lambda_calculate_mirror(lambda_t* lambda, image_t* image); -lambda_t* lambda_calculate_mirror_nl(lambda_t* lambda, image_t* image); lambda_t* lambda_calculate(lambda_t* lambda, image_t* image); void lambda_set_mirror(lambda_t* lambda, int mirror); diff --git a/src/threshold.c b/src/threshold.c index 104daf6..87451ab 100644 --- a/src/threshold.c +++ b/src/threshold.c @@ -17,7 +17,6 @@ * */ -#include #include "threshold.h" threshold_t* threshold_create_mirror(threshold_t* threshold, convmask_t* convmask, image_t* image) { diff --git a/src/threshold.h b/src/threshold.h index ebb2486..61c4c24 100644 --- a/src/threshold.h +++ b/src/threshold.h @@ -27,8 +27,8 @@ C_DECL_BEGIN typedef struct { - int x; - int y; + int x; + int y; double *data; } threshold_t; diff --git a/src/weights.c b/src/weights.c index 6c743f7..be4a490 100644 --- a/src/weights.c +++ b/src/weights.c @@ -17,9 +17,13 @@ * */ -#include #include "weights.h" +static weights_set(weights_t* weights, int x, int y, double value) { + weights->w[(weights->r2 + y) * weights->size + (weights->r2 + x)] = value; + weights->w[weights->stride + y * weights->size + x] = value; +} + weights_t* weights_create(weights_t* weights, convmask_t* convmask) { int r, r2, i, j, k, l; int rxnz, rynz; @@ -32,7 +36,7 @@ weights_t* weights_create(weights_t* weights, convmask_t* convmask) { weights->size = size = 2*r2 + 1; weights->stride = r2 * (size + 1); if (!(weights->w = (double*)malloc(sizeof(double) * size * size))) - return NULL; + return NULL; /* memory full */ for (i = 0; i <= r2; i++) { for (j = 0; j <= r2; j++) { @@ -43,17 +47,17 @@ weights_t* weights_create(weights_t* weights, convmask_t* convmask) { } } if (fabs(s) > 1e-6) { - if (i > rxnz) rxnz = i; - if (j > rynz) rynz = j; - } - weights_set(weights, i, j, s); - weights_set(weights, -i, -j, s); + if (i > rxnz) rxnz = i; + if (j > rynz) rynz = j; + } + weights_set(weights, i, j, s); + weights_set(weights, -i, -j, s); - s = 0.0; - for (k = -r; k <= r-i; k++) { - for (l = -r; l <= r-j; l++) { - s -= convmask_get(convmask, k, l+j) * convmask_get(convmask, k+i, l); - } + s = 0.0; + for (k = -r; k <= r-i; k++) { + for (l = -r; l <= r-j; l++) { + s -= convmask_get(convmask, k, l+j) * convmask_get(convmask, k+i, l); + } } if (fabs(s) > 1e-6) { if (i > rxnz) rxnz = i; @@ -74,10 +78,15 @@ void weights_destroy(weights_t* weights) { free(weights->w); } +double weights_get(weights_t* weights, int x, int y) { + return weights->w[(weights->r2 + y) * weights->size + (weights->r2 + x)]; +} + +#if defined(NDEBUG) void weights_print(weights_t* weights, FILE* file) { int i, j; - fprintf(file, "%s (rxnz, rynz)=(%d,%d)\n", "WEIGHTS: ", weights->rxnz, weights->rynz); + fprintf(file, "WEIGHTS: (rxnz, rynz)=(%d,%d)\n", weights->rxnz, weights->rynz); for (j = -weights->r2; j <= weights->r2; j++) { for (i = -weights->r2; i <= weights->r2; i++) { fprintf(file, " %3.3e", (float)weights_get(weights, i, j)); @@ -85,12 +94,4 @@ void weights_print(weights_t* weights, FILE* file) { fprintf(file, "\n"); } } - -double weights_get(weights_t* weights, int x, int y) { - return weights->w[(weights->r2 + y) * weights->size + (weights->r2 + x)]; -} - -void weights_set(weights_t* weights, int x, int y, double value) { - weights->w[(weights->r2 + y) * weights->size + (weights->r2 + x)] = value; - weights->w[weights->stride + y * weights->size + x] = value; -} +#endif diff --git a/src/weights.h b/src/weights.h index 3486869..a0c5644 100644 --- a/src/weights.h +++ b/src/weights.h @@ -20,7 +20,6 @@ #ifndef _WEIGHTS_H #define _WEIGHTS_H -#include #include "compiler.h" #include "convmask.h" @@ -36,11 +35,13 @@ typedef struct { weights_t* weights_create(weights_t* weights, convmask_t* convmask); void weights_destroy(weights_t* weights); +double weights_get(weights_t* weights, int x, int y); +#if defined(NDEBUG) +#include +#include void weights_print(weights_t* weights, FILE* file); - -double weights_get(weights_t* weights, int x, int y); -void weights_set(weights_t* weights, int x, int y, double value); +#endif C_DECL_END